From 0ebf4f6145bbc610d3410cbcb5c61333b1aeead2 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Mon, 22 Apr 2024 20:31:55 -0400 Subject: [PATCH] react waas demo to use contract call for 'runSendTransaction', and remove feeOptions from this example --- examples/react/src/components/Homepage.tsx | 119 ++++++++++++--------- examples/react/src/config.ts | 60 +++++------ examples/react/src/constants/demo-abi.ts | 11 ++ 3 files changed, 111 insertions(+), 79 deletions(-) create mode 100644 examples/react/src/constants/demo-abi.ts diff --git a/examples/react/src/components/Homepage.tsx b/examples/react/src/components/Homepage.tsx index 709a0bef..af7eb126 100644 --- a/examples/react/src/components/Homepage.tsx +++ b/examples/react/src/components/Homepage.tsx @@ -43,6 +43,7 @@ import { messageToSign } from '../constants' import { formatAddress, getCheckoutSettings } from '../utils' import { sequence } from '0xsequence' import abi from '../constants/nft-abi' +import demoAbi from '../constants/demo-abi' import { ethers } from 'ethers' import { Alert, AlertProps } from './Alert' @@ -81,15 +82,15 @@ function Homepage() { localStorage.getItem('confirmationEnabled') === 'true' ) - const [pendingFeeOptionConfirmation, confirmPendingFeeOption, rejectPendingFeeOption] = useWaasFeeOptions() + // const [pendingFeeOptionConfirmation, confirmPendingFeeOption, rejectPendingFeeOption] = useWaasFeeOptions() - const [selectedFeeOptionTokenName, setSelectedFeeOptionTokenName] = React.useState() + // const [selectedFeeOptionTokenName, setSelectedFeeOptionTokenName] = React.useState() - useEffect(() => { - if (pendingFeeOptionConfirmation) { - setSelectedFeeOptionTokenName(pendingFeeOptionConfirmation.options[0].token.name) - } - }, [pendingFeeOptionConfirmation]) + // useEffect(() => { + // if (pendingFeeOptionConfirmation) { + // setSelectedFeeOptionTokenName(pendingFeeOptionConfirmation.options[0].token.name) + // } + // }, [pendingFeeOptionConfirmation]) useEffect(() => { console.log(error?.message) @@ -99,44 +100,44 @@ function Homepage() { const { indexerClient } = getNetworkConfigAndClients(chainId) - const [feeOptionBalances, setFeeOptionBalances] = React.useState<{ tokenName: string; decimals: number; balance: string }[]>([]) - - const [feeOptionAlert, setFeeOptionAlert] = React.useState(undefined) - - useEffect(() => { - checkTokenBalancesForFeeOptions() - }, [pendingFeeOptionConfirmation]) - - const checkTokenBalancesForFeeOptions = async () => { - if (pendingFeeOptionConfirmation) { - const [account] = await walletClient.getAddresses() - const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account }) - - const tokenBalances = await indexerClient.getTokenBalances({ - accountAddress: account - }) - - console.log('feeOptions', pendingFeeOptionConfirmation.options) - console.log('nativeTokenBalance', nativeTokenBalance) - console.log('tokenBalances', tokenBalances) - - const balances = pendingFeeOptionConfirmation.options.map(option => { - if (option.token.contractAddress === null) { - return { tokenName: option.token.name, decimals: option.token.decimals, balance: nativeTokenBalance.balance.balanceWei } - } else { - return { - tokenName: option.token.name, - decimals: option.token.decimals, - balance: - tokenBalances.balances.find(b => b.contractAddress.toLowerCase() === option.token.contractAddress.toLowerCase()) - ?.balance || '0' - } - } - }) - - setFeeOptionBalances(balances) - } - } + // const [feeOptionBalances, setFeeOptionBalances] = React.useState<{ tokenName: string; decimals: number; balance: string }[]>([]) + + // const [feeOptionAlert, setFeeOptionAlert] = React.useState(undefined) + + // useEffect(() => { + // checkTokenBalancesForFeeOptions() + // }, [pendingFeeOptionConfirmation]) + + // const checkTokenBalancesForFeeOptions = async () => { + // if (pendingFeeOptionConfirmation) { + // const [account] = await walletClient.getAddresses() + // const nativeTokenBalance = await indexerClient.getEtherBalance({ accountAddress: account }) + + // const tokenBalances = await indexerClient.getTokenBalances({ + // accountAddress: account + // }) + + // console.log('feeOptions', pendingFeeOptionConfirmation.options) + // console.log('nativeTokenBalance', nativeTokenBalance) + // console.log('tokenBalances', tokenBalances) + + // const balances = pendingFeeOptionConfirmation.options.map(option => { + // if (option.token.contractAddress === null) { + // return { tokenName: option.token.name, decimals: option.token.decimals, balance: nativeTokenBalance.balance.balanceWei } + // } else { + // return { + // tokenName: option.token.name, + // decimals: option.token.decimals, + // balance: + // tokenBalances.balances.find(b => b.contractAddress.toLowerCase() === option.token.contractAddress.toLowerCase()) + // ?.balance || '0' + // } + // } + // }) + + // setFeeOptionBalances(balances) + // } + // } const networkForCurrentChainId = sequence.network.allNetworks.find(n => n.chainId === chainId) @@ -206,13 +207,33 @@ function Homepage() { } const runSendTransaction = async () => { + // NOTE: commented code is how to send ETH value to the account + // if (!walletClient) { + // return + // } + // const [account] = await walletClient.getAddresses() + // sendTransaction({ to: account, value: '0', gas: null }) + + // NOTE: below is a a simple contract call. See `runMintNFT` + // on another example where you can use the wagmi `writeContract` + // method to do the same thing. if (!walletClient) { return } - const [account] = await walletClient.getAddresses() + const contractAbiInterface = new ethers.utils.Interface([ + 'function demo()' + ]) - sendTransaction({ to: account, value: '0', gas: null }) + const data = contractAbiInterface.encodeFunctionData( + 'demo', [] + ) + + sendTransaction({ + to: '0x37470dac8a0255141745906c972e414b1409b470', + data, + gas: null + }) } const runMintNFT = async () => { @@ -436,7 +457,7 @@ function Homepage() { /> - {pendingFeeOptionConfirmation && feeOptionBalances.length > 0 && ( + {/* {pendingFeeOptionConfirmation && feeOptionBalances.length > 0 && (