#Get tokens from an AMM.
1 messages · Page 1 of 1 (latest)
@forest yarrow
Can you help?
lemme check with dev
You need to fetch
(a) LP tokens of a user
(b) AMMInfo (total LP tokens and asset balances)
Then you just do math.
For example:
- user has 100 LP XRP-EEE tokens
- XRP-EEE issued total of 1,000 LP tokens
- XRP-EEE pool currently has 5,000 XRP and 10,000 EEE
You can derive that this user is owning 100 / 1,000 = 0.1 (i.e. 10%) of XRP-EEE pool.
Since all pools have assets in 50/50 split, you just take 10% of each asset to know user's assets in the pool.
So, user has
XRP = 5,000 * 0.1 = 500 XRP
EEE = 10,000 * 0.1 = 1,000 EEEE
yes! that makes so much sense; it was right in front of my eyes the whole time 😅!
Thanks a lot @abstract fulcrum!
@abstract fulcrum Can you also guide me about the swap txn?
I saw the documentation but theres a lotta formulas, which are going over my head 😅, am sure theres a much simpler algorithm to do it?
How exactly do you determine the exchange rate?
For instance, xrp/fff pool, yes for low amounts its easy to calculate as 1 xrp = 0.154354 fff and vice versa, but as the amount of fff is less, if we increase the amount to lets say 100 xrp it equals to roughly 12 FFF, am guessing theres some kind of ratio here?
The price!
In the third image, we see that 100 xrp = 12.929 fff, but once i click swap, it takes the least amount recieved as 12.8643, am guessing its something related to slippage tolerance?
(pardon me if these questions feel stupid 😅 am not soo much into finance stuff, just learning about the concepts on the go)
There is a Spot Price, which is just basic ratio of tokens in the pool, i.e. if pool has 1 BTC and 40,000 USD - then 1 BTC is 40,000 USD. This itself is useful only for small trades, as they don't really affect the pool that much.
But there is also an Effective Price, which describes how much you'd pay for asset relative to your trade size. I.e. if you try to sell 1 BTC in (1 BTC / 40,000 USD) pool you'd impact the price of BTC a lot, you can't empty the pool completely, something has to stay in there. If you use the formulas you'd see you would get less than 40,000 USD for this trade. I think on Orchestra we simply simulate any trade you want to do and deduce the effective price from it. You can see effective price in "Summary" section btw, notice how it gets worse the bigger your trade is.
You can also see "Minimum received" under "Summary", which depends on your slippage tolerance. Slippage tolerance basically just means "if while transaction is processed the price gets worse by N% - fail the transaction", so that's why you see lower number in the transaction. The number in the main UI (12.929) is the amount you will receive if nothing else happens in the pool (i.e. imagine whole world sleeps and you're the only one using AMM - you'd get 12.929).
Math formulas are a bit scary but actually make a lot of sense if you look long enough at them 😁
You can simplify some stuff in there too, for example Wa and Wb are both 0.5 and fixed, you can reduce them in some places.