raydium-io / raydium-sdk-V2

Open-source Typescript SDK for Raydium
GNU General Public License v3.0
38 stars 38 forks source link

How to compute the actual realtime price for a raydium pool #38

Closed TechSkieur closed 3 weeks ago

TechSkieur commented 3 weeks ago

Hi experts,

Actually I have two questions:

  1. I have used raydium sdk to swap a token, got the tx Id => how could I compute the actual price for this particular swap transaction?
  2. after swap a token, I want to monitor the realtime price of this pool, and determine next moves. How to compute realtime price for this token?

According to my analysis, there are several prices I can get, but I'm a bit confused among them...

  1. I can use raydium.liquidity.getPoolInfoFromRpc to get realtime pool info, there are poolInfo.price and poolInfo.lpprice. I guess poolInfo.price is more close to realtime price?
  2. I can also get realtime poolRpcData json, from there I can obtain baseReserve and quoteReserve, so I can derive price as baseReserve/quoteReserve, this actually equals to filed currentPrice of raydium.liquidity.computeAmountOut output.
  3. Also when calling raydium.liquidity.computeAmountOut, can also get either currentPrice and executionPrice, what's the difference betweem them...

As you can see, there are several prices I can get or obtain, which one is the correct that I should use? Thanks for answering!

TechSkieur commented 3 weeks ago

@cruzshia Maybe you can help to answer? Really appreciate, thanks in advance!

cruzshia commented 3 weeks ago
  1. Yes use poolInfo.price, lpPrice is used to calculating user's value of liquidity

  2. Yes

  3. execution price means the price of current swap tx which might be different from current price(with slippage)

TechSkieur commented 3 weeks ago

@cruzshia thanks for such rapid response!!! One more question, is there any page/guide on explaining all those concepts? Or this is something needs to be understood based on experiences gradually...

cruzshia commented 3 weeks ago

I think this is common knowledge of amm, you can check out source code here https://github.com/raydium-io/raydium-sdk-V2/blob/master/src/raydium/liquidity/liquidity.ts#L1113.

TechSkieur commented 3 weeks ago

ok, got it, you solved my problem again! thanks, I'll close this ticket accordingly.