@jpool/bond-sdk 0.10.6 → 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.mjs CHANGED
@@ -4735,7 +4735,7 @@ function slotToEpoch(slot, cluster) {
4735
4735
 
4736
4736
  // src/idl/jbond.json
4737
4737
  var jbond_default = {
4738
- address: "BondQ7KqZreTcW2UbeTNDcLCJQ3aXAtLn2Fm6ftaJDU",
4738
+ address: "Fo17edWRJewZNTRibgx9iTfjywCW6dzS81VwBLmPFVq1",
4739
4739
  metadata: {
4740
4740
  name: "jbond",
4741
4741
  version: "0.2.1",
@@ -6257,32 +6257,54 @@ var jbond_default = {
6257
6257
  fields: [
6258
6258
  {
6259
6259
  name: "state",
6260
+ docs: [
6261
+ "The bond state address"
6262
+ ],
6260
6263
  type: "pubkey"
6261
6264
  },
6262
6265
  {
6263
6266
  name: "identity",
6267
+ docs: [
6268
+ "Validator identity pubkey"
6269
+ ],
6264
6270
  type: "pubkey"
6265
6271
  },
6266
6272
  {
6267
6273
  name: "vote_account",
6274
+ docs: [
6275
+ "Validator vote account pubkey"
6276
+ ],
6268
6277
  type: "pubkey"
6269
6278
  },
6270
6279
  {
6271
6280
  name: "creator",
6281
+ docs: [
6282
+ "The bond creator pubkey"
6283
+ ],
6272
6284
  type: "pubkey"
6273
6285
  },
6274
6286
  {
6275
6287
  name: "withdrawal_authority",
6288
+ docs: [
6289
+ "Optional withdrawal authority pubkey (if set, can withdraw on behalf of the validator,",
6290
+ "otherwise only identity can withdraw)"
6291
+ ],
6276
6292
  type: {
6277
6293
  option: "pubkey"
6278
6294
  }
6279
6295
  },
6280
6296
  {
6281
6297
  name: "created_at",
6298
+ docs: [
6299
+ "Bond creation timestamp"
6300
+ ],
6282
6301
  type: "i64"
6283
6302
  },
6284
6303
  {
6285
6304
  name: "bump",
6305
+ docs: [
6306
+ "Bump for the PDA"
6307
+ ],
6286
6308
  type: "u8"
6287
6309
  }
6288
6310
  ]
@@ -6397,6 +6419,20 @@ var NodeWallet = class {
6397
6419
  }
6398
6420
  };
6399
6421
  var SOL_DECIMALS = Math.log10(LAMPORTS_PER_SOL);
6422
+ function lamportsToSol(lamports) {
6423
+ if (typeof lamports === "number") {
6424
+ return Math.abs(lamports) / LAMPORTS_PER_SOL;
6425
+ }
6426
+ let signMultiplier = 1;
6427
+ if (lamports.isNeg()) {
6428
+ signMultiplier = -1;
6429
+ }
6430
+ const absLamports = lamports.abs();
6431
+ const lamportsString = absLamports.toString(10).padStart(10, "0");
6432
+ const splitIndex = lamportsString.length - SOL_DECIMALS;
6433
+ const solString = `${lamportsString.slice(0, splitIndex)}.${lamportsString.slice(splitIndex)}`;
6434
+ return signMultiplier * Number.parseFloat(solString);
6435
+ }
6400
6436
  function solToLamports(amount) {
6401
6437
  if (Number.isNaN(amount)) {
6402
6438
  return new BN(0);
@@ -6417,12 +6453,12 @@ function getBondTypeSeed(type) {
6417
6453
  }
6418
6454
 
6419
6455
  // src/client.ts
6420
- var BondClientEnv = /* @__PURE__ */ ((BondClientEnv2) => {
6421
- BondClientEnv2["DEV"] = "dev";
6422
- BondClientEnv2["STAGE"] = "stage";
6423
- BondClientEnv2["PROD"] = "prod";
6424
- return BondClientEnv2;
6425
- })(BondClientEnv || {});
6456
+ var JBondClientEnv = /* @__PURE__ */ ((JBondClientEnv2) => {
6457
+ JBondClientEnv2["DEV"] = "dev";
6458
+ JBondClientEnv2["STAGE"] = "stage";
6459
+ JBondClientEnv2["PROD"] = "prod";
6460
+ return JBondClientEnv2;
6461
+ })(JBondClientEnv || {});
6426
6462
  var JBondClient = class _JBondClient {
6427
6463
  constructor(provider, options) {
6428
6464
  this.provider = provider;
@@ -6434,6 +6470,12 @@ var JBondClient = class _JBondClient {
6434
6470
  get history() {
6435
6471
  return new HistoryManager(this);
6436
6472
  }
6473
+ /**
6474
+ * Creates a local instance of `JBondClient` for development and testing.
6475
+ */
6476
+ static local(options) {
6477
+ return new _JBondClient(AnchorProvider.local(), options);
6478
+ }
6437
6479
  /**
6438
6480
  * Creates an instance of `JBondClient` using a provided connection and wallet.
6439
6481
  */
@@ -6545,7 +6587,7 @@ var JBondClient = class _JBondClient {
6545
6587
  return this.provider.sendAndConfirm?.(new Transaction().add(ix));
6546
6588
  }
6547
6589
  /**
6548
- * Claim compensation
6590
+ * Claim compensation for a validator
6549
6591
  */
6550
6592
  async claimCompensation(props) {
6551
6593
  const ix = await this.getClaimIx(props);
@@ -6806,6 +6848,38 @@ var JBondClient = class _JBondClient {
6806
6848
  });
6807
6849
  return this.program.methods.bondClaim(solToLamports(amount)).accountsPartial(accounts).instruction();
6808
6850
  }
6851
+ /**
6852
+ * Build claim all compensations instructions
6853
+ */
6854
+ async getClaimAllCompensationsIxs(props) {
6855
+ const ixs = [];
6856
+ const validatorBonds = await this.getBondValidatorStates(props.bondType, props.name);
6857
+ const validatorBondBalances = await this.getBondStateValidatorBondBalances(
6858
+ props.bondType,
6859
+ props.name
6860
+ );
6861
+ for (const vb of validatorBonds) {
6862
+ console.log("Processing claim for validator:", vb.voteAccount.toBase58());
6863
+ const validatorBondBalance = lamportsToSol(validatorBondBalances.find((vbb) => vbb.voteAccount.equals(vb.voteAccount))?.balance || 0);
6864
+ const amountToClaim = validatorBondBalance - (props.collateralType ? 0 : lamportsToSol(await this.validatorBondRentExempt()));
6865
+ if (amountToClaim <= 0) {
6866
+ console.log("No balance to claim for validator:", vb.voteAccount.toBase58());
6867
+ continue;
6868
+ }
6869
+ console.log("Amount to claim:", amountToClaim);
6870
+ const ix = await this.getClaimIx({
6871
+ bondType: props.bondType,
6872
+ name: props.name,
6873
+ amount: amountToClaim,
6874
+ collateralType: props.collateralType,
6875
+ reserve: props.reserve,
6876
+ authority: props.authority,
6877
+ voteAccount: vb.voteAccount
6878
+ });
6879
+ ixs.push(ix);
6880
+ }
6881
+ return ixs;
6882
+ }
6809
6883
  /**
6810
6884
  * Build set withdraw authority instruction
6811
6885
  */
@@ -6906,12 +6980,13 @@ var JBondClient = class _JBondClient {
6906
6980
  }
6907
6981
  });
6908
6982
  }
6909
- /**
6910
- * Get total collected collateral for a bond state
6911
- */
6912
- async getBondStateTotalCollected(bondType, bondName, votes) {
6983
+ async getBondStateValidatorBondBalances(bondType, bondName) {
6913
6984
  const bondState = await this.getBondState(bondType, bondName);
6914
6985
  const collateralType = bondState.collateralType;
6986
+ const validatorBonds = await this.getBondValidatorStates(bondType, bondName);
6987
+ if (!validatorBonds.length) {
6988
+ return [];
6989
+ }
6915
6990
  const chunk = (arr, n = 100) => {
6916
6991
  const res = [];
6917
6992
  for (let i = 0; i < arr.length; i += n) {
@@ -6921,54 +6996,52 @@ var JBondClient = class _JBondClient {
6921
6996
  };
6922
6997
  return await matchVariant(collateralType, {
6923
6998
  native: async () => {
6924
- const addresses = votes.map((vote) => {
6925
- const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6926
- return address;
6927
- });
6999
+ const addresses = validatorBonds.map((vb) => vb.publicKey);
6928
7000
  const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
6929
- const balances = await Promise.all(
6930
- accountInfos.map(async (accountInfo) => {
6931
- if (!accountInfo) {
6932
- return 0;
6933
- }
6934
- const { lamports } = accountInfo;
6935
- return lamports;
6936
- })
6937
- );
6938
- return balances.reduce((sum, v) => sum + v, 0);
7001
+ return validatorBonds.map((vb, idx) => ({
7002
+ voteAccount: vb.voteAccount,
7003
+ balance: accountInfos[idx]?.lamports ?? 0
7004
+ }));
6939
7005
  },
6940
7006
  token: async (mint) => {
6941
- const tokenAccounts = votes.map((vote) => {
6942
- const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6943
- return getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
6944
- });
6945
- if (tokenAccounts.length === 0) {
6946
- return 0;
6947
- }
6948
- let total = 0n;
6949
- for (const part of chunk(tokenAccounts, 100)) {
6950
- const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
6951
- for (let i = 0; i < infos.length; i++) {
7007
+ const items = validatorBonds.map((vb) => ({
7008
+ voteAccount: vb.voteAccount,
7009
+ tokenAccount: getAssociatedTokenAddressSync(mint, vb.publicKey, true)
7010
+ }));
7011
+ const balances = [];
7012
+ for (const part of chunk(items, 100)) {
7013
+ const infos = await this.connection.getMultipleAccountsInfo(part.map((i) => i.tokenAccount)).catch(() => []);
7014
+ for (let i = 0; i < part.length; i++) {
6952
7015
  const info = infos[i];
6953
- if (!info) {
6954
- continue;
6955
- }
6956
- if (info.data.length !== ACCOUNT_SIZE) {
7016
+ const voteAccount = part[i].voteAccount;
7017
+ if (!info || info.data.length !== ACCOUNT_SIZE) {
7018
+ balances.push({ voteAccount, balance: 0 });
6957
7019
  continue;
6958
7020
  }
6959
7021
  try {
6960
7022
  const acc = AccountLayout.decode(info.data);
6961
7023
  const raw = BigInt(acc.amount.toString());
6962
- total += raw;
7024
+ const asNumber = Number(raw);
7025
+ balances.push({
7026
+ voteAccount,
7027
+ balance: Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER
7028
+ });
6963
7029
  } catch {
7030
+ balances.push({ voteAccount, balance: 0 });
6964
7031
  }
6965
7032
  }
6966
7033
  }
6967
- const asNumber = Number(total);
6968
- return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7034
+ return balances;
6969
7035
  }
6970
7036
  });
6971
7037
  }
7038
+ /**
7039
+ * Get total collected collateral for a bond state
7040
+ */
7041
+ async getBondStateTotalCollected(bondType, bondName) {
7042
+ const allBalances = await this.getBondStateValidatorBondBalances(bondType, bondName);
7043
+ return allBalances.reduce((sum, vb) => sum + vb.balance, 0);
7044
+ }
6972
7045
  /**
6973
7046
  * Get bond state stats
6974
7047
  * @param state
@@ -6976,17 +7049,21 @@ var JBondClient = class _JBondClient {
6976
7049
  */
6977
7050
  async getBondStateStats(state) {
6978
7051
  const bondType = anchorToBondType(state.bondType);
6979
- const validatorBonds = await this.getBondValidatorStates(bondType, state.name);
6980
7052
  const totalCollected = await this.getBondStateTotalCollected(
6981
7053
  bondType,
6982
- state.name,
6983
- validatorBonds.map((vb) => vb.voteAccount)
7054
+ state.name
6984
7055
  );
6985
7056
  return {
6986
7057
  totalCollected,
6987
7058
  status: this.getBondStateSessionStatus(state)
6988
7059
  };
6989
7060
  }
7061
+ /**
7062
+ * Get validator bond rent exempt amount
7063
+ */
7064
+ validatorBondRentExempt = async () => {
7065
+ return this.connection.getMinimumBalanceForRentExemption(this.program.account.validatorBond.size);
7066
+ };
6990
7067
  /**
6991
7068
  * Get all validator bonds for a given bond state
6992
7069
  */
@@ -7033,6 +7110,6 @@ buffer/index.js:
7033
7110
  *)
7034
7111
  */
7035
7112
 
7036
- export { BOND_STATE_SEED, BondClientEnv, BondTransactionType, BondType, CROWDFUNDING_BOND_SEED, ENV_PROGRAM_ID, GLOBAL_STATE_SEED, JBondClient, NodeWallet, STANDARD_BOND_SEED, SessionStatus, VALIDATOR_BOND_SEED };
7113
+ export { BOND_STATE_SEED, BondTransactionType, BondType, CROWDFUNDING_BOND_SEED, ENV_PROGRAM_ID, GLOBAL_STATE_SEED, JBondClient, JBondClientEnv, NodeWallet, STANDARD_BOND_SEED, SessionStatus, VALIDATOR_BOND_SEED };
7037
7114
  //# sourceMappingURL=index.mjs.map
7038
7115
  //# sourceMappingURL=index.mjs.map