@kamino-finance/klend-sdk 5.14.3 → 5.14.5

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 (122) hide show
  1. package/dist/classes/configItems.d.ts +166 -0
  2. package/dist/classes/configItems.d.ts.map +1 -0
  3. package/dist/classes/configItems.js +202 -0
  4. package/dist/classes/configItems.js.map +1 -0
  5. package/dist/classes/manager.d.ts +4 -7
  6. package/dist/classes/manager.d.ts.map +1 -1
  7. package/dist/classes/manager.js +37 -292
  8. package/dist/classes/manager.js.map +1 -1
  9. package/dist/classes/market.d.ts.map +1 -1
  10. package/dist/classes/market.js.map +1 -1
  11. package/dist/classes/reserve.d.ts +5 -14
  12. package/dist/classes/reserve.d.ts.map +1 -1
  13. package/dist/classes/reserve.js +93 -465
  14. package/dist/classes/reserve.js.map +1 -1
  15. package/dist/classes/utils.d.ts +1 -0
  16. package/dist/classes/utils.d.ts.map +1 -1
  17. package/dist/classes/utils.js +12 -0
  18. package/dist/classes/utils.js.map +1 -1
  19. package/dist/classes/vault.d.ts.map +1 -1
  20. package/dist/classes/vault.js +28 -1
  21. package/dist/classes/vault.js.map +1 -1
  22. package/dist/classes/vault_types.d.ts +1 -0
  23. package/dist/classes/vault_types.d.ts.map +1 -1
  24. package/dist/client_kamino_manager.d.ts.map +1 -1
  25. package/dist/client_kamino_manager.js +24 -11
  26. package/dist/client_kamino_manager.js.map +1 -1
  27. package/dist/idl.json +193 -16
  28. package/dist/idl_codegen/accounts/GlobalConfig.d.ts +40 -0
  29. package/dist/idl_codegen/accounts/GlobalConfig.d.ts.map +1 -0
  30. package/dist/idl_codegen/accounts/GlobalConfig.js +116 -0
  31. package/dist/idl_codegen/accounts/GlobalConfig.js.map +1 -0
  32. package/dist/idl_codegen/accounts/LendingMarket.d.ts +24 -3
  33. package/dist/idl_codegen/accounts/LendingMarket.d.ts.map +1 -1
  34. package/dist/idl_codegen/accounts/LendingMarket.js +25 -7
  35. package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -1
  36. package/dist/idl_codegen/accounts/index.d.ts +2 -0
  37. package/dist/idl_codegen/accounts/index.d.ts.map +1 -1
  38. package/dist/idl_codegen/accounts/index.js +3 -1
  39. package/dist/idl_codegen/accounts/index.js.map +1 -1
  40. package/dist/idl_codegen/errors/custom.d.ts +35 -11
  41. package/dist/idl_codegen/errors/custom.d.ts.map +1 -1
  42. package/dist/idl_codegen/errors/custom.js +64 -22
  43. package/dist/idl_codegen/errors/custom.js.map +1 -1
  44. package/dist/idl_codegen/instructions/idlMissingTypes.d.ts +2 -1
  45. package/dist/idl_codegen/instructions/idlMissingTypes.d.ts.map +1 -1
  46. package/dist/idl_codegen/instructions/idlMissingTypes.js +2 -1
  47. package/dist/idl_codegen/instructions/idlMissingTypes.js.map +1 -1
  48. package/dist/idl_codegen/instructions/index.d.ts +6 -0
  49. package/dist/idl_codegen/instructions/index.d.ts.map +1 -1
  50. package/dist/idl_codegen/instructions/index.js +7 -1
  51. package/dist/idl_codegen/instructions/index.js.map +1 -1
  52. package/dist/idl_codegen/instructions/initGlobalConfig.d.ts +10 -0
  53. package/dist/idl_codegen/instructions/initGlobalConfig.d.ts.map +1 -0
  54. package/dist/idl_codegen/instructions/initGlobalConfig.js +19 -0
  55. package/dist/idl_codegen/instructions/initGlobalConfig.js.map +1 -0
  56. package/dist/idl_codegen/instructions/updateGlobalConfig.d.ts +13 -0
  57. package/dist/idl_codegen/instructions/updateGlobalConfig.d.ts.map +1 -0
  58. package/dist/idl_codegen/instructions/updateGlobalConfig.js +61 -0
  59. package/dist/idl_codegen/instructions/updateGlobalConfig.js.map +1 -0
  60. package/dist/idl_codegen/instructions/updateGlobalConfigAdmin.d.ts +7 -0
  61. package/dist/idl_codegen/instructions/updateGlobalConfigAdmin.d.ts.map +1 -0
  62. package/dist/idl_codegen/instructions/updateGlobalConfigAdmin.js +16 -0
  63. package/dist/idl_codegen/instructions/updateGlobalConfigAdmin.js.map +1 -0
  64. package/dist/idl_codegen/instructions/updateReserveConfig.d.ts +5 -4
  65. package/dist/idl_codegen/instructions/updateReserveConfig.d.ts.map +1 -1
  66. package/dist/idl_codegen/instructions/updateReserveConfig.js +7 -5
  67. package/dist/idl_codegen/instructions/updateReserveConfig.js.map +1 -1
  68. package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts +2 -2
  69. package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts.map +1 -1
  70. package/dist/idl_codegen/instructions/withdrawProtocolFee.js +2 -6
  71. package/dist/idl_codegen/instructions/withdrawProtocolFee.js.map +1 -1
  72. package/dist/idl_codegen/types/UpdateGlobalConfigMode.d.ts +32 -0
  73. package/dist/idl_codegen/types/UpdateGlobalConfigMode.d.ts.map +1 -0
  74. package/dist/idl_codegen/types/UpdateGlobalConfigMode.js +108 -0
  75. package/dist/idl_codegen/types/UpdateGlobalConfigMode.js.map +1 -0
  76. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +33 -7
  77. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts.map +1 -1
  78. package/dist/idl_codegen/types/UpdateLendingMarketMode.js +60 -12
  79. package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -1
  80. package/dist/idl_codegen/types/index.d.ts +6 -2
  81. package/dist/idl_codegen/types/index.d.ts.map +1 -1
  82. package/dist/idl_codegen/types/index.js +3 -1
  83. package/dist/idl_codegen/types/index.js.map +1 -1
  84. package/dist/utils/instruction.d.ts.map +1 -1
  85. package/dist/utils/instruction.js.map +1 -1
  86. package/dist/utils/lookupTable.d.ts.map +1 -1
  87. package/dist/utils/lookupTable.js +1 -6
  88. package/dist/utils/lookupTable.js.map +1 -1
  89. package/dist/utils/managerTypes.js +1 -1
  90. package/dist/utils/managerTypes.js.map +1 -1
  91. package/dist/utils/seeds.d.ts +16 -0
  92. package/dist/utils/seeds.d.ts.map +1 -1
  93. package/dist/utils/seeds.js +23 -1
  94. package/dist/utils/seeds.js.map +1 -1
  95. package/package.json +1 -1
  96. package/src/classes/configItems.ts +295 -0
  97. package/src/classes/manager.ts +49 -392
  98. package/src/classes/market.ts +2 -6
  99. package/src/classes/reserve.ts +122 -786
  100. package/src/classes/utils.ts +12 -0
  101. package/src/classes/vault.ts +47 -2
  102. package/src/classes/vault_types.ts +1 -0
  103. package/src/client_kamino_manager.ts +40 -14
  104. package/src/idl.json +193 -16
  105. package/src/idl_codegen/accounts/GlobalConfig.ts +125 -0
  106. package/src/idl_codegen/accounts/LendingMarket.ts +41 -9
  107. package/src/idl_codegen/accounts/index.ts +2 -0
  108. package/src/idl_codegen/errors/custom.ts +65 -20
  109. package/src/idl_codegen/instructions/idlMissingTypes.ts +4 -2
  110. package/src/idl_codegen/instructions/index.ts +9 -0
  111. package/src/idl_codegen/instructions/initGlobalConfig.ts +30 -0
  112. package/src/idl_codegen/instructions/updateGlobalConfig.ts +47 -0
  113. package/src/idl_codegen/instructions/updateGlobalConfigAdmin.ts +24 -0
  114. package/src/idl_codegen/instructions/updateReserveConfig.ts +10 -8
  115. package/src/idl_codegen/instructions/withdrawProtocolFee.ts +4 -8
  116. package/src/idl_codegen/types/UpdateGlobalConfigMode.ts +90 -0
  117. package/src/idl_codegen/types/UpdateLendingMarketMode.ts +73 -13
  118. package/src/idl_codegen/types/index.ts +16 -2
  119. package/src/utils/instruction.ts +2 -2
  120. package/src/utils/lookupTable.ts +1 -7
  121. package/src/utils/managerTypes.ts +1 -1
  122. package/src/utils/seeds.ts +25 -0
@@ -28,7 +28,6 @@ import {
28
28
  } from './vault';
29
29
  import {
30
30
  AddAssetToMarketParams,
31
- assertNever,
32
31
  CreateKaminoMarketParams,
33
32
  createReserveIxs,
34
33
  ENV,
@@ -45,7 +44,6 @@ import {
45
44
  KaminoReserve,
46
45
  LendingMarket,
47
46
  lendingMarketAuthPda,
48
- LendingMarketFields,
49
47
  MarketWithAddress,
50
48
  parseForChangesReserveConfigAndGetIxs,
51
49
  parseOracleType,
@@ -53,7 +51,6 @@ import {
53
51
  PubkeyHashMap,
54
52
  Reserve,
55
53
  ReserveWithAddress,
56
- sameLengthArrayEquals,
57
54
  ScopeOracleConfig,
58
55
  updateEntireReserveConfigIx,
59
56
  updateLendingMarket,
@@ -65,12 +62,7 @@ import {
65
62
  import { PROGRAM_ID } from '../idl_codegen/programId';
66
63
  import { Scope, TokenMetadatas, U16_MAX } from '@kamino-finance/scope-sdk';
67
64
  import BN from 'bn.js';
68
- import {
69
- ElevationGroup,
70
- ReserveConfig,
71
- UpdateLendingMarketMode,
72
- UpdateLendingMarketModeKind,
73
- } from '../idl_codegen/types';
65
+ import { ReserveConfig, UpdateLendingMarketMode, UpdateLendingMarketModeKind } from '../idl_codegen/types';
74
66
  import Decimal from 'decimal.js';
75
67
  import * as anchor from '@coral-xyz/anchor';
76
68
  import { VaultState } from '../idl_codegen_kamino_vault/accounts';
@@ -96,6 +88,7 @@ import { FarmState } from '@kamino-finance/farms-sdk/dist';
96
88
  import SwitchboardProgram from '@switchboard-xyz/sbv2-lite';
97
89
  import { getSquadsMultisigAdminsAndThreshold, walletIsSquadsMultisig, WalletType } from '../utils/multisig';
98
90
  import { decodeVaultState } from '../utils/vault';
91
+ import { arrayElementConfigItems, ConfigUpdater } from './configItems';
99
92
 
100
93
  /**
101
94
  * KaminoManager is a class that provides a high-level interface to interact with the Kamino Lend and Kamino Vault programs, in order to create and manage a market, as well as vaults
@@ -391,7 +384,15 @@ export class KaminoManager {
391
384
  const ixs: TransactionInstruction[] = [];
392
385
 
393
386
  if (!reserveState || updateEntireConfig) {
394
- ixs.push(updateEntireReserveConfigIx(marketWithAddress, reserve, config, this._kaminoLendProgramId));
387
+ ixs.push(
388
+ updateEntireReserveConfigIx(
389
+ marketWithAddress.state.lendingMarketOwner,
390
+ marketWithAddress.address,
391
+ reserve,
392
+ config,
393
+ this._kaminoLendProgramId
394
+ )
395
+ );
395
396
  } else {
396
397
  ixs.push(
397
398
  ...parseForChangesReserveConfigAndGetIxs(
@@ -1269,383 +1270,51 @@ export class KaminoManager {
1269
1270
  };
1270
1271
  }
1271
1272
  } // KaminoManager
1272
- export type BaseLendingMarketKey = keyof LendingMarketFields;
1273
- const EXCLUDED_LENDING_MARKET_KEYS = [
1274
- 'version',
1275
- 'bumpSeed',
1276
- 'reserved0',
1277
- 'reserved1',
1278
- 'padding1',
1279
- 'padding2',
1280
- 'elevationGroupPadding',
1281
- 'quoteCurrency',
1282
- ] as const;
1283
- export type ExcludedLendingMarketKey = (typeof EXCLUDED_LENDING_MARKET_KEYS)[number];
1284
-
1285
- function isExcludedLendingMarketKey(value: unknown): value is ExcludedLendingMarketKey {
1286
- return EXCLUDED_LENDING_MARKET_KEYS.includes(value as ExcludedLendingMarketKey);
1287
- }
1288
1273
 
1289
- export type LendingMarketKey = Exclude<BaseLendingMarketKey, ExcludedLendingMarketKey>;
1290
-
1291
- const updateLendingMarketConfig = (
1292
- key: LendingMarketKey,
1293
- market: LendingMarket,
1294
- newMarket: LendingMarket
1295
- ): { mode: number; value: Buffer }[] => {
1296
- const updateLendingMarketIxsArgs: { mode: number; value: Buffer }[] = [];
1297
- switch (key) {
1298
- case 'lendingMarketOwner': {
1299
- if (!market.lendingMarketOwner.equals(newMarket.lendingMarketOwner)) {
1300
- updateLendingMarketIxsArgs.push({
1301
- mode: UpdateLendingMarketMode.UpdateOwner.discriminator,
1302
- value: updateMarketConfigEncodedValue(
1303
- UpdateLendingMarketMode.UpdateOwner.discriminator,
1304
- newMarket.lendingMarketOwner
1305
- ),
1306
- });
1307
- }
1308
- break;
1309
- }
1310
- case 'lendingMarketOwnerCached':
1311
- if (!market.lendingMarketOwnerCached.equals(newMarket.lendingMarketOwnerCached)) {
1312
- updateLendingMarketIxsArgs.push({
1313
- mode: UpdateLendingMarketMode.UpdateOwner.discriminator,
1314
- value: updateMarketConfigEncodedValue(
1315
- UpdateLendingMarketMode.UpdateOwner.discriminator,
1316
- newMarket.lendingMarketOwnerCached
1317
- ),
1318
- });
1319
- }
1320
- break;
1321
- case 'referralFeeBps':
1322
- if (market.referralFeeBps !== newMarket.referralFeeBps) {
1323
- updateLendingMarketIxsArgs.push({
1324
- mode: UpdateLendingMarketMode.UpdateReferralFeeBps.discriminator,
1325
- value: updateMarketConfigEncodedValue(
1326
- UpdateLendingMarketMode.UpdateReferralFeeBps.discriminator,
1327
- newMarket.referralFeeBps
1328
- ),
1329
- });
1330
- }
1331
- break;
1332
- case 'emergencyMode':
1333
- if (market.emergencyMode !== newMarket.emergencyMode) {
1334
- updateLendingMarketIxsArgs.push({
1335
- mode: UpdateLendingMarketMode.UpdateEmergencyMode.discriminator,
1336
- value: updateMarketConfigEncodedValue(
1337
- UpdateLendingMarketMode.UpdateEmergencyMode.discriminator,
1338
- newMarket.emergencyMode
1339
- ),
1340
- });
1341
- }
1342
- break;
1343
- case 'autodeleverageEnabled':
1344
- if (market.autodeleverageEnabled !== newMarket.autodeleverageEnabled) {
1345
- updateLendingMarketIxsArgs.push({
1346
- mode: UpdateLendingMarketMode.UpdateAutodeleverageEnabled.discriminator,
1347
- value: updateMarketConfigEncodedValue(
1348
- UpdateLendingMarketMode.UpdateAutodeleverageEnabled.discriminator,
1349
- newMarket.autodeleverageEnabled
1350
- ),
1351
- });
1352
- }
1353
- break;
1354
- case 'borrowDisabled':
1355
- if (market.borrowDisabled !== newMarket.borrowDisabled) {
1356
- updateLendingMarketIxsArgs.push({
1357
- mode: UpdateLendingMarketMode.UpdateBorrowingDisabled.discriminator,
1358
- value: updateMarketConfigEncodedValue(
1359
- UpdateLendingMarketMode.UpdateBorrowingDisabled.discriminator,
1360
- newMarket.borrowDisabled
1361
- ),
1362
- });
1363
- }
1364
- break;
1365
- case 'priceRefreshTriggerToMaxAgePct':
1366
- if (market.priceRefreshTriggerToMaxAgePct !== newMarket.priceRefreshTriggerToMaxAgePct) {
1367
- updateLendingMarketIxsArgs.push({
1368
- mode: UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.discriminator,
1369
- value: updateMarketConfigEncodedValue(
1370
- UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.discriminator,
1371
- newMarket.priceRefreshTriggerToMaxAgePct
1372
- ),
1373
- });
1374
- }
1375
- break;
1376
- case 'liquidationMaxDebtCloseFactorPct':
1377
- if (market.liquidationMaxDebtCloseFactorPct !== newMarket.liquidationMaxDebtCloseFactorPct) {
1378
- updateLendingMarketIxsArgs.push({
1379
- mode: UpdateLendingMarketMode.UpdateLiquidationCloseFactor.discriminator,
1380
- value: updateMarketConfigEncodedValue(
1381
- UpdateLendingMarketMode.UpdateLiquidationCloseFactor.discriminator,
1382
- newMarket.liquidationMaxDebtCloseFactorPct
1383
- ),
1384
- });
1385
- }
1386
- break;
1387
- case 'insolvencyRiskUnhealthyLtvPct':
1388
- if (market.insolvencyRiskUnhealthyLtvPct !== newMarket.insolvencyRiskUnhealthyLtvPct) {
1389
- updateLendingMarketIxsArgs.push({
1390
- mode: UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.discriminator,
1391
- value: updateMarketConfigEncodedValue(
1392
- UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.discriminator,
1393
- newMarket.insolvencyRiskUnhealthyLtvPct
1394
- ),
1395
- });
1396
- }
1397
- break;
1398
- case 'minFullLiquidationValueThreshold':
1399
- if (!market.minFullLiquidationValueThreshold.eq(newMarket.minFullLiquidationValueThreshold)) {
1400
- updateLendingMarketIxsArgs.push({
1401
- mode: UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.discriminator,
1402
- value: updateMarketConfigEncodedValue(
1403
- UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.discriminator,
1404
- newMarket.minFullLiquidationValueThreshold.toNumber()
1405
- ),
1406
- });
1407
- }
1408
- break;
1409
- case 'maxLiquidatableDebtMarketValueAtOnce':
1410
- if (!market.maxLiquidatableDebtMarketValueAtOnce.eq(newMarket.maxLiquidatableDebtMarketValueAtOnce)) {
1411
- updateLendingMarketIxsArgs.push({
1412
- mode: UpdateLendingMarketMode.UpdateLiquidationMaxValue.discriminator,
1413
- value: updateMarketConfigEncodedValue(
1414
- UpdateLendingMarketMode.UpdateLiquidationMaxValue.discriminator,
1415
- newMarket.maxLiquidatableDebtMarketValueAtOnce.toNumber()
1416
- ),
1417
- });
1418
- }
1419
- break;
1420
- case 'globalAllowedBorrowValue':
1421
- if (!market.globalAllowedBorrowValue.eq(newMarket.globalAllowedBorrowValue)) {
1422
- updateLendingMarketIxsArgs.push({
1423
- mode: UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.discriminator,
1424
- value: updateMarketConfigEncodedValue(
1425
- UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.discriminator,
1426
- newMarket.globalAllowedBorrowValue.toNumber()
1427
- ),
1428
- });
1429
- }
1430
- break;
1431
- case 'riskCouncil':
1432
- if (!market.riskCouncil.equals(newMarket.riskCouncil)) {
1433
- updateLendingMarketIxsArgs.push({
1434
- mode: UpdateLendingMarketMode.UpdateRiskCouncil.discriminator,
1435
- value: updateMarketConfigEncodedValue(
1436
- UpdateLendingMarketMode.UpdateRiskCouncil.discriminator,
1437
- newMarket.riskCouncil
1438
- ),
1439
- });
1440
- }
1441
- break;
1442
- case 'elevationGroups':
1443
- let elevationGroupsDiffs = 0;
1444
- for (let i = 0; i < market.elevationGroups.length; i++) {
1445
- if (
1446
- market.elevationGroups[i].id !== newMarket.elevationGroups[i].id ||
1447
- market.elevationGroups[i].maxLiquidationBonusBps !== newMarket.elevationGroups[i].maxLiquidationBonusBps ||
1448
- market.elevationGroups[i].ltvPct !== newMarket.elevationGroups[i].ltvPct ||
1449
- market.elevationGroups[i].liquidationThresholdPct !== newMarket.elevationGroups[i].liquidationThresholdPct ||
1450
- market.elevationGroups[i].allowNewLoans !== newMarket.elevationGroups[i].allowNewLoans ||
1451
- market.elevationGroups[i].maxReservesAsCollateral !== newMarket.elevationGroups[i].maxReservesAsCollateral ||
1452
- !market.elevationGroups[i].debtReserve.equals(newMarket.elevationGroups[i].debtReserve)
1453
- ) {
1454
- updateLendingMarketIxsArgs.push({
1455
- mode: UpdateLendingMarketMode.UpdateElevationGroup.discriminator,
1456
- value: updateMarketConfigEncodedValue(
1457
- UpdateLendingMarketMode.UpdateElevationGroup.discriminator,
1458
- newMarket.elevationGroups[i]
1459
- ),
1460
- });
1461
- elevationGroupsDiffs++;
1462
- }
1463
- }
1464
- if (elevationGroupsDiffs > 1) {
1465
- throw new Error('Can only update 1 elevation group at a time');
1466
- }
1467
- break;
1468
- case 'minNetValueInObligationSf':
1469
- if (!market.minNetValueInObligationSf.eq(newMarket.minNetValueInObligationSf)) {
1470
- updateLendingMarketIxsArgs.push({
1471
- mode: UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.discriminator,
1472
- value: updateMarketConfigEncodedValue(
1473
- UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.discriminator,
1474
- newMarket.minNetValueInObligationSf.toString()
1475
- ),
1476
- });
1477
- }
1478
- break;
1479
- case 'minValueSkipLiquidationBfChecks':
1480
- if (!market.minValueSkipLiquidationBfChecks.eq(newMarket.minValueSkipLiquidationBfChecks)) {
1481
- updateLendingMarketIxsArgs.push({
1482
- mode: UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.discriminator,
1483
- value: updateMarketConfigEncodedValue(
1484
- UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.discriminator,
1485
- newMarket.minValueSkipLiquidationBfChecks.toNumber()
1486
- ),
1487
- });
1488
- }
1489
- break;
1490
- case 'minValueSkipLiquidationLtvChecks':
1491
- if (!market.minValueSkipLiquidationLtvChecks.eq(newMarket.minValueSkipLiquidationLtvChecks)) {
1492
- updateLendingMarketIxsArgs.push({
1493
- mode: UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.discriminator,
1494
- value: updateMarketConfigEncodedValue(
1495
- UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.discriminator,
1496
- newMarket.minValueSkipLiquidationLtvChecks.toNumber()
1497
- ),
1498
- });
1499
- }
1500
- break;
1501
- case 'individualAutodeleverageMarginCallPeriodSecs':
1502
- if (
1503
- market.individualAutodeleverageMarginCallPeriodSecs !== newMarket.individualAutodeleverageMarginCallPeriodSecs
1504
- ) {
1505
- updateLendingMarketIxsArgs.push({
1506
- mode: UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.discriminator,
1507
- value: updateMarketConfigEncodedValue(
1508
- UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.discriminator,
1509
- newMarket.individualAutodeleverageMarginCallPeriodSecs.toNumber()
1510
- ),
1511
- });
1512
- }
1513
- break;
1514
- case 'name':
1515
- if (!sameLengthArrayEquals(market.name, newMarket.name)) {
1516
- updateLendingMarketIxsArgs.push({
1517
- mode: UpdateLendingMarketMode.UpdateName.discriminator,
1518
- value: updateMarketConfigEncodedValue(UpdateLendingMarketMode.UpdateName.discriminator, newMarket.name),
1519
- });
1520
- }
1521
- break;
1522
- case 'minInitialDepositAmount':
1523
- if (!market.minInitialDepositAmount.eq(newMarket.minInitialDepositAmount)) {
1524
- updateLendingMarketIxsArgs.push({
1525
- mode: UpdateLendingMarketMode.UpdateInitialDepositAmount.discriminator,
1526
- value: updateMarketConfigEncodedValue(
1527
- UpdateLendingMarketMode.UpdateInitialDepositAmount.discriminator,
1528
- newMarket.minInitialDepositAmount.toNumber()
1529
- ),
1530
- });
1531
- }
1532
- break;
1533
- case 'obligationOrdersEnabled':
1534
- if (market.obligationOrdersEnabled !== newMarket.obligationOrdersEnabled) {
1535
- updateLendingMarketIxsArgs.push({
1536
- mode: UpdateLendingMarketMode.UpdateObligationOrdersEnabled.discriminator,
1537
- value: updateMarketConfigEncodedValue(
1538
- UpdateLendingMarketMode.UpdateObligationOrdersEnabled.discriminator,
1539
- newMarket.obligationOrdersEnabled
1540
- ),
1541
- });
1542
- }
1543
- break;
1544
- default:
1545
- assertNever(key);
1546
- }
1547
- return updateLendingMarketIxsArgs;
1548
- };
1274
+ export const MARKET_UPDATER = new ConfigUpdater(UpdateLendingMarketMode.fromDecoded, LendingMarket, (config) => ({
1275
+ [UpdateLendingMarketMode.UpdateOwner.kind]: config.lendingMarketOwnerCached,
1276
+ [UpdateLendingMarketMode.UpdateImmutableFlag.kind]: config.immutable,
1277
+ [UpdateLendingMarketMode.UpdateEmergencyMode.kind]: config.emergencyMode,
1278
+ [UpdateLendingMarketMode.UpdateLiquidationCloseFactor.kind]: config.liquidationMaxDebtCloseFactorPct,
1279
+ [UpdateLendingMarketMode.UpdateLiquidationMaxValue.kind]: config.maxLiquidatableDebtMarketValueAtOnce,
1280
+ [UpdateLendingMarketMode.DeprecatedUpdateGlobalUnhealthyBorrow.kind]: [], // deprecated
1281
+ [UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.kind]: config.globalAllowedBorrowValue,
1282
+ [UpdateLendingMarketMode.UpdateRiskCouncil.kind]: config.riskCouncil,
1283
+ [UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.kind]: config.minFullLiquidationValueThreshold,
1284
+ [UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.kind]: config.insolvencyRiskUnhealthyLtvPct,
1285
+ [UpdateLendingMarketMode.UpdateElevationGroup.kind]: arrayElementConfigItems(config.elevationGroups),
1286
+ [UpdateLendingMarketMode.UpdateReferralFeeBps.kind]: config.referralFeeBps,
1287
+ [UpdateLendingMarketMode.DeprecatedUpdateMultiplierPoints.kind]: [], // deprecated
1288
+ [UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.kind]: config.priceRefreshTriggerToMaxAgePct,
1289
+ [UpdateLendingMarketMode.UpdateAutodeleverageEnabled.kind]: config.autodeleverageEnabled,
1290
+ [UpdateLendingMarketMode.UpdateBorrowingDisabled.kind]: config.borrowDisabled,
1291
+ [UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.kind]: config.minNetValueInObligationSf,
1292
+ [UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.kind]: config.minValueSkipLiquidationLtvChecks,
1293
+ [UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.kind]: config.minValueSkipLiquidationBfChecks,
1294
+ [UpdateLendingMarketMode.UpdatePaddingFields.kind]: [], // we do not update padding this way
1295
+ [UpdateLendingMarketMode.UpdateName.kind]: config.name,
1296
+ [UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.kind]:
1297
+ config.individualAutodeleverageMarginCallPeriodSecs,
1298
+ [UpdateLendingMarketMode.UpdateInitialDepositAmount.kind]: config.minInitialDepositAmount,
1299
+ [UpdateLendingMarketMode.UpdateObligationOrderCreationEnabled.kind]: config.obligationOrderCreationEnabled,
1300
+ [UpdateLendingMarketMode.UpdateObligationOrderExecutionEnabled.kind]: config.obligationOrderExecutionEnabled,
1301
+ }));
1549
1302
 
1550
1303
  function parseForChangesMarketConfigAndGetIxs(
1551
1304
  marketWithAddress: MarketWithAddress,
1552
1305
  newMarket: LendingMarket,
1553
1306
  programId: PublicKey
1554
1307
  ): TransactionInstruction[] {
1555
- const market = marketWithAddress.state;
1556
- const updateLendingMarketIxsArgs: { mode: number; value: Buffer }[] = [];
1557
-
1558
- for (const key in market.toJSON()) {
1559
- if (isExcludedLendingMarketKey(key)) {
1560
- continue;
1561
- }
1562
- updateLendingMarketIxsArgs.push(...updateLendingMarketConfig(key as LendingMarketKey, market, newMarket));
1563
- }
1564
-
1565
- const ixs: TransactionInstruction[] = [];
1566
-
1567
- updateLendingMarketIxsArgs.forEach((updateLendingMarketConfigArgs) => {
1568
- ixs.push(
1569
- updateMarketConfigIx(
1570
- marketWithAddress,
1571
- updateLendingMarketConfigArgs.mode,
1572
- updateLendingMarketConfigArgs.value,
1573
- programId
1574
- )
1575
- );
1576
- });
1577
-
1578
- return ixs;
1579
- }
1580
-
1581
- function updateMarketConfigEncodedValue(
1582
- discriminator: UpdateLendingMarketModeKind['discriminator'],
1583
- value: number | number[] | PublicKey | ElevationGroup | string
1584
- ): Buffer {
1585
- let buffer: Buffer = Buffer.alloc(72);
1586
- let pkBuffer: Buffer;
1587
- let valueBigInt: bigint;
1588
- let valueArray: number[];
1589
-
1590
- switch (discriminator) {
1591
- case UpdateLendingMarketMode.UpdateEmergencyMode.discriminator:
1592
- case UpdateLendingMarketMode.UpdateLiquidationCloseFactor.discriminator:
1593
- case UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.discriminator:
1594
- case UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.discriminator:
1595
- case UpdateLendingMarketMode.UpdateAutodeleverageEnabled.discriminator:
1596
- case UpdateLendingMarketMode.UpdateObligationOrdersEnabled.discriminator:
1597
- case UpdateLendingMarketMode.UpdateBorrowingDisabled.discriminator:
1598
- buffer.writeUIntLE(value as number, 0, 1);
1599
- break;
1600
- case UpdateLendingMarketMode.UpdateReferralFeeBps.discriminator:
1601
- buffer.writeUInt16LE(value as number, 0);
1602
- break;
1603
- case UpdateLendingMarketMode.UpdateLiquidationMaxValue.discriminator:
1604
- case UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.discriminator:
1605
- case UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.discriminator:
1606
- case UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.discriminator:
1607
- case UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.discriminator:
1608
- case UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.discriminator:
1609
- case UpdateLendingMarketMode.UpdateInitialDepositAmount.discriminator:
1610
- value = value as number;
1611
- buffer.writeBigUint64LE(BigInt(value), 0);
1612
- break;
1613
- case UpdateLendingMarketMode.UpdateOwner.discriminator:
1614
- case UpdateLendingMarketMode.UpdateRiskCouncil.discriminator:
1615
- pkBuffer = (value as PublicKey).toBuffer();
1616
- pkBuffer.copy(buffer, 0);
1617
- break;
1618
- case UpdateLendingMarketMode.UpdateElevationGroup.discriminator:
1619
- buffer = serializeElevationGroup(value as ElevationGroup);
1620
- break;
1621
- case UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.discriminator:
1622
- valueBigInt = BigInt(value as string);
1623
- for (let i = 0; i < 16; i++) {
1624
- buffer[15 - i] = Number((valueBigInt >> BigInt(i * 8)) & BigInt(0xff));
1625
- }
1626
- break;
1627
- case UpdateLendingMarketMode.UpdateName.discriminator:
1628
- valueArray = value as number[];
1629
- for (let i = 0; i < valueArray.length; i++) {
1630
- buffer.writeUIntLE(valueArray[i], i, 1);
1631
- }
1632
- break;
1633
- case UpdateLendingMarketMode.UpdatePaddingFields.discriminator:
1634
- case UpdateLendingMarketMode.DeprecatedUpdateGlobalUnhealthyBorrow.discriminator:
1635
- case UpdateLendingMarketMode.DeprecatedUpdateMultiplierPoints.discriminator:
1636
- // Deliberately skipped - we are not updating padding or deprecated fields using this method
1637
- break;
1638
- default:
1639
- assertNever(discriminator);
1640
- }
1641
-
1642
- return buffer;
1308
+ const encodedMarketUpdates = MARKET_UPDATER.encodeAllUpdates(marketWithAddress.state, newMarket);
1309
+ return encodedMarketUpdates.map((encodedMarketUpdate) =>
1310
+ updateMarketConfigIx(marketWithAddress, encodedMarketUpdate.mode, encodedMarketUpdate.value, programId)
1311
+ );
1643
1312
  }
1644
1313
 
1645
1314
  function updateMarketConfigIx(
1646
1315
  marketWithAddress: MarketWithAddress,
1647
- modeDiscriminator: number,
1648
- value: Buffer,
1316
+ mode: UpdateLendingMarketModeKind,
1317
+ value: Uint8Array,
1649
1318
  programId: PublicKey
1650
1319
  ): TransactionInstruction {
1651
1320
  const accounts: UpdateLendingMarketAccounts = {
@@ -1654,25 +1323,13 @@ function updateMarketConfigIx(
1654
1323
  };
1655
1324
 
1656
1325
  const args: UpdateLendingMarketArgs = {
1657
- mode: new anchor.BN(modeDiscriminator),
1658
- value: [...value],
1326
+ mode: new anchor.BN(mode.discriminator),
1327
+ // NOTE: the Market's update handler expects a `[u8; 72]` (contrary to e.g. the Reserve's update handler accepting
1328
+ // `Vec<u8>`). Hence, we need to add explicit padding here:
1329
+ value: [...value, ...Array(72 - value.length).fill(0)],
1659
1330
  };
1660
1331
 
1661
1332
  const ix = updateLendingMarket(args, accounts, programId);
1662
1333
 
1663
1334
  return ix;
1664
1335
  }
1665
-
1666
- function serializeElevationGroup(elevationGroup: ElevationGroup): Buffer {
1667
- const buffer = Buffer.alloc(72);
1668
- buffer.writeUInt16LE(elevationGroup.maxLiquidationBonusBps, 0);
1669
- buffer.writeUIntLE(elevationGroup.id, 2, 1);
1670
- buffer.writeUIntLE(elevationGroup.ltvPct, 3, 1);
1671
- buffer.writeUIntLE(elevationGroup.liquidationThresholdPct, 4, 1);
1672
- buffer.writeUIntLE(elevationGroup.allowNewLoans, 5, 1);
1673
- buffer.writeUIntLE(elevationGroup.maxReservesAsCollateral, 6, 1);
1674
- buffer.writeUIntLE(elevationGroup.padding0, 7, 1);
1675
- const debtReserveBuffer = elevationGroup.debtReserve.toBuffer();
1676
- debtReserveBuffer.copy(buffer, 8);
1677
- return buffer;
1678
- }
@@ -524,13 +524,9 @@ export class KaminoMarket {
524
524
  getRewardPrice
525
525
  );
526
526
  }
527
-
527
+
528
528
  if (isNotNullPubkey(debtFarmAddress)) {
529
- result.borrowingRewards = await this.getRewardInfoForFarm(
530
- debtFarmAddress,
531
- totalBorrowAmount,
532
- getRewardPrice
533
- );
529
+ result.borrowingRewards = await this.getRewardInfoForFarm(debtFarmAddress, totalBorrowAmount, getRewardPrice);
534
530
  }
535
531
 
536
532
  return result;