[英]I want to mint and transfer 1 erc20(custom) to the minter itself, just to track erc20 transaction
[英]execution reverted: ERC721PresetMinterPauserAutoId: must have minter role to mint
我正在嘗試使用Nestjs 、 Binance Tes.net 、 Metamask 、 truffle hdwallet-provider和openzeppelin創建一個端點來鑄造 NFT,如下所示。 智能合約創建過程沒有任何問題,但是當調用mint
function 時出現以下錯誤:
execution reverted: ERC721PresetMinterPauserAutoId: must have minter role to mint
示例代碼:
智能合約.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;
}
}
智能合約.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)
{}
}
有誰知道這里的問題是什么?
當您部署合約時,鑄造者角色設置為部署者地址。 可能是您正在從其他沒有鑄造者角色的地址調用請求。
如果你想從其他地址鑄造 nft,你必須向該地址授予鑄造角色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.