Skip to content

Commit

Permalink
add allowance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
3esmit committed Sep 24, 2023
1 parent e4c0650 commit 8e2de05
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .gas-report
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
| Deployment Cost | Deployment Size | | | | |
| 1788057 | 9919 | | | | |
| Function Name | min | avg | median | max | # calls |
| allowance | 0 | 269 | 0 | 808 | 3 |
| approve | 0 | 20829 | 30781 | 31708 | 3 |
| allowance | 0 | 80 | 0 | 808 | 10 |
| approve | 0 | 15995 | 23208 | 31708 | 9 |
| balanceOf | 0 | 355 | 0 | 2753 | 41 |
| balanceOfAt | 0 | 90 | 0 | 2363 | 26 |
| changeController | 0 | 1014 | 758 | 3558 | 5 |
Expand All @@ -13,16 +13,16 @@
| createCloneToken | 0 | 916398 | 916398 | 1832796 | 2 |
| decimals | 0 | 0 | 0 | 0 | 7 |
| destroyTokens | 8956 | 8956 | 8956 | 8956 | 1 |
| enableTransfers | 0 | 0 | 0 | 0 | 2 |
| generateTokens | 0 | 10636 | 0 | 95751 | 27 |
| enableTransfers | 0 | 0 | 0 | 0 | 3 |
| generateTokens | 0 | 9264 | 0 | 95751 | 31 |
| name | 0 | 0 | 0 | 0 | 7 |
| parentSnapShotBlock | 0 | 0 | 0 | 0 | 8 |
| parentToken | 0 | 0 | 0 | 0 | 8 |
| symbol | 0 | 0 | 0 | 0 | 7 |
| totalSupply | 0 | 273 | 0 | 1911 | 7 |
| totalSupplyAt | 0 | 285 | 0 | 1995 | 7 |
| transfer | 526 | 36631 | 40486 | 75187 | 16 |
| transferFrom | 0 | 17717 | 2139 | 66590 | 4 |
| transferFrom | 0 | 15187 | 3495 | 66590 | 5 |


| contracts/MiniMeTokenFactory.sol:MiniMeTokenFactory contract | | | | | |
Expand Down
8 changes: 6 additions & 2 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
AllowanceTest:testAllowance() (gas: 42674)
AllowanceTest:testDeployment() (gas: 26595)
AllowanceTest:testAllowance() (gas: 42696)
AllowanceTest:testAllowanceAlreadySet() (gas: 36779)
AllowanceTest:testAllowanceReset() (gas: 45888)
AllowanceTest:testApproveTransferDisabled() (gas: 7975)
AllowanceTest:testDeployment() (gas: 26617)
AllowanceTest:testNoAllowance() (gas: 9469)
ClaimTokensTest:testClaimERC20() (gas: 63734)
ClaimTokensTest:testClaimETH() (gas: 13637)
ClaimTokensTest:testClaimSelf() (gas: 61216)
Expand Down
71 changes: 70 additions & 1 deletion test/MiniMeToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { Deploy } from "../script/Deploy.s.sol";
import { DeploymentConfig } from "../script/DeploymentConfig.s.sol";

import { NotAuthorized } from "../contracts/Controlled.sol";
import { MiniMeToken, TransfersDisabled, InvalidDestination, NotEnoughBalance } from "../contracts/MiniMeToken.sol";
import {
MiniMeToken,
TransfersDisabled,
InvalidDestination,
NotEnoughBalance,
NotEnoughAllowance,
AllowanceAlreadySet
} from "../contracts/MiniMeToken.sol";
import { MiniMeTokenFactory } from "../contracts/MiniMeTokenFactory.sol";

contract MiniMeTokenTest is Test {
Expand Down Expand Up @@ -306,6 +313,68 @@ contract AllowanceTest is MiniMeTokenTest {
assertEq(minimeToken.balanceOfAt(accounts[2], nextBlock), 1, "balance at next block should be correct");
vm.resumeGasMetering();
}

function testNoAllowance() public {
vm.pauseGasMetering();
_generateTokens(accounts[0], 10);
vm.prank(accounts[1]);
uint256 allowed = minimeToken.allowance(accounts[0], accounts[1]);
assertEq(allowed, 0, "allowance should be correct");
vm.expectRevert(NotEnoughAllowance.selector);
vm.resumeGasMetering();
minimeToken.transferFrom(accounts[0], accounts[1], 1);
}

function testApproveTransferDisabled() public {
vm.pauseGasMetering();
_generateTokens(accounts[0], 10);
vm.prank(deployer);
minimeToken.enableTransfers(false);
vm.prank(accounts[0]);
vm.expectRevert(TransfersDisabled.selector);
vm.resumeGasMetering();
minimeToken.approve(accounts[1], 2);
vm.pauseGasMetering();
assertEq(minimeToken.allowance(accounts[0], accounts[1]), 0, "allowance should be 0");
vm.resumeGasMetering();
}

function testAllowanceAlreadySet() public {
vm.pauseGasMetering();
_generateTokens(accounts[0], 10);
vm.startPrank(accounts[0]);
vm.resumeGasMetering();
minimeToken.approve(accounts[1], 2);
vm.pauseGasMetering();
assertEq(minimeToken.allowance(accounts[0], accounts[1]), 2, "allowance should be 2");
vm.expectRevert(AllowanceAlreadySet.selector);
vm.resumeGasMetering();
minimeToken.approve(accounts[1], 3);
vm.pauseGasMetering();
vm.stopPrank();
assertEq(minimeToken.allowance(accounts[0], accounts[1]), 2, "allowance should stay 2");
vm.resumeGasMetering();
}

function testAllowanceReset() public {
vm.pauseGasMetering();
_generateTokens(accounts[0], 10);
vm.startPrank(accounts[0]);
vm.resumeGasMetering();
minimeToken.approve(accounts[1], 2);
vm.pauseGasMetering();
assertEq(minimeToken.allowance(accounts[0], accounts[1]), 2, "allowance should be 2");
vm.resumeGasMetering();
minimeToken.approve(accounts[1], 0);
vm.pauseGasMetering();
assertEq(minimeToken.allowance(accounts[0], accounts[1]), 0, "allowance should be 0");
vm.resumeGasMetering();
minimeToken.approve(accounts[1], 3);
vm.pauseGasMetering();
vm.stopPrank();
assertEq(minimeToken.allowance(accounts[0], accounts[1]), 3, "allowance should be 3");
vm.resumeGasMetering();
}
}

contract DestroyTokensTest is MiniMeTokenTest {
Expand Down

0 comments on commit 8e2de05

Please sign in to comment.