3

I am working on creating a contract that implements ERC1155 tokens. I want to be able to init a collection with different tiers. So let's say 3 tiers, tier1 will have 15 supply, tier2 50 supply and tier3 100 supply. What would be the best way to implement this between the following options:

1-Let users mint any nft they want (from any tier) with quantity they want until tiers reach their maxsupply. See code below

function mint(uint256[] memory _id, uint256[] memory _quantity) public payable {
        require(saleStarted, "Owner need to confirm prices and start the sale");
        require(alreadyMinted[msg.sender]==false, "You have already bought a ticket preivously");
        require(_id.length == _quantity.length, "more items or quantity specified than the other");
        uint256 sum = 0;
        for(uint256 i = 0; i < _id.length; i++) {
            require(mintedByTier[i] + _quantity[i] <= tierQuantity[i], "max supply excedeed");
            sum += _id[i] * _quantity[i];
            mintedByTier[i] += _quantity[i];
        }
        require(msg.value >= sum, "Not enough funds");
        _mintBatch(msg.sender, _id, _quantity, " ");
    }

2-Mint all tokens with the constructor when the smart contract is created. So the holder of all the ERC1155 tokens (at the start) will be the smart contract and then enable users to buy them from the smart contract and get them in their wallet. However I am not sure how to make the transfer between the smart contract and the buyer wallet address.

function purchase(uint256[] memory _id, uint256[] memory _quantity) public payable{
        require(saleStarted, "Owner need to confirm prices and start the sale");
        require(alreadyMinted[msg.sender]==false, "You have already bought a ticket preivously");
        require(_id.length == _quantity.length, "more items or quantity specified than the other");
        uint256 sum = 0;
        for(uint256 i = 0; i < _id.length; i++) {
            sum += _id[i] * _quantity[i];
        }
        safeBatchTransferFrom(address(this), msg.sender, _id, _quantity, " ");
    } 

My question is: what would be the best way to do it, like more secure and reliable.

Best regards

EDIT: I added code for the 2 different ways.

2
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Mar 22, 2022 at 18:31
  • It's done see update Mar 23, 2022 at 7:38

1 Answer 1

0

Option 1 (I prefer this): Most people are used to the mint function being used to purchase an nft. You have add extra logic to handle pricing for different tiers and checking the supply.

Option 2: If the contract owns the nft you won't need to approve any transfers. It should be as simple as charging them for the purchase and transfering the correct token id and amount. This option is also more expensive compared to option 1 since your constructor will have to call the mint function.

1
  • Hi, thank you for your reply. I try to make the transfer but it always says: error address require approval that's why I wanted to go with option 1. am I using the wrong transfer function from the ERC1155 doc? (safeBatchTransfer) Mar 23, 2022 at 7:39

Your Answer

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

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