@jpool/bond-sdk 0.10.6 → 0.11.0-next.4

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,35 @@ 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 validatorBondBalances = await this.getBondStateValidatorBondBalances(
6889
+ props.bondType,
6890
+ props.name
6891
+ );
6892
+ for (const bond_balance of validatorBondBalances) {
6893
+ const amountToClaim = lamportsToSol(bond_balance.balance);
6894
+ if (amountToClaim <= 0) {
6895
+ this.debug("No balance to claim for validator:", bond_balance.voteAccount.toBase58());
6896
+ continue;
6897
+ }
6898
+ this.debug("Amount to claim:", amountToClaim);
6899
+ const ix = await this.getClaimIx({
6900
+ bondType: props.bondType,
6901
+ name: props.name,
6902
+ amount: amountToClaim,
6903
+ collateralType: props.collateralType,
6904
+ reserve: props.reserve,
6905
+ authority: props.authority,
6906
+ voteAccount: bond_balance.voteAccount
6907
+ });
6908
+ ixs.push(ix);
6909
+ }
6910
+ return ixs;
6911
+ }
6841
6912
  /**
6842
6913
  * Build set withdraw authority instruction
6843
6914
  */
@@ -6923,8 +6994,8 @@ var JBondClient = class _JBondClient {
6923
6994
  if (!accountInfo) {
6924
6995
  return 0;
6925
6996
  }
6926
- const { data, lamports } = accountInfo;
6927
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6997
+ const { lamports } = accountInfo;
6998
+ const rentExempt = await this.validatorBondRentExempt();
6928
6999
  return Math.max(0, lamports - rentExempt);
6929
7000
  },
6930
7001
  token: async (mint) => {
@@ -6938,12 +7009,13 @@ var JBondClient = class _JBondClient {
6938
7009
  }
6939
7010
  });
6940
7011
  }
6941
- /**
6942
- * Get total collected collateral for a bond state
6943
- */
6944
- async getBondStateTotalCollected(bondType, bondName, votes) {
7012
+ async getBondStateValidatorBondBalances(bondType, bondName) {
6945
7013
  const bondState = await this.getBondState(bondType, bondName);
6946
7014
  const collateralType = bondState.collateralType;
7015
+ const validatorBonds = await this.getValidatorBondsForState(bondType, bondName);
7016
+ if (!validatorBonds.length) {
7017
+ return [];
7018
+ }
6947
7019
  const chunk = (arr, n = 100) => {
6948
7020
  const res = [];
6949
7021
  for (let i = 0; i < arr.length; i += n) {
@@ -6953,54 +7025,53 @@ var JBondClient = class _JBondClient {
6953
7025
  };
6954
7026
  return await matchVariant(collateralType, {
6955
7027
  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
- });
7028
+ const addresses = validatorBonds.map((vb) => vb.publicKey);
6960
7029
  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);
7030
+ const rentExempt = await this.validatorBondRentExempt();
7031
+ return validatorBonds.map((vb, idx) => ({
7032
+ voteAccount: vb.voteAccount,
7033
+ balance: Math.max(0, (accountInfos[idx]?.lamports ?? 0) - rentExempt)
7034
+ }));
6971
7035
  },
6972
7036
  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++) {
7037
+ const items = validatorBonds.map((vb) => ({
7038
+ voteAccount: vb.voteAccount,
7039
+ tokenAccount: splToken.getAssociatedTokenAddressSync(mint, vb.publicKey, true)
7040
+ }));
7041
+ const balances = [];
7042
+ for (const part of chunk(items, 100)) {
7043
+ const infos = await this.connection.getMultipleAccountsInfo(part.map((i) => i.tokenAccount)).catch(() => []);
7044
+ for (let i = 0; i < part.length; i++) {
6984
7045
  const info = infos[i];
6985
- if (!info) {
6986
- continue;
6987
- }
6988
- if (info.data.length !== splToken.ACCOUNT_SIZE) {
7046
+ const voteAccount = part[i].voteAccount;
7047
+ if (!info || info.data.length !== splToken.ACCOUNT_SIZE) {
7048
+ balances.push({ voteAccount, balance: 0 });
6989
7049
  continue;
6990
7050
  }
6991
7051
  try {
6992
7052
  const acc = splToken.AccountLayout.decode(info.data);
6993
7053
  const raw = BigInt(acc.amount.toString());
6994
- total += raw;
7054
+ const asNumber = Number(raw);
7055
+ balances.push({
7056
+ voteAccount,
7057
+ balance: Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER
7058
+ });
6995
7059
  } catch {
7060
+ balances.push({ voteAccount, balance: 0 });
6996
7061
  }
6997
7062
  }
6998
7063
  }
6999
- const asNumber = Number(total);
7000
- return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7064
+ return balances;
7001
7065
  }
7002
7066
  });
7003
7067
  }
7068
+ /**
7069
+ * Get total collected collateral for a bond state
7070
+ */
7071
+ async getBondStateTotalCollected(bondType, bondName) {
7072
+ const allBalances = await this.getBondStateValidatorBondBalances(bondType, bondName);
7073
+ return allBalances.reduce((sum, vb) => sum + vb.balance, 0);
7074
+ }
7004
7075
  /**
7005
7076
  * Get bond state stats
7006
7077
  * @param state
@@ -7008,21 +7079,25 @@ var JBondClient = class _JBondClient {
7008
7079
  */
7009
7080
  async getBondStateStats(state) {
7010
7081
  const bondType = anchorToBondType(state.bondType);
7011
- const validatorBonds = await this.getBondValidatorStates(bondType, state.name);
7012
7082
  const totalCollected = await this.getBondStateTotalCollected(
7013
7083
  bondType,
7014
- state.name,
7015
- validatorBonds.map((vb) => vb.voteAccount)
7084
+ state.name
7016
7085
  );
7017
7086
  return {
7018
7087
  totalCollected,
7019
7088
  status: this.getBondStateSessionStatus(state)
7020
7089
  };
7021
7090
  }
7091
+ /**
7092
+ * Get validator bond rent exempt amount
7093
+ */
7094
+ validatorBondRentExempt = async () => {
7095
+ return this.connection.getMinimumBalanceForRentExemption(this.program.account.validatorBond.size);
7096
+ };
7022
7097
  /**
7023
7098
  * Get all validator bonds for a given bond state
7024
7099
  */
7025
- async getBondValidatorStates(bondType, bondName) {
7100
+ async getValidatorBondsForState(bondType, bondName) {
7026
7101
  const [bondState] = this.pda.bondState(bondType, bondName);
7027
7102
  const accounts = await this.program.account.validatorBond.all([
7028
7103
  {
@@ -7066,13 +7141,13 @@ buffer/index.js:
7066
7141
  */
7067
7142
 
7068
7143
  exports.BOND_STATE_SEED = BOND_STATE_SEED;
7069
- exports.BondClientEnv = BondClientEnv;
7070
7144
  exports.BondTransactionType = BondTransactionType;
7071
7145
  exports.BondType = BondType;
7072
7146
  exports.CROWDFUNDING_BOND_SEED = CROWDFUNDING_BOND_SEED;
7073
7147
  exports.ENV_PROGRAM_ID = ENV_PROGRAM_ID;
7074
7148
  exports.GLOBAL_STATE_SEED = GLOBAL_STATE_SEED;
7075
7149
  exports.JBondClient = JBondClient;
7150
+ exports.JBondClientEnv = JBondClientEnv;
7076
7151
  exports.NodeWallet = NodeWallet;
7077
7152
  exports.STANDARD_BOND_SEED = STANDARD_BOND_SEED;
7078
7153
  exports.SessionStatus = SessionStatus;