From 11055c2ba9149e7210ca865d812694f8b2b93447 Mon Sep 17 00:00:00 2001 From: yugure-orca <109891005+yugure-orca@users.noreply.github.com> Date: Mon, 13 May 2024 11:56:51 +0900 Subject: [PATCH] Create 55b_list_whirlpool_with_a_specific_token.ts --- ...5b_list_whirlpool_with_a_specific_token.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 orca/whirlpool/whirlpools_sdk/55b_list_whirlpool_with_a_specific_token.ts diff --git a/orca/whirlpool/whirlpools_sdk/55b_list_whirlpool_with_a_specific_token.ts b/orca/whirlpool/whirlpools_sdk/55b_list_whirlpool_with_a_specific_token.ts new file mode 100644 index 0000000..1aae02f --- /dev/null +++ b/orca/whirlpool/whirlpools_sdk/55b_list_whirlpool_with_a_specific_token.ts @@ -0,0 +1,65 @@ +// tested on @orca-so/whirlpools-sdk@0.11.7 + +import { Connection, GetProgramAccountsResponse, Keypair, PublicKey } from "@solana/web3.js"; +import { + WhirlpoolContext, + ORCA_WHIRLPOOL_PROGRAM_ID, + ORCA_WHIRLPOOLS_CONFIG, + ParsableWhirlpool, +} from "@orca-so/whirlpools-sdk"; +import { Wallet } from "@coral-xyz/anchor"; + +// export RPC_ENDPOINT_URL= + +async function main() { + const RPC_ENDPOINT_URL = process.env["RPC_ENDPOINT_URL"] || ""; + + const connection = new Connection(RPC_ENDPOINT_URL); + const dummyWallet = new Wallet(Keypair.generate()); + const ctx = WhirlpoolContext.from(connection, dummyWallet, ORCA_WHIRLPOOL_PROGRAM_ID); + + const SAMO_MINT_ADDRESS = new PublicKey("7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU"); + + // filter by SAMO + const whirlpoolsByMintA = await fetchWhirlpoolsByMint(ctx.connection, ORCA_WHIRLPOOLS_CONFIG, "A", SAMO_MINT_ADDRESS); + const whirlpoolsByMintB = await fetchWhirlpoolsByMint(ctx.connection, ORCA_WHIRLPOOLS_CONFIG, "B", SAMO_MINT_ADDRESS); + const whirlpools = [...whirlpoolsByMintA, ...whirlpoolsByMintB]; + + for (const {pubkey, account} of whirlpools) { + const whirlpoolData = ParsableWhirlpool.parse(pubkey, account)!; + console.log( + "whirlpools with SAMO", + `address=${pubkey.toBase58()}`, + `A=${whirlpoolData.tokenMintA.toBase58()}`, + `B=${whirlpoolData.tokenMintB.toBase58()}`, + `tickSpacing=${whirlpoolData.tickSpacing}` + ); + } +} + +async function fetchWhirlpoolsByMint(connection: Connection, whirlpoolsConfig: PublicKey, aOrB: "A" | "B", mint: PublicKey): Promise { + const whirlpoolAccountSize = 653; + const whirlpoolsConfigOffset = 8; + const tokenMintAOffset = 101; + const tokenMintBOffset = 181; + + const tokenMintOffset = aOrB === "A" ? tokenMintAOffset : tokenMintBOffset; + + const accounts = await connection.getProgramAccounts( + ORCA_WHIRLPOOL_PROGRAM_ID, + { + filters: [ + // filter by size + {dataSize: whirlpoolAccountSize}, + // filter by whirlpoolsConfig + {memcmp: {offset: whirlpoolsConfigOffset, bytes: whirlpoolsConfig.toBase58()}}, + // filter by mint + {memcmp: {offset: tokenMintOffset, bytes: mint.toBase58()}}, + ] + } + ); + + return accounts; +} + +main();