raydium-io / raydium-sdk

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

How to find opening date / hour of a pool? #42

Open AndoKarim opened 10 months ago

AndoKarim commented 10 months ago

Hello, Do you know how I can access to this information based on the pool ID / Token ID? Is there an API endpoint by chance?

Thanks.

endrsmar commented 10 months ago

You need to first get LiquidityPoolKeys for your pool - either by parsing the https://api.raydium.io/v2/sdk/liquidity/mainnet.json and using jsonInfo2PoolKeys function from the sdk or by calling Liquidity.fetchAllPoolKeys and finding your pool. Then you call Liquidity.fetchInfo with the poolKeys you acqired earlier and the returned LiquidityPoolInfo has openTime property which is a UNIX epoch timestamp of when the pool opens.

AndoKarim commented 10 months ago

Hello thanks for the help. My idea is to listen to new created pools and it seems that mainnet.json does not have latest pools created if I check the last index of the array. Except if they're not sorted by creation date, but even by searching through the file with a new pool, I cant find it. I was thinking about listening directly to incoming transactions. What do you think?

Thanks again

endrsmar commented 10 months ago

Yes there is unfortunately some delay between pool being created and it being available in mainnet.json. You can listen for new pools, the best way is probably to use solana's web3.js library in following way: 1) Use Connection.onLogs method with Raydium V4 Pool program ID as the pubkey filter to create a websocket listener for any transaction going to the Raydium V4 Pool program 2) In the listener for the logs, filter out any transaction that isn't an LP initialization (you can simply check if the log entries contain "init_pc_amount" string, that is in the logs only for LP initialization transactions) 3) Fetch the transaction using Connection::getParsedTransaction 4) Parse the necessary data from the initialization transaction, you get most of the addresses that are needed for PoolKeys in that transaction except for couple of the Serum market addresses necessary 5) To get the Serum addresses you'll have fetch Account info for the Serum market address you decode from the previous tx by using Connection::getAccountInfo, then use @openbook-dex/openbook lib's MARKET_STATE_LAYOUT_V3.decode to get the market addresses. 6) Last step is to get the market authority address, you use raydium-lib's Market::getAssociatedAuthority with the Serum program id and market id, now you have all the keys needed to construct LiquidityPoolKeysV4 object neccessary to make a swap

endrsmar commented 10 months ago

Here's a gist of example new raydium pool listener, it parses the transactions and fetches pool keys for new pools https://gist.github.com/endrsmar/684c336c3729ec4472b2f337c50c3cdb

AndoKarim commented 10 months ago

Thanks a lot for your answer I understood many things.

Do you have a TG or else to discuss it easily ? Thanks!

endrsmar commented 10 months ago

Don't really use TG but if you have Discord feel free to shoot me a message at marist.1805

coin5555 commented 10 months ago

Hi I have an error in fetchInfo function. How to solve it? This is my code. const poolInfo = await Liquidity.fetchInfo({ connection, poolKeys }); "Liquidity is Raydium sdk function. poolKeys is publicKey type." This is error. tx failed => Error: simulate log not match keyword (argument="keyword"), value="GetPoolData"), code=INVALID_ARGUMENT, module=common/web3, version=1.1.0-beta.1) at W.makeError (logger.ts:216:1) at W.throwError (logger.ts:228:1) at W.throwArgumentError (logger.ts:232:1) at W.assertArgument (logger.ts:246:1) at Wt (web3.ts:252:1) at async qe.fetchMultipleInfo (liquidity.ts:1622:1) at async qe.fetchInfo (liquidity.ts:1566:1) at async calcAmountOut (raydium.js:45:1) at async tradeContext.js:191:1

AndoKarim commented 10 months ago

@endrsmar added you :)

luanhenriquepp commented 9 months ago

@AndoKarim why fecthinfo delay so much?

jgculebras commented 9 months ago

@endrsmar added you on discord, if I can ask you some questions when you available. Thanks

jjordan-quantum commented 9 months ago

Here's a gist of example new raydium pool listener, it parses the transactions and fetches pool keys for new pools https://gist.github.com/endrsmar/684c336c3729ec4472b2f337c50c3cdb

How did you figure this out? Like what were your documentation sources?

gizzye commented 8 months ago

pretty sure the documentation is the source code

tuncatunc commented 6 months ago

@AndoKarim, please check out the solana-trading-bot repository. It'll help.

https://github.com/warp-id/solana-trading-bot/blob/master/listeners/listeners.ts