1

I'm trying to create an end point to mint a NFT using Nestjs, Binance Testnet, Metamask, truffle hdwallet-provider and openzeppelin as below. Smart contract creation process is working without any issue, but when invoke the mint function getting below error:

execution reverted: ERC721PresetMinterPauserAutoId: must have minter role to mint

sample code:

smart-contract.service.ts

import { Injectable, Logger } from '@nestjs/common';
import Web3 from 'web3';
import HDWalletProvider from '@truffle/hdwallet-provider';
import * as fs from 'fs';
import * as path from 'path';

@Injectable()
export class SmartContractService {
  mnemonic = 'mnemonic goes here'
  client: any;
  nftAbi;
  nftBytecode;

  logger = new Logger('SmartContractService');

  constructor() {
    const provider = new HDWalletProvider(
      this.mnemonic,
      'https://data-seed-prebsc-1-s1.binance.org:8545',
    );
    this.client = new (Web3 as any)(
      provider,
    );
    this.initNFTConfigs();
  }

  getClient() {
    return this.client;
  }

  async createContract() {
    const accounts = await this.client.eth.getAccounts();
    const { _address } = await new this.client.eth.Contract(this.nftAbi)
      .deploy({
        data: this.nftBytecode,
        arguments: [
          'Sehas Nft',
          'nft',
          'https://my-json-server.typicode.com/AjanthaB/json-data/nft-images/',
        ],
      })
      .send({ from: accounts[0] });

    return _address;
  }

  async mintNFT(address: string) {
    const accounts = await this.client.eth.getAccounts();
    const nft = await new this.client.eth.Contract(this.nftAbi, address);
    await nft.methods.mint(accounts[0]).call();
  }

  private initNFTConfigs() {
    const filePath = path.resolve(__dirname, '../smart-contracts/HmtNFT.json');
    this.logger.log(filePath);
    const source = fs.readFileSync(filePath, 'utf-8');
    const { abi, bytecode } = JSON.parse(source);
    this.nftAbi = abi;
    this.nftBytecode = bytecode;
  }
}

smart-contract.controller.ts

import { Controller, Get } from '@nestjs/common';
import { SmartContractService } from './smart-contract.service';

@Controller('api/v1')
export class SmartContractController {

  constructor(private smartContractService: SmartContractService) {}

  @Get('/nft-contracts')
  async createSmartContract() {
    const address = await this.smartContractService.createContract();
    console.log('address: ' + address);
    if (address) {
      return await this.smartContractService.mintNFT(address);
    }
    return null;
  }
}

contract file:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol";

contract HmtNFT is ERC721PresetMinterPauserAutoId {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor(string memory name, string memory symbol, string memory baseTokenURI)
        ERC721PresetMinterPauserAutoId(name, symbol, baseTokenURI)
    {}
}

does anyone knows what is the issue here?

BZKN
  • 1,499
  • 2
  • 10
  • 25
Ajantha Bandara
  • 1,473
  • 15
  • 36

1 Answers1

0

When you deploy contract minter role is set to deployer address. May be you are calling request from other address which does not have minter role.

And if you want to mint nft from other addresses you have to Grant mint role to this address.

Abdul Maajid
  • 101
  • 7