Skip to content

Commit

Permalink
feat: improve documentation of chain utils methods (#45)
Browse files Browse the repository at this point in the history
* improve documentation of add chain

* improve documentation of switch chain

* update unclear documentation of switchChain
  • Loading branch information
VGLoic authored Aug 17, 2023
1 parent 6e6b9a0 commit e435991
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
14 changes: 6 additions & 8 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
26 changes: 23 additions & 3 deletions src/metamask-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,36 @@ export type IMetaMaskContext = MetaMaskState & {
*/
connect: () => Promise<string[] | null>;
/**
* 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 ```ts
* 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/"],
* }
* // Add Gnosis chain to MetaMask
* const onClick = () => addChain(GNOSIS_MAINNET_PARAMS);
* ```
*/
addChain: (parameters: AddEthereumChainParameter) => Promise<void>;
/**
* 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, 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();
* // Switch chain to Ethereum Mainnet
* await context.switchChain("0x1");
* const onClick = () => switchChain("0x1");
* ```
*/
switchChain: (chainId: string) => Promise<void>;
Expand Down

0 comments on commit e435991

Please sign in to comment.