From 21f3e061c8ca322c6da1a0a60c008e5240d31347 Mon Sep 17 00:00:00 2001 From: zgz2020 <47563814+zgz2020@users.noreply.github.com> Date: Thu, 21 Nov 2024 23:57:54 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20action=20for=20approv?= =?UTF-8?q?ing=20new=20rpc=20provider=20for=20ethereum=20mainnet=20(#1245)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: feat: add action for approvingnew RPC provider for Ethereum Mainnet * Adding support for Cypress --- docs/api/cypress/classes/MetaMask.md | 32 +++++++++++++++++ docs/api/playwright/classes/MetaMask.md | 36 +++++++++++++++++++ wallets/metamask/src/cypress/MetaMask.ts | 20 +++++++++++ .../metamask/src/cypress/configureSynpress.ts | 2 ++ .../src/cypress/support/synpressCommands.ts | 16 +++++++++ wallets/metamask/src/playwright/MetaMask.ts | 30 +++++++++++++++- .../NotificationPage/actions/ethereumRpc.ts | 15 ++++++++ .../pages/NotificationPage/actions/index.ts | 1 + .../playwright/pages/NotificationPage/page.ts | 13 +++++++ .../pages/NotificationPage/ethereumRpcPage.ts | 4 +++ .../selectors/pages/NotificationPage/index.ts | 4 ++- wallets/metamask/src/type/MetaMaskAbstract.ts | 10 ++++++ 12 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 wallets/metamask/src/playwright/pages/NotificationPage/actions/ethereumRpc.ts create mode 100644 wallets/metamask/src/selectors/pages/NotificationPage/ethereumRpcPage.ts diff --git a/docs/api/cypress/classes/MetaMask.md b/docs/api/cypress/classes/MetaMask.md index f3b2f6473..8c417b990 100644 --- a/docs/api/cypress/classes/MetaMask.md +++ b/docs/api/cypress/classes/MetaMask.md @@ -101,6 +101,22 @@ True if the token was added successfully *** +### approveNewEthereumRPC() + +```ts +approveNewEthereumRPC(): Promise +``` + +Approves adding a new RPC provider for Ethereum Mainnet. + +#### Returns + +`Promise`\<`boolean`\> + +True if the RPC provider was added successfully + +*** + ### approveNewNetwork() ```ts @@ -558,6 +574,22 @@ True if the key was provided successfully, false otherwise *** +### rejectNewEthereumRPC() + +```ts +rejectNewEthereumRPC(): Promise +``` + +Rejects adding a new RPC provider for Ethereum Mainnet. + +#### Returns + +`Promise`\<`boolean`\> + +True if the new RPC provided was rejected successfully + +*** + ### rejectNewNetwork() ```ts diff --git a/docs/api/playwright/classes/MetaMask.md b/docs/api/playwright/classes/MetaMask.md index 5c706026e..fa23ad616 100644 --- a/docs/api/playwright/classes/MetaMask.md +++ b/docs/api/playwright/classes/MetaMask.md @@ -132,6 +132,24 @@ If extensionId is not set. *** +### approveNewEthereumRPC() + +```ts +approveNewEthereumRPC(): Promise +``` + +Approves adding a new RPC provider for Ethereum Mainnet. + +#### Returns + +`Promise`\<`void`\> + +#### Throws + +If extensionId is not set. + +*** + ### approveNewNetwork() ```ts @@ -586,6 +604,24 @@ If extensionId is not set. *** +### rejectNewEthereumRPC() + +```ts +rejectNewEthereumRPC(): Promise +``` + +Rejects adding a new RPC provider for Ethereum Mainnet. + +#### Returns + +`Promise`\<`void`\> + +#### Throws + +If extensionId is not set. + +*** + ### rejectNewNetwork() ```ts diff --git a/wallets/metamask/src/cypress/MetaMask.ts b/wallets/metamask/src/cypress/MetaMask.ts index a4b33942d..2ac6ab30e 100644 --- a/wallets/metamask/src/cypress/MetaMask.ts +++ b/wallets/metamask/src/cypress/MetaMask.ts @@ -332,6 +332,26 @@ export default class MetaMask { return true } + /** + * Approves adding a new RPC provider for Ethereum Mainnet. + * + * @returns True if the RPC provider was approved successfully + */ + async approveNewEthereumRPC(): Promise { + await this.metamaskPlaywright.approveNewEthereumRPC() + return true + } + + /** + * Rejects adding a new RPC provider for Ethereum Mainnet. + * + * @returns True if the RPC provider was rejected successfully + */ + async rejectNewEthereumRPC(): Promise { + await this.metamaskPlaywright.rejectNewEthereumRPC() + return true + } + /** * Locks the MetaMask wallet. * @returns True if the wallet was locked successfully diff --git a/wallets/metamask/src/cypress/configureSynpress.ts b/wallets/metamask/src/cypress/configureSynpress.ts index 8960e76de..f3ab09ba1 100644 --- a/wallets/metamask/src/cypress/configureSynpress.ts +++ b/wallets/metamask/src/cypress/configureSynpress.ts @@ -135,8 +135,10 @@ export default function configureSynpress( addNetwork: (network: Network) => metamask?.addNetwork(network), approveNewNetwork: () => metamask?.approveNewNetwork(), approveSwitchNetwork: () => metamask?.approveSwitchNetwork(), + approveNewEthereumRPC: () => metamask?.approveNewEthereumRPC(), rejectNewNetwork: () => metamask?.rejectNewNetwork(), rejectSwitchNetwork: () => metamask?.rejectSwitchNetwork(), + rejectNewEthereumRPC: () => metamask?.rejectNewEthereumRPC(), // Anvil createAnvilNode: (options?: CreateAnvilOptions) => metamask?.createAnvilNode(options), diff --git a/wallets/metamask/src/cypress/support/synpressCommands.ts b/wallets/metamask/src/cypress/support/synpressCommands.ts index 3a4342e93..54b49ac19 100644 --- a/wallets/metamask/src/cypress/support/synpressCommands.ts +++ b/wallets/metamask/src/cypress/support/synpressCommands.ts @@ -42,8 +42,10 @@ declare global { addNetwork(network: Network): Chainable approveNewNetwork(): Chainable approveSwitchNetwork(): Chainable + approveNewEthereumRPC(): Chainable rejectNewNetwork(): Chainable rejectSwitchNetwork(): Chainable + rejectNewEthereumRPC(): Chainable deployToken(): Chainable addNewToken(): Chainable @@ -268,6 +270,20 @@ export default function synpressCommandsForMetaMask(): void { return cy.task('rejectSwitchNetwork') }) + /** + * Approves adding a new RPC provider for Ethereum Mainnet + */ + Cypress.Commands.add('approveNewEthereumRPC', () => { + return cy.task('approveNewEthereumRPC') + }) + + /** + * Rejects adding a new RPC provider for Ethereum Mainnet + */ + Cypress.Commands.add('rejectNewEthereumRPC', () => { + return cy.task('rejectNewEthereumRPC') + }) + // Token /** diff --git a/wallets/metamask/src/playwright/MetaMask.ts b/wallets/metamask/src/playwright/MetaMask.ts index b3120c7de..3e7c53630 100644 --- a/wallets/metamask/src/playwright/MetaMask.ts +++ b/wallets/metamask/src/playwright/MetaMask.ts @@ -283,13 +283,41 @@ export class MetaMask extends MetaMaskAbstract { await this.notificationPage.rejectSwitchNetwork(this.extensionId) } + /** + * Approves adding a new RPC provider for Ethereum Mainnet. + * + * @throws {Error} If extensionId is not set. + */ + async approveNewEthereumRPC(): Promise { + if (!this.extensionId) { + throw NO_EXTENSION_ID_ERROR + } + + await this.notificationPage.approveNewEthereumRPC(this.extensionId) + } + + /** + * Rejects adding a new RPC provider for Ethereum Mainnet. + * + * @throws {Error} If extensionId is not set. + */ + async rejectNewEthereumRPC(): Promise { + if (!this.extensionId) { + throw NO_EXTENSION_ID_ERROR + } + + await this.notificationPage.rejectNewEthereumRPC(this.extensionId) + } + /** * Confirms a transaction. * * @param options - Optional gas settings for the transaction. * @throws {Error} If extensionId is not set. */ - async confirmTransaction(options?: { gasSetting?: GasSettings }): Promise { + async confirmTransaction(options?: { + gasSetting?: GasSettings + }): Promise { if (!this.extensionId) { throw NO_EXTENSION_ID_ERROR } diff --git a/wallets/metamask/src/playwright/pages/NotificationPage/actions/ethereumRpc.ts b/wallets/metamask/src/playwright/pages/NotificationPage/actions/ethereumRpc.ts new file mode 100644 index 000000000..1c3f6d1ca --- /dev/null +++ b/wallets/metamask/src/playwright/pages/NotificationPage/actions/ethereumRpc.ts @@ -0,0 +1,15 @@ +import type { Page } from '@playwright/test' +import Selectors from '../../../../selectors/pages/NotificationPage' + +const approveNewEthereumRPC = async (notificationPage: Page) => { + await notificationPage.locator(Selectors.EthereumRpcPage.approveNewRpc).click() +} + +const rejectNewEthereumRPC = async (notificationPage: Page) => { + await notificationPage.locator(Selectors.EthereumRpcPage.rejectNewRpc).click() +} + +export const ethereumRpc = { + approveNewEthereumRPC, + rejectNewEthereumRPC +} diff --git a/wallets/metamask/src/playwright/pages/NotificationPage/actions/index.ts b/wallets/metamask/src/playwright/pages/NotificationPage/actions/index.ts index 3a2f10500..11a7730c9 100644 --- a/wallets/metamask/src/playwright/pages/NotificationPage/actions/index.ts +++ b/wallets/metamask/src/playwright/pages/NotificationPage/actions/index.ts @@ -6,3 +6,4 @@ export * from './transaction' export * from './network' export * from './token' export * from './encryption' +export * from './ethereumRpc' diff --git a/wallets/metamask/src/playwright/pages/NotificationPage/page.ts b/wallets/metamask/src/playwright/pages/NotificationPage/page.ts index 1052ccd41..12209d598 100644 --- a/wallets/metamask/src/playwright/pages/NotificationPage/page.ts +++ b/wallets/metamask/src/playwright/pages/NotificationPage/page.ts @@ -6,6 +6,7 @@ import { approvePermission, connectToDapp, decryptMessage, + ethereumRpc, network, providePublicEncryptionKey, signSimpleMessage, @@ -99,6 +100,18 @@ export class NotificationPage { await network.rejectSwitchNetwork(notificationPage) } + async approveNewEthereumRPC(extensionId: string) { + const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId) + + await ethereumRpc.approveNewEthereumRPC(notificationPage) + } + + async rejectNewEthereumRPC(extensionId: string) { + const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId) + + await ethereumRpc.rejectNewEthereumRPC(notificationPage) + } + async confirmTransaction(extensionId: string, options?: { gasSetting?: GasSettings }) { const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId) diff --git a/wallets/metamask/src/selectors/pages/NotificationPage/ethereumRpcPage.ts b/wallets/metamask/src/selectors/pages/NotificationPage/ethereumRpcPage.ts new file mode 100644 index 000000000..56e2fe8cd --- /dev/null +++ b/wallets/metamask/src/selectors/pages/NotificationPage/ethereumRpcPage.ts @@ -0,0 +1,4 @@ +export default { + approveNewRpc: '.confirmation-warning-modal__content .mm-button-primary--type-danger', + rejectNewRpc: '.confirmation-warning-modal__content .mm-button-secondary' +} diff --git a/wallets/metamask/src/selectors/pages/NotificationPage/index.ts b/wallets/metamask/src/selectors/pages/NotificationPage/index.ts index 66267cd73..76744ae89 100644 --- a/wallets/metamask/src/selectors/pages/NotificationPage/index.ts +++ b/wallets/metamask/src/selectors/pages/NotificationPage/index.ts @@ -1,5 +1,6 @@ import ActionFooter from './actionFooter' import ConnectPage from './connectPage' +import EthereumRpcPage from './ethereumRpcPage' import NetworkPage from './networkPage' import PermissionPage from './permissionPage' import SignaturePage from './signaturePage' @@ -8,8 +9,9 @@ import TransactionPage from './transactionPage' export default { ActionFooter, ConnectPage, - SignaturePage, + EthereumRpcPage, NetworkPage, PermissionPage, + SignaturePage, TransactionPage } diff --git a/wallets/metamask/src/type/MetaMaskAbstract.ts b/wallets/metamask/src/type/MetaMaskAbstract.ts index 9404e363d..29c4848ae 100644 --- a/wallets/metamask/src/type/MetaMaskAbstract.ts +++ b/wallets/metamask/src/type/MetaMaskAbstract.ts @@ -126,6 +126,16 @@ export abstract class MetaMaskAbstract { */ abstract rejectSwitchNetwork(): void + /** + * Approves a new RPC provider request. + */ + abstract approveNewEthereumRPC(): void + + /** + * Rejects a new RPC provider request. + */ + abstract rejectNewEthereumRPC(): void + /** * Confirms a transaction request. *