I am trying to create a mint function in my ERC20 smart contract, and then calling that function from my frontend app using web3.js but I always get the error: RPC Error: execution reverted: only admin
I am using "web3": "^1.5.2","react": "^17.0.2","solidity": "^0.8.0",
Here is my smart contract code:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract EpicCoin is ERC20 {
address public admin;
constructor() ERC20("EpicCoin", "EC") {
_mint(msg.sender, 10000 * 10 ** 18);
admin = msg.sender;
}
function mint(address _to, uint256 _value) public {
_mint(_to, _value);
}
}
Here is my mint code in React:
let minABI2 = [
// mint
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "mint",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"type": "function"
}
];
function mintHandle(e){
const mintValue = document.getElementById('inputField2').value;
function getAccounts(callback) {
web3.eth.getAccounts((error,result) => {
if (error) {
console.log(error);
} else {
callback(result);
}
});
}
getAccounts(function(result) {
const mintAddress = result[0];
let tokenAddress = "0x98d2f8442311fa3c06a2f3c3260f875e02469e38";
let decimals = web3.utils.toBN(18);
let amount = web3.utils.toBN(`${mintValue}`);
let value = amount.mul(web3.utils.toBN(10).pow(decimals));
let toAddress = `${mintAddress}`
let contract = new web3.eth.Contract(minABI2, tokenAddress);
contract.methods.mint(toAddress, value).call();
})
}