diff --git a/smart-wallet/sub-accounts-demo/src/app/page.tsx b/smart-wallet/sub-accounts-demo/src/app/page.tsx index f0c9011..16c6c0f 100644 --- a/smart-wallet/sub-accounts-demo/src/app/page.tsx +++ b/smart-wallet/sub-accounts-demo/src/app/page.tsx @@ -1,7 +1,9 @@ 'use client' import { parseEther } from 'viem' -import { useAccount, useConnect, useDisconnect, useSendTransaction, useSignMessage } from 'wagmi' +import { useAccount, useConnect, useWriteContract, useDisconnect, useSendTransaction, useSignMessage } from 'wagmi' +import { WETH_ADDRESS, WETH_ABI } from '../weth' +import { useState } from 'react' function App() { const account = useAccount() @@ -9,6 +11,48 @@ function App() { const { disconnect } = useDisconnect() const { sendTransactionAsync, data } = useSendTransaction() const { signMessage, data: signData } = useSignMessage() + + // weth data and functions + const [wrapAmount, setWrapAmount] = useState('0') + const [txHash, setTxHash] = useState('') + const { writeContractAsync: wrap, isPending: isWrapping, + isSuccess: isWrapped, isError: isErrorWrapping, error: errorWrapping } = useWriteContract() + const { writeContractAsync: unwrap, isPending: isUnwrapping, isSuccess: isUnwrapped, + isError: isErrorUnwrapping, error: errorUnwrapping } = useWriteContract() + + const handleWrap = async () => { + if (!wrapAmount || account.status !== 'connected') return + + try { + const tx = await wrap({ + address: WETH_ADDRESS as `0x${string}`, + abi: WETH_ABI, + functionName: 'deposit', + value: parseEther(wrapAmount) + }) + setTxHash(tx) + } catch (error) { + console.error('Error wrapping ETH:', error) + } + } + + const handleUnwrap = async () => { + if (!wrapAmount || account.status !== 'connected') return + + try { + const tx = await unwrap({ + address: WETH_ADDRESS as `0x${string}`, + abi: WETH_ABI, + functionName: 'withdraw', + args: [parseEther(wrapAmount)] + }) + setTxHash(tx) + } catch (error) { + console.error('Error unwrapping WETH:', error) + } + } + // end weth data and functions + return ( <>
@@ -63,8 +107,34 @@ function App() {
{signData}
+ +
+

Wrap ETH to WETH and Unwrap WETH to ETH

+ setWrapAmount(e.target.value)} /> + + +
{txHash && "Wrap tx Hash: " + txHash}
+
{isWrapping && "Wrapping..."}
+
{isWrapped && "Wrapped! 🎉"}
+
{isErrorWrapping && "Error wrapping: " + errorWrapping?.message}
+
{isUnwrapping && "Unwrapping..."}
+
{isUnwrapped && "Unwrapped! 🎉"}
+
{isErrorUnwrapping && "Error unwrapping: " + errorUnwrapping?.message}
+
) } -export default App +export default App \ No newline at end of file diff --git a/smart-wallet/sub-accounts-demo/src/weth.ts b/smart-wallet/sub-accounts-demo/src/weth.ts new file mode 100644 index 0000000..9e9e3ef --- /dev/null +++ b/smart-wallet/sub-accounts-demo/src/weth.ts @@ -0,0 +1,22 @@ + +export const WETH_ADDRESS = '0x4200000000000000000000000000000000000006'; // Base Sepolia WETH +export const WETH_ABI = [ + { + constant: false, + inputs: [], + name: 'deposit', + outputs: [], + payable: true, + stateMutability: 'payable', + type: 'function', + }, + { + constant: false, + inputs: [{ name: 'wad', type: 'uint256' }], + name: 'withdraw', + outputs: [], + payable: false, + stateMutability: 'nonpayable', + type: 'function', + }, +]; \ No newline at end of file