与智能合约互动以获取令牌ID
我的平台上有一个功能,可以发现用户是否是特定令牌的持有人,
const [tokenId, setTokenId] = useState(false);
const checkOwners = async () => {
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(contractAddress, abi, signer);
if (currentAccount !== null && nftContract !== null) {
let walletOfowners = await nftContract.tokensOfOwner(currentAccount);
if (walletOfowners.length > 0) {
setTokenId(true);
} else {
setTokenId(false);
}
}
}
};
但我希望我的代码获得连接用户所拥有的令牌(IDS)。智能合约确实具有tokensofowner功能。
//Returns all IDs owned by a particular holder.
function tokensOfOwner(address _owner) external view returns (uint[] memory) {
uint tokenCount = balanceOf(_owner);
uint[] memory tokensId = new uint256[](tokenCount);
for (uint i = 0; i < tokenCount; i++) {
tokensId[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokensId;
}
```
I have a function in my platform that finds out if a user is the holder of a specific token
const [tokenId, setTokenId] = useState(false);
const checkOwners = async () => {
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const nftContract = new ethers.Contract(contractAddress, abi, signer);
if (currentAccount !== null && nftContract !== null) {
let walletOfowners = await nftContract.tokensOfOwner(currentAccount);
if (walletOfowners.length > 0) {
setTokenId(true);
} else {
setTokenId(false);
}
}
}
};
But I want my code to get which tokens (Ids) the connected users hold. Smart contract does have a tokensOfOwner function.
//Returns all IDs owned by a particular holder.
function tokensOfOwner(address _owner) external view returns (uint[] memory) {
uint tokenCount = balanceOf(_owner);
uint[] memory tokensId = new uint256[](tokenCount);
for (uint i = 0; i < tokenCount; i++) {
tokensId[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokensId;
}
```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了我需要的功能:
I found out the function I needed: