Skip to content

Commit

Permalink
feat/CA Gift Donut demo app (#791)
Browse files Browse the repository at this point in the history
* Implementing demo app design

* add useGiftDonut hook

* chore:cleanup

* chores:cleanup and refactor

* fix:chain switch and error toast

* set $1 as donut price

* chores: fix style and text

* fix: network selection

* display toast success on completion
  • Loading branch information
KannuSingh authored Jan 7, 2025
1 parent 06020f3 commit 7dee4ab
Show file tree
Hide file tree
Showing 48 changed files with 2,336 additions and 1,243 deletions.
28 changes: 15 additions & 13 deletions advanced/dapps/chain-abstraction-demo/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
--popover: 0 0% 100%;
--popover-foreground: 240 10% 3.9%;
--primary: 240 5.9% 10%;
--primary-foreground: 0 0% 98%;
--primary-foreground: 0 0% 15%;
--secondary: 240 4.8% 95.9%;
--secondary-foreground: 240 5.9% 10%;
--muted: 240 4.8% 95.9%;
--muted-foreground: 240 3.8% 46.1%;
--accent: 240 4.8% 95.9%;
--accent-foreground: 240 5.9% 10%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 0 0% 98%;
--destructive-foreground: 0 0% 15%;
--border: 240 5.9% 90%;
--input: 240 5.9% 90%;
--ring: 240 10% 3.9%;
Expand All @@ -51,25 +51,27 @@
--radius: 0.5rem;
}
.dark {
--background: 240 10% 3.9%;
--foreground: 0 0% 98%;
--background: 0 0% 13%;
--foreground: 0 0% 15%;
--invert: 0 0% 13%;
--card: 240 10% 3.9%;
--card-foreground: 0 0% 98%;
--card-foreground: 0 0% 15%;
--popover: 240 10% 3.9%;
--popover-foreground: 0 0% 98%;
--primary: 0 0% 98%;
--primary-foreground: 240 5.9% 10%;
--secondary: 240 3.7% 15.9%;
--secondary-foreground: 0 0% 98%;
--popover-foreground: 0 0% 15%;
--primary: 0 0% 100%;
--primary-foreground: 0 0% 15%;
--secondary: 0 0% 60%;
--secondary-foreground: 0 0% 15%;
--tertiary-foreground: rgba(54, 54, 54, 1);
--muted: 240 3.7% 15.9%;
--muted-foreground: 240 5% 64.9%;
--accent: 240 3.7% 15.9%;
--accent-foreground: 0 0% 98%;
--accent-foreground: 0 0% 15%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 0 0% 98%;
--destructive-foreground: 0 0% 15%;
--border: 240 3.7% 15.9%;
--input: 240 3.7% 15.9%;
--ring: 240 4.9% 83.9%;
--ring: 207, 93%, 49%, 0.2;
--chart-1: 220 70% 50%;
--chart-2: 160 60% 45%;
--chart-3: 30 80% 55%;
Expand Down
64 changes: 64 additions & 0 deletions advanced/dapps/chain-abstraction-demo/app/hooks/useGiftDonut.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"use client";
import { config } from "@/config";
import { tokenAddresses } from "@/consts/tokens";
import { Network, Token } from "@/data/EIP155Data";
import { toast } from "sonner";
import { erc20Abi, Hex } from "viem";
import { getAccount, getWalletClient } from "wagmi/actions";

export default function useGiftDonut() {

const giftDonutAsync = async (
to: Hex,
donutCount: number,
token: Token,
network: Network,
) => {
try {
const client = await getWalletClient(config, {
chainId: network.chainId,
});
const account = getAccount(config);
const connectedChainId = account.chain?.id;
if (!connectedChainId) {
throw new Error("Chain undefined");
}

if (connectedChainId !== network.chainId) {
throw new Error("Please switch chain, connected chain does not match network");
}

const tokenName = token.name;
const tokenChainMapping = tokenAddresses[tokenName];
if (!tokenChainMapping) {
throw new Error("Token not supported");
}

const contract = tokenChainMapping[connectedChainId];
if (!contract) {
throw new Error("Cant send on specified chain");
}
const tokenAmount = donutCount * 1 * 10 ** 6;
console.log({ to, tokenAmount, contract });

const tx = await client.writeContract({
abi: erc20Abi,
address: contract,
functionName: "transfer",
args: [to, BigInt(tokenAmount)],
});
toast.success(`Transaction sent with hash: ${tx}`);
return tx;
} catch (e) {

if (e instanceof Error) {
toast.error(e.message)
}
else {
toast.error("Error sending gift donut");
}
console.error(e);
}
};
return { giftDonutAsync };
}
28 changes: 0 additions & 28 deletions advanced/dapps/chain-abstraction-demo/app/hooks/useSendUsdc.tsx

This file was deleted.

20 changes: 8 additions & 12 deletions advanced/dapps/chain-abstraction-demo/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import type { Metadata } from "next";
import { Inter as FontSans } from "next/font/google";
import { Inter } from "next/font/google";
import "./globals.css";
import { cookieToInitialState } from "wagmi";
import { config } from "@/config";
import AppKitProvider from "@/context";
import { Toaster } from "@/components/ui/toaster";
import { headers } from "next/headers";
import { cn } from "@/lib/utils";
import { ThemeProvider } from "@/components/theme-provider";
import { Toaster } from "@/components/ui/sonner";

const fontSans = FontSans({
const inter = Inter({
subsets: ["latin"],
variable: "--font-sans",
display: "swap",
});

export const metadata: Metadata = {
Expand All @@ -24,14 +22,14 @@ export default function RootLayout({
}: Readonly<{
children: React.ReactNode;
}>) {
const cookies = headers().get('cookie')
const cookies = headers().get("cookie");
return (
<html lang="en" suppressHydrationWarning>
<head />
<body
className={cn(
"min-h-screen bg-background font-sans antialiased pt-12 pb-24 mt-12 overflow-y-auto",
fontSans.variable
"min-h-screen bg-background font-sans antialiased overflow-y-auto",
inter.className,
)}
>
<AppKitProvider cookies={cookies}>
Expand All @@ -40,9 +38,7 @@ export default function RootLayout({
defaultTheme="dark"
disableTransitionOnChange
>
<div className="flex items-center justify-center min-h-screen">
{children}
</div>
<div className="flex justify-center min-h-screen">{children}</div>
</ThemeProvider>
</AppKitProvider>
<Toaster />
Expand Down
61 changes: 47 additions & 14 deletions advanced/dapps/chain-abstraction-demo/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
'use client'
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import Transfer from "./transfer";
"use client";

import * as React from "react";
import Image from "next/image";
import { ConnectWalletButton } from "@/components/ConnectWalletButton";
import { useAppKitAccount } from "@reown/appkit/react";
import Navbar from "@/components/Navbar";
import { GiftDonutModalTrigger } from "@/components/GiftDonutModalTrigger";

export default function Home() {
const { status, address } = useAppKitAccount();

return (
<main>
<div>
<Card >
<CardHeader>
<w3m-button />
</CardHeader>
<CardContent>
<Transfer/>
</CardContent>
</Card>
<div className="sm:w-1/2 flex flex-col sm:mx-10">
<Navbar />
<div className="flex flex-col justify-center gap-4 mt-8">
<div className="flex items-center justify-center h-64 relative ">
<Image
src="/donut-cover.png"
alt="Gift Donut"
className="object-cover"
fill={true}
/>
</div>
<div className="flex flex-col gap-5">
<div className="flex flex-col text-left">
<p className=" font-bold text-primary">Donut #1</p>
<p className=" text-secondary">Lorem ipsum dolor sit...</p>
</div>
<div className="flex justify-between items-center w-full">
<div className="flex flex-col text-left">
<p className=" text-secondary">Price</p>
<p className=" font-bold text-primary">$1.00</p>
</div>
{status === "connected" || address ? (
<div>
<GiftDonutModalTrigger
triggerText="Gift Donut"
initialView="Checkout"
className="bg-blue-500 hover:bg-blue-700 text-invert"
/>
</div>
) : (
<div>
<ConnectWalletButton />
</div>
)}
</div>
</div>
</div>
</main>
</div>
);
}
Loading

0 comments on commit 7dee4ab

Please sign in to comment.