Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve documentation of chain utils methods #45

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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