on master help sign in
pr: address feedback
e5e005atomholford
src/lib/metamask.ts+9−1
@@ -18,8 +18,12 @@ export const metamaskSignTransaction = async (txn: EIP1559Transaction) => {
18};
18};
19
19
20const metamaskSendTransaction = async (txn: FakeSignableTx, web3: Web3) => {
20const metamaskSendTransaction = async (txn: FakeSignableTx, web3: Web3) => {
21+ /**
22+ * Depending which version of Metamask is installed, there are different
23+ * approaches to get the `from` address.
24+ */
21 let from = null;
25 let from = null;
22− if(window.ethereum) {
26+ if (window.ethereum) {
23 from = window.ethereum.selectedAddress;
27 from = window.ethereum.selectedAddress;
24 if (!from) {
28 if (!from) {
25 const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
29 const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
@@ -32,6 +36,10 @@ const metamaskSendTransaction = async (txn: FakeSignableTx, web3: Web3) => {
32 from = accounts[0] ?? null;
36 from = accounts[0] ?? null;
33 }
37 }
34
38
39+ if (!from) {
40+ throw new Error('No accounts connected');
41+ }
42+
35 const { data, to, value, maxPriorityFeePerGas, maxFeePerGas } = txn.toJSON();
43 const { data, to, value, maxPriorityFeePerGas, maxFeePerGas } = txn.toJSON();
36 if (!(data && to && value && maxPriorityFeePerGas && maxFeePerGas && from)) {
44 if (!(data && to && value && maxPriorityFeePerGas && maxFeePerGas && from)) {
37 throw new Error('Unable to send Metamask TX, something is missing');
45 throw new Error('Unable to send Metamask TX, something is missing');