简体   繁体   English

恢复执行:ERC721PresetMinterPauserAutoId:必须具有铸造者角色才能铸造

[英]execution reverted: ERC721PresetMinterPauserAutoId: must have minter role to mint

I'm trying to create an end point to mint a NFT using Nestjs , Binance Tes.net , Metamask , truffle hdwallet-provider and openzeppelin as below.我正在尝试使用NestjsBinance Tes.netMetamasktruffle hdwallet-provideropenzeppelin创建一个端点来铸造 NFT,如下所示。 Smart contract creation process is working without any issue, but when invoke the mint function getting below error:智能合约创建过程没有任何问题,但是当调用mint function 时出现以下错误:

execution reverted: ERC721PresetMinterPauserAutoId: must have minter role to mint

sample code:示例代码:

smart-contract.service.ts智能合约.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智能合约.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?有谁知道这里的问题是什么?

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.如果你想从其他地址铸造 nft,你必须向该地址授予铸造角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM