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

Add metamask_watchAsset #4606

Merged
merged 41 commits into from
Aug 28, 2018
Merged

Add metamask_watchAsset #4606

merged 41 commits into from
Aug 28, 2018

Conversation

danfinlay
Copy link
Contributor

@danfinlay danfinlay commented Jun 18, 2018

Adds the method metamask_watchAsset, a vendor-prefixed version of wallet_watchAsset per EIP 747.

To Do:

  • Refine & finalize the EIP specification.
  • Fix rpc-engine crash preventing the confirmation from working correctly.
  • Add parameter for specifying token icon (maybe as a base64 string w/ a max length)

Sample Usage

web3.currentProvider.sendAsync({
  method: 'metamask_watchAsset',
  params: {
    "type":"ERC20",
    "options":{
      "address":"0x2e8c6bbe8e3aa934ee5a851b2cdff52412d61b86",
      "symbol":"FOX",
      "decimals":18,
      "image":'https://pbs.twimg.com/profile_images/802481220340908032/M_vde_oi_400x400.jpg'
    },
  },
  id: 20,
}, console.log)

@danfinlay danfinlay added the DO-NOT-MERGE Pull requests that should not be merged label Jun 18, 2018
@estebanmino
Copy link
Contributor

estebanmino commented Aug 3, 2018

Right now this PR is working as follow

ezgif com-video-to-gif

@cjeria @danfinlay any thoughts on how it should be the cancellation of this suggestion?

@estebanmino
Copy link
Contributor

ezgif com-video-to-gif 1

A new component was created to confirm the suggested tokens as you can see on the video. I think that could be a better approach for it. Thoughts?

Now we have the possibility to developers that want to use this feature to send a personal message through the method to the user, giving more information about this suggested token (?).

@danfinlay @cjeria

@metamaskbot
Copy link
Collaborator

Builds ready [15ea8c0]: mascara, chrome, firefox, edge, opera

@danfinlay
Copy link
Contributor Author

Okay, three thoughts:

  • I'd like to get @cjeria to review the UX here. I think this might be worth some dedicated UI, since "back" and selecting other tokens is not really in the spirit of the feature.
  • I'd like us to tuck the method under some kind of beta flag until we have an EIP and want to formalize: Maybe metamask_watchToken? Is that crazy?
  • I'd like the method to support a token image also. Not sure whether it should be a bytes64 encoded jpeg, or a link to a hosted image or what. That might be a good question for an ethereum-magicians post. I'm happy to help draft that.

@estebanmino
Copy link
Contributor

Following your comments @danfinlay

  • There are new components for this feature, in order to avoid the Back button there is a Cancel button that closes the notification. Working on new and old UI.

43747188-62926b74-99b6-11e8-894f-fbb2f4c79566

  • Sounds good, not crazy for me at least.
  • How do we continue with this?

@danfinlay danfinlay removed the DO-NOT-MERGE Pull requests that should not be merged label Aug 23, 2018
@danfinlay danfinlay changed the title [DO NOT MERGE] - Begin adding eth_watchToken Add wallet_watchAsset Aug 23, 2018
@danfinlay danfinlay changed the title Add wallet_watchAsset Add metamask_watchAsset Aug 23, 2018
Copy link
Contributor

@brunobar79 brunobar79 left a comment

Choose a reason for hiding this comment

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

LGTM! just a few strings that should be moved to the localization files and a potential safe check.

@@ -247,7 +306,8 @@ class PreferencesController {
} else {
tokens.push(newEntry)
}
this._updateAccountTokens(tokens)
assetImages[address] = image
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if it's really needed but maybe use toChecksumAddress or normalizeAddress to make sure it will always find the right key (assuming that assetImages keys are already checksummed)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, in line 295 the normalization happens, so for each token added it will have as key the correct normalized address.


const ownAddress = identitiesList.includes(standardAddress)
if (ownAddress) {
msg = 'Personal address detected. Input the token contract address.'
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with all these comments about messages.json file, but the original old ui (old-ui/app/add-tokens.js) all names are in this way and are not used to do translations, so I assumed that this wasn't necessary. Let me know what you think.

Copy link
Contributor

@brunobar79 brunobar79 Aug 28, 2018

Choose a reason for hiding this comment

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

Disregard all the previous comments about localization. Didn't realize it was old-ui

sandbox.restore()
})

it('should do anything if method not corresponds', async function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't ?

Choose a reason for hiding this comment

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

Updated

Choose a reason for hiding this comment

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

bitpshr
bitpshr previously approved these changes Aug 27, 2018
Copy link
Contributor

@bitpshr bitpshr left a comment

Choose a reason for hiding this comment

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

This looks great. There are a few strings that can be internationalized, but nothing show-stopping. Excited to see this feature in the wild.

One question: are we standardizing on the metamask_ custom RPC method prefix? Should we version this as well in case new versions of the proposal arise?

@@ -1,5 +1,6 @@
const ObservableStore = require('obs-store')
const normalizeAddress = require('eth-sig-util').normalize
const isValidAddress = require('ethereumjs-util').isValidAddress
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: You can save one level of indirection by doing const { isValidAddress } = require('ethereumjs-util');

@metamaskbot
Copy link
Collaborator

Builds ready [3106374]: mascara, chrome, firefox, edge, opera

@estebanmino
Copy link
Contributor

Thanks for reviewing this.
@bitpshr the idea of that prefix is to have the first version of this before watchAsset is standardized on EM, we want to test and find room to improve it.
Waiting for @brunobar79 comments to merge.

brunobar79
brunobar79 previously approved these changes Aug 28, 2018
Copy link
Contributor

@brunobar79 brunobar79 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! Ping me after resolving conflicts and I'll merge

@ghost
Copy link

ghost commented Apr 24, 2021

Why is there a Character Limit for the symbol? e.g. Tokens with a character length of 8 can be imported into Metamask via the address but not with this method.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 24, 2021

CLA Signature Action:

Thank you for your submission, we really appreciate it. We ask that you all read and sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just by adding a comment to this pull request with this exact sentence:

I have read the CLA Document and I hereby sign the CLA

By commenting with the above message you are agreeing to the terms of the CLA. Your account will be recorded as agreeing to our CLA so you don't need to sign it again for future contributions to this repository.

1 out of 2 committers have signed the CLA.
@danfinlay
@estebanmino

@df257
Copy link

df257 commented Nov 24, 2021

I want add two token together, how to do it?
like this:
image

@hiroshi-yamamoto-dublr
Copy link

hiroshi-yamamoto-dublr commented Jul 21, 2022

Trying to add the same token twice gives the following error:

image

This warning should not be given if the token was already added to the wallet with the same chainId and token address (in fact the MetaMask window should probably not pop up at all in that case). This way the caller doesn't have to first call the MetaMask API to see if the token is already in the wallet (if that is even possible?) before calling the EIP747 API.

@Winrargivemebot
Copy link

ethereum.request({
method: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address: '0x02bfed0e1b1186e80ea3c74b43b22ffde8f8238a',
symbol: '$PEN',
decimals: 18,
image: 'https://i.ibb.co/pfVjJCV/PENGUIN.png',
},
},
});
.then((success) => {
if (success) {
console.log('FOO successfully added to wallet!')
} else {
throw new Error('Something went wrong.')
}
})
.catch(console.error)

@WojasKrk
Copy link

Builds ready [56bed3f]: mascara, chrome, firefox, edge, opera

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.

10 participants