-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
06020f3
commit 7dee4ab
Showing
48 changed files
with
2,336 additions
and
1,243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
advanced/dapps/chain-abstraction-demo/app/hooks/useGiftDonut.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
advanced/dapps/chain-abstraction-demo/app/hooks/useSendUsdc.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.