Liquidity reserves of both tokens.
returns yOutput, or yDelta for xInput (or xDelta)
* Price section for an understanding of the DEX's pricing model and for a price function to add to your contract.
* we’re paying a 0.3% fee for additional deduction to DEX
* let’s move on to the interesting part of the AMM: exchanging tokens. Say that a user wants to get token_b in exchange for token_a of some specified amount (token_a_amount). Let’s denote the amount of tokens they will get by token_b_amount. In the equations below we let:
* a and b denote the amounts of token_a and token_b the user swaps,
* x and y denote the current balances of token_a and token_b in the AMM (that is, AmmState.token_a_balance and AmmState.token_b_balance).
* The AMM formula states that the value of token_a_balance * token_b_balance (that is, x * y) should be preserved. So we have:
* (x+a).(y-b) = x.y
* Let’s isolate b (as the rest of the values are known):
* b = y.a/(x+a)
* Output token price is determined using above.
#Slippage algorithm - for same amount of axie consecutive ethereum outputted is slightly slipped/less, due to change in reserves.
Algorithm
1)Swap Axies to Eth
2)Swap same amount of Eth to Axies from different exchange.
Repeat this couple of times to increase your current token.
Practically swapping is done across two different exchanges.
A simple AMM in Cairo to reduce slippage -
Reference - https://starknet.io/docs/0.10.0/hello_cairo/amm.html#
let’s move on to the interesting part of the AMM: exchanging tokens. Say that a user wants to get token_b in exchange for token_a of some specified amount (token_a_amount). Let’s denote the amount of tokens they will get by token_b_amount. In the equations below we let:
a and b denote the amounts of token_a and token_b the user swaps,
x and y denote the current balances of token_a and token_b in the AMM (that is, AmmState.token_a_balance and AmmState.token_b_balance).
The AMM formula states that the value of token_a_balance * token_b_balance (that is, x * y) should be preserved. So we have:
(x+a).(y-b) = x.y
Let’s isolate b (as the rest of the values are known):
b = y.a/(x+a)