@jup-ag/lend 0.0.77 → 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';
@@ -1266,6 +1266,23 @@ async function getExchangePrices({
1266
1266
  vaultBorrowExchangePrice
1267
1267
  };
1268
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
+ }
1269
1286
  async function loadRelevantBranchesForLiquidate(vaultId, vaultState, program) {
1270
1287
  const branches = [];
1271
1288
  const currentBranchId = vaultState.currentBranchId;
@@ -1295,6 +1312,51 @@ async function loadRelevantBranchesForLiquidate(vaultId, vaultState, program) {
1295
1312
  if (!doesBranchExist(0)) branches.push({ branchId: 0 });
1296
1313
  return branches;
1297
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
+ }
1298
1360
 
1299
1361
  const MPL_TOKEN_METADATA_PROGRAM_ID = new PublicKey(
1300
1362
  "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
@@ -1724,7 +1786,6 @@ async function getInitTickIdLiquidationContext(vaultId, tick, signer, program) {
1724
1786
  async function getLiquidateContext({
1725
1787
  vaultId,
1726
1788
  to,
1727
- sources,
1728
1789
  program,
1729
1790
  connection,
1730
1791
  signer
@@ -1753,10 +1814,10 @@ async function getLiquidateContext({
1753
1814
  } = await getRemainingAccountsLiquidate(
1754
1815
  vaultId,
1755
1816
  vaultState,
1817
+ vaultConfig,
1756
1818
  otherIxs,
1757
- vaultConfig.oracle,
1758
- sources,
1759
1819
  program,
1820
+ connection,
1760
1821
  signer
1761
1822
  );
1762
1823
  return {
@@ -1852,7 +1913,6 @@ const getLiquidateIx = async ({
1852
1913
  } = await getLiquidateContext({
1853
1914
  vaultId,
1854
1915
  to,
1855
- sources: [],
1856
1916
  program,
1857
1917
  signer,
1858
1918
  connection
@@ -1903,11 +1963,10 @@ async function getOtherInstructionsLiquidate(vaultId, vaultState, program, signe
1903
1963
  newBranchPda
1904
1964
  };
1905
1965
  }
1906
- async function loadRelevantTicksForLiquidate(vaultId, vaultState, program) {
1966
+ async function loadRelevantTicksForLiquidate(vaultId, vaultState, liquidationTick, program) {
1907
1967
  const ticks = [];
1908
- const MAX_TICKS = 15;
1909
1968
  let topTick = vaultState.topmostTick;
1910
- if (topTick > MIN_TICK$1)
1969
+ if (topTick > liquidationTick)
1911
1970
  try {
1912
1971
  const topTickData = await program.account.tick.fetch(
1913
1972
  getTick(vaultId, topTick)
@@ -1923,7 +1982,7 @@ async function loadRelevantTicksForLiquidate(vaultId, vaultState, program) {
1923
1982
  console.warn(`Failed to find next tick with debt:`, error);
1924
1983
  }
1925
1984
  const doesTickExist = (tick) => ticks.some((t) => t.tick === tick);
1926
- while (nextTick > MIN_TICK$1 && !doesTickExist(nextTick) && ticks.length < MAX_TICKS) {
1985
+ while (nextTick > liquidationTick && !doesTickExist(nextTick)) {
1927
1986
  try {
1928
1987
  const nextTickData = await program.account.tick.fetch(
1929
1988
  getTick(vaultId, nextTick)
@@ -1935,27 +1994,40 @@ async function loadRelevantTicksForLiquidate(vaultId, vaultState, program) {
1935
1994
  console.warn(`Failed to fetch next tick ${nextTick}:`, error);
1936
1995
  }
1937
1996
  }
1938
- return ticks;
1997
+ return { ticks, nextTick };
1939
1998
  }
1940
- async function getRemainingAccountsLiquidate(vaultId, vaultState, otherIxs, oracleAddress, sources, program, signer) {
1999
+ async function getRemainingAccountsLiquidate(vaultId, vaultState, vaultConfig, otherIxs, program, connection, signer) {
1941
2000
  const remainingAccounts = [];
1942
- if (sources.length === 0) {
1943
- const oracleProgram = new Program(oracle, program.provider);
1944
- const oracleData = await oracleProgram.account.oracle.fetch(oracleAddress);
1945
- sources = oracleData.sources.map((source) => new PublicKey(source.source));
1946
- }
1947
- 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) {
1948
2010
  remainingAccounts.push({
1949
- pubkey: source,
2011
+ pubkey: source.source,
1950
2012
  isWritable: false,
1951
2013
  isSigner: false
1952
2014
  });
1953
2015
  }
1954
- const [branches, tickAccounts, tickHasDebt] = await Promise.all([
2016
+ const [branches, { ticks: tickAccounts, nextTick }] = await Promise.all([
1955
2017
  loadRelevantBranchesForLiquidate(vaultId, vaultState, program),
1956
- loadRelevantTicksForLiquidate(vaultId, vaultState, program),
1957
- loadRelevantTicksHasDebtArrays(vaultId, program)
2018
+ loadRelevantTicksForLiquidate(
2019
+ vaultId,
2020
+ vaultState,
2021
+ liquidationTick,
2022
+ program
2023
+ )
1958
2024
  ]);
2025
+ const tickHasDebt = await loadRelevantTicksHasDebtArraysLiquidate(
2026
+ vaultId,
2027
+ vaultState.topmostTick,
2028
+ nextTick,
2029
+ program
2030
+ );
1959
2031
  for (const branch of branches) {
1960
2032
  remainingAccounts.push({
1961
2033
  pubkey: getBranch(vaultId, branch.branchId),
@@ -1999,7 +2071,7 @@ async function getRemainingAccountsLiquidate(vaultId, vaultState, otherIxs, orac
1999
2071
  });
2000
2072
  }
2001
2073
  const remainingAccountsIndices = [
2002
- sources.length,
2074
+ oracleSources.length,
2003
2075
  branches.length,
2004
2076
  tickAccounts.length,
2005
2077
  tickHasDebt.length
@@ -2074,4 +2146,4 @@ function parseVaultLiquidations(logs) {
2074
2146
  return results;
2075
2147
  }
2076
2148
 
2077
- 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.77";
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.77",
3
+ "version": "0.0.78",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "type": "module",