For a new token to be automatically acquired by the metamask we have the following code:
const tokenAddress = 'CONTRACT_ADDRESS';
const tokenSymbol = 'SYMBOL';
const tokenDecimals = DECIMALS;
const tokenImage = 'URL_ICON';
try {
// wasAdded is a boolean. Like any RPC method, an error may be thrown.
const wasAdded = await window.ethereum.request({
method: 'wallet_watchAsset',
params: {
type: 'ERC20', // Initially only supports ERC20, but eventually more!
options: {
address: tokenAddress, // The address that the token is at.
symbol: tokenSymbol, // A ticker symbol or shorthand, up to 5 chars.
decimals: tokenDecimals, // The number of decimals in the token
image: tokenImage, // A string url of the token logo
},
},
});
if (wasAdded) {
console.log('Thanks for your interest!');
} else {
console.log('Your loss!');
}
}
Now, there is a discomfort. Each time tokens are purchased, this code is activated, regardless of whether it has already been included.
My cuestion is…
Is there a way to detect if metamask already has the token included?