Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Transaction Insight API #642

Merged
merged 52 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
06d4ed3
Add support for multiple handler exports
FrederikBolding Jul 19, 2022
9f17c20
Add permission
FrederikBolding Jul 19, 2022
c42e480
Fix types and tests
FrederikBolding Jul 19, 2022
700db20
Use enum for HandlerType
FrederikBolding Jul 20, 2022
b555210
Add SnapEndowments enum
FrederikBolding Jul 20, 2022
94a8578
Rename export
FrederikBolding Jul 20, 2022
75eb7ed
Use named args and improve types
FrederikBolding Jul 20, 2022
4423b09
Add controller action
FrederikBolding Jul 21, 2022
450b290
Fix after rebase
FrederikBolding Jul 22, 2022
b826e12
Fix tests after rebase
FrederikBolding Jul 22, 2022
13c42e5
addressed PR comments
hmalik88 Aug 11, 2022
b2c2e59
fixed enum according to ts naming convention
hmalik88 Aug 11, 2022
6438e47
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 16, 2022
781c591
address more PR comments
hmalik88 Aug 17, 2022
888ced5
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 17, 2022
5f70c0f
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 17, 2022
6f1aa2c
more fixes
hmalik88 Aug 17, 2022
a3b0bbd
Add reference to utils
Mrtenz Aug 18, 2022
a93f472
Add support for multiple handler exports
FrederikBolding Jul 19, 2022
7cfeb7b
Add permission
FrederikBolding Jul 19, 2022
d80e669
Fix types and tests
FrederikBolding Jul 19, 2022
ee744b7
Use enum for HandlerType
FrederikBolding Jul 20, 2022
4ea3a57
Add SnapEndowments enum
FrederikBolding Jul 20, 2022
b880143
Rename export
FrederikBolding Jul 20, 2022
ea0ef5b
Use named args and improve types
FrederikBolding Jul 20, 2022
00b43db
Add controller action
FrederikBolding Jul 21, 2022
fdaa8ba
Fix after rebase
FrederikBolding Jul 22, 2022
7f5c804
Fix tests after rebase
FrederikBolding Jul 22, 2022
026c4bf
addressed PR comments
hmalik88 Aug 11, 2022
612abb5
fixed enum according to ts naming convention
hmalik88 Aug 11, 2022
72b8a4f
address more PR comments
hmalik88 Aug 17, 2022
46022c4
more fixes
hmalik88 Aug 17, 2022
f0d92d6
Add reference to utils
Mrtenz Aug 18, 2022
ab1c684
Fix circular import
FrederikBolding Aug 22, 2022
4198a5d
Fix build
FrederikBolding Aug 22, 2022
056ce89
Fix a bunch of tests
FrederikBolding Aug 22, 2022
5e4dde6
Update coverage
FrederikBolding Aug 22, 2022
42a417f
Fix test
FrederikBolding Aug 22, 2022
78c497a
Merge branch 'fb/tx-insights' of github.com:MetaMask/snaps-skunkworks…
hmalik88 Aug 22, 2022
e00458b
addressed PR comments
hmalik88 Aug 22, 2022
ee7d5fa
updated test
hmalik88 Aug 22, 2022
33e052b
fixed build issue
hmalik88 Aug 22, 2022
4cee36e
update jest config
hmalik88 Aug 22, 2022
91ca88a
Merge branch 'main' into fb/tx-insights
hmalik88 Aug 22, 2022
f8a6af8
addressed nit
hmalik88 Aug 22, 2022
3591713
updated jest config again
hmalik88 Aug 23, 2022
e9fdf89
Merge branch 'main' into fb/tx-insights
rekmarks Aug 23, 2022
479498e
Remove ChainId type
rekmarks Aug 23, 2022
84f2328
Fix isCaipChainId implementation
rekmarks Aug 23, 2022
45af1c7
Remove needless 'any', re-enable lint rule, fixup
rekmarks Aug 23, 2022
286c080
Fix utils test coverage
rekmarks Aug 23, 2022
c524b38
fixup! Fix utils test coverage
rekmarks Aug 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/controllers/src/snaps/SnapController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SnapManifest,
HandlerType,
} from '@metamask/snap-utils';

import { Crypto } from '@peculiar/webcrypto';
import { EthereumRpcError, ethErrors, serializeError } from 'eth-rpc-errors';
import fetchMock from 'jest-fetch-mock';
Expand Down Expand Up @@ -3661,7 +3662,7 @@ describe('SnapController', () => {
expect(
await messenger.call('SnapController:handleRequest', {
snapId: 'npm:fooSnap',
handler: HandlerType.OnTransactionInsight,
handler: HandlerType.OnTransaction,
origin: 'foo',
request: {},
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/execution-environments/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = {
global: {
branches: 84.33,
functions: 92.91,
lines: 86.65,
statements: 86.86,
lines: 86.67,
statements: 86.88,
},
},
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ describe('BaseSnapExecutor', () => {
});
});

it('supports onTransactionInsight export', async () => {
it('supports onTransaction export', async () => {
const CODE = `
module.exports.onTransactionInsight = ({ origin, transaction, chainId, metadata }) => ({ origin, transaction, chainId, metadata });
module.exports.onTransaction = ({ origin, transaction, chainId }) => ({ origin, transaction, chainId });
`;
const executor = new TestSnapExecutor();

Expand All @@ -750,15 +750,15 @@ describe('BaseSnapExecutor', () => {
// We also have to decide on the shape of that object.
const transaction = { maxFeePerGas: '0x' };

const params = { transaction, metadata: {}, chainId: '0x1' };
const params = { transaction, chainId: 'eip155:1' };

await executor.writeCommand({
jsonrpc: '2.0',
id: 2,
method: 'snapRpc',
params: [
FAKE_SNAP_NAME,
HandlerType.OnTransactionInsight,
HandlerType.OnTransaction,
FAKE_ORIGIN,
{ jsonrpc: '2.0', method: 'foo', params },
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@metamask/snap-types';
import { errorCodes, ethErrors, serializeError } from 'eth-rpc-errors';
import { JsonRpcNotification } from '@metamask/utils';
import { HandlerType } from '@metamask/snap-utils';
import { assert, HandlerType } from '@metamask/snap-utils';
import EEOpenRPCDocument from '../openrpc.json';
import {
Endowments,
Expand Down Expand Up @@ -82,8 +82,11 @@ export class BaseSnapExecutor {
}

// We're capturing the handler in case someone modifies the data object before the call
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const handler = data.exports[handlerName]!;
const handler = data.exports[handlerName];
assert(
ritave marked this conversation as resolved.
Show resolved Hide resolved
handler !== undefined,
`No ${handlerName} handler exported for snap "${target}`,
);
// @todo fix type
return this.executeInSnapContext(target, () => handler(args as any));
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
},
Expand Down
8 changes: 2 additions & 6 deletions packages/execution-environments/src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ function getHandlerArguments(
request: JsonRpcRequest<unknown[] | { [key: string]: unknown }>,
): InvokeSnapArgs {
switch (handler) {
case HandlerType.OnTransactionInsight:
case HandlerType.OnTransaction:
// eslint-disable-next-line no-case-declarations
const { transaction, metadata, chainId } = request.params as Record<
string,
any
>;
const { transaction, chainId } = request.params as Record<string, any>;
return {
origin,
transaction,
metadata,
chainId,
};
case HandlerType.OnRpcRequest:
Expand Down
2 changes: 1 addition & 1 deletion packages/execution-environments/src/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"schema": {
"title": "Handler",
"type": "string",
"enum": ["onRpcRequest", "onTransactionInsight"]
"enum": ["onRpcRequest", "onTransaction"]
}
},
{
Expand Down
14 changes: 7 additions & 7 deletions packages/types/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Json } from '@metamask/controllers';
import { MetaMaskInpageProvider } from '@metamask/providers';
import { JsonRpcRequest } from '@metamask/types';
import { HandlerType, ChainId } from '@metamask/snap-utils';
import { HandlerType } from '@metamask/snap-utils';

/**
* Command request sent to a worker.
Expand All @@ -28,18 +28,16 @@ export type SnapRpcHandler = (args: {

export type OnRpcRequestHandler = SnapRpcHandler;

export type TransactionInsight = {
export type OnTransactionResponse = {
insights: { [key: string]: unknown };
cancel?: boolean;
};

// @todo improve type
export type OnTransactionInsightHandler = (args: {
export type OnTransactionHandler = (args: {
origin: string;
transaction: { [key: string]: unknown };
metadata: { [key: string]: unknown };
chainId: ChainId;
}) => Promise<TransactionInsight>;
}) => Promise<OnTransactionResponse>;

export type SnapRpcHookArgs = {
origin: string;
Expand All @@ -52,6 +50,8 @@ export type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;

export type SnapProvider = MetaMaskInpageProvider;

export type ChainId = string;

export type ErrorJSON = {
message: string;
code: number;
Expand All @@ -60,7 +60,7 @@ export type ErrorJSON = {

export type SnapExports = {
onRpcRequest?: OnRpcRequestHandler;
onTransactionInsight?: OnTransactionInsightHandler;
onTransaction?: OnTransactionHandler;
};

type ObjectParameters<
Expand Down
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"@babel/core": "^7.18.6",
"@metamask/snap-types": "^0.19.0",
"@metamask/utils": "^2.0.0",
"ajv": "^8.11.0",
"fast-deep-equal": "^3.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export enum SNAP_STREAM_NAMES {

export enum HandlerType {
OnRpcRequest = 'onRpcRequest',
OnTransactionInsight = 'onTransactionInsight',
OnTransaction = 'onTransaction',
}

export type SnapId = string;
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2724,7 +2724,7 @@ __metadata:
languageName: unknown
linkType: soft

"@metamask/snap-types@^0.19.1, @metamask/snap-types@workspace:packages/types":
"@metamask/snap-types@^0.19.0, @metamask/snap-types@^0.19.1, @metamask/snap-types@workspace:packages/types":
version: 0.0.0-use.local
resolution: "@metamask/snap-types@workspace:packages/types"
dependencies:
Expand Down Expand Up @@ -2765,6 +2765,7 @@ __metadata:
"@metamask/eslint-config-jest": ^9.0.0
"@metamask/eslint-config-nodejs": ^9.0.0
"@metamask/eslint-config-typescript": ^9.0.1
"@metamask/snap-types": ^0.19.0
"@metamask/utils": ^2.0.0
"@types/jest": ^27.5.1
"@types/semver": ^7.3.10
Expand Down