0

I'm using the fa2_nft_minimal.py smart contract that has the following entry point for minting:

    @sp.entry_point
    def mint_many(self, to_, metadata):
        token_id = sp.compute(self.data.next_token_id)
        self.data.token_metadata[token_id] = sp.record(
            token_id=token_id, token_info=sp.map({"": metadata},{"": metadata}, {"": metadata})
        )
        self.data.ledger[token_id] = to_
        self.data.next_token_id += 1
    enter preformatted text here

How do I update the contract so it can mint several NFTs in only one operation?

1 Answer 1

1

Use the fa2 library instead of fa2_nft_minimal.py which is more an example than production ready.

Option A: each minted NFT has different metadata

Solution: use the MintNft mixin that accepts batchs. The example is here: https://smartpy.io/docs/guides/FA/FA2/examples/#with-admin-and-mint

import smartpy as sp
FA2 = sp.io.import_script_from_url("https://smartpy.io/templates/fa2_lib.py")

class NftWithAdmin(FA2.Admin, FA2.MintNft, FA2.Fa2Nft):
    def __init__(self, admin, **kwargs):
        FA2.Fa2Nft.__init__(self, **kwargs)
        FA2.Admin.__init__(self, admin)

@sp.add_test(name="NFT with admin and mint")
def test():
    sc = sp.test_scenario()
    c1 = NftWithAdmin(
        admin = sp.test_account("admin").address,
        metadata = sp.utils.metadata_of_url("https://example.com"),
    )
    sc += c1

Option B: All tokens minted in batch share the same metadata

Solution 1: Use fungible tokens. In this case, same answer as Option A except Nft is replaced by Fungible.

Solution 2: create your own mint entrypoint:

    @sp.entry_point
    def mint(self, batch):
        """Admin can mint new or existing tokens."""
        sp.set_type(
            batch,
            sp.TList(
                sp.TRecord(
                    to_=sp.TList(sp.TAddress),
                    metadata=sp.TMap(sp.TString, sp.TBytes),
                ).layout(("to_", "metadata"))
            ),
        )
        sp.verify(self.is_administrator(sp.sender), "FA2_NOT_ADMIN")
        with sp.for_("action", batch) as action:
            with sp.for_("to_", action.to_) as to_:
                token_id = sp.compute(self.data.last_token_id)
                metadata = sp.record(token_id=token_id, token_info=action.metadata)
                self.data.token_metadata[token_id] = metadata
                self.data.ledger[token_id] = to_
                self.data.last_token_id += 1
2
  • This batch mint it multiple tokens, how would you mint a single token with multiple references? Commented Oct 27, 2022 at 16:46
  • I updated my answer.
    – Jordan_tez
    Commented Oct 29, 2022 at 12:24

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.