簡體   English   中英

如何在我的 MetaMask 錢包中正確獲取不同代幣的余額?

[英]How to properly get the balance of different tokens in my MetaMask wallet?

如何在我的 MetaMask 錢包中獲得像 Alpaca 這樣的代幣余額? 谷歌搜索我發現了一段代碼,但它返回了這個錯誤:

TypeError: contract.balanceOf is not a function

這是代碼:

let minABI = [
  // balanceOf
  {
    "constant":true,
    "inputs":[{"name":"_owner","type":"address"}],
    "name":"balanceOf",
    "outputs":[{"name":"balance","type":"uint256"}],
    "type":"function"
  },
];

let alpacaAddress = "0x8F0528cE5eF7B51152A59745bEfDD91D97091d2F";

let contract = new web3.eth.Contract(minABI, alpacaAddress);
contract.balanceOf(MyWallet, (error, balance) => {
    console.log(balance.toString());
});

知道為什么 contract.balanceOf 不是函數嗎?

Web3js 使用methods輔助屬性。 此外,您還需要使用.call()函數來執行只讀調用。

所以在你的情況下:

contract.methods.balanceOf().call()

文檔: https : //web3js.readthedocs.io/en/v1.3.4/web3-eth-contract.html#methods-mymethod-call


似乎您調用balanceOf()代碼段使用了來自 Truffle ( docs ) 的語法,這是一個不同的庫,因此它不適用於 web3 Contract 實例 ( new web3.eth.Contract() )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM