def test_strat_graceful_exit_generic(strategy, web3, chain, vault, currency, whale, strategist, gov): deposit_limit = Wei("1000000 ether") vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": gov}) amount0 = Wei("500 ether") deposit(amount0, whale, currency, vault) amount1 = Wei("50 ether") deposit(amount1, gov, currency, vault) strategy.harvest({"from": gov}) sleep(chain, 30) vault.revokeStrategy(strategy, {"from": gov}) # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) ## emergency shutdown strategy.harvest({"from": gov}) strategy.harvest({"from": gov}) assert currency.balanceOf(vault) >= amount0 + amount1
def test_profit_is_registered(web3, chain, comp, vault, largerunningstrategy, whale, gov, dai): stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) #1m deposit amount = Wei('1000000 ether') deposit(amount, whale, dai, vault) harvest(largerunningstrategy, gov) #all money in vault assert largerunningstrategy.estimatedTotalAssets() > amount * 0.99 stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) sample = 500 wait(sample, chain) harvest(largerunningstrategy, gov) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) debt = vault.strategies(largerunningstrategy)[5] returns = vault.strategies(largerunningstrategy)[6] assert returns > 0 blocks_per_year = 2_300_000 apr = returns / debt * (blocks_per_year / sample) print(f'implied apr: {apr:.8%}') assert apr > 0
def test_harvest_trigger(web3, chain, comp, vault, largerunningstrategy, whale, gov, dai): largerunningstrategy.setMinCompToSell(Wei('0.01 ether'), {"from": gov}) assert largerunningstrategy.harvestTrigger(Wei('1 ether')) == False #sleep a day chain.sleep(86401) chain.mine(1) assert largerunningstrategy.harvestTrigger(Wei('1 ether')) == True largerunningstrategy.harvest({"from": gov}) assert largerunningstrategy.harvestTrigger(Wei('0.0002 ether')) == False deposit(Wei('100 ether'), whale, dai, vault) assert largerunningstrategy.harvestTrigger(Wei('0.0002 ether')) == True assert largerunningstrategy.harvestTrigger(Wei('0.006 ether')) == False largerunningstrategy.harvest({"from": gov}) times = 0 while largerunningstrategy.harvestTrigger(Wei('0.0002 ether')) == False: wait(50, chain) print(largerunningstrategy.predictCompAccrued().to('ether'), ' comp prediction') times = times + 1 assert times < 10 assert times > 3 largerunningstrategy.harvest({"from": gov})
def test_emergency_exit(web3, strategy, vault, whale, chain, dai, cdai, gov, comp): amount1 = Wei('5000 ether') deposit(amount1, whale, dai, vault) amount1 = Wei('500 ether') deposit(amount1, gov, dai, vault) strategy.harvest({'from': gov}) wait(30, chain) assert vault.emergencyShutdown() == False vault.setEmergencyShutdown(True, {"from": gov}) assert vault.emergencyShutdown() stateOfStrat(strategy, dai, comp) stateOfVault(vault, strategy) strategy.harvest({'from': gov}) print('\n Emergency shut down + harvest done') stateOfStrat(strategy, dai, comp) stateOfVault(vault, strategy) print('\n Withdraw All') vault.withdraw(vault.balanceOf(gov), {'from': gov}) stateOfStrat(strategy, dai, comp) stateOfVault(vault, strategy)
def test_good_migration(strategy, chain, Strategy, web3, vault, currency, whale, rando, gov, strategist): # Call this once to seed the strategy with debt vault.addStrategy(strategy, 2**256 - 1, 2**256 - 1, 50, {"from": gov}) amount1 = Wei("50_000 ether") deposit(amount1, whale, currency, vault) amount1 = Wei("500 ether") deposit(amount1, gov, currency, vault) strategy.harvest({"from": gov}) sleep(chain, 10) strategy.harvest({"from": gov}) strategy_debt = vault.strategies(strategy)[4] # totalDebt prior_position = strategy.estimatedTotalAssets() assert strategy_debt > 0 new_strategy = strategist.deploy(Strategy, vault, "", strategy.cToken()) assert vault.strategies(new_strategy)[4] == 0 assert currency.balanceOf(new_strategy) == 0 # Only Governance can migrate with brownie.reverts(): vault.migrateStrategy(strategy, new_strategy, {"from": rando}) vault.migrateStrategy(strategy, new_strategy, {"from": gov}) assert vault.strategies(strategy)[4] == 0 assert vault.strategies(new_strategy)[4] == strategy_debt assert (new_strategy.estimatedTotalAssets() > prior_position * 0.999 or new_strategy.estimatedTotalAssets() < prior_position * 1.001)
def test_getting_too_close_to_liq(web3, chain, comp, vault, largerunningstrategy, whale, gov, dai): stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) largerunningstrategy.setCollateralTarget(Wei('0.74999999999 ether'), {"from": gov}) deposit(Wei('1000 ether'), whale, dai, vault) balanceBefore = vault.totalAssets() collat = 0 while collat < largerunningstrategy.collateralTarget() / 1.001e18: largerunningstrategy.harvest({'from': gov}) deposits, borrows = largerunningstrategy.getCurrentPosition() collat = borrows / deposits stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) assertCollateralRatio(largerunningstrategy) print(largerunningstrategy.getblocksUntilLiquidation()) assert largerunningstrategy.tendTrigger(1e18) == True largerunningstrategy.tend({'from': gov}) assertCollateralRatio(largerunningstrategy) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) largerunningstrategy.setCollateralTarget(Wei('0.73 ether'), {"from": gov}) assert largerunningstrategy.tendTrigger(1e18) == False largerunningstrategy.tend({'from': gov}) assertCollateralRatio(largerunningstrategy) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy)
def test_apr_generic(strategy, web3, chain, vault, currency, whale, strategist, gov): deposit_limit = Wei("1000000 ether") vault.setDepositLimit(deposit_limit, {"from": gov}) vault.addStrategy(strategy, 10_000, 0, 2 ** 256 - 1, 50, {"from": gov}) deposit_amount = Wei("1000 ether") deposit(deposit_amount, whale, currency, vault) # invest strategy.harvest({"from": gov}) startingBalance = vault.totalAssets() for i in range(2): waitBlock = 25 print(f"\n----wait {waitBlock} blocks----") sleep(chain, waitBlock) strategy.harvest({"from": strategist}) profit = (vault.totalAssets() - startingBalance).to("ether") strState = vault.strategies(strategy).dict() totalReturns = strState["totalGain"] totaleth = totalReturns.to("ether") difff = profit - totaleth blocks_per_year = 2_300_000 assert startingBalance != 0 time = (i + 1) * waitBlock assert time != 0 apr = (totalReturns / startingBalance) * (blocks_per_year / time) print(apr) print(f"implied apr: {apr:.8%}") vault.withdraw(vault.balanceOf(whale), {"from": whale})
def test_strat_emergency_exit_generic(strategy, web3, chain, interface, vault, currency, whale, strategist, gov): deposit_limit = Wei("1000000 ether") vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": gov}) amount0 = Wei("500 ether") deposit(amount0, whale, currency, vault) amount1 = Wei("50 ether") deposit(amount1, gov, currency, vault) strategy.harvest({"from": gov}) sleep(chain, 31) assert strategy.emergencyExit() == False strategy.setEmergencyExit({"from": gov}) assert strategy.emergencyExit() # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) ## emergency shutdown strategy.harvest({"from": gov}) assert currency.balanceOf(vault) >= amount0 + amount1 # Emergency shut down + harvest done # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) # Withdraw All vault.withdraw(vault.balanceOf(gov), {"from": gov})
def test_vault_emergency_exit_generic( strategy, web3, chain, vault, currency, whale, strategist, gov ): deposit_limit = Wei("1000000 ether") vault.setDepositLimit(deposit_limit, {"from": gov}) vault.addStrategy(strategy, 10_000, 0, 2 ** 256 - 1, 50, {"from": gov}) amount0 = Wei("500 ether") deposit(amount0, whale, currency, vault) amount1 = Wei("50 ether") deposit(amount1, gov, currency, vault) strategy.harvest({"from": gov}) sleep(chain, 30) assert vault.emergencyShutdown() == False vault.setEmergencyShutdown(True, {"from": gov}) assert vault.emergencyShutdown() ## emergency shutdown strategy.harvest({"from": gov}) strategy.harvest({"from": gov}) assert currency.balanceOf(vault) > amount0 + amount1 assert strategy.estimatedTotalAssets() < Wei("0.01 ether") # Restore power vault.setEmergencyShutdown(False, {"from": gov}) strategy.harvest({"from": gov}) assert strategy.estimatedTotalAssets() > amount0 + amount1 assert currency.balanceOf(vault) == 0 # Withdraw All vault.withdraw(vault.balanceOf(gov), {"from": gov})
def test_strat_emergency_exit_generic( strategy, web3, chain, vault, currency, whale, strategist, gov ): deposit_limit = Wei("1000000 ether") vault.setDepositLimit(deposit_limit, {"from": gov}) vault.addStrategy(strategy, 10_000, 0, 2 ** 256 - 1, 50, {"from": gov}) amount0 = Wei("500 ether") deposit(amount0, whale, currency, vault) amount1 = Wei("50 ether") deposit(amount1, gov, currency, vault) strategy.harvest({"from": gov}) sleep(chain, 30) assert strategy.emergencyExit() == False strategy.setEmergencyExit({"from": gov}) assert strategy.emergencyExit() ## emergency shutdown strategy.harvest({"from": gov}) assert currency.balanceOf(vault) >= amount0 + amount1 # Withdraw All vault.withdraw(vault.balanceOf(gov), {"from": gov})
def test_unknown_2(web3,StrategyUniswapPairPickle, strategy_generic, Vault, interface, chain,uni_wethwbtc, whaleU, accounts): starting_balance = uni_wethwbtc.balanceOf(whaleU) pjar = interface.PickleJar('0xc80090aa05374d336875907372ee4ee636cbc562') pboss = accounts.at('0xf00d98806a785bb0e1854a0ccc8a39c9c4f4316a', force=True) #deploy vault vault = whaleU.deploy( Vault, uni_wethwbtc, whaleU, whaleU, "ss", "sss" ) strategy = whaleU.deploy(StrategyUniswapPairPickle, vault, pjar, 15) deposit_limit = Wei('10 ether') vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": whaleU}) deposit(Wei('0.0001 ether'), whaleU, uni_wethwbtc, vault) strategy.harvest( {'from': whaleU}) for i in range(20): pjar.earn({'from': pboss}) chain.mine(10) withdraw( 10, whaleU, uni_wethwbtc, vault) strategy.harvest( {'from': whaleU}) strState = vault.strategies(strategy) totalDebt = strState[5] estimateAssets = strategy.estimatedTotalAssets() percentd = totalDebt/estimateAssets print(f'totalDebt/estimateAssets: {percentd:.5%}')
def test_add_keeper(live_vault_dai2, Contract, web3, accounts, chain, cdai, comp, dai, live_strategy_dai2,currency, whale,samdev): strategist = samdev strategy = live_strategy_dai2 vault = live_vault_dai2 #stateOfStrat(strategy, dai, comp) #genericStateOfVault(vault, dai) keeper = Contract.from_explorer("0x13dAda6157Fee283723c0254F43FF1FdADe4EEd6") kp3r = Contract.from_explorer("0x1cEB5cB57C4D4E2b2433641b95Dd330A33185A44") #strategy.setKeeper(keeper, {'from': strategist}) #carlos = accounts.at("0x73f2f3A4fF97B6A6d7afc03C449f0e9a0c0d90aB", force=True) #keeper.addStrategy(strategy, 1700000, 10, {'from': carlos}) bot = accounts.at("0xfe56a0dbdad44Dd14E4d560632Cc842c8A13642b", force=True) assert keeper.harvestable(strategy) == False depositAmount = Wei('3500 ether') deposit(depositAmount, whale, currency, vault) assert keeper.harvestable(strategy) == False depositAmount = Wei('1000 ether') deposit(depositAmount, whale, currency, vault) assert keeper.harvestable(strategy) == True keeper.harvest(strategy, {'from': bot}) balanceBefore = kp3r.balanceOf(bot) #print(tx.events) chain.mine(4) #assert kp3r.balanceOf(bot) > balanceBefore #strategy.harvest({'from': strategist}) assert keeper.harvestable(strategy) == False stateOfStrat(strategy, dai, comp) genericStateOfVault(vault, dai) #stateOfStrat(strategy, dai, comp) #stateOfVault(vault, strategy) #depositAmount = Wei('1000 ether') #deposit(depositAmount, whale, currency, vault) #stateOfStrat(strategy, dai, comp) #genericStateOfVault(vault, dai) #strategy.harvest({'from': strategist}) #stateOfStrat(strategy, dai, comp) #genericStateOfVault(vault, dai)
def test_vault_shares_generic( strategy, web3, chain, vault, currency, whale, strategist, gov ): deposit_limit = Wei("1000 ether") # set limit to the vault vault.setDepositLimit(deposit_limit, {"from": gov}) vault.addStrategy(strategy, 10_000, 0, 2 ** 256 - 1, 0, {"from": gov}) print(currency) assert vault.totalSupply() == 0 amount1 = Wei("50 ether") deposit(amount1, whale, currency, vault) whale_share = vault.balanceOf(whale) deposit(amount1, gov, currency, vault) gov_share = vault.balanceOf(gov) assert gov_share == whale_share assert vault.pricePerShare() == 1e18 assert vault.pricePerShare() * whale_share / 1e18 == amount1 assert vault.pricePerShare() * whale_share / 1e18 == vault.totalAssets() / 2 assert gov_share == whale_share strategy.harvest({"from": gov}) chain.mine(2) chain.sleep(2) strategy.harvest({"from": gov}) # no profit yet whale_share = vault.balanceOf(whale) gov_share = vault.balanceOf(gov) assert gov_share > whale_share sleep(chain, 100) whale_share = vault.balanceOf(whale) gov_share = vault.balanceOf(gov) # no profit just aum fee. meaning total balance should be the same assert (gov_share + whale_share) * vault.pricePerShare() / 1e18 == 100 * 1e18 strategy.harvest({"from": gov}) whale_share = vault.balanceOf(whale) gov_share = vault.balanceOf(gov) # add strategy return assert vault.totalSupply() == whale_share + gov_share value = (gov_share + whale_share) * vault.pricePerShare() / 1e18 assert ( value * 0.99999 < vault.totalAssets() and value * 1.00001 > vault.totalAssets() ) # check we are within 0.1% of expected returns assert ( value < strategy.estimatedTotalAssets() * 1.001 and value > strategy.estimatedTotalAssets() * 0.999 ) assert gov_share > whale_share
def test_vault_shares_generic(strategy_changeable, web3, chain, Vault, currency, whale, strategist): gov = strategist vault = strategist.deploy(Vault, currency, strategist, strategist, "TestVault", "Amount") deposit_limit = Wei('1000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(strategy_changeable, vault) vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": strategist}) amount1 = Wei('50 ether') deposit(amount1, whale, currency, vault) whale_share = vault.balanceOf(whale) deposit(amount1, gov, currency, vault) gov_share = vault.balanceOf(gov) assert gov_share == whale_share assert vault.pricePerShare() == 1e18 assert vault.pricePerShare() * whale_share / 1e18 == amount1 assert vault.pricePerShare() * whale_share / 1e18 == vault.totalAssets( ) / 2 assert gov_share == whale_share strategy.harvest({'from': gov}) #no profit yet whale_share = vault.balanceOf(whale) gov_share = vault.balanceOf(gov) assert gov_share > whale_share wait(100, chain) whale_share = vault.balanceOf(whale) gov_share = vault.balanceOf(gov) # no profit just aum fee. meaning total balance should be the same assert (gov_share + whale_share) * vault.pricePerShare() / 1e18 == 100 * 1e18 strategy.harvest({'from': gov}) whale_share = vault.balanceOf(whale) gov_share = vault.balanceOf(gov) #add strategy return assert vault.totalSupply() == whale_share + gov_share value = (gov_share + whale_share) * vault.pricePerShare() / 1e18 assert value == 100 * 1e18 + vault.strategies(strategy)[6] #check we are within 0.1% of expected returns assert value < strategy.estimatedTotalAssets( ) * 1.001 and value > strategy.estimatedTotalAssets() * 0.999 assert gov_share > whale_share
def test_apr_generic(strategy_changeable, web3, chain, Vault, currency, whale, strategist): vault = strategist.deploy(Vault, currency, strategist, strategist, "TestVault", "Amount") deposit_limit = Wei('1000000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(strategy_changeable, vault) vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": strategist}) deposit_amount = Wei('1000 ether') deposit(deposit_amount, whale, currency, vault) harvest(strategy, strategist, vault) startingBalance = vault.totalAssets() genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) for i in range(10): waitBlock = 25 print(f'\n----wait {waitBlock} blocks----') chain.mine(waitBlock) print(f'\n----harvest----') strategy.harvest({'from': strategist}) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) profit = (vault.totalAssets() - startingBalance).to('ether') strState = vault.strategies(strategy) totalReturns = strState[6] totaleth = totalReturns.to('ether') print(f'Real Profit: {profit:.5f}') difff = profit - totaleth print(f'Diff: {difff}') blocks_per_year = 2_300_000 assert startingBalance != 0 time = (i + 1) * waitBlock assert time != 0 apr = (totalReturns / startingBalance) * (blocks_per_year / time) print(apr) print(f'implied apr: {apr:.8%}') vault.withdraw(vault.balanceOf(whale), {'from': whale})
def normal_operation(chain, ychad, vault, user, whale, strategy, dai): print('\n---starting normal operations----') for i in count(1): waitBlock = random.randint(0,20) print(f'\n----wait {waitBlock} blocks----') chain.mine(waitBlock) action = random.randint(0,3) if action ==0: harvest(strategy, user) elif action == 1: withdraw(random.randint(10,100), strategy,whale, dai, vault) elif action == 2: earn(strategy, vault, user) elif action == 3: deposit(str(f'{random.randint(1,100000)} ether'), whale, dai, vault)
def test_donations(strategy_changeable, web3, chain, Vault, currency, whale, strategist): gov = strategist vault = strategist.deploy(Vault, currency, strategist, strategist, "TestVault", "Amount") deposit_limit = Wei('1000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(strategy_changeable, vault) vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": strategist}) amount = Wei('500 ether') deposit(amount, gov, currency, vault) assert vault.strategies(strategy)[5] == 0 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[6] == 0 donation = Wei('100 ether') #donation to strategy currency.transfer(strategy, donation, {'from': whale}) assert vault.strategies(strategy)[6] == 0 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[6] >= donation assert currency.balanceOf(vault) >= donation strategy.harvest({'from': gov}) assert vault.strategies(strategy)[5] >= donation + amount #donation to vault currency.transfer(vault, donation, {'from': whale}) assert vault.strategies(strategy)[6] >= donation and vault.strategies( strategy)[6] < donation * 2 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[5] >= donation * 2 + amount strategy.harvest({'from': gov}) assert vault.strategies(strategy)[6] >= donation and vault.strategies( strategy)[6] < donation * 2 #check share price is close to expected assert vault.pricePerShare() > ( (donation * 2 + amount) / amount) * 0.95 * 1e18 and vault.pricePerShare() < ( (donation * 2 + amount) / amount) * 1.05 * 1e18
def test_apr_generic(strategy, web3, chain, vault, currency, whale, strategist, gov): deposit_limit = Wei("1000000 ether") vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": gov}) deposit_amount = Wei("1000 ether") deposit(deposit_amount, whale, currency, vault) # invest strategy.harvest({"from": gov}) startingBalance = vault.totalAssets() # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) for i in range(2): waitBlock = 25 print(f"\n----wait {waitBlock} blocks----") sleep(chain, waitBlock) strategy.harvest({"from": strategist}) # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) profit = (vault.totalAssets() - startingBalance).to("ether") strState = vault.strategies(strategy) totalReturns = strState[6] totaleth = totalReturns.to("ether") # print(f'Real Profit: {profit:.5f}') difff = profit - totaleth # print(f'Diff: {difff}') blocks_per_year = 2_300_000 assert startingBalance != 0 time = (i + 1) * waitBlock assert time != 0 apr = (totalReturns / startingBalance) * (blocks_per_year / time) print(apr) print(f"implied apr: {apr:.8%}") vault.withdraw(vault.balanceOf(whale), {"from": whale})
def test_emergency_exit_generic(strategy_changeable, web3, chain, Vault, currency, whale, strategist): gov = strategist vault = strategist.deploy(Vault, currency, strategist, strategist, "TestVault", "Amount") deposit_limit = Wei('1000000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(strategy_changeable, vault) vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": strategist}) amount1 = Wei('500 ether') deposit(amount1, whale, currency, vault) amount1 = Wei('50 ether') deposit(amount1, gov, currency, vault) strategy.harvest({'from': gov}) wait(30, chain) assert vault.emergencyShutdown() == False vault.setEmergencyShutdown(True, {"from": gov}) assert vault.emergencyShutdown() genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) strategy.harvest({'from': gov}) assert currency.balanceOf(strategy) == 0 strategy.harvest({'from': gov}) assert currency.balanceOf(strategy) == strategy.estimatedTotalAssets() print('\n Emergency shut down + harvest done') genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) print('\n Withdraw All') vault.withdraw(vault.balanceOf(gov), {'from': gov}) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency)
def test_good_migration(strategy_changeable, web3, chain, Vault, currency, whale, rando, strategist): # Call this once to seed the strategy with debt gov = strategist vault = strategist.deploy(Vault, currency, strategist, strategist, "TestVault", "Amount") deposit_limit = Wei('1000000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(strategy_changeable, vault) vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": strategist}) amount1 = Wei('500 ether') deposit(amount1, whale, currency, vault) amount1 = Wei('50 ether') deposit(amount1, gov, currency, vault) strategy.harvest({'from': gov}) wait(30, chain) strategy.harvest({'from': gov}) strategy_debt = vault.strategies(strategy)[4] # totalDebt prior_position = strategy.estimatedTotalAssets() assert strategy_debt > 0 new_strategy = strategist.deploy(strategy_changeable, vault) assert vault.strategies(new_strategy)[4] == 0 assert currency.balanceOf(new_strategy) == 0 # Only Governance can migrate with brownie.reverts(): vault.migrateStrategy(strategy, new_strategy, {"from": rando}) vault.migrateStrategy(strategy, new_strategy, {"from": gov}) assert vault.strategies(strategy)[4] == 0 assert vault.strategies(new_strategy)[4] == strategy_debt assert new_strategy.estimatedTotalAssets( ) > prior_position * 0.999 or new_strategy.estimatedTotalAssets( ) < prior_position * 1.001
def test_getting_too_close_to_liq(web3, chain, cdai, comp, vault, largerunningstrategy, whale, gov, dai): stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) largerunningstrategy.setCollateralTarget(Wei('0.7498 ether'), {"from": gov}) deposit(Wei('1000 ether'), whale, dai, vault) balanceBefore = vault.totalAssets() collat = 0 assert largerunningstrategy.tendTrigger(1e18) == False largerunningstrategy.harvest({'from': gov}) deposits, borrows = largerunningstrategy.getCurrentPosition() collat = borrows / deposits print(collat) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) assertCollateralRatio(largerunningstrategy) lastCol = collat while largerunningstrategy.tendTrigger(1e18) == False: cdai.mint(0, {"from": gov}) waitBlock = 100 wait(waitBlock, chain) deposits, borrows = largerunningstrategy.getCurrentPosition() collat = borrows / deposits assert collat > lastCol lastCol = collat print("Collat ratio: ", collat) print("Blocks to liq: ", largerunningstrategy.getblocksUntilLiquidation()) largerunningstrategy.tend({'from': gov}) largerunningstrategy.setCollateralTarget(Wei('0.73 ether'), {"from": gov}) assert largerunningstrategy.tendTrigger(1e18) == False largerunningstrategy.tend({'from': gov}) assertCollateralRatio(largerunningstrategy) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy)
def test_apr_generic(strategy, web3, chain, vault, currency, whale, interface, strategist, gov): dai = currency deposit_limit = Wei('100_000_000 ether') vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": gov}) deposit_amount = Wei('10_000_000 ether') deposit(deposit_amount, whale, currency, vault) #invest strategy.harvest({'from': gov}) strategy.setProfitFactor(1, {"from": strategist}) strategy.setMinCompToSell(1, {"from": gov}) strategy.setMinWant(0, {"from": gov}) startingBalance = vault.totalAssets() genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) for i in range(10): waitBlock = 25 print(f'\n----wait {waitBlock} blocks----') sleep(chain, waitBlock) strategy.harvest({'from': strategist}) profit = (vault.totalAssets() - startingBalance).to('ether') strState = vault.strategies(strategy) totalReturns = strState[6] totaleth = totalReturns.to('ether') print(f'Real Profit: {profit:.5f}') difff = profit - totaleth print(f'Diff: {difff}') blocks_per_year = 2_300_000 assert startingBalance != 0 time = (i + 1) * waitBlock assert time != 0 apr = (totalReturns / startingBalance) * (blocks_per_year / time) print(f'implied apr: {apr:.8%}') vault.withdraw(vault.balanceOf(whale), {'from': whale})
def test_leverage_up_and_down(web3, chain, comp, vault, largerunningstrategy, whale, gov, dai, strategist, isolation): stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) deposit(dai.balanceOf(whale), whale, dai, vault) balanceBefore = vault.totalAssets() collat = 0 while collat < largerunningstrategy.collateralTarget() / 1.001e18: # while collat < largerunningstrategy.collateralTarget() : largerunningstrategy.harvest({'from': gov}) deposits, borrows = largerunningstrategy.getCurrentPosition() collat = borrows / deposits print(collat) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) assertCollateralRatio(largerunningstrategy) harvest(largerunningstrategy, gov, vault) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) largerunningstrategy.setCollateralTarget(Wei('0.1 ether'), {"from": gov}) largerunningstrategy.tend({'from': gov}) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) while collat > largerunningstrategy.collateralTarget() / 1e18: # while collat < largerunningstrategy.collateralTarget() : largerunningstrategy.tend({'from': gov}) deposits, borrows = largerunningstrategy.getCurrentPosition() collat = borrows / deposits print(collat) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy)
def test_weth_del(web3, chain, Vault, StrategyMKRVaultDAIDelegate, live_vault, live_strategy, whale, comp,dai,weth, samdev): #deploy new strat weth_vault = samdev.deploy( Vault, weth, samdev, samdev, "", "" ) strategy = samdev.deploy(StrategyMKRVaultDAIDelegate, weth_vault) weth_vault.addStrategy(strategy, 2 ** 256 - 1, 2 ** 256 - 1, 50, {"from": samdev}) deposit( Wei('100 ether'), whale,weth, weth_vault) #print("\n******* Dai ******") #genericStateOfStrat(live_strategy, dai, live_vault) #genericStateOfVault(live_vault, dai) #print("\n******* Weth ******") #genericStateOfStrat(strategy, weth, weth_vault) #genericStateOfVault(weth_vault, weth) print("\n******* Harvest Weth ******") strategy.harvest({'from': samdev}) print("\n******* Weth ******") genericStateOfStrat(strategy, weth, weth_vault) genericStateOfVault(weth_vault, weth) print("\n******* Dai ******") genericStateOfStrat(live_strategy, dai, live_vault) genericStateOfVault(live_vault, dai) print("\n******* Harvest Dai ******") live_strategy.harvest({'from': samdev}) print("\n******* Weth ******") genericStateOfStrat(strategy, weth, weth_vault) genericStateOfVault(weth_vault, weth) print("\n******* Dai ******") genericStateOfStrat(live_strategy, dai, live_vault) genericStateOfVault(live_vault, dai)
def test_vault_emergency_exit_generic(strategy, web3, chain, interface, vault, currency, whale, strategist, gov): deposit_limit = Wei("1000000 ether") vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": gov}) amount0 = Wei("500 ether") deposit(amount0, whale, currency, vault) amount1 = Wei("50 ether") deposit(amount1, gov, currency, vault) strategy.harvest({"from": gov}) sleep(chain, 30) assert vault.emergencyShutdown() == False vault.setEmergencyShutdown(True, {"from": gov}) assert vault.emergencyShutdown() # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) ## emergency shutdown # stateOfStrat(strategy, interface) strategy.harvest({"from": gov}) # stateOfStrat(strategy, interface) strategy.harvest({"from": gov}) assert currency.balanceOf(vault) > amount0 + amount1 assert strategy.estimatedTotalAssets() < Wei("0.01 ether") # Emergency shut down + harvest done # genericStateOfStrat(strategy, currency, vault) # genericStateOfVault(vault, currency) # Restore power vault.setEmergencyShutdown(False, {"from": gov}) strategy.harvest({"from": gov}) assert strategy.estimatedTotalAssets() > amount0 + amount1 assert currency.balanceOf(vault) == 0 # Withdraw All vault.withdraw(vault.balanceOf(gov), {"from": gov})
def test_donations(strategy, web3, chain, vault, currency, whale, strategist, gov): deposit_limit = Wei('1_000_000 ether') vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": gov}) amount = Wei('500_000 ether') deposit(amount, whale, currency, vault) assert vault.strategies(strategy)[5] == 0 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[6] == 0 donation = Wei('1000 ether') #donation to strategy currency.transfer(strategy, donation, {'from': whale}) assert vault.strategies(strategy)[6] == 0 sleep(chain, 10) strategy.harvest({'from': gov}) assert vault.strategies(strategy)[6] >= donation * 0.9999 assert currency.balanceOf(vault) >= donation * 0.99999 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[5] >= (donation + amount) * 0.99999 #donation to vault currency.transfer(vault, donation, {'from': whale}) assert vault.strategies( strategy)[6] >= donation * 0.9999 and vault.strategies( strategy)[6] < donation * 2 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[5] >= (donation * 2 + amount) * 0.9999 strategy.harvest({'from': gov}) assert vault.strategies(strategy)[ 6] >= donation * 0.999 and vault.strategies(strategy)[6] < donation * 2 #check share price is close to expected assert vault.pricePerShare() > ( (donation * 2 + amount) / amount) * 0.95 * 1e18 and vault.pricePerShare() < ( (donation * 2 + amount) / amount) * 1.05 * 1e18
def test_donations(strategy, web3, chain, vault, currency, whale, strategist, gov): deposit_limit = Wei("1000 ether") vault.setDepositLimit(deposit_limit, {"from": gov}) vault.addStrategy(strategy, 10_000, 0, 2 ** 256 - 1, 50, {"from": gov}) amount = Wei("50 ether") deposit(amount, gov, currency, vault) assert vault.strategies(strategy).dict()["totalDebt"] == 0 strategy.harvest({"from": gov}) assert vault.strategies(strategy).dict()["totalGain"] == 0 donation = Wei("1 ether") # donation to strategy currency.transfer(strategy, donation, {"from": whale}) assert vault.strategies(strategy).dict()["totalGain"] == 0 strategy.harvest({"from": gov}) assert vault.strategies(strategy).dict()["totalGain"] >= donation assert currency.balanceOf(vault) >= donation strategy.harvest({"from": gov}) assert vault.strategies(strategy).dict()["totalDebt"] >= donation + amount # donation to vault currency.transfer(vault, donation, {"from": whale}) assert ( vault.strategies(strategy).dict()["totalGain"] >= donation and vault.strategies(strategy).dict()["totalGain"] < donation * 2 ) strategy.harvest({"from": gov}) assert vault.strategies(strategy).dict()["totalDebt"] >= donation * 2 + amount strategy.harvest({"from": gov}) assert ( vault.strategies(strategy).dict()["totalGain"] >= donation and vault.strategies(strategy).dict()["totalGain"] < donation * 2 ) # check share price is close to expected assert ( vault.pricePerShare() > ((donation * 2 + amount) / amount) * 0.95 * 1e18 and vault.pricePerShare() < ((donation * 2 + amount) / amount) * 1.05 * 1e18 )
def test_withdraw_all(web3, chain, comp, vault, largerunningstrategy, whale, gov, dai, strategist, isolation): balance_before = dai.balanceOf(strategist) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) amount = Wei('10000 ether') deposit(amount, strategist, dai, vault) harvest(largerunningstrategy, gov, vault) wait(50, chain) harvest(largerunningstrategy, gov, vault) stateOfStrat(largerunningstrategy, dai, comp) stateOfVault(vault, largerunningstrategy) withdraw(1, strategist, dai, vault) profitW = dai.balanceOf(strategist) - balance_before profit = profitW.to('ether') print(f'profit: {profit:.5f}')
def test_full_live(web3, chain, comp, Vault, YearnDaiCompStratV2, dai, whale, strategist, cdai): #our humble strategist is going to publish both the vault and the strategy # this is to mint small amount of dai reguarly to replicate people interacting with cdai contract dai.approve(cdai, 2**256 - 1, {"from": whale}) starting_balance = dai.balanceOf(strategist) #deploy vault vault = strategist.deploy(Vault, dai, strategist, strategist, "Ytest DAI Vault V2", "ytDAI2") #100k dai limit to begin with deposit_limit = Wei('1000000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(YearnDaiCompStratV2, vault) strategy.setGasFactor(1, {"from": strategist}) assert (strategy.gasFactor() == 1) strategy.setMinCompToSell(Wei('0.1 ether'), {"from": strategist}) assert (strategy.minCompToSell() == Wei('0.1 ether')) #Current comp/eth rate compEthRate = strategy.getCompValInWei(Wei('1 ether')) print('Current comp/eth rate:', compEthRate) #enable the strategy rate_limit = deposit_limit vault.addStrategy(strategy, rate_limit, rate_limit, 50, {"from": strategist}) #our humble strategist deposits some test funds deposit(Wei('1000 ether'), strategist, dai, vault) stateOfStrat(strategy, dai, comp) stateOfVault(vault, strategy) assert strategy.estimatedTotalAssets() == 0 harvest(strategy, strategist, vault) #whale deposits as well deposit(Wei('1000 ether'), whale, dai, vault) for i in range(15): #assertCollateralRatio(strategy) waitBlock = random.randint(10, 50) print(f'\n----wait {waitBlock} blocks----') chain.mine(waitBlock) #if harvest condition harvest. if tend tend print(strategy.predictCompAccrued().to('ether'), ' comp prediction') print(comp.balanceOf(strategy).to('ether'), ' comp in balance') harvest(strategy, strategist, vault) tend(strategy, strategist) something = True action = random.randint(0, 9) if action == 1: withdraw(random.randint(50, 100), whale, dai, vault) elif action == 2: withdraw(random.randint(50, 100), whale, dai, vault) elif action == 3: deposit(Wei(str(f'{random.randint(10000,100000)} ether')), whale, dai, vault) elif action > 3 and action < 6: cdai.mint(1, {"from": whale}) else: something = False if something: stateOfStrat(strategy, dai, comp) stateOfVault(vault, strategy) #strategist withdraws withdraw(1, strategist, dai, vault) stateOfStrat(strategy, dai, comp) stateOfVault(vault, strategy) profit = dai.balanceOf(strategist) - starting_balance print(Wei(profit).to('ether'), ' profit') print(vault.strategies(strategy)[6], ' total returns of strat')
def test_full_generic(strategy_changeable, web3, chain, Vault, currency, whale, strategist): #our humble strategist is going to publish both the vault and the strategy starting_balance = currency.balanceOf(strategist) #deploy vault vault = strategist.deploy(Vault, currency, strategist, strategist, "TestVault", "Amount") deposit_limit = Wei('1000000 ether') #set limit to the vault vault.setDepositLimit(deposit_limit, {"from": strategist}) #deploy strategy strategy = strategist.deploy(strategy_changeable, vault) vault.addStrategy(strategy, deposit_limit, deposit_limit, 50, {"from": strategist}) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) #our humble strategist deposits some test funds depositAmount = Wei('501 ether') deposit(depositAmount, strategist, currency, vault) #print(vault.creditAvailable(strategy)) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) assert strategy.estimatedTotalAssets() == 0 assert strategy.harvestTrigger(1) == True harvest(strategy, strategist, vault) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) assert strategy.estimatedTotalAssets( ) >= depositAmount * 0.999999 #losing some dust is ok assert strategy.harvestTrigger(1) == False #whale deposits as well whale_deposit = Wei('1000 ether') deposit(whale_deposit, whale, currency, vault) assert strategy.harvestTrigger(1000000 * 30 * 1e9) == True harvest(strategy, strategist, vault) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) for i in range(15): waitBlock = random.randint(10, 50) print(f'\n----wait {waitBlock} blocks----') chain.mine(waitBlock) #if harvest condition harvest. if tend tend harvest(strategy, strategist, vault) tend(strategy, strategist) something = True action = random.randint(0, 9) if action == 1: withdraw(random.randint(50, 100), whale, currency, vault) elif action == 2: withdraw(random.randint(50, 100), whale, currency, vault) elif action == 3: deposit(Wei(str(f'{random.randint(10,100)} ether')), whale, currency, vault) else: something = False if something: genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) #strategist withdraws withdraw(1, strategist, currency, vault) genericStateOfStrat(strategy, currency, vault) genericStateOfVault(vault, currency) profit = currency.balanceOf(strategist) - starting_balance print(Wei(profit).to('ether'), ' profit') print(vault.strategies(strategy)[6].to('ether'), ' total returns of strat')