-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(metamask): Add support for adding networks on demand (#1006)
- Loading branch information
1 parent
64297af
commit ced0a9d
Showing
8 changed files
with
156 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Page } from '@playwright/test' | ||
import { z } from 'zod' | ||
import { waitFor } from '../../../utils/waitFor' | ||
import Selectors from '../selectors' | ||
|
||
const Network = z.object({ | ||
name: z.string(), | ||
rpcUrl: z.string(), | ||
chainId: z.number(), | ||
symbol: z.string(), | ||
blockExplorerUrl: z.string().optional() | ||
}) | ||
|
||
export type Network = z.infer<typeof Network> | ||
|
||
export async function addNetwork(page: Page, network: Network) { | ||
const { name, rpcUrl, chainId, symbol, blockExplorerUrl } = Network.parse(network) | ||
|
||
await page.locator(Selectors.networkDropdown.dropdownButton).click() | ||
await page.locator(Selectors.networkDropdown.addNetworkButton).click() | ||
|
||
await page.locator(Selectors.settings.networks.addNetworkManuallyButton).click() | ||
|
||
await page.locator(Selectors.settings.networks.newNetworkForm.networkNameInput).fill(name) | ||
|
||
await page.locator(Selectors.settings.networks.newNetworkForm.rpcUrlInput).fill(rpcUrl) | ||
|
||
// We have to wait for the RPC URL error to appear. | ||
const rpcUrlErrorLocator = page.locator(Selectors.settings.networks.newNetworkForm.rpcUrlError) | ||
if (await waitFor(() => rpcUrlErrorLocator.isVisible(), 1_000, false)) { | ||
const rpcUrlErrorText = await rpcUrlErrorLocator.textContent({ timeout: 1_000 }) | ||
throw new Error(`[AddNetwork] RPC URL error: ${rpcUrlErrorText}`) | ||
} | ||
|
||
await page.locator(Selectors.settings.networks.newNetworkForm.chainIdInput).fill(chainId.toString()) | ||
|
||
// We have to wait for the Chain ID error to appear. | ||
const chainIdErrorLocator = page.locator(Selectors.settings.networks.newNetworkForm.chainIdError) | ||
if (await waitFor(() => chainIdErrorLocator.isVisible(), 1_000, false)) { | ||
const chainIdErrorText = await chainIdErrorLocator.textContent({ timeout: 1_000 }) | ||
throw new Error(`[AddNetwork] Chain ID error: ${chainIdErrorText}`) | ||
} | ||
|
||
await page.locator(Selectors.settings.networks.newNetworkForm.symbolInput).fill(symbol) | ||
|
||
if (blockExplorerUrl) { | ||
await page.locator(Selectors.settings.networks.newNetworkForm.blockExplorerUrlInput).fill(blockExplorerUrl) | ||
} | ||
|
||
await page.locator(Selectors.settings.networks.newNetworkForm.saveButton).click() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { testWithSynpress } from 'fixtures' | ||
import { MetaMask, unlockForFixture } from '../../../src' | ||
|
||
import { z } from 'zod' | ||
import basicSetup from '../wallet-setup/basic.setup' | ||
|
||
const test = testWithSynpress(basicSetup, unlockForFixture) | ||
|
||
const { expect } = test | ||
|
||
const network = { | ||
name: 'OP Mainnet', | ||
rpcUrl: 'https://mainnet.optimism.io', | ||
chainId: 10, | ||
symbol: 'ETH', | ||
blockExplorerUrl: 'https://optimistic.etherscan.io' | ||
} | ||
|
||
test('should add network', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
await metamask.addNetwork(network) | ||
|
||
await expect(metamaskPage.locator(metamask.homePage.selectors.currentNetwork)).toHaveText(network.name) | ||
}) | ||
|
||
test('should add network without block explorer', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
await metamask.addNetwork({ | ||
...network, | ||
blockExplorerUrl: undefined | ||
}) | ||
|
||
await expect(metamaskPage.locator(metamask.homePage.selectors.currentNetwork)).toHaveText(network.name) | ||
}) | ||
|
||
test('should validate the network object with Zod', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
// @ts-ignore | ||
await expect(metamask.addNetwork({})).rejects.toThrowError(z.ZodError) | ||
}) | ||
|
||
test('should throw if there is an issue with rpc url', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
const promise = metamask.addNetwork({ | ||
...network, | ||
rpcUrl: 'hps://mainnet.optimism.io' // Incorrect. | ||
}) | ||
|
||
await expect(promise).rejects.toThrowError( | ||
'[AddNetwork] RPC URL error: URLs require the appropriate HTTP/HTTPS prefix.' | ||
) | ||
}) | ||
|
||
test('should throw if there is an issue with chain id', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
const promise = metamask.addNetwork({ | ||
...network, | ||
chainId: 0x42069 // Incorrect. | ||
}) | ||
|
||
await expect(promise).rejects.toThrowError( | ||
'[AddNetwork] Chain ID error: The RPC URL you have entered returned a different chain ID (10). Please update the Chain ID to match the RPC URL of the network you are trying to add.' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters