-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 55b_list_whirlpool_with_a_specific_token.ts
- Loading branch information
1 parent
521d527
commit 11055c2
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
orca/whirlpool/whirlpools_sdk/55b_list_whirlpool_with_a_specific_token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// tested on @orca-so/[email protected] | ||
|
||
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=<YOUR RPC ENDPOINT> | ||
|
||
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<GetProgramAccountsResponse> { | ||
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(); |