admin管理员组文章数量:1345303
I would like to query several ERC20 tokens on the RSK network to obtain the following fields: symbol
, name
, and decimals
.
How can I do this using web3.js?
I would like to query several ERC20 tokens on the RSK network to obtain the following fields: symbol
, name
, and decimals
.
How can I do this using web3.js?
Share Improve this question edited Sep 26, 2021 at 8:56 bguiz 28.4k49 gold badges163 silver badges253 bronze badges asked Jun 2, 2021 at 13:45 MiltonMilton 1711 gold badge1 silver badge4 bronze badges2 Answers
Reset to default 8To do this using web3.js:
web3
: Have a web3 instance initialised and connected to a web3 providerabiOfToken
: The ABI object for this particular token. Note that if you do not have this, you can obtain it by runningsolc
against the original contract code; or alternatively you can simply use a "standard" ABI object for ERC-20 tokensaddressOfToken
: The deployed smart contract address for the token
Once you have the above, you can do the following within an async
function:
const tokenContract = new web3.eth.Contract(
abiOfToken, addressOfToken);
const symbol = await tokenContract.methods.symbol().call();
const decimals = await tokenContract.methods.decimals().call();
const name = await tkenContract.methods.name().call();
The above code runs them in sequence, and provided for clarity. In practice, since you are running this for multiple tokens, you may want to consider running the queries in parallel, and extracting them to a separate function, like so:
// run this just once, as part of initialisation
const tokenContract = new web3.eth.Contract(abiOfToken, addressOfToken);
// run this multiple times by putting in its own function
async function getTokenInfo(tokenContract) {
const [decimals, name, symbol] = await Promise.all([
tokenContract.methods.symbol().call(),
tokenContract.methods.decimals().call(),
tokenContract.methods.name().call(),
]);
return { decimals, name, symbol };
}
// ERC20 ABI (Standard ABI für ERC20 Tokens)
const ERC20_ABI = [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{"name": "","type": "string"}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{"name": "","type": "string"}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{"name": "","type": "uint8"}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{"name": "","type": "uint256"}],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
// Replace with the ERC20 token address you want to query
const tokenAddress = "0x69352631646fB9471F84aeb31E2A15bf7c069868";
async function getTokenDetails() {
try {
const tokenContract = new web3.eth.Contract(ERC20_ABI, tokenAddress);
// Fetch token details
const [name, symbol, decimals, totalSupply] = await Promise.all([
tokenContract.methods.name().call(),
tokenContract.methods.symbol().call(),
tokenContract.methods.decimals().call(),
tokenContract.methods.totalSupply().call()
]);
console.log(`Token Name: ${name}`);
console.log(`Token Symbol: ${symbol}`);
console.log(`Decimals: ${decimals}`);
console.log(`Total Supply: ${totalSupply}`);
} catch (error) {
console.error("Error fetching token details:", error);
}
}
getTokenDetails();
本文标签: javascriptHow can I get info such as symbol of an ERC20 token on RSKStack Overflow
版权声明:本文标题:javascript - How can I get info such as symbol of an ERC20 token on RSK? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743777775a2537297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论