예제 #1
0
def test_steth_positive_rebase(vault, vault_user, stranger, liquidations_admin,
                               steth_token, mock_bridge_connector,
                               withdraw_from_terra, lido_oracle_report,
                               steth_adjusted_ammount, helpers):
    amount = 1 * 10**18
    adjusted_amount = steth_adjusted_ammount(amount)

    steth_token.approve(vault, amount, {'from': vault_user})
    vault.submit(amount, TERRA_ADDRESS, '0xab', vault.version(),
                 {'from': vault_user})
    assert mock_bridge_connector.terra_beth_balance_of(TERRA_ADDRESS) == amount

    vault_steth_balance_before = steth_token.balanceOf(vault)
    assert helpers.equal_with_precision(vault_steth_balance_before, amount, 10)

    lido_oracle_report(steth_rebase_mult=1.01)

    vault_steth_balance_after = steth_token.balanceOf(vault)
    assert vault_steth_balance_after > vault_steth_balance_before

    assert helpers.equal_with_precision(vault.get_rate(), 10**18, max_diff=100)

    vault.collect_rewards({'from': liquidations_admin})

    assert helpers.equal_with_precision(vault.get_rate(), 10**18, max_diff=100)

    withdraw_from_terra(TERRA_ADDRESS, to_address=stranger, amount=amount)
    vault.withdraw(amount, vault.version(), {'from': stranger})

    assert helpers.equal_with_precision(steth_token.balanceOf(stranger),
                                        adjusted_amount, 100)
예제 #2
0
def test_steth_rewards_after_penalties(
        vault, vault_user, another_vault_user, stranger, another_stranger,
        liquidations_admin, steth_token, beth_token, withdraw_from_terra,
        lido_oracle_report, mock_bridge_connector, helpers,
        steth_adjusted_ammount):
    amount = 1 * 10**18
    adjusted_amount = steth_adjusted_ammount(amount)
    negative_rebase_multiplier = 0.99
    positive_rebase_multiplier = 1 / negative_rebase_multiplier

    steth_token.approve(vault, amount, {'from': vault_user})
    vault.submit(amount, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    lido_oracle_report(steth_rebase_mult=negative_rebase_multiplier)

    assert helpers.equal_with_precision(
        vault.get_rate(), (1 / negative_rebase_multiplier) * 10**18, 100)

    tx = vault.collect_rewards({'from': liquidations_admin})

    helpers.assert_single_event_named('RewardsCollected',
                                      tx,
                                      source=vault,
                                      evt_keys_dict={
                                          'steth_amount': 0,
                                          'ust_amount': 0
                                      })

    steth_token.approve(vault, amount, {'from': another_vault_user})
    vault.submit(amount, ANOTHER_TERRA_ADDRESS, b'', vault.version(),
                 {'from': another_vault_user})

    lido_oracle_report(steth_rebase_mult=positive_rebase_multiplier)
    chain.mine(timedelta=3600 * 24 + 1)
    vault.collect_rewards({'from': liquidations_admin})

    withdraw_from_terra(
        TERRA_ADDRESS,
        to_address=stranger,
        amount=mock_bridge_connector.terra_beth_balance_of(TERRA_ADDRESS))
    vault.withdraw(beth_token.balanceOf(stranger), vault.version(),
                   {'from': stranger})

    assert helpers.equal_with_precision(
        steth_token.balanceOf(stranger),
        steth_adjusted_ammount(adjusted_amount * negative_rebase_multiplier),
        10**10)

    withdraw_from_terra(ANOTHER_TERRA_ADDRESS,
                        to_address=another_stranger,
                        amount=mock_bridge_connector.terra_beth_balance_of(
                            ANOTHER_TERRA_ADDRESS))
    vault.withdraw(beth_token.balanceOf(another_stranger), vault.version(),
                   {'from': another_stranger})

    assert helpers.equal_with_precision(
        steth_token.balanceOf(another_stranger), adjusted_amount, 10**10)
예제 #3
0
def test_shares_balance_decrease_liquidation(
        vault, vault_user, liquidations_admin, deposit_to_terra, steth_token,
        beth_token, helpers, withdraw_from_terra, lido_oracle_report):
    deposit_to_terra(TERRA_ADDRESS, vault_user, amount=10**18)

    lido_oracle_report(steth_rebase_mult=1)
    vault.collect_rewards({'from': liquidations_admin})

    withdraw_from_terra(TERRA_ADDRESS,
                        to_address=vault_user,
                        amount=0.5 * 10**18)
    vault.withdraw(0.5 * 10**18, vault.version(), {'from': vault_user})

    chain.mine(timedelta=3600 * 28)

    vault_steth_balance_before = steth_token.balanceOf(vault)
    vault_beth_balance_before = beth_token.balanceOf(vault)

    lido_oracle_report(steth_rebase_mult=1.1)
    vault.collect_rewards({'from': liquidations_admin})

    assert beth_token.balanceOf(vault) == vault_beth_balance_before

    assert helpers.equal_with_precision(steth_token.balanceOf(vault),
                                        vault_steth_balance_before, 100)
예제 #4
0
def test_beth_io_possible_after_non_rebasing_lido_oracle_report(
        vault, vault_user, lido_oracle_report, steth_token,
        withdraw_from_terra):
    steth_token.approve(vault, 2 * 10**18, {'from': vault_user})

    assert vault.can_deposit_or_withdraw()
    vault.submit(10**18, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    lido_oracle_report(steth_rebase_mult=1.0)

    assert vault.can_deposit_or_withdraw()
    vault.submit(10**18, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    withdraw_from_terra(TERRA_ADDRESS, to_address=vault_user, amount=10**18)
    vault.withdraw(10**18, vault.version(), {'from': vault_user})
예제 #5
0
def test_beth_io_possible_after_steth_io(lido, vault, vault_user, stranger,
                                         steth_token, withdraw_from_terra,
                                         steth_io_amount):
    steth_token.approve(vault, 2 * 10**18, {'from': vault_user})

    assert vault.can_deposit_or_withdraw()
    vault.submit(10**18, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    lido.submit(ZERO_ADDRESS, {'from': stranger, 'value': steth_io_amount})

    assert vault.can_deposit_or_withdraw()
    vault.submit(10**18, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    withdraw_from_terra(TERRA_ADDRESS, to_address=vault_user, amount=10**18)
    vault.withdraw(10**18, vault.version(), {'from': vault_user})
예제 #6
0
def test_steth_negative_rebase(vault, vault_user, stranger, liquidations_admin,
                               steth_token, mock_bridge_connector,
                               withdraw_from_terra, lido_oracle_report,
                               steth_adjusted_ammount, helpers):
    amount = 1 * 10**18
    adjusted_amount = steth_adjusted_ammount(amount)

    steth_token.approve(vault, amount, {'from': vault_user})
    vault.submit(amount, TERRA_ADDRESS, '0xab', vault.version(),
                 {'from': vault_user})
    assert mock_bridge_connector.terra_beth_balance_of(TERRA_ADDRESS) == amount

    vault_steth_balance_before = steth_token.balanceOf(vault)
    assert helpers.equal_with_precision(vault_steth_balance_before, amount, 10)

    lido_oracle_report(steth_rebase_mult=0.99)

    vault_steth_balance_after = steth_token.balanceOf(vault)
    assert vault_steth_balance_after < vault_steth_balance_before

    expected_withdraw_rate = int((1 / 0.99) * 10**18)

    assert helpers.equal_with_precision(vault.get_rate(),
                                        expected_withdraw_rate, 100)

    withdraw_from_terra(TERRA_ADDRESS, to_address=stranger, amount=amount)

    tx = vault.collect_rewards({'from': liquidations_admin})

    helpers.assert_single_event_named('RewardsCollected',
                                      tx,
                                      source=vault,
                                      evt_keys_dict={
                                          'steth_amount': 0,
                                          'ust_amount': 0
                                      })

    assert helpers.equal_with_precision(vault.get_rate(),
                                        expected_withdraw_rate, 100)

    vault.withdraw(amount, vault.version(), {'from': stranger})

    assert helpers.equal_with_precision(
        steth_token.balanceOf(stranger),
        steth_adjusted_ammount(adjusted_amount * 0.99), 100)
예제 #7
0
def test_rate_changes(vault, lido_oracle_report, vault_user,
                      liquidations_admin, steth_token, helpers):
    assert vault.get_rate() == 10**18

    steth_token.approve(vault, 10**18, {'from': vault_user})
    vault.submit(10**18, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    assert helpers.equal_with_precision(vault.get_rate(),
                                        10**18,
                                        max_diff=1000)

    lido_oracle_report(steth_rebase_mult=1.1)

    assert helpers.equal_with_precision(vault.get_rate(),
                                        10**18,
                                        max_diff=1000)

    vault.collect_rewards({'from': liquidations_admin})

    assert helpers.equal_with_precision(vault.get_rate(),
                                        10**18,
                                        max_diff=1000)

    lido_oracle_report(steth_rebase_mult=0.9)

    assert helpers.equal_with_precision(vault.get_rate(),
                                        int((1 / 0.9) * 10**18),
                                        max_diff=1000)

    lido_oracle_report(steth_rebase_mult=1.01)

    assert helpers.equal_with_precision(vault.get_rate(),
                                        int((1 / 0.909) * 10**18),
                                        max_diff=1000)

    chain.mine(timedelta=3600 * 24 + 1)
    vault.collect_rewards({'from': liquidations_admin})

    assert helpers.equal_with_precision(vault.get_rate(),
                                        int((1 / 0.909) * 10**18),
                                        max_diff=1000)

    lido_oracle_report(steth_rebase_mult=2.0)

    assert helpers.equal_with_precision(vault.get_rate(),
                                        10**18,
                                        max_diff=1000)

    chain.mine(timedelta=3600 * 24 + 1)
    vault.collect_rewards({'from': liquidations_admin})

    assert helpers.equal_with_precision(vault.get_rate(),
                                        int((1 / 0.909) * 10**18),
                                        max_diff=1000)
예제 #8
0
def test_insurance_is_not_counted_as_rewards(
    vault,
    vault_user,
    another_vault_user,
    liquidations_admin,
    steth_token,
    steth_adjusted_ammount,
    lido_oracle_report,
    apply_insurance,
    helpers,
):
    amount = steth_adjusted_ammount(10**18)

    steth_token.approve(vault, amount, {'from': vault_user})
    vault.submit(amount, TERRA_ADDRESS, b'', vault.version(),
                 {'from': vault_user})

    vault_steth_balance_before = steth_token.balanceOf(vault)

    lido_oracle_report(steth_rebase_mult=0.99)
    vault.collect_rewards({'from': liquidations_admin})

    assert helpers.equal_with_precision(vault.get_rate(),
                                        int((1 / 0.99) * 10**18), 100)

    chain.mine(timedelta=24 * 3600 + 1)
    apply_insurance(steth_rebase_mult=1 / 0.99)

    assert vault.get_rate() >= 10**18

    assert helpers.equal_with_precision(vault.get_rate(),
                                        10**18,
                                        max_diff_percent=0.1)

    tx = vault.collect_rewards({'from': liquidations_admin})

    helpers.assert_single_event_named('RewardsCollected',
                                      tx,
                                      source=vault,
                                      evt_keys_dict={
                                          'steth_amount': 0,
                                          'ust_amount': 0
                                      })

    assert helpers.equal_with_precision(vault.get_rate(),
                                        10**18,
                                        max_diff_percent=0.1)
def test_integrates_with_real_vault(
    RewardsLiquidator,
    mock_bridge_connector,
    steth_token,
    deployer,
    admin,
    vault,
    vault_user,
    liquidations_admin,
    lido_oracle_report,
    steth_adjusted_ammount,
    helpers,
):
    liquidator = RewardsLiquidator.deploy(
        vault,
        admin,
        int(MAX_STETH_ETH_PRICE_DIFF_PERCENT * 10 ** 18 / 100),
        int(MAX_ETH_USDC_PRICE_DIFF_PERCENT * 10 ** 18 / 100),
        int(MAX_USDC_UST_PRICE_DIFF_PERCENT * 10 ** 18 / 100),
        int(MAX_STETH_UST_PRICE_DIFF_PERCENT * 10 ** 18 / 100),
        {"from": deployer},
    )

    vault.set_rewards_liquidator(liquidator, {"from": admin})

    amount = 1 * 10 ** 18
    adjusted_amount = steth_adjusted_ammount(amount)

    steth_token.approve(vault, amount, {"from": vault_user})
    vault.submit(amount, TERRA_ADDRESS, b"", vault.version(), {"from": vault_user})
    assert mock_bridge_connector.terra_beth_balance_of(TERRA_ADDRESS) == adjusted_amount

    lido_oracle_report(steth_rebase_mult=1.01)

    tx = vault.collect_rewards({"from": liquidations_admin})

    vault_evt = helpers.assert_single_event_named("RewardsCollected", tx, source=vault)

    assert vault_evt["steth_amount"] > 0
    assert vault_evt["ust_amount"] > 0

    liquidator_evt = helpers.assert_single_event_named("SoldStethToUST", tx, source=liquidator)

    assert abs(liquidator_evt["steth_amount"] - vault_evt["steth_amount"]) < 10
    assert liquidator_evt["ust_amount"] == vault_evt["ust_amount"]