Skip to content

Commit 40ca553

Browse files
authored
Merge pull request #40 from Hipo/price-impact
add price_impact field to SwapQuote
2 parents 910d109 + 84ccc26 commit 40ca553

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

tinyman/v1/pools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class SwapQuote:
8484
amount_out: AssetAmount
8585
swap_fees: int
8686
slippage: float
87+
price_impact: float
8788

8889
@property
8990
def amount_out_with_slippage(self) -> AssetAmount:
@@ -316,12 +317,17 @@ def fetch_fixed_input_swap_quote(self, amount_in: AssetAmount, slippage=0.05) ->
316317

317318
amount_out = AssetAmount(asset_out, int(asset_out_amount))
318319

320+
swap_price = amount_out.amount / amount_in.amount
321+
pool_price = output_supply / input_supply
322+
price_impact = abs(round((swap_price / pool_price) - 1, 5))
323+
319324
quote = SwapQuote(
320325
swap_type='fixed-input',
321326
amount_in=amount_in,
322327
amount_out=amount_out,
323328
swap_fees=AssetAmount(amount_in.asset, int(swap_fees)),
324329
slippage=slippage,
330+
price_impact=price_impact
325331
)
326332
return quote
327333

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

349355
amount_in = AssetAmount(asset_in, int(asset_in_amount))
350356

357+
swap_price = amount_out.amount / amount_in.amount
358+
pool_price = output_supply / input_supply
359+
price_impact = abs(round((swap_price / pool_price) - 1, 5))
360+
351361
quote = SwapQuote(
352362
swap_type='fixed-output',
353363
amount_out=amount_out,
354364
amount_in=amount_in,
355365
swap_fees=AssetAmount(amount_in.asset, int(swap_fees)),
356366
slippage=slippage,
367+
price_impact=price_impact
357368
)
358369

359370
return quote

0 commit comments

Comments
 (0)