How do you calculate the price impact?
What would be the formula look like for swap of 350 ADA for C4?
If you can read code, they have a method for computing estimated receive amount for a swap in the blockfrost adapter. You should be able to use this to calculate price impact.
this.value.find(({ unit }) => unit === this.assetB)?.quantity ?? "0"
);
}
/**
* Get the output amount if we swap a certain amount of a token in the pair
* @param assetIn The asset that we want to swap from
* @param amountIn The amount that we want to swap from
* @returns The amount of the other token that we get from the swap and its price impact
*/
getAmountOut(
assetIn: string,
amountIn: bigint
): { amountOut: bigint; priceImpact: Big } {
invariant(
assetIn === this.assetA || assetIn === this.assetB,
`asset ${assetIn} doesn't exist in pool ${this.assetA}-${this.assetB}`
);
const [reserveIn, reserveOut] =
assetIn === this.assetA
? [this.reserveA, this.reserveB]
I have created an analogous function in the Python library I maintain:
"""
if self.unit_a != "lovelace":
raise NotImplementedError("tvl for non-ADA pools is not implemented.")
tvl = (Decimal(self.reserve_a) / Decimal(10**6)).quantize(
1 / Decimal(10**6)
)
return tvl
def get_amount_out(self, asset: Assets) -> Tuple[Assets, float]:
"""Get the output asset amount given an input asset amount.
Args:
asset: An asset with a defined quantity.
Returns:
A tuple where the first value is the estimated asset returned from the swap
and the second value is the price impact ratio.
"""
assert len(asset) == 1, "Asset should only have one token."
1 Like