@kamino-finance/kliquidity-sdk 7.0.0 → 7.0.2

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.
package/src/Kamino.ts CHANGED
@@ -64,7 +64,7 @@ import {
64
64
  TotalStrategyVaultTokens,
65
65
  TreasuryFeeVault,
66
66
  } from './models';
67
- import { PROGRAM_ID_CLI as WHIRLPOOL_PROGRAM_ID, setWhirlpoolsProgramId } from './whirlpools-client/programId';
67
+ import { setWhirlpoolsProgramId } from './whirlpools-client/programId';
68
68
  import { OraclePrices, Scope } from '@kamino-finance/scope-sdk';
69
69
  import {
70
70
  batchFetch,
@@ -179,7 +179,7 @@ import {
179
179
  StrategyStatusKind,
180
180
  } from './kamino-client/types';
181
181
  import { AmmConfig, PersonalPositionState, PoolState } from './raydium_client';
182
- import { PROGRAM_ID as RAYDIUM_PROGRAM_ID, setRaydiumProgramId } from './raydium_client/programId';
182
+ import { setRaydiumProgramId } from './raydium_client/programId';
183
183
  import {
184
184
  getPdaProtocolPositionAddress,
185
185
  i32ToBytes,
@@ -330,7 +330,7 @@ import {
330
330
  } from './rebalance_methods/autodriftRebalance';
331
331
  import { KaminoPrices, OraclePricesAndCollateralInfos } from './models';
332
332
  import { getRemoveLiquidityQuote } from './whirlpools-client/shim/remove-liquidity';
333
- import { METEORA_PROGRAM_ID, setMeteoraProgramId } from './meteora_client/programId';
333
+ import { setMeteoraProgramId } from './meteora_client/programId';
334
334
  import { computeMeteoraFee, MeteoraPool, MeteoraService } from './services/MeteoraService';
335
335
  import {
336
336
  binIdToBinArrayIndex,
@@ -401,19 +401,17 @@ export class Kamino {
401
401
  setWhirlpoolsProgramId(whirlpoolProgramId);
402
402
  }
403
403
 
404
- if (cluster === 'localnet') {
405
- if (raydiumProgramId) {
406
- setRaydiumProgramId(raydiumProgramId);
407
- }
404
+ if (raydiumProgramId) {
405
+ setRaydiumProgramId(raydiumProgramId);
408
406
  }
409
407
 
410
408
  if (meteoraProgramId) {
411
409
  setMeteoraProgramId(meteoraProgramId);
412
410
  }
413
411
 
414
- this._orcaService = new OrcaService(connection, cluster, this._globalConfig, this._kaminoProgramId);
415
- this._raydiumService = new RaydiumService(connection, cluster);
416
- this._meteoraService = new MeteoraService(connection, this._globalConfig);
412
+ this._orcaService = new OrcaService(connection, cluster, whirlpoolProgramId);
413
+ this._raydiumService = new RaydiumService(connection, raydiumProgramId);
414
+ this._meteoraService = new MeteoraService(connection, meteoraProgramId);
417
415
  }
418
416
 
419
417
  getConnection = () => this._connection;
@@ -2551,7 +2549,10 @@ export class Kamino {
2551
2549
  }
2552
2550
 
2553
2551
  if (dex.toNumber() == dexToNumber('METEORA')) {
2554
- const [key, _] = PublicKey.findProgramAddressSync([Buffer.from('__event_authority')], METEORA_PROGRAM_ID);
2552
+ const [key, _] = PublicKey.findProgramAddressSync(
2553
+ [Buffer.from('__event_authority')],
2554
+ this._meteoraService.getMeteoraProgramId()
2555
+ );
2555
2556
  return key;
2556
2557
  }
2557
2558
  throw new Error('Invalid dex');
@@ -4111,7 +4112,7 @@ export class Kamino {
4111
4112
  strategyState.tokenBMint
4112
4113
  );
4113
4114
 
4114
- let programId = WHIRLPOOL_PROGRAM_ID;
4115
+ let programId = this._orcaService.getWhirlpoolProgramId();
4115
4116
 
4116
4117
  let poolRewardVault0 = PublicKey.default;
4117
4118
  let poolRewardVault1 = PublicKey.default;
@@ -4132,7 +4133,7 @@ export class Kamino {
4132
4133
  rewardMint1 = whirlpool.rewardInfos[1].mint;
4133
4134
  rewardMint2 = whirlpool.rewardInfos[2].mint;
4134
4135
  } else if (strategyState.strategyDex.toNumber() == dexToNumber('RAYDIUM')) {
4135
- programId = RAYDIUM_PROGRAM_ID;
4136
+ programId = this._raydiumService.getRaydiumProgramId();
4136
4137
 
4137
4138
  const poolState = await PoolState.fetch(this._connection, strategyState.pool);
4138
4139
  if (!poolState) {
@@ -4145,7 +4146,7 @@ export class Kamino {
4145
4146
  rewardMint1 = poolState.rewardInfos[1].tokenMint;
4146
4147
  rewardMint2 = poolState.rewardInfos[2].tokenMint;
4147
4148
  } else if (strategyState.strategyDex.toNumber() == dexToNumber('METEORA')) {
4148
- programId = METEORA_PROGRAM_ID;
4149
+ programId = this._meteoraService.getMeteoraProgramId();
4149
4150
 
4150
4151
  const poolState = await LbPair.fetch(this._connection, strategyState.pool);
4151
4152
  if (!poolState) {
@@ -4215,7 +4216,7 @@ export class Kamino {
4215
4216
  if (strategyState.strategyDex.toNumber() == dexToNumber('RAYDIUM')) {
4216
4217
  const [poolTickArrayBitmap, _poolTickArrayBitmapBump] = PublicKey.findProgramAddressSync(
4217
4218
  [Buffer.from('pool_tick_array_bitmap_extension'), strategyState.pool.toBuffer()],
4218
- RAYDIUM_PROGRAM_ID
4219
+ this._raydiumService.getRaydiumProgramId()
4219
4220
  );
4220
4221
 
4221
4222
  ixn.keys.push({ pubkey: poolTickArrayBitmap, isSigner: false, isWritable: true });
@@ -4231,7 +4232,7 @@ export class Kamino {
4231
4232
  getMetadataProgramAddressesOrca = (positionMint: PublicKey): MetadataProgramAddressesOrca => {
4232
4233
  const [position, positionBump] = PublicKey.findProgramAddressSync(
4233
4234
  [Buffer.from('position'), positionMint.toBuffer()],
4234
- WHIRLPOOL_PROGRAM_ID
4235
+ this._orcaService.getWhirlpoolProgramId()
4235
4236
  );
4236
4237
 
4237
4238
  const [positionMetadata, positionMetadataBump] = PublicKey.findProgramAddressSync(
@@ -4254,7 +4255,7 @@ export class Kamino {
4254
4255
  tickUpperIndex: number
4255
4256
  ): MetadataProgramAddressesRaydium => {
4256
4257
  const { publicKey: protocolPosition, nonce: protocolPositionBump } = getPdaProtocolPositionAddress(
4257
- RAYDIUM_PROGRAM_ID,
4258
+ this._raydiumService.getRaydiumProgramId(),
4258
4259
  pool,
4259
4260
  tickLowerIndex,
4260
4261
  tickUpperIndex
@@ -4262,7 +4263,7 @@ export class Kamino {
4262
4263
 
4263
4264
  const [position, positionBump] = PublicKey.findProgramAddressSync(
4264
4265
  [Buffer.from('position'), positionMint.toBuffer()],
4265
- RAYDIUM_PROGRAM_ID
4266
+ this._raydiumService.getRaydiumProgramId()
4266
4267
  );
4267
4268
 
4268
4269
  const [positionMetadata, positionMetadataBump] = PublicKey.findProgramAddressSync(
@@ -4291,11 +4292,11 @@ export class Kamino {
4291
4292
 
4292
4293
  const [lowerTickPubkey, lowerTickBump] = PublicKey.findProgramAddressSync(
4293
4294
  [Buffer.from('tick_array'), whirlpool.toBuffer(), Buffer.from(startTickIndex.toString())],
4294
- WHIRLPOOL_PROGRAM_ID
4295
+ this._orcaService.getWhirlpoolProgramId()
4295
4296
  );
4296
4297
  const [upperTickPubkey, upperTickBump] = PublicKey.findProgramAddressSync(
4297
4298
  [Buffer.from('tick_array'), whirlpool.toBuffer(), Buffer.from(endTickIndex.toString())],
4298
- WHIRLPOOL_PROGRAM_ID
4299
+ this._orcaService.getWhirlpoolProgramId()
4299
4300
  );
4300
4301
  return {
4301
4302
  lowerTick: lowerTickPubkey,
@@ -4316,11 +4317,11 @@ export class Kamino {
4316
4317
 
4317
4318
  const [lowerTickPubkey, lowerTickBump] = PublicKey.findProgramAddressSync(
4318
4319
  [Buffer.from('tick_array'), pool.toBuffer(), i32ToBytes(startTickIndex)],
4319
- RAYDIUM_PROGRAM_ID
4320
+ this._raydiumService.getRaydiumProgramId()
4320
4321
  );
4321
4322
  const [upperTickPubkey, upperTickBump] = PublicKey.findProgramAddressSync(
4322
4323
  [Buffer.from('tick_array'), pool.toBuffer(), i32ToBytes(endTickIndex)],
4323
- RAYDIUM_PROGRAM_ID
4324
+ this._raydiumService.getRaydiumProgramId()
4324
4325
  );
4325
4326
  return {
4326
4327
  lowerTick: lowerTickPubkey,
@@ -4385,7 +4386,7 @@ export class Kamino {
4385
4386
  pool: PublicKey,
4386
4387
  tickLowerIndex: number
4387
4388
  ): LowerAndUpperTickPubkeys => {
4388
- const meteoraProgramId = METEORA_PROGRAM_ID;
4389
+ const meteoraProgramId = this._meteoraService.getMeteoraProgramId();
4389
4390
 
4390
4391
  const lowerBinArrayIndex = binIdToBinArrayIndex(new BN(tickLowerIndex));
4391
4392
  const [lowerTick, lowerTickBump] = deriveBinArray(pool, lowerBinArrayIndex, meteoraProgramId);
@@ -4599,7 +4600,7 @@ export class Kamino {
4599
4600
  tokenProgram: TOKEN_PROGRAM_ID,
4600
4601
  tokenProgram2022: TOKEN_2022_PROGRAM_ID,
4601
4602
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
4602
- poolProgram: WHIRLPOOL_PROGRAM_ID,
4603
+ poolProgram: this._orcaService.getWhirlpoolProgramId(),
4603
4604
  oldPositionOrBaseVaultAuthority: isRebalancing ? oldPositionOrBaseVaultAuthority : baseVaultAuthority,
4604
4605
  oldPositionMintOrBaseVaultAuthority: isRebalancing ? oldPositionMintOrBaseVaultAuthority : positionMint,
4605
4606
  oldPositionTokenAccountOrBaseVaultAuthority: isRebalancing
@@ -4729,7 +4730,7 @@ export class Kamino {
4729
4730
  tokenProgram: TOKEN_PROGRAM_ID,
4730
4731
  tokenProgram2022: TOKEN_2022_PROGRAM_ID,
4731
4732
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
4732
- poolProgram: RAYDIUM_PROGRAM_ID,
4733
+ poolProgram: this._raydiumService.getRaydiumProgramId(),
4733
4734
  oldPositionOrBaseVaultAuthority: isRebalancing ? oldPositionOrBaseVaultAuthority : baseVaultAuthority,
4734
4735
  oldPositionMintOrBaseVaultAuthority: isRebalancing ? oldPositionMintOrBaseVaultAuthority : positionMint,
4735
4736
  oldPositionTokenAccountOrBaseVaultAuthority: isRebalancing
@@ -4754,7 +4755,7 @@ export class Kamino {
4754
4755
  };
4755
4756
  const [poolTickArrayBitmap, _poolTickArrayBitmapBump] = PublicKey.findProgramAddressSync(
4756
4757
  [Buffer.from('pool_tick_array_bitmap_extension'), pool.toBuffer()],
4757
- RAYDIUM_PROGRAM_ID
4758
+ this._raydiumService.getRaydiumProgramId()
4758
4759
  );
4759
4760
 
4760
4761
  const ix = openLiquidityPosition(args, accounts);
@@ -4869,7 +4870,7 @@ export class Kamino {
4869
4870
  tokenProgram: TOKEN_PROGRAM_ID,
4870
4871
  tokenProgram2022: TOKEN_2022_PROGRAM_ID,
4871
4872
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
4872
- poolProgram: WHIRLPOOL_PROGRAM_ID,
4873
+ poolProgram: this._orcaService.getWhirlpoolProgramId(),
4873
4874
  oldPositionOrBaseVaultAuthority: isRebalancing ? oldPositionOrBaseVaultAuthority : baseVaultAuthority,
4874
4875
  oldPositionMintOrBaseVaultAuthority: isRebalancing ? oldPositionMintOrBaseVaultAuthority : positionMint,
4875
4876
  oldPositionTokenAccountOrBaseVaultAuthority: isRebalancing
@@ -6881,7 +6882,7 @@ export class Kamino {
6881
6882
  return [new Decimal(0), new Decimal(0)];
6882
6883
  }
6883
6884
  // Given A in ATA, calc how much A and B
6884
- const accessor = new OrcaDAL(whirlpoolConfig, WHIRLPOOL_PROGRAM_ID, this._connection);
6885
+ const accessor = new OrcaDAL(whirlpoolConfig, this._orcaService.getWhirlpoolProgramId(), this._connection);
6885
6886
  const orcaPosition = new OrcaPosition(accessor);
6886
6887
  const defaultSlippagePercentage = Percentage.fromFraction(1, 1000); // 0.1%
6887
6888
 
@@ -7018,7 +7019,11 @@ export class Kamino {
7018
7019
  return [tokenAAmount, tokenBAmount];
7019
7020
  } else {
7020
7021
  const binArrayIndex = binIdToBinArrayIndex(new BN(poolState.activeId));
7021
- const [binArrayPk] = deriveBinArray(strategyState.pool, binArrayIndex, METEORA_PROGRAM_ID);
7022
+ const [binArrayPk] = deriveBinArray(
7023
+ strategyState.pool,
7024
+ binArrayIndex,
7025
+ this._meteoraService.getMeteoraProgramId()
7026
+ );
7022
7027
  const binArray = await BinArray.fetch(this._connection, binArrayPk);
7023
7028
  if (!binArray) {
7024
7029
  throw new Error(`bin array ${binArrayPk.toString()} is not found`);
@@ -7567,7 +7572,11 @@ export class Kamino {
7567
7572
  const tickIndex = getNextValidTickIndex(priceToTickIndex(price, decimalsA, decimalsB), whilrpoolState.tickSpacing);
7568
7573
  const startTickIndex = getStartTickIndex(tickIndex, whilrpoolState.tickSpacing);
7569
7574
 
7570
- const [startTickIndexPk, _startTickIndexBump] = getTickArray(WHIRLPOOL_PROGRAM_ID, poolAddress, startTickIndex);
7575
+ const [startTickIndexPk, _startTickIndexBump] = getTickArray(
7576
+ this._orcaService.getWhirlpoolProgramId(),
7577
+ poolAddress,
7578
+ startTickIndex
7579
+ );
7571
7580
  const tick = await TickArray.fetch(this._connection, startTickIndexPk);
7572
7581
  // initialize tick if it doesn't exist
7573
7582
  if (!tick) {
@@ -7603,7 +7612,11 @@ export class Kamino {
7603
7612
  const binArray = getBinIdFromPriceWithDecimals(price, poolState.binStep, true, decimalsA, decimalsB);
7604
7613
  const binArrayIndex = binIdToBinArrayIndex(new BN(binArray));
7605
7614
 
7606
- const [startTickIndexPk, _startTickIndexBump] = deriveBinArray(poolAddress, binArrayIndex, METEORA_PROGRAM_ID);
7615
+ const [startTickIndexPk, _startTickIndexBump] = deriveBinArray(
7616
+ poolAddress,
7617
+ binArrayIndex,
7618
+ this._meteoraService.getMeteoraProgramId()
7619
+ );
7607
7620
  const tick = await TickArray.fetch(this._connection, startTickIndexPk);
7608
7621
  // initialize tick if it doesn't exist
7609
7622
  if (!tick) {
@@ -22,11 +22,15 @@ export interface MeteoraPool {
22
22
 
23
23
  export class MeteoraService {
24
24
  private readonly _connection: Connection;
25
- private readonly _globalConfig: PublicKey;
25
+ private readonly _meteoraProgramId: PublicKey;
26
26
 
27
- constructor(connection: Connection, globalConfig: PublicKey) {
27
+ constructor(connection: Connection, meteoraProgramId: PublicKey = METEORA_PROGRAM_ID) {
28
28
  this._connection = connection;
29
- this._globalConfig = globalConfig;
29
+ this._meteoraProgramId = meteoraProgramId;
30
+ }
31
+
32
+ getMeteoraProgramId(): PublicKey {
33
+ return this._meteoraProgramId;
30
34
  }
31
35
 
32
36
  async getPool(poolAddress: PublicKey): Promise<LbPair | null> {
@@ -25,28 +25,22 @@ import {
25
25
  import { WHIRLPOOL_PROGRAM_ID } from '../whirlpools-client/programId';
26
26
  import { CollateralInfo } from '../kamino-client/types';
27
27
  import { KaminoPrices } from '../models';
28
- import { PROGRAM_ID } from '../kamino-client/programId';
29
28
 
30
29
  export class OrcaService {
31
30
  private readonly _connection: Connection;
32
- private readonly _cluster: SolanaCluster;
31
+ private readonly _whirilpoolProgramId: PublicKey;
33
32
  private readonly _orcaNetwork: OrcaNetwork;
34
33
  private readonly _orcaApiUrl: string;
35
- private readonly _globalConfig: PublicKey;
36
- private readonly _kaminoProgramId: PublicKey;
37
-
38
- constructor(
39
- connection: Connection,
40
- cluster: SolanaCluster,
41
- globalConfig: PublicKey,
42
- kaminoProgramId: PublicKey = PROGRAM_ID
43
- ) {
34
+
35
+ constructor(connection: Connection, cluster: SolanaCluster, whirlpoolProgramId: PublicKey = WHIRLPOOL_PROGRAM_ID) {
44
36
  this._connection = connection;
45
- this._cluster = cluster;
46
- this._globalConfig = globalConfig;
37
+ this._whirilpoolProgramId = whirlpoolProgramId;
47
38
  this._orcaNetwork = cluster === 'mainnet-beta' ? OrcaNetwork.MAINNET : OrcaNetwork.DEVNET;
48
39
  this._orcaApiUrl = `https://api.${cluster === 'mainnet-beta' ? 'mainnet' : 'devnet'}.orca.so`;
49
- this._kaminoProgramId = kaminoProgramId;
40
+ }
41
+
42
+ getWhirlpoolProgramId(): PublicKey {
43
+ return this._whirilpoolProgramId;
50
44
  }
51
45
 
52
46
  async getOrcaWhirlpools() {
@@ -1,5 +1,4 @@
1
1
  import { Connection, PublicKey } from '@solana/web3.js';
2
- import { SolanaCluster } from '@hubbleprotocol/hubble-config';
3
2
  import {
4
3
  LiquidityDistribution as RaydiumLiquidityDistribuion,
5
4
  Pool,
@@ -25,11 +24,15 @@ import { priceToTickIndexWithRounding } from '../utils/raydium';
25
24
 
26
25
  export class RaydiumService {
27
26
  private readonly _connection: Connection;
28
- private readonly _cluster: SolanaCluster;
27
+ private readonly _raydiumProgramId: PublicKey;
29
28
 
30
- constructor(connection: Connection, cluster: SolanaCluster) {
29
+ constructor(connection: Connection, raydiumProgramId: PublicKey = RAYDIUM_PROGRAM_ID) {
30
+ this._raydiumProgramId = raydiumProgramId;
31
31
  this._connection = connection;
32
- this._cluster = cluster;
32
+ }
33
+
34
+ getRaydiumProgramId(): PublicKey {
35
+ return this._raydiumProgramId;
33
36
  }
34
37
 
35
38
  async getRaydiumWhirlpools(): Promise<RaydiumPoolsResponse> {
@@ -307,7 +310,7 @@ export class RaydiumService {
307
310
  }
308
311
 
309
312
  async getPositionsCountByPool(pool: PublicKey): Promise<number> {
310
- const positions = await this._connection.getProgramAccounts(RAYDIUM_PROGRAM_ID, {
313
+ const positions = await this._connection.getProgramAccounts(this._raydiumProgramId, {
311
314
  commitment: 'confirmed',
312
315
  filters: [
313
316
  { dataSize: PositionInfoLayout.span },