11import * as anchor from "@coral-xyz/anchor" ;
22import { BN } from "@coral-xyz/anchor" ;
33import * 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" ;
105import { systemTransferTx , TickSpacing } from "../../../utils" ;
11- import { defaultConfirmOptions } from "../../../utils/const " ;
6+ import { startLiteSVM , createLiteSVMProvider } from "../../../utils/litesvm " ;
127import { WhirlpoolTestFixture } from "../../../utils/fixture" ;
138import { Keypair , LAMPORTS_PER_SOL } from "@solana/web3.js" ;
149import type { PublicKey } from "@solana/web3.js" ;
@@ -22,23 +17,35 @@ import { PositionBundleUtil } from "../../../../dist/utils/public/position-bundl
2217import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet" ;
2318
2419describe ( "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} ) ;
0 commit comments