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