Skip to content

Commit bdbd6ad

Browse files
authored
Improve the ts-sdk testing setup (#448)
* Improve the ts-sdk testing setup * Remove unused files * Lint
1 parent ed6ad5b commit bdbd6ad

File tree

9 files changed

+659
-564
lines changed

9 files changed

+659
-564
lines changed

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

Lines changed: 34 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,37 @@ import {
77
openFullRangePositionInstructions,
88
increaseLiquidityInstructions,
99
} from "../src/increaseLiquidity";
10-
import {
11-
TOKEN_MINT_1,
12-
TOKEN_MINT_2,
13-
sendTransaction,
14-
rpc,
15-
initPayer,
16-
setAccount,
17-
} from "./mockRpc";
18-
import { setDefaultFunder, SPLASH_POOL_TICK_SPACING } from "../src/config";
10+
import { sendTransaction, rpc } from "./utils/mockRpc";
11+
import { SPLASH_POOL_TICK_SPACING } from "../src/config";
1912
import { swapInstructions } from "../src/swap";
20-
import type { Address, TransactionSigner } from "@solana/web3.js";
13+
import type { Address } from "@solana/web3.js";
2114
import { harvestPositionInstructions } from "../src/harvest";
2215
import {
2316
decreaseLiquidityInstructions,
2417
closePositionInstructions,
2518
} from "../src/decreaseLiquidity";
26-
import {
27-
AccountState,
28-
fetchToken,
29-
findAssociatedTokenPda,
30-
getTokenEncoder,
31-
TOKEN_PROGRAM_ADDRESS,
32-
} from "@solana-program/token";
19+
import { fetchToken } from "@solana-program/token";
3320
import {
3421
fetchPosition,
3522
fetchWhirlpool,
3623
getPositionAddress,
3724
} from "@orca-so/whirlpools-client";
3825
import assert from "assert";
26+
import { setupAta, setupMint } from "./utils/token";
27+
import { orderMints } from "../src/token";
3928

4029
describe("e2e", () => {
30+
let mintA: Address;
31+
let mintB: Address;
4132
let ataA: Address;
4233
let ataB: Address;
43-
let payer: TransactionSigner;
4434

4535
beforeAll(async () => {
46-
payer = await initPayer();
47-
setDefaultFunder(payer);
48-
49-
[ataA, ataB] = await Promise.all([
50-
findAssociatedTokenPda({
51-
mint: TOKEN_MINT_1,
52-
owner: payer.address,
53-
tokenProgram: TOKEN_PROGRAM_ADDRESS,
54-
}).then((x) => x[0]),
55-
findAssociatedTokenPda({
56-
mint: TOKEN_MINT_2,
57-
owner: payer.address,
58-
tokenProgram: TOKEN_PROGRAM_ADDRESS,
59-
}).then((x) => x[0]),
60-
]);
61-
62-
setAccount(
63-
ataA,
64-
getTokenEncoder().encode({
65-
mint: TOKEN_MINT_1,
66-
owner: payer.address,
67-
amount: 500e9,
68-
delegate: null,
69-
state: AccountState.Initialized,
70-
isNative: null,
71-
delegatedAmount: 0,
72-
closeAuthority: null,
73-
}),
74-
TOKEN_PROGRAM_ADDRESS,
75-
);
76-
77-
setAccount(
78-
ataB,
79-
getTokenEncoder().encode({
80-
mint: TOKEN_MINT_2,
81-
owner: payer.address,
82-
amount: 500e9,
83-
delegate: null,
84-
state: AccountState.Initialized,
85-
isNative: null,
86-
delegatedAmount: 0,
87-
closeAuthority: null,
88-
}),
89-
TOKEN_PROGRAM_ADDRESS,
90-
);
36+
const mint1 = await setupMint({ decimals: 9 });
37+
const mint2 = await setupMint({ decimals: 6 });
38+
[mintA, mintB] = orderMints(mint1, mint2);
39+
ataA = await setupAta(mintA, { amount: 500e9 });
40+
ataB = await setupAta(mintB, { amount: 500e9 });
9141
});
9242

9343
const fetchPositionByMint = async (positionMint: Address) => {
@@ -97,30 +47,25 @@ describe("e2e", () => {
9747

9848
const testInitSplashPool = async () => {
9949
const { instructions: createPoolInstructions, poolAddress } =
100-
await createSplashPoolInstructions(rpc, TOKEN_MINT_1, TOKEN_MINT_2);
101-
await sendTransaction(createPoolInstructions, payer);
50+
await createSplashPoolInstructions(rpc, mintA, mintB);
51+
await sendTransaction(createPoolInstructions);
10252

10353
const pool = await fetchWhirlpool(rpc, poolAddress);
104-
assert.strictEqual(pool.data.tokenMintA, TOKEN_MINT_1);
105-
assert.strictEqual(pool.data.tokenMintB, TOKEN_MINT_2);
54+
assert.strictEqual(pool.data.tokenMintA, mintA);
55+
assert.strictEqual(pool.data.tokenMintB, mintB);
10656
assert.strictEqual(pool.data.tickSpacing, SPLASH_POOL_TICK_SPACING);
10757

10858
return poolAddress;
10959
};
11060

11161
const testInitConcentratedLiquidityPool = async () => {
11262
const { instructions: createPoolInstructions, poolAddress } =
113-
await createConcentratedLiquidityPoolInstructions(
114-
rpc,
115-
TOKEN_MINT_1,
116-
TOKEN_MINT_2,
117-
128,
118-
);
119-
await sendTransaction(createPoolInstructions, payer);
63+
await createConcentratedLiquidityPoolInstructions(rpc, mintA, mintB, 128);
64+
await sendTransaction(createPoolInstructions);
12065

12166
const pool = await fetchWhirlpool(rpc, poolAddress);
122-
assert.strictEqual(pool.data.tokenMintA, TOKEN_MINT_1);
123-
assert.strictEqual(pool.data.tokenMintB, TOKEN_MINT_2);
67+
assert.strictEqual(pool.data.tokenMintA, mintA);
68+
assert.strictEqual(pool.data.tokenMintB, mintB);
12469
assert.strictEqual(pool.data.tickSpacing, 128);
12570

12671
return poolAddress;
@@ -134,7 +79,7 @@ describe("e2e", () => {
13479
await openFullRangePositionInstructions(rpc, poolAddress, {
13580
liquidity: 1000000000n,
13681
});
137-
await sendTransaction(instructions, payer);
82+
await sendTransaction(instructions);
13883

13984
const positionAfter = await fetchPositionByMint(positionMint);
14085
const tokenAAfter = await fetchToken(rpc, ataA);
@@ -162,7 +107,7 @@ describe("e2e", () => {
162107
positionMint,
163108
{ liquidity: 10000n },
164109
);
165-
await sendTransaction(instructions, payer);
110+
await sendTransaction(instructions);
166111

167112
const positionAfter = await fetchPositionByMint(positionMint);
168113
const tokenAAfter = await fetchToken(rpc, ataA);
@@ -191,7 +136,7 @@ describe("e2e", () => {
191136
positionMint,
192137
{ liquidity: 10000n },
193138
);
194-
await sendTransaction(instructions, payer);
139+
await sendTransaction(instructions);
195140

196141
const positionAfter = await fetchPositionByMint(positionMint);
197142
const tokenAAfter = await fetchToken(rpc, ataA);
@@ -218,7 +163,7 @@ describe("e2e", () => {
218163
rpc,
219164
positionMint,
220165
);
221-
await sendTransaction(instructions, payer);
166+
await sendTransaction(instructions);
222167

223168
const tokenAAfter = await fetchToken(rpc, ataA);
224169
const tokenBAfter = await fetchToken(rpc, ataB);
@@ -241,7 +186,7 @@ describe("e2e", () => {
241186
positionMint,
242187
{ liquidity: 1000000000n },
243188
);
244-
await sendTransaction(instructions, payer);
189+
await sendTransaction(instructions);
245190

246191
const positionAfter = await rpc.getMultipleAccounts([positionMint]).send();
247192
const tokenAAfter = await fetchToken(rpc, ataA);
@@ -263,10 +208,10 @@ describe("e2e", () => {
263208

264209
const { instructions, quote } = await swapInstructions(
265210
rpc,
266-
{ inputAmount: 100n, mint: TOKEN_MINT_1 },
211+
{ inputAmount: 100n, mint: mintA },
267212
poolAddress,
268213
);
269-
await sendTransaction(instructions, payer);
214+
await sendTransaction(instructions);
270215

271216
const tokenAAfter = await fetchToken(rpc, ataA);
272217
const tokenBAfter = await fetchToken(rpc, ataB);
@@ -287,10 +232,10 @@ describe("e2e", () => {
287232

288233
const { instructions, quote } = await swapInstructions(
289234
rpc,
290-
{ outputAmount: 100n, mint: TOKEN_MINT_1 },
235+
{ outputAmount: 100n, mint: mintA },
291236
poolAddress,
292237
);
293-
await sendTransaction(instructions, payer);
238+
await sendTransaction(instructions);
294239

295240
const tokenAAfter = await fetchToken(rpc, ataA);
296241
const tokenBAfter = await fetchToken(rpc, ataB);
@@ -311,10 +256,10 @@ describe("e2e", () => {
311256

312257
const { instructions, quote } = await swapInstructions(
313258
rpc,
314-
{ inputAmount: 100000n, mint: TOKEN_MINT_2 },
259+
{ inputAmount: 100000n, mint: mintB },
315260
poolAddress,
316261
);
317-
await sendTransaction(instructions, payer);
262+
await sendTransaction(instructions);
318263

319264
const tokenAAfter = await fetchToken(rpc, ataA);
320265
const tokenBAfter = await fetchToken(rpc, ataB);
@@ -335,10 +280,10 @@ describe("e2e", () => {
335280

336281
const { instructions, quote } = await swapInstructions(
337282
rpc,
338-
{ outputAmount: 100000n, mint: TOKEN_MINT_2 },
283+
{ outputAmount: 100000n, mint: mintB },
339284
poolAddress,
340285
);
341-
await sendTransaction(instructions, payer);
286+
await sendTransaction(instructions);
342287

343288
const tokenAAfter = await fetchToken(rpc, ataA);
344289
const tokenBAfter = await fetchToken(rpc, ataB);
Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,89 @@
1-
import { describe } from "vitest";
1+
import { describe, it, beforeAll } from "vitest";
2+
import {
3+
fetchConcentratedLiquidityPool,
4+
fetchSplashPool,
5+
fetchWhirlpoolsByTokenPair,
6+
} from "../src/pool";
7+
import { rpc } from "./utils/mockRpc";
8+
import assert from "assert";
9+
import {
10+
SPLASH_POOL_TICK_SPACING,
11+
WHIRLPOOLS_CONFIG_ADDRESS,
12+
} from "../src/config";
13+
import type { Address } from "@solana/web3.js";
14+
import { setupMint } from "./utils/token";
15+
import { setupWhirlpool } from "./utils/program";
16+
import { getWhirlpoolAddress } from "@orca-so/whirlpools-client";
17+
import { orderMints } from "../src/token";
218

3-
describe.skip("Fetch Pool", () => {
4-
// TODO: <-
19+
describe("Fetch Pool", () => {
20+
let mintA: Address;
21+
let mintB: Address;
22+
let defaultPool: Address;
23+
let concentratedPool: Address;
24+
let splashPool: Address;
25+
26+
beforeAll(async () => {
27+
const mint1 = await setupMint();
28+
const mint2 = await setupMint();
29+
[mintA, mintB] = orderMints(mint1, mint2);
30+
concentratedPool = await setupWhirlpool(mintA, mintB, 64);
31+
defaultPool = await getWhirlpoolAddress(
32+
WHIRLPOOLS_CONFIG_ADDRESS,
33+
mintA,
34+
mintB,
35+
128,
36+
).then((x) => x[0]);
37+
splashPool = await setupWhirlpool(mintA, mintB, SPLASH_POOL_TICK_SPACING);
38+
});
39+
40+
it("Should be able to fetch a splash pool", async () => {
41+
const pool = await fetchSplashPool(rpc, mintA, mintB);
42+
assert.strictEqual(pool.initialized, true);
43+
assert.strictEqual(pool.liquidity, 0n);
44+
assert.strictEqual(pool.tickSpacing, SPLASH_POOL_TICK_SPACING);
45+
assert.strictEqual(pool.address, splashPool);
46+
assert.strictEqual(pool.tokenMintA, mintA);
47+
assert.strictEqual(pool.tokenMintB, mintB);
48+
assert.strictEqual(pool.feeRate, 1000);
49+
assert.strictEqual(pool.protocolFeeRate, 100);
50+
assert.strictEqual(pool.whirlpoolsConfig, WHIRLPOOLS_CONFIG_ADDRESS);
51+
});
52+
53+
it("Should be able to fetch a concentrated liquidity pool", async () => {
54+
const pool = await fetchConcentratedLiquidityPool(rpc, mintA, mintB, 64);
55+
assert.strictEqual(pool.initialized, true);
56+
assert.strictEqual(pool.liquidity, 0n);
57+
assert.strictEqual(pool.tickSpacing, 64);
58+
assert.strictEqual(pool.address, concentratedPool);
59+
assert.strictEqual(pool.tokenMintA, mintA);
60+
assert.strictEqual(pool.tokenMintB, mintB);
61+
assert.strictEqual(pool.feeRate, 300);
62+
assert.strictEqual(pool.protocolFeeRate, 100);
63+
assert.strictEqual(pool.whirlpoolsConfig, WHIRLPOOLS_CONFIG_ADDRESS);
64+
});
65+
66+
it("Should be able to try fetching a non-existent pool", async () => {
67+
const pool = await fetchConcentratedLiquidityPool(rpc, mintA, mintB, 128);
68+
assert.strictEqual(pool.initialized, false);
69+
assert.strictEqual(pool.tickSpacing, 128);
70+
assert.strictEqual(pool.address, defaultPool);
71+
assert.strictEqual(pool.tokenMintA, mintA);
72+
assert.strictEqual(pool.tokenMintB, mintB);
73+
assert.strictEqual(pool.feeRate, 1000);
74+
assert.strictEqual(pool.protocolFeeRate, 100);
75+
assert.strictEqual(pool.whirlpoolsConfig, WHIRLPOOLS_CONFIG_ADDRESS);
76+
});
77+
78+
// TODO: Enable this test once solana-bankrun exposes getProgramAccounts
79+
it.skip("Should be able to fetch all pools for a pair", async () => {
80+
const pools = await fetchWhirlpoolsByTokenPair(rpc, mintA, mintB);
81+
assert.strictEqual(pools.length, 3);
82+
assert.strictEqual(pools[0].initialized, true);
83+
assert.strictEqual(pools[0].tickSpacing, 64);
84+
assert.strictEqual(pools[1].initialized, true);
85+
assert.strictEqual(pools[1].tickSpacing, SPLASH_POOL_TICK_SPACING);
86+
assert.strictEqual(pools[2].initialized, false);
87+
assert.strictEqual(pools[2].tickSpacing, 128);
88+
});
589
});
Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
1-
import { describe } from "vitest";
1+
import type { Address} from "@solana/web3.js";
2+
import { generateKeyPairSigner } from "@solana/web3.js";
3+
import { assert, beforeAll, describe, it } from "vitest";
4+
import { setupAta, setupMint } from "./utils/token";
5+
import {
6+
setupPosition,
7+
setupPositionBundle,
8+
setupTEPosition,
9+
setupWhirlpool,
10+
} from "./utils/program";
11+
import { SPLASH_POOL_TICK_SPACING } from "../src/config";
12+
import { fetchPositionsForOwner } from "../src/position";
13+
import { rpc, signer } from "./utils/mockRpc";
14+
import { orderMints } from "../src/token";
215

3-
describe.skip("Fetch Position", () => {
4-
// TODO: <-
16+
describe("Fetch Position", () => {
17+
let mintA: Address;
18+
let mintB: Address;
19+
let pool: Address;
20+
let splashPool: Address;
21+
22+
beforeAll(async () => {
23+
const mint1 = await setupMint();
24+
const mint2 = await setupMint();
25+
[mintA, mintB] = orderMints(mint1, mint2);
26+
await setupAta(mintA, { amount: 500e9 });
27+
await setupAta(mintB, { amount: 500e9 });
28+
pool = await setupWhirlpool(mintA, mintB, 128);
29+
splashPool = await setupWhirlpool(mintA, mintB, SPLASH_POOL_TICK_SPACING);
30+
await setupPosition(pool);
31+
await setupPosition(splashPool);
32+
await setupTEPosition(pool);
33+
await setupPositionBundle(pool);
34+
await setupPositionBundle(splashPool, [{}, {}]);
35+
});
36+
37+
// TODO: enable this when solana-bankrun supports gpa
38+
it.skip("Should fetch all positions for an address", async () => {
39+
const positions = await fetchPositionsForOwner(rpc, signer.address);
40+
assert.strictEqual(positions.length, 5);
41+
});
42+
43+
// TODO: enable this when solana-bankrun supports gpa
44+
it.skip("Should fetch no positions for a different address", async () => {
45+
const other = await generateKeyPairSigner();
46+
const positions = await fetchPositionsForOwner(rpc, other.address);
47+
assert.strictEqual(positions.length, 0);
48+
});
549
});

0 commit comments

Comments
 (0)