I am trying to implement and test an ERC20 token by inheriting from ERC20.sol from OpenZeppelin.
I want most of the functions to be by default from OpenZeppelin but I also want to overrride some of them.
For example, I have overriden increaseAllowance
with the below code with the goal not to call approve in increaseAllowance to avoid front running.
function increaseAllowance(address spender, uint256 addedValue) public override returns (bool) {
require(spender != address(0), "KToken: transfer to the zero address");
_allowances[msg.sender][spender] += addedValue;
emit Approval(msg.sender, spender, _allowances[msg.sender][spender]);
return true;
}
When I test this function the _allowances
state variable is not updated as expected. It keeps the value it had before and it is not increased by addedValue
.
This is the test that I am running.
it("account1 should be able to increase allowance to account2", async () => {
const kateToken = await deploy(accounts);
// transfer 1000 tokens to account1 FROM owner
await kateToken.transfer(accounts[1], toWei('1000'), {from: accounts[0]});
// approve 50 tokens FROM account1 TO account2
await kateToken.approve(accounts[2], toWei('50'), {from: accounts[1]});
let allowanceToAccountBEFORE = fromWei(await kateToken.allowance(accounts[1], accounts[2]));
// attempt to increase allowance to account2 by 150 tokens
let increaseAllowanceProccess = await kateToken.increaseAllowance(accounts[2], toWei('150'), {from: accounts[1]});
// get allowance of account2 converted from string to number so as to make calcs in asserts
let allowanceToAccountAFTER = parseInt(fromWei(await kateToken.allowance(accounts[1], accounts[2])));
assert.equal(increaseAllowanceProccess.receipt.status, true, `proccess to increase allowance did not succeed`);
assert.equal(allowanceToAccountBEFORE, allowanceToAccountAFTER-150, `allowance of account2 is not increased by 150 tokens`);
});
If I comment my code and increaseAllowance
is called by OpenZeppelin's the test runs with success.
If not, allowance is not updated by 150 tokens, it remains 50 which was set by approve. But the strange thing is that I get the Approval Event (emited with only the addedValue), I can get the tx receipt with status true.
Do I have any logical or other issue that I can not get?
Is _allowances
state variable from my contract different than _allowances
state variable from OpenZeppelin's ERC20 Contract which I am inheriting from?
By the way, I have also implemented the same code by inheriting from IERC20 with no issues and succesful testing, and I can not understand why this does not work with ERC20.
Thank you in advance!
_allowances
but shouldn't be possible in recent version because it is declared as private._allowances
from the publicallowance
function, if that helps._allowances
declared in your contract that will be different from the variable declared in OZ's contracts. To access the variable you have to useallowance
to read it and_approve
to modify ti.