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

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
 
@@ -6872,14 +6872,17 @@ var JBondClient = class _JBondClient {
6872
6872
  /**
6873
6873
  * Get all bond states with total collected collateral
6874
6874
  */
6875
- async getAllBondStates(bondType) {
6875
+ async getAllBondStates(bondType, session_status) {
6876
6876
  const bondStates = [];
6877
6877
  const bondStateAccounts = await this.program.account.bondState.all();
6878
6878
  for (const { account: state } of bondStateAccounts) {
6879
6879
  if (!sameVariant(state.bondType, bondType)) {
6880
6880
  continue;
6881
6881
  }
6882
- const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6882
+ if (session_status !== void 0 && this.getBondStateSessionStatus(state) !== session_status) {
6883
+ continue;
6884
+ }
6885
+ const bondStateStats = await this.getBondStateStats(state);
6883
6886
  bondStates.push([state, bondStateStats]);
6884
6887
  }
6885
6888
  return bondStates;
@@ -6887,7 +6890,7 @@ var JBondClient = class _JBondClient {
6887
6890
  /**
6888
6891
  * Get session status for bond state
6889
6892
  */
6890
- async getBondStateSessionStatus(bondState) {
6893
+ getBondStateSessionStatus(bondState) {
6891
6894
  const now = Math.floor(Date.now() / 1e3);
6892
6895
  if (bondState.sessionFinishTs.toNumber() > now) {
6893
6896
  return 0 /* Live */;
@@ -6969,6 +6972,13 @@ var JBondClient = class _JBondClient {
6969
6972
  async getBondStateTotalCollected(bondType, bondName, votes) {
6970
6973
  const bondStateData = await this.getBondState(bondType, bondName);
6971
6974
  const bondStateCollateralType = bondStateData.collateralType;
6975
+ const chunk = (arr, n = 100) => {
6976
+ const res = [];
6977
+ for (let i = 0; i < arr.length; i += n) {
6978
+ res.push(arr.slice(i, i + n));
6979
+ }
6980
+ return res;
6981
+ };
6972
6982
  return await matchVariant(bondStateCollateralType, {
6973
6983
  native: async () => {
6974
6984
  const addresses = votes.map((vote) => {
@@ -6987,40 +6997,49 @@ var JBondClient = class _JBondClient {
6987
6997
  );
6988
6998
  return balances.reduce((sum, v) => sum + v, 0);
6989
6999
  },
7000
+ // TODO
6990
7001
  token: async (mint) => {
6991
7002
  const tokenAccounts = votes.map((vote) => {
6992
7003
  const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6993
7004
  return getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
6994
7005
  });
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;
7006
+ if (tokenAccounts.length === 0) {
7007
+ return 0;
7008
+ }
7009
+ let total = 0n;
7010
+ for (const part of chunk(tokenAccounts, 100)) {
7011
+ const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
7012
+ for (let i = 0; i < infos.length; i++) {
7013
+ const info = infos[i];
7014
+ if (!info) {
7015
+ continue;
7016
+ }
7017
+ if (info.data.length !== ACCOUNT_SIZE) {
7018
+ continue;
7000
7019
  }
7001
7020
  try {
7002
- const balance = await this.connection.getTokenAccountBalance(tokenAccounts[accountInfos.indexOf(accountInfo)]);
7003
- return Number(balance.value.uiAmount ?? 0) * LAMPORTS_PER_SOL;
7021
+ const acc = AccountLayout.decode(info.data);
7022
+ const raw = BigInt(acc.amount.toString());
7023
+ total += raw;
7004
7024
  } catch {
7005
- return 0;
7006
7025
  }
7007
- })
7008
- );
7009
- return balances.reduce((sum, v) => sum + v, 0);
7026
+ }
7027
+ }
7028
+ const asNumber = Number(total);
7029
+ return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7010
7030
  }
7011
7031
  });
7012
7032
  }
7013
- async getBondStateStats(bondType, bondName) {
7014
- const bondState = await this.getBondState(bondType, bondName);
7015
- const validatorBonds = await this.getValidatorBondsByState(bondState.bondType, bondState.name);
7033
+ async getBondStateStats(state) {
7034
+ const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
7016
7035
  const totalCollected = await this.getBondStateTotalCollected(
7017
- bondType,
7018
- bondName,
7036
+ state.bondType,
7037
+ state.name,
7019
7038
  validatorBonds.map((vb) => vb.voteAccount)
7020
7039
  );
7021
7040
  const bondStateStats = {
7022
7041
  totalCollected,
7023
- status: await this.getBondStateSessionStatus(bondState)
7042
+ status: await this.getBondStateSessionStatus(state)
7024
7043
  };
7025
7044
  return bondStateStats;
7026
7045
  }