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

feat: Add firstTimeInteraction to transactionMeta #4895

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

OGPoyraz
Copy link
Member

@OGPoyraz OGPoyraz commented Nov 5, 2024

Explanation

This PR adds firstTimeInteraction to transactionMeta by calling https://primitives.api.cx.metamask.io/ API
relationships endpoint.
For more information please see docs here: https://primitives.api.cx.metamask.io/docs#/Account%20%7C%20Relationships/IndexedChainDataAPIController_getAccountAddressRelationship

References

Related to: https://github.com/MetaMask/MetaMask-planning/issues/3040

Changelog

@metamask/transaction-controller

  • ADDED: Add firstTimeInteraction to transactionMeta
    • This field is getting filled by calling https://primitives.api.cx.metamask.io relationships endpoint.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@OGPoyraz
Copy link
Member Author

OGPoyraz commented Nov 5, 2024

@metamaskbot publish-preview

Copy link
Contributor

github-actions bot commented Nov 5, 2024

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.3-preview-35c9c85",
  "@metamask-previews/address-book-controller": "6.0.1-preview-35c9c85",
  "@metamask-previews/announcement-controller": "7.0.1-preview-35c9c85",
  "@metamask-previews/approval-controller": "7.1.1-preview-35c9c85",
  "@metamask-previews/assets-controllers": "42.0.0-preview-35c9c85",
  "@metamask-previews/base-controller": "7.0.2-preview-35c9c85",
  "@metamask-previews/build-utils": "3.0.1-preview-35c9c85",
  "@metamask-previews/chain-controller": "0.1.3-preview-35c9c85",
  "@metamask-previews/composable-controller": "9.0.1-preview-35c9c85",
  "@metamask-previews/controller-utils": "11.4.2-preview-35c9c85",
  "@metamask-previews/ens-controller": "15.0.0-preview-35c9c85",
  "@metamask-previews/eth-json-rpc-provider": "4.1.6-preview-35c9c85",
  "@metamask-previews/gas-fee-controller": "22.0.0-preview-35c9c85",
  "@metamask-previews/json-rpc-engine": "10.0.1-preview-35c9c85",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.5-preview-35c9c85",
  "@metamask-previews/keyring-controller": "17.3.1-preview-35c9c85",
  "@metamask-previews/logging-controller": "6.0.1-preview-35c9c85",
  "@metamask-previews/message-manager": "11.0.1-preview-35c9c85",
  "@metamask-previews/multichain": "0.0.0-preview-35c9c85",
  "@metamask-previews/name-controller": "8.0.1-preview-35c9c85",
  "@metamask-previews/network-controller": "22.0.1-preview-35c9c85",
  "@metamask-previews/notification-controller": "7.0.0-preview-35c9c85",
  "@metamask-previews/notification-services-controller": "0.12.1-preview-35c9c85",
  "@metamask-previews/permission-controller": "11.0.3-preview-35c9c85",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-35c9c85",
  "@metamask-previews/phishing-controller": "12.3.0-preview-35c9c85",
  "@metamask-previews/polling-controller": "12.0.1-preview-35c9c85",
  "@metamask-previews/preferences-controller": "13.2.0-preview-35c9c85",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-35c9c85",
  "@metamask-previews/queued-request-controller": "7.0.0-preview-35c9c85",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-35c9c85",
  "@metamask-previews/selected-network-controller": "19.0.0-preview-35c9c85",
  "@metamask-previews/signature-controller": "21.0.0-preview-35c9c85",
  "@metamask-previews/transaction-controller": "38.2.0-preview-35c9c85",
  "@metamask-previews/user-operation-controller": "17.0.0-preview-35c9c85"
}

@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.3-preview-3f52b814",
  "@metamask-previews/address-book-controller": "6.0.1-preview-3f52b814",
  "@metamask-previews/announcement-controller": "7.0.1-preview-3f52b814",
  "@metamask-previews/approval-controller": "7.1.1-preview-3f52b814",
  "@metamask-previews/assets-controllers": "42.0.0-preview-3f52b814",
  "@metamask-previews/base-controller": "7.0.2-preview-3f52b814",
  "@metamask-previews/build-utils": "3.0.1-preview-3f52b814",
  "@metamask-previews/chain-controller": "0.1.3-preview-3f52b814",
  "@metamask-previews/composable-controller": "9.0.1-preview-3f52b814",
  "@metamask-previews/controller-utils": "11.4.2-preview-3f52b814",
  "@metamask-previews/ens-controller": "15.0.0-preview-3f52b814",
  "@metamask-previews/eth-json-rpc-provider": "4.1.6-preview-3f52b814",
  "@metamask-previews/gas-fee-controller": "22.0.0-preview-3f52b814",
  "@metamask-previews/json-rpc-engine": "10.0.1-preview-3f52b814",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.5-preview-3f52b814",
  "@metamask-previews/keyring-controller": "17.3.1-preview-3f52b814",
  "@metamask-previews/logging-controller": "6.0.1-preview-3f52b814",
  "@metamask-previews/message-manager": "11.0.1-preview-3f52b814",
  "@metamask-previews/multichain": "0.0.0-preview-3f52b814",
  "@metamask-previews/name-controller": "8.0.1-preview-3f52b814",
  "@metamask-previews/network-controller": "22.0.1-preview-3f52b814",
  "@metamask-previews/notification-controller": "7.0.0-preview-3f52b814",
  "@metamask-previews/notification-services-controller": "0.12.1-preview-3f52b814",
  "@metamask-previews/permission-controller": "11.0.3-preview-3f52b814",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-3f52b814",
  "@metamask-previews/phishing-controller": "12.3.0-preview-3f52b814",
  "@metamask-previews/polling-controller": "12.0.1-preview-3f52b814",
  "@metamask-previews/preferences-controller": "13.2.0-preview-3f52b814",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-3f52b814",
  "@metamask-previews/queued-request-controller": "7.0.0-preview-3f52b814",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-3f52b814",
  "@metamask-previews/selected-network-controller": "19.0.0-preview-3f52b814",
  "@metamask-previews/signature-controller": "21.0.0-preview-3f52b814",
  "@metamask-previews/transaction-controller": "38.2.0-preview-3f52b814",
  "@metamask-previews/user-operation-controller": "17.0.0-preview-3f52b814"
}

@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "19.0.0-preview-fefbd325",
  "@metamask-previews/address-book-controller": "6.0.1-preview-fefbd325",
  "@metamask-previews/announcement-controller": "7.0.1-preview-fefbd325",
  "@metamask-previews/approval-controller": "7.1.1-preview-fefbd325",
  "@metamask-previews/assets-controllers": "43.0.0-preview-fefbd325",
  "@metamask-previews/base-controller": "7.0.2-preview-fefbd325",
  "@metamask-previews/build-utils": "3.0.1-preview-fefbd325",
  "@metamask-previews/chain-controller": "0.1.3-preview-fefbd325",
  "@metamask-previews/composable-controller": "9.0.1-preview-fefbd325",
  "@metamask-previews/controller-utils": "11.4.3-preview-fefbd325",
  "@metamask-previews/ens-controller": "15.0.0-preview-fefbd325",
  "@metamask-previews/eth-json-rpc-provider": "4.1.6-preview-fefbd325",
  "@metamask-previews/gas-fee-controller": "22.0.1-preview-fefbd325",
  "@metamask-previews/json-rpc-engine": "10.0.1-preview-fefbd325",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.5-preview-fefbd325",
  "@metamask-previews/keyring-controller": "18.0.0-preview-fefbd325",
  "@metamask-previews/logging-controller": "6.0.2-preview-fefbd325",
  "@metamask-previews/message-manager": "11.0.1-preview-fefbd325",
  "@metamask-previews/multichain": "0.0.0-preview-fefbd325",
  "@metamask-previews/name-controller": "8.0.1-preview-fefbd325",
  "@metamask-previews/network-controller": "22.0.2-preview-fefbd325",
  "@metamask-previews/notification-controller": "7.0.0-preview-fefbd325",
  "@metamask-previews/notification-services-controller": "0.13.0-preview-fefbd325",
  "@metamask-previews/permission-controller": "11.0.3-preview-fefbd325",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-fefbd325",
  "@metamask-previews/phishing-controller": "12.3.0-preview-fefbd325",
  "@metamask-previews/polling-controller": "12.0.1-preview-fefbd325",
  "@metamask-previews/preferences-controller": "14.0.0-preview-fefbd325",
  "@metamask-previews/profile-sync-controller": "1.0.0-preview-fefbd325",
  "@metamask-previews/queued-request-controller": "7.0.1-preview-fefbd325",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-fefbd325",
  "@metamask-previews/selected-network-controller": "19.0.0-preview-fefbd325",
  "@metamask-previews/signature-controller": "22.0.0-preview-fefbd325",
  "@metamask-previews/transaction-controller": "39.0.0-preview-fefbd325",
  "@metamask-previews/user-operation-controller": "18.0.0-preview-fefbd325"
}

@OGPoyraz OGPoyraz marked this pull request as ready for review November 13, 2024 11:19
@OGPoyraz OGPoyraz requested a review from a team as a code owner November 13, 2024 11:19
import { FirstTimeInteractionError } from '../errors';
import { projectLogger } from '../logger';

export const BASE_URL = 'https://accounts.api.cx.metamask.io';
Copy link
Member

Choose a reason for hiding this comment

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

Should the name of this file reflect the API itself so accounts-api.ts?

I've also added this same util (for a different endpoint) for the ongoing incoming transactions refactor, so should we match the name and location to simplify the merge?

https://github.com/MetaMask/core/blob/8b7874cde7e15281638e53170ee0e47329f1c9da/packages/transaction-controller/src/api/accounts-api.ts

): Promise<FirstTimeInteractionResponse> {
const url = await getFirstTimeInteractionUrl(request);

log('Sending request', url, request);
Copy link
Member

Choose a reason for hiding this comment

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

As we'll use additional endpoints for incoming transactions, should we keep logs more specific so Sending first time request?

if (responseJson.error) {
const { message, code } = responseJson.error;

if (message === FAILED_TO_PARSE_MESSAGE) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there an error code we could use instead to keep it less coupled?

if (responseJson.error) {
const { message, code } = responseJson.error;

if (message === FAILED_TO_PARSE_MESSAGE) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a comment to clarify why we can assume this specific error means it's a first time interaction?


return {
isFirstTimeInteraction:
responseJson?.count === 0 || responseJson?.count === undefined,
Copy link
Member

Choose a reason for hiding this comment

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

Will the second condition mean we return true if there is no response JSON at all?

log('Sending request', url, request);

const response = await fetch(url, {
method: 'GET',
Copy link
Member

Choose a reason for hiding this comment

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

I can't find the name currently, but there is a specific header we should use so the API team can track the source of the requests.

let firstTimeInteractionResponse: FirstTimeInteractionResponse;

try {
validateFirstTimeInteraction(request);
Copy link
Member

Choose a reason for hiding this comment

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

The params are validated when we first add the transaction in addTransaction, so why do we need to validate an object generated internally?

@@ -3614,6 +3627,73 @@ export class TransactionController extends BaseController<
return transactionMeta;
}

async #updateFirstInteraction(
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should do anything now, but if these API usages grow, we could standardise this pattern to reduce some duplication.

/**
* Whether the transaction is the first time interaction.
*/
firstTimeInteraction?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Do we also need a firstTimeInteractionLoading property so the UI can more easily differentiate loading and disabled?


firstTimeInteractionResponse = await this.#trace(
{ name: 'FirstTimeInteraction', parentContext: traceContext },
() => getFirstTimeInteraction(request),
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a toggle to disable this or is that a future ticket?

At what point is it easier to provide a single isExternalApiDisabled callback?

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.

2 participants