From e54669200630476dba9ce7ce3bc90382ec0e7c99 Mon Sep 17 00:00:00 2001 From: VGLoic Date: Tue, 15 Aug 2023 14:31:04 +0200 Subject: [PATCH 1/3] improve documentation of add chain --- README.md | 12 ++++++------ src/__tests__/index.test.tsx | 14 ++++++-------- src/metamask-context.ts | 20 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2c2645a..fa7873b 100644 --- a/README.md +++ b/README.md @@ -99,14 +99,14 @@ function WrongNetwork() { const { addChain } = useMetaMask(); const gnosisChainNetworkParams = { chainId: "0x64", - chainName: "Gnosis Chain", - rpcUrls: ["https://rpc.gnosischain.com/"], + chainName: "Gnosis", nativeCurrency: { - name: "xDAI", - symbol: "xDAI", - decimals: 18, + name: "xDai", + symbol: "XDAI", + decimals: 18, }, - blockExplorerUrls: ["https://blockscout.com/xdai/mainnet/"] + rpcUrls: ["https://rpc.gnosischain.com/"], + blockExplorerUrls: ["https://gnosisscan.io/"], }; // Request to add Gnosis chain and then switch to it return ( diff --git a/src/__tests__/index.test.tsx b/src/__tests__/index.test.tsx index b5b4e15..c8f734b 100644 --- a/src/__tests__/index.test.tsx +++ b/src/__tests__/index.test.tsx @@ -6,18 +6,16 @@ import { useMetaMask, MetaMaskProvider } from "../"; describe("MetaMask provider", () => { const addChainPrams = { chainId: "0x64", - chainName: "xDAI Chain", - rpcUrls: ["https://dai.poa.network"], - iconUrls: [ - "https://xdaichain.com/fake/example/url/xdai.svg", - "https://xdaichain.com/fake/example/url/xdai.png", - ], + chainName: "Gnosis", nativeCurrency: { - name: "xDAI", - symbol: "xDAI", + name: "xDai", + symbol: "XDAI", decimals: 18, }, + rpcUrls: ["https://rpc.gnosischain.com/"], + blockExplorerUrls: ["https://gnosisscan.io/"], }; + const address = "0x19F7Fa0a30d5829acBD9B35bA2253a759a37EfC5"; describe("when MetaMask is not available", () => { diff --git a/src/metamask-context.ts b/src/metamask-context.ts index 8c3f60c..4bd743c 100644 --- a/src/metamask-context.ts +++ b/src/metamask-context.ts @@ -57,8 +57,24 @@ export type IMetaMaskContext = MetaMaskState & { */ connect: () => Promise; /** - * Request addition of a new network - * @param parameters New chain parameters, see [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085) for full description + * Request addition of a new chain to MetaMask + * @dev See [MetaMask API](https://docs.metamask.io/wallet/reference/rpc-api/#wallet_addethereumchain) or [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085) for full description + * @param parameters The chain parameters + * @example ```typescript + * const { addChain } = useMetaMask(); + * const GNOSIS_MAINNET_PARAMS = { + * chainId: "0x64", + * chainName: "Gnosis", + * nativeCurrency: { + * name: "xDai", + * symbol: "XDAI", + * decimals: 18, + * }, + * rpcUrls: ["https://rpc.gnosischain.com/"], + * blockExplorerUrls: ["https://gnosisscan.io/"], + * } + * const onClick = () => addChain(GNOSIS_MAINNET_PARAMS); + * ``` */ addChain: (parameters: AddEthereumChainParameter) => Promise; /** From db679645b2bdfcf94c3b7f96d846cac97c50ee97 Mon Sep 17 00:00:00 2001 From: VGLoic Date: Tue, 15 Aug 2023 14:34:44 +0200 Subject: [PATCH 2/3] improve documentation of switch chain --- src/metamask-context.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/metamask-context.ts b/src/metamask-context.ts index 4bd743c..79ecc44 100644 --- a/src/metamask-context.ts +++ b/src/metamask-context.ts @@ -60,7 +60,7 @@ export type IMetaMaskContext = MetaMaskState & { * Request addition of a new chain to MetaMask * @dev See [MetaMask API](https://docs.metamask.io/wallet/reference/rpc-api/#wallet_addethereumchain) or [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085) for full description * @param parameters The chain parameters - * @example ```typescript + * @example ```ts * const { addChain } = useMetaMask(); * const GNOSIS_MAINNET_PARAMS = { * chainId: "0x64", @@ -73,16 +73,20 @@ export type IMetaMaskContext = MetaMaskState & { * rpcUrls: ["https://rpc.gnosischain.com/"], * blockExplorerUrls: ["https://gnosisscan.io/"], * } + * // Add Gnosis chain to MetaMask * const onClick = () => addChain(GNOSIS_MAINNET_PARAMS); * ``` */ addChain: (parameters: AddEthereumChainParameter) => Promise; /** * Request a switch of network + * @dev See [MetaMask API](https://docs.metamask.io/wallet/reference/rpc-api/#wallet_switchethereumchain) or [EIP-3326](https://ethereum-magicians.org/t/eip-3326-wallet-switchethereumchain/5471) for full description + * @dev An error with code `4902` will be thrown if the chain has not been added to MetaMask, in this case, use `addChain` first * @param chainId Chain ID of the network in hexadecimal * @example ```ts + * const { addChain } = useMetaMask(); * // Switch chain to Ethereum Mainnet - * await context.switchChain("0x1"); + * const onClick = () => switchChain("0x1"); * ``` */ switchChain: (chainId: string) => Promise; From e1f94fde02de066e3301753d2aeaddb99df7ec78 Mon Sep 17 00:00:00 2001 From: VGLoic Date: Tue, 15 Aug 2023 15:09:44 +0200 Subject: [PATCH 3/3] update unclear documentation of switchChain --- src/metamask-context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/metamask-context.ts b/src/metamask-context.ts index 79ecc44..98170e5 100644 --- a/src/metamask-context.ts +++ b/src/metamask-context.ts @@ -81,7 +81,7 @@ export type IMetaMaskContext = MetaMaskState & { /** * Request a switch of network * @dev See [MetaMask API](https://docs.metamask.io/wallet/reference/rpc-api/#wallet_switchethereumchain) or [EIP-3326](https://ethereum-magicians.org/t/eip-3326-wallet-switchethereumchain/5471) for full description - * @dev An error with code `4902` will be thrown if the chain has not been added to MetaMask, in this case, use `addChain` first + * @dev An error with code `4902` will be thrown if the chain has not been added to MetaMask. In this case, one would need to use the `addChain` method in order to add the chain to MetaMask and switch to it. * @param chainId Chain ID of the network in hexadecimal * @example ```ts * const { addChain } = useMetaMask();