@kamino-finance/klend-sdk 5.11.1 → 5.11.2-beta.0

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.
Files changed (41) hide show
  1. package/dist/classes/action.d.ts +12 -2
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +245 -29
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/obligation.d.ts +1 -1
  6. package/dist/classes/obligation.d.ts.map +1 -1
  7. package/dist/classes/obligation.js +1 -1
  8. package/dist/classes/obligation.js.map +1 -1
  9. package/dist/classes/vault.js +6 -6
  10. package/dist/classes/vault.js.map +1 -1
  11. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
  12. package/dist/lending_operations/repay_with_collateral_calcs.js +9 -5
  13. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  14. package/dist/lending_operations/repay_with_collateral_operations.d.ts +5 -0
  15. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  16. package/dist/lending_operations/repay_with_collateral_operations.js +26 -1
  17. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  18. package/dist/utils/lookupTable.d.ts +27 -0
  19. package/dist/utils/lookupTable.d.ts.map +1 -1
  20. package/dist/utils/lookupTable.js +58 -0
  21. package/dist/utils/lookupTable.js.map +1 -1
  22. package/dist/utils/seeds.d.ts +11 -1
  23. package/dist/utils/seeds.d.ts.map +1 -1
  24. package/dist/utils/seeds.js +13 -3
  25. package/dist/utils/seeds.js.map +1 -1
  26. package/dist/utils/userMetadata.js +6 -6
  27. package/dist/utils/userMetadata.js.map +1 -1
  28. package/package.json +1 -1
  29. package/src/classes/action.ts +378 -49
  30. package/src/classes/obligation.ts +1 -1
  31. package/src/classes/vault.ts +1 -1
  32. package/src/lending_operations/repay_with_collateral_calcs.ts +14 -5
  33. package/src/lending_operations/repay_with_collateral_operations.ts +63 -20
  34. package/src/utils/lookupTable.ts +62 -0
  35. package/src/utils/seeds.ts +14 -4
  36. package/src/utils/userMetadata.ts +14 -14
  37. package/dist/classes/lut_utils.d.ts +0 -29
  38. package/dist/classes/lut_utils.d.ts.map +0 -1
  39. package/dist/classes/lut_utils.js +0 -62
  40. package/dist/classes/lut_utils.js.map +0 -1
  41. package/src/classes/lut_utils.ts +0 -63
@@ -21,6 +21,7 @@ import BN from 'bn.js';
21
21
  import Decimal from 'decimal.js';
22
22
  import {
23
23
  borrowObligationLiquidity,
24
+ depositAndWithdraw,
24
25
  borrowObligationLiquidityV2,
25
26
  depositObligationCollateral,
26
27
  depositObligationCollateralV2,
@@ -41,6 +42,7 @@ import {
41
42
  RefreshObligationFarmsForReserveAccounts,
42
43
  RefreshObligationFarmsForReserveArgs,
43
44
  refreshReserve,
45
+ repayAndWithdrawAndRedeem,
44
46
  repayObligationLiquidity,
45
47
  repayObligationLiquidityV2,
46
48
  requestElevationGroup,
@@ -90,7 +92,9 @@ export type ActionType =
90
92
  | 'repayAndWithdraw'
91
93
  | 'refreshObligation'
92
94
  | 'requestElevationGroup'
93
- | 'withdrawReferrerFees';
95
+ | 'withdrawReferrerFees'
96
+ | 'repayAndWithdrawV2'
97
+ | 'depositAndWithdraw';
94
98
 
95
99
  export type AuxiliaryIx = 'setup' | 'inBetween' | 'cleanup';
96
100
 
@@ -817,6 +821,132 @@ export class KaminoAction {
817
821
  return axn;
818
822
  }
819
823
 
824
+ static async buildDepositAndWithdrawV2Txns(
825
+ kaminoMarket: KaminoMarket,
826
+ depositAmount: string | BN,
827
+ depositMint: PublicKey,
828
+ withdrawAmount: string | BN,
829
+ withdrawMint: PublicKey,
830
+ payer: PublicKey,
831
+ currentSlot: number,
832
+ obligation: KaminoObligation | ObligationType,
833
+ extraComputeBudget: number = 1_000_000, // if > 0 then adds the ixn
834
+ includeAtaIxns: boolean = true, // if true it includes create and close wsol and token atas,
835
+ requestElevationGroup: boolean = false,
836
+ includeUserMetadata: boolean = true, // if true it includes user metadata,
837
+ referrer: PublicKey = PublicKey.default,
838
+ scopeRefresh: ScopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }
839
+ ) {
840
+ const axn = await KaminoAction.initializeMultiTokenAction(
841
+ kaminoMarket,
842
+ 'depositAndWithdraw',
843
+ depositAmount,
844
+ depositMint,
845
+ withdrawMint,
846
+ payer,
847
+ payer,
848
+ obligation,
849
+ withdrawAmount,
850
+ referrer,
851
+ currentSlot
852
+ );
853
+ const addInitObligationForFarm = true;
854
+ const twoTokenAction = true;
855
+ if (extraComputeBudget > 0) {
856
+ axn.addComputeBudgetIxn(extraComputeBudget);
857
+ }
858
+
859
+ const allReserves = new PublicKeySet<PublicKey>([
860
+ ...axn.depositReserves,
861
+ ...axn.borrowReserves,
862
+ axn.reserve.address,
863
+ axn.outflowReserve!.address,
864
+ ]).toArray();
865
+ const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
866
+
867
+ if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
868
+ await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
869
+ }
870
+
871
+ await axn.addSupportIxs(
872
+ 'depositAndWithdraw',
873
+ includeAtaIxns,
874
+ requestElevationGroup,
875
+ includeUserMetadata,
876
+ addInitObligationForFarm,
877
+ true,
878
+ twoTokenAction
879
+ );
880
+ const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve!, axn.outflowAmount!);
881
+ axn.addDepositAndWithdrawV2Ixs(withdrawCollateralAmount);
882
+
883
+ return axn;
884
+ }
885
+
886
+ static async buildRepayAndWithdrawV2Txns(
887
+ kaminoMarket: KaminoMarket,
888
+ repayAmount: string | BN,
889
+ repayMint: PublicKey,
890
+ withdrawAmount: string | BN,
891
+ withdrawMint: PublicKey,
892
+ payer: PublicKey,
893
+ currentSlot: number,
894
+ obligation: KaminoObligation | ObligationType,
895
+ extraComputeBudget: number = 1_000_000, // if > 0 then adds the ixn
896
+ includeAtaIxns: boolean = true, // if true it includes create and close wsol and token atas,
897
+ requestElevationGroup: boolean = false,
898
+ includeUserMetadata: boolean = true, // if true it includes user metadata,
899
+ createLookupTable: boolean = true,
900
+ referrer: PublicKey = PublicKey.default,
901
+ scopeRefresh: ScopeRefresh = { includeScopeRefresh: false, scopeFeed: 'hubble' }
902
+ ) {
903
+ const axn = await KaminoAction.initializeMultiTokenAction(
904
+ kaminoMarket,
905
+ 'repayAndWithdrawV2',
906
+ repayAmount,
907
+ repayMint,
908
+ withdrawMint,
909
+ payer,
910
+ payer,
911
+ obligation,
912
+ withdrawAmount,
913
+ referrer,
914
+ currentSlot
915
+ );
916
+ const addInitObligationForFarm = true;
917
+ const twoTokenAction = true;
918
+ if (extraComputeBudget > 0) {
919
+ axn.addComputeBudgetIxn(extraComputeBudget);
920
+ }
921
+
922
+ const allReserves = new PublicKeySet<PublicKey>([
923
+ ...axn.depositReserves,
924
+ ...axn.borrowReserves,
925
+ axn.reserve.address,
926
+ axn.outflowReserve!.address,
927
+ ]).toArray();
928
+ const tokenIds = axn.getTokenIdsForScopeRefresh(kaminoMarket, allReserves);
929
+
930
+ if (tokenIds.length > 0 && scopeRefresh.includeScopeRefresh) {
931
+ await axn.addScopeRefreshIxs(tokenIds, scopeRefresh.scopeFeed);
932
+ }
933
+
934
+ await axn.addSupportIxs(
935
+ 'repayAndWithdrawV2',
936
+ includeAtaIxns,
937
+ requestElevationGroup,
938
+ includeUserMetadata,
939
+ addInitObligationForFarm,
940
+ true,
941
+ createLookupTable,
942
+ twoTokenAction
943
+ );
944
+ const withdrawCollateralAmount = axn.getWithdrawCollateralAmount(axn.outflowReserve!, axn.outflowAmount!);
945
+ axn.addRepayAndWithdrawV2Ixs(withdrawCollateralAmount);
946
+
947
+ return axn;
948
+ }
949
+
820
950
  static async buildRepayAndWithdrawTxns(
821
951
  kaminoMarket: KaminoMarket,
822
952
  repayAmount: string | BN,
@@ -1328,10 +1458,7 @@ export class KaminoAction {
1328
1458
  reserveFarmState: PROGRAM_ID,
1329
1459
  }
1330
1460
  : {
1331
- obligationFarmUserState: obligationFarmStatePda(
1332
- this.getObligationPda(),
1333
- this.reserve.state.farmCollateral
1334
- )[0],
1461
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmCollateral, this.getObligationPda()),
1335
1462
  reserveFarmState: this.reserve.state.farmCollateral,
1336
1463
  };
1337
1464
 
@@ -1396,10 +1523,7 @@ export class KaminoAction {
1396
1523
  reserveFarmState: PROGRAM_ID,
1397
1524
  }
1398
1525
  : {
1399
- obligationFarmUserState: obligationFarmStatePda(
1400
- this.getObligationPda(),
1401
- this.reserve.state.farmCollateral
1402
- )[0],
1526
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmCollateral, this.getObligationPda()),
1403
1527
  reserveFarmState: this.reserve.state.farmCollateral,
1404
1528
  };
1405
1529
 
@@ -1481,7 +1605,7 @@ export class KaminoAction {
1481
1605
  reserveFarmState: PROGRAM_ID,
1482
1606
  }
1483
1607
  : {
1484
- obligationFarmUserState: obligationFarmStatePda(this.getObligationPda(), this.reserve.state.farmDebt)[0],
1608
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmDebt, this.getObligationPda()),
1485
1609
  reserveFarmState: this.reserve.state.farmDebt,
1486
1610
  };
1487
1611
 
@@ -1556,10 +1680,7 @@ export class KaminoAction {
1556
1680
  reserveFarmState: PROGRAM_ID,
1557
1681
  }
1558
1682
  : {
1559
- obligationFarmUserState: obligationFarmStatePda(
1560
- this.getObligationPda(),
1561
- this.reserve.state.farmCollateral
1562
- )[0],
1683
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmCollateral, this.getObligationPda()),
1563
1684
  reserveFarmState: this.reserve.state.farmCollateral,
1564
1685
  };
1565
1686
 
@@ -1643,7 +1764,7 @@ export class KaminoAction {
1643
1764
  reserveFarmState: PROGRAM_ID,
1644
1765
  }
1645
1766
  : {
1646
- obligationFarmUserState: obligationFarmStatePda(this.getObligationPda(), this.reserve.state.farmDebt)[0],
1767
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmDebt, this.getObligationPda()),
1647
1768
  reserveFarmState: this.reserve.state.farmDebt,
1648
1769
  };
1649
1770
  const depositReserveAccountMetas = depositReservesList.map((reserve) => {
@@ -1679,6 +1800,208 @@ export class KaminoAction {
1679
1800
  this.lendingIxs.push(repayIx);
1680
1801
  }
1681
1802
 
1803
+ addRepayAndWithdrawV2Ixs(withdrawCollateralAmount: BN) {
1804
+ this.lendingIxsLabels.push(
1805
+ `repayAndWithdrawAndRedeem(repayReserve=${this.reserve!.address})(withdrawReserve=${
1806
+ this.outflowReserve!.address
1807
+ })(obligation=${this.getObligationPda()})`
1808
+ );
1809
+
1810
+ const depositReservesList = this.getAdditionalDepositReservesList();
1811
+
1812
+ const depositReserveAccountMetas = depositReservesList.map((reserve) => {
1813
+ return { pubkey: reserve, isSigner: false, isWritable: true };
1814
+ });
1815
+ const borrowReserveAccountMetas = this.borrowReserves.map((reserve) => {
1816
+ return { pubkey: reserve, isSigner: false, isWritable: true };
1817
+ });
1818
+
1819
+ if (!this.outflowAmount) {
1820
+ throw new Error(`outflowAmount not set`);
1821
+ }
1822
+
1823
+ if (!this.outflowReserve) {
1824
+ throw new Error(`outflowReserve not set`);
1825
+ }
1826
+
1827
+ if (!this.additionalTokenAccountAddress) {
1828
+ throw new Error(`additionalTokenAccountAddress not set`);
1829
+ }
1830
+
1831
+ const collateralFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(PublicKey.default)
1832
+ ? {
1833
+ obligationFarmUserState: this.kaminoMarket.programId,
1834
+ reserveFarmState: this.kaminoMarket.programId,
1835
+ }
1836
+ : {
1837
+ obligationFarmUserState: obligationFarmStatePda(
1838
+ this.outflowReserve.state.farmCollateral,
1839
+ this.getObligationPda()
1840
+ ),
1841
+ reserveFarmState: this.outflowReserve.state.farmCollateral,
1842
+ };
1843
+
1844
+ const debtFarmsAccounts = this.reserve.state.farmDebt.equals(PublicKey.default)
1845
+ ? {
1846
+ obligationFarmUserState: this.kaminoMarket.programId,
1847
+ reserveFarmState: this.kaminoMarket.programId,
1848
+ }
1849
+ : {
1850
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmDebt, this.getObligationPda()),
1851
+ reserveFarmState: this.reserve.state.farmDebt,
1852
+ };
1853
+
1854
+ const repayAndWithdrawIx = repayAndWithdrawAndRedeem(
1855
+ {
1856
+ repayAmount: this.amount,
1857
+ withdrawCollateralAmount,
1858
+ },
1859
+ {
1860
+ repayAccounts: {
1861
+ owner: this.owner,
1862
+ obligation: this.getObligationPda(),
1863
+ lendingMarket: this.kaminoMarket.getAddress(),
1864
+ repayReserve: this.reserve!.address,
1865
+ reserveLiquidityMint: this.reserve.getLiquidityMint(),
1866
+ userSourceLiquidity: this.userTokenAccountAddress,
1867
+ reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
1868
+ tokenProgram: this.reserve.getLiquidityTokenProgram(),
1869
+ instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
1870
+ },
1871
+ withdrawAccounts: {
1872
+ owner: this.owner,
1873
+ obligation: this.getObligationPda(),
1874
+ lendingMarket: this.kaminoMarket.getAddress(),
1875
+ lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1876
+ withdrawReserve: this.outflowReserve.address,
1877
+ reserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
1878
+ reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1879
+ reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1880
+ reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1881
+ userDestinationLiquidity: this.additionalTokenAccountAddress,
1882
+ placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1883
+ collateralTokenProgram: TOKEN_PROGRAM_ID,
1884
+ liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
1885
+ instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
1886
+ },
1887
+ collateralFarmsAccounts,
1888
+ debtFarmsAccounts,
1889
+ farmsProgram: farmsId,
1890
+ },
1891
+ this.kaminoMarket.programId
1892
+ );
1893
+
1894
+ repayAndWithdrawIx.keys = repayAndWithdrawIx.keys.concat([
1895
+ ...depositReserveAccountMetas,
1896
+ ...borrowReserveAccountMetas,
1897
+ ]);
1898
+
1899
+ this.lendingIxs.push(repayAndWithdrawIx);
1900
+ }
1901
+
1902
+ addDepositAndWithdrawV2Ixs(withdrawCollateralAmount: BN) {
1903
+ this.lendingIxsLabels.push(
1904
+ `depositAndWithdrawV2(depositReserve=${this.reserve!.address})(withdrawReserve=${
1905
+ this.outflowReserve!.address
1906
+ })(obligation=${this.getObligationPda()})`
1907
+ );
1908
+
1909
+ const depositReservesList = this.getAdditionalDepositReservesList();
1910
+
1911
+ const depositReserveAccountMetas = depositReservesList.map((reserve) => {
1912
+ return { pubkey: reserve, isSigner: false, isWritable: true };
1913
+ });
1914
+ const borrowReserveAccountMetas = this.borrowReserves.map((reserve) => {
1915
+ return { pubkey: reserve, isSigner: false, isWritable: true };
1916
+ });
1917
+
1918
+ if (!this.outflowAmount) {
1919
+ throw new Error(`outflowAmount not set`);
1920
+ }
1921
+
1922
+ if (!this.outflowReserve) {
1923
+ throw new Error(`outflowReserve not set`);
1924
+ }
1925
+
1926
+ if (!this.additionalTokenAccountAddress) {
1927
+ throw new Error(`additionalTokenAccountAddress not set`);
1928
+ }
1929
+
1930
+ const depositFarmsAccounts = this.reserve.state.farmCollateral.equals(PublicKey.default)
1931
+ ? {
1932
+ obligationFarmUserState: this.kaminoMarket.programId,
1933
+ reserveFarmState: this.kaminoMarket.programId,
1934
+ }
1935
+ : {
1936
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmDebt, this.getObligationPda()),
1937
+ reserveFarmState: this.reserve.state.farmCollateral,
1938
+ };
1939
+ const withdrawFarmsAccounts = this.outflowReserve.state.farmCollateral.equals(PublicKey.default)
1940
+ ? {
1941
+ obligationFarmUserState: this.kaminoMarket.programId,
1942
+ reserveFarmState: this.kaminoMarket.programId,
1943
+ }
1944
+ : {
1945
+ obligationFarmUserState: obligationFarmStatePda(
1946
+ this.outflowReserve.state.farmCollateral,
1947
+ this.getObligationPda()
1948
+ ),
1949
+ reserveFarmState: this.outflowReserve.state.farmCollateral,
1950
+ };
1951
+
1952
+ const depositAndWithdrawIx = depositAndWithdraw(
1953
+ {
1954
+ liquidityAmount: this.amount,
1955
+ withdrawCollateralAmount,
1956
+ },
1957
+ {
1958
+ depositAccounts: {
1959
+ owner: this.owner,
1960
+ obligation: this.getObligationPda(),
1961
+ lendingMarket: this.kaminoMarket.getAddress(),
1962
+ lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1963
+ reserve: this.reserve.address,
1964
+ reserveLiquidityMint: this.reserve.getLiquidityMint(),
1965
+ reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
1966
+ reserveCollateralMint: this.reserve.getCTokenMint(),
1967
+ reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault, // destinationCollateral
1968
+ userSourceLiquidity: this.userTokenAccountAddress,
1969
+ placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1970
+ collateralTokenProgram: TOKEN_PROGRAM_ID,
1971
+ liquidityTokenProgram: this.reserve.getLiquidityTokenProgram(),
1972
+ instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
1973
+ },
1974
+ withdrawAccounts: {
1975
+ owner: this.owner,
1976
+ obligation: this.getObligationPda(),
1977
+ lendingMarket: this.kaminoMarket.getAddress(),
1978
+ lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
1979
+ withdrawReserve: this.outflowReserve.address,
1980
+ reserveLiquidityMint: this.outflowReserve.getLiquidityMint(),
1981
+ reserveCollateralMint: this.outflowReserve.getCTokenMint(),
1982
+ reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
1983
+ reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
1984
+ userDestinationLiquidity: this.additionalTokenAccountAddress,
1985
+ placeholderUserDestinationCollateral: this.kaminoMarket.programId,
1986
+ collateralTokenProgram: TOKEN_PROGRAM_ID,
1987
+ liquidityTokenProgram: this.outflowReserve.getLiquidityTokenProgram(),
1988
+ instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
1989
+ },
1990
+ depositFarmsAccounts,
1991
+ withdrawFarmsAccounts,
1992
+ farmsProgram: farmsId,
1993
+ },
1994
+ this.kaminoMarket.programId
1995
+ );
1996
+
1997
+ depositAndWithdrawIx.keys = depositAndWithdrawIx.keys.concat([
1998
+ ...depositReserveAccountMetas,
1999
+ ...borrowReserveAccountMetas,
2000
+ ]);
2001
+
2002
+ this.lendingIxs.push(depositAndWithdrawIx);
2003
+ }
2004
+
1682
2005
  async addDepositAndBorrowIx() {
1683
2006
  this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateral`);
1684
2007
  this.lendingIxsLabels.push(`borrowObligationLiquidity`);
@@ -1764,10 +2087,7 @@ export class KaminoAction {
1764
2087
  reserveFarmState: PROGRAM_ID,
1765
2088
  }
1766
2089
  : {
1767
- obligationFarmUserState: obligationFarmStatePda(
1768
- this.getObligationPda(),
1769
- this.reserve.state.farmCollateral
1770
- )[0],
2090
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmCollateral, this.getObligationPda()),
1771
2091
  reserveFarmState: this.reserve.state.farmCollateral,
1772
2092
  };
1773
2093
 
@@ -1828,10 +2148,7 @@ export class KaminoAction {
1828
2148
  reserveFarmState: PROGRAM_ID,
1829
2149
  }
1830
2150
  : {
1831
- obligationFarmUserState: obligationFarmStatePda(
1832
- this.getObligationPda(),
1833
- this.outflowReserve.state.farmDebt!
1834
- )[0],
2151
+ obligationFarmUserState: obligationFarmStatePda(this.outflowReserve.state.farmDebt, this.getObligationPda()),
1835
2152
  reserveFarmState: this.outflowReserve.state.farmDebt,
1836
2153
  };
1837
2154
 
@@ -1957,7 +2274,7 @@ export class KaminoAction {
1957
2274
  reserveFarmState: PROGRAM_ID,
1958
2275
  }
1959
2276
  : {
1960
- obligationFarmUserState: obligationFarmStatePda(this.getObligationPda(), this.reserve.state.farmDebt)[0],
2277
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmDebt, this.getObligationPda()),
1961
2278
  reserveFarmState: this.reserve.state.farmDebt,
1962
2279
  };
1963
2280
 
@@ -2006,9 +2323,9 @@ export class KaminoAction {
2006
2323
  }
2007
2324
  : {
2008
2325
  obligationFarmUserState: obligationFarmStatePda(
2009
- this.getObligationPda(),
2010
- this.outflowReserve.state.farmCollateral
2011
- )[0],
2326
+ this.outflowReserve.state.farmCollateral,
2327
+ this.getObligationPda()
2328
+ ),
2012
2329
  reserveFarmState: this.outflowReserve.state.farmCollateral,
2013
2330
  };
2014
2331
 
@@ -2042,7 +2359,7 @@ export class KaminoAction {
2042
2359
  );
2043
2360
  }
2044
2361
 
2045
- async addLiquidateIx(maxAllowedLtvOverridePercent: number = 0) {
2362
+ addLiquidateIx(maxAllowedLtvOverridePercent: number = 0) {
2046
2363
  this.lendingIxsLabels.push(`liquidateObligationAndRedeemReserveCollateral`);
2047
2364
  if (!this.outflowReserve) {
2048
2365
  throw Error(`Withdraw reserve during liquidation is not defined`);
@@ -2115,9 +2432,9 @@ export class KaminoAction {
2115
2432
  }
2116
2433
  : {
2117
2434
  obligationFarmUserState: obligationFarmStatePda(
2118
- this.getObligationPda(),
2119
- this.outflowReserve.state.farmCollateral
2120
- )[0],
2435
+ this.outflowReserve.state.farmCollateral,
2436
+ this.getObligationPda()
2437
+ ),
2121
2438
  reserveFarmState: this.outflowReserve.state.farmCollateral,
2122
2439
  };
2123
2440
 
@@ -2127,7 +2444,7 @@ export class KaminoAction {
2127
2444
  reserveFarmState: PROGRAM_ID,
2128
2445
  }
2129
2446
  : {
2130
- obligationFarmUserState: obligationFarmStatePda(this.getObligationPda(), this.reserve.state.farmDebt)[0],
2447
+ obligationFarmUserState: obligationFarmStatePda(this.reserve.state.farmDebt, this.getObligationPda()),
2131
2448
  reserveFarmState: this.reserve.state.farmDebt,
2132
2449
  };
2133
2450
 
@@ -2239,6 +2556,7 @@ export class KaminoAction {
2239
2556
  'repay',
2240
2557
  'depositAndBorrow',
2241
2558
  'repayAndWithdraw',
2559
+ 'repayAndWithdrawV2',
2242
2560
  'refreshObligation',
2243
2561
  ].includes(action)
2244
2562
  ) {
@@ -2257,12 +2575,17 @@ export class KaminoAction {
2257
2575
 
2258
2576
  let currentReserves: KaminoReserve[] = [];
2259
2577
 
2260
- if (action === 'liquidate' || action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
2578
+ if (
2579
+ action === 'liquidate' ||
2580
+ action === 'depositAndBorrow' ||
2581
+ action === 'repayAndWithdraw' ||
2582
+ action === 'repayAndWithdrawV2'
2583
+ ) {
2261
2584
  if (!this.outflowReserve) {
2262
2585
  throw new Error('outflowReserve is undefined');
2263
2586
  }
2264
2587
 
2265
- if (action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
2588
+ if (action === 'depositAndBorrow' || action === 'repayAndWithdraw' || action === 'repayAndWithdrawV2') {
2266
2589
  currentReserves = [this.reserve, this.outflowReserve];
2267
2590
  if (action === 'depositAndBorrow') {
2268
2591
  if (this.obligation) {
@@ -2307,7 +2630,12 @@ export class KaminoAction {
2307
2630
  if (this.outflowReserve) {
2308
2631
  await this.addInitObligationForFarm(this.outflowReserve, ReserveFarmKind.Debt, addAsSupportIx);
2309
2632
  }
2310
- } else if (action === 'repayAndWithdraw' || action === 'borrow' || action === 'repay') {
2633
+ } else if (
2634
+ action === 'repayAndWithdraw' ||
2635
+ action === 'borrow' ||
2636
+ action === 'repay' ||
2637
+ action === 'repayAndWithdrawV2'
2638
+ ) {
2311
2639
  // todo - probably don't need to add both debt and collateral for everything here
2312
2640
  await this.addInitObligationForFarm(this.reserve, ReserveFarmKind.Debt, addAsSupportIx);
2313
2641
  if (this.outflowReserve) {
@@ -2338,7 +2666,7 @@ export class KaminoAction {
2338
2666
  }
2339
2667
 
2340
2668
  if (requestElevationGroup) {
2341
- if (action === 'repay') {
2669
+ if (action === 'repay' || action === 'repayAndWithdrawV2') {
2342
2670
  const repayObligationLiquidity = this.obligation!.borrows.get(this.reserve.address);
2343
2671
 
2344
2672
  if (!repayObligationLiquidity) {
@@ -2527,7 +2855,7 @@ export class KaminoAction {
2527
2855
  if (action === 'deposit' && this.outflowReserve) {
2528
2856
  await this.addInitReferrerTokenStateIx(this.outflowReserve);
2529
2857
  }
2530
- await this.addInitObligationIxs();
2858
+ this.addInitObligationIxs();
2531
2859
  }
2532
2860
 
2533
2861
  await this.addSupportIxsWithoutInitObligation(
@@ -2759,7 +3087,7 @@ export class KaminoAction {
2759
3087
  farms.push([
2760
3088
  ReserveFarmKind.Collateral,
2761
3089
  kaminoReserve.state.farmCollateral,
2762
- obligationFarmStatePda(this.getObligationPda(), kaminoReserve.state.farmCollateral)[0],
3090
+ obligationFarmStatePda(kaminoReserve.state.farmCollateral, this.getObligationPda()),
2763
3091
  kaminoReserve,
2764
3092
  ]);
2765
3093
  }
@@ -2767,7 +3095,7 @@ export class KaminoAction {
2767
3095
  farms.push([
2768
3096
  ReserveFarmKind.Debt,
2769
3097
  kaminoReserve.state.farmDebt,
2770
- obligationFarmStatePda(this.getObligationPda(), kaminoReserve.state.farmDebt)[0],
3098
+ obligationFarmStatePda(kaminoReserve.state.farmDebt, this.getObligationPda()),
2771
3099
  kaminoReserve,
2772
3100
  ]);
2773
3101
  }
@@ -2851,18 +3179,18 @@ export class KaminoAction {
2851
3179
  const farms: [number, PublicKey, PublicKey][] = [];
2852
3180
 
2853
3181
  if (mode === ReserveFarmKind.Collateral && isNotNullPubkey(reserve.state.farmCollateral)) {
2854
- const userPda = obligationFarmStatePda(this.getObligationPda(), reserve.state.farmCollateral)[0];
2855
- const account = await this.kaminoMarket.getConnection().getAccountInfo(userPda);
3182
+ const pda = obligationFarmStatePda(reserve.state.farmCollateral, this.getObligationPda());
3183
+ const account = await this.kaminoMarket.getConnection().getAccountInfo(pda);
2856
3184
  if (!account) {
2857
- farms.push([ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, userPda]);
3185
+ farms.push([ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, pda]);
2858
3186
  }
2859
3187
  }
2860
3188
 
2861
3189
  if (mode === ReserveFarmKind.Debt && isNotNullPubkey(reserve.state.farmDebt)) {
2862
- const userPda = obligationFarmStatePda(this.getObligationPda(), reserve.state.farmDebt)[0];
2863
- const account = await this.kaminoMarket.getConnection().getAccountInfo(userPda);
3190
+ const pda = obligationFarmStatePda(reserve.state.farmDebt, this.getObligationPda());
3191
+ const account = await this.kaminoMarket.getConnection().getAccountInfo(pda);
2864
3192
  if (!account) {
2865
- farms.push([ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, userPda]);
3193
+ farms.push([ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, pda]);
2866
3194
  }
2867
3195
  }
2868
3196
 
@@ -2896,7 +3224,7 @@ export class KaminoAction {
2896
3224
  });
2897
3225
  }
2898
3226
 
2899
- private async addInitObligationIxs() {
3227
+ private addInitObligationIxs() {
2900
3228
  if (!this.obligation) {
2901
3229
  const obligationPda = this.getObligationPda();
2902
3230
  const [userMetadataAddress, _bump] = userMetadataPda(this.owner, this.kaminoMarket.programId);
@@ -3119,7 +3447,7 @@ export class KaminoAction {
3119
3447
 
3120
3448
  let safeRepay = new BN(this.amount);
3121
3449
 
3122
- if (this.obligation && action === 'repay' && this.amount.eq(new BN(U64_MAX))) {
3450
+ if (this.obligation && (action === 'repay' || action === 'repayAndWithdrawV2') && this.amount.eq(new BN(U64_MAX))) {
3123
3451
  const borrow = this.obligation.state.borrows.find(
3124
3452
  (borrow) => borrow.borrowReserve.toString() === this.reserve.address.toString()
3125
3453
  );
@@ -3133,7 +3461,7 @@ export class KaminoAction {
3133
3461
  this.currentSlot,
3134
3462
  this.kaminoMarket.state.referralFeeBps
3135
3463
  );
3136
- // TODO: shouldn't this calc be added to all other stuff as well?
3464
+
3137
3465
  safeRepay = new BN(
3138
3466
  Math.floor(
3139
3467
  KaminoObligation.getBorrowAmount(borrow)
@@ -3161,6 +3489,7 @@ export class KaminoAction {
3161
3489
  const sendAction =
3162
3490
  action === 'deposit' ||
3163
3491
  action === 'repay' ||
3492
+ action === 'repayAndWithdrawV2' ||
3164
3493
  action === 'mint' ||
3165
3494
  (action === 'liquidate' && this.secondaryMint?.equals(NATIVE_MINT)); // only sync WSOL amount if liquidator repays SOL which is secondaryMint
3166
3495
 
@@ -3270,7 +3599,7 @@ export class KaminoAction {
3270
3599
  additionalUserTokenAccountAddress = userOutflowTokenAccountAddress;
3271
3600
  primaryMint = inflowTokenMint;
3272
3601
  secondaryMint = outflowTokenMint;
3273
- } else if (action === 'repayAndWithdraw') {
3602
+ } else if (action === 'repayAndWithdraw' || action === 'repayAndWithdrawV2') {
3274
3603
  primaryMint = inflowTokenMint;
3275
3604
  secondaryMint = outflowTokenMint;
3276
3605
  userTokenAccountAddress = userInflowTokenAccountAddress;
@@ -266,7 +266,7 @@ export class KaminoObligation {
266
266
  /**
267
267
  * @returns total borrow power of the obligation, relative to max LTV of each asset's reserve
268
268
  */
269
- getAllowedBorrowValue(): Decimal {
269
+ getMaxAllowedBorrowValue(): Decimal {
270
270
  return new Fraction(this.state.allowedBorrowValueSf).toDecimal();
271
271
  }
272
272
 
@@ -82,7 +82,7 @@ import {
82
82
  import { batchFetch, collToLamportsDecimal, ZERO } from '@kamino-finance/kliquidity-sdk';
83
83
  import { FullBPSDecimal } from '@kamino-finance/kliquidity-sdk/dist/utils/CreationParameters';
84
84
  import { FarmState } from '@kamino-finance/farms-sdk/dist';
85
- import { getAccountsInLUT, initLookupTableIx } from './lut_utils';
85
+ import { getAccountsInLUT, initLookupTableIx } from '../utils/lookupTable';
86
86
  import {
87
87
  getFarmStakeIxs,
88
88
  getFarmUnstakeAndWithdrawIxs,