Skip to content

Commit

Permalink
Simplify routerhandler._calc_balancer_out_given_in
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos authored and platonfloria committed May 21, 2024
1 parent 6fd6338 commit 71382da
Showing 1 changed file with 4 additions and 31 deletions.
35 changes: 4 additions & 31 deletions fastlane_bot/helpers/routehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,10 @@ def _calc_balancer_out_given_in(balance_in: Decimal,
"""

denominator = balance_in + amount_in
base = divUp(balance_in, denominator) # balanceIn.divUp(denominator);
exponent = divDown(weight_in, weight_out) # weightIn.divDown(weightOut);
power = powUp(base, exponent) # base.powUp(exponent);

return mulDown(balance_out, complement(power)) # balanceOut.mulDown(power.complement());
base = balance_in / denominator if denominator > 0 else 0
exponent = weight_in / weight_out
power = base ** exponent
return balance_out * (1 - power)

def _solve_trade_output(
self, curve: Pool, trade: TradeInstruction, amount_in: Decimal = None
Expand Down Expand Up @@ -1203,32 +1202,6 @@ def calculate_trade_outputs(
def _from_wei_to_decimals(self, tkn0_amt: Decimal, tkn0_decimals: int) -> Decimal:
return Decimal(str(tkn0_amt)) / Decimal("10") ** Decimal(str(tkn0_decimals))

# TODO: Those functions should probably be private; also -- are they needed at
# all? Most of them seem to be extremely trivial

def divUp(a: Decimal, b: Decimal) -> Decimal:
if a * b == 0:
return Decimal(0)
else:
return a / b


def mulDown(a: Decimal, b: Decimal) -> Decimal:
return a * b


def divDown(a: Decimal, b: Decimal) -> Decimal:
result = a / b
return result


def complement(a: Decimal) -> Decimal:
return Decimal(1 - a) if a < 1 else Decimal(0)


def powUp(a: Decimal, b: Decimal) -> Decimal:
return a ** b


class BalancerInputTooLargeError(AssertionError):
pass
Expand Down

0 comments on commit 71382da

Please sign in to comment.