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 harvest_all(badger: BadgerSystem, skip): for key, vault in badger.sett_system.vaults.items(): if key in skip: continue console.print("\n[bold yellow]===== Harvest: " + str(key) + " =====[/bold yellow]\n") print("Harvest: " + key) snap = SnapshotManager(badger, key) strategy = badger.getStrategy(key) keeper = accounts.at(badger.keeper) before = snap.snap() if strategy.keeper() == badger.badgerRewardsManager: snap.settHarvestViaManager( strategy, { "from": keeper, "gas_limit": 2000000, "allow_revert": True }, confirm=False, ) else: snap.settHarvest( { "from": keeper, "gas_limit": 2000000, "allow_revert": True }, confirm=False, ) tx_wait() if rpc.is_active(): chain.mine() after = snap.snap() snap.printCompare(before, after)
def harvest_all(badger: BadgerSystem, skip): keeper = badger.deployer for key, vault in badger.sett_system.vaults.items(): if key in skip: continue console.print("\n[bold yellow]===== Harvest: " + str(key) + " =====[/bold yellow]\n") print("Harvest: " + key) snap = SnapshotManager(badger, key) strategy = badger.getStrategy(key) keeper = accounts.at(strategy.keeper()) before = snap.snap() snap.settHarvest({'from': keeper, "gas_price": gas_strategy}) after = snap.snap() snap.printCompare(before, after)
def harvest_all(badger: BadgerSystem, skip): for key, vault in badger.sett_system.vaults.items(): if key in skip: continue console.print("\n[bold yellow]===== Harvest: " + str(key) + " =====[/bold yellow]\n") print("Harvest: " + key) snap = SnapshotManager(badger, key) strategy = badger.getStrategy(key) keeper = accounts.at(strategy.keeper()) before = snap.snap() snap.printTable(before) snap.settHarvest({'from': keeper, "gas_price": gas_strategy, "gas_limit": 2000000, "allow_revert": True}, confirm=False) after = snap.snap() snap.printTable(after) snap.printCompare(before, after)
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_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)
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 harvest_all(badger: BadgerSystem, skip, min_profit=0): """ Runs harvest function for strategies if they are expected to be profitable. If a profit estimate fails for any reason the default behavior is to treat it as having a profit of zero. :param badger: badger system :param skip: strategies to skip checking :param min_profit: minimum estimated profit (in ETH or BNB) required for harvest to be executed on chain """ for key, vault in badger.sett_system.vaults.items(): if key in skip: continue console.print("\n[bold yellow]===== Harvest: " + str(key) + " =====[/bold yellow]\n") print("Harvest: " + key) snap = SnapshotManager(badger, key) strategy = badger.getStrategy(key) before = snap.snap() if strategy.keeper() == badger.badgerRewardsManager: keeper = accounts.at(strategy.keeper()) estimated_profit = snap.estimateProfitHarvestViaManager( key, strategy, { "from": keeper, "gas_limit": 2000000, "allow_revert": True }, min_profit, ) if estimated_profit >= min_profit: snap.settHarvestViaManager( strategy, { "from": keeper, "gas_limit": 2000000, "allow_revert": True }, confirm=False, ) else: estimated_profit = snap.estimateProfitHarvest( key, { "from": keeper, "gas_limit": 2000000, "allow_revert": True }, min_profit, ) if estimated_profit >= min_profit: snap.settHarvest( { "from": keeper, "gas_limit": 2000000, "allow_revert": True }, confirm=False, ) tx_wait() if rpc.is_active(): chain.mine() after = snap.snap() snap.printCompare(before, after)