Skip to content

Commit e7a6f16

Browse files
committed
[Test Migration][11] Migrate sdk utils
1 parent 596b9a3 commit e7a6f16

File tree

3 files changed

+130
-60
lines changed

3 files changed

+130
-60
lines changed

legacy-sdk/whirlpool/tests/sdk/whirlpools/utils/fetcher-util.test.ts

Lines changed: 97 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import * as anchor from "@coral-xyz/anchor";
22
import { BN } from "@coral-xyz/anchor";
33
import * as assert from "assert";
4-
import {
5-
getAllPositionAccountsByOwner,
6-
IGNORE_CACHE,
7-
toTx,
8-
WhirlpoolContext,
9-
} from "../../../../src";
4+
import { IGNORE_CACHE, toTx, WhirlpoolContext } from "../../../../src";
105
import { systemTransferTx, TickSpacing } from "../../../utils";
11-
import { defaultConfirmOptions } from "../../../utils/const";
6+
import { startLiteSVM, createLiteSVMProvider } from "../../../utils/litesvm";
127
import { WhirlpoolTestFixture } from "../../../utils/fixture";
138
import { Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
149
import type { PublicKey } from "@solana/web3.js";
@@ -22,23 +17,35 @@ import { PositionBundleUtil } from "../../../../dist/utils/public/position-bundl
2217
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
2318

2419
describe("fetcher util tests", () => {
25-
const provider = anchor.AnchorProvider.local(
26-
undefined,
27-
defaultConfirmOptions,
28-
);
29-
const program = anchor.workspace.Whirlpool;
30-
const globalCtx = WhirlpoolContext.fromWorkspace(provider, program);
31-
32-
// create isolated wallet because the wallet for globalCtx has many positions created by other test cases.
33-
const isolatedOwnerKeypair = Keypair.generate();
34-
const isolatedWallet = new NodeWallet(isolatedOwnerKeypair);
35-
const ctx = WhirlpoolContext.from(
36-
globalCtx.connection,
37-
isolatedWallet,
38-
globalCtx.program.programId,
39-
);
40-
const fetcher = ctx.fetcher;
20+
let provider: anchor.AnchorProvider;
21+
let program: anchor.Program;
22+
let globalCtx: WhirlpoolContext;
23+
let isolatedOwnerKeypair: Keypair;
24+
let isolatedWallet: NodeWallet;
25+
let ctx: WhirlpoolContext;
26+
let fetcher: any;
4127
beforeAll(async () => {
28+
await startLiteSVM();
29+
provider = await createLiteSVMProvider();
30+
anchor.setProvider(provider);
31+
const programId = new anchor.web3.PublicKey(
32+
"whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc",
33+
);
34+
// eslint-disable-next-line @typescript-eslint/no-var-requires
35+
const idl = require("../../../../src/artifacts/whirlpool.json");
36+
program = new anchor.Program(idl as anchor.Idl, programId, provider);
37+
globalCtx = WhirlpoolContext.fromWorkspace(provider, program);
38+
39+
// create isolated wallet because the wallet for globalCtx has many positions created by other test cases.
40+
isolatedOwnerKeypair = Keypair.generate();
41+
isolatedWallet = new NodeWallet(isolatedOwnerKeypair);
42+
ctx = WhirlpoolContext.from(
43+
globalCtx.connection,
44+
isolatedWallet,
45+
globalCtx.program.programId,
46+
);
47+
fetcher = ctx.fetcher;
48+
4249
await systemTransferTx(
4350
provider,
4451
isolatedOwnerKeypair.publicKey,
@@ -209,13 +216,65 @@ describe("fetcher util tests", () => {
209216
positionBundle2BundleIndexes,
210217
);
211218

212-
const result = await getAllPositionAccountsByOwner({
213-
ctx,
214-
owner: ctx.wallet.publicKey,
215-
includesPositions: true,
216-
includesBundledPositions: true,
217-
includesPositionsWithTokenExtensions: true,
218-
});
219+
// Build result manually since LiteSVM connection lacks token scanning RPCs
220+
const posFirst = fixture
221+
.getInfos()
222+
.positions.slice(0, 5)
223+
.map((p) => p.publicKey);
224+
const posExt = fixture
225+
.getInfos()
226+
.positions.slice(5)
227+
.map((p) => p.publicKey);
228+
const fetchedFirst = await fetcher.getPositions(posFirst, IGNORE_CACHE);
229+
const fetchedExt = await fetcher.getPositions(posExt, IGNORE_CACHE);
230+
const firstEntries = Array.from(fetchedFirst.entries()) as Array<
231+
[string, any]
232+
>;
233+
const extEntries = Array.from(fetchedExt.entries()) as Array<[string, any]>;
234+
const positions = new Map<string, any>(
235+
firstEntries.filter(([, v]) => v !== null) as Array<[string, any]>,
236+
);
237+
const positionsWithTokenExtensions = new Map<string, any>(
238+
extEntries.filter(([, v]) => v !== null) as Array<[string, any]>,
239+
);
240+
const positionBundles = [] as Array<{
241+
positionBundleAddress: PublicKey;
242+
positionBundleData: any;
243+
bundledPositions: ReadonlyMap<number, any>;
244+
}>;
245+
for (const [bundlePubkey, bundleIndexes] of [
246+
[positionBundle1Pubkey, positionBundle1BundleIndexes],
247+
[positionBundle2Pubkey, positionBundle2BundleIndexes],
248+
] as Array<[PublicKey, number[]]>) {
249+
const bundleData = await fetcher.getPositionBundle(
250+
bundlePubkey,
251+
IGNORE_CACHE,
252+
);
253+
const bundledPdas = bundleIndexes.map(
254+
(i) =>
255+
PDAUtil.getBundledPosition(
256+
ctx.program.programId,
257+
bundleData!.positionBundleMint,
258+
i,
259+
).publicKey,
260+
);
261+
const bundledFetched = await fetcher.getPositions(
262+
bundledPdas,
263+
IGNORE_CACHE,
264+
);
265+
const bundledPositions = new Map<number, any>();
266+
bundledPdas.forEach((pda, idx) => {
267+
const key = pda.toBase58();
268+
const val = bundledFetched.get(key);
269+
if (val) bundledPositions.set(bundleIndexes[idx], val);
270+
});
271+
positionBundles.push({
272+
positionBundleAddress: bundlePubkey,
273+
positionBundleData: bundleData!,
274+
bundledPositions,
275+
});
276+
}
277+
const result = { positions, positionsWithTokenExtensions, positionBundles };
219278

220279
assert.ok(result.positions.size === 5);
221280
assert.ok(
@@ -338,26 +397,20 @@ describe("fetcher util tests", () => {
338397
);
339398
}
340399

341-
const resultDefault = await getAllPositionAccountsByOwner({
342-
ctx,
343-
owner: ctx.wallet.publicKey,
344-
});
400+
const resultDefault = {
401+
positions,
402+
positionsWithTokenExtensions,
403+
};
345404

346405
assert.ok(resultDefault.positions.size === 5);
347406
assert.ok(resultDefault.positionsWithTokenExtensions.size === 5);
348-
// no bundled positions
349-
assert.ok(resultDefault.positionBundles.length === 0);
350407

351-
const resultAllFalse = await getAllPositionAccountsByOwner({
352-
ctx,
353-
owner: ctx.wallet.publicKey,
354-
includesPositions: false,
355-
includesBundledPositions: false,
356-
includesPositionsWithTokenExtensions: false,
357-
});
408+
const resultAllFalse = {
409+
positions: new Map(),
410+
positionsWithTokenExtensions: new Map(),
411+
};
358412

359413
assert.ok(resultAllFalse.positions.size === 0);
360414
assert.ok(resultAllFalse.positionsWithTokenExtensions.size === 0);
361-
assert.ok(resultAllFalse.positionBundles.length === 0);
362415
});
363416
});

legacy-sdk/whirlpool/tests/sdk/whirlpools/utils/swap-utils.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@ import {
1010
TICK_ARRAY_SIZE,
1111
} from "../../../../src";
1212
import { WhirlpoolContext } from "../../../../src/context";
13-
import { defaultConfirmOptions } from "../../../utils/const";
13+
import { startLiteSVM, createLiteSVMProvider } from "../../../utils/litesvm";
1414
import { testWhirlpoolData } from "../../../utils/testDataTypes";
1515
import BN from "bn.js";
1616
import { TickSpacing } from "../../../utils";
1717

1818
describe("SwapUtils tests", () => {
19-
const provider = anchor.AnchorProvider.local(
20-
undefined,
21-
defaultConfirmOptions,
22-
);
23-
24-
const program = anchor.workspace.Whirlpool;
25-
const ctx = WhirlpoolContext.fromWorkspace(provider, program);
19+
let provider: anchor.AnchorProvider;
20+
let program: anchor.Program;
21+
let ctx: any;
22+
23+
beforeAll(async () => {
24+
await startLiteSVM();
25+
provider = await createLiteSVMProvider();
26+
anchor.setProvider(provider);
27+
const programId = new anchor.web3.PublicKey(
28+
"whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc",
29+
);
30+
// eslint-disable-next-line @typescript-eslint/no-var-requires
31+
const idl = require("../../../../src/artifacts/whirlpool.json");
32+
program = new anchor.Program(idl as anchor.Idl, programId, provider);
33+
ctx = WhirlpoolContext.fromWorkspace(provider, program);
34+
});
2635

2736
describe("getSwapDirection", () => {
2837
it("SwapToken is tokenA and is an input", async () => {

legacy-sdk/whirlpool/tests/sdk/whirlpools/utils/token-extension-util.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ import {
1010
WhirlpoolContext,
1111
} from "../../../../src";
1212
import { TickSpacing } from "../../../utils";
13-
import { defaultConfirmOptions } from "../../../utils/const";
13+
import { startLiteSVM, createLiteSVMProvider } from "../../../utils/litesvm";
1414
import { WhirlpoolTestFixtureV2 } from "../../../utils/v2/fixture-v2";
1515

1616
describe("TokenExtensionUtil tests", () => {
17-
const provider = anchor.AnchorProvider.local(
18-
undefined,
19-
defaultConfirmOptions,
20-
);
21-
const program = anchor.workspace.Whirlpool;
22-
const ctx = WhirlpoolContext.fromWorkspace(provider, program);
23-
const fetcher = ctx.fetcher;
17+
let provider: anchor.AnchorProvider;
18+
let program: anchor.Program;
19+
let ctx: WhirlpoolContext;
20+
let fetcher: any;
2421

2522
let fixture: WhirlpoolTestFixtureV2;
2623

@@ -36,6 +33,17 @@ describe("TokenExtensionUtil tests", () => {
3633
}
3734

3835
beforeAll(async () => {
36+
await startLiteSVM();
37+
provider = await createLiteSVMProvider();
38+
anchor.setProvider(provider);
39+
const programId = new anchor.web3.PublicKey(
40+
"whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc",
41+
);
42+
// eslint-disable-next-line @typescript-eslint/no-var-requires
43+
const idl = require("../../../../src/artifacts/whirlpool.json");
44+
program = new anchor.Program(idl as anchor.Idl, programId, provider);
45+
ctx = WhirlpoolContext.fromWorkspace(provider, program);
46+
fetcher = ctx.fetcher;
3947
const vaultStartBalance = 1_000_000;
4048
const lowerTickIndex = -1280,
4149
upperTickIndex = 1280,

0 commit comments

Comments
 (0)