Based on OpenZeppilin ERC20Mintable contract, I add a function to limit maximum minterable token amount. I add a static variable uint256 MaxSupply
to record and want to make MaxSupply
accessiable to contract owner only.
But when I use onlyMinter
modifier, It retruns a long strange number,like below:
Token Balance : 1000000000000000000000
Token current supply : 1000000000000000000000
Token MaxSupply: 3963877391197344453575983046348115674221700746820753546331534351508065746944
However, when remove onlyMinter
modifier, the result is correct as expectation:
Token Balance : 1000000000000000000000
Token current supply : 1000000000000000000000
Token MaxSupply: 1100000000000000000000
Environment
The OpenZeppelin version should be latest. I got it by run:
git clone https://github.com/OpenZeppelin/openzeppelin-contracts.git
and copy all the used .sol file to a folder.
the solidity version is solc@0.5.9
, higer than declared version in solidity source code.
Details
I call the contract using the following code:
var tokenContractAddress = "0x*****************";
var myAddress = "0x******************";
var async = require('asyncawait/async');
var await = require('asyncawait/await');
const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');
const main = async () => {
var abi = JSON.parse(fs.readFileSync('/root/Mintable/abi.json', 'utf-8'));
const contract = new web3.eth.Contract(abi, tokenContractAddress);
await web3.eth.personal.unlockAccount(myAddress, "******", 600);
var balance_from = await contract.methods.balanceOf(myAddress).call();
var totalsupply = await contract.methods.totalSupply().call();
var MaxSupply = await contract.methods.ViewMaxSupply().call();
console.log(`Token Balance : ${balance_from}`);
console.log(`Token current supply : ${totalsupply}`);
console.log(`Token MaxSupply: ${MaxSupply}`);
};
main();
Code to reproduce
pragma solidity ^0.5.0;
import "./ERC20.sol";
import "./MinterRole.sol";
contract ERC20MintableMaxSupply is ERC20, MinterRole {
string public name;
string public symbol;
uint8 public decimals;
uint256 MaxMintAmount;//max minerable amount after first issue
uint256 MaxSupply;//initial issue amount plus MaxMintAmount
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 _initSupply,
uint256 _MaxMintAmount
) public {
name = _name;
symbol = _symbol;
decimals = _decimals;
MaxMintAmount = _MaxMintAmount;
MaxSupply = _initSupply + _MaxMintAmount;
_mint(msg.sender, _initSupply * (10 ** uint256(decimals)));
}
function mint(address account,uint256 MintAmount) public onlyMinter returns (bool) {
uint256 curSupply;
curSupply = totalSupply() + MintAmount * (10 ** uint256(decimals));
require( curSupply <= ( MaxSupply * (10 ** uint256(decimals)) ), "error: not permitted to exceed maximun supply.");
_mint(account, MintAmount * (10 ** uint256(decimals)));
return true;
}
function ViewMaxSupply() public view returns (uint256) {
return ( MaxSupply * (10 ** uint256(decimals)) );
}
}
Please help. Thanks.