Skip to content

Commit a0acb62

Browse files
committed
joinSwapPoolAmountOut
1 parent a82df36 commit a0acb62

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

model/balancer_pool.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import namedtuple
2-
from decimal import Decimal
2+
from decimal import Decimal, ROUND_HALF_DOWN
33

44
from model.balancer_constants import MIN_FEE, MAX_BOUND_TOKENS, INIT_POOL_SUPPLY, EXIT_FEE, MAX_IN_RATIO, MAX_OUT_RATIO
55
from model.balancer_math import BalancerMath
@@ -294,3 +294,36 @@ def join_swap_extern_amount_in(self, token_in: str, token_amount_in: Decimal, mi
294294
# _pushPoolShare(msg.sender, pool_amount_out);
295295
# _pullUnderlying(token_in, msg.sender, token_amount_in);
296296
return pool_amount_out
297+
298+
299+
300+
def join_swap_pool_amount_out(self, token_in: str, pool_amount_out: Decimal, max_amount_in: Decimal) -> Decimal:
301+
if not self._records[token_in].bound:
302+
raise Exception("ERR_NOT_BOUND")
303+
304+
in_record = self._records[token_in]
305+
306+
token_amount_in = self.calc_single_in_given_pool_out(
307+
token_balance_in = in_record.balance,
308+
token_weight_in = in_record.denorm,
309+
pool_supply = self.pool_token_supply,
310+
total_weight = self.total_weight,
311+
pool_amount_out = pool_amount_out,
312+
swap_fee = self._swap_fee)
313+
314+
if token_amount_in == 0:
315+
raise Exception("ERR_MATH_APPROX")
316+
317+
if token_amount_in > max_amount_in:
318+
raise Exception("ERR_LIMIT_IN")
319+
320+
if token_amount_in > in_record.balance * MAX_IN_RATIO:
321+
raise Exception("ERR_MAX_IN_RATIO")
322+
323+
in_record.balance = in_record.balance + token_amount_in
324+
self._mint_pool_share(pool_amount_out)
325+
# _pushPoolShare(msg.sender, poolAmountOut);
326+
# _pullUnderlying(tokenIn, msg.sender, tokenAmountIn);
327+
328+
return token_amount_in
329+

tests/test_balancer_pool.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,20 @@ def test_join_swap_extern_amount_in(self):
105105
self.assertAlmostEqual(p_ao, Decimal('2.309727440687574969'))
106106
self.assertAlmostEqual(pool.pool_token_supply, Decimal('135.224857816660897462'))
107107

108+
def test_join_join_swap_pool_amount_out(self):
109+
pool = BalancerPool(initial_pool_supply=Decimal('100'))
110+
pool.bind('WETH', Decimal('4'), Decimal('10'))
111+
pool.bind('DAI', Decimal('12'), Decimal('11'))
112+
swap_fee = Decimal('0.001')
113+
pool.set_swap_fee(swap_fee)
114+
p_ao = Decimal('2.2')
115+
t_ai = pool.join_swap_pool_amount_out(token_in='DAI', pool_amount_out=p_ao, max_amount_in=Decimal('1000'))
116+
self.assertAlmostEqual(pool.pool_token_supply, Decimal('102.2'))
117+
self.assertAlmostEqual(t_ai, Decimal('0.509279173873455029'))
118+
self.assertAlmostEqual(pool.get_balance('DAI'), Decimal('12.509279173873455029'))
119+
self.assertAlmostEqual(pool.get_balance('WETH'), Decimal('4'))
120+
121+
122+
108123
if __name__ == '__main__':
109124
unittest.main()

0 commit comments

Comments
 (0)