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

Disaggregate the detection param for addNft() #1417

Merged
merged 5 commits into from
Jun 9, 2023

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented Jun 7, 2023

This PR decouples the detection parameter passed to the addNft method on the NftController, introducing more granular control over the logic within the function. Now instead of receiving a single detection argument as { chainId, accountAddress } the presence of which was meant to both indicate that the nft being added was detected and which account/chainId to add the NFT to in state, there are two separate arguments accountParams and `source.

This allows us to be more clear and explicit about the source of the NFT being added (manually, via dapp, or auto-detection) which in turn allows us finer tuned logic control and event handling for those different sources.

Changelog entries:

@metamask/approval-controller

  • FIXED: Fix ApprovalController constructor so that it accepts a messenger created via a getRestricted without explicitly specifying type parameters

@metamask/assets-controller

  • ADDED: Add a fifth parameter to addNft in NftController which can be used to specify whether the NFT was detected, added manually, or suggested by a dapp
  • FIXED: Fix watchNft in NftController to ensure that if the network changes before the user accepts the request, the NFT is added to the chain ID and address before the request was initiated

@adonesky1 adonesky1 requested a review from a team as a code owner June 7, 2023 20:50
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Makes sense. Just one question.

userAddress: selectedAddress,
chainId,
});
await this.addNft(
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there existing tests covering this? If so do they need to be extended to verify that these changes produce the desired effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so I have a PR pointed at this one to test atleast part of the additions here.

You're right we should test this a bit more explicitly though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah gotcha, okay. I'll take a look at that one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

more tests added here: 7b54bcf

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah. That's good - but are there any tests that need to be added for NftDetectionController specifically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added one here: 4e9b1ee#diff-c48a7b91e7a594a25f0868505f6dd588bbfc783890f0ee2c381399daeba3e04eR316

NftDetection behavior should be unchanged but we should have had this test before.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see. Cool. Thank you!

mcmire
mcmire previously approved these changes Jun 8, 2023
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

I had just one suggestion, but this otherwise looks good.

Would you mind adding a little changelog section to the PR description? That helps us when compiling the changelog for the new release. I'd imagine it would be something like:

## `@metamask/approval-controller`

- **FIXED:** Fix ApprovalController constructor so that it accepts a messenger created via a `getRestricted` without explicitly specifying type parameters

## `@metamask/assets-controller`

- **ADDED:** Add a fifth parameter to `addNft` in NftController which can be used to specify whether the NFT was detected, added manually, or suggested by a dapp
- **FIXED:** Fix `watchNft` in NftController to ensure that if the network changes before the user accepts the request, the NFT is added to the chain ID and address before the request was initiated
- **FIXED:** Fix `detectNfts` in NftDetectionController 

@@ -116,3 +116,9 @@ export const NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP: Record<
[NetworkId.sepolia]: NetworkType.sepolia,
[NetworkId.mainnet]: NetworkType.mainnet,
};

export enum Source {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this type is only used within assets-controllers, can it live inside there? Also, I wonder if this name is too generic — I can see it clashing with something else in the future. What do you think about NftSource?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I was thinking that it might be useful for other controllers (particularly for metrics). But yeah I suppose those other controllers that would use it would probably be in the assets-controllers package. Do you think we should create a shared constants file for that package? And regarding the name I was thinking we keep it generic again because it can probably be used for other non-nft specific cases in the future. But perhaps we can just rename it when it gets used as such?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

did something like this here: 56466f2

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I was caught up in meetings today. I think that's better!

@adonesky1 adonesky1 requested a review from mcmire June 8, 2023 20:18
@adonesky1 adonesky1 merged commit 7b32741 into main Jun 9, 2023
@adonesky1 adonesky1 deleted the decouple-detection-param branch June 9, 2023 00:41
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
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