@jpool/bond-sdk 0.10.5 → 0.11.0-next.3

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
@@ -4759,7 +4759,7 @@ function slotToEpoch(slot, cluster) {
4759
4759
 
4760
4760
  // src/idl/jbond.json
4761
4761
  var jbond_default = {
4762
- address: "BondQ7KqZreTcW2UbeTNDcLCJQ3aXAtLn2Fm6ftaJDU",
4762
+ address: "Fo17edWRJewZNTRibgx9iTfjywCW6dzS81VwBLmPFVq1",
4763
4763
  metadata: {
4764
4764
  name: "jbond",
4765
4765
  version: "0.2.1",
@@ -6281,32 +6281,54 @@ var jbond_default = {
6281
6281
  fields: [
6282
6282
  {
6283
6283
  name: "state",
6284
+ docs: [
6285
+ "The bond state address"
6286
+ ],
6284
6287
  type: "pubkey"
6285
6288
  },
6286
6289
  {
6287
6290
  name: "identity",
6291
+ docs: [
6292
+ "Validator identity pubkey"
6293
+ ],
6288
6294
  type: "pubkey"
6289
6295
  },
6290
6296
  {
6291
6297
  name: "vote_account",
6298
+ docs: [
6299
+ "Validator vote account pubkey"
6300
+ ],
6292
6301
  type: "pubkey"
6293
6302
  },
6294
6303
  {
6295
6304
  name: "creator",
6305
+ docs: [
6306
+ "The bond creator pubkey"
6307
+ ],
6296
6308
  type: "pubkey"
6297
6309
  },
6298
6310
  {
6299
6311
  name: "withdrawal_authority",
6312
+ docs: [
6313
+ "Optional withdrawal authority pubkey (if set, can withdraw on behalf of the validator,",
6314
+ "otherwise only identity can withdraw)"
6315
+ ],
6300
6316
  type: {
6301
6317
  option: "pubkey"
6302
6318
  }
6303
6319
  },
6304
6320
  {
6305
6321
  name: "created_at",
6322
+ docs: [
6323
+ "Bond creation timestamp"
6324
+ ],
6306
6325
  type: "i64"
6307
6326
  },
6308
6327
  {
6309
6328
  name: "bump",
6329
+ docs: [
6330
+ "Bump for the PDA"
6331
+ ],
6310
6332
  type: "u8"
6311
6333
  }
6312
6334
  ]
@@ -6429,6 +6451,20 @@ var NodeWallet = class {
6429
6451
  // src/utils/web3.ts
6430
6452
  init_cjs_shims();
6431
6453
  var SOL_DECIMALS = Math.log10(web3_js.LAMPORTS_PER_SOL);
6454
+ function lamportsToSol(lamports) {
6455
+ if (typeof lamports === "number") {
6456
+ return Math.abs(lamports) / web3_js.LAMPORTS_PER_SOL;
6457
+ }
6458
+ let signMultiplier = 1;
6459
+ if (lamports.isNeg()) {
6460
+ signMultiplier = -1;
6461
+ }
6462
+ const absLamports = lamports.abs();
6463
+ const lamportsString = absLamports.toString(10).padStart(10, "0");
6464
+ const splitIndex = lamportsString.length - SOL_DECIMALS;
6465
+ const solString = `${lamportsString.slice(0, splitIndex)}.${lamportsString.slice(splitIndex)}`;
6466
+ return signMultiplier * Number.parseFloat(solString);
6467
+ }
6432
6468
  function solToLamports(amount) {
6433
6469
  if (Number.isNaN(amount)) {
6434
6470
  return new anchor.BN(0);
@@ -6449,12 +6485,12 @@ function getBondTypeSeed(type) {
6449
6485
  }
6450
6486
 
6451
6487
  // src/client.ts
6452
- var BondClientEnv = /* @__PURE__ */ ((BondClientEnv2) => {
6453
- BondClientEnv2["DEV"] = "dev";
6454
- BondClientEnv2["STAGE"] = "stage";
6455
- BondClientEnv2["PROD"] = "prod";
6456
- return BondClientEnv2;
6457
- })(BondClientEnv || {});
6488
+ var JBondClientEnv = /* @__PURE__ */ ((JBondClientEnv2) => {
6489
+ JBondClientEnv2["DEV"] = "dev";
6490
+ JBondClientEnv2["STAGE"] = "stage";
6491
+ JBondClientEnv2["PROD"] = "prod";
6492
+ return JBondClientEnv2;
6493
+ })(JBondClientEnv || {});
6458
6494
  var JBondClient = class _JBondClient {
6459
6495
  constructor(provider, options) {
6460
6496
  this.provider = provider;
@@ -6466,6 +6502,12 @@ var JBondClient = class _JBondClient {
6466
6502
  get history() {
6467
6503
  return new HistoryManager(this);
6468
6504
  }
6505
+ /**
6506
+ * Creates a local instance of `JBondClient` for development and testing.
6507
+ */
6508
+ static local(options) {
6509
+ return new _JBondClient(anchor.AnchorProvider.local(), options);
6510
+ }
6469
6511
  /**
6470
6512
  * Creates an instance of `JBondClient` using a provided connection and wallet.
6471
6513
  */
@@ -6577,7 +6619,7 @@ var JBondClient = class _JBondClient {
6577
6619
  return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
6578
6620
  }
6579
6621
  /**
6580
- * Claim compensation
6622
+ * Claim compensation for a validator
6581
6623
  */
6582
6624
  async claimCompensation(props) {
6583
6625
  const ix = await this.getClaimIx(props);
@@ -6838,6 +6880,38 @@ var JBondClient = class _JBondClient {
6838
6880
  });
6839
6881
  return this.program.methods.bondClaim(solToLamports(amount)).accountsPartial(accounts).instruction();
6840
6882
  }
6883
+ /**
6884
+ * Build claim all compensations instructions
6885
+ */
6886
+ async getClaimAllCompensationsIxs(props) {
6887
+ const ixs = [];
6888
+ const validatorBonds = await this.getBondValidatorStates(props.bondType, props.name);
6889
+ const validatorBondBalances = await this.getBondStateValidatorBondBalances(
6890
+ props.bondType,
6891
+ props.name
6892
+ );
6893
+ for (const vb of validatorBonds) {
6894
+ console.log("Processing claim for validator:", vb.voteAccount.toBase58());
6895
+ const validatorBondBalance = lamportsToSol(validatorBondBalances.find((vbb) => vbb.voteAccount.equals(vb.voteAccount))?.balance || 0);
6896
+ const amountToClaim = validatorBondBalance - (props.collateralType ? 0 : lamportsToSol(await this.validatorBondRentExempt()));
6897
+ if (amountToClaim <= 0) {
6898
+ console.log("No balance to claim for validator:", vb.voteAccount.toBase58());
6899
+ continue;
6900
+ }
6901
+ console.log("Amount to claim:", amountToClaim);
6902
+ const ix = await this.getClaimIx({
6903
+ bondType: props.bondType,
6904
+ name: props.name,
6905
+ amount: amountToClaim,
6906
+ collateralType: props.collateralType,
6907
+ reserve: props.reserve,
6908
+ authority: props.authority,
6909
+ voteAccount: vb.voteAccount
6910
+ });
6911
+ ixs.push(ix);
6912
+ }
6913
+ return ixs;
6914
+ }
6841
6915
  /**
6842
6916
  * Build set withdraw authority instruction
6843
6917
  */
@@ -6938,12 +7012,13 @@ var JBondClient = class _JBondClient {
6938
7012
  }
6939
7013
  });
6940
7014
  }
6941
- /**
6942
- * Get total collected collateral for a bond state
6943
- */
6944
- async getBondStateTotalCollected(bondType, bondName, votes) {
7015
+ async getBondStateValidatorBondBalances(bondType, bondName) {
6945
7016
  const bondState = await this.getBondState(bondType, bondName);
6946
7017
  const collateralType = bondState.collateralType;
7018
+ const validatorBonds = await this.getBondValidatorStates(bondType, bondName);
7019
+ if (!validatorBonds.length) {
7020
+ return [];
7021
+ }
6947
7022
  const chunk = (arr, n = 100) => {
6948
7023
  const res = [];
6949
7024
  for (let i = 0; i < arr.length; i += n) {
@@ -6953,54 +7028,52 @@ var JBondClient = class _JBondClient {
6953
7028
  };
6954
7029
  return await matchVariant(collateralType, {
6955
7030
  native: async () => {
6956
- const addresses = votes.map((vote) => {
6957
- const [address] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
6958
- return address;
6959
- });
7031
+ const addresses = validatorBonds.map((vb) => vb.publicKey);
6960
7032
  const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
6961
- const balances = await Promise.all(
6962
- accountInfos.map(async (accountInfo) => {
6963
- if (!accountInfo) {
6964
- return 0;
6965
- }
6966
- const { lamports } = accountInfo;
6967
- return lamports;
6968
- })
6969
- );
6970
- return balances.reduce((sum, v) => sum + v, 0);
7033
+ return validatorBonds.map((vb, idx) => ({
7034
+ voteAccount: vb.voteAccount,
7035
+ balance: accountInfos[idx]?.lamports ?? 0
7036
+ }));
6971
7037
  },
6972
7038
  token: async (mint) => {
6973
- const tokenAccounts = votes.map((vote) => {
6974
- const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new web3_js.PublicKey(vote));
6975
- return splToken.getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
6976
- });
6977
- if (tokenAccounts.length === 0) {
6978
- return 0;
6979
- }
6980
- let total = 0n;
6981
- for (const part of chunk(tokenAccounts, 100)) {
6982
- const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
6983
- for (let i = 0; i < infos.length; i++) {
7039
+ const items = validatorBonds.map((vb) => ({
7040
+ voteAccount: vb.voteAccount,
7041
+ tokenAccount: splToken.getAssociatedTokenAddressSync(mint, vb.publicKey, true)
7042
+ }));
7043
+ const balances = [];
7044
+ for (const part of chunk(items, 100)) {
7045
+ const infos = await this.connection.getMultipleAccountsInfo(part.map((i) => i.tokenAccount)).catch(() => []);
7046
+ for (let i = 0; i < part.length; i++) {
6984
7047
  const info = infos[i];
6985
- if (!info) {
6986
- continue;
6987
- }
6988
- if (info.data.length !== splToken.ACCOUNT_SIZE) {
7048
+ const voteAccount = part[i].voteAccount;
7049
+ if (!info || info.data.length !== splToken.ACCOUNT_SIZE) {
7050
+ balances.push({ voteAccount, balance: 0 });
6989
7051
  continue;
6990
7052
  }
6991
7053
  try {
6992
7054
  const acc = splToken.AccountLayout.decode(info.data);
6993
7055
  const raw = BigInt(acc.amount.toString());
6994
- total += raw;
7056
+ const asNumber = Number(raw);
7057
+ balances.push({
7058
+ voteAccount,
7059
+ balance: Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER
7060
+ });
6995
7061
  } catch {
7062
+ balances.push({ voteAccount, balance: 0 });
6996
7063
  }
6997
7064
  }
6998
7065
  }
6999
- const asNumber = Number(total);
7000
- return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7066
+ return balances;
7001
7067
  }
7002
7068
  });
7003
7069
  }
7070
+ /**
7071
+ * Get total collected collateral for a bond state
7072
+ */
7073
+ async getBondStateTotalCollected(bondType, bondName) {
7074
+ const allBalances = await this.getBondStateValidatorBondBalances(bondType, bondName);
7075
+ return allBalances.reduce((sum, vb) => sum + vb.balance, 0);
7076
+ }
7004
7077
  /**
7005
7078
  * Get bond state stats
7006
7079
  * @param state
@@ -7008,17 +7081,21 @@ var JBondClient = class _JBondClient {
7008
7081
  */
7009
7082
  async getBondStateStats(state) {
7010
7083
  const bondType = anchorToBondType(state.bondType);
7011
- const validatorBonds = await this.getBondValidatorStates(bondType, state.name);
7012
7084
  const totalCollected = await this.getBondStateTotalCollected(
7013
7085
  bondType,
7014
- state.name,
7015
- validatorBonds.map((vb) => vb.voteAccount)
7086
+ state.name
7016
7087
  );
7017
7088
  return {
7018
7089
  totalCollected,
7019
7090
  status: this.getBondStateSessionStatus(state)
7020
7091
  };
7021
7092
  }
7093
+ /**
7094
+ * Get validator bond rent exempt amount
7095
+ */
7096
+ validatorBondRentExempt = async () => {
7097
+ return this.connection.getMinimumBalanceForRentExemption(this.program.account.validatorBond.size);
7098
+ };
7022
7099
  /**
7023
7100
  * Get all validator bonds for a given bond state
7024
7101
  */
@@ -7066,13 +7143,13 @@ buffer/index.js:
7066
7143
  */
7067
7144
 
7068
7145
  exports.BOND_STATE_SEED = BOND_STATE_SEED;
7069
- exports.BondClientEnv = BondClientEnv;
7070
7146
  exports.BondTransactionType = BondTransactionType;
7071
7147
  exports.BondType = BondType;
7072
7148
  exports.CROWDFUNDING_BOND_SEED = CROWDFUNDING_BOND_SEED;
7073
7149
  exports.ENV_PROGRAM_ID = ENV_PROGRAM_ID;
7074
7150
  exports.GLOBAL_STATE_SEED = GLOBAL_STATE_SEED;
7075
7151
  exports.JBondClient = JBondClient;
7152
+ exports.JBondClientEnv = JBondClientEnv;
7076
7153
  exports.NodeWallet = NodeWallet;
7077
7154
  exports.STANDARD_BOND_SEED = STANDARD_BOND_SEED;
7078
7155
  exports.SessionStatus = SessionStatus;