reactjs 从地址中检索ERC20令牌的余额

xuo3flqw  于 2022-11-29  发布在  React
关注(0)|答案(1)|浏览(243)

错误:返回的值无效,是否用完了汽油?如果您没有为从中检索数据的合同使用正确的ABI,从不存在的块号请求数据,或者查询未完全同步的节点,也可能会看到此错误。
数据类型

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])

合约没有回应函数调用。是ABI的问题吗?有没有方法可以替换ABI或者改进它?

z8dt9xmd

z8dt9xmd1#

我变了

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

const web3 = new Web3(window.ethereum)

相关问题