Skip to content

Commit

Permalink
Merge pull request #154 from corpus-io/feature/reduceGasForTransfer
Browse files Browse the repository at this point in the history
check for requirements == 0
  • Loading branch information
malteish authored Jun 16, 2023
2 parents ce6a931 + c55f6f4 commit cf4267d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
4 changes: 3 additions & 1 deletion contracts/Token.sol
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ contract Token is ERC2771Context, ERC20Permit, Pausable, AccessControl {
*/
function _checkIfAllowedToTransact(address _address) internal view {
require(
hasRole(TRANSFERER_ROLE, _address) || allowList.map(_address) & requirements == requirements,
requirements == 0 ||
hasRole(TRANSFERER_ROLE, _address) ||
allowList.map(_address) & requirements == requirements,
"Sender or Receiver is not allowed to transact. Either locally issue the role as a TRANSFERER or they must meet requirements as defined in the allowList"
);
}
Expand Down
52 changes: 45 additions & 7 deletions test/Token.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ contract tokenTest is Test {
Fees memory fees = Fees(100, 100, 100, 0);
feeSettings = new FeeSettings(fees, admin);
token = new Token(trustedForwarder, feeSettings, admin, allowList, 0x0, "testToken", "TEST");
console.log(msg.sender);

// set up roles
vm.startPrank(admin);
Expand Down Expand Up @@ -175,12 +174,6 @@ contract tokenTest is Test {
assertTrue(token.hasRole(role, pauser));
}

// function testFailRemoveLastDefaultAdmin() public {
// bytes32 role = token.DEFAULT_ADMIN_ROLE();
// vm.prank(admin);
// token.revokeRole(role, admin);
// }

function testSetRequirements(uint256 newRequirements) public {
bytes32 role = token.REQUIREMENT_ROLE();
vm.prank(admin);
Expand Down Expand Up @@ -825,6 +818,51 @@ contract tokenTest is Test {
assertTrue(token.balanceOf(burner) == 40);
}

function testTransferWith0Requirements() public {
uint256 mintAmount = 200;
uint256 transferAmount = 82;
address receiver = address(0x123);
vm.assume(mintAmount >= transferAmount);
vm.assume(mintAmount < type(uint256).max / 2); // avoid overflow due to fees
vm.assume(receiver != address(0));
vm.assume(receiver != pauser);

// create tokens
bytes32 roleMintAllower = token.MINTALLOWER_ROLE();

vm.prank(admin);
token.grantRole(roleMintAllower, mintAllower);
vm.prank(mintAllower);
token.increaseMintingAllowance(minter, mintAmount);
assertTrue(token.mintingAllowance(minter) == mintAmount);

// set requirements to 0
bytes32 role = token.REQUIREMENT_ROLE();
vm.prank(admin);
token.grantRole(role, requirer);
vm.prank(requirer);
token.setRequirements(0);
assertTrue(token.requirements() == 0);

// mint some tokens
vm.prank(minter);
token.mint(pauser, mintAmount);

assertTrue(token.balanceOf(pauser) == mintAmount);

// transfer token
vm.prank(pauser);
uint256 gasAfter = gasleft();
uint256 gasBefore = gasleft();
token.transfer(receiver, transferAmount);
gasAfter = gasleft();

console.log("gas used: ", gasBefore - gasAfter);

assertTrue(token.balanceOf(pauser) == mintAmount - transferAmount);
assertTrue(token.balanceOf(receiver) == transferAmount);
}

function testLoseAndGainRequirements() public {
address person1 = vm.addr(1);
address person2 = vm.addr(2);
Expand Down

0 comments on commit cf4267d

Please sign in to comment.