raydium-io / raydium-sdk

An SDK for building applications on top of Raydium.
https://sdk.raydium.io
GNU General Public License v3.0
308 stars 125 forks source link

How to subscribe on pool reserves changes? #52

Open hurek opened 5 months ago

hurek commented 5 months ago

Hello! Is it possible to subscribe on pool reserves changes via websocket and calculate real-time prices?

precious-void commented 4 months ago

@hurek I think you can listen to the logs of the raydium amm programm and check the instructions to have GetPoolInfo with data for your specific pool. That's how Liquidity.fetchInfo works in raydium-sdk, they fetch the program logs and extract the data from there.

https://github.com/raydium-io/raydium-sdk/blob/master/src/liquidity/liquidity.ts#L1988C1-L1993C1 https://github.com/raydium-io/raydium-sdk/blob/master/src/common/web3.ts#L278

Here is a helpful comment in other thread with an example on how it could be done: https://github.com/raydium-io/raydium-sdk/issues/42#issuecomment-1882963286

hurek commented 4 months ago

@shtelzerartem What about onProgramAccountChange() with Raydium_v4 pubkey as input? I suppose we can use LiquidityState layout to decode accountInfo.data and get baseReserve quoteReserve isn't it?

What is faster - listen the raydium accounts change or logs?