我无法使用Web3JS从智能合同中检索合同余额。我也没搞错。
除了balanceOf之外,我还尝试调用了其他几个函数,它们都返回了以下内容:
符号(KWeak):WeakReference {}。
下面是我使用的代码:
contract.methods.balanceOf(address).call((err, result) => {console.log(result)})
Promise {
<pending>,
domain:
Domain {
domain: null,
_events:
[Object: null prototype] {
removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [],
[Symbol(kWeak)]: WeakReference {} } }预期的结果应该是给定地址帐户的余额。
发布于 2022-09-03 10:16:12
我知道你找到了答案,但你只知道如何去寻找这个问题。您只需使用.then来解决您的承诺,我们将使用异步/等待语法来解决这个问题,如下所示。
contract.methods.balanceOf(address).call().then((balance) => console.log(balance))或
async function getBalnce(){
const contract = await new web3Instance.eth.Contract(contractABI,contractAddress);
const balance = await contract.methods.balanceOf(address).call();
return balance;
}https://stackoverflow.com/questions/56916673
复制相似问题