@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.d.mts CHANGED
@@ -1911,7 +1911,7 @@ declare class JBondClient {
1911
1911
  /**
1912
1912
  * Get all bond states with total collected collateral
1913
1913
  */
1914
- getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
1914
+ getAllBondStates(bondType: BondType, session_status?: SessionStatus): Promise<Array<[BondState, BondStateStats]>>;
1915
1915
  /**
1916
1916
  * Get session status for bond state
1917
1917
  */
package/dist/index.d.ts CHANGED
@@ -1911,7 +1911,7 @@ declare class JBondClient {
1911
1911
  /**
1912
1912
  * Get all bond states with total collected collateral
1913
1913
  */
1914
- getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
1914
+ getAllBondStates(bondType: BondType, session_status?: SessionStatus): Promise<Array<[BondState, BondStateStats]>>;
1915
1915
  /**
1916
1916
  * Get session status for bond state
1917
1917
  */
package/dist/index.js CHANGED
@@ -6908,14 +6908,17 @@ var JBondClient = class _JBondClient {
6908
6908
  /**
6909
6909
  * Get all bond states with total collected collateral
6910
6910
  */
6911
- async getAllBondStates(bondType) {
6911
+ async getAllBondStates(bondType, session_status) {
6912
6912
  const bondStates = [];
6913
6913
  const bondStateAccounts = await this.program.account.bondState.all();
6914
6914
  for (const { account: state } of bondStateAccounts) {
6915
6915
  if (!sameVariant(state.bondType, bondType)) {
6916
6916
  continue;
6917
6917
  }
6918
- const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6918
+ if (session_status !== void 0 && this.getBondStateSessionStatus(state) !== session_status) {
6919
+ continue;
6920
+ }
6921
+ const bondStateStats = await this.getBondStateStats(state);
6919
6922
  bondStates.push([state, bondStateStats]);
6920
6923
  }
6921
6924
  return bondStates;
@@ -6923,7 +6926,7 @@ var JBondClient = class _JBondClient {
6923
6926
  /**
6924
6927
  * Get session status for bond state
6925
6928
  */
6926
- async getBondStateSessionStatus(bondState) {
6929
+ getBondStateSessionStatus(bondState) {
6927
6930
  const now = Math.floor(Date.now() / 1e3);
6928
6931
  if (bondState.sessionFinishTs.toNumber() > now) {
6929
6932
  return 0 /* Live */;
@@ -7005,6 +7008,13 @@ var JBondClient = class _JBondClient {
7005
7008
  async getBondStateTotalCollected(bondType, bondName, votes) {
7006
7009
  const bondStateData = await this.getBondState(bondType, bondName);
7007
7010
  const bondStateCollateralType = bondStateData.collateralType;
7011
+ const chunk = (arr, n = 100) => {
7012
+ const res = [];
7013
+ for (let i = 0; i < arr.length; i += n) {
7014
+ res.push(arr.slice(i, i + n));
7015
+ }
7016
+ return res;
7017
+ };
7008
7018
  return await matchVariant(bondStateCollateralType, {
7009
7019
  native: async () => {
7010
7020
  const addresses = votes.map((vote) => {
@@ -7023,40 +7033,49 @@ var JBondClient = class _JBondClient {
7023
7033
  );
7024
7034
  return balances.reduce((sum, v) => sum + v, 0);
7025
7035
  },
7036
+ // TODO
7026
7037
  token: async (mint) => {
7027
7038
  const tokenAccounts = votes.map((vote) => {
7028
7039
  const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
7029
7040
  return splToken.getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
7030
7041
  });
7031
- const accountInfos = await this.connection.getMultipleAccountsInfo(tokenAccounts);
7032
- const balances = await Promise.all(
7033
- accountInfos.map(async (accountInfo) => {
7034
- if (!accountInfo) {
7035
- return 0;
7042
+ if (tokenAccounts.length === 0) {
7043
+ return 0;
7044
+ }
7045
+ let total = 0n;
7046
+ for (const part of chunk(tokenAccounts, 100)) {
7047
+ const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
7048
+ for (let i = 0; i < infos.length; i++) {
7049
+ const info = infos[i];
7050
+ if (!info) {
7051
+ continue;
7052
+ }
7053
+ if (info.data.length !== splToken.ACCOUNT_SIZE) {
7054
+ continue;
7036
7055
  }
7037
7056
  try {
7038
- const balance = await this.connection.getTokenAccountBalance(tokenAccounts[accountInfos.indexOf(accountInfo)]);
7039
- return Number(balance.value.uiAmount ?? 0) * web3_js.LAMPORTS_PER_SOL;
7057
+ const acc = splToken.AccountLayout.decode(info.data);
7058
+ const raw = BigInt(acc.amount.toString());
7059
+ total += raw;
7040
7060
  } catch {
7041
- return 0;
7042
7061
  }
7043
- })
7044
- );
7045
- return balances.reduce((sum, v) => sum + v, 0);
7062
+ }
7063
+ }
7064
+ const asNumber = Number(total);
7065
+ return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7046
7066
  }
7047
7067
  });
7048
7068
  }
7049
- async getBondStateStats(bondType, bondName) {
7050
- const bondState = await this.getBondState(bondType, bondName);
7051
- const validatorBonds = await this.getValidatorBondsByState(bondState.bondType, bondState.name);
7069
+ async getBondStateStats(state) {
7070
+ const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
7052
7071
  const totalCollected = await this.getBondStateTotalCollected(
7053
- bondType,
7054
- bondName,
7072
+ state.bondType,
7073
+ state.name,
7055
7074
  validatorBonds.map((vb) => vb.voteAccount)
7056
7075
  );
7057
7076
  const bondStateStats = {
7058
7077
  totalCollected,
7059
- status: await this.getBondStateSessionStatus(bondState)
7078
+ status: await this.getBondStateSessionStatus(state)
7060
7079
  };
7061
7080
  return bondStateStats;
7062
7081
  }