admin管理员组文章数量:1401149
I make login with metamask on my website (Laravel & Vuejs). I installed Web3 and doing actions such as getAccounts, Sign , getBalance and etc.
But I want to get all tokens(Such as BNB, RARI and etc) balance in metamask. I wrote this code:
// accounts[0] => default wallet number
window.web3.eth.getBalance(accounts[0], (err, b) => {
if (err)
console.log(err);
let balance = {};
balance.ether = window.web3.utils.fromWei(b, "ether");
});
But just return ETH Token Balance not all tokens.
How do i get all tokens balance? Can you help me?
I make login with metamask on my website (Laravel & Vuejs). I installed Web3 and doing actions such as getAccounts, Sign , getBalance and etc.
But I want to get all tokens(Such as BNB, RARI and etc) balance in metamask. I wrote this code:
// accounts[0] => default wallet number
window.web3.eth.getBalance(accounts[0], (err, b) => {
if (err)
console.log(err);
let balance = {};
balance.ether = window.web3.utils.fromWei(b, "ether");
});
But just return ETH Token Balance not all tokens.
How do i get all tokens balance? Can you help me?
Share Improve this question edited Aug 17, 2022 at 18:29 miken32 42.8k16 gold badges125 silver badges174 bronze badges asked May 3, 2021 at 5:35 AminAmin 3971 gold badge6 silver badges19 bronze badges 1- You must check the amount by each token address and wallet address – Amin Commented Jun 19, 2021 at 19:15
1 Answer
Reset to default 5As you already figured out in the ments, nobody knows all the token balances of your account, not even MetaMask. This is due to the fact that the tokens do not reside in your account, but in the token smart contract which tracks your token balance.
Therefore, you have to check each token's contract for the account you are querying to get the token balance. Which brings us to the next issue: How do we know each token's contract address?
Wallets, such as MyCrypto or MetaMask maintain their own whitelists of well-known token contracts. The ethereum-lists
collective has you covered for ERC-20 tokens:
https://github./ethereum-lists/tokens
It currently lists more than 2000 tokens for Ethereum and you can either pick your favorites or parse them all. Each token has a JSON spec definition containing the most important parameters, e.g.:
{
"symbol": "TUSD",
"name": "TrueUSD",
"type": "ERC20",
"address": "0x0000000000085d4780B73119b644AE5ecd22b376",
"ens_address": "",
"decimals": 18,
"website": "https://www.trusttoken.",
"logo": {
"src": "",
"width": "",
"height": "",
"ipfs_hash": ""
},
"support": {
"email": "[email protected]",
"url": ""
},
"social": {
"blog": "https://blog.trusttoken.",
"chat": "",
"facebook": "",
"forum": "",
"github": "https://github./trusttoken",
"gitter": "",
"instagram": "",
"linkedin": "",
"reddit": "https://www.reddit./r/TrustToken/",
"slack": "",
"telegram": "https://t.me/joinchat/HihkMkTja1gIyBRM1J1_vg",
"twitter": "https://twitter./TrustToken",
"youtube": ""
}
}
Source: https://github./ethereum-lists/tokens/blob/c11d278944dc66e95b3b1c44786676b697c84b0a/tokens/eth/0x0000000000085d4780B73119b644AE5ecd22b376.json
本文标签: javascriptHow do I get all the tokens inside my Metamask wallet by web3 or othersStack Overflow
版权声明:本文标题:javascript - How do I get all the tokens inside my Metamask wallet by web3 or others? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744239502a2596719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论