From 558efc8d27d2f2566b35b8bc546c89e9c9df2eee Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Fri, 18 Feb 2022 10:59:25 +0100 Subject: [PATCH] refactor: simplify `eth_sendTransaction` for ethers example --- dapps/react-dapp-v2-with-ethers/src/App.tsx | 34 +++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/dapps/react-dapp-v2-with-ethers/src/App.tsx b/dapps/react-dapp-v2-with-ethers/src/App.tsx index b7bb64e..637a2b4 100644 --- a/dapps/react-dapp-v2-with-ethers/src/App.tsx +++ b/dapps/react-dapp-v2-with-ethers/src/App.tsx @@ -27,7 +27,7 @@ import { SToggleContainer, } from "./components/app"; import { useWalletConnectClient } from "./contexts/ClientContext"; -import { utils } from "ethers"; +import { BigNumber, utils } from "ethers"; interface IFormattedRpcResponse { method: string; @@ -89,6 +89,36 @@ export default function App() { await ping(); }; + const testSendTransaction: () => Promise = async () => { + if (!web3Provider) { + throw new Error("web3Provider not connected"); + } + + const { chainId } = await web3Provider.getNetwork(); + const [address] = await web3Provider.listAccounts(); + const balance = await web3Provider.getBalance(address); + + const tx = await formatTestTransaction("eip155:" + chainId + ":" + address); + + if (balance.lt(BigNumber.from(tx.gasPrice).mul(tx.gasLimit))) { + return { + method: "eth_sendTransaction", + address, + valid: false, + result: "Insufficient funds for intrinsic transaction cost", + }; + } + + const txHash = await web3Provider.send("eth_sendTransaction", [tx]); + + return { + method: "eth_sendTransaction", + address, + valid: true, + result: txHash, + }; + }; + const testSignTransaction: () => Promise = async () => { if (!web3Provider) { throw new Error("web3Provider not connected"); @@ -214,7 +244,7 @@ export default function App() { }; return [ - // { method: "eth_sendTransaction", callback: onSendTransaction }, + { method: "eth_sendTransaction", callback: wrapRpcRequest(testSendTransaction) }, { method: "eth_signTransaction", callback: wrapRpcRequest(testSignTransaction) }, { method: "personal_sign", callback: wrapRpcRequest(testSignMessage) }, { method: "eth_sign (standard)", callback: wrapRpcRequest(testEthSign) },