@jup-ag/lend 0.0.76 → 0.0.78

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.
@@ -3909,10 +3909,9 @@ type LiquidateParams = {
3909
3909
  type LiquidateContextParams = {
3910
3910
  vaultId: number;
3911
3911
  to: PublicKey;
3912
- sources: PublicKey[];
3913
3912
  program: Program<Vaults>;
3914
3913
  } & ConnectionParams;
3915
- declare function getLiquidateContext({ vaultId, to, sources, program, connection, signer, }: LiquidateContextParams): Promise<{
3914
+ declare function getLiquidateContext({ vaultId, to, program, connection, signer, }: LiquidateContextParams): Promise<{
3916
3915
  accounts: {
3917
3916
  signer: PublicKey;
3918
3917
  signerTokenAccount: PublicKey;
@@ -4075,7 +4074,31 @@ declare function loadRelevantTicksHasDebtArrays(vaultId: number, program: Progra
4075
4074
  childrenBits: number[];
4076
4075
  }[];
4077
4076
  }[]>;
4077
+ declare function loadRelevantTicksHasDebtArraysLiquidate(vaultId: number, topTick: number, nextTick: number, program: Program<Vaults>): Promise<{
4078
+ vaultId: number;
4079
+ index: number;
4080
+ tickHasDebt: {
4081
+ childrenBits: number[];
4082
+ }[];
4083
+ }[]>;
4078
4084
  declare function loadRelevantBranchesForLiquidate(vaultId: number, vaultState: any, program: Program<Vaults>): Promise<any[]>;
4085
+ declare function readOraclePrice({ connection, signer, oracle, }: {
4086
+ connection: Connection;
4087
+ signer?: PublicKey;
4088
+ oracle: PublicKey;
4089
+ }): Promise<{
4090
+ oraclePriceOperate: any;
4091
+ oraclePriceLiquidate: any;
4092
+ oracleSources: {
4093
+ source: PublicKey;
4094
+ invert: boolean;
4095
+ multiplier: BN;
4096
+ divisor: BN;
4097
+ sourceType: {} & {
4098
+ pyth: Record<string, never>;
4099
+ };
4100
+ }[];
4101
+ }>;
4079
4102
 
4080
4103
  declare const MIN_TICK = -16383;
4081
4104
  declare const MAX_TICK = 16383;
@@ -4122,5 +4145,5 @@ declare const getAllLiquidations: (params: Omit<LiquidateParams, "debtAmount" |
4122
4145
  }[];
4123
4146
  }[]>;
4124
4147
 
4125
- export { MAX_TICK, MIN_TICK, TICKS_PER_TICK_HAS_DEBT, TICK_HAS_DEBT_ARRAY_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE_IN_BITS, TOTAL_INDICES_NEEDED, ZERO_TICK_SCALED_RATIO, findNextTickWithDebt, getAccountOwner, getAllLiquidations, getCurrentPosition, getCurrentPositionState, getFinalPosition, getFirstTickForIndex, getInitPositionContext, getLiquidateContext, getLiquidateIx, getLiquidations, getOperateContext, getOperateIx, getRatioAtTick, getTickAtRatio, getTickIndices, getVaultsProgram, loadRelevantBranches, loadRelevantBranchesForLiquidate, loadRelevantTicksHasDebtArrays, readTickHasDebtArray, simulateLiquidate };
4148
+ export { MAX_TICK, MIN_TICK, TICKS_PER_TICK_HAS_DEBT, TICK_HAS_DEBT_ARRAY_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE_IN_BITS, TOTAL_INDICES_NEEDED, ZERO_TICK_SCALED_RATIO, findNextTickWithDebt, getAccountOwner, getAllLiquidations, getCurrentPosition, getCurrentPositionState, getFinalPosition, getFirstTickForIndex, getInitPositionContext, getLiquidateContext, getLiquidateIx, getLiquidations, getOperateContext, getOperateIx, getRatioAtTick, getTickAtRatio, getTickIndices, getVaultsProgram, loadRelevantBranches, loadRelevantBranchesForLiquidate, loadRelevantTicksHasDebtArrays, loadRelevantTicksHasDebtArraysLiquidate, readOraclePrice, readTickHasDebtArray, simulateLiquidate };
4126
4149
  export type { ConnectionParams, LiquidateContextParams, LiquidateParams, OperateContextParams, OperateParams };
@@ -3909,10 +3909,9 @@ type LiquidateParams = {
3909
3909
  type LiquidateContextParams = {
3910
3910
  vaultId: number;
3911
3911
  to: PublicKey;
3912
- sources: PublicKey[];
3913
3912
  program: Program<Vaults>;
3914
3913
  } & ConnectionParams;
3915
- declare function getLiquidateContext({ vaultId, to, sources, program, connection, signer, }: LiquidateContextParams): Promise<{
3914
+ declare function getLiquidateContext({ vaultId, to, program, connection, signer, }: LiquidateContextParams): Promise<{
3916
3915
  accounts: {
3917
3916
  signer: PublicKey;
3918
3917
  signerTokenAccount: PublicKey;
@@ -4075,7 +4074,31 @@ declare function loadRelevantTicksHasDebtArrays(vaultId: number, program: Progra
4075
4074
  childrenBits: number[];
4076
4075
  }[];
4077
4076
  }[]>;
4077
+ declare function loadRelevantTicksHasDebtArraysLiquidate(vaultId: number, topTick: number, nextTick: number, program: Program<Vaults>): Promise<{
4078
+ vaultId: number;
4079
+ index: number;
4080
+ tickHasDebt: {
4081
+ childrenBits: number[];
4082
+ }[];
4083
+ }[]>;
4078
4084
  declare function loadRelevantBranchesForLiquidate(vaultId: number, vaultState: any, program: Program<Vaults>): Promise<any[]>;
4085
+ declare function readOraclePrice({ connection, signer, oracle, }: {
4086
+ connection: Connection;
4087
+ signer?: PublicKey;
4088
+ oracle: PublicKey;
4089
+ }): Promise<{
4090
+ oraclePriceOperate: any;
4091
+ oraclePriceLiquidate: any;
4092
+ oracleSources: {
4093
+ source: PublicKey;
4094
+ invert: boolean;
4095
+ multiplier: BN;
4096
+ divisor: BN;
4097
+ sourceType: {} & {
4098
+ pyth: Record<string, never>;
4099
+ };
4100
+ }[];
4101
+ }>;
4079
4102
 
4080
4103
  declare const MIN_TICK = -16383;
4081
4104
  declare const MAX_TICK = 16383;
@@ -4122,5 +4145,5 @@ declare const getAllLiquidations: (params: Omit<LiquidateParams, "debtAmount" |
4122
4145
  }[];
4123
4146
  }[]>;
4124
4147
 
4125
- export { MAX_TICK, MIN_TICK, TICKS_PER_TICK_HAS_DEBT, TICK_HAS_DEBT_ARRAY_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE_IN_BITS, TOTAL_INDICES_NEEDED, ZERO_TICK_SCALED_RATIO, findNextTickWithDebt, getAccountOwner, getAllLiquidations, getCurrentPosition, getCurrentPositionState, getFinalPosition, getFirstTickForIndex, getInitPositionContext, getLiquidateContext, getLiquidateIx, getLiquidations, getOperateContext, getOperateIx, getRatioAtTick, getTickAtRatio, getTickIndices, getVaultsProgram, loadRelevantBranches, loadRelevantBranchesForLiquidate, loadRelevantTicksHasDebtArrays, readTickHasDebtArray, simulateLiquidate };
4148
+ export { MAX_TICK, MIN_TICK, TICKS_PER_TICK_HAS_DEBT, TICK_HAS_DEBT_ARRAY_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE_IN_BITS, TOTAL_INDICES_NEEDED, ZERO_TICK_SCALED_RATIO, findNextTickWithDebt, getAccountOwner, getAllLiquidations, getCurrentPosition, getCurrentPositionState, getFinalPosition, getFirstTickForIndex, getInitPositionContext, getLiquidateContext, getLiquidateIx, getLiquidations, getOperateContext, getOperateIx, getRatioAtTick, getTickAtRatio, getTickIndices, getVaultsProgram, loadRelevantBranches, loadRelevantBranchesForLiquidate, loadRelevantTicksHasDebtArrays, loadRelevantTicksHasDebtArraysLiquidate, readOraclePrice, readTickHasDebtArray, simulateLiquidate };
4126
4149
  export type { ConnectionParams, LiquidateContextParams, LiquidateParams, OperateContextParams, OperateParams };
@@ -1,4 +1,4 @@
1
- import { Transaction, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, TransactionMessage, ComputeBudgetProgram, VersionedTransaction } from '@solana/web3.js';
1
+ import { PublicKey, VersionedTransaction, Transaction, SystemProgram, SYSVAR_RENT_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, TransactionMessage, ComputeBudgetProgram } from '@solana/web3.js';
2
2
  import BN from 'bn.js';
3
3
  import { Program } from '@coral-xyz/anchor';
4
4
  import { l as liquidity } from '../shared/lend.CioR9-te.mjs';
@@ -889,9 +889,29 @@ const getCurrentPositionState = async ({
889
889
  getTick(vaultId, positionTick)
890
890
  );
891
891
  if (tickData.isLiquidated || tickData.totalIds > position.tickId) {
892
- const tickIdData = await program.account.tickIdLiquidation.fetch(
893
- getTickIdLiquidation(vaultId, positionTick, position.tickId)
894
- );
892
+ let tickIdData = await program.account.tickIdLiquidation.fetch(
893
+ getTickIdLiquidation(
894
+ vaultId,
895
+ positionTick,
896
+ position.tickId
897
+ )
898
+ ).catch(() => null);
899
+ if (!tickIdData) {
900
+ tickIdData = {
901
+ vaultId,
902
+ tick: positionTick,
903
+ tickMap: position.tickId,
904
+ isFullyLiquidated1: 0,
905
+ liquidationBranchId1: 0,
906
+ debtFactor1: new BN(0),
907
+ isFullyLiquidated2: 0,
908
+ liquidationBranchId2: 0,
909
+ debtFactor2: new BN(0),
910
+ isFullyLiquidated3: 0,
911
+ liquidationBranchId3: 0,
912
+ debtFactor3: new BN(0)
913
+ };
914
+ }
895
915
  const branches = await getAllBranches({ vaultId, program });
896
916
  const { isFullyLiquidated, branchId, connectionFactor } = getLiquidationStatus(position.tickId, tickData, tickIdData);
897
917
  if (isFullyLiquidated) {
@@ -1246,6 +1266,23 @@ async function getExchangePrices({
1246
1266
  vaultBorrowExchangePrice
1247
1267
  };
1248
1268
  }
1269
+ async function loadRelevantTicksHasDebtArraysLiquidate(vaultId, topTick, nextTick, program) {
1270
+ const { arrayIndex: topTickIndex } = getTickIndices(topTick);
1271
+ const { arrayIndex: nextTickIndex } = getTickIndices(nextTick);
1272
+ const promises = [];
1273
+ for (let arrIdx = topTickIndex; arrIdx >= nextTickIndex; arrIdx--) {
1274
+ promises.push(
1275
+ program.account.tickHasDebtArray.fetch(
1276
+ getTickHasDebt(vaultId, arrIdx)
1277
+ )
1278
+ );
1279
+ }
1280
+ const results = await Promise.allSettled(promises);
1281
+ const tickHasDebtArrays = results.filter((result) => result.status === "fulfilled").map(
1282
+ (result) => result.value
1283
+ );
1284
+ return tickHasDebtArrays;
1285
+ }
1249
1286
  async function loadRelevantBranchesForLiquidate(vaultId, vaultState, program) {
1250
1287
  const branches = [];
1251
1288
  const currentBranchId = vaultState.currentBranchId;
@@ -1275,6 +1312,51 @@ async function loadRelevantBranchesForLiquidate(vaultId, vaultState, program) {
1275
1312
  if (!doesBranchExist(0)) branches.push({ branchId: 0 });
1276
1313
  return branches;
1277
1314
  }
1315
+ async function readOraclePrice({
1316
+ connection,
1317
+ signer,
1318
+ oracle: oracle$1
1319
+ }) {
1320
+ signer ??= new PublicKey("JupRhwjrF5fAcs6dFhLH59r3TJFvbcyLP2NRM8UGH9H");
1321
+ const program = new Program(oracle, {
1322
+ connection,
1323
+ publicKey: signer,
1324
+ async simulate(tx) {
1325
+ if (tx instanceof VersionedTransaction) {
1326
+ const res = await connection.simulateTransaction(tx);
1327
+ if (res.value.err) {
1328
+ throw new Error(`Simulation failed: ${res.value.err}`);
1329
+ }
1330
+ return res.value;
1331
+ } else {
1332
+ tx.feePayer = signer;
1333
+ const res = await connection.simulateTransaction(tx);
1334
+ if (res.value.err) {
1335
+ throw new Error(`Simulation failed: ${res.value.err}`);
1336
+ }
1337
+ return res.value;
1338
+ }
1339
+ }
1340
+ });
1341
+ const oracleData = await program.account.oracle.fetch(oracle$1);
1342
+ let remainingAccounts = [];
1343
+ for (const source of oracleData.sources) {
1344
+ remainingAccounts.push({
1345
+ pubkey: source.source,
1346
+ isWritable: false,
1347
+ isSigner: false
1348
+ });
1349
+ }
1350
+ const [oraclePriceOperate, oraclePriceLiquidate] = await Promise.all([
1351
+ program.methods.getExchangeRateOperate(oracleData.nonce).accounts({ oracle: oracle$1 }).remainingAccounts(remainingAccounts).view(),
1352
+ program.methods.getExchangeRateLiquidate(oracleData.nonce).accounts({ oracle: oracle$1 }).remainingAccounts(remainingAccounts).view()
1353
+ ]);
1354
+ return {
1355
+ oraclePriceOperate,
1356
+ oraclePriceLiquidate,
1357
+ oracleSources: oracleData.sources
1358
+ };
1359
+ }
1278
1360
 
1279
1361
  const MPL_TOKEN_METADATA_PROGRAM_ID = new PublicKey(
1280
1362
  "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
@@ -1704,7 +1786,6 @@ async function getInitTickIdLiquidationContext(vaultId, tick, signer, program) {
1704
1786
  async function getLiquidateContext({
1705
1787
  vaultId,
1706
1788
  to,
1707
- sources,
1708
1789
  program,
1709
1790
  connection,
1710
1791
  signer
@@ -1733,10 +1814,10 @@ async function getLiquidateContext({
1733
1814
  } = await getRemainingAccountsLiquidate(
1734
1815
  vaultId,
1735
1816
  vaultState,
1817
+ vaultConfig,
1736
1818
  otherIxs,
1737
- vaultConfig.oracle,
1738
- sources,
1739
1819
  program,
1820
+ connection,
1740
1821
  signer
1741
1822
  );
1742
1823
  return {
@@ -1832,7 +1913,6 @@ const getLiquidateIx = async ({
1832
1913
  } = await getLiquidateContext({
1833
1914
  vaultId,
1834
1915
  to,
1835
- sources: [],
1836
1916
  program,
1837
1917
  signer,
1838
1918
  connection
@@ -1883,11 +1963,10 @@ async function getOtherInstructionsLiquidate(vaultId, vaultState, program, signe
1883
1963
  newBranchPda
1884
1964
  };
1885
1965
  }
1886
- async function loadRelevantTicksForLiquidate(vaultId, vaultState, program) {
1966
+ async function loadRelevantTicksForLiquidate(vaultId, vaultState, liquidationTick, program) {
1887
1967
  const ticks = [];
1888
- const MAX_TICKS = 15;
1889
1968
  let topTick = vaultState.topmostTick;
1890
- if (topTick > MIN_TICK$1)
1969
+ if (topTick > liquidationTick)
1891
1970
  try {
1892
1971
  const topTickData = await program.account.tick.fetch(
1893
1972
  getTick(vaultId, topTick)
@@ -1903,7 +1982,7 @@ async function loadRelevantTicksForLiquidate(vaultId, vaultState, program) {
1903
1982
  console.warn(`Failed to find next tick with debt:`, error);
1904
1983
  }
1905
1984
  const doesTickExist = (tick) => ticks.some((t) => t.tick === tick);
1906
- while (nextTick > MIN_TICK$1 && !doesTickExist(nextTick) && ticks.length < MAX_TICKS) {
1985
+ while (nextTick > liquidationTick && !doesTickExist(nextTick)) {
1907
1986
  try {
1908
1987
  const nextTickData = await program.account.tick.fetch(
1909
1988
  getTick(vaultId, nextTick)
@@ -1915,27 +1994,40 @@ async function loadRelevantTicksForLiquidate(vaultId, vaultState, program) {
1915
1994
  console.warn(`Failed to fetch next tick ${nextTick}:`, error);
1916
1995
  }
1917
1996
  }
1918
- return ticks;
1997
+ return { ticks, nextTick };
1919
1998
  }
1920
- async function getRemainingAccountsLiquidate(vaultId, vaultState, otherIxs, oracleAddress, sources, program, signer) {
1999
+ async function getRemainingAccountsLiquidate(vaultId, vaultState, vaultConfig, otherIxs, program, connection, signer) {
1921
2000
  const remainingAccounts = [];
1922
- if (sources.length === 0) {
1923
- const oracleProgram = new Program(oracle, program.provider);
1924
- const oracleData = await oracleProgram.account.oracle.fetch(oracleAddress);
1925
- sources = oracleData.sources.map((source) => new PublicKey(source.source));
1926
- }
1927
- for (const source of sources) {
2001
+ const { oraclePriceLiquidate, oracleSources } = await readOraclePrice({
2002
+ oracle: vaultConfig.oracle,
2003
+ connection,
2004
+ signer
2005
+ });
2006
+ const liquidationRatio = new BN(oraclePriceLiquidate).div(new BN(10).pow(new BN(15))).mul(new BN(281474976710656));
2007
+ const liquidationThresholdRatio = liquidationRatio.mul(new BN(vaultConfig.liquidationThreshold)).div(new BN(10).pow(new BN(3)));
2008
+ const liquidationTick = getTickAtRatio(liquidationThresholdRatio);
2009
+ for (const source of oracleSources) {
1928
2010
  remainingAccounts.push({
1929
- pubkey: source,
2011
+ pubkey: source.source,
1930
2012
  isWritable: false,
1931
2013
  isSigner: false
1932
2014
  });
1933
2015
  }
1934
- const [branches, tickAccounts, tickHasDebt] = await Promise.all([
2016
+ const [branches, { ticks: tickAccounts, nextTick }] = await Promise.all([
1935
2017
  loadRelevantBranchesForLiquidate(vaultId, vaultState, program),
1936
- loadRelevantTicksForLiquidate(vaultId, vaultState, program),
1937
- loadRelevantTicksHasDebtArrays(vaultId, program)
2018
+ loadRelevantTicksForLiquidate(
2019
+ vaultId,
2020
+ vaultState,
2021
+ liquidationTick,
2022
+ program
2023
+ )
1938
2024
  ]);
2025
+ const tickHasDebt = await loadRelevantTicksHasDebtArraysLiquidate(
2026
+ vaultId,
2027
+ vaultState.topmostTick,
2028
+ nextTick,
2029
+ program
2030
+ );
1939
2031
  for (const branch of branches) {
1940
2032
  remainingAccounts.push({
1941
2033
  pubkey: getBranch(vaultId, branch.branchId),
@@ -1979,7 +2071,7 @@ async function getRemainingAccountsLiquidate(vaultId, vaultState, otherIxs, orac
1979
2071
  });
1980
2072
  }
1981
2073
  const remainingAccountsIndices = [
1982
- sources.length,
2074
+ oracleSources.length,
1983
2075
  branches.length,
1984
2076
  tickAccounts.length,
1985
2077
  tickHasDebt.length
@@ -2054,4 +2146,4 @@ function parseVaultLiquidations(logs) {
2054
2146
  return results;
2055
2147
  }
2056
2148
 
2057
- export { MAX_TICK$1 as MAX_TICK, MIN_TICK$1 as MIN_TICK, TICKS_PER_TICK_HAS_DEBT, TICK_HAS_DEBT_ARRAY_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE_IN_BITS, TOTAL_INDICES_NEEDED, ZERO_TICK_SCALED_RATIO$1 as ZERO_TICK_SCALED_RATIO, findNextTickWithDebt, getAccountOwner, getAllLiquidations, getCurrentPosition, getCurrentPositionState, getFinalPosition, getFirstTickForIndex, getInitPositionContext, getLiquidateContext, getLiquidateIx, getLiquidations, getOperateContext, getOperateIx, getRatioAtTick, getTickAtRatio, getTickIndices, getVaultsProgram, loadRelevantBranches, loadRelevantBranchesForLiquidate, loadRelevantTicksHasDebtArrays, readTickHasDebtArray, simulateLiquidate };
2149
+ export { MAX_TICK$1 as MAX_TICK, MIN_TICK$1 as MIN_TICK, TICKS_PER_TICK_HAS_DEBT, TICK_HAS_DEBT_ARRAY_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE, TICK_HAS_DEBT_CHILDREN_SIZE_IN_BITS, TOTAL_INDICES_NEEDED, ZERO_TICK_SCALED_RATIO$1 as ZERO_TICK_SCALED_RATIO, findNextTickWithDebt, getAccountOwner, getAllLiquidations, getCurrentPosition, getCurrentPositionState, getFinalPosition, getFirstTickForIndex, getInitPositionContext, getLiquidateContext, getLiquidateIx, getLiquidations, getOperateContext, getOperateIx, getRatioAtTick, getTickAtRatio, getTickIndices, getVaultsProgram, loadRelevantBranches, loadRelevantBranchesForLiquidate, loadRelevantTicksHasDebtArrays, loadRelevantTicksHasDebtArraysLiquidate, readOraclePrice, readTickHasDebtArray, simulateLiquidate };
package/dist/index.mjs CHANGED
@@ -8,6 +8,6 @@ import './shared/lend.CioR9-te.mjs';
8
8
  import '@solana/spl-token';
9
9
  import 'bn.js';
10
10
 
11
- const version = "0.0.76";
11
+ const version = "0.0.78";
12
12
 
13
13
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jup-ag/lend",
3
- "version": "0.0.76",
3
+ "version": "0.0.78",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "type": "module",