def test_sett_earn_permissions(settConfig): # Setup badger = badger_single_sett(settConfig) state_setup(badger, settConfig) settId = settConfig["id"] sett = badger.getSett(settId) randomUser = accounts[8] assert sett.strategist() == AddressZero # End Setup # == Authorized Actors == # earn authorizedActors = [ sett.governance(), sett.keeper(), ] with brownie.reverts("onlyAuthorizedActors"): sett.earn({"from": randomUser}) for actor in authorizedActors: chain.snapshot() sett.earn({"from": actor}) chain.revert()
def test_sett_pausing_permissions(settConfig): # Setup badger = badger_single_sett(settConfig) state_setup(badger, settConfig) settId = settConfig["id"] sett = badger.getSett(settId) deployer = badger.deployer randomUser = accounts[8] assert sett.strategist() == AddressZero # End Setup authorizedPausers = [ sett.governance(), sett.guardian(), ] authorizedUnpausers = [ sett.governance(), ] # pause onlyPausers for pauser in authorizedPausers: sett.pause({"from": pauser}) sett.unpause({"from": authorizedUnpausers[0]}) with brownie.reverts("onlyPausers"): sett.pause({"from": randomUser}) # unpause onlyPausers for unpauser in authorizedUnpausers: sett.pause({"from": unpauser}) sett.unpause({"from": unpauser}) sett.pause({"from": sett.guardian()}) with brownie.reverts("onlyGovernance"): sett.unpause({"from": randomUser}) settKeeper = accounts.at(sett.keeper(), force=True) with brownie.reverts("Pausable: paused"): sett.earn({"from": settKeeper}) with brownie.reverts("Pausable: paused"): sett.withdrawAll({"from": deployer}) with brownie.reverts("Pausable: paused"): sett.withdraw(1, {"from": deployer}) with brownie.reverts("Pausable: paused"): sett.deposit(1, {"from": randomUser}) with brownie.reverts("Pausable: paused"): sett.depositAll({"from": randomUser}) sett.unpause({"from": authorizedUnpausers[0]}) sett.deposit(1, {"from": deployer}) sett.earn({"from": settKeeper}) sett.withdraw(1, {"from": deployer}) sett.withdrawAll({"from": deployer})
def test_simulation(settConfig): # connect to prod deploy and run simulation badger = badger_single_sett(settConfig, deploy=False) snap = DiggSnapshotManager(badger, settConfig["id"]) simulation = SimulationManager(badger, snap, settConfig["id"]) simulation.provision() # Randomize 100 actions. simulation.randomize(100) simulation.run()
def test_simulation_after_upgrade_crv_setts(settID): # Upgrade crv strategy badger = connect_badger(badger_config.prod_json) """ TODO Get the Implementation before upgrade """ txFilename = queue_upgrade_crv_strat(badger, settID) # Sleep 2 days to pass timelock delay period. chain.sleep(2 * days(2)) badger.governance_execute_transaction(txFilename) """ TODO assert tht implementation has changed """ ## Object representing the sett we want and the mode we're in thisSettConfig = {"id": settID, "mode": "test"} ## Get badger so we can get info in sett and strats badger = badger_single_sett(thisSettConfig) ## We now have the want, we can mint some deployer = badger.deployer ## Mints token for us distribute_from_whales(deployer) snap = SnapshotManager(badger, settID) simulation = SimulationManager(badger, snap, settID) simulation.provision() # Randomize 30 actions. simulation.randomize(30) simulation.run() assert_deposit_withdraw_single_user_flow(thisSettConfig) assert_single_user_harvest_flow(thisSettConfig) assert_migrate_single_user(thisSettConfig) assert_withdraw_other(thisSettConfig) assert_single_user_harvest_flow_remove_fees(thisSettConfig) assert_strategy_action_permissions(thisSettConfig) assert_strategy_config_permissions(thisSettConfig) assert_strategy_pausing_permissions(thisSettConfig) assert_sett_pausing_permissions(thisSettConfig) assert_sett_config_permissions(thisSettConfig) assert_controller_permissions(thisSettConfig)
def test_deposit_withdraw_single_user_flow(settConfig): badger = badger_single_sett(settConfig) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) deployer = badger.deployer settKeeper = accounts.at(sett.keeper(), force=True) snap = SnapshotManager(badger, settConfig["id"]) randomUser = accounts[6] print("== Testing == ", settConfig["id"], want.address) # Deposit assert want.balanceOf(deployer) > 0 depositAmount = int(want.balanceOf(deployer) * 0.8) assert depositAmount > 0 want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) # Earn with brownie.reverts("onlyAuthorizedActors"): sett.earn({"from": randomUser}) min = sett.min() max = sett.max() remain = max - min snap.settEarn({"from": settKeeper}) chain.sleep(15) chain.mine(1) snap.settWithdraw(depositAmount // 2, {"from": deployer}) chain.sleep(10000) chain.mine(1) snap.settWithdraw(depositAmount // 2 - 1, {"from": deployer})
def test_deposit_withdraw_single_user_flow(settId): badger = badger_single_sett(settId) controller = badger.getController(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) snap = SnapshotManager(badger, settId) deployer = badger.deployer randomUser = accounts[6] print(want, want.address, want.totalSupply(), deployer) # Deposit depositAmount = int(want.balanceOf(deployer) * 0.8) want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) # Earn with brownie.reverts("onlyAuthorizedActors"): sett.earn({"from": randomUser}) min = sett.min() max = sett.max() remain = max - min assert sett.keeper() == deployer snap.settEarn({"from": deployer}) chain.sleep(15) chain.mine(1) snap.settWithdraw(depositAmount // 2, {"from": deployer}) chain.sleep(10000) chain.mine(1) snap.settWithdrawAll({"from": deployer})
def test_sett_config_permissions(settConfig): # Setup badger = badger_single_sett(settConfig) state_setup(badger, settConfig) settId = settConfig["id"] sett = badger.getSett(settId) randomUser = accounts[8] assert sett.strategist() == AddressZero # End Setup # == Governance == validActor = sett.governance() # setMin with brownie.reverts("onlyGovernance"): sett.setMin(0, {"from": randomUser}) sett.setMin(0, {"from": validActor}) assert sett.min() == 0 # setController with brownie.reverts("onlyGovernance"): sett.setController(AddressZero, {"from": randomUser}) sett.setController(AddressZero, {"from": validActor}) assert sett.controller() == AddressZero # setStrategist with brownie.reverts("onlyGovernance"): sett.setStrategist(validActor, {"from": randomUser}) sett.setStrategist(validActor, {"from": validActor}) assert sett.strategist() == validActor with brownie.reverts("onlyGovernance"): sett.setKeeper(validActor, {"from": randomUser}) sett.setKeeper(validActor, {"from": validActor}) assert sett.keeper() == validActor
def test_claw(settConfig): badger = badger_single_sett(settConfig, deploy=False) snap = SnapshotManager(badger, settConfig["id"]) deployer = badger.deployer want = badger.getStrategyWant(settConfig["id"]) sett = badger.getSett(settConfig["id"]) depositAmount = int(want.balanceOf(deployer) * 0.8) assert depositAmount > 0 want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) assert want.balanceOf(deployer) > 0 distribute_test_ether(deployer, Wei("20 ether")) distribute_from_whales(deployer) claw = deploy_claw_minimal(deployer) if settConfig["id"] == "native.badger": _manage_position(claw, "bClaw", deployer) if settConfig["id"] == "sushi.sushiWbtcEth": _manage_position(claw, "sClaw", deployer)
def test_withdraw_other(settId): """ - Controller should be able to withdraw other tokens - Controller should not be able to withdraw core tokens - Non-controller shouldn't be able to do either """ badger = badger_single_sett(settId) controller = badger.getController(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) deployer = badger.deployer randomUser = accounts[6] startingBalance = want.balanceOf(deployer) depositAmount = Wei("1 ether") print(getTokenMetadata(want.address), startingBalance) assert startingBalance >= depositAmount # Deposit want.approve(sett, MaxUint256, {"from": deployer}) sett.deposit(depositAmount, {"from": deployer}) after = sett_snapshot(sett, strategy, deployer) chain.sleep(15) chain.mine() sett.earn({"from": deployer}) chain.sleep(days(0.5)) chain.mine() if strategy.isTendable(): strategy.tend({"from": deployer}) strategy.harvest({"from": deployer}) chain.sleep(days(0.5)) chain.mine() mockAmount = Wei("1000 ether") mockToken = MockToken.deploy({"from": deployer}) mockToken.initialize([strategy], [mockAmount], {"from": deployer}) assert mockToken.balanceOf(strategy) == mockAmount # Should not be able to withdraw protected tokens protectedTokens = strategy.getProtectedTokens() print(protectedTokens) for token in protectedTokens: with brownie.reverts(): controller.inCaseStrategyTokenGetStuck(strategy, token, {"from": deployer}) # Should send balance of non-protected token to sender controller.inCaseStrategyTokenGetStuck(strategy, mockToken, {"from": deployer}) with brownie.reverts(): controller.inCaseStrategyTokenGetStuck( strategy, mockToken, {"from": randomUser} ) assert mockToken.balanceOf(controller) == mockAmount
def test_single_user_harvest_flow(settConfig): badger = badger_single_sett(settConfig) depositAmount = 50 * 1e8 # renBTC decimal is 8 # test settings controller = badger.getController(settConfig["id"]) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) deployer = badger.deployer # production settings # controller = interface.IController("0x9b4efa18c0c6b4822225b81d150f3518160f8609"); # sett = interface.ISett("0x77f07Dd580cc957109c70c7fa81aa5704f8a3572") # strategy = StrategyUnitProtocolRenbtc.at("0x5640d6E2F72e76FBCb5296d59EA28C7375F1fE12"); # want = interface.IERC20("0xEB4C2781e4ebA804CE9a9803C67d0893436bB27D") # deployer = accounts.at("0x576cD258835C529B54722F84Bb7d4170aA932C64", force=True) # controllerGov = accounts.at("0xB65cef03b9B89f99517643226d76e286ee999e77", force=True) # ethWhale = accounts.at("0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE", force=True) # ethWhale.transfer(deployer, "100 ether") # settGuestList = interface.IVipCappedGuestList("0x9FC48e61B6a75eE263ca160aCF3288A99238719E"); # settGuestList.setGuests([deployer], [True], {"from": deployer}); # settGuestList.setUserDepositCap(depositAmount * 2, {"from": deployer}); # settGuestList.setTotalDepositCap(depositAmount * 10, {"from": deployer}); # controller.setVault(want, sett, {"from": deployer}); # controller.approveStrategy(want, strategy, {"from": controllerGov}); # controller.setStrategy(want, strategy, {"from": deployer}); settKeeper = accounts.at(sett.keeper(), force=True) strategyKeeper = accounts.at(strategy.keeper(), force=True) snap = SnapshotManager(badger, settConfig["id"]) governance = strategy.governance() tendable = strategy.isTendable() distribute_from_whales(deployer) startingBalance = want.balanceOf(deployer) assert startingBalance >= depositAmount assert startingBalance >= 0 # Deposit want.approve(sett, MaxUint256, {"from": deployer}) # sett.deposit(depositAmount, {"from": deployer}); snap.settDeposit(depositAmount, {"from": deployer}) assert want.balanceOf(sett) > 0 print("want.balanceOf(sett)", want.balanceOf(sett)) # Earn # sett.earn({"from": settKeeper}); snap.settEarn({"from": settKeeper}) # Harvest chain.sleep(hours(0.1)) chain.mine() # strategy.harvest({"from": strategyKeeper}) snap.settHarvest({"from": strategyKeeper}) # Withdraw half # sett.withdraw(depositAmount // 2, {"from": deployer}) snap.settWithdraw(depositAmount // 2, {"from": deployer}) # KeepMinRatio to maintain collateralization safe enough from liquidation currentRatio = strategy.currentRatio() safeRatio = currentRatio + 20 strategy.setMinRatio(safeRatio, {"from": governance}) strategy.keepMinRatio({"from": governance}) assert strategy.currentRatio() > safeRatio # sugar-daddy usdp discrepancy due to accrued interest in Unit Protocol debtTotal = strategy.getDebtBalance() curveGauge = interface.ICurveGauge( "0x055be5DDB7A925BfEF3417FC157f53CA77cA7222") usdp3crvInGauge = curveGauge.balanceOf(strategy) curvePool = interface.ICurveFi( "0x42d7025938bEc20B69cBae5A77421082407f053A") usdpOfPool = curvePool.calc_withdraw_one_coin(usdp3crvInGauge, 0) sugar = (debtTotal - usdpOfPool) * 2 usdpToken = interface.IERC20("0x1456688345527bE1f37E9e627DA0837D6f08C925") if sugar > 0: usdpToken.transfer(strategy, sugar, {"from": deployer}) print("sugar debt=", sugar) # Harvest again chain.sleep(hours(0.1)) chain.mine() # strategy.harvest({"from": strategyKeeper}) snap.settHarvest({"from": strategyKeeper}) # Withdraw all wantInSettBalance = sett.getPricePerFullShare() * sett.totalSupply() / 1e18 print("wantInSett=", wantInSettBalance) print("wantInStrategy=", strategy.balanceOfPool()) print("pricePerFullShare=", sett.getPricePerFullShare()) wantToWithdraw = sett.balanceOf( deployer) * sett.getPricePerFullShare() / 1e18 print("wantToWithdraw=", wantToWithdraw) assert wantToWithdraw <= wantInSettBalance sugarWithdrawAll = (strategy.getDebtBalance() - strategy.balanceOfPool()) * 2 if sugarWithdrawAll > 0: usdpToken.transfer(strategy, sugarWithdrawAll, {"from": deployer}) print("sugarWithdrawAll=", sugarWithdrawAll) renbtcToken = interface.IERC20( "0xEB4C2781e4ebA804CE9a9803C67d0893436bB27D") controller.withdrawAll(renbtcToken, {"from": deployer}) # sett.withdrawAll({"from": deployer}) snap.settWithdrawAll({"from": deployer}) assert True
def test_strategy_action_permissions(settConfig): # Setup badger = badger_single_sett(settConfig) state_setup(badger, settConfig) settId = settConfig["id"] controller = badger.getControllerFor(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) tendable = strategy.isTendable() deployer = badger.deployer randomUser = accounts[8] # End Setup # ===== Strategy ===== authorizedActors = [ strategy.governance(), strategy.keeper(), ] with brownie.reverts("onlyAuthorizedActorsOrController"): strategy.deposit({"from": randomUser}) for actor in authorizedActors: strategy.deposit({"from": actor}) # harvest: onlyAuthorizedActors with brownie.reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) for actor in authorizedActors: strategy.harvest({"from": actor}) # (if tendable) tend: onlyAuthorizedActors if tendable: with brownie.reverts("onlyAuthorizedActors"): strategy.tend({"from": randomUser}) for actor in authorizedActors: strategy.tend({"from": actor}) actorsToCheck = [ randomUser, strategy.governance(), strategy.strategist(), strategy.keeper(), ] # withdrawAll onlyController for actor in actorsToCheck: with brownie.reverts("onlyController"): strategy.withdrawAll({"from": actor}) # withdraw onlyController for actor in actorsToCheck: with brownie.reverts("onlyController"): strategy.withdraw(1, {"from": actor}) # withdrawOther _onlyNotProtectedTokens for actor in actorsToCheck: with brownie.reverts("onlyController"): strategy.withdrawOther(controller, {"from": actor})
def assert_migrate_single_user(settConfig): badger = badger_single_sett(settConfig) controller = badger.getController(settConfig["id"]) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) strategyKeeper = accounts.at(strategy.keeper(), force=True) deployer = badger.deployer randomUser = accounts[6] snap = SnapshotManager(badger, settConfig["id"]) startingBalance = want.balanceOf(deployer) depositAmount = startingBalance // 2 assert startingBalance >= depositAmount # Deposit want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) chain.sleep(15) chain.mine() sett.earn({"from": strategyKeeper}) chain.snapshot() # Test no harvests chain.sleep(days(2)) chain.mine() before = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf() } with brownie.reverts(): controller.withdrawAll(strategy.want(), {"from": randomUser}) controller.withdrawAll(strategy.want(), {"from": deployer}) after = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf() } assert after["settWant"] > before["settWant"] assert after["stratWant"] < before["stratWant"] assert after["stratWant"] == 0 # Test tend only if strategy.isTendable(): chain.revert() chain.sleep(days(2)) chain.mine() strategy.tend({"from": strategyKeeper}) before = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf() } with brownie.reverts(): controller.withdrawAll(strategy.want(), {"from": randomUser}) controller.withdrawAll(strategy.want(), {"from": deployer}) after = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf() } assert after["settWant"] > before["settWant"] assert after["stratWant"] < before["stratWant"] assert after["stratWant"] == 0 # Test harvest, with tend if tendable chain.revert() chain.sleep(days(1)) chain.mine() if strategy.isTendable(): strategy.tend({"from": strategyKeeper}) chain.sleep(days(1)) chain.mine() before = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf(), "rewardsWant": want.balanceOf(controller.rewards()), } with brownie.reverts(): controller.withdrawAll(strategy.want(), {"from": randomUser}) controller.withdrawAll(strategy.want(), {"from": deployer}) after = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf() } assert after["settWant"] > before["settWant"] assert after["stratWant"] < before["stratWant"] assert after["stratWant"] == 0
def assert_withdraw_other(settConfig): """ - Controller should be able to withdraw other tokens - Controller should not be able to withdraw core tokens - Non-controller shouldn't be able to do either """ badger = badger_single_sett(settConfig) controller = badger.getController(settConfig["id"]) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) deployer = badger.deployer randomUser = accounts[6] strategyKeeper = accounts.at(strategy.keeper(), force=True) startingBalance = want.balanceOf(deployer) depositAmount = Wei("1 ether") assert startingBalance >= depositAmount # Deposit want.approve(sett, MaxUint256, {"from": deployer}) sett.deposit(depositAmount, {"from": deployer}) chain.sleep(15) chain.mine() sett.earn({"from": strategyKeeper}) chain.sleep(days(0.5)) chain.mine() if strategy.isTendable(): strategy.tend({"from": strategyKeeper}) ## Extra sleep because some tend will actually swap chain.sleep(days(1)) chain.mine() strategy.harvest({"from": strategyKeeper}) chain.sleep(days(0.5)) chain.mine() mockAmount = Wei("1000 ether") mockToken = MockToken.deploy({"from": deployer}) mockToken.initialize([strategy], [mockAmount], {"from": deployer}) assert mockToken.balanceOf(strategy) == mockAmount # Should not be able to withdraw protected tokens protectedTokens = strategy.getProtectedTokens() for token in protectedTokens: with brownie.reverts(): controller.inCaseStrategyTokenGetStuck(strategy, token, {"from": strategyKeeper}) # Should send balance of non-protected token to sender controller.inCaseStrategyTokenGetStuck(strategy, mockToken, {"from": deployer}) with brownie.reverts(): controller.inCaseStrategyTokenGetStuck(strategy, mockToken, {"from": randomUser}) assert mockToken.balanceOf(controller) == mockAmount
def test_strategy_pausing_permissions(settConfig): # Setup badger = badger_single_sett(settConfig) state_setup(badger, settConfig) settId = settConfig["id"] controller = badger.getControllerFor(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) tendable = strategy.isTendable() deployer = badger.deployer randomUser = accounts[8] # End Setup authorizedPausers = [ strategy.governance(), strategy.guardian(), ] authorizedUnpausers = [ strategy.governance(), ] # pause onlyPausers for pauser in authorizedPausers: strategy.pause({"from": pauser}) strategy.unpause({"from": authorizedUnpausers[0]}) with brownie.reverts("onlyPausers"): strategy.pause({"from": randomUser}) # unpause onlyPausers for unpauser in authorizedUnpausers: strategy.pause({"from": unpauser}) strategy.unpause({"from": unpauser}) with brownie.reverts("onlyGovernance"): strategy.unpause({"from": randomUser}) strategy.pause({"from": strategy.guardian()}) strategyKeeper = accounts.at(strategy.keeper(), force=True) with brownie.reverts("Pausable: paused"): sett.withdrawAll({"from": deployer}) with brownie.reverts("Pausable: paused"): strategy.harvest({"from": strategyKeeper}) if strategy.isTendable(): with brownie.reverts("Pausable: paused"): strategy.tend({"from": strategyKeeper}) strategy.unpause({"from": authorizedUnpausers[0]}) sett.deposit(1, {"from": deployer}) sett.withdraw(1, {"from": deployer}) sett.withdrawAll({"from": deployer}) strategy.harvest({"from": strategyKeeper}) if strategy.isTendable(): strategy.tend({"from": strategyKeeper})
def test_strategy_config_permissions(settConfig): # Setup badger = badger_single_sett(settConfig) state_setup(badger, settConfig) settId = settConfig["id"] controller = badger.getControllerFor(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) tendable = strategy.isTendable() deployer = badger.deployer randomUser = accounts[8] # End Setup governance = strategy.governance() # Valid User should update strategy.setGuardian(AddressZero, {"from": governance}) assert strategy.guardian() == AddressZero strategy.setWithdrawalFee(0, {"from": governance}) assert strategy.withdrawalFee() == 0 strategy.setPerformanceFeeStrategist(0, {"from": governance}) assert strategy.performanceFeeStrategist() == 0 strategy.setPerformanceFeeGovernance(0, {"from": governance}) assert strategy.performanceFeeGovernance() == 0 strategy.setController(AddressZero, {"from": governance}) assert strategy.controller() == AddressZero # Invalid User should fail with brownie.reverts("onlyGovernance"): strategy.setGuardian(AddressZero, {"from": randomUser}) with brownie.reverts("onlyGovernance"): strategy.setWithdrawalFee(0, {"from": randomUser}) with brownie.reverts("onlyGovernance"): strategy.setPerformanceFeeStrategist(0, {"from": randomUser}) with brownie.reverts("onlyGovernance"): strategy.setPerformanceFeeGovernance(0, {"from": randomUser}) with brownie.reverts("onlyGovernance"): strategy.setController(AddressZero, {"from": randomUser}) # Special fees: onlyGovernance # Pickle: setPicklePerformanceFeeGovernance # Pickle: setPicklePerformanceFeeStrategist if settId == "pickle.renCrv": strategy.setPicklePerformanceFeeGovernance(0, {"from": governance}) assert strategy.picklePerformanceFeeGovernance() == 0 strategy.setPicklePerformanceFeeStrategist(0, {"from": governance}) assert strategy.picklePerformanceFeeStrategist() == 0 with brownie.reverts("onlyGovernance"): strategy.setPicklePerformanceFeeGovernance(0, {"from": randomUser}) with brownie.reverts("onlyGovernance"): strategy.setPicklePerformanceFeeStrategist(0, {"from": randomUser}) # Harvest: if settId == "harvest.renCrv": strategy.setFarmPerformanceFeeGovernance(0, {"from": governance}) assert strategy.farmPerformanceFeeGovernance() == 0 strategy.setFarmPerformanceFeeStrategist(0, {"from": governance}) assert strategy.farmPerformanceFeeStrategist() == 0 with brownie.reverts("onlyGovernance"): strategy.setFarmPerformanceFeeGovernance(0, {"from": randomUser}) with brownie.reverts("onlyGovernance"): strategy.setFarmPerformanceFeeStrategist(0, {"from": randomUser})
def test_single_user_harvest_flow(settId): suiteName = "test_single_user_harvest_flow" + ": " + settId testRecorder = TestRecorder(suiteName) badger = badger_single_sett(settId) controller = badger.getController(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) snap = SnapshotManager(badger, settId) deployer = badger.deployer randomUser = accounts[6] tendable = strategy.isTendable() startingBalance = want.balanceOf(deployer) depositAmount = Wei("1 ether") assert startingBalance >= depositAmount # Deposit want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) # Earn snap.settEarn(depositAmount, {"from": deployer}) if tendable: with brownie.reverts("onlyAuthorizedActors"): strategy.tend({"from": randomUser}) snap.settTend({"from": deployer}) chain.sleep(days(0.5)) chain.mine() if tendable: snap.settTend({"from": deployer}) chain.sleep(days(1)) chain.mine() with brownie.reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) snap.settHarvest({"from": deployer}) chain.sleep(days(1)) chain.mine() if tendable: snap.settTend({"from": deployer}) chain.sleep(days(3)) chain.mine() snap.settHarvest({"from": deployer}) snap.settWithdrawAll({"from": deployer}) endingBalance = want.balanceOf(deployer) assert endingBalance > startingBalance
def test_single_user_harvest_flow_remove_fees(settId): suiteName = "test_single_user_harvest_flow_remove_fees" + ": " + settId testRecorder = TestRecorder(suiteName) badger = badger_single_sett(settId) controller = badger.getController(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) deployer = badger.deployer randomUser = accounts[6] tendable = strategy.isTendable() startingBalance = want.balanceOf(deployer) depositAmount = Wei("1 ether") assert startingBalance >= depositAmount # Deposit before = sett_snapshot(sett, strategy, deployer) want.approve(sett, MaxUint256, {"from": deployer}) sett.deposit(depositAmount, {"from": deployer}) after = sett_snapshot(sett, strategy, deployer) confirm_deposit(before, after, deployer, depositAmount) # Earn before = sett_snapshot(sett, strategy, deployer) sett.earn({"from": deployer}) after = sett_snapshot(sett, strategy, deployer) confirm_earn(before, after) chain.sleep(days(0.5)) chain.mine() if tendable: before = sett_snapshot(sett, strategy, deployer) tx = strategy.tend({"from": deployer}) after = sett_snapshot(sett, strategy, deployer) testRecorder.add_record(EventRecord("Tend", tx.events, tx.timestamp)) confirm_tend(before, after, deployer) chain.sleep(days(1)) chain.mine() with brownie.reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) before = sett_snapshot(sett, strategy, deployer) tx = strategy.harvest({"from": deployer}) after = sett_snapshot(sett, strategy, deployer) testRecorder.add_record(EventRecord("Harvest", tx.events, tx.timestamp)) testRecorder.print_to_file(suiteName + ".json") confirm_harvest(before, after, deployer) after_harvest = sett_snapshot(sett, strategy, deployer) print("tx.events", tx.events) # Harvesting on the HarvestMetaFarm does not increase the underlying position, it sends rewards to the rewardsTree # For HarvestMetaFarm, we expect FARM rewards to be distributed to rewardsTree if settId == "harvest.renCrv": assert want.balanceOf(controller.rewards() > 0) # For most Setts, harvesting should increase the underlying position else: assert want.balanceOf(controller.rewards() > 0) chain.sleep(days(1)) chain.mine() if tendable: tx = strategy.tend({"from": deployer}) testRecorder.add_record(EventRecord("Tend", tx.events, tx.timestamp)) chain.sleep(days(3)) chain.mine() before_harvest = sett_snapshot(sett, strategy, deployer) tx = strategy.harvest({"from": deployer}) after_harvest = sett_snapshot(sett, strategy, deployer) testRecorder.add_record(EventRecord("Harvest", tx.events, tx.timestamp)) harvested = tx.events["Harvest"][0]["harvested"] if settId != "harvest.renCrv": assert harvested > 0 assert ( after_harvest.sett.pricePerFullShare > before_harvest.sett.pricePerFullShare ) assert after_harvest.strategy.balanceOf > before_harvest.strategy.balanceOf sett.withdrawAll({"from": deployer}) endingBalance = want.balanceOf(deployer) report = { "time": "4 days", "gains": endingBalance - startingBalance, "gainsPercentage": (endingBalance - startingBalance) / startingBalance, } # testRecorder.add_record(EventRecord("Final Report", report, 0)) testRecorder.print_to_file(suiteName + ".json")
def test_single_user_harvest_flow(settConfig): badger = badger_single_sett(settConfig) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) settKeeper = accounts.at(sett.keeper(), force=True) strategyKeeper = accounts.at(strategy.keeper(), force=True) snap = DiggSnapshotManager(badger, settConfig["id"]) deployer = badger.deployer randomUser = accounts[6] tendable = strategy.isTendable() startingBalance = want.balanceOf(deployer) depositAmount = startingBalance // 2 assert startingBalance >= depositAmount assert startingBalance >= 0 # Deposit want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) # Push/rebase on an exchange rate of 1.2 (DIGG trading at 1.2x BTC) snap.rebase(1.2 * 10**18, {"from": deployer}) # Earn snap.settEarn({"from": settKeeper}) if tendable: with reverts("onlyAuthorizedActors"): strategy.tend({"from": randomUser}) snap.settTend({"from": strategyKeeper}) chain.sleep(days(0.5)) chain.mine() # Push/rebase on an exchange rate of 0.6 (DIGG trading at 0.8x BTC) snap.rebase(0.6 * 10**18, {"from": deployer}) if tendable: snap.settTend({"from": strategyKeeper}) chain.sleep(days(1)) chain.mine() with reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) snap.settHarvest({"from": strategyKeeper}) chain.sleep(days(1)) chain.mine() # Push/rebase on an exchange rate of 1.6 (DIGG trading at 1.6x BTC) snap.rebase(1.6 * 10**18, {"from": deployer}) if tendable: snap.settTend({"from": strategyKeeper}) snap.settWithdraw(depositAmount // 2, {"from": deployer}) chain.sleep(days(3)) chain.mine() # Push/rebase on an exchange rate of 0.7 (DIGG trading at 0.7x BTC) snap.rebase(0.7 * 10**18, {"from": deployer}) snap.settHarvest({"from": strategyKeeper}) snap.settWithdraw(depositAmount // 2 - 1, {"from": deployer}) assert False
def test_migrate_single_user(settId): badger = badger_single_sett(settId) controller = badger.getController(settId) sett = badger.getSett(settId) strategy = badger.getStrategy(settId) want = badger.getStrategyWant(settId) deployer = badger.deployer randomUser = accounts[6] startingBalance = want.balanceOf(deployer) depositAmount = Wei("1 ether") assert startingBalance >= depositAmount # Deposit want.approve(sett, MaxUint256, {"from": deployer}) sett.deposit(depositAmount, {"from": deployer}) after = sett_snapshot(sett, strategy, deployer) chain.sleep(15) chain.mine() sett.earn({"from": deployer}) chain.snapshot() # Test no harvests chain.sleep(days(2)) chain.mine() before = {"settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf()} with brownie.reverts(): controller.withdrawAll(strategy.want(), {"from": randomUser}) controller.withdrawAll(strategy.want(), {"from": deployer}) after = {"settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf()} assert after["settWant"] > before["settWant"] assert after["stratWant"] < before["stratWant"] assert after["stratWant"] == 0 # Test tend only if strategy.isTendable(): chain.revert() chain.sleep(days(2)) chain.mine() strategy.tend({"from": deployer}) before = {"settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf()} with brownie.reverts(): controller.withdrawAll(strategy.want(), {"from": randomUser}) controller.withdrawAll(strategy.want(), {"from": deployer}) after = {"settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf()} assert after["settWant"] > before["settWant"] assert after["stratWant"] < before["stratWant"] assert after["stratWant"] == 0 # Test harvest, with tend if tendable chain.revert() chain.sleep(days(1)) chain.mine() if strategy.isTendable(): strategy.tend({"from": deployer}) chain.sleep(days(1)) chain.mine() before = { "settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf(), "rewardsWant": want.balanceOf(controller.rewards()), } with brownie.reverts(): controller.withdrawAll(strategy.want(), {"from": randomUser}) controller.withdrawAll(strategy.want(), {"from": deployer}) after = {"settWant": want.balanceOf(sett), "stratWant": strategy.balanceOf()} assert after["settWant"] > before["settWant"] assert after["stratWant"] < before["stratWant"] assert after["stratWant"] == 0
def assert_single_user_harvest_flow(settConfig): badger = badger_single_sett(settConfig) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) settKeeper = accounts.at(sett.keeper(), force=True) strategyKeeper = accounts.at(strategy.keeper(), force=True) snap = SnapshotManager(badger, settConfig["id"]) deployer = badger.deployer randomUser = accounts[6] tendable = strategy.isTendable() startingBalance = want.balanceOf(deployer) depositAmount = startingBalance // 2 assert startingBalance >= depositAmount assert startingBalance >= 0 # Deposit want.approve(sett, MaxUint256, {"from": deployer}) snap.settDeposit(depositAmount, {"from": deployer}) assert want.balanceOf(sett) > 0 print("want.balanceOf(sett)", want.balanceOf(sett)) # Earn snap.settEarn({"from": settKeeper}) if tendable: with brownie.reverts("onlyAuthorizedActors"): strategy.tend({"from": randomUser}) snap.settTend({"from": strategyKeeper}) chain.sleep(days(0.5)) chain.mine() if tendable: snap.settTend({"from": strategyKeeper}) chain.sleep(days(1)) chain.mine() with brownie.reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) snap.settHarvest({"from": strategyKeeper}) chain.sleep(days(1)) chain.mine() if tendable: snap.settTend({"from": strategyKeeper}) snap.settWithdraw(depositAmount // 2, {"from": deployer}) chain.sleep(days(3)) chain.mine() snap.settHarvest({"from": strategyKeeper}) snap.settWithdraw(depositAmount // 2 - 1, {"from": deployer})
def assert_single_user_harvest_flow_remove_fees(settConfig): suiteName = "assert_single_user_harvest_flow_remove_fees" + ": " + settConfig[ "id"] badger = badger_single_sett(settConfig) controller = badger.getController(settConfig["id"]) sett = badger.getSett(settConfig["id"]) strategy = badger.getStrategy(settConfig["id"]) want = badger.getStrategyWant(settConfig["id"]) deployer = badger.deployer randomUser = accounts[6] snap = SnapshotManager(badger, settConfig["id"]) tendable = strategy.isTendable() startingBalance = want.balanceOf(deployer) strategyKeeper = accounts.at(strategy.keeper(), force=True) depositAmount = Wei("1 ether") assert startingBalance >= depositAmount # Deposit want.approve(sett, MaxUint256, {"from": deployer}) sett.deposit(depositAmount, {"from": deployer}) # Earn sett.earn({"from": strategyKeeper}) chain.sleep(days(0.5)) chain.mine() if tendable: tx = snap.settTend({"from": strategyKeeper}) chain.sleep(days(1)) chain.mine() with brownie.reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) snap.settHarvest({"from": strategyKeeper}) # Harvesting on the HarvestMetaFarm does not increase the underlying position, it sends rewards to the rewardsTree # For HarvestMetaFarm, we expect FARM rewards to be distributed to rewardsTree assert want.balanceOf(controller.rewards()) > 0 chain.sleep(days(1)) chain.mine() if tendable: tx = strategy.tend({"from": strategyKeeper}) chain.sleep(days(3)) chain.mine() tx = snap.settHarvest({"from": strategyKeeper}) sett.withdrawAll({"from": deployer}) endingBalance = want.balanceOf(deployer) report = { "time": "4 days", "gains": endingBalance - startingBalance, "gainsPercentage": (endingBalance - startingBalance) / startingBalance, } print(report)