diff --git a/ts-sdk/whirlpool/src/position.ts b/ts-sdk/whirlpool/src/position.ts index d61ffa911..9d86a23e2 100644 --- a/ts-sdk/whirlpool/src/position.ts +++ b/ts-sdk/whirlpool/src/position.ts @@ -3,9 +3,11 @@ import { fetchAllMaybePosition, fetchAllMaybePositionBundle, fetchAllPosition, + fetchAllPositionWithFilter, getBundledPositionAddress, getPositionAddress, getPositionBundleAddress, + positionWhirlpoolFilter, } from "@orca-so/whirlpools-client"; import { _POSITION_BUNDLE_SIZE } from "@orca-so/whirlpools-core"; import { getTokenDecoder, TOKEN_PROGRAM_ADDRESS } from "@solana-program/token"; @@ -14,6 +16,7 @@ import type { Account, Address, GetMultipleAccountsApi, + GetProgramAccountsApi, GetTokenAccountsByOwnerApi, Rpc, } from "@solana/web3.js"; @@ -178,3 +181,34 @@ export async function fetchPositionsForOwner( return positionsOrBundles; } + +/** + * Fetches all positions for a given Whirlpool. + * + * @param {SolanaRpc} rpc - The Solana RPC client used to fetch positions. + * @param {Address} whirlpool - The address of the Whirlpool. + * @returns {Promise} - A promise that resolves to an array of hydrated positions. + * + * @example + * import { fetchPositionsInWhirlpool } from '@orca-so/whirlpools'; + * import { createSolanaRpc, devnet, address } from '@solana/web3.js'; + * + * const devnetRpc = createSolanaRpc(devnet('https://api.devnet.solana.com')); + * await devnetRpc.requestAirdrop(wallet.address, lamports(1000000000n)).send(); + * + * const whirlpool = address("Czfq3xZZDmsdGdUyrNLtRhGc47cXcZtLG4crryfu44zE"); + * const positions = await fetchPositionsInWhirlpool(devnetRpc, whirlpool); + */ +export async function fetchPositionsInWhirlpool( + rpc: Rpc, + whirlpool: Address, +): Promise { + const positions = await fetchAllPositionWithFilter( + rpc, + positionWhirlpoolFilter(whirlpool), + ); + return positions.map((x) => ({ + ...x, + isPositionBundle: false, + })); +} diff --git a/ts-sdk/whirlpool/tests/position.test.ts b/ts-sdk/whirlpool/tests/position.test.ts index d0010302d..3201aa1c8 100644 --- a/ts-sdk/whirlpool/tests/position.test.ts +++ b/ts-sdk/whirlpool/tests/position.test.ts @@ -9,7 +9,7 @@ import { setupWhirlpool, } from "./utils/program"; import { SPLASH_POOL_TICK_SPACING } from "../src/config"; -import { fetchPositionsForOwner } from "../src/position"; +import { fetchPositionsForOwner, fetchPositionsInWhirlpool } from "../src/position"; import { rpc, signer } from "./utils/mockRpc"; import { orderMints } from "../src/token"; @@ -46,4 +46,10 @@ describe("Fetch Position", () => { const positions = await fetchPositionsForOwner(rpc, other.address); assert.strictEqual(positions.length, 0); }); + + // TODO: enable this when solana-bankrun supports gpa + it.skip("Should fetch positions for a whirlpool", async () => { + const positions = await fetchPositionsInWhirlpool(rpc, pool); + assert.strictEqual(positions.length, 3); + }); });