简体   繁体   English

Web3:从地址中检索 ERC20 代币的余额

[英]Web3: retrieving the balance of an ERC20 token from an address

ERR: Returned values aren't valid, did it run Out of Gas? ERR:返回的值无效,它是否用尽了 Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.如果您没有为从中检索数据的合约使用正确的 ABI、从不存在的块号请求数据或查询未完全同步的节点,您也可能会看到此错误。

abi: https://github.com/creed-victor/frontend-test/blob/main/WeenusTokenABI.json abi: https://github.com/creed-victor/frontend-test/blob/main/WeenusTokenABI.json

    const tokenAddress = "0x101848D5C5bBca18E6b4431eEdF6B95E9ADF82FA";

 const web3Context = useWeb3(`wss://mainnet.infura.io/ws/v3/${infuraProjectId}`);

  const { networkId, networkName, accounts, providerName, lib  } = web3Context;

  const web3 = new Web3(`wss://mainnet.infura.io/ws/v3/${infuraProjectId}`)

  const weenusContract = new web3.eth.Contract(abi, tokenAddress);
const getTokenBalance = useCallback(async () => {
    let tokenBalance = accounts && accounts.length > 0 ? await contract.methods.balanceOf(accounts[0]).call().then(receipt=> {return receipt}) : 'unknown';
    setTokenBalance(tokenBalance);
  }, [accounts])

  useEffect(()=>{
    getTokenBalance();
  }, [accounts, getTokenBalance])

Contract isn't responding to the function call.合同没有响应 function 调用。 Is it the abi?是阿比吗? Is there a way I could substitute the abi or improve upon it?有没有办法可以替代 abi 或改进它?

changed const web3 = new Web3( wss://mainnet.infura.io/ws/v3/${infuraProjectId} ) to const web3 = new Web3(window.ethereum)const web3 = new Web3( wss://mainnet.infura.io/ws/v3/${infuraProjectId} )更改为const web3 = new Web3(window.ethereum)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 AccessControl 铸造 ERC20 代币 - ERC20 token minting with AccessControl .allowance 方法用于将 erc20 令牌导入元掩码 - .allowance method for importing erc20 token to metamask 如何在 javascript 函数中允许 ERC20 代币支付 - How can I allow ERC20 token payment inside javascript function 如何更快地更新 React Native 应用程序中的 ERC20 令牌数量? - How to update ERC20 token amounts in React Native application quicker? Solidity 众筹 function 在我的 web 页面上失败,但在直接发送时成功,但在成功交易时不发送 ERC20 - Solidity crowdsale function fails on my web page but is successful when sent directly but doesn't send ERC20 on successful transaction 使用 ethers.js 调用 erc20 批准 function - Call erc20 approve function using ethers.js Ethers js 在合约之间传输 ERC20 - Ethers js transferring ERC20 between contracts 如何在 SOLANA 上使用 web3 js 从令牌地址获取元数据 - How to get Metadata from a Token adress using web3 js on SOLANA 如何在 React 上的 bsc 测试网上将 Metamask 钱包地址连接到 web3 - How to connect Metamask wallet address to web3 on bsc testnet on React Token.networks.networkId] 在 web3 中返回 undefined - Token.networks[networkId] returns undefined in web3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM