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

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
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';
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(
@@ -6905,16 +6879,7 @@ var JBondClient = class _JBondClient {
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
+ const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6918
6883
  bondStates.push([state, bondStateStats]);
6919
6884
  }
6920
6885
  return bondStates;
@@ -6997,18 +6962,68 @@ var JBondClient = class _JBondClient {
6997
6962
  } catch {
6998
6963
  return 0;
6999
6964
  }
6965
+ }
6966
+ });
6967
+ }
6968
+ // Get total collected collateral for a bond state
6969
+ async getBondStateTotalCollected(bondType, bondName, votes) {
6970
+ const bondStateData = await this.getBondState(bondType, bondName);
6971
+ const bondStateCollateralType = bondStateData.collateralType;
6972
+ return await matchVariant(bondStateCollateralType, {
6973
+ native: async () => {
6974
+ const addresses = votes.map((vote) => {
6975
+ const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6976
+ return address;
6977
+ });
6978
+ const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
6979
+ const balances = await Promise.all(
6980
+ accountInfos.map(async (accountInfo) => {
6981
+ if (!accountInfo) {
6982
+ return 0;
6983
+ }
6984
+ const { lamports } = accountInfo;
6985
+ return lamports;
6986
+ })
6987
+ );
6988
+ return balances.reduce((sum, v) => sum + v, 0);
7000
6989
  },
7001
- stakeAccount: async () => {
7002
- const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
7003
- if (!accountInfo) {
7004
- return 0;
7005
- }
7006
- const { data, lamports } = accountInfo;
7007
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
7008
- return Math.max(0, lamports - rentExempt);
6990
+ token: async (mint) => {
6991
+ const tokenAccounts = votes.map((vote) => {
6992
+ const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6993
+ return getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
6994
+ });
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;
7000
+ }
7001
+ try {
7002
+ const balance = await this.connection.getTokenAccountBalance(tokenAccounts[accountInfos.indexOf(accountInfo)]);
7003
+ return Number(balance.value.uiAmount ?? 0) * LAMPORTS_PER_SOL;
7004
+ } catch {
7005
+ return 0;
7006
+ }
7007
+ })
7008
+ );
7009
+ return balances.reduce((sum, v) => sum + v, 0);
7009
7010
  }
7010
7011
  });
7011
7012
  }
7013
+ async getBondStateStats(bondType, bondName) {
7014
+ const bondState = await this.getBondState(bondType, bondName);
7015
+ const validatorBonds = await this.getValidatorBondsByState(bondState.bondType, bondState.name);
7016
+ const totalCollected = await this.getBondStateTotalCollected(
7017
+ bondType,
7018
+ bondName,
7019
+ validatorBonds.map((vb) => vb.voteAccount)
7020
+ );
7021
+ const bondStateStats = {
7022
+ totalCollected,
7023
+ status: await this.getBondStateSessionStatus(bondState)
7024
+ };
7025
+ return bondStateStats;
7026
+ }
7012
7027
  /**
7013
7028
  * Load stake pool account and cache it if not already cached
7014
7029
  * @private