Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-llimos committed Nov 3, 2023
1 parent c20f741 commit 872824b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
6 changes: 1 addition & 5 deletions contracts/utils/TokenVestingStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ contract TokenVestingStaking is ITokenVestingStaking, Shared {
token.safeTransfer(beneficiary, unreleased);
}

// TODO: Revoking during linear vesting (after staking period) will not respect the linear vesting,
// it will just revoke anything on the contract and anything that is currently staked. We
// either accept this or we don't allow revokation after stakingVestingEnd
/**
* @notice Allows the revoker to revoke the vesting and stop the beneficiary from releasing any
* tokens if the vesting period has not been completed. Any staked tokens at the time of
Expand Down Expand Up @@ -200,7 +197,6 @@ contract TokenVestingStaking is ITokenVestingStaking, Shared {
*/
function _releasableAmount(IERC20 token) private view returns (uint256) {
return _vestedAmount(token) - released[token];
// return block.timestamp < end ? 0 : token.balanceOf(address(this));
}

/**
Expand All @@ -219,7 +215,7 @@ contract TokenVestingStaking is ITokenVestingStaking, Shared {
if (block.timestamp >= end || revoked) {
return totalBalance;
} else {
// Assumption cliff == 0
// No cliff
return (totalBalance * (block.timestamp - stakingVestingEnd)) / (end - stakingVestingEnd);
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/token_vesting/release/test_release_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def test_release_staking_rewards_after_end(
)


# Test that the assert(!canStake) is not reached => cliff == end == start + QUARTER_YEAR + YEAR
@given(st_sleepTime=strategy("uint256", min_value=QUARTER_YEAR, max_value=YEAR * 2))
def test_release_around_cliff(
addrs, cf, tokenVestingStaking, addressHolder, st_sleepTime
Expand Down
31 changes: 31 additions & 0 deletions tests/token_vesting/revoke/test_revoke_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,34 @@ def test_fund_revoked_staked(addrs, cf, tokenVestingStaking):
with reverts(REV_MSG_TOKEN_REVOKED):
tv.fundStateChainAccount(nodeID1, MAX_TEST_FUND, {"from": addrs.BENEFICIARY})
retrieve_revoked_and_check(tv, cf, addrs.REVOKER, MAX_TEST_FUND)


# Revoke with some tokens staked and linear vesting has started
def test_revoke_staked_linear(addrs, cf, tokenVestingStaking):
tv, stakingVestingEnd, end, total = tokenVestingStaking
amount_staked = (total * 2) // 3
tv.fundStateChainAccount(JUNK_HEX, amount_staked, {"from": addrs.BENEFICIARY})
# Half the vesting period
chain.sleep(stakingVestingEnd + (end - stakingVestingEnd) // 2 - getChainTime())
amountInContract = total - amount_staked
assert cf.flip.balanceOf(tv) == amountInContract

# Release approx half of the tokens in the contract
tx = tv.release(cf.flip, {"from": addrs.BENEFICIARY})
releasedAmount = tx.events["TokensReleased"]["amount"]
assert amountInContract // 2 <= releasedAmount <= amountInContract * 1.01 // 2
amountInContract = amountInContract - releasedAmount
assert cf.flip.balanceOf(tv) == amountInContract

# Revoke remaining tokens in the contract
tx = tv.revoke(cf.flip, {"from": addrs.REVOKER})
revokedAmount = tx.events["TokenVestingRevoked"]["refund"]
assert revokedAmount == amountInContract

# Imitate unstake the amount
cf.flip.transfer(tv, amount_staked, {"from": addrs.DEPLOYER})

with reverts(REV_MSG_TOKEN_REVOKED):
tv.release(cf.flip, {"from": addrs.BENEFICIARY})

retrieve_revoked_and_check(tv, cf, addrs.REVOKER, amount_staked)
30 changes: 30 additions & 0 deletions tests/token_vesting/stakeStProvider/test_stProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,33 @@ def test_stProviderClaimRewardsSlash(addrs, tokenVestingStaking, cf, mockStProvi

with reverts(REV_MSG_INTEGER_OVERFLOW):
tv.claimStProviderRewards(2**256 - 1, {"from": addrs.BENEFICIARY})


def test_stProviderRewards_release(addrs, tokenVestingStaking, cf, mockStProvider):
tv, stakingVestingEnd, end, total = tokenVestingStaking
stFLIP, minter, _, _ = mockStProvider
reward_amount = total

cf.flip.approve(minter, 2**256 - 1, {"from": addrs.DEPLOYER})
minter.mint(addrs.DEPLOYER, reward_amount, {"from": addrs.DEPLOYER})

flipStaked = total // 3
flipInContract = total - flipStaked
tv.stakeToStProvider(flipStaked, {"from": addrs.BENEFICIARY})
stFlipInContract = flipStaked

# Earn rewards
stFLIP.transfer(tv, reward_amount, {"from": addrs.DEPLOYER})
stFlipInContract += reward_amount

# Approximately into the middle of the vesting period
chain.sleep(stakingVestingEnd + (end - stakingVestingEnd) // 2 - getChainTime())

# The vested percentage of FLIP and stFLIP can be released.
tx = tv.release(cf.flip, {"from": addrs.BENEFICIARY})
releasedAmount = tx.events["TokensReleased"]["amount"]
assert flipInContract // 2 <= releasedAmount <= flipInContract * 1.01 // 2

tx = tv.release(stFLIP, {"from": addrs.BENEFICIARY})
releasedAmount = tx.events["TokensReleased"]["amount"]
assert stFlipInContract // 2 <= releasedAmount <= stFlipInContract * 1.01 // 2

0 comments on commit 872824b

Please sign in to comment.