Skip to content

Commit

Permalink
test: Fix up hook tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Nov 21, 2024
1 parent cc0ddc2 commit e1ae02c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 26 deletions.
12 changes: 6 additions & 6 deletions python/test/hooks/after_add_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def on_after_add_liquidity(self, kind, amounts_in_scaled18, amounts_in_raw, bpt_
if not (isinstance(hook_state, dict) and hook_state is not None and 'expected_balances_live_scaled18' in hook_state):
raise ValueError('Unexpected hookState')
assert kind == add_liquidity_input['kind']
assert bpt_amount_out == 146464294351915965
assert bpt_amount_out == 146464294351867896
assert amounts_in_scaled18 == add_liquidity_input['max_amounts_in_raw']
assert amounts_in_raw == add_liquidity_input['max_amounts_in_raw']
assert balances_scaled18 == hook_state['expected_balances_live_scaled18']
Expand Down Expand Up @@ -90,7 +90,7 @@ def on_compute_dynamic_swap_fee(self):
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [1000000000000000000, 1000000000000000000],
Expand Down Expand Up @@ -123,15 +123,15 @@ def test_hook_after_add_liquidity_no_fee():
200000000000000001,
100000000000000001,
]
assert test["bpt_amount_out_raw"] == 146464294351915965
assert test["bpt_amount_out_raw"] == 146464294351867896

def test_hook_after_add_liquidity_with_fee():
# aggregateSwapFee of 50% should take half of remaining
# hook state is used to pass expected value to tests
# aggregate fee amount is 2554373534619714n which is deducted from amount in
# aggregate fee amount is 2554373534622012 which is deducted from amount in
input_hook_state = {
"expected_balances_live_scaled18": [
1197445626465380286,
1200000000000000000 - 2554373534622012,
1100000000000000000,
],
}
Expand All @@ -145,5 +145,5 @@ def test_hook_after_add_liquidity_with_fee():
200000000000000001,
100000000000000001,
]
assert test["bpt_amount_out_raw"] == 146464294351915965
assert test["bpt_amount_out_raw"] == 146464294351867896

2 changes: 1 addition & 1 deletion python/test/hooks/after_remove_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def on_compute_dynamic_swap_fee(self):
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [1000000000000000000, 1000000000000000000],
Expand Down
20 changes: 11 additions & 9 deletions python/test/hooks/after_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [1000000000000000000, 1000000000000000000],
Expand All @@ -45,12 +45,14 @@


swap_input = {
"amount_raw": 1,
"amount_raw": 1000000000000000000,
"swap_kind": SwapKind.GIVENIN.value,
"token_in": pool['tokens'][0],
"token_out": pool['tokens'][1],
}

expected_calculated = 100000000000

class CustomPool():
def __init__(self, pool_state):
self.pool_state = pool_state
Expand Down Expand Up @@ -109,10 +111,9 @@ def on_after_swap(self, params):
assert params['token_in'] == swap_input['token_in']
assert params['token_out'] == swap_input['token_out']
assert params['amount_in_scaled18'] == swap_input['amount_raw']
assert params['amount_calculated_raw'] == 90000000000
assert params['amount_calculated_scaled18'] == 90000000000
assert params['amount_out_scaled18'] == 90000000000
assert params['token_in_balance_scaled18'] == int(pool['balancesLiveScaled18'][0] + swap_input['amount_raw'])
assert params['amount_calculated_raw'] == expected_calculated
assert params['amount_calculated_scaled18'] == expected_calculated
assert params['amount_out_scaled18'] == expected_calculated
assert [token_in_balanceScaled18, token_out_balance_scaled18] == hook_state['expectedBalancesLiveScaled18']
return {'success': True, 'hook_adjusted_amount_calculated_raw': 1}

Expand All @@ -131,7 +132,7 @@ def test_hook_after_swap_no_fee():
input_hook_state = {
"expectedBalancesLiveScaled18": [
pool['balancesLiveScaled18'][0] + swap_input['amount_raw'],
999999910000000000,
pool['balancesLiveScaled18'][1] - expected_calculated,
],
}
test = vault.swap(
Expand All @@ -146,10 +147,11 @@ def test_hook_after_swap_with_fee():
# aggregateSwapFee of 50% should take half of remaining
# hook state is used to pass expected value to tests
# Aggregate fee amount is 50% of swap fee
expected_aggregate_swap_fee_amount = 50000000000000000
input_hook_state = {
"expectedBalancesLiveScaled18": [
pool['balancesLiveScaled18'][0] + swap_input['amount_raw'],
999999905000000000,
pool['balancesLiveScaled18'][0] + swap_input['amount_raw'] - expected_aggregate_swap_fee_amount,
pool['balancesLiveScaled18'][1] - expected_calculated,
],
}
test = vault.swap(
Expand Down
4 changes: 2 additions & 2 deletions python/test/hooks/before_add_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def on_compute_dynamic_swap_fee(self):
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [2000000000000000000, 2000000000000000000],
Expand Down Expand Up @@ -122,4 +122,4 @@ def test_hook_before_add_liquidity_no_fee():
200000000000000000,
100000000000000000,
]
assert test["bpt_amount_out_raw"] == 146464294351915965
assert test["bpt_amount_out_raw"] == 146464294351867896
2 changes: 1 addition & 1 deletion python/test/hooks/before_remove_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def on_compute_dynamic_swap_fee(self):
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [2000000000000000000, 2000000000000000000],
Expand Down
2 changes: 1 addition & 1 deletion python/test/hooks/before_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [2000000000000000000, 2000000000000000000],
Expand Down
6 changes: 3 additions & 3 deletions python/test/hooks/exit_fee.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 100000000000000000,
"balancesLiveScaled18": [5000000000000000, 5000000000000000000],
Expand All @@ -63,7 +63,7 @@ def test_hook_exit_fee_no_fee():
)
assert test["amounts_out_raw"] == [
316227766016,
316227766016840
316227766016844
]

def test_hook_exit_fee_with_fee():
Expand All @@ -79,5 +79,5 @@ def test_hook_exit_fee_with_fee():
)
assert test["amounts_out_raw"] == [
300416377716,
300416377715998
300416377716002
]
6 changes: 3 additions & 3 deletions python/test/test_custom_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ def test_custom_pool():
"0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
],
"scalingFactors": [1000000000000000000, 1000000000000000000],
"scalingFactors": [1, 1],
"weights": [500000000000000000, 500000000000000000],
"swapFee": 0,
"balancesLiveScaled18": [64604926441576011, 46686842105263157924],
"tokenRates": [1000000000000000000, 1000000000000000000],
"totalSupply": 1736721048412749353,
"randoms": [77, 88],
"randoms": [7000000000000000000, 8000000000000000000],
"aggregateSwapFee": 0,
}
vault = Vault(custom_pool_classes={"CustomPool": CustomPool})
calculated_amount = vault.swap(
{
"amount_raw": 1,
"amount_raw": 1000000000000000000,
"token_in": "0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9",
"token_out": "0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75",
"swap_kind": 0,
Expand Down

0 comments on commit e1ae02c

Please sign in to comment.