[英]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.