简体   繁体   English

我如何在 usdt 中获得余额?

[英]How do I get a balance in usdt?

My code:我的代码:

const tokens = {
  usdt: "0xbA6879d0Df4b09fC678Ca065c00dd345AdF0365e"
}
const minABI = [
  {
    constant: true,
    inputs: [{ name: "_owner", type: "address" }],
    name: "balanceOf",
    outputs: [{ name: "balance", type: "uint256" }],
    type: "function",
  },
];
async function getUsdtBalance() {
  const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
  const Web3Client = new Web3(window.ethereum);

  const contract = new Web3Client.eth.Contract(minABI, tokens.usdt)

  const result = await contract.methods.balanceOf(accounts[0]).call();
  const format = Web3Client.utils.fromWei(result);
  console.log(format)
}

Error: Returned values aren't valid, did it run Out of Gas?错误:返回的值无效,是否耗尽了 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、从不存在的块号请求数据或查询未完全同步的节点,您也可能会看到此错误。

It's a test network.这是一个测试网络。 I have 99 eth, and 0 usdt.我有 99 个 eth 和 0 个 usdt。

Most likely an error in the ABI, I myself do not understand anything, I took it from the guide.很可能是 ABI 中的一个错误,我自己什么都不懂,我是从指南中获取的。

It looks like the code is working.看起来代码正在运行。 I also think there is a problem with the ABI.我也认为ABI有问题。 I would change the ABI to something like this:我会将 ABI 更改为如下所示:

const minABI = [
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "account",
          "type": "address"
        }
      ],
      "name": "balanceOf",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    }
];

I have added a field called stateMutability .我添加了一个名为stateMutability的字段。 This field allows you to define how the function should interact with the data in the blockchain.此字段允许您定义函数应如何与区块链中的数据交互。 In our case we are not modifying the data, but only reading it, so we set the value to view .在我们的例子中,我们没有修改数据,而只是读取它,所以我们将值设置为view

If there is still a problem, it may make sense to check the token address.如果仍然存在问题,检查令牌地址可能是有意义的。

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

相关问题 如何使用 useEffect 获取合约余额? - How do I grab Contract Balance with useEffect? (Javascript RESTful 获取函数)如何查询参数以从账户映射中获取账户余额 - (Javascript RESTful get function) How do I query param to get account balance from mapping of accounts 如何使用 NodeJs 在投资 web 应用程序中操纵账户余额? - How do I manipulate account balance in an investment web application with NodeJs? 如何自动化 USDT 发送交易? - How to automate USDT sending transactions? 如何使用智能合约获得正确的余额? - How to get the correct balance using a smart contract? 如何使用 BURSTJS 获取 BURST 账户余额? - How to get BURST acount balance using BURSTJS? 如何减少我的 function? 我收到以下错误:Expect to use destructuring assignment - {balance} - How to reduce my function? I get the following error : Expect to use destructuring assignment - {balance} 在javascript中,如何在控制总金额的同时,向用户的余额添加随机数? - In javascript, how do I add a random amount to a user's balance while controlling how much gets given total? 我如何才能从调用者的余额转移到合约自己的余额? - How can I transfer near from the caller's balance to the contract's own balance? 如何在 USDT 代币转账事件中订阅多个 Eth 地址 - How to subscribe to Multiple Eth addresses on USDT token transfer events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM