Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Jul 10, 2024
1 parent 5b90101 commit af77139
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 () => {
Expand Down
106 changes: 56 additions & 50 deletions packages/snaps-controllers/src/insights/SnapInsightsController.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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,
Expand All @@ -260,7 +266,7 @@ export class SnapInsightsController extends BaseController<
params: Record<string, Json>;
}) {
return this.messagingSystem.call('SnapController:handleRequest', {
snapId: snapId,
snapId,
origin: '',
handler,
request: {
Expand Down
14 changes: 9 additions & 5 deletions packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import type {
CronjobControllerActions,
CronjobControllerEvents,
} from '../cronjob';
import type {
SnapInsightsControllerAllowedActions,
SnapInsightsControllerAllowedEvents,
} from '../insights';
import type {
SnapInterfaceControllerActions,
SnapInterfaceControllerAllowedActions,
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-controllers/src/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './execution-environment';
export * from './service';
export * from './sleep';
export * from './location';
export * from './registry';
export * from './registry';

0 comments on commit af77139

Please sign in to comment.