@@ -27,6 +27,7 @@ use solana_program::{
27
27
pubkey:: Pubkey ,
28
28
} ;
29
29
use spl_token_2022:: {
30
+ check_spl_token_program_account,
30
31
error:: TokenError ,
31
32
extension:: StateWithExtensions ,
32
33
state:: { Account , Mint } ,
@@ -41,7 +42,9 @@ impl Processor {
41
42
account_info : & AccountInfo ,
42
43
token_program_id : & Pubkey ,
43
44
) -> Result < Account , SwapError > {
44
- if account_info. owner != token_program_id {
45
+ if account_info. owner != token_program_id
46
+ && check_spl_token_program_account ( account_info. owner ) . is_err ( )
47
+ {
45
48
Err ( SwapError :: IncorrectTokenProgramId )
46
49
} else {
47
50
StateWithExtensions :: < Account > :: unpack ( & account_info. data . borrow ( ) )
@@ -55,7 +58,9 @@ impl Processor {
55
58
account_info : & AccountInfo ,
56
59
token_program_id : & Pubkey ,
57
60
) -> Result < Mint , SwapError > {
58
- if account_info. owner != token_program_id {
61
+ if account_info. owner != token_program_id
62
+ && check_spl_token_program_account ( account_info. owner ) . is_err ( )
63
+ {
59
64
Err ( SwapError :: IncorrectTokenProgramId )
60
65
} else {
61
66
StateWithExtensions :: < Mint > :: unpack ( & account_info. data . borrow ( ) )
@@ -171,7 +176,7 @@ impl Processor {
171
176
token_a_info : & AccountInfo ,
172
177
token_b_info : & AccountInfo ,
173
178
pool_mint_info : & AccountInfo ,
174
- token_program_info : & AccountInfo ,
179
+ pool_token_program_info : & AccountInfo ,
175
180
user_token_a_info : Option < & AccountInfo > ,
176
181
user_token_b_info : Option < & AccountInfo > ,
177
182
pool_fee_account_info : Option < & AccountInfo > ,
@@ -193,7 +198,7 @@ impl Processor {
193
198
if * pool_mint_info. key != * token_swap. pool_mint ( ) {
194
199
return Err ( SwapError :: IncorrectPoolMint . into ( ) ) ;
195
200
}
196
- if * token_program_info . key != * token_swap. token_program_id ( ) {
201
+ if * pool_token_program_info . key != * token_swap. token_program_id ( ) {
197
202
return Err ( SwapError :: IncorrectTokenProgramId . into ( ) ) ;
198
203
}
199
204
if let Some ( user_token_a_info) = user_token_a_info {
@@ -433,7 +438,7 @@ impl Processor {
433
438
434
439
Self :: token_transfer (
435
440
swap_info. key ,
436
- pool_token_program_info . clone ( ) ,
441
+ source_token_program_info . clone ( ) ,
437
442
source_info. clone ( ) ,
438
443
swap_source_info. clone ( ) ,
439
444
user_transfer_authority_info. clone ( ) ,
@@ -495,7 +500,7 @@ impl Processor {
495
500
496
501
Self :: token_transfer (
497
502
swap_info. key ,
498
- pool_token_program_info . clone ( ) ,
503
+ destination_token_program_info . clone ( ) ,
499
504
swap_destination_info. clone ( ) ,
500
505
destination_info. clone ( ) ,
501
506
authority_info. clone ( ) ,
@@ -585,7 +590,7 @@ impl Processor {
585
590
586
591
Self :: token_transfer (
587
592
swap_info. key ,
588
- pool_token_program_info . clone ( ) ,
593
+ token_a_program_info . clone ( ) ,
589
594
source_a_info. clone ( ) ,
590
595
token_a_info. clone ( ) ,
591
596
user_transfer_authority_info. clone ( ) ,
@@ -594,7 +599,7 @@ impl Processor {
594
599
) ?;
595
600
Self :: token_transfer (
596
601
swap_info. key ,
597
- pool_token_program_info . clone ( ) ,
602
+ token_b_program_info . clone ( ) ,
598
603
source_b_info. clone ( ) ,
599
604
token_b_info. clone ( ) ,
600
605
user_transfer_authority_info. clone ( ) ,
@@ -721,7 +726,7 @@ impl Processor {
721
726
if token_a_amount > 0 {
722
727
Self :: token_transfer (
723
728
swap_info. key ,
724
- pool_token_program_info . clone ( ) ,
729
+ token_a_program_info . clone ( ) ,
725
730
token_a_info. clone ( ) ,
726
731
dest_token_a_info. clone ( ) ,
727
732
authority_info. clone ( ) ,
@@ -732,7 +737,7 @@ impl Processor {
732
737
if token_b_amount > 0 {
733
738
Self :: token_transfer (
734
739
swap_info. key ,
735
- pool_token_program_info . clone ( ) ,
740
+ token_b_program_info . clone ( ) ,
736
741
token_b_info. clone ( ) ,
737
742
dest_token_b_info. clone ( ) ,
738
743
authority_info. clone ( ) ,
@@ -831,7 +836,7 @@ impl Processor {
831
836
TradeDirection :: AtoB => {
832
837
Self :: token_transfer (
833
838
swap_info. key ,
834
- pool_token_program_info . clone ( ) ,
839
+ source_token_program_info . clone ( ) ,
835
840
source_info. clone ( ) ,
836
841
swap_token_a_info. clone ( ) ,
837
842
user_transfer_authority_info. clone ( ) ,
@@ -842,7 +847,7 @@ impl Processor {
842
847
TradeDirection :: BtoA => {
843
848
Self :: token_transfer (
844
849
swap_info. key ,
845
- pool_token_program_info . clone ( ) ,
850
+ source_token_program_info . clone ( ) ,
846
851
source_info. clone ( ) ,
847
852
swap_token_b_info. clone ( ) ,
848
853
user_transfer_authority_info. clone ( ) ,
@@ -980,7 +985,7 @@ impl Processor {
980
985
TradeDirection :: AtoB => {
981
986
Self :: token_transfer (
982
987
swap_info. key ,
983
- pool_token_program_info . clone ( ) ,
988
+ destination_token_program_info . clone ( ) ,
984
989
swap_token_a_info. clone ( ) ,
985
990
destination_info. clone ( ) ,
986
991
authority_info. clone ( ) ,
@@ -991,7 +996,7 @@ impl Processor {
991
996
TradeDirection :: BtoA => {
992
997
Self :: token_transfer (
993
998
swap_info. key ,
994
- pool_token_program_info . clone ( ) ,
999
+ destination_token_program_info . clone ( ) ,
995
1000
swap_token_b_info. clone ( ) ,
996
1001
destination_info. clone ( ) ,
997
1002
authority_info. clone ( ) ,
@@ -1521,9 +1526,10 @@ mod tests {
1521
1526
maximum_token_b_amount : u64 ,
1522
1527
) -> ProgramResult {
1523
1528
let user_transfer_authority = Pubkey :: new_unique ( ) ;
1529
+ let token_a_program_id = depositor_token_a_account. owner ;
1524
1530
do_process_instruction (
1525
1531
approve (
1526
- & self . token_a_program_id ,
1532
+ & token_a_program_id,
1527
1533
depositor_token_a_key,
1528
1534
& user_transfer_authority,
1529
1535
depositor_key,
@@ -1539,9 +1545,10 @@ mod tests {
1539
1545
)
1540
1546
. unwrap ( ) ;
1541
1547
1548
+ let token_b_program_id = depositor_token_b_account. owner ;
1542
1549
do_process_instruction (
1543
1550
approve (
1544
- & self . token_b_program_id ,
1551
+ & token_b_program_id,
1545
1552
depositor_token_b_key,
1546
1553
& user_transfer_authority,
1547
1554
depositor_key,
@@ -1557,12 +1564,13 @@ mod tests {
1557
1564
)
1558
1565
. unwrap ( ) ;
1559
1566
1567
+ let pool_token_program_id = depositor_pool_account. owner ;
1560
1568
do_process_instruction (
1561
1569
deposit_all_token_types (
1562
1570
& SWAP_PROGRAM_ID ,
1563
- & self . token_a_program_id ,
1564
- & self . token_b_program_id ,
1565
- & self . pool_token_program_id ,
1571
+ & token_a_program_id,
1572
+ & token_b_program_id,
1573
+ & pool_token_program_id,
1566
1574
& self . swap_key ,
1567
1575
& self . authority_key ,
1568
1576
& user_transfer_authority,
@@ -1611,10 +1619,11 @@ mod tests {
1611
1619
minimum_token_b_amount : u64 ,
1612
1620
) -> ProgramResult {
1613
1621
let user_transfer_authority_key = Pubkey :: new_unique ( ) ;
1622
+ let pool_token_program_id = pool_account. owner ;
1614
1623
// approve user transfer authority to take out pool tokens
1615
1624
do_process_instruction (
1616
1625
approve (
1617
- & self . pool_token_program_id ,
1626
+ & pool_token_program_id,
1618
1627
pool_key,
1619
1628
& user_transfer_authority_key,
1620
1629
user_key,
@@ -1631,12 +1640,14 @@ mod tests {
1631
1640
. unwrap ( ) ;
1632
1641
1633
1642
// withdraw token a and b correctly
1643
+ let token_a_program_id = token_a_account. owner ;
1644
+ let token_b_program_id = token_b_account. owner ;
1634
1645
do_process_instruction (
1635
1646
withdraw_all_token_types (
1636
1647
& SWAP_PROGRAM_ID ,
1637
- & self . pool_token_program_id ,
1638
- & self . token_a_program_id ,
1639
- & self . token_b_program_id ,
1648
+ & pool_token_program_id,
1649
+ & token_a_program_id,
1650
+ & token_b_program_id,
1640
1651
& self . swap_key ,
1641
1652
& self . authority_key ,
1642
1653
& user_transfer_authority_key,
@@ -1703,11 +1714,12 @@ mod tests {
1703
1714
)
1704
1715
. unwrap ( ) ;
1705
1716
1717
+ let pool_token_program_id = deposit_pool_account. owner ;
1706
1718
do_process_instruction (
1707
1719
deposit_single_token_type_exact_amount_in (
1708
1720
& SWAP_PROGRAM_ID ,
1709
1721
& source_token_program_id,
1710
- & self . pool_token_program_id ,
1722
+ & pool_token_program_id,
1711
1723
& self . swap_key ,
1712
1724
& self . authority_key ,
1713
1725
& user_transfer_authority_key,
@@ -1749,10 +1761,11 @@ mod tests {
1749
1761
maximum_pool_token_amount : u64 ,
1750
1762
) -> ProgramResult {
1751
1763
let user_transfer_authority_key = Pubkey :: new_unique ( ) ;
1764
+ let pool_token_program_id = pool_account. owner ;
1752
1765
// approve user transfer authority to take out pool tokens
1753
1766
do_process_instruction (
1754
1767
approve (
1755
- & self . pool_token_program_id ,
1768
+ & pool_token_program_id,
1756
1769
pool_key,
1757
1770
& user_transfer_authority_key,
1758
1771
user_key,
@@ -1772,7 +1785,7 @@ mod tests {
1772
1785
do_process_instruction (
1773
1786
withdraw_single_token_type_exact_amount_out (
1774
1787
& SWAP_PROGRAM_ID ,
1775
- & self . pool_token_program_id ,
1788
+ & pool_token_program_id,
1776
1789
& destination_token_program_id,
1777
1790
& self . swap_key ,
1778
1791
& self . authority_key ,
@@ -2412,7 +2425,7 @@ mod tests {
2412
2425
// pool fee account has wrong mint
2413
2426
{
2414
2427
let ( _pool_fee_key, pool_fee_account) = mint_token (
2415
- & pool_token_program_id ,
2428
+ & token_a_program_id ,
2416
2429
& accounts. token_a_mint_key ,
2417
2430
& mut accounts. token_a_mint_account ,
2418
2431
& user_key,
@@ -2576,7 +2589,7 @@ mod tests {
2576
2589
{
2577
2590
let wrong_program_id = Pubkey :: new_unique ( ) ;
2578
2591
assert_eq ! (
2579
- Err ( SwapError :: IncorrectTokenProgramId . into ( ) ) ,
2592
+ Err ( ProgramError :: IncorrectProgramId ) ,
2580
2593
do_process_instruction(
2581
2594
initialize(
2582
2595
& SWAP_PROGRAM_ID ,
@@ -3231,8 +3244,13 @@ mod tests {
3231
3244
pool_key,
3232
3245
mut pool_account,
3233
3246
) = accounts. setup_token_accounts ( & user_key, & depositor_key, deposit_a, deposit_b, 0 ) ;
3247
+ let expected_error: ProgramError = if token_a_account. owner == token_b_account. owner {
3248
+ TokenError :: MintMismatch . into ( )
3249
+ } else {
3250
+ ProgramError :: InvalidAccountData
3251
+ } ;
3234
3252
assert_eq ! (
3235
- Err ( TokenError :: MintMismatch . into ( ) ) ,
3253
+ Err ( expected_error ) ,
3236
3254
accounts. deposit_all_token_types(
3237
3255
& depositor_key,
3238
3256
& token_b_key,
@@ -3264,10 +3282,15 @@ mod tests {
3264
3282
_token_b_key,
3265
3283
mut _token_b_account,
3266
3284
_pool_key,
3267
- mut _pool_account ,
3285
+ pool_account ,
3268
3286
) = accounts. setup_token_accounts ( & user_key, & depositor_key, deposit_a, deposit_b, 0 ) ;
3287
+ let expected_error: ProgramError = if token_a_account. owner == pool_account. owner {
3288
+ TokenError :: MintMismatch . into ( )
3289
+ } else {
3290
+ SwapError :: IncorrectTokenProgramId . into ( )
3291
+ } ;
3269
3292
assert_eq ! (
3270
- Err ( TokenError :: MintMismatch . into ( ) ) ,
3293
+ Err ( expected_error ) ,
3271
3294
accounts. deposit_all_token_types(
3272
3295
& depositor_key,
3273
3296
& token_a_key,
@@ -3841,8 +3864,13 @@ mod tests {
3841
3864
initial_b,
3842
3865
withdraw_amount. try_into ( ) . unwrap ( ) ,
3843
3866
) ;
3867
+ let expected_error: ProgramError = if token_a_account. owner == token_b_account. owner {
3868
+ TokenError :: MintMismatch . into ( )
3869
+ } else {
3870
+ ProgramError :: InvalidAccountData
3871
+ } ;
3844
3872
assert_eq ! (
3845
- Err ( TokenError :: MintMismatch . into ( ) ) ,
3873
+ Err ( expected_error ) ,
3846
3874
accounts. withdraw_all_token_types(
3847
3875
& withdrawer_key,
3848
3876
& pool_key,
@@ -3880,16 +3908,21 @@ mod tests {
3880
3908
_token_b_key,
3881
3909
_token_b_account,
3882
3910
_pool_key,
3883
- _pool_account ,
3911
+ pool_account ,
3884
3912
) = accounts. setup_token_accounts (
3885
3913
& user_key,
3886
3914
& withdrawer_key,
3887
3915
withdraw_amount. try_into ( ) . unwrap ( ) ,
3888
3916
initial_b,
3889
3917
withdraw_amount. try_into ( ) . unwrap ( ) ,
3890
3918
) ;
3919
+ let expected_error: ProgramError = if token_a_account. owner == pool_account. owner {
3920
+ TokenError :: MintMismatch . into ( )
3921
+ } else {
3922
+ SwapError :: IncorrectTokenProgramId . into ( )
3923
+ } ;
3891
3924
assert_eq ! (
3892
- Err ( TokenError :: MintMismatch . into ( ) ) ,
3925
+ Err ( expected_error ) ,
3893
3926
accounts. withdraw_all_token_types(
3894
3927
& withdrawer_key,
3895
3928
& wrong_token_a_key,
@@ -4661,10 +4694,15 @@ mod tests {
4661
4694
token_b_key,
4662
4695
mut token_b_account,
4663
4696
_pool_key,
4664
- mut _pool_account ,
4697
+ pool_account ,
4665
4698
) = accounts. setup_token_accounts ( & user_key, & depositor_key, deposit_a, deposit_b, 0 ) ;
4699
+ let expected_error: ProgramError = if token_b_account. owner == pool_account. owner {
4700
+ TokenError :: MintMismatch . into ( )
4701
+ } else {
4702
+ SwapError :: IncorrectTokenProgramId . into ( )
4703
+ } ;
4666
4704
assert_eq ! (
4667
- Err ( TokenError :: MintMismatch . into ( ) ) ,
4705
+ Err ( expected_error ) ,
4668
4706
accounts. deposit_single_token_type_exact_amount_in(
4669
4707
& depositor_key,
4670
4708
& token_a_key,
@@ -5184,16 +5222,21 @@ mod tests {
5184
5222
token_b_key,
5185
5223
mut token_b_account,
5186
5224
_pool_key,
5187
- _pool_account ,
5225
+ pool_account ,
5188
5226
) = accounts. setup_token_accounts (
5189
5227
& user_key,
5190
5228
& withdrawer_key,
5191
5229
maximum_pool_token_amount,
5192
5230
initial_b,
5193
5231
maximum_pool_token_amount,
5194
5232
) ;
5233
+ let expected_error: ProgramError = if token_a_account. owner == pool_account. owner {
5234
+ TokenError :: MintMismatch . into ( )
5235
+ } else {
5236
+ SwapError :: IncorrectTokenProgramId . into ( )
5237
+ } ;
5195
5238
assert_eq ! (
5196
- Err ( TokenError :: MintMismatch . into ( ) ) ,
5239
+ Err ( expected_error ) ,
5197
5240
accounts. withdraw_single_token_type_exact_amount_out(
5198
5241
& withdrawer_key,
5199
5242
& token_a_key,
@@ -6448,8 +6491,13 @@ mod tests {
6448
6491
_pool_key,
6449
6492
_pool_account,
6450
6493
) = accounts. setup_token_accounts ( & user_key, & swapper_key, initial_a, initial_b, 0 ) ;
6494
+ let expected_error: ProgramError = if token_a_account. owner == token_b_account. owner {
6495
+ TokenError :: MintMismatch . into ( )
6496
+ } else {
6497
+ ProgramError :: IncorrectProgramId
6498
+ } ;
6451
6499
assert_eq ! (
6452
- Err ( TokenError :: MintMismatch . into ( ) ) ,
6500
+ Err ( expected_error ) ,
6453
6501
accounts. swap(
6454
6502
& swapper_key,
6455
6503
& token_b_key,
0 commit comments