Skip to content

Commit cf29c17

Browse files
authored
Add fetch positions by whirlpool function (#485)
* Add fetch positions by whirlpool function * Rename * Add typedoc
1 parent bc2b1e1 commit cf29c17

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

ts-sdk/whirlpool/src/position.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {
33
fetchAllMaybePosition,
44
fetchAllMaybePositionBundle,
55
fetchAllPosition,
6+
fetchAllPositionWithFilter,
67
getBundledPositionAddress,
78
getPositionAddress,
89
getPositionBundleAddress,
10+
positionWhirlpoolFilter,
911
} from "@orca-so/whirlpools-client";
1012
import { _POSITION_BUNDLE_SIZE } from "@orca-so/whirlpools-core";
1113
import { getTokenDecoder, TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
@@ -14,6 +16,7 @@ import type {
1416
Account,
1517
Address,
1618
GetMultipleAccountsApi,
19+
GetProgramAccountsApi,
1720
GetTokenAccountsByOwnerApi,
1821
Rpc,
1922
} from "@solana/web3.js";
@@ -178,3 +181,34 @@ export async function fetchPositionsForOwner(
178181

179182
return positionsOrBundles;
180183
}
184+
185+
/**
186+
* Fetches all positions for a given Whirlpool.
187+
*
188+
* @param {SolanaRpc} rpc - The Solana RPC client used to fetch positions.
189+
* @param {Address} whirlpool - The address of the Whirlpool.
190+
* @returns {Promise<HydratedPosition[]>} - A promise that resolves to an array of hydrated positions.
191+
*
192+
* @example
193+
* import { fetchPositionsInWhirlpool } from '@orca-so/whirlpools';
194+
* import { createSolanaRpc, devnet, address } from '@solana/web3.js';
195+
*
196+
* const devnetRpc = createSolanaRpc(devnet('https://api.devnet.solana.com'));
197+
* await devnetRpc.requestAirdrop(wallet.address, lamports(1000000000n)).send();
198+
*
199+
* const whirlpool = address("Czfq3xZZDmsdGdUyrNLtRhGc47cXcZtLG4crryfu44zE");
200+
* const positions = await fetchPositionsInWhirlpool(devnetRpc, whirlpool);
201+
*/
202+
export async function fetchPositionsInWhirlpool(
203+
rpc: Rpc<GetProgramAccountsApi>,
204+
whirlpool: Address,
205+
): Promise<HydratedPosition[]> {
206+
const positions = await fetchAllPositionWithFilter(
207+
rpc,
208+
positionWhirlpoolFilter(whirlpool),
209+
);
210+
return positions.map((x) => ({
211+
...x,
212+
isPositionBundle: false,
213+
}));
214+
}

ts-sdk/whirlpool/tests/position.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
setupWhirlpool,
1010
} from "./utils/program";
1111
import { SPLASH_POOL_TICK_SPACING } from "../src/config";
12-
import { fetchPositionsForOwner } from "../src/position";
12+
import { fetchPositionsForOwner, fetchPositionsInWhirlpool } from "../src/position";
1313
import { rpc, signer } from "./utils/mockRpc";
1414
import { orderMints } from "../src/token";
1515

@@ -46,4 +46,10 @@ describe("Fetch Position", () => {
4646
const positions = await fetchPositionsForOwner(rpc, other.address);
4747
assert.strictEqual(positions.length, 0);
4848
});
49+
50+
// TODO: enable this when solana-bankrun supports gpa
51+
it.skip("Should fetch positions for a whirlpool", async () => {
52+
const positions = await fetchPositionsInWhirlpool(rpc, pool);
53+
assert.strictEqual(positions.length, 3);
54+
});
4955
});

0 commit comments

Comments
 (0)