@kamino-finance/klend-sdk 5.11.5 → 5.11.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,6 @@ const types_1 = require("../idl_codegen/types");
16
16
  const farms_sdk_1 = require("@kamino-finance/farms-sdk");
17
17
  const ObligationType_1 = require("../utils/ObligationType");
18
18
  const lib_1 = require("../lib");
19
- const SOL_PADDING_FOR_INTEREST = new bn_js_1.default('1000000');
20
19
  class KaminoAction {
21
20
  kaminoMarket;
22
21
  reserve;
@@ -25,9 +24,6 @@ class KaminoAction {
25
24
  payer;
26
25
  obligation = null;
27
26
  referrer;
28
- userTokenAccountAddress;
29
- userCollateralAccountAddress;
30
- additionalTokenAccountAddress;
31
27
  /**
32
28
  * Null unless the obligation is not passed
33
29
  */
@@ -47,17 +43,13 @@ class KaminoAction {
47
43
  lendingIxsLabels;
48
44
  cleanupIxs;
49
45
  cleanupIxsLabels;
50
- preTxnIxs;
51
- preTxnIxsLabels;
52
- postTxnIxs;
53
- postTxnIxsLabels;
54
46
  refreshFarmsCleanupTxnIxs;
55
47
  refreshFarmsCleanupTxnIxsLabels;
56
48
  depositReserves;
57
49
  borrowReserves;
58
50
  preLoadedDepositReservesSameTx;
59
51
  currentSlot;
60
- constructor(kaminoMarket, owner, obligation, userTokenAccountAddress, userCollateralAccountAddress, mint, positions, amount, depositReserves, borrowReserves, reserveState, currentSlot, secondaryMint, additionalTokenAccountAddress, outflowReserveState, outflowAmount, referrer, payer) {
52
+ constructor(kaminoMarket, owner, obligation, mint, positions, amount, depositReserves, borrowReserves, reserveState, currentSlot, secondaryMint, outflowReserveState, outflowAmount, referrer, payer) {
61
53
  if (obligation instanceof obligation_1.KaminoObligation) {
62
54
  this.obligation = obligation;
63
55
  }
@@ -70,8 +62,6 @@ class KaminoAction {
70
62
  this.amount = new bn_js_1.default(amount);
71
63
  this.mint = mint;
72
64
  this.positions = positions;
73
- this.userTokenAccountAddress = userTokenAccountAddress;
74
- this.userCollateralAccountAddress = userCollateralAccountAddress;
75
65
  this.computeBudgetIxs = [];
76
66
  this.computeBudgetIxsLabels = [];
77
67
  this.setupIxs = [];
@@ -82,15 +72,10 @@ class KaminoAction {
82
72
  this.lendingIxsLabels = [];
83
73
  this.cleanupIxs = [];
84
74
  this.cleanupIxsLabels = [];
85
- this.preTxnIxs = [];
86
- this.preTxnIxsLabels = [];
87
- this.postTxnIxs = [];
88
- this.postTxnIxsLabels = [];
89
75
  this.refreshFarmsCleanupTxnIxs = [];
90
76
  this.refreshFarmsCleanupTxnIxsLabels = [];
91
77
  this.depositReserves = depositReserves;
92
78
  this.borrowReserves = borrowReserves;
93
- this.additionalTokenAccountAddress = additionalTokenAccountAddress;
94
79
  this.secondaryMint = secondaryMint;
95
80
  this.reserve = reserveState;
96
81
  this.outflowReserve = outflowReserveState;
@@ -104,10 +89,9 @@ class KaminoAction {
104
89
  if (reserve === undefined) {
105
90
  throw new Error(`Reserve ${mint} not found in market ${kaminoMarket.getAddress().toBase58()}`);
106
91
  }
107
- const { userTokenAccountAddress, userCollateralAccountAddress } = KaminoAction.getUserAccountAddresses(payer ?? owner, reserve.state);
108
92
  const { kaminoObligation, depositReserves, borrowReserves, distinctReserveCount } = await KaminoAction.loadObligation(action, kaminoMarket, owner, reserve.address, obligation);
109
93
  const referrerKey = await this.getReferrerKey(kaminoMarket, owner, kaminoObligation, referrer);
110
- return new KaminoAction(kaminoMarket, owner, kaminoObligation || obligation, userTokenAccountAddress, userCollateralAccountAddress, mint, distinctReserveCount, amount, depositReserves, borrowReserves, reserve, currentSlot, undefined, undefined, undefined, undefined, referrerKey, payer);
94
+ return new KaminoAction(kaminoMarket, owner, kaminoObligation || obligation, mint, distinctReserveCount, amount, depositReserves, borrowReserves, reserve, currentSlot, undefined, undefined, undefined, referrerKey, payer);
111
95
  }
112
96
  static getUserAccountAddresses(owner, reserve) {
113
97
  const userTokenAccountAddress = (0, utils_1.getAssociatedTokenAddress)(reserve.liquidity.mintPubkey, owner, true, reserve.liquidity.tokenProgram, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID);
@@ -431,8 +415,8 @@ class KaminoAction {
431
415
  }
432
416
  static async buildWithdrawReferrerFeeTxns(owner, tokenMint, kaminoMarket, currentSlot = 0) {
433
417
  const { axn, createAtaIxs } = await KaminoAction.initializeWithdrawReferrerFees(tokenMint, owner, kaminoMarket, currentSlot);
434
- axn.preTxnIxs.push(...createAtaIxs);
435
- axn.preTxnIxsLabels.push(`createAtasIxs[${axn.userTokenAccountAddress.toString()}]`);
418
+ axn.setupIxs.push(...createAtaIxs);
419
+ axn.setupIxsLabels.push(`createAtasIxs[${axn.owner.toString()}]`);
436
420
  if (!axn.referrer.equals(web3_js_1.PublicKey.default)) {
437
421
  const referrerTokenState = (0, utils_1.referrerTokenStatePda)(axn.referrer, axn.reserve.address, axn.kaminoMarket.programId)[0];
438
422
  const account = await axn.kaminoMarket.getConnection().getAccountInfo(referrerTokenState);
@@ -445,42 +429,24 @@ class KaminoAction {
445
429
  return axn;
446
430
  }
447
431
  async getTransactions() {
448
- const txns = {
449
- preLendingTxn: null,
450
- lendingTxn: null,
451
- postLendingTxn: null,
452
- };
453
- if (this.preTxnIxs.length) {
454
- txns.preLendingTxn = new web3_js_1.Transaction({
455
- feePayer: this.owner,
456
- recentBlockhash: (await this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
457
- }).add(...this.preTxnIxs);
458
- }
432
+ let txns;
459
433
  if (this.lendingIxs.length === 2) {
460
- txns.lendingTxn = new web3_js_1.Transaction({
434
+ txns = new web3_js_1.Transaction({
461
435
  feePayer: this.owner,
462
436
  recentBlockhash: (await this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
463
437
  }).add(...this.setupIxs, ...[this.lendingIxs[0]], ...this.inBetweenIxs, ...[this.lendingIxs[1]], ...this.cleanupIxs);
464
438
  }
465
439
  else {
466
- txns.lendingTxn = new web3_js_1.Transaction({
440
+ txns = new web3_js_1.Transaction({
467
441
  feePayer: this.owner,
468
442
  recentBlockhash: (await this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
469
443
  }).add(...this.setupIxs, ...this.lendingIxs, ...this.cleanupIxs);
470
444
  }
471
- if (this.postTxnIxs.length) {
472
- txns.postLendingTxn = new web3_js_1.Transaction({
473
- feePayer: this.owner,
474
- recentBlockhash: (await this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
475
- }).add(...this.postTxnIxs);
476
- }
477
445
  return txns;
478
446
  }
479
447
  async sendTransactions(sendTransaction) {
480
448
  const txns = await this.getTransactions();
481
- await this.sendSingleTransaction(txns.preLendingTxn, sendTransaction);
482
- const signature = await this.sendSingleTransaction(txns.lendingTxn, sendTransaction);
483
- await this.sendSingleTransaction(txns.postLendingTxn, sendTransaction);
449
+ const signature = await this.sendSingleTransaction(txns, sendTransaction);
484
450
  return signature;
485
451
  }
486
452
  async sendSingleTransaction(txn, sendTransaction) {
@@ -492,9 +458,7 @@ class KaminoAction {
492
458
  }
493
459
  async simulateTransactions(sendTransaction) {
494
460
  const txns = await this.getTransactions();
495
- await this.simulateSingleTransaction(txns.preLendingTxn, sendTransaction);
496
- const signature = await this.simulateSingleTransaction(txns.lendingTxn, sendTransaction);
497
- await this.simulateSingleTransaction(txns.postLendingTxn, sendTransaction);
461
+ const signature = await this.simulateSingleTransaction(txns, sendTransaction);
498
462
  return signature;
499
463
  }
500
464
  async simulateSingleTransaction(txn, sendTransaction) {
@@ -514,8 +478,8 @@ class KaminoAction {
514
478
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
515
479
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
516
480
  reserveCollateralMint: this.reserve.getCTokenMint(),
517
- userSourceLiquidity: this.userTokenAccountAddress,
518
- userDestinationCollateral: this.userCollateralAccountAddress,
481
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
482
+ userDestinationCollateral: this.getUserCollateralAccountAddress(this.reserve),
519
483
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
520
484
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
521
485
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -533,8 +497,8 @@ class KaminoAction {
533
497
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
534
498
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
535
499
  reserveCollateralMint: this.reserve.getCTokenMint(),
536
- userSourceCollateral: this.userCollateralAccountAddress,
537
- userDestinationLiquidity: this.userTokenAccountAddress,
500
+ userSourceCollateral: this.getUserCollateralAccountAddress(this.reserve),
501
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.reserve),
538
502
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
539
503
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
540
504
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -555,7 +519,7 @@ class KaminoAction {
555
519
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
556
520
  reserveCollateralMint: this.reserve.getCTokenMint(),
557
521
  reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
558
- userSourceLiquidity: this.userTokenAccountAddress,
522
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
559
523
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
560
524
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
561
525
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -586,7 +550,7 @@ class KaminoAction {
586
550
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
587
551
  reserveCollateralMint: this.reserve.getCTokenMint(),
588
552
  reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
589
- userSourceLiquidity: this.userTokenAccountAddress,
553
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
590
554
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
591
555
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
592
556
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -607,7 +571,7 @@ class KaminoAction {
607
571
  lendingMarket: this.kaminoMarket.getAddress(),
608
572
  depositReserve: this.reserve.address,
609
573
  reserveDestinationCollateral: this.reserve.state.collateral.supplyVault,
610
- userSourceCollateral: this.userCollateralAccountAddress,
574
+ userSourceCollateral: this.getUserCollateralAccountAddress(this.reserve),
611
575
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
612
576
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
613
577
  }, this.kaminoMarket.programId));
@@ -632,7 +596,7 @@ class KaminoAction {
632
596
  lendingMarket: this.kaminoMarket.getAddress(),
633
597
  depositReserve: this.reserve.address,
634
598
  reserveDestinationCollateral: this.reserve.state.collateral.supplyVault,
635
- userSourceCollateral: this.userCollateralAccountAddress,
599
+ userSourceCollateral: this.getUserCollateralAccountAddress(this.reserve),
636
600
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
637
601
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
638
602
  },
@@ -658,7 +622,7 @@ class KaminoAction {
658
622
  borrowReserve: this.reserve.address,
659
623
  borrowReserveLiquidityMint: this.reserve.getLiquidityMint(),
660
624
  reserveSourceLiquidity: this.reserve.state.liquidity.supplyVault,
661
- userDestinationLiquidity: this.userTokenAccountAddress,
625
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.reserve),
662
626
  borrowReserveLiquidityFeeReceiver: this.reserve.state.liquidity.feeVault,
663
627
  referrerTokenState: (0, utils_1.referrerTokenStatePda)(this.referrer, this.reserve.address, this.kaminoMarket.programId)[0],
664
628
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -696,7 +660,7 @@ class KaminoAction {
696
660
  borrowReserve: this.reserve.address,
697
661
  borrowReserveLiquidityMint: this.reserve.getLiquidityMint(),
698
662
  reserveSourceLiquidity: this.reserve.state.liquidity.supplyVault,
699
- userDestinationLiquidity: this.userTokenAccountAddress,
663
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.reserve),
700
664
  borrowReserveLiquidityFeeReceiver: this.reserve.state.liquidity.feeVault,
701
665
  referrerTokenState: (0, utils_1.referrerTokenStatePda)(this.referrer, this.reserve.address, this.kaminoMarket.programId)[0],
702
666
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -726,7 +690,7 @@ class KaminoAction {
726
690
  reserveCollateralMint: this.reserve.getCTokenMint(),
727
691
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
728
692
  reserveSourceCollateral: this.reserve.state.collateral.supplyVault,
729
- userDestinationLiquidity: this.userTokenAccountAddress,
693
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.reserve),
730
694
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
731
695
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
732
696
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -757,7 +721,7 @@ class KaminoAction {
757
721
  reserveCollateralMint: this.reserve.getCTokenMint(),
758
722
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
759
723
  reserveSourceCollateral: this.reserve.state.collateral.supplyVault,
760
- userDestinationLiquidity: this.userTokenAccountAddress,
724
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.reserve),
761
725
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
762
726
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
763
727
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -782,7 +746,7 @@ class KaminoAction {
782
746
  lendingMarket: this.kaminoMarket.getAddress(),
783
747
  repayReserve: this.reserve.address,
784
748
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
785
- userSourceLiquidity: this.userTokenAccountAddress,
749
+ userSourceLiquidity: this.getTokenAccountAddressByUser(this.reserve, this.payer),
786
750
  reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
787
751
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
788
752
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -815,7 +779,7 @@ class KaminoAction {
815
779
  lendingMarket: this.kaminoMarket.getAddress(),
816
780
  repayReserve: this.reserve.address,
817
781
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
818
- userSourceLiquidity: this.userTokenAccountAddress,
782
+ userSourceLiquidity: this.getTokenAccountAddressByUser(this.reserve, this.payer),
819
783
  reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
820
784
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
821
785
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -843,7 +807,7 @@ class KaminoAction {
843
807
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
844
808
  reserveCollateralMint: this.reserve.getCTokenMint(),
845
809
  reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
846
- userSourceLiquidity: this.userTokenAccountAddress,
810
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
847
811
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
848
812
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
849
813
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -852,9 +816,6 @@ class KaminoAction {
852
816
  if (!this.outflowReserve) {
853
817
  throw new Error(`outflowReserve not set`);
854
818
  }
855
- if (!this.additionalTokenAccountAddress) {
856
- throw new Error(`additionalTokenAccountAddress not set`);
857
- }
858
819
  if (!this.outflowAmount) {
859
820
  throw new Error(`outflowAmount not set`);
860
821
  }
@@ -875,7 +836,7 @@ class KaminoAction {
875
836
  borrowReserve: this.outflowReserve.address,
876
837
  borrowReserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
877
838
  reserveSourceLiquidity: this.outflowReserve.state.liquidity.supplyVault,
878
- userDestinationLiquidity: this.additionalTokenAccountAddress,
839
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.outflowReserve),
879
840
  borrowReserveLiquidityFeeReceiver: this.outflowReserve.state.liquidity.feeVault,
880
841
  referrerTokenState: (0, utils_1.referrerTokenStatePda)(this.referrer, this.outflowReserve.address, this.kaminoMarket.programId)[0],
881
842
  tokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
@@ -909,7 +870,7 @@ class KaminoAction {
909
870
  reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
910
871
  reserveCollateralMint: this.reserve.getCTokenMint(),
911
872
  reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
912
- userSourceLiquidity: this.userTokenAccountAddress,
873
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
913
874
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
914
875
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
915
876
  liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -921,9 +882,6 @@ class KaminoAction {
921
882
  if (!this.outflowReserve) {
922
883
  throw new Error(`outflowReserve not set`);
923
884
  }
924
- if (!this.additionalTokenAccountAddress) {
925
- throw new Error(`additionalTokenAccountAddress not set`);
926
- }
927
885
  if (!this.outflowAmount) {
928
886
  throw new Error(`outflowAmount not set`);
929
887
  }
@@ -954,7 +912,7 @@ class KaminoAction {
954
912
  borrowReserve: this.outflowReserve.address,
955
913
  borrowReserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
956
914
  reserveSourceLiquidity: this.outflowReserve.state.liquidity.supplyVault,
957
- userDestinationLiquidity: this.additionalTokenAccountAddress,
915
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.outflowReserve),
958
916
  borrowReserveLiquidityFeeReceiver: this.outflowReserve.state.liquidity.feeVault,
959
917
  referrerTokenState: (0, utils_1.referrerTokenStatePda)(this.referrer, this.outflowReserve.address, this.kaminoMarket.programId)[0],
960
918
  tokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
@@ -981,7 +939,7 @@ class KaminoAction {
981
939
  lendingMarket: this.kaminoMarket.getAddress(),
982
940
  repayReserve: this.reserve.address,
983
941
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
984
- userSourceLiquidity: this.userTokenAccountAddress,
942
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
985
943
  reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
986
944
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
987
945
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -991,9 +949,6 @@ class KaminoAction {
991
949
  if (!this.outflowReserve) {
992
950
  throw new Error(`outflowReserve not set`);
993
951
  }
994
- if (!this.additionalTokenAccountAddress) {
995
- throw new Error(`additionalTokenAccountAddress not set`);
996
- }
997
952
  if (!this.outflowAmount) {
998
953
  throw new Error(`outflowAmount not set`);
999
954
  }
@@ -1009,7 +964,7 @@ class KaminoAction {
1009
964
  reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1010
965
  reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1011
966
  reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1012
- userDestinationLiquidity: this.additionalTokenAccountAddress,
967
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.outflowReserve),
1013
968
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1014
969
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1015
970
  liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
@@ -1041,7 +996,7 @@ class KaminoAction {
1041
996
  lendingMarket: this.kaminoMarket.getAddress(),
1042
997
  repayReserve: this.reserve.address,
1043
998
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
1044
- userSourceLiquidity: this.userTokenAccountAddress,
999
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
1045
1000
  reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
1046
1001
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
1047
1002
  instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -1055,9 +1010,6 @@ class KaminoAction {
1055
1010
  if (!this.outflowReserve) {
1056
1011
  throw new Error(`outflowReserve not set`);
1057
1012
  }
1058
- if (!this.additionalTokenAccountAddress) {
1059
- throw new Error(`additionalTokenAccountAddress not set`);
1060
- }
1061
1013
  if (!this.outflowAmount) {
1062
1014
  throw new Error(`outflowAmount not set`);
1063
1015
  }
@@ -1083,7 +1035,7 @@ class KaminoAction {
1083
1035
  reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1084
1036
  reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1085
1037
  reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1086
- userDestinationLiquidity: this.additionalTokenAccountAddress,
1038
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.outflowReserve),
1087
1039
  placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1088
1040
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1089
1041
  liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
@@ -1098,9 +1050,6 @@ class KaminoAction {
1098
1050
  if (!this.outflowReserve) {
1099
1051
  throw Error(`Withdraw reserve during liquidation is not defined`);
1100
1052
  }
1101
- if (!this.additionalTokenAccountAddress) {
1102
- throw Error(`Liquidating token account address is not defined`);
1103
- }
1104
1053
  const depositReservesList = this.getAdditionalDepositReservesList();
1105
1054
  const depositReserveAccountMetas = depositReservesList.map((reserve) => {
1106
1055
  return { pubkey: reserve, isSigner: false, isWritable: true };
@@ -1123,9 +1072,9 @@ class KaminoAction {
1123
1072
  withdrawReserveCollateralMint: this.outflowReserve.getCTokenMint(),
1124
1073
  withdrawReserveCollateralSupply: this.outflowReserve.state.collateral.supplyVault,
1125
1074
  withdrawReserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1126
- userSourceLiquidity: this.additionalTokenAccountAddress,
1127
- userDestinationCollateral: this.userCollateralAccountAddress,
1128
- userDestinationLiquidity: this.userTokenAccountAddress,
1075
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
1076
+ userDestinationCollateral: this.getUserCollateralAccountAddress(this.outflowReserve),
1077
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.outflowReserve),
1129
1078
  withdrawReserveLiquidityFeeReceiver: this.outflowReserve.state.liquidity.feeVault,
1130
1079
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1131
1080
  repayLiquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -1143,9 +1092,6 @@ class KaminoAction {
1143
1092
  if (!this.outflowReserve) {
1144
1093
  throw Error(`Withdraw reserve during liquidation is not defined`);
1145
1094
  }
1146
- if (!this.additionalTokenAccountAddress) {
1147
- throw Error(`Liquidating token account address is not defined`);
1148
- }
1149
1095
  const depositReservesList = this.getAdditionalDepositReservesList();
1150
1096
  const depositReserveAccountMetas = depositReservesList.map((reserve) => {
1151
1097
  return { pubkey: reserve, isSigner: false, isWritable: true };
@@ -1187,9 +1133,9 @@ class KaminoAction {
1187
1133
  withdrawReserveCollateralMint: this.outflowReserve.getCTokenMint(),
1188
1134
  withdrawReserveCollateralSupply: this.outflowReserve.state.collateral.supplyVault,
1189
1135
  withdrawReserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1190
- userSourceLiquidity: this.additionalTokenAccountAddress,
1191
- userDestinationCollateral: this.userCollateralAccountAddress,
1192
- userDestinationLiquidity: this.userTokenAccountAddress,
1136
+ userSourceLiquidity: this.getUserTokenAccountAddress(this.reserve),
1137
+ userDestinationCollateral: this.getUserCollateralAccountAddress(this.outflowReserve),
1138
+ userDestinationLiquidity: this.getUserTokenAccountAddress(this.outflowReserve),
1193
1139
  withdrawReserveLiquidityFeeReceiver: this.outflowReserve.state.liquidity.feeVault,
1194
1140
  collateralTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1195
1141
  repayLiquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
@@ -1792,7 +1738,7 @@ class KaminoAction {
1792
1738
  reserveLiquidityMint: this.reserve.getLiquidityMint(),
1793
1739
  referrerTokenState: referrerTokenStateAddress,
1794
1740
  reserveSupplyLiquidity: this.reserve.state.liquidity.supplyVault,
1795
- referrerTokenAccount: this.userTokenAccountAddress,
1741
+ referrerTokenAccount: this.getUserTokenAccountAddress(this.reserve),
1796
1742
  lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1797
1743
  tokenProgram: this.reserve.getLiquidityTokenProgram(),
1798
1744
  }, this.kaminoMarket.programId);
@@ -1808,50 +1754,44 @@ class KaminoAction {
1808
1754
  await this.updateWSOLAccount(action);
1809
1755
  }
1810
1756
  if ((action === 'withdraw' || action === 'borrow' || action === 'redeem') && !this.mint.equals(spl_token_1.NATIVE_MINT)) {
1811
- const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getLiquidityMint(), this.owner, this.reserve.getLiquidityTokenProgram(), this.userTokenAccountAddress);
1757
+ const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getLiquidityMint(), this.owner, this.reserve.getLiquidityTokenProgram(), this.getUserTokenAccountAddress(this.reserve));
1812
1758
  this.setupIxs.unshift(createUserTokenAccountIx);
1813
1759
  this.setupIxsLabels.unshift(`CreateLiquidityUserAta[${this.owner}]`);
1814
1760
  }
1815
1761
  if (action === 'liquidate') {
1816
1762
  if (!this.outflowReserve) {
1817
- throw new Error(`Outflow reserve state not found ${this.mint}`);
1763
+ throw new Error(`Outflow reserve state not found ${this.secondaryMint}`);
1818
1764
  }
1819
- const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getLiquidityMint(), this.owner, this.outflowReserve.getLiquidityTokenProgram(), this.userTokenAccountAddress);
1765
+ const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getLiquidityMint(), this.owner, this.outflowReserve.getLiquidityTokenProgram(), this.getUserTokenAccountAddress(this.outflowReserve));
1820
1766
  this.setupIxs.unshift(createUserTokenAccountIx);
1821
- this.setupIxsLabels.unshift(`CreateUserAta[${this.userTokenAccountAddress.toBase58()}]`);
1822
- const [, createUserCollateralAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getCTokenMint(), this.owner, spl_token_1.TOKEN_PROGRAM_ID, this.userCollateralAccountAddress);
1767
+ this.setupIxsLabels.unshift(`CreateUserAta[${this.getUserTokenAccountAddress(this.outflowReserve).toBase58()}]`);
1768
+ const [, createUserCollateralAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getCTokenMint(), this.owner, spl_token_1.TOKEN_PROGRAM_ID, this.getUserCollateralAccountAddress(this.outflowReserve));
1823
1769
  this.setupIxs.unshift(createUserCollateralAccountIx);
1824
- this.setupIxsLabels.unshift(`CreateCollateralUserAta[${this.userCollateralAccountAddress.toString()}]`);
1825
- if (!this.additionalTokenAccountAddress) {
1826
- throw new Error(`Additional token account address not found ${this.mint}`);
1827
- }
1770
+ this.setupIxsLabels.unshift(`CreateCollateralUserAta[${this.getUserCollateralAccountAddress(this.outflowReserve).toString()}]`);
1828
1771
  }
1829
1772
  if (action === 'depositAndBorrow' || (action === 'repayAndWithdraw' && !this.secondaryMint?.equals(spl_token_1.NATIVE_MINT))) {
1830
- if (!this.additionalTokenAccountAddress) {
1831
- throw new Error(`Additional token account address not found ${this.secondaryMint}`);
1832
- }
1833
1773
  if (!this.outflowReserve) {
1834
1774
  throw new Error(`Outflow reserve state not found ${this.mint}`);
1835
1775
  }
1776
+ const additionalUserTokenAccountAddress = this.getUserTokenAccountAddress(this.outflowReserve);
1836
1777
  const additionalUserTokenAccountInfo = await this.kaminoMarket
1837
1778
  .getConnection()
1838
- .getAccountInfo(this.additionalTokenAccountAddress);
1779
+ .getAccountInfo(additionalUserTokenAccountAddress);
1839
1780
  if (!additionalUserTokenAccountInfo) {
1840
- const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getLiquidityMint(), this.owner, this.outflowReserve.getLiquidityTokenProgram(), this.additionalTokenAccountAddress);
1781
+ const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getLiquidityMint(), this.owner, this.outflowReserve.getLiquidityTokenProgram(), additionalUserTokenAccountAddress);
1841
1782
  this.setupIxs.unshift(createUserTokenAccountIx);
1842
1783
  this.setupIxsLabels.unshift(`CreateAdditionalUserTokenAta[${this.owner}]`);
1843
1784
  }
1844
1785
  }
1845
1786
  if (action === 'withdraw' || action === 'mint' || action === 'deposit' || action === 'repayAndWithdraw') {
1846
- const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getLiquidityMint(), this.owner, this.reserve.getLiquidityTokenProgram(), this.userTokenAccountAddress);
1847
- this.preTxnIxs.push(createUserTokenAccountIx);
1848
- this.preTxnIxsLabels.push(`CreateUserAta[${this.userTokenAccountAddress.toBase58()}]`);
1787
+ const [, createUserTokenAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getLiquidityMint(), this.owner, this.reserve.getLiquidityTokenProgram(), this.getUserTokenAccountAddress(this.reserve));
1788
+ this.setupIxs.unshift(createUserTokenAccountIx);
1789
+ this.setupIxsLabels.unshift(`CreateUserAta[${this.getUserTokenAccountAddress(this.reserve).toBase58()}]`);
1849
1790
  }
1850
1791
  if (action === 'mint') {
1851
- const collateralMintPubkey = this.reserve.getCTokenMint();
1852
- const [, createUserCollateralAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, collateralMintPubkey, this.owner, spl_token_1.TOKEN_PROGRAM_ID, this.userCollateralAccountAddress);
1792
+ const [, createUserCollateralAccountIx] = (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getCTokenMint(), this.owner, spl_token_1.TOKEN_PROGRAM_ID, this.getUserCollateralAccountAddress(this.reserve));
1853
1793
  this.setupIxs.unshift(createUserCollateralAccountIx);
1854
- this.setupIxsLabels.unshift(`CreateCollateralUserAta[${this.userCollateralAccountAddress.toString()}]`);
1794
+ this.setupIxsLabels.unshift(`CreateCollateralUserAta[${this.getUserCollateralAccountAddress(this.reserve).toString()}]`);
1855
1795
  }
1856
1796
  }
1857
1797
  async updateWSOLAccount(action) {
@@ -1874,15 +1814,16 @@ class KaminoAction {
1874
1814
  safeRepay = new bn_js_1.default(Math.floor(obligation_1.KaminoObligation.getBorrowAmount(borrow)
1875
1815
  .mul(cumulativeBorrowRateReserve)
1876
1816
  .div(cumulativeBorrowRateObligation)
1877
- .add(new decimal_js_1.default(SOL_PADDING_FOR_INTEREST.toString()))
1817
+ .add(new decimal_js_1.default(utils_1.SOL_PADDING_FOR_INTEREST.toString()))
1878
1818
  .toNumber()).toString());
1879
1819
  }
1880
- let userTokenAccountAddress = this.userTokenAccountAddress;
1820
+ let userTokenAccountAddress = this.getUserTokenAccountAddress(this.reserve);
1881
1821
  if (this.secondaryMint?.equals(spl_token_1.NATIVE_MINT)) {
1882
- if (!this.additionalTokenAccountAddress) {
1883
- throw new Error(`Additional token account address not found ${this.secondaryMint}`);
1822
+ if (!this.outflowReserve) {
1823
+ throw new Error(`Outflow reserve state not found ${this.secondaryMint}`);
1884
1824
  }
1885
- userTokenAccountAddress = this.additionalTokenAccountAddress;
1825
+ const additionalUserTokenAccountAddress = this.getUserTokenAccountAddress(this.outflowReserve);
1826
+ userTokenAccountAddress = additionalUserTokenAccountAddress;
1886
1827
  }
1887
1828
  const userWSOLAccountInfo = await this.kaminoMarket.getConnection().getAccountInfo(userTokenAccountAddress);
1888
1829
  const rentExempt = await this.kaminoMarket.getConnection().getMinimumBalanceForRentExemption(165);
@@ -1890,7 +1831,7 @@ class KaminoAction {
1890
1831
  const sendAction = action === 'deposit' ||
1891
1832
  action === 'repay' ||
1892
1833
  action === 'mint' ||
1893
- (action === 'liquidate' && this.secondaryMint?.equals(spl_token_1.NATIVE_MINT)); // only sync WSOL amount if liquidator repays SOL which is secondaryMint
1834
+ (action === 'liquidate' && this.mint?.equals(spl_token_1.NATIVE_MINT)); // only sync WSOL amount if liquidator repays SOL which is secondaryMint
1894
1835
  const transferLamportsIx = web3_js_1.SystemProgram.transfer({
1895
1836
  fromPubkey: this.owner,
1896
1837
  toPubkey: userTokenAccountAddress,
@@ -1930,55 +1871,32 @@ class KaminoAction {
1930
1871
  if (!outflowReserve || !inflowReserve) {
1931
1872
  throw new Error('reserve states are not fetched');
1932
1873
  }
1933
- const { userTokenAccountAddress: userOutflowTokenAccountAddress, userCollateralAccountAddress: userOutflowCollateralAccountAddress, } = KaminoAction.getUserAccountAddresses(payer, outflowReserve.state);
1934
- const { userTokenAccountAddress: userInflowTokenAccountAddress, userCollateralAccountAddress: userInflowCollateralAccountAddress, } = KaminoAction.getUserAccountAddresses(payer, inflowReserve.state);
1935
1874
  const { kaminoObligation, depositReserves, borrowReserves, distinctReserveCount } = await KaminoAction.loadObligation(action, kaminoMarket, obligationOwner, inflowReserve.address, obligation, outflowReserve.address);
1936
1875
  const referrerKey = await this.getReferrerKey(kaminoMarket, payer, kaminoObligation, referrer);
1937
- let userTokenAccountAddress;
1938
- let userCollateralAccountAddress;
1939
- let additionalUserTokenAccountAddress;
1940
1876
  let secondaryMint;
1941
1877
  let primaryMint;
1942
- if (action === 'liquidate') {
1943
- userTokenAccountAddress = userOutflowTokenAccountAddress;
1944
- userCollateralAccountAddress = userOutflowCollateralAccountAddress;
1945
- additionalUserTokenAccountAddress = userInflowTokenAccountAddress;
1946
- primaryMint = outflowTokenMint;
1947
- secondaryMint = inflowTokenMint;
1948
- }
1949
- else if (action === 'depositAndBorrow') {
1950
- userTokenAccountAddress = userInflowTokenAccountAddress;
1951
- userCollateralAccountAddress = userInflowCollateralAccountAddress;
1952
- additionalUserTokenAccountAddress = userOutflowTokenAccountAddress;
1878
+ if (action === 'liquidate' || action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
1953
1879
  primaryMint = inflowTokenMint;
1954
1880
  secondaryMint = outflowTokenMint;
1955
1881
  }
1956
- else if (action === 'repayAndWithdraw') {
1957
- primaryMint = inflowTokenMint;
1958
- secondaryMint = outflowTokenMint;
1959
- userTokenAccountAddress = userInflowTokenAccountAddress;
1960
- userCollateralAccountAddress = userOutflowCollateralAccountAddress;
1961
- additionalUserTokenAccountAddress = userOutflowTokenAccountAddress;
1962
- }
1963
1882
  else {
1964
1883
  throw new Error('Invalid action');
1965
1884
  }
1966
- return new KaminoAction(kaminoMarket, payer, kaminoObligation || obligation, userTokenAccountAddress, userCollateralAccountAddress, primaryMint, distinctReserveCount, inflowAmount, depositReserves, borrowReserves, inflowReserve, currentSlot, secondaryMint, additionalUserTokenAccountAddress, outflowReserve, outflowAmount, referrerKey);
1885
+ return new KaminoAction(kaminoMarket, payer, kaminoObligation || obligation, primaryMint, distinctReserveCount, inflowAmount, depositReserves, borrowReserves, inflowReserve, currentSlot, secondaryMint, outflowReserve, outflowAmount, referrerKey);
1967
1886
  }
1968
1887
  static async initializeWithdrawReferrerFees(mint, owner, kaminoMarket, currentSlot = 0) {
1969
1888
  const reserve = kaminoMarket.getReserveByMint(mint);
1970
1889
  if (reserve === undefined) {
1971
1890
  throw new Error(`Reserve ${mint} not found in market ${kaminoMarket.getAddress().toBase58()}`);
1972
1891
  }
1973
- const [{ ata, createAtaIx }] = (0, utils_1.createAtasIdempotent)(owner, [
1892
+ const [{ createAtaIx }] = (0, utils_1.createAtasIdempotent)(owner, [
1974
1893
  {
1975
1894
  mint: reserve.getLiquidityMint(),
1976
1895
  tokenProgram: reserve.getLiquidityTokenProgram(),
1977
1896
  },
1978
1897
  ]);
1979
- const userTokenAccountAddress = ata;
1980
1898
  return {
1981
- axn: new KaminoAction(kaminoMarket, owner, new ObligationType_1.VanillaObligation(kaminoMarket.programId), userTokenAccountAddress, web3_js_1.PublicKey.default, mint, 0, new bn_js_1.default(0), [], [], reserve, currentSlot, undefined, undefined, undefined, undefined, undefined),
1899
+ axn: new KaminoAction(kaminoMarket, owner, new ObligationType_1.VanillaObligation(kaminoMarket.programId), mint, 0, new bn_js_1.default(0), [], [], reserve, currentSlot, undefined, undefined, undefined, undefined, undefined),
1982
1900
  createAtaIxs: [createAtaIx],
1983
1901
  };
1984
1902
  }
@@ -2025,6 +1943,15 @@ class KaminoAction {
2025
1943
  }
2026
1944
  return referrerKey;
2027
1945
  }
1946
+ getUserTokenAccountAddress(reserve) {
1947
+ return (0, utils_1.getAssociatedTokenAddress)(reserve.getLiquidityMint(), this.owner, true, reserve.getLiquidityTokenProgram());
1948
+ }
1949
+ getTokenAccountAddressByUser(reserve, user) {
1950
+ return (0, utils_1.getAssociatedTokenAddress)(reserve.getLiquidityMint(), user, true, reserve.getLiquidityTokenProgram());
1951
+ }
1952
+ getUserCollateralAccountAddress(reserve) {
1953
+ return (0, utils_1.getAssociatedTokenAddress)(reserve.getCTokenMint(), this.owner, true);
1954
+ }
2028
1955
  static actionToIxs(action) {
2029
1956
  const ixs = [...action.computeBudgetIxs, ...action.setupIxs];
2030
1957
  ixs.push(...KaminoAction.actionToLendingIxs(action));
@@ -2048,14 +1975,14 @@ class KaminoAction {
2048
1975
  return labels;
2049
1976
  }
2050
1977
  static actionToLendingIxLabels(action) {
2051
- const lables = [];
1978
+ const labels = [];
2052
1979
  for (let i = 0; i < action.lendingIxsLabels.length; i++) {
2053
- lables.push(action.lendingIxsLabels[i]);
1980
+ labels.push(action.lendingIxsLabels[i]);
2054
1981
  if (i !== action.lendingIxsLabels.length - 1) {
2055
- lables.push(...action.inBetweenIxsLabels);
1982
+ labels.push(...action.inBetweenIxsLabels);
2056
1983
  }
2057
1984
  }
2058
- return lables;
1985
+ return labels;
2059
1986
  }
2060
1987
  }
2061
1988
  exports.KaminoAction = KaminoAction;