ethereum - 检索 web3 contract.ownerOf NFT ERC721
问题描述
我正在玩 web3 和前端,试图从以太坊区块链中读取一些东西。我想做的是验证 NFT (erc721) 的所有者。
我在这里使用了这段代码,它使我能够检查链接到连接的钱包地址的某个 ERC20 的余额
function getERC20TokenBalance(tokenAddress, walletAddress, callback) {
let minABI = [
// balanceOf
{
"constant":true,
"inputs":[{"name":"_owner","type":"address"}],
"name":"balanceOf",
"outputs":[{"name":"balance","type":"uint256"}],
"type":"function"
},
// decimals
{
"constant":true,
"inputs":[],
"name":"decimals",
"outputs":[{"name":"","type":"uint8"}],
"type":"function"
}
];
let contract = web3.eth.contract(minABI).at(tokenAddress);
contract.balanceOf(walletAddress, (error, balance) => {
// ERC20トークンの decimals を取得
contract.decimals((error, decimals) => {
balance = balance.div(10**decimals);
console.log(balance.toString());
callback(balance);
});
});
}
setInterval(function() {
let tokenAddress = '0x50f5474724e0ee42d9a4e711ccfb275809fd6d4a';
let walletAddress = web3.eth.accounts[0];
if(tokenAddress != "" && walletAddress != "") {
getERC20TokenBalance(tokenAddress, walletAddress, (balance) => {
document.getElementById('text-bal').innerText = balance.toFixed(3);
});
};
}, 100);
现在我想验证某个 NFT,我使用了 0x50f5474724e0ee42d9a4e711ccfb275809fd6d4a(SANDBOX 土地合同)。使用 balanceOf,它确实显示该地址中有 LAND NFT,但我想检查 tokenID,将其更改为
let contract = web3.eth.contract(minABI).at(tokenAddress);
contract.ownerOf('18429');
它什么也没返回,你们能指出我正确的方向吗:)?
解决方案
代币不在合约的余额上,它们在合约里面。根据erc721 - 你必须调用balanceOf
合约的方法。
推荐阅读
- html - 如何对齐三个div?
- python - 条件检查 item.mount 失败
- c# - C#:什么正则表达式可以匹配这些文本?
- python - 在熊猫的数字前加零
- php - Joomla 模块未显示在自定义模板上
- java - 为什么我会收到 UndeliverableException
- cakephp-4.x - CakePHP 4.0.3 中的路由问题“找不到匹配 X 的路由”
- c# - Google Firestore 设置值失败
- javascript - React JS:从 json 对象填充 rsuite 下拉列表
- python - admin.site.register(Question) doesn't work