Skip to content

Commit

Permalink
feat: simplified/granularised tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed May 21, 2022
1 parent 88b8d47 commit 62038fe
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 232 deletions.
1 change: 0 additions & 1 deletion tests/fixtures/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from brownie import Contract
from brownie_tokens import ERC20


# ANYCALL DEPLOYMENT


Expand Down
93 changes: 49 additions & 44 deletions tests/reward_forwarder/test_deposit_reward.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,104 @@
import pytest
import brownie
import pytest

WEEK = 86400 * 7


def test_reward_deposit(alice, bob, charlie, child_gauge, reward_forwarder, reward_token):
@pytest.fixture(scope="module", autouse=True)
def setup(alice, bob, charlie, child_gauge, reward_token, lp_token):

reward_token._mint_for_testing(charlie, 10 ** 26, {"from": alice})
reward_forwarder.allow(reward_token, {"from": alice})
lp_token.approve(child_gauge, 10**21, {"from": alice})
lp_token._mint_for_testing(alice, 10**21, {"from": alice})
child_gauge.deposit(10**21, {"from": alice})

reward_token._mint_for_testing(charlie, 10**26, {"from": alice})
child_gauge.set_manager(bob, {"from": alice})


@pytest.fixture(scope="module", autouse=False)
def setup_with_deposited_rewards(alice, bob, charlie, reward_forwarder, child_gauge, reward_token):

child_gauge.add_reward(reward_token, reward_forwarder, {"from": bob})
reward_token.transfer(reward_forwarder, 10**20, {"from": charlie})

reward_token.transfer(reward_forwarder, 10 ** 20, {"from": charlie})
reward_forwarder.allow(reward_token, {"from": alice})
reward_forwarder.deposit_reward_token(reward_token, {"from": charlie})

assert reward_token.balanceOf(child_gauge) == 10 ** 20

def test_reward_deposit(alice, bob, charlie, child_gauge, reward_forwarder, reward_token):

def test_reward_token_approval(alice, bob, charlie, child_gauge, reward_forwarder, reward_token):
reward_forwarder.allow(reward_token, {"from": alice})
child_gauge.add_reward(reward_token, reward_forwarder, {"from": bob})
reward_token.transfer(reward_forwarder, 10**20, {"from": charlie})
reward_forwarder.deposit_reward_token(reward_token, {"from": charlie})

reward_token._mint_for_testing(charlie, 10 ** 26, {"from": alice})
reward_token.transfer(reward_forwarder, 10 ** 20, {"from": charlie})
assert reward_token.balanceOf(child_gauge) == 10**20
assert child_gauge.reward_data(reward_token)["rate"] > 0

child_gauge.set_manager(bob, {"from": alice})

def test_reward_deposit_reverts_without_allowance(
bob, charlie, child_gauge, reward_forwarder, reward_token
):

reward_token.transfer(reward_forwarder, 10**20, {"from": charlie})
child_gauge.add_reward(reward_token, reward_forwarder, {"from": bob})

# empty reward_forwarder cannot transfer tokens unless `allow` is called
with brownie.reverts():
reward_forwarder.deposit_reward_token(reward_token, {"from": charlie})


def test_unauthorised_distributor(alice, charlie, reward_forwarder, reward_token):
def test_reward_deposit_reverts_with_unauthorised_distributor(
alice, charlie, reward_forwarder, reward_token
):

reward_token._mint_for_testing(charlie, 10 ** 26, {"from": alice})
reward_forwarder.allow(reward_token, {"from": alice})
reward_token.transfer(reward_forwarder, 10 ** 20, {"from": charlie})
reward_token.transfer(reward_forwarder, 10**20, {"from": charlie})

# reward_forwarder cannot deposit unless it is added as a distributor for that token
# in the gauge contract
with brownie.reverts():
reward_forwarder.deposit_reward_token(reward_token, {"from": charlie})


def test_unauthorised_reward_token_for_authorised_distributor(
def test_reward_deposit_revert_for_unauthorised_token(
alice, bob, charlie, child_gauge, reward_forwarder, reward_token, unauthorised_token
):

reward_token._mint_for_testing(charlie, 10 ** 26, {"from": alice})
unauthorised_token._mint_for_testing(charlie, 10 ** 26, {"from": alice})
unauthorised_token._mint_for_testing(charlie, 10**26, {"from": alice})

reward_forwarder.allow(reward_token, {"from": alice})
reward_forwarder.allow(unauthorised_token, {"from": alice})

# only add one token to gauge rewards
child_gauge.set_manager(bob, {"from": alice})
child_gauge.add_reward(reward_token, reward_forwarder, {"from": bob})

reward_token.transfer(reward_forwarder, 10 ** 20, {"from": charlie})
unauthorised_token.transfer(reward_forwarder, 10 ** 20, {"from": charlie})
reward_token.transfer(reward_forwarder, 10**20, {"from": charlie})
unauthorised_token.transfer(reward_forwarder, 10**20, {"from": charlie})

with brownie.reverts():
reward_forwarder.deposit_reward_token(unauthorised_token, {"from": charlie})


def test_zero_reward_rate_claims(
alice, bob, charlie, child_gauge, chain, reward_forwarder, reward_token, lp_token
def test_reward_claim_when_reward_rate_is_zero(
alice,
bob,
charlie,
child_gauge,
chain,
reward_forwarder,
reward_token,
setup_with_deposited_rewards,
):

# mint lptokens and deposit into gauge:
lp_token.approve(child_gauge, 10 ** 21, {"from": alice})
lp_token._mint_for_testing(alice, 10 ** 21, {"from": alice})
child_gauge.deposit(10 ** 21, {"from": alice})

# mint reward tokens and approve transfers for RewardForwarder:
reward_token._mint_for_testing(charlie, 10 ** 26, {"from": alice})
reward_forwarder.allow(reward_token, {"from": alice})

# set gauge managers:
child_gauge.set_manager(bob, {"from": alice})
child_gauge.add_reward(reward_token, reward_forwarder, {"from": bob})

# deposit rewards and check if claimable reward token rate is non-zero:
reward_token.transfer(reward_forwarder, 10 ** 20, {"from": charlie})
reward_forwarder.deposit_reward_token(reward_token, {"from": charlie})
assert reward_token.balanceOf(reward_forwarder) == 0 # no tokens in the reward forwarder
assert child_gauge.reward_data(reward_token)[2] > 0 # token distribution rate is non-zero

# sleep for a week until after period finish:
chain.sleep(WEEK + 1) # sleep for 1 week and 1 second
assert chain.time() > child_gauge.reward_data(reward_token)[1] # `period_end` reached

# transferring zero reward tokens: this will make reward_token distribution rate zero
# reward forwarder has zero balance. Transferring zero reward tokens.
reward_forwarder.deposit_reward_token(reward_token, {"from": charlie})
assert child_gauge.reward_data(reward_token)[2] == 0 # token distribution rate becomes zero

# Every deposit of a reward token checkpoints reward distribution, updating integrals for users
# Token distribution rate should become zero:
assert child_gauge.reward_data(reward_token)[2] == 0

# check alice's balance before and after claims:
assert reward_token.balanceOf(alice) == 0
Expand Down
Loading

0 comments on commit 62038fe

Please sign in to comment.