获取 ERC1155 令牌列表
So the question was an underlying question while trying to understand how all these platforms like OpenSeas are identifying all of the minted tokens for standard ERC721 and ERC1155 contracts. It was perplexing because there’s no “getAllMintedTokens()” method in the standard smart contracts.
So without further adieu, the answer is by searching for all past events of type “TransferSingle”. They then parse through the results and fetch the metadata using the metadata methods that are standard in ERC721/ERC1155.
For those curious about the technical how-to. Keep reading.
let transferEvents = await mega.getPastEvents('TransferSingle', { fromBlock: 0, toBlock: 'latest' });
The result of this is an array of events. There’s a property called returnValues that contains an id and a value which correspond to the tokenId and the numberOfTokensToMint. And then a to property which corresponds to the recipient address.
The full data for a single element in the array looks like the JSON below.
{ address: '0xAa45AcDa319377397805437340fdf70F730090f2', blockHash: '0x89b17a2efb883c731679681d06e5f694c345059277ac2a634fada532e3c35171', blockNumber: 8247859, logIndex: 1, removed: false, transactionHash: '0x65bd36cc73797bc09c86f9c7775f81fde5b641c94d08fdb4dde561ae9396c9e2', transactionIndex: 1, id: 'log_6c71ba76', returnValues: Result { '0': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', '1': '0x0000000000000000000000000000000000000000', '2': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', '3': '1', '4': '1', operator: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', from: '0x0000000000000000000000000000000000000000', to: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', id: '1', value: '1' }, event: 'TransferSingle', signature: '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62', raw: { data: '0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62', '0x000000000000000000000000d954f4513bde1e00f3986630a7e73c4f9aa564fe', '0x0000000000000000000000000000000000000000000000000000000000000000', '0x000000000000000000000000d954f4513bde1e00f3986630a7e73c4f9aa564fe' ] }, args: Result { '0': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', '1': '0x0000000000000000000000000000000000000000', '2': '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', '3': BN { negative: 0, words: [Array], length: 1, red: null }, '4': BN { negative: 0, words: [Array], length: 1, red: null }, __length__: 5, operator: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', from: '0x0000000000000000000000000000000000000000', to: '0xd954F4513BdE1E00F3986630A7e73c4f9aA564fE', id: BN { negative: 0, words: [Array], length: 1, red: null }, value: BN { negative: 0, words: [Array], length: 1, red: null } } }
https://ethereum.stackexchange.com/questions/94716/get-a-list-of-all-token-types-for-erc1155
如果你想要 ERC1155 中的 ownerOf,你必须自己实现它。
rarible 和 opensea 使用像 thegraph.com 这样的索引服务来跟踪转移和谁目前拥有令牌。
https://ethereum.stackexchange.com/questions/92275/how-can-i-get-a-list-of-all-owners-of-an-erc1155-nft-by-using-a-web3-call
要在Ethereum或 Polygon 等区块链上获取用户拥有的所有 NFT ,您必须从创世以来解析整个区块链,跟踪所有和合同,并跟踪每个 NFT 的最新所有权状态。ERC-721ERC-1155
这需要大量的工程资源和时间。
但是,可以通过使用绕过这种努力Alchemy 的 NFT API.
https://docs.alchemy.com/docs/how-to-get-all-nfts-owned-by-an-address