@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.js CHANGED
@@ -4838,7 +4838,7 @@ var HistoryManager = class {
4838
4838
 
4839
4839
  // src/idl/jbond.json
4840
4840
  var jbond_default = {
4841
- address: "DBuUGWbLz8CkAMQiagMfrQzj8SSg1rHQJWrTSc97HWfR",
4841
+ address: "93BH9J99hQS3YinydR6SVjHuAEECoA9kR1BHwnXeDzQe",
4842
4842
  metadata: {
4843
4843
  name: "jbond",
4844
4844
  version: "0.2.1",
@@ -4994,6 +4994,68 @@ var jbond_default = {
4994
4994
  }
4995
4995
  ]
4996
4996
  },
4997
+ {
4998
+ name: "bond_disable",
4999
+ docs: [
5000
+ "Disables a validator's bond, preventing it from participating further."
5001
+ ],
5002
+ discriminator: [
5003
+ 159,
5004
+ 156,
5005
+ 97,
5006
+ 253,
5007
+ 70,
5008
+ 122,
5009
+ 117,
5010
+ 129
5011
+ ],
5012
+ accounts: [
5013
+ {
5014
+ name: "bond_state",
5015
+ writable: true
5016
+ },
5017
+ {
5018
+ name: "authority",
5019
+ writable: true,
5020
+ signer: true,
5021
+ relations: [
5022
+ "bond_state"
5023
+ ]
5024
+ }
5025
+ ],
5026
+ args: []
5027
+ },
5028
+ {
5029
+ name: "bond_enable",
5030
+ docs: [
5031
+ "Enables a previously disabled validator's bond."
5032
+ ],
5033
+ discriminator: [
5034
+ 133,
5035
+ 48,
5036
+ 84,
5037
+ 9,
5038
+ 117,
5039
+ 131,
5040
+ 164,
5041
+ 128
5042
+ ],
5043
+ accounts: [
5044
+ {
5045
+ name: "bond_state",
5046
+ writable: true
5047
+ },
5048
+ {
5049
+ name: "authority",
5050
+ writable: true,
5051
+ signer: true,
5052
+ relations: [
5053
+ "bond_state"
5054
+ ]
5055
+ }
5056
+ ],
5057
+ args: []
5058
+ },
4997
5059
  {
4998
5060
  name: "bond_initialize",
4999
5061
  docs: [
@@ -5901,6 +5963,16 @@ var jbond_default = {
5901
5963
  code: 6021,
5902
5964
  name: "InvalidArgument",
5903
5965
  msg: "Invalid argument provided"
5966
+ },
5967
+ {
5968
+ code: 6022,
5969
+ name: "BondNotDisabled",
5970
+ msg: "Bond is not disabled"
5971
+ },
5972
+ {
5973
+ code: 6023,
5974
+ name: "BondNotEnabled",
5975
+ msg: "Bond is not enabled"
5904
5976
  }
5905
5977
  ],
5906
5978
  types: [
@@ -6079,6 +6151,14 @@ var jbond_default = {
6079
6151
  ],
6080
6152
  type: "i64"
6081
6153
  },
6154
+ {
6155
+ name: "status",
6156
+ type: {
6157
+ defined: {
6158
+ name: "BondStatus"
6159
+ }
6160
+ }
6161
+ },
6082
6162
  {
6083
6163
  name: "bump",
6084
6164
  docs: [
@@ -6089,6 +6169,20 @@ var jbond_default = {
6089
6169
  ]
6090
6170
  }
6091
6171
  },
6172
+ {
6173
+ name: "BondStatus",
6174
+ type: {
6175
+ kind: "enum",
6176
+ variants: [
6177
+ {
6178
+ name: "Active"
6179
+ },
6180
+ {
6181
+ name: "Disabled"
6182
+ }
6183
+ ]
6184
+ }
6185
+ },
6092
6186
  {
6093
6187
  name: "BondType",
6094
6188
  docs: [
@@ -6224,15 +6318,6 @@ var jbond_default = {
6224
6318
  type: {
6225
6319
  kind: "struct",
6226
6320
  fields: [
6227
- {
6228
- name: "bonds",
6229
- docs: [
6230
- "List of all registered bonds"
6231
- ],
6232
- type: {
6233
- vec: "pubkey"
6234
- }
6235
- },
6236
6321
  {
6237
6322
  name: "authority",
6238
6323
  docs: [
@@ -6256,7 +6341,7 @@ var jbond_default = {
6256
6341
  kind: "struct",
6257
6342
  fields: [
6258
6343
  {
6259
- name: "bond",
6344
+ name: "state",
6260
6345
  type: "pubkey"
6261
6346
  },
6262
6347
  {
@@ -6277,26 +6362,10 @@ var jbond_default = {
6277
6362
  option: "pubkey"
6278
6363
  }
6279
6364
  },
6280
- {
6281
- name: "total_compensation_amount",
6282
- type: "u64"
6283
- },
6284
- {
6285
- name: "last_compensation_amount",
6286
- type: "u64"
6287
- },
6288
- {
6289
- name: "last_compensation_epoch",
6290
- type: "u64"
6291
- },
6292
6365
  {
6293
6366
  name: "created_at",
6294
6367
  type: "i64"
6295
6368
  },
6296
- {
6297
- name: "is_active",
6298
- type: "bool"
6299
- },
6300
6369
  {
6301
6370
  name: "bump",
6302
6371
  type: "u8"
@@ -6569,6 +6638,14 @@ var JBondClient = class _JBondClient {
6569
6638
  const ix = await this.buildBondFinishIx(props);
6570
6639
  return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
6571
6640
  }
6641
+ async bondEnable(props) {
6642
+ const ix = await this.buildBondEnableIx(props);
6643
+ return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
6644
+ }
6645
+ async bondDisable(props) {
6646
+ const ix = await this.buildBondDisableIx(props);
6647
+ return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
6648
+ }
6572
6649
  async buildBondGlobalStateInitializeIx(props) {
6573
6650
  const [globalState] = this.pda.globalState();
6574
6651
  const authority = props.authority ?? this.provider.wallet?.publicKey;
@@ -6612,8 +6689,6 @@ var JBondClient = class _JBondClient {
6612
6689
  token: (mint) => {
6613
6690
  accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, props.reserve);
6614
6691
  accounts.mint = mint;
6615
- },
6616
- stakeAccount: () => {
6617
6692
  }
6618
6693
  });
6619
6694
  return this.program.methods.bondInitialize({
@@ -6662,9 +6737,6 @@ var JBondClient = class _JBondClient {
6662
6737
  token: (mint) => {
6663
6738
  accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6664
6739
  Object.assign(accounts, { mint, bondTokenAccount: accounts.bondTokenAccount });
6665
- },
6666
- stakeAccount: () => {
6667
- throw new Error("Stake account collateral type is not currently supported");
6668
6740
  }
6669
6741
  });
6670
6742
  return this.program.methods.bondRegister(bondType, name).accountsPartial(accounts).instruction();
@@ -6710,13 +6782,6 @@ var JBondClient = class _JBondClient {
6710
6782
  accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6711
6783
  accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6712
6784
  accounts.associatedTokenProgram = splToken.ASSOCIATED_TOKEN_PROGRAM_ID;
6713
- },
6714
- stakeAccount: () => {
6715
- if (!collateral.stakeAccount) {
6716
- throw new Error("Missing stakeAccount for stake collateral");
6717
- }
6718
- accounts.stakeAccount = collateral.stakeAccount;
6719
- accounts.stakeProgram = web3_js.StakeProgram.programId;
6720
6785
  }
6721
6786
  });
6722
6787
  instructions.push(
@@ -6753,19 +6818,12 @@ var JBondClient = class _JBondClient {
6753
6818
  accounts.destinationTokenAccount = splToken.getAssociatedTokenAddressSync(mint, destination, true);
6754
6819
  accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6755
6820
  accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6756
- },
6757
- stakeAccount: () => {
6758
- if (!withdraw.stakeAccount) {
6759
- throw new Error("Missing stakeAccount for stake collateral");
6760
- }
6761
- accounts.stakeAccount = withdraw.stakeAccount;
6762
- accounts.stakeProgram = web3_js.StakeProgram.programId;
6763
6821
  }
6764
6822
  });
6765
6823
  return this.program.methods.bondWithdraw(this.lamports(withdraw.amount)).accountsPartial(accounts).instruction();
6766
6824
  }
6767
6825
  async buildClaimIx(props) {
6768
- const { claim, bondType, name } = props;
6826
+ const { bondType, name } = props;
6769
6827
  const [bondState] = this.pda.bondState(bondType, name);
6770
6828
  const [validatorBond] = this.pda.validatorBond(bondType, name, props.voteAccount);
6771
6829
  const reserve = props.reserve ?? (await this.getBondState(bondType, name)).reserve;
@@ -6794,13 +6852,6 @@ var JBondClient = class _JBondClient {
6794
6852
  accounts.bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
6795
6853
  accounts.reserveTokenAccount = splToken.getAssociatedTokenAddressSync(mint, reserve, true);
6796
6854
  accounts.tokenProgram = splToken.TOKEN_PROGRAM_ID;
6797
- },
6798
- stakeAccount: () => {
6799
- if (!claim.stakeAccount) {
6800
- throw new Error("Missing stakeAccount for stake collateral");
6801
- }
6802
- accounts.stakeAccount = claim.stakeAccount;
6803
- accounts.stakeProgram = web3_js.StakeProgram.programId;
6804
6855
  }
6805
6856
  });
6806
6857
  return this.program.methods.bondClaim(this.lamports(props.claim.amount)).accountsPartial(
@@ -6829,6 +6880,20 @@ var JBondClient = class _JBondClient {
6829
6880
  authority: this.program.provider.wallet?.publicKey
6830
6881
  }).instruction();
6831
6882
  }
6883
+ async buildBondEnableIx(props) {
6884
+ const [bondState] = this.pda.bondState(props.bondType, props.name);
6885
+ return this.program.methods.bondEnable().accountsPartial({
6886
+ bondState,
6887
+ authority: this.program.provider.wallet?.publicKey
6888
+ }).instruction();
6889
+ }
6890
+ async buildBondDisableIx(props) {
6891
+ const [bondState] = this.pda.bondState(props.bondType, props.name);
6892
+ return this.program.methods.bondDisable().accountsPartial({
6893
+ bondState,
6894
+ authority: this.program.provider.wallet?.publicKey
6895
+ }).instruction();
6896
+ }
6832
6897
  /**
6833
6898
  * Fetch global state or throw if not found
6834
6899
  */
@@ -6850,16 +6915,7 @@ var JBondClient = class _JBondClient {
6850
6915
  if (!sameVariant(state.bondType, bondType)) {
6851
6916
  continue;
6852
6917
  }
6853
- const validatorBonds = await this.getValidatorBondsByState(state.bondType, state.name);
6854
- const balances = await Promise.all(
6855
- validatorBonds.map(
6856
- (vb) => this.getValidatorBondBalance(state.bondType, state.name, vb.voteAccount).then((v) => v ?? 0)
6857
- )
6858
- );
6859
- const bondStateStats = {
6860
- totalCollected: balances.reduce((sum, v) => sum + v, 0),
6861
- status: await this.getBondStateSessionStatus(state)
6862
- };
6918
+ const bondStateStats = await this.getBondStateStats(state.bondType, state.name);
6863
6919
  bondStates.push([state, bondStateStats]);
6864
6920
  }
6865
6921
  return bondStates;
@@ -6942,18 +6998,68 @@ var JBondClient = class _JBondClient {
6942
6998
  } catch {
6943
6999
  return 0;
6944
7000
  }
7001
+ }
7002
+ });
7003
+ }
7004
+ // Get total collected collateral for a bond state
7005
+ async getBondStateTotalCollected(bondType, bondName, votes) {
7006
+ const bondStateData = await this.getBondState(bondType, bondName);
7007
+ const bondStateCollateralType = bondStateData.collateralType;
7008
+ return await matchVariant(bondStateCollateralType, {
7009
+ native: async () => {
7010
+ const addresses = votes.map((vote) => {
7011
+ const [address] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
7012
+ return address;
7013
+ });
7014
+ const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
7015
+ const balances = await Promise.all(
7016
+ accountInfos.map(async (accountInfo) => {
7017
+ if (!accountInfo) {
7018
+ return 0;
7019
+ }
7020
+ const { lamports } = accountInfo;
7021
+ return lamports;
7022
+ })
7023
+ );
7024
+ return balances.reduce((sum, v) => sum + v, 0);
6945
7025
  },
6946
- stakeAccount: async () => {
6947
- const accountInfo = await this.connection.getAccountInfo(address).catch(() => null);
6948
- if (!accountInfo) {
6949
- return 0;
6950
- }
6951
- const { data, lamports } = accountInfo;
6952
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6953
- return Math.max(0, lamports - rentExempt);
7026
+ token: async (mint) => {
7027
+ const tokenAccounts = votes.map((vote) => {
7028
+ const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
7029
+ return splToken.getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
7030
+ });
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;
7036
+ }
7037
+ try {
7038
+ const balance = await this.connection.getTokenAccountBalance(tokenAccounts[accountInfos.indexOf(accountInfo)]);
7039
+ return Number(balance.value.uiAmount ?? 0) * web3_js.LAMPORTS_PER_SOL;
7040
+ } catch {
7041
+ return 0;
7042
+ }
7043
+ })
7044
+ );
7045
+ return balances.reduce((sum, v) => sum + v, 0);
6954
7046
  }
6955
7047
  });
6956
7048
  }
7049
+ async getBondStateStats(bondType, bondName) {
7050
+ const bondState = await this.getBondState(bondType, bondName);
7051
+ const validatorBonds = await this.getValidatorBondsByState(bondState.bondType, bondState.name);
7052
+ const totalCollected = await this.getBondStateTotalCollected(
7053
+ bondType,
7054
+ bondName,
7055
+ validatorBonds.map((vb) => vb.voteAccount)
7056
+ );
7057
+ const bondStateStats = {
7058
+ totalCollected,
7059
+ status: await this.getBondStateSessionStatus(bondState)
7060
+ };
7061
+ return bondStateStats;
7062
+ }
6957
7063
  /**
6958
7064
  * Load stake pool account and cache it if not already cached
6959
7065
  * @private