From 9dcf3bdf492fdc62d06d4564b3176f3ca7f3dee0 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Fri, 26 Jan 2024 17:34:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(metamask):=20Rena?= =?UTF-8?q?me=20to=20`approveTokenPermission`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/classes/MetaMask.md | 64 +++++++++---------- wallets/metamask/src/metamask.ts | 12 ++-- .../src/pages/NotificationPage/page.ts | 7 +- .../e2e/metamask/approvePermission.spec.ts | 12 ++-- .../e2e/metamask/rejectPermission.spec.ts | 2 +- 5 files changed, 50 insertions(+), 47 deletions(-) diff --git a/docs/api/classes/MetaMask.md b/docs/api/classes/MetaMask.md index 993969147..3051ef60d 100644 --- a/docs/api/classes/MetaMask.md +++ b/docs/api/classes/MetaMask.md @@ -11,8 +11,8 @@ This class is the heart of Synpress's MetaMask API. - [addNetwork()](MetaMask.md#addnetwork) - [addNewAccount()](MetaMask.md#addnewaccount) - [approveNewNetwork()](MetaMask.md#approvenewnetwork) - - [approvePermission()](MetaMask.md#approvepermission) - [approveSwitchNetwork()](MetaMask.md#approveswitchnetwork) + - [approveTokenPermission()](MetaMask.md#approvetokenpermission) - [confirmSignature()](MetaMask.md#confirmsignature) - [confirmTransaction()](MetaMask.md#confirmtransaction) - [connectToDapp()](MetaMask.md#connecttodapp) @@ -23,9 +23,9 @@ This class is the heart of Synpress's MetaMask API. - [openSettings()](MetaMask.md#opensettings) - [openSidebarMenu()](MetaMask.md#opensidebarmenu) - [rejectNewNetwork()](MetaMask.md#rejectnewnetwork) - - [rejectPermission()](MetaMask.md#rejectpermission) - [rejectSignature()](MetaMask.md#rejectsignature) - [rejectSwitchNetwork()](MetaMask.md#rejectswitchnetwork) + - [rejectTokenPermission()](MetaMask.md#rejecttokenpermission) - [rejectTransaction()](MetaMask.md#rejecttransaction) - [resetAccount()](MetaMask.md#resetaccount) - [switchAccount()](MetaMask.md#switchaccount) @@ -138,16 +138,30 @@ Approves a new network request. *** -### approvePermission() +### approveSwitchNetwork() + +```ts +approveSwitchNetwork(): Promise +``` + +Approves a switch network request. + +#### Returns + +`Promise`\<`void`\> + +*** + +### approveTokenPermission() ```ts -approvePermission(options?): Promise +approveTokenPermission(options?): Promise ``` Approves a permission request to spend tokens. ::: warning -This function does not work with the NFTs approvals. +For NFT approvals, use `confirmTransaction` method. ::: #### Parameters @@ -164,20 +178,6 @@ This function does not work with the NFTs approvals. *** -### approveSwitchNetwork() - -```ts -approveSwitchNetwork(): Promise -``` - -Approves a switch network request. - -#### Returns - -`Promise`\<`void`\> - -*** - ### confirmSignature() ```ts @@ -343,17 +343,13 @@ Rejects a new network request. *** -### rejectPermission() +### rejectSignature() ```ts -rejectPermission(): Promise +rejectSignature(): Promise ``` -Rejects a permission request to spend tokens. - -::: warning -This function does not work with the NFTs approvals. -::: +Rejects a signature request. This function supports all types of commonly used signatures. #### Returns @@ -361,13 +357,13 @@ This function does not work with the NFTs approvals. *** -### rejectSignature() +### rejectSwitchNetwork() ```ts -rejectSignature(): Promise +rejectSwitchNetwork(): Promise ``` -Rejects a signature request. This function supports all types of commonly used signatures. +Rejects a switch network request. #### Returns @@ -375,13 +371,17 @@ Rejects a signature request. This function supports all types of commonly used s *** -### rejectSwitchNetwork() +### rejectTokenPermission() ```ts -rejectSwitchNetwork(): Promise +rejectTokenPermission(): Promise ``` -Rejects a switch network request. +Rejects a permission request to spend tokens. + +::: warning +For NFT approvals, use `confirmTransaction` method. +::: #### Returns diff --git a/wallets/metamask/src/metamask.ts b/wallets/metamask/src/metamask.ts index 6121dec57..62701875a 100644 --- a/wallets/metamask/src/metamask.ts +++ b/wallets/metamask/src/metamask.ts @@ -256,34 +256,34 @@ export class MetaMask { * Approves a permission request to spend tokens. * * ::: warning - * This function does not work with the NFTs approvals. + * For NFT approvals, use `confirmTransaction` method. * ::: * * @param options - The permission options. * @param options.spendLimit - The spend limit to use for the permission. * @param options.gasSetting - The gas setting to use for the approval transaction. */ - async approvePermission(options?: { spendLimit?: 'max' | number; gasSetting?: GasSetting }) { + async approveTokenPermission(options?: { spendLimit?: 'max' | number; gasSetting?: GasSetting }) { if (!this.extensionId) { throw NO_EXTENSION_ID_ERROR } - await this.notificationPage.approvePermission(this.extensionId, options) + await this.notificationPage.approveTokenPermission(this.extensionId, options) } /** * Rejects a permission request to spend tokens. * * ::: warning - * This function does not work with the NFTs approvals. + * For NFT approvals, use `confirmTransaction` method. * ::: */ - async rejectPermission() { + async rejectTokenPermission() { if (!this.extensionId) { throw NO_EXTENSION_ID_ERROR } - await this.notificationPage.rejectPermission(this.extensionId) + await this.notificationPage.rejectTokenPermission(this.extensionId) } /** diff --git a/wallets/metamask/src/pages/NotificationPage/page.ts b/wallets/metamask/src/pages/NotificationPage/page.ts index 1e9ed7934..33ee8bfbb 100644 --- a/wallets/metamask/src/pages/NotificationPage/page.ts +++ b/wallets/metamask/src/pages/NotificationPage/page.ts @@ -108,7 +108,10 @@ export class NotificationPage { await transaction.confirmAndWaitForMining(this.page, notificationPage, options?.gasSetting ?? 'site') } - async approvePermission(extensionId: string, options?: { spendLimit?: 'max' | number; gasSetting?: GasSetting }) { + async approveTokenPermission( + extensionId: string, + options?: { spendLimit?: 'max' | number; gasSetting?: GasSetting } + ) { const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId) if (options?.spendLimit !== undefined) { @@ -118,7 +121,7 @@ export class NotificationPage { await approvePermission.approve(notificationPage, options?.gasSetting ?? 'site') } - async rejectPermission(extensionId: string) { + async rejectTokenPermission(extensionId: string) { const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId) await approvePermission.reject(notificationPage) diff --git a/wallets/metamask/test/e2e/metamask/approvePermission.spec.ts b/wallets/metamask/test/e2e/metamask/approvePermission.spec.ts index b4609cbe3..99ceef382 100644 --- a/wallets/metamask/test/e2e/metamask/approvePermission.spec.ts +++ b/wallets/metamask/test/e2e/metamask/approvePermission.spec.ts @@ -23,7 +23,7 @@ describe('with default gas setting', () => { await page.locator('#approveTokens').click() - await metamask.approvePermission() + await metamask.approveTokenPermission() }) test('should approve tokens with the `max` limit', async ({ page, metamask, deployToken }) => { @@ -31,7 +31,7 @@ describe('with default gas setting', () => { await page.locator('#approveTokens').click() - await metamask.approvePermission({ spendLimit: 'max' }) + await metamask.approveTokenPermission({ spendLimit: 'max' }) }) test('should approve tokens with the custom limit', async ({ page, metamask, deployToken }) => { @@ -39,7 +39,7 @@ describe('with default gas setting', () => { await page.locator('#approveTokens').click() - await metamask.approvePermission({ spendLimit: 420 }) + await metamask.approveTokenPermission({ spendLimit: 420 }) }) }) @@ -49,7 +49,7 @@ describe('with custom gas setting', () => { await page.locator('#approveTokens').click() - await metamask.approvePermission({ + await metamask.approveTokenPermission({ gasSetting: 'site' }) }) @@ -63,7 +63,7 @@ describe('with custom gas setting', () => { await page.locator('#approveTokens').click() - await metamask.approvePermission({ + await metamask.approveTokenPermission({ spendLimit: 'max', gasSetting: { maxBaseFee: 250, @@ -81,7 +81,7 @@ describe('with custom gas setting', () => { await page.locator('#approveTokens').click() - await metamask.approvePermission({ + await metamask.approveTokenPermission({ spendLimit: 420, gasSetting: { maxBaseFee: 250, diff --git a/wallets/metamask/test/e2e/metamask/rejectPermission.spec.ts b/wallets/metamask/test/e2e/metamask/rejectPermission.spec.ts index 0aa0fad0f..25f31d7de 100644 --- a/wallets/metamask/test/e2e/metamask/rejectPermission.spec.ts +++ b/wallets/metamask/test/e2e/metamask/rejectPermission.spec.ts @@ -14,5 +14,5 @@ test('should reject approve request', async ({ page, metamask, connectToAnvil }) await page.locator('#approveTokens').click() - await metamask.rejectPermission() + await metamask.rejectTokenPermission() })