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

BREAKING: Transaction Insight API #642

merged 52 commits into from
Aug 23, 2022

Conversation

FrederikBolding
Copy link
Member

@FrederikBolding FrederikBolding commented Jul 19, 2022

Adds support for multiple handlers/exports on the BaseSnapExecutor and adds a new export to be used for transaction insight. Also adds an endowment that will work as a flag to detect whether the snap wants to receive transaction insight requests.

JS Implementation:

module.exports.onTransaction = async ({ origin, transaction, chainId }) => {
// do something
return { insights }
}

TS Implementation:

import { OnTransactionHandler } from '@metamask/snap-types';

export const onTransaction: OnTransactionHandler = ({ origin, transaction, chainId }) => {
// do something
return { insights }
}

This PR is breaking because some exports are renamed and moved around. For example, `@metamask/execution-environments' no longer exports any enums.

@FrederikBolding FrederikBolding changed the title TX Insights TX Insight Jul 19, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jul 19, 2022

Codecov Report

Merging #642 (c524b38) into main (d81a713) will increase coverage by 0.04%.
The diff coverage is 86.58%.

@@            Coverage Diff             @@
##             main     #642      +/-   ##
==========================================
+ Coverage   85.75%   85.79%   +0.04%     
==========================================
  Files         108      107       -1     
  Lines        3165     3197      +32     
  Branches      612      621       +9     
==========================================
+ Hits         2714     2743      +29     
- Misses        421      424       +3     
  Partials       30       30              
Impacted Files Coverage Δ
...kages/controllers/src/services/ExecutionService.ts 100.00% <ø> (ø)
packages/controllers/src/utils.ts 100.00% <ø> (ø)
...ution-environments/src/common/endowments/crypto.ts 100.00% <ø> (ø)
packages/rpc-methods/src/restricted/invokeSnap.ts 0.00% <0.00%> (ø)
packages/rpc-methods/src/restricted/manageState.ts 0.00% <0.00%> (ø)
packages/utils/src/manifest.ts 100.00% <ø> (ø)
...ages/execution-environments/src/common/commands.ts 93.02% <83.33%> (-6.98%) ⬇️
...ntrollers/src/services/AbstractExecutionService.ts 90.41% <100.00%> (-0.07%) ⬇️
...ges/controllers/src/services/iframe/test/server.ts 85.71% <100.00%> (-0.50%) ⬇️
packages/controllers/src/snaps/SnapController.ts 94.68% <100.00%> (-0.18%) ⬇️
... and 13 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach makes sense. In addition to inline comments, a couple of things:

  1. We should consider how we're going to route messages when snaps are able to export objects with methods, like the keyring. We don't need to support that here, but we shouldn't make it more difficult to implement in the future.
  2. Only tangentially related to this PR, but do we throw an error if a snap exports something we don't recognize? We should create an issue for that if we don't.

packages/controllers/src/snaps/SnapController.ts Outdated Show resolved Hide resolved
packages/controllers/src/snaps/endowments/constants.ts Outdated Show resolved Hide resolved
packages/controllers/src/snaps/SnapController.ts Outdated Show resolved Hide resolved
packages/controllers/src/snaps/SnapController.ts Outdated Show resolved Hide resolved
@FrederikBolding
Copy link
Member Author

  1. We should consider how we're going to route messages when snaps are able to export objects with methods, like the keyring. We don't need to support that here, but we shouldn't make it more difficult to implement in the future.

Hmm, yeah I'm not entirely sure how we would do that 🤔 - Will think more about it.

  1. Only tangentially related to this PR, but do we throw an error if a snap exports something we don't recognize? We should create an issue for that if we don't.

We don't, but only the exports with the proper names are registered as available exports in the BaseSnapExecutor (as of this PR). Is there a reason to throw?

@FrederikBolding FrederikBolding changed the title TX Insight Transaction Insight API Jul 21, 2022
@FrederikBolding FrederikBolding marked this pull request as ready for review July 21, 2022 12:08
@FrederikBolding FrederikBolding requested a review from a team as a code owner July 21, 2022 12:08
@hmalik88 hmalik88 dismissed ritave’s stale review August 22, 2022 18:41

addressed changes.

ritave
ritave previously approved these changes Aug 22, 2022
packages/types/src/types.d.ts Outdated Show resolved Hide resolved
packages/types/src/types.d.ts Outdated Show resolved Hide resolved
rekmarks
rekmarks previously approved these changes Aug 23, 2022
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines +20 to +50
// TODO: Add validation in cases.
/**
* Formats the arguments for the given handler.
*
* @param origin - The origin of the request.
* @param handler - The handler to pass the request to.
* @param request - The request object.
* @returns The formatted arguments.
*/
function getHandlerArguments(
origin: Origin,
handler: HandlerType,
request: JsonRpcRequest<unknown[] | { [key: string]: unknown }>,
): InvokeSnapArgs {
switch (handler) {
case HandlerType.OnTransaction: {
const { transaction, chainId } = request.params as Record<string, any>;
return {
origin,
transaction,
chainId,
};
}

case HandlerType.OnRpcRequest:
return { origin, request };

default:
return assertExhaustive(handler);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference: Is there any reason not to have validation in these cases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by validation? At the snapRpc level we do a isHandler check before passing on the handler to getHandlerArguments.

@rekmarks rekmarks changed the title Transaction Insight API BREAKING: Transaction Insight API Aug 23, 2022
@rekmarks
Copy link
Member

@hmalik88 note that I removed the ChainId type because it was ultimately just a string and it was unclear if it referred to an Ethereum chain id or a CAIP chain id. The SnapId should also be removed under the same reasoning, but that's too big of a change for right now.

We should really just use string when something can be any string, and then use template string types (e.g. 0x${string}) if we want something more specific.

@hmalik88
Copy link
Contributor

@hmalik88 note that I removed the ChainId type because it was ultimately just a string and it was unclear if it referred to an Ethereum chain id or a CAIP chain id. The SnapId should also be removed under the same reasoning, but that's too big of a change for right now.

We should really just use string when something can be any string, and then use template string types (e.g. 0x${string}) if we want something more specific.

Okay, I'm cool with the change. Can't use template string types for certain things though like the CAIP id.

@hmalik88 hmalik88 merged commit b7d3b81 into main Aug 23, 2022
@hmalik88 hmalik88 deleted the fb/tx-insights branch August 23, 2022 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants