@jpool/bond-sdk 0.9.0-next.11 → 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;
@@ -4803,7 +4803,7 @@ var HistoryManager = class {
4803
4803
 
4804
4804
  // src/idl/jbond.json
4805
4805
  var jbond_default = {
4806
- address: "DBuUGWbLz8CkAMQiagMfrQzj8SSg1rHQJWrTSc97HWfR",
4806
+ address: "93BH9J99hQS3YinydR6SVjHuAEECoA9kR1BHwnXeDzQe",
4807
4807
  metadata: {
4808
4808
  name: "jbond",
4809
4809
  version: "0.2.1",
@@ -4959,6 +4959,68 @@ var jbond_default = {
4959
4959
  }
4960
4960
  ]
4961
4961
  },
4962
+ {
4963
+ name: "bond_disable",
4964
+ docs: [
4965
+ "Disables a validator's bond, preventing it from participating further."
4966
+ ],
4967
+ discriminator: [
4968
+ 159,
4969
+ 156,
4970
+ 97,
4971
+ 253,
4972
+ 70,
4973
+ 122,
4974
+ 117,
4975
+ 129
4976
+ ],
4977
+ accounts: [
4978
+ {
4979
+ name: "bond_state",
4980
+ writable: true
4981
+ },
4982
+ {
4983
+ name: "authority",
4984
+ writable: true,
4985
+ signer: true,
4986
+ relations: [
4987
+ "bond_state"
4988
+ ]
4989
+ }
4990
+ ],
4991
+ args: []
4992
+ },
4993
+ {
4994
+ name: "bond_enable",
4995
+ docs: [
4996
+ "Enables a previously disabled validator's bond."
4997
+ ],
4998
+ discriminator: [
4999
+ 133,
5000
+ 48,
5001
+ 84,
5002
+ 9,
5003
+ 117,
5004
+ 131,
5005
+ 164,
5006
+ 128
5007
+ ],
5008
+ accounts: [
5009
+ {
5010
+ name: "bond_state",
5011
+ writable: true
5012
+ },
5013
+ {
5014
+ name: "authority",
5015
+ writable: true,
5016
+ signer: true,
5017
+ relations: [
5018
+ "bond_state"
5019
+ ]
5020
+ }
5021
+ ],
5022
+ args: []
5023
+ },
4962
5024
  {
4963
5025
  name: "bond_initialize",
4964
5026
  docs: [
@@ -5866,6 +5928,16 @@ var jbond_default = {
5866
5928
  code: 6021,
5867
5929
  name: "InvalidArgument",
5868
5930
  msg: "Invalid argument provided"
5931
+ },
5932
+ {
5933
+ code: 6022,
5934
+ name: "BondNotDisabled",
5935
+ msg: "Bond is not disabled"
5936
+ },
5937
+ {
5938
+ code: 6023,
5939
+ name: "BondNotEnabled",
5940
+ msg: "Bond is not enabled"
5869
5941
  }
5870
5942
  ],
5871
5943
  types: [
@@ -6044,6 +6116,14 @@ var jbond_default = {
6044
6116
  ],
6045
6117
  type: "i64"
6046
6118
  },
6119
+ {
6120
+ name: "status",
6121
+ type: {
6122
+ defined: {
6123
+ name: "BondStatus"
6124
+ }
6125
+ }
6126
+ },
6047
6127
  {
6048
6128
  name: "bump",
6049
6129
  docs: [
@@ -6054,6 +6134,20 @@ var jbond_default = {
6054
6134
  ]
6055
6135
  }
6056
6136
  },
6137
+ {
6138
+ name: "BondStatus",
6139
+ type: {
6140
+ kind: "enum",
6141
+ variants: [
6142
+ {
6143
+ name: "Active"
6144
+ },
6145
+ {
6146
+ name: "Disabled"
6147
+ }
6148
+ ]
6149
+ }
6150
+ },
6057
6151
  {
6058
6152
  name: "BondType",
6059
6153
  docs: [
@@ -6189,15 +6283,6 @@ var jbond_default = {
6189
6283
  type: {
6190
6284
  kind: "struct",
6191
6285
  fields: [
6192
- {
6193
- name: "bonds",
6194
- docs: [
6195
- "List of all registered bonds"
6196
- ],
6197
- type: {
6198
- vec: "pubkey"
6199
- }
6200
- },
6201
6286
  {
6202
6287
  name: "authority",
6203
6288
  docs: [
@@ -6221,7 +6306,7 @@ var jbond_default = {
6221
6306
  kind: "struct",
6222
6307
  fields: [
6223
6308
  {
6224
- name: "bond",
6309
+ name: "state",
6225
6310
  type: "pubkey"
6226
6311
  },
6227
6312
  {
@@ -6242,26 +6327,10 @@ var jbond_default = {
6242
6327
  option: "pubkey"
6243
6328
  }
6244
6329
  },
6245
- {
6246
- name: "total_compensation_amount",
6247
- type: "u64"
6248
- },
6249
- {
6250
- name: "last_compensation_amount",
6251
- type: "u64"
6252
- },
6253
- {
6254
- name: "last_compensation_epoch",
6255
- type: "u64"
6256
- },
6257
6330
  {
6258
6331
  name: "created_at",
6259
6332
  type: "i64"
6260
6333
  },
6261
- {
6262
- name: "is_active",
6263
- type: "bool"
6264
- },
6265
6334
  {
6266
6335
  name: "bump",
6267
6336
  type: "u8"
@@ -6533,6 +6602,14 @@ var JBondClient = class _JBondClient {
6533
6602
  const ix = await this.buildBondFinishIx(props);
6534
6603
  return this.provider.sendAndConfirm?.(new Transaction().add(ix));
6535
6604
  }
6605
+ async bondEnable(props) {
6606
+ const ix = await this.buildBondEnableIx(props);
6607
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
6608
+ }
6609
+ async bondDisable(props) {
6610
+ const ix = await this.buildBondDisableIx(props);
6611
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
6612
+ }
6536
6613
  async buildBondGlobalStateInitializeIx(props) {
6537
6614
  const [globalState] = this.pda.globalState();
6538
6615
  const authority = props.authority ?? this.provider.wallet?.publicKey;
@@ -6576,8 +6653,6 @@ var JBondClient = class _JBondClient {
6576
6653
  token: (mint) => {
6577
6654
  accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, props.reserve);
6578
6655
  accounts.mint = mint;
6579
- },
6580
- stakeAccount: () => {
6581
6656
  }
6582
6657
  });
6583
6658
  return this.program.methods.bondInitialize({
@@ -6626,9 +6701,6 @@ var JBondClient = class _JBondClient {
6626
6701
  token: (mint) => {
6627
6702
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6628
6703
  Object.assign(accounts, { mint, bondTokenAccount: accounts.bondTokenAccount });
6629
- },
6630
- stakeAccount: () => {
6631
- throw new Error("Stake account collateral type is not currently supported");
6632
6704
  }
6633
6705
  });
6634
6706
  return this.program.methods.bondRegister(bondType, name).accountsPartial(accounts).instruction();
@@ -6674,13 +6746,6 @@ var JBondClient = class _JBondClient {
6674
6746
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6675
6747
  accounts.tokenProgram = TOKEN_PROGRAM_ID;
6676
6748
  accounts.associatedTokenProgram = ASSOCIATED_TOKEN_PROGRAM_ID;
6677
- },
6678
- stakeAccount: () => {
6679
- if (!collateral.stakeAccount) {
6680
- throw new Error("Missing stakeAccount for stake collateral");
6681
- }
6682
- accounts.stakeAccount = collateral.stakeAccount;
6683
- accounts.stakeProgram = StakeProgram.programId;
6684
6749
  }
6685
6750
  });
6686
6751
  instructions.push(
@@ -6717,19 +6782,12 @@ var JBondClient = class _JBondClient {
6717
6782
  accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, destination, true);
6718
6783
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6719
6784
  accounts.tokenProgram = TOKEN_PROGRAM_ID;
6720
- },
6721
- stakeAccount: () => {
6722
- if (!withdraw.stakeAccount) {
6723
- throw new Error("Missing stakeAccount for stake collateral");
6724
- }
6725
- accounts.stakeAccount = withdraw.stakeAccount;
6726
- accounts.stakeProgram = StakeProgram.programId;
6727
6785
  }
6728
6786
  });
6729
6787
  return this.program.methods.bondWithdraw(this.lamports(withdraw.amount)).accountsPartial(accounts).instruction();
6730
6788
  }
6731
6789
  async buildClaimIx(props) {
6732
- const { claim, bondType, name } = props;
6790
+ const { bondType, name } = props;
6733
6791
  const [bondState] = this.pda.bondState(bondType, name);
6734
6792
  const [validatorBond] = this.pda.validatorBond(bondType, name, props.voteAccount);
6735
6793
  const reserve = props.reserve ?? (await this.getBondState(bondType, name)).reserve;
@@ -6758,13 +6816,6 @@ var JBondClient = class _JBondClient {
6758
6816
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
6759
6817
  accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
6760
6818
  accounts.tokenProgram = TOKEN_PROGRAM_ID;
6761
- },
6762
- stakeAccount: () => {
6763
- if (!claim.stakeAccount) {
6764
- throw new Error("Missing stakeAccount for stake collateral");
6765
- }
6766
- accounts.stakeAccount = claim.stakeAccount;
6767
- accounts.stakeProgram = StakeProgram.programId;
6768
6819
  }
6769
6820
  });
6770
6821
  return this.program.methods.bondClaim(this.lamports(props.claim.amount)).accountsPartial(
@@ -6793,6 +6844,20 @@ var JBondClient = class _JBondClient {
6793
6844
  authority: this.program.provider.wallet?.publicKey
6794
6845
  }).instruction();
6795
6846
  }
6847
+ async buildBondEnableIx(props) {
6848
+ const [bondState] = this.pda.bondState(props.bondType, props.name);
6849
+ return this.program.methods.bondEnable().accountsPartial({
6850
+ bondState,
6851
+ authority: this.program.provider.wallet?.publicKey
6852
+ }).instruction();
6853
+ }
6854
+ async buildBondDisableIx(props) {
6855
+ const [bondState] = this.pda.bondState(props.bondType, props.name);
6856
+ return this.program.methods.bondDisable().accountsPartial({
6857
+ bondState,
6858
+ authority: this.program.provider.wallet?.publicKey
6859
+ }).instruction();
6860
+ }
6796
6861
  /**
6797
6862
  * Fetch global state or throw if not found
6798
6863
  */
@@ -6814,16 +6879,7 @@ var JBondClient = class _JBondClient {
6814
6879
  if (!sameVariant(state.bondType, bondType)) {
6815
6880
  continue;
6816
6881
  }
6817
- const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
6818
- const balances = await Promise.all(
6819
- validatorBonds.map(
6820
- (vb) => this.getValidatorBondBalance(state.bondType, state.name, vb.voteAccount).then((v) => v ?? 0)
6821
- )
6822
- );
6823
- const bondStateStats = {
6824
- totalCollected: balances.reduce((sum, v) => sum + v, 0),
6825
- status: await this.getBondStateSessionStatus(state)
6826
- };
6882
+ const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6827
6883
  bondStates.push([state, bondStateStats]);
6828
6884
  }
6829
6885
  return bondStates;
@@ -6906,18 +6962,68 @@ var JBondClient = class _JBondClient {
6906
6962
  } catch {
6907
6963
  return 0;
6908
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);
6909
6989
  },
6910
- stakeAccount: async () => {
6911
- const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
6912
- if (!accountInfo) {
6913
- return 0;
6914
- }
6915
- const { data, lamports } = accountInfo;
6916
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6917
- 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);
6918
7010
  }
6919
7011
  });
6920
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
+ }
6921
7027
  /**
6922
7028
  * Load stake pool account and cache it if not already cached
6923
7029
  * @private