@@ -42,6 +42,8 @@ class SwapInstructionsTest {
4242 private val mockSwapAuthority = generateRandomPublicKeyForTest() // The temporary swap authority
4343 private val mockRecentBlockhash = generateRandomPublicKeyForTest()
4444
45+ private val mockFeeDestination = generateRandomPublicKeyForTest()
46+
4547 // Mock VMs
4648 private val coreVmMetadata = VmMetadata (
4749 authority = vmAuthority,
@@ -348,19 +350,23 @@ class SwapInstructionsTest {
348350 seed = mockSeed,
349351 sellFeeBps = 100 ,
350352 vmLockDurationInDays = 21 ,
351- alts = emptyList()
353+ alts = emptyList(),
354+ feeDestination = mockFeeDestination
352355 )
353356
354357 @Test
355358 fun testBuildNewCurrencyBuyInstructionsCount () {
356- val amount = 100_000L
359+ val totalAmount = 100_000L
360+ val fee = 5_000L
361+ val amount = totalAmount - fee
357362
358363 val instructions = buildNewCurrencyBuyInstructions(
359364 serverParameters = mockNewCurrencyServerParams,
360365 nonce = mockNonce,
361366 authority = mockNewCurrencyAuthority,
362367 coreMintMetadata = coreMint,
363368 amount = amount,
369+ feeAmount = fee,
364370 )
365371
366372 // Expected Sequence (11 instructions):
@@ -381,14 +387,17 @@ class SwapInstructionsTest {
381387
382388 @Test
383389 fun testBuildNewCurrencyBuyInstructionPrograms () {
384- val amount = 100_000L
390+ val totalAmount = 100_000L
391+ val fee = 5_000L
392+ val amount = totalAmount - fee
385393
386394 val instructions = buildNewCurrencyBuyInstructions(
387395 serverParameters = mockNewCurrencyServerParams,
388396 nonce = mockNonce,
389397 authority = mockNewCurrencyAuthority,
390398 coreMintMetadata = coreMint,
391399 amount = amount,
400+ feeAmount = fee,
392401 )
393402
394403 // 1. System::AdvanceNonce
@@ -427,14 +436,17 @@ class SwapInstructionsTest {
427436
428437 @Test
429438 fun testBuildNewCurrencyBuyInstructionAccounts () {
430- val amount = 100_000L
439+ val totalAmount = 100_000L
440+ val fee = 5_000L
441+ val amount = totalAmount - fee
431442
432443 val instructions = buildNewCurrencyBuyInstructions(
433444 serverParameters = mockNewCurrencyServerParams,
434445 nonce = mockNonce,
435446 authority = mockNewCurrencyAuthority,
436447 coreMintMetadata = coreMint,
437448 amount = amount,
449+ feeAmount = fee,
438450 )
439451
440452 // Derive expected PDAs
@@ -494,9 +506,13 @@ class SwapInstructionsTest {
494506 assertEquals(expectedDepositAta, instructions[7 ].accounts[1 ].publicKey)
495507 assertEquals(derivedTargetMint, instructions[7 ].accounts[3 ].publicKey)
496508
497- // 9. VM::TransferForSwap : vmAuthority at 0 matches core VM authority
509+ // 9. VM::TransferForSwapWithFee : vmAuthority at 0 matches core VM authority
498510 assertEquals(coreMint.vmMetadata.authority, instructions[8 ].accounts[0 ].publicKey)
499511 assertEquals(coreMint.vmMetadata.vm, instructions[8 ].accounts[1 ].publicKey)
512+ assertEquals(8 , instructions[8 ].accounts.size)
513+ // feeDestination at index 6
514+ assertEquals(mockFeeDestination, instructions[8 ].accounts[6 ].publicKey)
515+ assertTrue(instructions[8 ].accounts[6 ].isWritable)
500516
501517 // 10. Reserve::BuyTokens: buyer at 0 is authority
502518 assertEquals(mockNewCurrencyAuthority, instructions[9 ].accounts[0 ].publicKey)
@@ -523,14 +539,17 @@ class SwapInstructionsTest {
523539
524540 @Test
525541 fun testBuildNewCurrencyBuyInstructionInitializeCurrencyData () {
526- val amount = 100_000L
542+ val totalAmount = 100_000L
543+ val fee = 5_000L
544+ val amount = totalAmount - fee
527545
528546 val instructions = buildNewCurrencyBuyInstructions(
529547 serverParameters = mockNewCurrencyServerParams,
530548 nonce = mockNonce,
531549 authority = mockNewCurrencyAuthority,
532550 coreMintMetadata = coreMint,
533551 amount = amount,
552+ feeAmount = fee,
534553 )
535554
536555 // InitializeCurrency data: command(1) + name(32) + symbol(8) + seed(32) + bump(1) + mintBump(1) + padding(6) = 81 bytes
@@ -561,14 +580,17 @@ class SwapInstructionsTest {
561580
562581 @Test
563582 fun testBuildNewCurrencyBuyInstructionInitializePoolData () {
564- val amount = 100_000L
583+ val totalAmount = 100_000L
584+ val fee = 5_000L
585+ val amount = totalAmount - fee
565586
566587 val instructions = buildNewCurrencyBuyInstructions(
567588 serverParameters = mockNewCurrencyServerParams,
568589 nonce = mockNonce,
569590 authority = mockNewCurrencyAuthority,
570591 coreMintMetadata = coreMint,
571592 amount = amount,
593+ feeAmount = fee,
572594 )
573595
574596 // InitializePool data: command(1) + sellFee(2) + bump(1) + vaultTargetBump(1) + vaultBaseBump(1) + padding(1) = 7 bytes
@@ -583,14 +605,17 @@ class SwapInstructionsTest {
583605
584606 @Test
585607 fun testBuildNewCurrencyBuyInstructionInitVmData () {
586- val amount = 100_000L
608+ val totalAmount = 100_000L
609+ val fee = 5_000L
610+ val amount = totalAmount - fee
587611
588612 val instructions = buildNewCurrencyBuyInstructions(
589613 serverParameters = mockNewCurrencyServerParams,
590614 nonce = mockNonce,
591615 authority = mockNewCurrencyAuthority,
592616 coreMintMetadata = coreMint,
593617 amount = amount,
618+ feeAmount = fee,
594619 )
595620
596621 // InitVm data: command(1) + lockDuration(1) + vmBump(1) + vmOmnibusBump(1) = 4 bytes
@@ -602,24 +627,27 @@ class SwapInstructionsTest {
602627
603628 @Test
604629 fun testBuildNewCurrencyBuyInstructionBuyTokensData () {
605- val amount = 100_000L
630+ val totalAmount = 100_000L
631+ val fee = 5_000L
632+ val amount = totalAmount - fee
606633
607634 val instructions = buildNewCurrencyBuyInstructions(
608635 serverParameters = mockNewCurrencyServerParams,
609636 nonce = mockNonce,
610637 authority = mockNewCurrencyAuthority,
611638 coreMintMetadata = coreMint,
612639 amount = amount,
640+ feeAmount = fee,
613641 )
614642
615643 // BuyTokens data: command(1) + inAmount(8) + minOutAmount(8) = 17 bytes
616644 val buyData = instructions[9 ].data
617645 assertEquals(17 , buyData.size)
618646 assertEquals(CurrencyCreatorProgram .Command .buyTokens.value, buyData[0 ])
619647
620- // inAmount = 100_000 = 0xA0860100_00000000 in LE
621- assertEquals(0xA0 .toByte(), buyData[1 ])
622- assertEquals(0x86 .toByte(), buyData[2 ])
648+ // inAmount = 95_000 = 0x18730100_00000000 in LE
649+ assertEquals(0x18 .toByte(), buyData[1 ])
650+ assertEquals(0x73 .toByte(), buyData[2 ])
623651 assertEquals(0x01 .toByte(), buyData[3 ])
624652 assertEquals(0x00 .toByte(), buyData[4 ])
625653
@@ -629,16 +657,56 @@ class SwapInstructionsTest {
629657 }
630658 }
631659
660+ @Test
661+ fun testBuildNewCurrencyBuyTransferForSwapWithFeeData () {
662+ val totalAmount = 100_000L
663+ val fee = 5_000L
664+ val amount = totalAmount - fee
665+
666+ val instructions = buildNewCurrencyBuyInstructions(
667+ serverParameters = mockNewCurrencyServerParams,
668+ nonce = mockNonce,
669+ authority = mockNewCurrencyAuthority,
670+ coreMintMetadata = coreMint,
671+ amount = amount,
672+ feeAmount = fee,
673+ )
674+
675+ // TransferForSwapWithFee data: command(1) + swapAmount(8) + feeAmount(8) + bump(1) = 18 bytes
676+ val transferData = instructions[8 ].data
677+ assertEquals(18 , transferData.size)
678+
679+ // Command byte is transferForSwap (17)
680+ assertEquals(VirtualMachineProgram .Command .transferForSwap.value, transferData[0 ])
681+
682+ // swapAmount = 95_000 at bytes [1..8] in LE
683+ assertEquals(0x18 .toByte(), transferData[1 ])
684+ assertEquals(0x73 .toByte(), transferData[2 ])
685+ assertEquals(0x01 .toByte(), transferData[3 ])
686+ for (i in 4 .. 8 ) assertEquals(0x00 .toByte(), transferData[i])
687+
688+ // feeAmount = 5_000 at bytes [9..16] in LE
689+ assertEquals(0x88 .toByte(), transferData[9 ])
690+ assertEquals(0x13 .toByte(), transferData[10 ])
691+ for (i in 11 .. 16 ) assertEquals(0x00 .toByte(), transferData[i])
692+
693+ // bump is last byte
694+ assertTrue(transferData[17 ] in Byte .MIN_VALUE .. Byte .MAX_VALUE )
695+ }
696+
632697 @Test
633698 fun testNewCurrencyBuyInstructionAuthorityIsBuyer () {
634- val amount = 50_000L
699+ val totalAmount = 50_000L
700+ val fee = 5_000L
701+ val amount = totalAmount - fee
635702
636703 val instructions = buildNewCurrencyBuyInstructions(
637704 serverParameters = mockNewCurrencyServerParams,
638705 nonce = mockNonce,
639706 authority = mockNewCurrencyAuthority,
640707 coreMintMetadata = coreMint,
641708 amount = amount,
709+ feeAmount = fee,
642710 )
643711
644712 // In the new currency flow, authority == buyer == swapAuthority
0 commit comments