@kamino-finance/klend-sdk 5.13.19 → 5.13.21-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 (124) 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/farm_utils.d.ts.map +1 -1
  6. package/dist/classes/farm_utils.js +3 -10
  7. package/dist/classes/farm_utils.js.map +1 -1
  8. package/dist/classes/manager.d.ts +5 -8
  9. package/dist/classes/manager.d.ts.map +1 -1
  10. package/dist/classes/manager.js +39 -294
  11. package/dist/classes/manager.js.map +1 -1
  12. package/dist/classes/reserve.d.ts +5 -14
  13. package/dist/classes/reserve.d.ts.map +1 -1
  14. package/dist/classes/reserve.js +91 -463
  15. package/dist/classes/reserve.js.map +1 -1
  16. package/dist/classes/utils.d.ts +1 -0
  17. package/dist/classes/utils.d.ts.map +1 -1
  18. package/dist/classes/utils.js +12 -0
  19. package/dist/classes/utils.js.map +1 -1
  20. package/dist/classes/vault.d.ts +1 -1
  21. package/dist/classes/vault.d.ts.map +1 -1
  22. package/dist/classes/vault.js +4 -5
  23. package/dist/classes/vault.js.map +1 -1
  24. package/dist/client_kamino_manager.d.ts.map +1 -1
  25. package/dist/client_kamino_manager.js +27 -26
  26. package/dist/client_kamino_manager.js.map +1 -1
  27. package/dist/idl.json +186 -9
  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 +25 -1
  41. package/dist/idl_codegen/errors/custom.d.ts.map +1 -1
  42. package/dist/idl_codegen/errors/custom.js +43 -1
  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 +4 -3
  65. package/dist/idl_codegen/instructions/updateReserveConfig.d.ts.map +1 -1
  66. package/dist/idl_codegen/instructions/updateReserveConfig.js +5 -3
  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/accountListing.d.ts +1 -1
  85. package/dist/utils/accountListing.d.ts.map +1 -1
  86. package/dist/utils/accountListing.js +2 -2
  87. package/dist/utils/accountListing.js.map +1 -1
  88. package/dist/utils/lookupTable.d.ts.map +1 -1
  89. package/dist/utils/lookupTable.js +1 -6
  90. package/dist/utils/lookupTable.js.map +1 -1
  91. package/dist/utils/managerTypes.d.ts.map +1 -1
  92. package/dist/utils/managerTypes.js +10 -4
  93. package/dist/utils/managerTypes.js.map +1 -1
  94. package/dist/utils/seeds.d.ts +16 -0
  95. package/dist/utils/seeds.d.ts.map +1 -1
  96. package/dist/utils/seeds.js +23 -1
  97. package/dist/utils/seeds.js.map +1 -1
  98. package/package.json +1 -1
  99. package/src/classes/configItems.ts +295 -0
  100. package/src/classes/farm_utils.ts +3 -11
  101. package/src/classes/manager.ts +51 -394
  102. package/src/classes/reserve.ts +119 -783
  103. package/src/classes/utils.ts +12 -0
  104. package/src/classes/vault.ts +6 -7
  105. package/src/client_kamino_manager.ts +42 -34
  106. package/src/idl.json +186 -9
  107. package/src/idl_codegen/accounts/GlobalConfig.ts +125 -0
  108. package/src/idl_codegen/accounts/LendingMarket.ts +41 -9
  109. package/src/idl_codegen/accounts/index.ts +2 -0
  110. package/src/idl_codegen/errors/custom.ts +45 -0
  111. package/src/idl_codegen/instructions/idlMissingTypes.ts +4 -2
  112. package/src/idl_codegen/instructions/index.ts +9 -0
  113. package/src/idl_codegen/instructions/initGlobalConfig.ts +30 -0
  114. package/src/idl_codegen/instructions/updateGlobalConfig.ts +47 -0
  115. package/src/idl_codegen/instructions/updateGlobalConfigAdmin.ts +24 -0
  116. package/src/idl_codegen/instructions/updateReserveConfig.ts +7 -5
  117. package/src/idl_codegen/instructions/withdrawProtocolFee.ts +4 -8
  118. package/src/idl_codegen/types/UpdateGlobalConfigMode.ts +90 -0
  119. package/src/idl_codegen/types/UpdateLendingMarketMode.ts +73 -13
  120. package/src/idl_codegen/types/index.ts +16 -2
  121. package/src/utils/accountListing.ts +2 -3
  122. package/src/utils/lookupTable.ts +1 -7
  123. package/src/utils/managerTypes.ts +10 -4
  124. 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(
@@ -657,9 +658,9 @@ export class KaminoManager {
657
658
  * Get all lending markets
658
659
  * @returns an array of all lending markets
659
660
  */
660
- async getAllMarkets(programId: PublicKey = PROGRAM_ID): Promise<KaminoMarket[]> {
661
+ async getAllMarkets(): Promise<KaminoMarket[]> {
661
662
  // Get all lending markets
662
- const marketGenerator = getAllLendingMarketAccounts(this.getConnection(), programId);
663
+ const marketGenerator = getAllLendingMarketAccounts(this.getConnection());
663
664
  const slotDuration = await getMedianSlotDurationInMsFromLastEpochs();
664
665
 
665
666
  const lendingMarketPairs: [PublicKey, LendingMarket][] = [];
@@ -1247,383 +1248,51 @@ export class KaminoManager {
1247
1248
  };
1248
1249
  }
1249
1250
  } // KaminoManager
1250
- export type BaseLendingMarketKey = keyof LendingMarketFields;
1251
- const EXCLUDED_LENDING_MARKET_KEYS = [
1252
- 'version',
1253
- 'bumpSeed',
1254
- 'reserved0',
1255
- 'reserved1',
1256
- 'padding1',
1257
- 'padding2',
1258
- 'elevationGroupPadding',
1259
- 'quoteCurrency',
1260
- ] as const;
1261
- export type ExcludedLendingMarketKey = (typeof EXCLUDED_LENDING_MARKET_KEYS)[number];
1262
-
1263
- function isExcludedLendingMarketKey(value: unknown): value is ExcludedLendingMarketKey {
1264
- return EXCLUDED_LENDING_MARKET_KEYS.includes(value as ExcludedLendingMarketKey);
1265
- }
1266
1251
 
1267
- export type LendingMarketKey = Exclude<BaseLendingMarketKey, ExcludedLendingMarketKey>;
1268
-
1269
- const updateLendingMarketConfig = (
1270
- key: LendingMarketKey,
1271
- market: LendingMarket,
1272
- newMarket: LendingMarket
1273
- ): { mode: number; value: Buffer }[] => {
1274
- const updateLendingMarketIxsArgs: { mode: number; value: Buffer }[] = [];
1275
- switch (key) {
1276
- case 'lendingMarketOwner': {
1277
- if (!market.lendingMarketOwner.equals(newMarket.lendingMarketOwner)) {
1278
- updateLendingMarketIxsArgs.push({
1279
- mode: UpdateLendingMarketMode.UpdateOwner.discriminator,
1280
- value: updateMarketConfigEncodedValue(
1281
- UpdateLendingMarketMode.UpdateOwner.discriminator,
1282
- newMarket.lendingMarketOwner
1283
- ),
1284
- });
1285
- }
1286
- break;
1287
- }
1288
- case 'lendingMarketOwnerCached':
1289
- if (!market.lendingMarketOwnerCached.equals(newMarket.lendingMarketOwnerCached)) {
1290
- updateLendingMarketIxsArgs.push({
1291
- mode: UpdateLendingMarketMode.UpdateOwner.discriminator,
1292
- value: updateMarketConfigEncodedValue(
1293
- UpdateLendingMarketMode.UpdateOwner.discriminator,
1294
- newMarket.lendingMarketOwnerCached
1295
- ),
1296
- });
1297
- }
1298
- break;
1299
- case 'referralFeeBps':
1300
- if (market.referralFeeBps !== newMarket.referralFeeBps) {
1301
- updateLendingMarketIxsArgs.push({
1302
- mode: UpdateLendingMarketMode.UpdateReferralFeeBps.discriminator,
1303
- value: updateMarketConfigEncodedValue(
1304
- UpdateLendingMarketMode.UpdateReferralFeeBps.discriminator,
1305
- newMarket.referralFeeBps
1306
- ),
1307
- });
1308
- }
1309
- break;
1310
- case 'emergencyMode':
1311
- if (market.emergencyMode !== newMarket.emergencyMode) {
1312
- updateLendingMarketIxsArgs.push({
1313
- mode: UpdateLendingMarketMode.UpdateEmergencyMode.discriminator,
1314
- value: updateMarketConfigEncodedValue(
1315
- UpdateLendingMarketMode.UpdateEmergencyMode.discriminator,
1316
- newMarket.emergencyMode
1317
- ),
1318
- });
1319
- }
1320
- break;
1321
- case 'autodeleverageEnabled':
1322
- if (market.autodeleverageEnabled !== newMarket.autodeleverageEnabled) {
1323
- updateLendingMarketIxsArgs.push({
1324
- mode: UpdateLendingMarketMode.UpdateAutodeleverageEnabled.discriminator,
1325
- value: updateMarketConfigEncodedValue(
1326
- UpdateLendingMarketMode.UpdateAutodeleverageEnabled.discriminator,
1327
- newMarket.autodeleverageEnabled
1328
- ),
1329
- });
1330
- }
1331
- break;
1332
- case 'borrowDisabled':
1333
- if (market.borrowDisabled !== newMarket.borrowDisabled) {
1334
- updateLendingMarketIxsArgs.push({
1335
- mode: UpdateLendingMarketMode.UpdateBorrowingDisabled.discriminator,
1336
- value: updateMarketConfigEncodedValue(
1337
- UpdateLendingMarketMode.UpdateBorrowingDisabled.discriminator,
1338
- newMarket.borrowDisabled
1339
- ),
1340
- });
1341
- }
1342
- break;
1343
- case 'priceRefreshTriggerToMaxAgePct':
1344
- if (market.priceRefreshTriggerToMaxAgePct !== newMarket.priceRefreshTriggerToMaxAgePct) {
1345
- updateLendingMarketIxsArgs.push({
1346
- mode: UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.discriminator,
1347
- value: updateMarketConfigEncodedValue(
1348
- UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.discriminator,
1349
- newMarket.priceRefreshTriggerToMaxAgePct
1350
- ),
1351
- });
1352
- }
1353
- break;
1354
- case 'liquidationMaxDebtCloseFactorPct':
1355
- if (market.liquidationMaxDebtCloseFactorPct !== newMarket.liquidationMaxDebtCloseFactorPct) {
1356
- updateLendingMarketIxsArgs.push({
1357
- mode: UpdateLendingMarketMode.UpdateLiquidationCloseFactor.discriminator,
1358
- value: updateMarketConfigEncodedValue(
1359
- UpdateLendingMarketMode.UpdateLiquidationCloseFactor.discriminator,
1360
- newMarket.liquidationMaxDebtCloseFactorPct
1361
- ),
1362
- });
1363
- }
1364
- break;
1365
- case 'insolvencyRiskUnhealthyLtvPct':
1366
- if (market.insolvencyRiskUnhealthyLtvPct !== newMarket.insolvencyRiskUnhealthyLtvPct) {
1367
- updateLendingMarketIxsArgs.push({
1368
- mode: UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.discriminator,
1369
- value: updateMarketConfigEncodedValue(
1370
- UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.discriminator,
1371
- newMarket.insolvencyRiskUnhealthyLtvPct
1372
- ),
1373
- });
1374
- }
1375
- break;
1376
- case 'minFullLiquidationValueThreshold':
1377
- if (!market.minFullLiquidationValueThreshold.eq(newMarket.minFullLiquidationValueThreshold)) {
1378
- updateLendingMarketIxsArgs.push({
1379
- mode: UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.discriminator,
1380
- value: updateMarketConfigEncodedValue(
1381
- UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.discriminator,
1382
- newMarket.minFullLiquidationValueThreshold.toNumber()
1383
- ),
1384
- });
1385
- }
1386
- break;
1387
- case 'maxLiquidatableDebtMarketValueAtOnce':
1388
- if (!market.maxLiquidatableDebtMarketValueAtOnce.eq(newMarket.maxLiquidatableDebtMarketValueAtOnce)) {
1389
- updateLendingMarketIxsArgs.push({
1390
- mode: UpdateLendingMarketMode.UpdateLiquidationMaxValue.discriminator,
1391
- value: updateMarketConfigEncodedValue(
1392
- UpdateLendingMarketMode.UpdateLiquidationMaxValue.discriminator,
1393
- newMarket.maxLiquidatableDebtMarketValueAtOnce.toNumber()
1394
- ),
1395
- });
1396
- }
1397
- break;
1398
- case 'globalAllowedBorrowValue':
1399
- if (!market.globalAllowedBorrowValue.eq(newMarket.globalAllowedBorrowValue)) {
1400
- updateLendingMarketIxsArgs.push({
1401
- mode: UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.discriminator,
1402
- value: updateMarketConfigEncodedValue(
1403
- UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.discriminator,
1404
- newMarket.globalAllowedBorrowValue.toNumber()
1405
- ),
1406
- });
1407
- }
1408
- break;
1409
- case 'riskCouncil':
1410
- if (!market.riskCouncil.equals(newMarket.riskCouncil)) {
1411
- updateLendingMarketIxsArgs.push({
1412
- mode: UpdateLendingMarketMode.UpdateRiskCouncil.discriminator,
1413
- value: updateMarketConfigEncodedValue(
1414
- UpdateLendingMarketMode.UpdateRiskCouncil.discriminator,
1415
- newMarket.riskCouncil
1416
- ),
1417
- });
1418
- }
1419
- break;
1420
- case 'elevationGroups':
1421
- let elevationGroupsDiffs = 0;
1422
- for (let i = 0; i < market.elevationGroups.length; i++) {
1423
- if (
1424
- market.elevationGroups[i].id !== newMarket.elevationGroups[i].id ||
1425
- market.elevationGroups[i].maxLiquidationBonusBps !== newMarket.elevationGroups[i].maxLiquidationBonusBps ||
1426
- market.elevationGroups[i].ltvPct !== newMarket.elevationGroups[i].ltvPct ||
1427
- market.elevationGroups[i].liquidationThresholdPct !== newMarket.elevationGroups[i].liquidationThresholdPct ||
1428
- market.elevationGroups[i].allowNewLoans !== newMarket.elevationGroups[i].allowNewLoans ||
1429
- market.elevationGroups[i].maxReservesAsCollateral !== newMarket.elevationGroups[i].maxReservesAsCollateral ||
1430
- !market.elevationGroups[i].debtReserve.equals(newMarket.elevationGroups[i].debtReserve)
1431
- ) {
1432
- updateLendingMarketIxsArgs.push({
1433
- mode: UpdateLendingMarketMode.UpdateElevationGroup.discriminator,
1434
- value: updateMarketConfigEncodedValue(
1435
- UpdateLendingMarketMode.UpdateElevationGroup.discriminator,
1436
- newMarket.elevationGroups[i]
1437
- ),
1438
- });
1439
- elevationGroupsDiffs++;
1440
- }
1441
- }
1442
- if (elevationGroupsDiffs > 1) {
1443
- throw new Error('Can only update 1 elevation group at a time');
1444
- }
1445
- break;
1446
- case 'minNetValueInObligationSf':
1447
- if (!market.minNetValueInObligationSf.eq(newMarket.minNetValueInObligationSf)) {
1448
- updateLendingMarketIxsArgs.push({
1449
- mode: UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.discriminator,
1450
- value: updateMarketConfigEncodedValue(
1451
- UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.discriminator,
1452
- newMarket.minNetValueInObligationSf.toString()
1453
- ),
1454
- });
1455
- }
1456
- break;
1457
- case 'minValueSkipLiquidationBfChecks':
1458
- if (!market.minValueSkipLiquidationBfChecks.eq(newMarket.minValueSkipLiquidationBfChecks)) {
1459
- updateLendingMarketIxsArgs.push({
1460
- mode: UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.discriminator,
1461
- value: updateMarketConfigEncodedValue(
1462
- UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.discriminator,
1463
- newMarket.minValueSkipLiquidationBfChecks.toNumber()
1464
- ),
1465
- });
1466
- }
1467
- break;
1468
- case 'minValueSkipLiquidationLtvChecks':
1469
- if (!market.minValueSkipLiquidationLtvChecks.eq(newMarket.minValueSkipLiquidationLtvChecks)) {
1470
- updateLendingMarketIxsArgs.push({
1471
- mode: UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.discriminator,
1472
- value: updateMarketConfigEncodedValue(
1473
- UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.discriminator,
1474
- newMarket.minValueSkipLiquidationLtvChecks.toNumber()
1475
- ),
1476
- });
1477
- }
1478
- break;
1479
- case 'individualAutodeleverageMarginCallPeriodSecs':
1480
- if (
1481
- market.individualAutodeleverageMarginCallPeriodSecs !== newMarket.individualAutodeleverageMarginCallPeriodSecs
1482
- ) {
1483
- updateLendingMarketIxsArgs.push({
1484
- mode: UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.discriminator,
1485
- value: updateMarketConfigEncodedValue(
1486
- UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.discriminator,
1487
- newMarket.individualAutodeleverageMarginCallPeriodSecs.toNumber()
1488
- ),
1489
- });
1490
- }
1491
- break;
1492
- case 'name':
1493
- if (!sameLengthArrayEquals(market.name, newMarket.name)) {
1494
- updateLendingMarketIxsArgs.push({
1495
- mode: UpdateLendingMarketMode.UpdateName.discriminator,
1496
- value: updateMarketConfigEncodedValue(UpdateLendingMarketMode.UpdateName.discriminator, newMarket.name),
1497
- });
1498
- }
1499
- break;
1500
- case 'minInitialDepositAmount':
1501
- if (!market.minInitialDepositAmount.eq(newMarket.minInitialDepositAmount)) {
1502
- updateLendingMarketIxsArgs.push({
1503
- mode: UpdateLendingMarketMode.UpdateInitialDepositAmount.discriminator,
1504
- value: updateMarketConfigEncodedValue(
1505
- UpdateLendingMarketMode.UpdateInitialDepositAmount.discriminator,
1506
- newMarket.minInitialDepositAmount.toNumber()
1507
- ),
1508
- });
1509
- }
1510
- break;
1511
- case 'obligationOrdersEnabled':
1512
- if (market.obligationOrdersEnabled !== newMarket.obligationOrdersEnabled) {
1513
- updateLendingMarketIxsArgs.push({
1514
- mode: UpdateLendingMarketMode.UpdateObligationOrdersEnabled.discriminator,
1515
- value: updateMarketConfigEncodedValue(
1516
- UpdateLendingMarketMode.UpdateObligationOrdersEnabled.discriminator,
1517
- newMarket.obligationOrdersEnabled
1518
- ),
1519
- });
1520
- }
1521
- break;
1522
- default:
1523
- assertNever(key);
1524
- }
1525
- return updateLendingMarketIxsArgs;
1526
- };
1252
+ export const MARKET_UPDATER = new ConfigUpdater(UpdateLendingMarketMode.fromDecoded, LendingMarket, (config) => ({
1253
+ [UpdateLendingMarketMode.UpdateOwner.kind]: config.lendingMarketOwnerCached,
1254
+ [UpdateLendingMarketMode.UpdateImmutableFlag.kind]: config.immutable,
1255
+ [UpdateLendingMarketMode.UpdateEmergencyMode.kind]: config.emergencyMode,
1256
+ [UpdateLendingMarketMode.UpdateLiquidationCloseFactor.kind]: config.liquidationMaxDebtCloseFactorPct,
1257
+ [UpdateLendingMarketMode.UpdateLiquidationMaxValue.kind]: config.maxLiquidatableDebtMarketValueAtOnce,
1258
+ [UpdateLendingMarketMode.DeprecatedUpdateGlobalUnhealthyBorrow.kind]: [], // deprecated
1259
+ [UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.kind]: config.globalAllowedBorrowValue,
1260
+ [UpdateLendingMarketMode.UpdateRiskCouncil.kind]: config.riskCouncil,
1261
+ [UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.kind]: config.minFullLiquidationValueThreshold,
1262
+ [UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.kind]: config.insolvencyRiskUnhealthyLtvPct,
1263
+ [UpdateLendingMarketMode.UpdateElevationGroup.kind]: arrayElementConfigItems(config.elevationGroups),
1264
+ [UpdateLendingMarketMode.UpdateReferralFeeBps.kind]: config.referralFeeBps,
1265
+ [UpdateLendingMarketMode.DeprecatedUpdateMultiplierPoints.kind]: [], // deprecated
1266
+ [UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.kind]: config.priceRefreshTriggerToMaxAgePct,
1267
+ [UpdateLendingMarketMode.UpdateAutodeleverageEnabled.kind]: config.autodeleverageEnabled,
1268
+ [UpdateLendingMarketMode.UpdateBorrowingDisabled.kind]: config.borrowDisabled,
1269
+ [UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.kind]: config.minNetValueInObligationSf,
1270
+ [UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.kind]: config.minValueSkipLiquidationLtvChecks,
1271
+ [UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.kind]: config.minValueSkipLiquidationBfChecks,
1272
+ [UpdateLendingMarketMode.UpdatePaddingFields.kind]: [], // we do not update padding this way
1273
+ [UpdateLendingMarketMode.UpdateName.kind]: config.name,
1274
+ [UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.kind]:
1275
+ config.individualAutodeleverageMarginCallPeriodSecs,
1276
+ [UpdateLendingMarketMode.UpdateInitialDepositAmount.kind]: config.minInitialDepositAmount,
1277
+ [UpdateLendingMarketMode.UpdateObligationOrderCreationEnabled.kind]: config.obligationOrderCreationEnabled,
1278
+ [UpdateLendingMarketMode.UpdateObligationOrderExecutionEnabled.kind]: config.obligationOrderExecutionEnabled,
1279
+ }));
1527
1280
 
1528
1281
  function parseForChangesMarketConfigAndGetIxs(
1529
1282
  marketWithAddress: MarketWithAddress,
1530
1283
  newMarket: LendingMarket,
1531
1284
  programId: PublicKey
1532
1285
  ): TransactionInstruction[] {
1533
- const market = marketWithAddress.state;
1534
- const updateLendingMarketIxsArgs: { mode: number; value: Buffer }[] = [];
1535
-
1536
- for (const key in market.toJSON()) {
1537
- if (isExcludedLendingMarketKey(key)) {
1538
- continue;
1539
- }
1540
- updateLendingMarketIxsArgs.push(...updateLendingMarketConfig(key as LendingMarketKey, market, newMarket));
1541
- }
1542
-
1543
- const ixs: TransactionInstruction[] = [];
1544
-
1545
- updateLendingMarketIxsArgs.forEach((updateLendingMarketConfigArgs) => {
1546
- ixs.push(
1547
- updateMarketConfigIx(
1548
- marketWithAddress,
1549
- updateLendingMarketConfigArgs.mode,
1550
- updateLendingMarketConfigArgs.value,
1551
- programId
1552
- )
1553
- );
1554
- });
1555
-
1556
- return ixs;
1557
- }
1558
-
1559
- function updateMarketConfigEncodedValue(
1560
- discriminator: UpdateLendingMarketModeKind['discriminator'],
1561
- value: number | number[] | PublicKey | ElevationGroup | string
1562
- ): Buffer {
1563
- let buffer: Buffer = Buffer.alloc(72);
1564
- let pkBuffer: Buffer;
1565
- let valueBigInt: bigint;
1566
- let valueArray: number[];
1567
-
1568
- switch (discriminator) {
1569
- case UpdateLendingMarketMode.UpdateEmergencyMode.discriminator:
1570
- case UpdateLendingMarketMode.UpdateLiquidationCloseFactor.discriminator:
1571
- case UpdateLendingMarketMode.UpdateInsolvencyRiskLtv.discriminator:
1572
- case UpdateLendingMarketMode.UpdatePriceRefreshTriggerToMaxAgePct.discriminator:
1573
- case UpdateLendingMarketMode.UpdateAutodeleverageEnabled.discriminator:
1574
- case UpdateLendingMarketMode.UpdateObligationOrdersEnabled.discriminator:
1575
- case UpdateLendingMarketMode.UpdateBorrowingDisabled.discriminator:
1576
- buffer.writeUIntLE(value as number, 0, 1);
1577
- break;
1578
- case UpdateLendingMarketMode.UpdateReferralFeeBps.discriminator:
1579
- buffer.writeUInt16LE(value as number, 0);
1580
- break;
1581
- case UpdateLendingMarketMode.UpdateLiquidationMaxValue.discriminator:
1582
- case UpdateLendingMarketMode.UpdateGlobalAllowedBorrow.discriminator:
1583
- case UpdateLendingMarketMode.UpdateMinFullLiquidationThreshold.discriminator:
1584
- case UpdateLendingMarketMode.UpdateMinValueBfSkipPriorityLiqCheck.discriminator:
1585
- case UpdateLendingMarketMode.UpdateMinValueLtvSkipPriorityLiqCheck.discriminator:
1586
- case UpdateLendingMarketMode.UpdateIndividualAutodeleverageMarginCallPeriodSecs.discriminator:
1587
- case UpdateLendingMarketMode.UpdateInitialDepositAmount.discriminator:
1588
- value = value as number;
1589
- buffer.writeBigUint64LE(BigInt(value), 0);
1590
- break;
1591
- case UpdateLendingMarketMode.UpdateOwner.discriminator:
1592
- case UpdateLendingMarketMode.UpdateRiskCouncil.discriminator:
1593
- pkBuffer = (value as PublicKey).toBuffer();
1594
- pkBuffer.copy(buffer, 0);
1595
- break;
1596
- case UpdateLendingMarketMode.UpdateElevationGroup.discriminator:
1597
- buffer = serializeElevationGroup(value as ElevationGroup);
1598
- break;
1599
- case UpdateLendingMarketMode.UpdateMinNetValueObligationPostAction.discriminator:
1600
- valueBigInt = BigInt(value as string);
1601
- for (let i = 0; i < 16; i++) {
1602
- buffer[15 - i] = Number((valueBigInt >> BigInt(i * 8)) & BigInt(0xff));
1603
- }
1604
- break;
1605
- case UpdateLendingMarketMode.UpdateName.discriminator:
1606
- valueArray = value as number[];
1607
- for (let i = 0; i < valueArray.length; i++) {
1608
- buffer.writeUIntLE(valueArray[i], i, 1);
1609
- }
1610
- break;
1611
- case UpdateLendingMarketMode.UpdatePaddingFields.discriminator:
1612
- case UpdateLendingMarketMode.DeprecatedUpdateGlobalUnhealthyBorrow.discriminator:
1613
- case UpdateLendingMarketMode.DeprecatedUpdateMultiplierPoints.discriminator:
1614
- // Deliberately skipped - we are not updating padding or deprecated fields using this method
1615
- break;
1616
- default:
1617
- assertNever(discriminator);
1618
- }
1619
-
1620
- return buffer;
1286
+ const encodedMarketUpdates = MARKET_UPDATER.encodeAllUpdates(marketWithAddress.state, newMarket);
1287
+ return encodedMarketUpdates.map((encodedMarketUpdate) =>
1288
+ updateMarketConfigIx(marketWithAddress, encodedMarketUpdate.mode, encodedMarketUpdate.value, programId)
1289
+ );
1621
1290
  }
1622
1291
 
1623
1292
  function updateMarketConfigIx(
1624
1293
  marketWithAddress: MarketWithAddress,
1625
- modeDiscriminator: number,
1626
- value: Buffer,
1294
+ mode: UpdateLendingMarketModeKind,
1295
+ value: Uint8Array,
1627
1296
  programId: PublicKey
1628
1297
  ): TransactionInstruction {
1629
1298
  const accounts: UpdateLendingMarketAccounts = {
@@ -1632,25 +1301,13 @@ function updateMarketConfigIx(
1632
1301
  };
1633
1302
 
1634
1303
  const args: UpdateLendingMarketArgs = {
1635
- mode: new anchor.BN(modeDiscriminator),
1636
- value: [...value],
1304
+ mode: new anchor.BN(mode.discriminator),
1305
+ // NOTE: the Market's update handler expects a `[u8; 72]` (contrary to e.g. the Reserve's update handler accepting
1306
+ // `Vec<u8>`). Hence, we need to add explicit padding here:
1307
+ value: [...value, ...Array(72 - value.length).fill(0)],
1637
1308
  };
1638
1309
 
1639
1310
  const ix = updateLendingMarket(args, accounts, programId);
1640
1311
 
1641
1312
  return ix;
1642
1313
  }
1643
-
1644
- function serializeElevationGroup(elevationGroup: ElevationGroup): Buffer {
1645
- const buffer = Buffer.alloc(72);
1646
- buffer.writeUInt16LE(elevationGroup.maxLiquidationBonusBps, 0);
1647
- buffer.writeUIntLE(elevationGroup.id, 2, 1);
1648
- buffer.writeUIntLE(elevationGroup.ltvPct, 3, 1);
1649
- buffer.writeUIntLE(elevationGroup.liquidationThresholdPct, 4, 1);
1650
- buffer.writeUIntLE(elevationGroup.allowNewLoans, 5, 1);
1651
- buffer.writeUIntLE(elevationGroup.maxReservesAsCollateral, 6, 1);
1652
- buffer.writeUIntLE(elevationGroup.padding0, 7, 1);
1653
- const debtReserveBuffer = elevationGroup.debtReserve.toBuffer();
1654
- debtReserveBuffer.copy(buffer, 8);
1655
- return buffer;
1656
- }