From af77139f940bfbf42376e9e394c5b153a954ad4a Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Wed, 10 Jul 2024 10:26:35 +0200 Subject: [PATCH] Fix lint --- .../insights/SnapInsightsController.test.ts | 15 +-- .../src/insights/SnapInsightsController.ts | 106 +++++++++--------- .../src/test-utils/controller.ts | 14 ++- .../snaps-controllers/src/test-utils/index.ts | 2 +- 4 files changed, 74 insertions(+), 63 deletions(-) diff --git a/packages/snaps-controllers/src/insights/SnapInsightsController.test.ts b/packages/snaps-controllers/src/insights/SnapInsightsController.test.ts index ac225c3797..a0cd93dc05 100644 --- a/packages/snaps-controllers/src/insights/SnapInsightsController.test.ts +++ b/packages/snaps-controllers/src/insights/SnapInsightsController.test.ts @@ -1,3 +1,11 @@ +import { HandlerType } from '@metamask/snaps-utils'; +import { + getTruncatedSnap, + MOCK_LOCAL_SNAP_ID, + MOCK_SNAP_ID, +} from '@metamask/snaps-utils/test-utils'; +import { nanoid } from 'nanoid'; + import { getRestrictedSnapInsightsControllerMessenger, getRootSnapInsightsControllerMessenger, @@ -7,13 +15,6 @@ import { TYPED_SIGNATURE_MOCK, } from '../test-utils'; import { SnapInsightsController } from './SnapInsightsController'; -import { - getTruncatedSnap, - MOCK_LOCAL_SNAP_ID, - MOCK_SNAP_ID, -} from '@metamask/snaps-utils/test-utils'; -import { nanoid } from 'nanoid'; -import { HandlerType } from '@metamask/snaps-utils'; describe('SnapInsightsController', () => { it('adds insight for transactions', async () => { diff --git a/packages/snaps-controllers/src/insights/SnapInsightsController.ts b/packages/snaps-controllers/src/insights/SnapInsightsController.ts index 3c8e5edcee..657e6f674c 100644 --- a/packages/snaps-controllers/src/insights/SnapInsightsController.ts +++ b/packages/snaps-controllers/src/insights/SnapInsightsController.ts @@ -1,27 +1,29 @@ import type { RestrictedControllerMessenger } from '@metamask/base-controller'; import { BaseController } from '@metamask/base-controller'; -import { - TransactionControllerUnapprovedTransactionAddedEvent, - TransactionMeta, -} from '@metamask/transaction-controller'; -import { +import type { + Caveat, + GetPermissions, + ValidPermission, +} from '@metamask/permission-controller'; +import type { SignatureController, SignatureStateChange, } from '@metamask/signature-controller'; -import { GetAllSnaps, getRunnableSnaps, HandleSnapRequest } from '../snaps'; -import { Json, SnapId } from '@metamask/snaps-sdk'; -import { HandlerType } from '@metamask/snaps-utils'; -import { hasProperty } from '@metamask/utils'; import { getSignatureOriginCaveat, getTransactionOriginCaveat, SnapEndowments, } from '@metamask/snaps-rpc-methods'; -import { - Caveat, - GetPermissions, - ValidPermission, -} from '@metamask/permission-controller'; +import type { Json, SnapId } from '@metamask/snaps-sdk'; +import { HandlerType, logError } from '@metamask/snaps-utils'; +import type { + TransactionControllerUnapprovedTransactionAddedEvent, + TransactionMeta, +} from '@metamask/transaction-controller'; +import { hasProperty } from '@metamask/utils'; + +import type { GetAllSnaps, HandleSnapRequest } from '../snaps'; +import { getRunnableSnaps } from '../snaps'; const controllerName = 'SnapInsightsController'; @@ -137,7 +139,7 @@ export class SnapInsightsController extends BaseController< SnapEndowments.TransactionInsight, ); - const promises = snaps.map(({ snapId, permission }) => { + const promises = snaps.map(async ({ snapId, permission }) => { const hasTransactionOriginCaveat = getTransactionOriginCaveat(permission); const transactionOrigin = hasTransactionOriginCaveat && origin ? origin : null; @@ -153,25 +155,27 @@ export class SnapInsightsController extends BaseController< state.insights[id] = { loading: true, results: [] }; }); - Promise.allSettled(promises).then((settled) => { - const results = settled.map((promise, idx) => { - const snapId = snaps[idx].snapId; - if (promise.status === 'rejected') { + Promise.allSettled(promises) + .then((settled) => { + const results = settled.map((promise, idx) => { + const { snapId } = snaps[idx]; + if (promise.status === 'rejected') { + return { + error: promise.reason, + snapId, + }; + } return { - error: promise.reason, snapId, + response: promise.value, }; - } - return { - snapId, - response: promise.value, - }; - }); - this.update((state) => { - state.insights[id].loading = false; - state.insights[id].results = results; - }); - }); + }); + this.update((state) => { + state.insights[id].loading = false; + state.insights[id].results = results; + }); + }) + .catch(logError); } #handleSignatureStateChange(state: SignatureControllerState) { @@ -214,7 +218,7 @@ export class SnapInsightsController extends BaseController< signatureMethod, }; - const promises = snaps.map(({ snapId, permission }) => { + const promises = snaps.map(async ({ snapId, permission }) => { const hasSignatureOriginCaveat = getSignatureOriginCaveat(permission); const signatureOrigin = hasSignatureOriginCaveat ? origin : null; @@ -229,28 +233,30 @@ export class SnapInsightsController extends BaseController< state.insights[id] = { loading: true, results: [] }; }); - Promise.allSettled(promises).then((settled) => { - const results = settled.map((promise, idx) => { - const snapId = snaps[idx].snapId; - if (promise.status === 'rejected') { + Promise.allSettled(promises) + .then((settled) => { + const results = settled.map((promise, idx) => { + const { snapId } = snaps[idx]; + if (promise.status === 'rejected') { + return { + error: promise.reason, + snapId, + }; + } return { - error: promise.reason, snapId, + response: promise.value, }; - } - return { - snapId, - response: promise.value, - }; - }); - this.update((state) => { - state.insights[id].loading = false; - state.insights[id].results = results; - }); - }); + }); + this.update((state) => { + state.insights[id].loading = false; + state.insights[id].results = results; + }); + }) + .catch(logError); } - #handleSnapRequest({ + async #handleSnapRequest({ snapId, handler, params, @@ -260,7 +266,7 @@ export class SnapInsightsController extends BaseController< params: Record; }) { return this.messagingSystem.call('SnapController:handleRequest', { - snapId: snapId, + snapId, origin: '', handler, request: { diff --git a/packages/snaps-controllers/src/test-utils/controller.ts b/packages/snaps-controllers/src/test-utils/controller.ts index 3da25eceba..4ba94d8daa 100644 --- a/packages/snaps-controllers/src/test-utils/controller.ts +++ b/packages/snaps-controllers/src/test-utils/controller.ts @@ -38,6 +38,10 @@ import type { CronjobControllerActions, CronjobControllerEvents, } from '../cronjob'; +import type { + SnapInsightsControllerAllowedActions, + SnapInsightsControllerAllowedEvents, +} from '../insights'; import type { SnapInterfaceControllerActions, SnapInterfaceControllerAllowedActions, @@ -57,10 +61,6 @@ import type { KeyDerivationOptions } from '../types'; import { MOCK_CRONJOB_PERMISSION } from './cronjob'; import { getNodeEES, getNodeEESMessenger } from './execution-environment'; import { MockSnapsRegistry } from './registry'; -import { - SnapInsightsControllerAllowedActions, - SnapInsightsControllerAllowedEvents, -} from '../insights'; const asyncNoOp = async () => Promise.resolve(); @@ -777,7 +777,11 @@ export const getRestrictedSnapInsightsControllerMessenger = ( 'TransactionController:unapprovedTransactionAdded', 'SignatureController:stateChange', ], - allowedActions: ['PermissionController:getPermissions', 'SnapController:getAll', 'SnapController:handleRequest'], + allowedActions: [ + 'PermissionController:getPermissions', + 'SnapController:getAll', + 'SnapController:handleRequest', + ], }); return controllerMessenger; diff --git a/packages/snaps-controllers/src/test-utils/index.ts b/packages/snaps-controllers/src/test-utils/index.ts index a8f5716a58..91b9d1585a 100644 --- a/packages/snaps-controllers/src/test-utils/index.ts +++ b/packages/snaps-controllers/src/test-utils/index.ts @@ -4,4 +4,4 @@ export * from './execution-environment'; export * from './service'; export * from './sleep'; export * from './location'; -export * from './registry'; \ No newline at end of file +export * from './registry';