Skip to content

Commit

Permalink
add price_impact field to SwapQuote
Browse files Browse the repository at this point in the history
  • Loading branch information
gokselcoban committed Jul 20, 2022
1 parent a999d6c commit 84ccc26
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tinyman/v1/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class SwapQuote:
amount_out: AssetAmount
swap_fees: int
slippage: float
price_impact: float

@property
def amount_out_with_slippage(self) -> AssetAmount:
Expand Down Expand Up @@ -316,12 +317,17 @@ def fetch_fixed_input_swap_quote(self, amount_in: AssetAmount, slippage=0.05) ->

amount_out = AssetAmount(asset_out, int(asset_out_amount))

swap_price = amount_out.amount / amount_in.amount
pool_price = output_supply / input_supply
price_impact = abs(round((swap_price / pool_price) - 1, 5))

quote = SwapQuote(
swap_type='fixed-input',
amount_in=amount_in,
amount_out=amount_out,
swap_fees=AssetAmount(amount_in.asset, int(swap_fees)),
slippage=slippage,
price_impact=price_impact
)
return quote

Expand All @@ -348,12 +354,17 @@ def fetch_fixed_output_swap_quote(self, amount_out: AssetAmount, slippage=0.05)

amount_in = AssetAmount(asset_in, int(asset_in_amount))

swap_price = amount_out.amount / amount_in.amount
pool_price = output_supply / input_supply
price_impact = abs(round((swap_price / pool_price) - 1, 5))

quote = SwapQuote(
swap_type='fixed-output',
amount_out=amount_out,
amount_in=amount_in,
swap_fees=AssetAmount(amount_in.asset, int(swap_fees)),
slippage=slippage,
price_impact=price_impact
)

return quote
Expand Down

0 comments on commit 84ccc26

Please sign in to comment.