我正在尝试创建一个终结点来创建一个NFT,它使用hdwallet-provider Nestjs、Binance Testnet、Metamask、松露和openzeppelin,如下所示。智能契约创建过程工作正常,但当调用mint
函数时出现以下错误:
execution reverted: ERC721PresetMinterPauserAutoId: must have minter role to mint
样本代码:
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;
}
}
合同档案:
// 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)
{}
}
有人知道这里有什么问题吗?
发布于 2022-02-22 07:23:26
当您部署契约minter角色时,角色设置为部署者地址。可能是您从没有minter角色的其他地址拨打请求。
如果您想要从其他地址创建nft,则必须将mint角色授予此地址。
https://stackoverflow.com/questions/71211447
复制相似问题