@jpool/bond-sdk 0.9.0-next.12 → 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,7 +1,7 @@
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';
4
- import { PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL, StakeProgram } from '@solana/web3.js';
3
+ import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync, ACCOUNT_SIZE, AccountLayout } from '@solana/spl-token';
4
+ import { PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js';
5
5
  import bs58 from 'bs58';
6
6
 
7
7
  var __create = Object.create;
@@ -6653,8 +6653,6 @@ var JBondClient = class _JBondClient {
6653
6653
  token: (mint) => {
6654
6654
  accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, props.reserve);
6655
6655
  accounts.mint = mint;
6656
- },
6657
- stakeAccount: () => {
6658
6656
  }
6659
6657
  });
6660
6658
  return this.program.methods.bondInitialize({
@@ -6703,9 +6701,6 @@ var JBondClient = class _JBondClient {
6703
6701
  token: (mint) => {
6704
6702
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6705
6703
  Object.assign(accounts, { mint, bondTokenAccount: accounts.bondTokenAccount });
6706
- },
6707
- stakeAccount: () => {
6708
- throw new Error("Stake account collateral type is not currently supported");
6709
6704
  }
6710
6705
  });
6711
6706
  return this.program.methods.bondRegister(bondType, name).accountsPartial(accounts).instruction();
@@ -6751,13 +6746,6 @@ var JBondClient = class _JBondClient {
6751
6746
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6752
6747
  accounts.tokenProgram = TOKEN_PROGRAM_ID;
6753
6748
  accounts.associatedTokenProgram = ASSOCIATED_TOKEN_PROGRAM_ID;
6754
- },
6755
- stakeAccount: () => {
6756
- if (!collateral.stakeAccount) {
6757
- throw new Error("Missing stakeAccount for stake collateral");
6758
- }
6759
- accounts.stakeAccount = collateral.stakeAccount;
6760
- accounts.stakeProgram = StakeProgram.programId;
6761
6749
  }
6762
6750
  });
6763
6751
  instructions.push(
@@ -6794,19 +6782,12 @@ var JBondClient = class _JBondClient {
6794
6782
  accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, destination, true);
6795
6783
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6796
6784
  accounts.tokenProgram = TOKEN_PROGRAM_ID;
6797
- },
6798
- stakeAccount: () => {
6799
- if (!withdraw.stakeAccount) {
6800
- throw new Error("Missing stakeAccount for stake collateral");
6801
- }
6802
- accounts.stakeAccount = withdraw.stakeAccount;
6803
- accounts.stakeProgram = StakeProgram.programId;
6804
6785
  }
6805
6786
  });
6806
6787
  return this.program.methods.bondWithdraw(this.lamports(withdraw.amount)).accountsPartial(accounts).instruction();
6807
6788
  }
6808
6789
  async buildClaimIx(props) {
6809
- const { claim, bondType, name } = props;
6790
+ const { bondType, name } = props;
6810
6791
  const [bondState] = this.pda.bondState(bondType, name);
6811
6792
  const [validatorBond] = this.pda.validatorBond(bondType, name, props.voteAccount);
6812
6793
  const reserve = props.reserve ?? (await this.getBondState(bondType, name)).reserve;
@@ -6835,13 +6816,6 @@ var JBondClient = class _JBondClient {
6835
6816
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6836
6817
  accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
6837
6818
  accounts.tokenProgram = TOKEN_PROGRAM_ID;
6838
- },
6839
- stakeAccount: () => {
6840
- if (!claim.stakeAccount) {
6841
- throw new Error("Missing stakeAccount for stake collateral");
6842
- }
6843
- accounts.stakeAccount = claim.stakeAccount;
6844
- accounts.stakeProgram = StakeProgram.programId;
6845
6819
  }
6846
6820
  });
6847
6821
  return this.program.methods.bondClaim(this.lamports(props.claim.amount)).accountsPartial(
@@ -6898,23 +6872,17 @@ var JBondClient = class _JBondClient {
6898
6872
  /**
6899
6873
  * Get all bond states with total collected collateral
6900
6874
  */
6901
- async getAllBondStates(bondType) {
6875
+ async getAllBondStates(bondType, session_status) {
6902
6876
  const bondStates = [];
6903
6877
  const bondStateAccounts = await this.program.account.bondState.all();
6904
6878
  for (const { account: state } of bondStateAccounts) {
6905
6879
  if (!sameVariant(state.bondType, bondType)) {
6906
6880
  continue;
6907
6881
  }
6908
- const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
6909
- const balances = await Promise.all(
6910
- validatorBonds.map(
6911
- (vb) => this.getValidatorBondBalance(state.bondType, state.name, vb.voteAccount).then((v) => v ?? 0)
6912
- )
6913
- );
6914
- const bondStateStats = {
6915
- totalCollected: balances.reduce((sum, v) => sum + v, 0),
6916
- status: await this.getBondStateSessionStatus(state)
6917
- };
6882
+ if (session_status !== void 0 && this.getBondStateSessionStatus(state) !== session_status) {
6883
+ continue;
6884
+ }
6885
+ const bondStateStats = await this.getBondStateStats(state);
6918
6886
  bondStates.push([state, bondStateStats]);
6919
6887
  }
6920
6888
  return bondStates;
@@ -6922,7 +6890,7 @@ var JBondClient = class _JBondClient {
6922
6890
  /**
6923
6891
  * Get session status for bond state
6924
6892
  */
6925
- async getBondStateSessionStatus(bondState) {
6893
+ getBondStateSessionStatus(bondState) {
6926
6894
  const now = Math.floor(Date.now() / 1e3);
6927
6895
  if (bondState.sessionFinishTs.toNumber() > now) {
6928
6896
  return 0 /* Live */;
@@ -6997,18 +6965,84 @@ var JBondClient = class _JBondClient {
6997
6965
  } catch {
6998
6966
  return 0;
6999
6967
  }
6968
+ }
6969
+ });
6970
+ }
6971
+ // Get total collected collateral for a bond state
6972
+ async getBondStateTotalCollected(bondType, bondName, votes) {
6973
+ const bondStateData = await this.getBondState(bondType, bondName);
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
+ };
6982
+ return await matchVariant(bondStateCollateralType, {
6983
+ native: async () => {
6984
+ const addresses = votes.map((vote) => {
6985
+ const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6986
+ return address;
6987
+ });
6988
+ const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
6989
+ const balances = await Promise.all(
6990
+ accountInfos.map(async (accountInfo) => {
6991
+ if (!accountInfo) {
6992
+ return 0;
6993
+ }
6994
+ const { lamports } = accountInfo;
6995
+ return lamports;
6996
+ })
6997
+ );
6998
+ return balances.reduce((sum, v) => sum + v, 0);
7000
6999
  },
7001
- stakeAccount: async () => {
7002
- const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
7003
- if (!accountInfo) {
7000
+ // TODO
7001
+ token: async (mint) => {
7002
+ const tokenAccounts = votes.map((vote) => {
7003
+ const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
7004
+ return getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
7005
+ });
7006
+ if (tokenAccounts.length === 0) {
7004
7007
  return 0;
7005
7008
  }
7006
- const { data, lamports } = accountInfo;
7007
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
7008
- return Math.max(0, lamports - rentExempt);
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;
7019
+ }
7020
+ try {
7021
+ const acc = AccountLayout.decode(info.data);
7022
+ const raw = BigInt(acc.amount.toString());
7023
+ total += raw;
7024
+ } catch {
7025
+ }
7026
+ }
7027
+ }
7028
+ const asNumber = Number(total);
7029
+ return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7009
7030
  }
7010
7031
  });
7011
7032
  }
7033
+ async getBondStateStats(state) {
7034
+ const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
7035
+ const totalCollected = await this.getBondStateTotalCollected(
7036
+ state.bondType,
7037
+ state.name,
7038
+ validatorBonds.map((vb) => vb.voteAccount)
7039
+ );
7040
+ const bondStateStats = {
7041
+ totalCollected,
7042
+ status: await this.getBondStateSessionStatus(state)
7043
+ };
7044
+ return bondStateStats;
7045
+ }
7012
7046
  /**
7013
7047
  * Load stake pool account and cache it if not already cached
7014
7048
  * @private