@jpool/bond-sdk 0.9.0-next.13 → 0.9.0-next.15

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/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Program, AnchorProvider, BN } from '@coral-xyz/anchor';
2
2
  import { getStakePoolAccount, STAKE_POOL_PROGRAM_ID, StakePoolInstruction } from '@solana/spl-stake-pool';
3
- import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
3
+ import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync, ACCOUNT_SIZE, AccountLayout } from '@solana/spl-token';
4
4
  import { PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js';
5
5
  import bs58 from 'bs58';
6
6
 
@@ -6682,7 +6682,7 @@ var JBondClient = class _JBondClient {
6682
6682
  if (!creator) {
6683
6683
  throw new Error("Missing creator");
6684
6684
  }
6685
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6685
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6686
6686
  const accounts = {
6687
6687
  bondState,
6688
6688
  validatorBond,
@@ -6725,7 +6725,7 @@ var JBondClient = class _JBondClient {
6725
6725
  creator: props.payer
6726
6726
  }));
6727
6727
  }
6728
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6728
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6729
6729
  const accounts = {
6730
6730
  bondState,
6731
6731
  validatorBond,
@@ -6762,7 +6762,7 @@ var JBondClient = class _JBondClient {
6762
6762
  if (!payer || !destination) {
6763
6763
  throw new Error("Missing payer/destination");
6764
6764
  }
6765
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6765
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6766
6766
  const accounts = {
6767
6767
  bondState,
6768
6768
  validatorBond,
@@ -6790,12 +6790,12 @@ var JBondClient = class _JBondClient {
6790
6790
  const { bondType, name } = props;
6791
6791
  const [bondState] = this.pda.bondState(bondType, name);
6792
6792
  const [validatorBond] = this.pda.validatorBond(bondType, name, props.voteAccount);
6793
- const reserve = props.reserve ?? (await this.getBondState(bondType, name)).reserve;
6793
+ const reserve = props.reserve ?? (await this.getBondState(bondType, name))[0].reserve;
6794
6794
  const authority = props.authority ?? this.program.provider.wallet?.publicKey;
6795
6795
  if (!reserve) {
6796
6796
  throw new Error("Reserve not set");
6797
6797
  }
6798
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6798
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6799
6799
  const accounts = {
6800
6800
  bondState,
6801
6801
  validatorBond,
@@ -6865,21 +6865,29 @@ var JBondClient = class _JBondClient {
6865
6865
  const [globalState] = this.pda.globalState();
6866
6866
  return await this.program.account.globalState.fetch(globalState);
6867
6867
  }
6868
+ /**
6869
+ * Fetch bond state with stats
6870
+ * @param bondType - Type of the bond
6871
+ * @param bondName - Name of the bond
6872
+ */
6868
6873
  async getBondState(bondType, bondName) {
6869
- const [bondState] = this.pda.bondState(bondType, bondName);
6870
- return await this.program.account.bondState.fetch(bondState);
6874
+ const bondState = await this.program.account.bondState.fetch(this.pda.bondState(bondType, bondName)[0]);
6875
+ return [bondState, await this.getBondStateStats(bondState)];
6871
6876
  }
6872
6877
  /**
6873
6878
  * Get all bond states with total collected collateral
6874
6879
  */
6875
- async getAllBondStates(bondType) {
6880
+ async getAllBondStates(bondType, session_status) {
6876
6881
  const bondStates = [];
6877
6882
  const bondStateAccounts = await this.program.account.bondState.all();
6878
6883
  for (const { account: state } of bondStateAccounts) {
6879
6884
  if (!sameVariant(state.bondType, bondType)) {
6880
6885
  continue;
6881
6886
  }
6882
- const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6887
+ if (session_status !== void 0 && this.getBondStateSessionStatus(state) !== session_status) {
6888
+ continue;
6889
+ }
6890
+ const bondStateStats = await this.getBondStateStats(state);
6883
6891
  bondStates.push([state, bondStateStats]);
6884
6892
  }
6885
6893
  return bondStates;
@@ -6887,7 +6895,7 @@ var JBondClient = class _JBondClient {
6887
6895
  /**
6888
6896
  * Get session status for bond state
6889
6897
  */
6890
- async getBondStateSessionStatus(bondState) {
6898
+ getBondStateSessionStatus(bondState) {
6891
6899
  const now = Math.floor(Date.now() / 1e3);
6892
6900
  if (bondState.sessionFinishTs.toNumber() > now) {
6893
6901
  return 0 /* Live */;
@@ -6931,7 +6939,7 @@ var JBondClient = class _JBondClient {
6931
6939
  */
6932
6940
  async getBondCollateralType(bondType, bondName) {
6933
6941
  const bondState = await this.getBondState(bondType, bondName);
6934
- return bondState.collateralType;
6942
+ return bondState[0].collateralType;
6935
6943
  }
6936
6944
  /**
6937
6945
  * Get validator bond account balance (in SOL)
@@ -6943,7 +6951,7 @@ var JBondClient = class _JBondClient {
6943
6951
  async getValidatorBondBalance(bondType, bondName, vote) {
6944
6952
  const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6945
6953
  const bondStateData = await this.getBondState(bondType, bondName);
6946
- const bondStateCollateralType = bondStateData.collateralType;
6954
+ const bondStateCollateralType = bondStateData[0].collateralType;
6947
6955
  return await matchVariant(bondStateCollateralType, {
6948
6956
  native: async () => {
6949
6957
  const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
@@ -6958,7 +6966,7 @@ var JBondClient = class _JBondClient {
6958
6966
  const tokenAccount = getAssociatedTokenAddressSync(mint, address, true);
6959
6967
  try {
6960
6968
  const balance = await this.connection.getTokenAccountBalance(tokenAccount);
6961
- return Number(balance.value.uiAmount ?? 0);
6969
+ return Number(balance.value.uiAmount ?? 0) * LAMPORTS_PER_SOL;
6962
6970
  } catch {
6963
6971
  return 0;
6964
6972
  }
@@ -6968,7 +6976,14 @@ var JBondClient = class _JBondClient {
6968
6976
  // Get total collected collateral for a bond state
6969
6977
  async getBondStateTotalCollected(bondType, bondName, votes) {
6970
6978
  const bondStateData = await this.getBondState(bondType, bondName);
6971
- const bondStateCollateralType = bondStateData.collateralType;
6979
+ const bondStateCollateralType = bondStateData[0].collateralType;
6980
+ const chunk = (arr, n = 100) => {
6981
+ const res = [];
6982
+ for (let i = 0; i < arr.length; i += n) {
6983
+ res.push(arr.slice(i, i + n));
6984
+ }
6985
+ return res;
6986
+ };
6972
6987
  return await matchVariant(bondStateCollateralType, {
6973
6988
  native: async () => {
6974
6989
  const addresses = votes.map((vote) => {
@@ -6992,35 +7007,43 @@ var JBondClient = class _JBondClient {
6992
7007
  const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6993
7008
  return getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
6994
7009
  });
6995
- const accountInfos = await this.connection.getMultipleAccountsInfo(tokenAccounts);
6996
- const balances = await Promise.all(
6997
- accountInfos.map(async (accountInfo) => {
6998
- if (!accountInfo) {
6999
- return 0;
7010
+ if (tokenAccounts.length === 0) {
7011
+ return 0;
7012
+ }
7013
+ let total = 0n;
7014
+ for (const part of chunk(tokenAccounts, 100)) {
7015
+ const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
7016
+ for (let i = 0; i < infos.length; i++) {
7017
+ const info = infos[i];
7018
+ if (!info) {
7019
+ continue;
7020
+ }
7021
+ if (info.data.length !== ACCOUNT_SIZE) {
7022
+ continue;
7000
7023
  }
7001
7024
  try {
7002
- const balance = await this.connection.getTokenAccountBalance(tokenAccounts[accountInfos.indexOf(accountInfo)]);
7003
- return Number(balance.value.uiAmount ?? 0) * LAMPORTS_PER_SOL;
7025
+ const acc = AccountLayout.decode(info.data);
7026
+ const raw = BigInt(acc.amount.toString());
7027
+ total += raw;
7004
7028
  } catch {
7005
- return 0;
7006
7029
  }
7007
- })
7008
- );
7009
- return balances.reduce((sum, v) => sum + v, 0);
7030
+ }
7031
+ }
7032
+ const asNumber = Number(total);
7033
+ return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7010
7034
  }
7011
7035
  });
7012
7036
  }
7013
- async getBondStateStats(bondType, bondName) {
7014
- const bondState = await this.getBondState(bondType, bondName);
7015
- const validatorBonds = await this.getValidatorBondsByState(bondState.bondType, bondState.name);
7037
+ async getBondStateStats(state) {
7038
+ const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
7016
7039
  const totalCollected = await this.getBondStateTotalCollected(
7017
- bondType,
7018
- bondName,
7040
+ state.bondType,
7041
+ state.name,
7019
7042
  validatorBonds.map((vb) => vb.voteAccount)
7020
7043
  );
7021
7044
  const bondStateStats = {
7022
7045
  totalCollected,
7023
- status: await this.getBondStateSessionStatus(bondState)
7046
+ status: await this.getBondStateSessionStatus(state)
7024
7047
  };
7025
7048
  return bondStateStats;
7026
7049
  }