0

I am trying to get all the token id for a given erc721 contract address. What would be the best way of doing this? I have tried using the web3 library with the following code but get response size limit exceeded.

const Web3Eth = require("web3-eth");
const URL =
  "wss://eth-mainnet.alchemyapi.io/v2/XXXXXXXX";
const web3Eth = new Web3Eth(Web3Eth.givenProvider || URL);
const smartContractAddress = "0x06012c8cf97bead5deae237070f9587f8e7a266d";
const contract = new web3Eth.Contract(abi_, smartContractAddress);

async function fetchTokens() {
  return contract.getPastEvents(
    "Transfer",
    {
      fromBlock: 0,
      toBlock: "latest",
    },
    function (error, events) {
      if (!error) {
        for (var i = 0; i < events.length; i++) {
          console.log(events[i].returnValues.tokenId);
        }
      }
    }
  );
}
fetchTokens();

Error:

(node:13891) UnhandledPromiseRejectionWarning: Error: Returned error: Log response size exceeded. You can make eth_getLogs requests with up to a 2K block range and no limit on the response size, or you can request any block range with a cap of 10K logs in the response.

1 Answer 1

1

If the contract is minting tokens in random orders, it must have some mechanic to keep track of the token. Otherwise they are minted in order.

If they have been minted in orders; 1,2,3,4,...

Then you could simply call for the total supply and you would know how many token there is from 1 to total supply.

If they are randomly created. 2,45,12,91

Then there must be some logic in the contract that knows how many can be minted and why they have this ID. It should contain a record of that somewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.