@@ -686,82 +686,6 @@ def _expand_submatrix(A, k: int, start: int):
686686 return A_limits_i
687687
688688
689- def _get_stableswap_bounds (amm , amm_i : AmmIndexObject , approx , k , scaling ):
690- B = [0 ] + [scaling [tkn ] for tkn in amm .asset_list ]
691- C = [scaling [amm .unique_id ]]
692- ann = amm .ann
693- s0 = amm .shares
694- D0 = amm .d
695- n_amm = len (amm .asset_list ) + 1
696- sum_assets = sum ([amm .liquidity [tkn ] for tkn in amm .asset_list ])
697- # D0' = D_0 * (1 - 1/ann)
698- D0_prime = D0 * (1 - 1 / ann )
699- # a0 ~= -delta_s/s0 + [1 / (sum x_i^0 - D0') * sum delta_x_i - (D0'/s0) / (sum x_i^0 - D0') * delta_s]
700- denom = sum_assets - D0_prime
701- if approx [0 ] == "linear" :
702- A5j = np .zeros ((1 , k ))
703- A5j [0 , amm_i .aux [0 ]] = 1 # a_{j,0} coefficient
704- A5j [0 , amm_i .shares_net ] = (1 + D0_prime / denom ) * C [0 ] / s0 # delta_s coefficient
705- for t in range (1 , n_amm ):
706- A5j [0 , amm_i .asset_net [t - 1 ]] = - B [t ] / denom # delta_x_i coefficient
707- b5j = np .array ([0 ])
708- cones5j = [cb .ZeroConeT (1 )]
709- cones_count5j = [1 ]
710- else :
711- A5j = np .zeros ((3 , k ))
712- b5j = np .array ([0 , 0 , 0 ])
713- # x = a_{j,0}
714- A5j [0 , amm_i .aux [0 ]] = - 1 # a_{j,0} coefficient
715- # y = 1 + C_jS_j / s_0
716- A5j [1 , amm_i .shares_net ] = - C [0 ] / s0 # delta_s coefficient
717- b5j [1 ] = 1
718- # z = An^n / D_0 sum(x_i^0 + B_i X_i) + (1 - An^n)(1 + C_jS_j / s_0)
719- A5j [2 , amm_i .shares_net ] = D0_prime * C [0 ] / denom / s0
720- for t in range (1 , n_amm ):
721- A5j [2 , amm_i .asset_net [t - 1 ]] = - B [t ] / denom
722- b5j [2 ] = 1
723- cones5j = [cb .ExponentialConeT ()]
724- cones_count5j = [3 ]
725-
726- for t in range (1 , n_amm ):
727- x0 = amm .liquidity [amm .asset_list [t - 1 ]]
728- if approx [t ] == "linear" :
729- A5jt = np .zeros ((1 , k ))
730- A5jt [0 , amm_i .aux [t ]] = 1 # a_{j,t} coefficient
731- A5jt [0 , amm_i .shares_net ] = C [0 ] / s0 # delta_s coefficient
732- A5jt [0 , amm_i .asset_net [t - 1 ]] = - B [t ] / x0 # delta_x_i coefficient
733- b5jt = np .array ([0 ])
734- cone5jt = cb .ZeroConeT (1 )
735- cone_count5jt = 1
736- else :
737- A5jt = np .zeros ((3 , k ))
738- b5jt = np .zeros (3 )
739- # x = a_{j,t}
740- A5jt [0 , amm_i .aux [t ]] = - 1
741- # y = 1 + C_jS_j / s_0
742- A5jt [1 , amm_i .shares_net ] = - C [0 ] / s0
743- b5jt [1 ] = 1
744- # z = (x_t^0 + B_t X_t) / D_0
745- A5jt [2 , amm_i .asset_net [t - 1 ]] = - B [t ] / x0
746- b5jt [2 ] = 1
747- cone5jt = cb .ExponentialConeT ()
748- cone_count5jt = 3
749- cones5j .append (cone5jt )
750- cones_count5j .append (cone_count5jt )
751- A5j = np .vstack ([A5j , A5jt ])
752- b5j = np .append (b5j , np .array (b5jt ))
753-
754- A5j_final = np .zeros ((1 , k ))
755- for t in range (n_amm ):
756- A5j_final [0 , amm_i .aux [t ]] = - 1
757- b5j_final = np .array ([0 ])
758- A5j = np .vstack ([A5j , A5j_final ])
759- b5j = np .append (b5j , b5j_final )
760- cones5j .append (cb .NonnegativeConeT (1 ))
761- cones_count5j .append (1 )
762-
763- return A5j , b5j , cones5j , cones_count5j
764-
765689def _get_amm_bounds (p , indices_to_keep = None ):
766690 # CFMM invariants must be respected
767691 n , u , m , N = p .n , p .u , p .m , p .N
@@ -772,16 +696,10 @@ def _get_amm_bounds(p, indices_to_keep=None):
772696 cones5 = []
773697 cones_count5 = []
774698 for j , amm in enumerate (p .amm_list ):
775- amm_i = p .amm_i [j ]
776699 amm_constraints = p .amm_constraints [j ]
777700 approx = p .get_amm_approx (j )
778- if isinstance (amm , StableSwapPoolState ):
779- A5j , b5j , cones5j , cones_count5j = _get_stableswap_bounds (amm , amm_i , approx , k , p ._scaling )
780- elif isinstance (amm , ConstantProductPoolState ):
781- A5j_small , b5j , cones5j , cones_count5j = amm_constraints .get_amm_bounds (approx , p ._scaling )
782- A5j = _expand_submatrix (A5j_small , k , p .amm_i [j ].shares_net )
783- else :
784- raise AssertionError ("Unrecognized AMM type" )
701+ A5j_small , b5j , cones5j , cones_count5j = amm_constraints .get_amm_bounds (approx , p ._scaling )
702+ A5j = _expand_submatrix (A5j_small , k , p .amm_i [j ].shares_net )
785703 A5 = np .vstack ([A5 , A5j ])
786704 b5 = np .concatenate ([b5 , b5j ])
787705 cones5 = cones5 + cones5j
0 commit comments