Suisse
Suisse

Reputation: 3613

Iterate over an array of objects and mint an NFT on NEAR protocol

I have this method:

#[payable]
pub fn nft_mint(&mut self, nft_list: Vec<Nft>) -> Vec<Token> {
    let mut t_result: Vec<Token> = Vec::new();
    for nft in nft_list {
        let s = self.tokens.mint(nft.token_id, nft.receiver_id, Some(nft.token_metadata));
        t_result.push(s);
    }
    return t_result;
}

and from frontend:

let token_list = [{
    token_id: nanoid(),
    receiver_id: window.accountId,
    token_metadata: { id: i, title: "test", myNumber: 123, price: 10, url: "http://..."}
  }];

    window.contract
      .nft_mint({ nft_list: token_list })
      .then((res) => {
        console.log("Success", res);
      }).catch(err => {
        console.log("Error", err);
      });

after deploying and initializing the contract, when I try to call the method, even that I have enough NEAR (on testnet 190NEAR) I get this error:

{
  "type": "action",
  "error": {
    "type": "functionCallError",
    "error": {
      "type": "executionError",
      "error": "Smart contract panicked: panicked at 'Must attach 6290000000000000000000 

yoctoNEAR to cover storage',
 /home/HideThePainHarold/.cargo/registry/src/github.com-1ecass99db9ec823/near-contract-standards-3.2.0/src/non_fungible_token/utils.rs:33:5"

    }
  }
}

and how would I make the sender pay for the actual minting (not just gas fees)?

Upvotes: 1

Views: 89

Answers (1)

Benjamin Kurrek
Benjamin Kurrek

Reputation: 1094

Storing information on-chain costs $NEAR. The smart contract chargers users who mint NFTs for the $NEAR that it costs. At the time of writing this, it costs 1 $NEAR for every 100kb of data stored.

If the smart contract didn't charge the user and minting was open to the public (not allowlisted), the contract could be subject to a storage attack.

All you need to do to fix your problem is attach $NEAR to the call.

To gain a better understanding as to what's happening behind the scenes, there's a great section of the NFT zero to hero tutorial that talks about minting NFTs.

Upvotes: 2

Related Questions