def single_user_harvest_flow(badger: BadgerSystem, settConfig, user): strategy = badger.getStrategy(settConfig["id"]) want = interface.IERC20(registry.pancake.chefPairs.bnbBtcb) snap = SnapshotManager(badger, settConfig["id"]) sett = badger.getSett(settConfig["id"]) settKeeper = accounts.at(sett.keeper(), force=True) strategyKeeper = accounts.at(strategy.keeper(), force=True) randomUser = accounts[6] tendable = strategy.isTendable() startingBalance = want.balanceOf(user) depositAmount = startingBalance // 200 assert startingBalance >= depositAmount assert startingBalance >= 0 # Deposit want.approve(sett, MaxUint256, {"from": user}) snap.settDeposit(depositAmount, {"from": user}) 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}) numTends = 48 timeBetweenTends = days(365) // numTends console.print({"numTends": numTends, "timeBetweenTends": timeBetweenTends}) for i in range(0, numTends): console.print("Tend {}".format(i)) snap.settTend({"from": strategyKeeper}) chain.sleep(timeBetweenTends) chain.mine() with brownie.reverts("onlyAuthorizedActors"): strategy.harvest({"from": randomUser}) snap.settHarvest({"from": strategyKeeper})
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 deposit_withdraw_single_user_flow(badger, sett_id, user): controller = badger.getController("native") strategy = badger.getStrategy(sett_id) want = interface.IERC20(strategy.want()) snap = SnapshotManager(badger, sett_id) sett = badger.getSett(sett_id) strategist = badger.deployer settKeeper = accounts.at(sett.keeper(), force=True) randomUser = accounts[6] # Deposit assert want.balanceOf(user) > 0 depositAmount = int(want.balanceOf(user) * 0.8) assert depositAmount > 0 want.approve(sett, MaxUint256, {"from": user}) # sett.deposit(depositAmount, {"from": deployer}) snap.settDeposit(depositAmount, {"from": user}) # Earn # with brownie.reverts("onlyAuthorizedActors"): # sett.earn({"from": randomUser}) min = sett.min() max = sett.max() remain = max - min # sett.earn({"from": settKeeper}) snap.settEarn({"from": settKeeper}) chain.sleep(15) chain.mine(1) snap.settWithdraw(depositAmount // 2, {"from": user}) chain.sleep(10000) chain.mine(1) snap.settWithdraw(depositAmount // 2 - 1, {"from": user})
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 deposit_withdraw_single_user_flow(badger, settConfig, user): strategy = badger.getStrategy(settConfig["id"]) want = interface.IERC20(registry.pancake.chefPairs.bnbBtcb) snap = SnapshotManager(badger, settConfig["id"]) sett = badger.getSett(settConfig["id"]) settKeeper = accounts.at(sett.keeper(), force=True) # Deposit assert want.balanceOf(user) > 0 depositAmount = int(want.balanceOf(user) * 0.8) assert depositAmount > 0 want.approve(sett, MaxUint256, {"from": user}) snap.settDeposit(depositAmount, {"from": user}) # Earn # with brownie.reverts("onlyAuthorizedActors"): # sett.earn({"from": randomUser}) min = sett.min() max = sett.max() remain = max - min # sett.earn({"from": settKeeper}) snap.settEarn({"from": settKeeper}) chain.sleep(15) chain.mine(1) snap.settWithdraw(depositAmount // 2, {"from": user}) chain.sleep(10000) chain.mine(1) snap.settWithdrawAll({"from": user})
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 single_user_harvest_flow(badger: BadgerSystem, sett_id, user): controller = badger.getController("native") strategy = badger.getStrategy(sett_id) want = interface.IERC20(strategy.want()) snap = SnapshotManager(badger, sett_id) sett = badger.getSett(sett_id) strategist = accounts.at(strategy.strategist(), force=True) console.log({ "key": sett_id, "want": strategy.want(), "token0": strategy.token0(), "token1": strategy.token1(), "path0": strategy.getTokenSwapPath(registry.pancake.cake, strategy.token0()), "path1": strategy.getTokenSwapPath(registry.pancake.cake, strategy.token1()), }) strategist = badger.deployer settKeeper = accounts.at(sett.keeper(), force=True) strategyKeeper = accounts.at(strategy.keeper(), force=True) randomUser = accounts[6] tendable = strategy.isTendable() startingBalance = want.balanceOf(user) depositAmount = startingBalance // 200 assert startingBalance >= depositAmount assert startingBalance >= 0 # Deposit want.approve(sett, MaxUint256, {"from": user}) snap.settDeposit(depositAmount, {"from": user}) assert want.balanceOf(sett) > 0 print("want.balanceOf(sett)", want.balanceOf(sett)) # Earn snap.settEarn({"from": settKeeper}) numTends = 48 timeBetweenTends = days(0.5) // numTends console.print({"numTends": numTends, "timeBetweenTends": timeBetweenTends}) for i in range(0, numTends): console.print("Tend {}".format(i)) snap.settTend({"from": strategyKeeper}) chain.sleep(timeBetweenTends) chain.mine() snap.settHarvest({"from": strategyKeeper}) # if tendable: # snap.settTend({"from": strategyKeeper}) # snap.settWithdraw(depositAmount // 2, {"from": user}) # chain.sleep(days(3)) # chain.mine() # snap.settHarvest({"from": strategyKeeper}) # snap.settWithdraw(depositAmount // 2 - 1, {"from": user}) assert False
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_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 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