11import { describe , expect , it } from 'vitest' ;
2- import { Keypair } from '@solana/web3.js' ;
2+ import { Keypair , SystemProgram } from '@solana/web3.js' ;
33import { Buffer } from 'buffer' ;
44import { LIGHT_TOKEN_PROGRAM_ID , bn } from '@lightprotocol/stateless.js' ;
5- import { TOKEN_PROGRAM_ID } from '@solana/spl-token' ;
5+ import { TOKEN_PROGRAM_ID , TOKEN_2022_PROGRAM_ID } from '@solana/spl-token' ;
66import {
77 createApproveInstruction ,
88 createAtaInstruction ,
@@ -14,7 +14,13 @@ import {
1414 createThawInstruction ,
1515 createTransferCheckedInstruction ,
1616 createDecompressInstruction ,
17+ createSplInterfaceInstruction ,
1718} from '../../src/instructions' ;
19+ import {
20+ COMPRESSED_TOKEN_PROGRAM_ID ,
21+ deriveCpiAuthorityPda ,
22+ deriveSplInterfacePdaWithIndex ,
23+ } from '../../src/constants' ;
1824
1925describe ( 'instruction builders' , ( ) => {
2026 function buildParsedCompressedAccount ( params ?: {
@@ -349,4 +355,72 @@ describe('instruction builders', () => {
349355 expect ( instruction . keys [ 2 ] . pubkey . equals ( owner ) ) . toBe ( true ) ;
350356 expect ( instruction . keys [ 2 ] . isSigner ) . toBe ( true ) ;
351357 } ) ;
358+
359+ it ( 'creates spl interface instruction with required index' , ( ) => {
360+ const feePayer = Keypair . generate ( ) . publicKey ;
361+ const mint = Keypair . generate ( ) . publicKey ;
362+ const index = 0 ;
363+
364+ const instruction = createSplInterfaceInstruction ( {
365+ feePayer,
366+ mint,
367+ index,
368+ } ) ;
369+
370+ const [ splInterfacePda ] = deriveSplInterfacePdaWithIndex ( mint , index ) ;
371+
372+ expect ( instruction . programId . equals ( COMPRESSED_TOKEN_PROGRAM_ID ) ) . toBe (
373+ true ,
374+ ) ;
375+ expect (
376+ instruction . data . equals (
377+ Buffer . from ( [ 23 , 169 , 27 , 122 , 147 , 169 , 209 , 152 ] ) ,
378+ ) ,
379+ ) . toBe ( true ) ;
380+ expect ( instruction . keys ) . toHaveLength ( 6 ) ;
381+ expect ( instruction . keys [ 0 ] . pubkey . equals ( feePayer ) ) . toBe ( true ) ;
382+ expect ( instruction . keys [ 0 ] . isSigner ) . toBe ( true ) ;
383+ expect ( instruction . keys [ 0 ] . isWritable ) . toBe ( true ) ;
384+ expect ( instruction . keys [ 1 ] . pubkey . equals ( splInterfacePda ) ) . toBe ( true ) ;
385+ expect ( instruction . keys [ 1 ] . isWritable ) . toBe ( true ) ;
386+ expect ( instruction . keys [ 2 ] . pubkey . equals ( SystemProgram . programId ) ) . toBe (
387+ true ,
388+ ) ;
389+ expect ( instruction . keys [ 3 ] . pubkey . equals ( mint ) ) . toBe ( true ) ;
390+ expect ( instruction . keys [ 3 ] . isWritable ) . toBe ( true ) ;
391+ expect ( instruction . keys [ 4 ] . pubkey . equals ( TOKEN_PROGRAM_ID ) ) . toBe ( true ) ;
392+ expect ( instruction . keys [ 5 ] . pubkey . equals ( deriveCpiAuthorityPda ( ) ) ) . toBe (
393+ true ,
394+ ) ;
395+ } ) ;
396+
397+ it ( 'creates spl interface instruction with custom token program' , ( ) => {
398+ const feePayer = Keypair . generate ( ) . publicKey ;
399+ const mint = Keypair . generate ( ) . publicKey ;
400+ const index = 1 ;
401+
402+ const instruction = createSplInterfaceInstruction ( {
403+ feePayer,
404+ mint,
405+ index,
406+ tokenProgramId : TOKEN_2022_PROGRAM_ID ,
407+ } ) ;
408+
409+ expect (
410+ instruction . keys [ 4 ] . pubkey . equals ( TOKEN_2022_PROGRAM_ID ) ,
411+ ) . toBe ( true ) ;
412+ } ) ;
413+
414+ it ( 'throws when spl interface index is out of range' , ( ) => {
415+ const feePayer = Keypair . generate ( ) . publicKey ;
416+ const mint = Keypair . generate ( ) . publicKey ;
417+
418+ expect ( ( ) =>
419+ createSplInterfaceInstruction ( {
420+ feePayer,
421+ mint,
422+ index : 256 ,
423+ } ) ,
424+ ) . toThrow ( / i n t e g e r i n \[ 0 , 2 5 5 \] / i) ;
425+ } ) ;
352426} ) ;
0 commit comments