Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: increase coverage of spot prices function #152

Open
wants to merge 4 commits into
base: lp/account-balance-overrides
Choose a base branch
from

Conversation

tvinagre
Copy link
Contributor

Context

Some protocols, like BalancerV3 do not have the PriceFunction capability. This PR enhances the coverage of Spot Prices function, proposing an alternative way of calculating spot prices.

The code calculates a marginal price between token pairs using a numerical approximation of the derivative of the exchange function. Here's how it works:

  1. It takes two tokens (t0 and t1) and calculates:

    • x1: A small sell amount (1% of the sell limit)
    • x2: A slightly larger sell amount (1.01% of the sell limit)
  2. It performs two swap simulations:

    • y1: The amount of t1 received when selling amount x1 of t0
    • y2: The amount of t1 received when selling amount x2 of t0
  3. It calculates the marginal price as the ratio of the difference:

    • num = y2 - y1: The incremental output amount
    • den = x2 - x1: The incremental input amount
    • marginal_price = (y2 - y1) / (x2 - x1)
  4. It applies a decimal correction to account for different token decimal places:

    • token_correction = 10^(sell_token_decimals - buy_token_decimals)
    • final_price = marginal_price * token_correction

This numerical derivative approach approximates the instantaneous exchange rate at the given point on the AMM curve, which is more accurate than using average prices for larger amounts.

This function was already extended on Python's adapter (https://github.com/propeller-heads/tycho-simulation/pull/126/files)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

2 participants