0

I am making an ERC 1155 contract and it is a mixture of NFTs and FTs, my intention is that from a token id forward, the user only be able to own one of the token, something like the minting below:

pragma solidity ^0.8.2;

import "@openzeppelin/contracts@4.3.2/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts@4.3.2/access/Ownable.sol";
import "@openzeppelin/contracts@4.3.2/utils/Strings.sol";

contract MyToken is ERC1155, Ownable {
    constructor() ERC1155("") {}
    string private _uri;
    uint256 private _indexID = 1000000000;
    
    function mint()
        public
        payable
    {
        require(msg.value > 0.001 ether);
        _mint(msg.sender, _indexID, 1, "");
        _indexID++;
    }
    
    ...
}

Now I am trying to add a function which can be used to return all the token ids owned by an address, in case that someone already has experience with such senario or knows a work around, it will really great if you can please share it here

thank you for your attention

2
  • You could create a mapping on the mint function that for an owner returns an array of tokenIds
    – Julissa DC
    Nov 12 at 0:03
  • thank you for your suggestion true, I have done that, however then you need to update the mapping every time a trade happens which means more overriden functions and room to make mistakes. Also in general it will be a big map which I am trying to avoid Nov 12 at 12:37
1

You should do that off-chain if you can. For an off-chain solution see Get a list of all token types for ERC1155

To get all tokens of an account within a smart contract you'd have to add enumeration. Openzeppelin doesn't have it to reduce gas costs.

I use solidstate, it has an enumerable extension (it's designed for eip-2535, but you can still use it)

https://github.com/solidstate-network/solidstate-solidity/blob/5a8c6745d85b3f39f8f05bcbc5b5a78e7189b216/contracts/token/ERC1155/enumerable/ERC1155Enumerable.sol

2
  • Intresting question and answer there, so what companies like opensea are doing is in fact retrieving the tokens based on our previous transactions? Is there any source code on this on Github or anywhere else that is using web3js? cause let transferEvents = await mega.getPastEvents('TransferSingle', { fromBlock: 0, toBlock: 'latest' }); does not seem to be Web3Js or I am mistaking something Nov 12 at 12:58
  • 1
    web3js does have getPastEvents function web3js.readthedocs.io/en/v1.3.4/…
    – dk1a
    Nov 12 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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