From 154e05afa7572ae264c3608ef5c728c3bb2ab9c0 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Fri, 18 Feb 2022 10:51:35 +0100 Subject: [PATCH] refactor: use `web3Provider.listAccounts()` --- dapps/react-dapp-v2-with-ethers/src/App.tsx | 21 +++++++--------- dapps/react-dapp-v2/src/App.tsx | 2 ++ .../src/contexts/JsonRpcContext.tsx | 24 ++++++++++++------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/dapps/react-dapp-v2-with-ethers/src/App.tsx b/dapps/react-dapp-v2-with-ethers/src/App.tsx index d68008d..b7bb64e 100644 --- a/dapps/react-dapp-v2-with-ethers/src/App.tsx +++ b/dapps/react-dapp-v2-with-ethers/src/App.tsx @@ -42,7 +42,6 @@ export default function App() { const [rpcResult, setRpcResult] = useState(); const [modal, setModal] = useState(""); - const [selectedChainId, setSelectedChainId] = useState(); const closeModal = () => setModal(""); const openPingModal = () => setModal("ping"); @@ -94,8 +93,11 @@ export default function App() { if (!web3Provider) { throw new Error("web3Provider not connected"); } - const address = accounts[0]; - const tx = await formatTestTransaction(selectedChainId + ":" + address); + + const { chainId } = await web3Provider.getNetwork(); + const [address] = await web3Provider.listAccounts(); + + const tx = await formatTestTransaction("eip155:" + chainId + ":" + address); const signature = await web3Provider.send("eth_signTransaction", [tx]); return { @@ -112,7 +114,7 @@ export default function App() { } const msg = "hello world"; const hexMsg = encoding.utf8ToHex(msg, true); - const address = accounts[0]; + const [address] = await web3Provider.listAccounts(); const signature = await web3Provider.send("personal_sign", [hexMsg, address]); const valid = utils.verifyMessage(msg, signature) === address; return { @@ -129,7 +131,7 @@ export default function App() { } const msg = "hello world"; const hexMsg = encoding.utf8ToHex(msg, true); - const address = accounts[0]; + const [address] = await web3Provider.listAccounts(); const signature = await web3Provider.send("eth_sign", [address, hexMsg]); const valid = utils.verifyMessage(msg, signature) === address; return { @@ -179,7 +181,7 @@ export default function App() { const message = JSON.stringify(typedData); - const address = accounts[0]; + const [address] = await web3Provider.listAccounts(); // eth_signTypedData params const params = [address, message]; @@ -239,11 +241,6 @@ export default function App() { setLocaleStorageTestnetFlag(nextIsTestnetState); }; - const onConnect = (chainId: string) => { - setSelectedChainId(chainId); - onEnable(chainId); - }; - // Renders the appropriate model for the given request that is currently in-flight. const renderModal = () => { switch (modal) { @@ -271,7 +268,7 @@ export default function App() { {chainOptions.map(chainId => ( - + ))} diff --git a/dapps/react-dapp-v2/src/App.tsx b/dapps/react-dapp-v2/src/App.tsx index 71e4aac..84082f6 100644 --- a/dapps/react-dapp-v2/src/App.tsx +++ b/dapps/react-dapp-v2/src/App.tsx @@ -219,6 +219,8 @@ export default function App() { ); }; + console.log(accounts); + return ( diff --git a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx index 61f5fa0..df5dac0 100644 --- a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx @@ -170,14 +170,22 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea }; } - const result: string = await client!.request({ - topic: session!.topic, - chainId, - request: { - method: "eth_sendTransaction", - params: [tx], - }, - }); + let result = ""; + + try { + result = await client!.request({ + topic: session!.topic, + chainId, + request: { + method: "eth_sendTransaction", + params: [tx], + }, + }); + } catch (error) { + console.error(error); + } + + console.log(result); // format displayed result return {