Skip to content

Commit

Permalink
Merge pull request #19 from Giveth/feat/test_transfer
Browse files Browse the repository at this point in the history
Transfer Test
  • Loading branch information
aminlatifi authored Aug 17, 2022
2 parents cb7d819 + 9dadb01 commit a397d43
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 12 deletions.
5 changes: 1 addition & 4 deletions test/CalculatePower.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ contract CalculatePowerTest is GIVpowerTest {
function testSqrt(uint256 amount, uint8 rounds) public {
uint256 maxLockRounds = givPower.MAX_LOCK_ROUNDS();

// uint256 maxAmount = type(uint256).max / Math.sqrt(1e18 * (maxLockRounds + 1));
uint256 maxAmount = 1 << 220; // Chose this to be less dependent on the implemented Math.sqrt

vm.assume(rounds > 0);
vm.assume(rounds <= maxLockRounds);
vm.assume(amount < maxAmount);
vm.assume(amount < MAX_GIV_BALANCE);

string[] memory runJsInputs = new string[](4);

Expand Down
10 changes: 7 additions & 3 deletions test/GIVpowerTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import 'contracts/GardenUnipoolTokenDistributor.sol';
import './interfaces/IERC20Bridged.sol';

contract GIVpowerTest is Test {
uint256 public constant MAX_GIV_BALANCE = 10 ** 28; // 10 Billion, Total minted giv is 1B at the moment

ProxyAdmin gardenUnipoolProxyAdmin;
TransparentUpgradeableProxy gardenUnipoolProxy;
GIVpower implementation;
GIVpower givPower;
ITokenManager tokenManager;
IERC20Bridged givToken;
IERC20 ggivToken;
IERC20 gGivToken;
address givethMultisig;

// accounts
Expand All @@ -44,6 +46,7 @@ contract GIVpowerTest is Test {

event Staked(address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 amount);
event TokenLocked(address indexed account, uint256 amount, uint256 rounds, uint256 untilRound);
event TokenUnlocked(address indexed account, uint256 amount, uint256 round);
Expand All @@ -61,7 +64,7 @@ contract GIVpowerTest is Test {

givToken = IERC20Bridged(address(tokenManager.wrappableToken()));

ggivToken = IERC20(address(tokenManager.token()));
gGivToken = IERC20(address(tokenManager.token()));

givethMultisig = gardenUnipoolProxyAdmin.owner();
}
Expand All @@ -79,14 +82,15 @@ contract GIVpowerTest is Test {
givToken.mint(sender, 100 ether);

// labels
vm.label(sender, 'sender');
vm.label(senderWithNoBalance, 'senderWithNoBalance');
vm.label(givethMultisig, 'givethMultisig');
vm.label(address(gardenUnipoolProxyAdmin), 'ProxyAdmin');
vm.label(address(gardenUnipoolProxy), 'Proxy');
vm.label(address(givPower), 'GIVpower');
vm.label(address(tokenManager), 'TokenManager');
vm.label(address(givToken), 'GivethToken');
vm.label(address(ggivToken), 'gGivToken');
vm.label(address(gGivToken), 'gGivToken');
}

function getImplementationStorageData(address[] memory _users) public view returns (StorageData memory) {
Expand Down
10 changes: 5 additions & 5 deletions test/GeneralTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract GeneralTest is GIVpowerTest {

uint256 initialTotalSupply = givPower.totalSupply();
uint256 initialUnipoolBalance = givPower.balanceOf(sender);
uint256 initialgGivBalance = ggivToken.balanceOf(sender);
uint256 initialgGivBalance = gGivToken.balanceOf(sender);

// Before lock unipool balance should be same as gGiv balance
assertEq(initialgGivBalance, initialgGivBalance);
Expand All @@ -103,13 +103,13 @@ contract GeneralTest is GIVpowerTest {
vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(address(0), sender, wrapAmount);

vm.expectEmit(true, true, true, true, address(ggivToken));
vm.expectEmit(true, true, true, true, address(gGivToken));
emit Transfer(address(0), sender, wrapAmount);

tokenManager.wrap(wrapAmount);

assertEq(givPower.balanceOf(sender), initialUnipoolBalance + wrapAmount);
assertEq(ggivToken.balanceOf(sender), initialgGivBalance + wrapAmount);
assertEq(gGivToken.balanceOf(sender), initialgGivBalance + wrapAmount);
assertEq(givPower.totalSupply(), initialTotalSupply + wrapAmount);

/// LOCK
Expand All @@ -127,7 +127,7 @@ contract GeneralTest is GIVpowerTest {

assertEq(givPower.balanceOf(sender), wrapAmount + powerIncreaseAfterLock);
// gGIV balance should not change after lock
assertEq(ggivToken.balanceOf(sender), initialgGivBalance + wrapAmount);
assertEq(gGivToken.balanceOf(sender), initialgGivBalance + wrapAmount);

/// UNWRAP

Expand All @@ -137,7 +137,7 @@ contract GeneralTest is GIVpowerTest {
vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(sender, address(0), wrapAmount - lockAmount);

vm.expectEmit(true, true, true, true, address(ggivToken));
vm.expectEmit(true, true, true, true, address(gGivToken));
emit Transfer(sender, address(0), wrapAmount - lockAmount);

tokenManager.unwrap(wrapAmount - lockAmount);
Expand Down
169 changes: 169 additions & 0 deletions test/Transfer.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.6;

import './GIVpowerTest.sol';

contract TransferTest is GIVpowerTest {
function setUp() public override {
super.setUp();

vm.startPrank(omniBridge);
givToken.mint(sender, MAX_GIV_BALANCE - givToken.balanceOf(sender));
vm.stopPrank();
}

function testInitialBalance() public {
assertEq(givToken.balanceOf(sender), MAX_GIV_BALANCE);
assertEq(gGivToken.balanceOf(sender), 0);
assertEq(givPower.balanceOf(sender), 0);

assertEq(givToken.balanceOf(senderWithNoBalance), 0);
assertEq(gGivToken.balanceOf(senderWithNoBalance), 0);
assertEq(givPower.balanceOf(senderWithNoBalance), 0);
}

function testDirectTransfer(uint256 amount) public {
vm.assume(amount <= 100 ether);
vm.assume(amount > 0);

vm.expectEmit(true, true, true, true, address(givToken));
emit Approval(sender, address(tokenManager), amount);

vm.startPrank(sender);
givToken.approve(address(tokenManager), amount);

vm.expectEmit(true, true, true, true, address(givToken));
emit Transfer(sender, address(tokenManager), amount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Staked(sender, amount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(address(0), sender, amount);

vm.expectEmit(true, true, true, true, address(gGivToken));
emit Transfer(address(0), sender, amount);

tokenManager.wrap(amount);

assertEq(givToken.balanceOf(sender), MAX_GIV_BALANCE - amount);
assertEq(gGivToken.balanceOf(sender), amount);
assertEq(givPower.balanceOf(sender), amount);

vm.expectRevert(GIVpower.TokenNonTransferable.selector);
givPower.transfer(senderWithNoBalance, amount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Withdrawn(sender, amount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Staked(senderWithNoBalance, amount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(sender, senderWithNoBalance, amount);

vm.expectEmit(true, true, true, true, address(gGivToken));
emit Transfer(sender, senderWithNoBalance, amount);

gGivToken.transfer(senderWithNoBalance, amount);

assertEq(givToken.balanceOf(sender), MAX_GIV_BALANCE - amount);
assertEq(gGivToken.balanceOf(sender), 0);
assertEq(givPower.balanceOf(sender), 0);

assertEq(givToken.balanceOf(senderWithNoBalance), 0);
assertEq(gGivToken.balanceOf(senderWithNoBalance), amount);
assertEq(givPower.balanceOf(senderWithNoBalance), amount);

vm.stopPrank();
}

function testLock(uint256 amount, uint8 rounds) public {
uint256 maxLockRounds = givPower.MAX_LOCK_ROUNDS();

vm.assume(amount < MAX_GIV_BALANCE);
vm.assume(amount > 0);
vm.assume(rounds <= maxLockRounds);
vm.assume(rounds > 0);

vm.startPrank(sender);
givToken.approve(address(tokenManager), amount);
tokenManager.wrap(amount);

assertEq(gGivToken.balanceOf(sender), amount);
assertEq(givPower.balanceOf(sender), amount);

vm.expectRevert(GIVpower.TokenNonTransferable.selector);
givPower.transfer(senderWithNoBalance, amount);

uint256 lockReward = givPower.calculatePower(amount, rounds) - amount;

vm.assume(lockReward > 0);

vm.expectEmit(true, true, true, true, address(givPower));
emit Staked(sender, lockReward);

vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(address(0), sender, lockReward);

uint256 unlockRound = givPower.currentRound() + rounds;
givPower.lock(amount, rounds);

assertEq(gGivToken.balanceOf(sender), amount);
assertEq(givPower.balanceOf(sender), amount + lockReward);

vm.expectRevert(GIVpower.TokenNonTransferable.selector);
givPower.transfer(senderWithNoBalance, amount);

vm.expectRevert(GIVpower.TokenNonTransferable.selector);
givPower.transfer(senderWithNoBalance, amount + lockReward);

vm.expectRevert(GIVpower.TokensAreLocked.selector);
gGivToken.transfer(senderWithNoBalance, amount);

skip(givPower.ROUND_DURATION() * (rounds + 1));

vm.expectEmit(true, true, true, true, address(givPower));
emit Withdrawn(sender, lockReward);
vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(sender, address(0), lockReward);

address[] memory unlockAccounts = new address[](1);
unlockAccounts[0] = sender;
givPower.unlock(unlockAccounts, unlockRound);

///////////// Lock half the amount, the rest must be transferable
uint256 lockAmount = amount / 2;

vm.assume(lockAmount > 0);

lockReward = givPower.calculatePower(lockAmount, rounds) - lockAmount;

vm.assume(lockReward > 0);

unlockRound = givPower.currentRound() + rounds;
givPower.lock(lockAmount, rounds);

vm.expectRevert(GIVpower.TokensAreLocked.selector);
gGivToken.transfer(senderWithNoBalance, amount - lockAmount + 1);

// These seem repetitive
vm.expectEmit(true, true, true, true, address(givPower));
emit Withdrawn(sender, amount - lockAmount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Staked(senderWithNoBalance, amount - lockAmount);

vm.expectEmit(true, true, true, true, address(givPower));
emit Transfer(sender, senderWithNoBalance, amount - lockAmount);

vm.expectEmit(true, true, true, true, address(gGivToken));
emit Transfer(sender, senderWithNoBalance, amount - lockAmount);

gGivToken.transfer(senderWithNoBalance, amount - lockAmount);

skip(givPower.ROUND_DURATION() * (rounds + 1));
givPower.unlock(unlockAccounts, unlockRound);
gGivToken.transfer(senderWithNoBalance, lockAmount);
}
}

0 comments on commit a397d43

Please sign in to comment.