Skip to content

Commit

Permalink
Add fetch positions by whirlpool function (#485)
Browse files Browse the repository at this point in the history
* Add fetch positions by whirlpool function

* Rename

* Add typedoc
  • Loading branch information
wjthieme authored Nov 8, 2024
1 parent bc2b1e1 commit cf29c17
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
34 changes: 34 additions & 0 deletions ts-sdk/whirlpool/src/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -14,6 +16,7 @@ import type {
Account,
Address,
GetMultipleAccountsApi,
GetProgramAccountsApi,
GetTokenAccountsByOwnerApi,
Rpc,
} from "@solana/web3.js";
Expand Down Expand Up @@ -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<HydratedPosition[]>} - 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<GetProgramAccountsApi>,
whirlpool: Address,
): Promise<HydratedPosition[]> {
const positions = await fetchAllPositionWithFilter(
rpc,
positionWhirlpoolFilter(whirlpool),
);
return positions.map((x) => ({
...x,
isPositionBundle: false,
}));
}
8 changes: 7 additions & 1 deletion ts-sdk/whirlpool/tests/position.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
});
});

0 comments on commit cf29c17

Please sign in to comment.