@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.d.mts CHANGED
@@ -1907,11 +1907,16 @@ declare class JBondClient {
1907
1907
  * Fetch global state or throw if not found
1908
1908
  */
1909
1909
  getGlobalState(): Promise<GlobalState>;
1910
- getBondState(bondType: BondType, bondName: string): Promise<BondState>;
1910
+ /**
1911
+ * Fetch bond state with stats
1912
+ * @param bondType - Type of the bond
1913
+ * @param bondName - Name of the bond
1914
+ */
1915
+ getBondState(bondType: BondType, bondName: string): Promise<[BondState, BondStateStats]>;
1911
1916
  /**
1912
1917
  * Get all bond states with total collected collateral
1913
1918
  */
1914
- getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
1919
+ getAllBondStates(bondType: BondType, session_status?: SessionStatus): Promise<Array<[BondState, BondStateStats]>>;
1915
1920
  /**
1916
1921
  * Get session status for bond state
1917
1922
  */
package/dist/index.d.ts CHANGED
@@ -1907,11 +1907,16 @@ declare class JBondClient {
1907
1907
  * Fetch global state or throw if not found
1908
1908
  */
1909
1909
  getGlobalState(): Promise<GlobalState>;
1910
- getBondState(bondType: BondType, bondName: string): Promise<BondState>;
1910
+ /**
1911
+ * Fetch bond state with stats
1912
+ * @param bondType - Type of the bond
1913
+ * @param bondName - Name of the bond
1914
+ */
1915
+ getBondState(bondType: BondType, bondName: string): Promise<[BondState, BondStateStats]>;
1911
1916
  /**
1912
1917
  * Get all bond states with total collected collateral
1913
1918
  */
1914
- getAllBondStates(bondType: BondType): Promise<Array<[BondState, BondStateStats]>>;
1919
+ getAllBondStates(bondType: BondType, session_status?: SessionStatus): Promise<Array<[BondState, BondStateStats]>>;
1915
1920
  /**
1916
1921
  * Get session status for bond state
1917
1922
  */
package/dist/index.js CHANGED
@@ -6718,7 +6718,7 @@ var JBondClient = class _JBondClient {
6718
6718
  if (!creator) {
6719
6719
  throw new Error("Missing creator");
6720
6720
  }
6721
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6721
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6722
6722
  const accounts = {
6723
6723
  bondState,
6724
6724
  validatorBond,
@@ -6761,7 +6761,7 @@ var JBondClient = class _JBondClient {
6761
6761
  creator: props.payer
6762
6762
  }));
6763
6763
  }
6764
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6764
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6765
6765
  const accounts = {
6766
6766
  bondState,
6767
6767
  validatorBond,
@@ -6798,7 +6798,7 @@ var JBondClient = class _JBondClient {
6798
6798
  if (!payer || !destination) {
6799
6799
  throw new Error("Missing payer/destination");
6800
6800
  }
6801
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6801
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6802
6802
  const accounts = {
6803
6803
  bondState,
6804
6804
  validatorBond,
@@ -6826,12 +6826,12 @@ var JBondClient = class _JBondClient {
6826
6826
  const { bondType, name } = props;
6827
6827
  const [bondState] = this.pda.bondState(bondType, name);
6828
6828
  const [validatorBond] = this.pda.validatorBond(bondType, name, props.voteAccount);
6829
- const reserve = props.reserve ?? (await this.getBondState(bondType, name)).reserve;
6829
+ const reserve = props.reserve ?? (await this.getBondState(bondType, name))[0].reserve;
6830
6830
  const authority = props.authority ?? this.program.provider.wallet?.publicKey;
6831
6831
  if (!reserve) {
6832
6832
  throw new Error("Reserve not set");
6833
6833
  }
6834
- const collateralType = (await this.getBondState(bondType, name)).collateralType;
6834
+ const collateralType = (await this.getBondState(bondType, name))[0].collateralType;
6835
6835
  const accounts = {
6836
6836
  bondState,
6837
6837
  validatorBond,
@@ -6901,21 +6901,29 @@ var JBondClient = class _JBondClient {
6901
6901
  const [globalState] = this.pda.globalState();
6902
6902
  return await this.program.account.globalState.fetch(globalState);
6903
6903
  }
6904
+ /**
6905
+ * Fetch bond state with stats
6906
+ * @param bondType - Type of the bond
6907
+ * @param bondName - Name of the bond
6908
+ */
6904
6909
  async getBondState(bondType, bondName) {
6905
- const [bondState] = this.pda.bondState(bondType, bondName);
6906
- return await this.program.account.bondState.fetch(bondState);
6910
+ const bondState = await this.program.account.bondState.fetch(this.pda.bondState(bondType, bondName)[0]);
6911
+ return [bondState, await this.getBondStateStats(bondState)];
6907
6912
  }
6908
6913
  /**
6909
6914
  * Get all bond states with total collected collateral
6910
6915
  */
6911
- async getAllBondStates(bondType) {
6916
+ async getAllBondStates(bondType, session_status) {
6912
6917
  const bondStates = [];
6913
6918
  const bondStateAccounts = await this.program.account.bondState.all();
6914
6919
  for (const { account: state } of bondStateAccounts) {
6915
6920
  if (!sameVariant(state.bondType, bondType)) {
6916
6921
  continue;
6917
6922
  }
6918
- const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6923
+ if (session_status !== void 0 && this.getBondStateSessionStatus(state) !== session_status) {
6924
+ continue;
6925
+ }
6926
+ const bondStateStats = await this.getBondStateStats(state);
6919
6927
  bondStates.push([state, bondStateStats]);
6920
6928
  }
6921
6929
  return bondStates;
@@ -6923,7 +6931,7 @@ var JBondClient = class _JBondClient {
6923
6931
  /**
6924
6932
  * Get session status for bond state
6925
6933
  */
6926
- async getBondStateSessionStatus(bondState) {
6934
+ getBondStateSessionStatus(bondState) {
6927
6935
  const now = Math.floor(Date.now() / 1e3);
6928
6936
  if (bondState.sessionFinishTs.toNumber() > now) {
6929
6937
  return 0 /* Live */;
@@ -6967,7 +6975,7 @@ var JBondClient = class _JBondClient {
6967
6975
  */
6968
6976
  async getBondCollateralType(bondType, bondName) {
6969
6977
  const bondState = await this.getBondState(bondType, bondName);
6970
- return bondState.collateralType;
6978
+ return bondState[0].collateralType;
6971
6979
  }
6972
6980
  /**
6973
6981
  * Get validator bond account balance (in SOL)
@@ -6979,7 +6987,7 @@ var JBondClient = class _JBondClient {
6979
6987
  async getValidatorBondBalance(bondType, bondName, vote) {
6980
6988
  const [address] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
6981
6989
  const bondStateData = await this.getBondState(bondType, bondName);
6982
- const bondStateCollateralType = bondStateData.collateralType;
6990
+ const bondStateCollateralType = bondStateData[0].collateralType;
6983
6991
  return await matchVariant(bondStateCollateralType, {
6984
6992
  native: async () => {
6985
6993
  const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
@@ -6994,7 +7002,7 @@ var JBondClient = class _JBondClient {
6994
7002
  const tokenAccount = splToken.getAssociatedTokenAddressSync(mint, address, true);
6995
7003
  try {
6996
7004
  const balance = await this.connection.getTokenAccountBalance(tokenAccount);
6997
- return Number(balance.value.uiAmount ?? 0);
7005
+ return Number(balance.value.uiAmount ?? 0) * web3_js.LAMPORTS_PER_SOL;
6998
7006
  } catch {
6999
7007
  return 0;
7000
7008
  }
@@ -7004,7 +7012,14 @@ var JBondClient = class _JBondClient {
7004
7012
  // Get total collected collateral for a bond state
7005
7013
  async getBondStateTotalCollected(bondType, bondName, votes) {
7006
7014
  const bondStateData = await this.getBondState(bondType, bondName);
7007
- const bondStateCollateralType = bondStateData.collateralType;
7015
+ const bondStateCollateralType = bondStateData[0].collateralType;
7016
+ const chunk = (arr, n = 100) => {
7017
+ const res = [];
7018
+ for (let i = 0; i < arr.length; i += n) {
7019
+ res.push(arr.slice(i, i + n));
7020
+ }
7021
+ return res;
7022
+ };
7008
7023
  return await matchVariant(bondStateCollateralType, {
7009
7024
  native: async () => {
7010
7025
  const addresses = votes.map((vote) => {
@@ -7028,35 +7043,43 @@ var JBondClient = class _JBondClient {
7028
7043
  const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
7029
7044
  return splToken.getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
7030
7045
  });
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;
7046
+ if (tokenAccounts.length === 0) {
7047
+ return 0;
7048
+ }
7049
+ let total = 0n;
7050
+ for (const part of chunk(tokenAccounts, 100)) {
7051
+ const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
7052
+ for (let i = 0; i < infos.length; i++) {
7053
+ const info = infos[i];
7054
+ if (!info) {
7055
+ continue;
7056
+ }
7057
+ if (info.data.length !== splToken.ACCOUNT_SIZE) {
7058
+ continue;
7036
7059
  }
7037
7060
  try {
7038
- const balance = await this.connection.getTokenAccountBalance(tokenAccounts[accountInfos.indexOf(accountInfo)]);
7039
- return Number(balance.value.uiAmount ?? 0) * web3_js.LAMPORTS_PER_SOL;
7061
+ const acc = splToken.AccountLayout.decode(info.data);
7062
+ const raw = BigInt(acc.amount.toString());
7063
+ total += raw;
7040
7064
  } catch {
7041
- return 0;
7042
7065
  }
7043
- })
7044
- );
7045
- return balances.reduce((sum, v) => sum + v, 0);
7066
+ }
7067
+ }
7068
+ const asNumber = Number(total);
7069
+ return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7046
7070
  }
7047
7071
  });
7048
7072
  }
7049
- async getBondStateStats(bondType, bondName) {
7050
- const bondState = await this.getBondState(bondType, bondName);
7051
- const validatorBonds = await this.getValidatorBondsByState(bondState.bondType, bondState.name);
7073
+ async getBondStateStats(state) {
7074
+ const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
7052
7075
  const totalCollected = await this.getBondStateTotalCollected(
7053
- bondType,
7054
- bondName,
7076
+ state.bondType,
7077
+ state.name,
7055
7078
  validatorBonds.map((vb) => vb.voteAccount)
7056
7079
  );
7057
7080
  const bondStateStats = {
7058
7081
  totalCollected,
7059
- status: await this.getBondStateSessionStatus(bondState)
7082
+ status: await this.getBondStateSessionStatus(state)
7060
7083
  };
7061
7084
  return bondStateStats;
7062
7085
  }