简体   繁体   English

ERC-721:如何获取所有代币 ID?

[英]ERC-721: How to get all token ids?

I want to iterate over all token ids of a ethereum ERC-721 contract.我想遍历以太坊 ERC-721 合约的所有代币 ID。 Some contracts have counting ids (0, 1, 2, 3, ...) which is easy, but some have random ids, eg https://etherscan.io/token/0xf87e31492faf9a91b02ee0deaad50d51d56d5d4d#inventory有些合约有计数 ID(0、1、2、3,...),这很容易,但有些合约有随机 ID,例如https://etherscan.io/token/0xf87e31492faf9a91b02ee0deaad50d51d56d5d4d#inventory

Sadly etherscan only shows the last 10000 token ids used, but I want to iterate over all 79490. Is there a way to accomplish this?遗憾的是 etherscan 只显示最后使用的 10000 个令牌 ID,但我想遍历所有 79490 个。有没有办法实现这个? For me, everything is fine.对我来说,一切都很好。 Setup my own ethereum node, using some API.使用一些 API 设置我自己的以太坊节点。

You can loop through all Transfer() events emitted by the collection contract.您可以遍历收集合约发出的所有Transfer()事件。

You're looking for transfers from address 0x0 (minted tokens).您正在寻找from地址0x0 (铸造代币)的转账。 And excluding from the list transfers to address 0x0 (destroyed tokens).并且从列表中排除to地址0x0 (销毁的代币)的传输。

One of the ways to achieve this is by using the Web3 Contract getPastEvents() function ( docs ).实现此目的的方法之一是使用 Web3 Contract getPastEvents()函数 ( docs )。

const myContract = new web3.eth.Contract(abiJson, contractAddress);
myContract.getPastEvents('Transfer', {
    filter: {
        _from: '0x0000000000000000000000000000000000000000'
    },
    fromBlock: 0
}).then((events) => {
    for (let event of events) {
        console.log(event.returnValues._tokenId);
    }
});

There's no easy way to do it with an Ethereum node in a contract-agnostic way...the ERC-712 does not specify any interface methods that allow querying for all token ID, so unless the contract you're looking at uses sequential token ids, there's no way to guess all token ids from a simple node query.没有简单的方法可以以与合约无关的方式使用以太坊节点来做到这一点……ERC-712 没有指定任何允许查询所有代币 ID 的接口方法,因此除非您正在查看的合约使用顺序代币ids,没有办法从一个简单的节点查询中猜测所有的 token id。

Unless you want to iterate over the whole transaction history of the contract to get the ids of every minted NFT (you'd need an archive node for that, as a full node would not have the full transaction history) you should use an API from services that index all NFT activity.除非您想遍历合约的整个交易历史以获取每个铸造的 NFT 的 ID(您需要一个存档节点,因为完整节点没有完整的交易历史),您应该使用来自的 API索引所有 NFT 活动的服务。

You could use this API from CovalentHQ: https://www.covalenthq.com/docs/api/#/0/Class-A/Get-NFT-Token-IDs-for-contract/lng=en你可以使用 CovalentHQ 的这个 API: https : //www.covalenthq.com/docs/api/#/0/Class-A/Get-NFT-Token-IDs-for-contract/lng=en

Or this one from Morallis: https://docs.moralis.io/moralis-server/web3-sdk/token#getalltokenids或者来自 Morallis 的这个: https ://docs.moralis.io/moralis-server/web3-sdk/token#getalltokenids

I needed the same with Ethers instead of Web3, here i the code snippet for ethers.js:我需要 Ethers 而不是 Web3,这里是 ethers.js 的代码片段:

const getTransferEvents = async () => { 
    const provider = new ethers.providers.Web3Provider(window.ethereum)
    const contract = new ethers.Contract("address", "abi", provider);

    const events = await contract.queryFilter('Transfer', 0);

    console.log(events);
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM