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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

To do this using web3.js:

  • web3: Have a web3 instance initialised and connected to a web3 provider
  • abiOfToken: The ABI object for this particular token. Note that if you do not have this, you can obtain it by running solc against the original contract code; or alternatively you can simply use a "standard" ABI object for ERC-20 tokens
  • addressOfToken: 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