[英]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.