Reputation: 148
I am trying to find an NFT collection holder count (ERC721 token). For example, OpenSea and EtherScan have this data but I couldn't find a native ERC721 method to achieve this.
What is the best way of doing this?
Thanks
Upvotes: 1
Views: 2616
Reputation: 49361
you do not need any third-party contract or service. You can set 3 mappings:
// each operation on mapping takes O(1) time
//keep track of how many tokens each address have
mapping(address => uint256) private ownedTokens
// from tokenId=>ownerAddress
// keep track of owner of each tokenId
mapping(uint256 => address) private tokenOwner
// keep track of how many tokens does an address own
mapping(address => uint256) private ownedTokensCount
mappings are very useful in solidity to optimize your code. When you write the mint function, you have to update those mappings accordingly.
function mint(address to, uint256 tokenId) external {
// ADD YOUR REQUIRE LOGIC
ownedTokensCount[to] += 1;
tokenOwner[tokenId] = to;
ownedTokens[to] = tokenId;
}
Upvotes: 0
Reputation: 148
Well, after a few hours more research, I found some solutions and wanted to share those.
There are some ways to do this:
ERC721Enumerable
, your case study is a little bit easier. Because you can find the token count using totalSupply()
so you can skip the first part of the code below.Moralis
. It returns the owner count with a list of addresses and extra data. See the already exist answerasync testGetOwnerCount({ }, payload) {
// Connect the contract
let nftContract = new ethers.Contract(nftContractAddress, ERC721.abi, signer);
// First find the mint count which is equal to the token count
const transferFilter = nftContract.filters.Transfer("0x0000000000000000000000000000000000000000", null, null)
const tokens = await nftContract.queryFilter(transferFilter)
const tokenCount = tokens.length;
// Iterate over tokens and store owner addresses in an array
let owners = []
for (let i = 0; i < tokenCount; i++) {
// First, find the all transfers of the token
// from null` to `null` so we get all the transfers of `tokenId`
const transferFilter = nftContract.filters.Transfer(null, null, parseInt(tokens[i].args.tokenId))
const tokenTransfers = await nftContract.queryFilter(transferFilter)
// `args.to` of the last element gives the current owner of issued token
let lastTransfer = tokenTransfers[tokenTransfers.length - 1]
let currentOwner = lastTransfer.args.to
// If the address has already found before, don't add it...
if (!owners.includes(currentOwner)) {
owners.push(currentOwner)
}
}
}
There might be better ways to achieve this but this was the best I could find since I want to handle all the contracts not only those are ERC721Enumerable
and don't want to use another tool.
Upvotes: 2
Reputation: 1326
The best way would be to use the Transfer event since that's what for. You can query all transfer events, and filter by ID.
Yet you can too create your custom function(id, address)
which will be called on each nft transfer, there you can push to an array in a mapping id -> address[]
where the array represents the addresses of the peeps who owned that NFT.
And then create another function(id)
to return that array depending on the id sent to the function.
Upvotes: 1