@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.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,35 @@ 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 validatorBondBalances = await this.getBondStateValidatorBondBalances(
6857
+ props.bondType,
6858
+ props.name
6859
+ );
6860
+ for (const bond_balance of validatorBondBalances) {
6861
+ const amountToClaim = lamportsToSol(bond_balance.balance);
6862
+ if (amountToClaim <= 0) {
6863
+ this.debug("No balance to claim for validator:", bond_balance.voteAccount.toBase58());
6864
+ continue;
6865
+ }
6866
+ this.debug("Amount to claim:", amountToClaim);
6867
+ const ix = await this.getClaimIx({
6868
+ bondType: props.bondType,
6869
+ name: props.name,
6870
+ amount: amountToClaim,
6871
+ collateralType: props.collateralType,
6872
+ reserve: props.reserve,
6873
+ authority: props.authority,
6874
+ voteAccount: bond_balance.voteAccount
6875
+ });
6876
+ ixs.push(ix);
6877
+ }
6878
+ return ixs;
6879
+ }
6809
6880
  /**
6810
6881
  * Build set withdraw authority instruction
6811
6882
  */
@@ -6891,8 +6962,8 @@ var JBondClient = class _JBondClient {
6891
6962
  if (!accountInfo) {
6892
6963
  return 0;
6893
6964
  }
6894
- const { data, lamports } = accountInfo;
6895
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
6965
+ const { lamports } = accountInfo;
6966
+ const rentExempt = await this.validatorBondRentExempt();
6896
6967
  return Math.max(0, lamports - rentExempt);
6897
6968
  },
6898
6969
  token: async (mint) => {
@@ -6906,12 +6977,13 @@ var JBondClient = class _JBondClient {
6906
6977
  }
6907
6978
  });
6908
6979
  }
6909
- /**
6910
- * Get total collected collateral for a bond state
6911
- */
6912
- async getBondStateTotalCollected(bondType, bondName, votes) {
6980
+ async getBondStateValidatorBondBalances(bondType, bondName) {
6913
6981
  const bondState = await this.getBondState(bondType, bondName);
6914
6982
  const collateralType = bondState.collateralType;
6983
+ const validatorBonds = await this.getValidatorBondsForState(bondType, bondName);
6984
+ if (!validatorBonds.length) {
6985
+ return [];
6986
+ }
6915
6987
  const chunk = (arr, n = 100) => {
6916
6988
  const res = [];
6917
6989
  for (let i = 0; i < arr.length; i += n) {
@@ -6921,54 +6993,53 @@ var JBondClient = class _JBondClient {
6921
6993
  };
6922
6994
  return await matchVariant(collateralType, {
6923
6995
  native: async () => {
6924
- const addresses = votes.map((vote) => {
6925
- const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6926
- return address;
6927
- });
6996
+ const addresses = validatorBonds.map((vb) => vb.publicKey);
6928
6997
  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);
6998
+ const rentExempt = await this.validatorBondRentExempt();
6999
+ return validatorBonds.map((vb, idx) => ({
7000
+ voteAccount: vb.voteAccount,
7001
+ balance: Math.max(0, (accountInfos[idx]?.lamports ?? 0) - rentExempt)
7002
+ }));
6939
7003
  },
6940
7004
  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++) {
7005
+ const items = validatorBonds.map((vb) => ({
7006
+ voteAccount: vb.voteAccount,
7007
+ tokenAccount: getAssociatedTokenAddressSync(mint, vb.publicKey, true)
7008
+ }));
7009
+ const balances = [];
7010
+ for (const part of chunk(items, 100)) {
7011
+ const infos = await this.connection.getMultipleAccountsInfo(part.map((i) => i.tokenAccount)).catch(() => []);
7012
+ for (let i = 0; i < part.length; i++) {
6952
7013
  const info = infos[i];
6953
- if (!info) {
6954
- continue;
6955
- }
6956
- if (info.data.length !== ACCOUNT_SIZE) {
7014
+ const voteAccount = part[i].voteAccount;
7015
+ if (!info || info.data.length !== ACCOUNT_SIZE) {
7016
+ balances.push({ voteAccount, balance: 0 });
6957
7017
  continue;
6958
7018
  }
6959
7019
  try {
6960
7020
  const acc = AccountLayout.decode(info.data);
6961
7021
  const raw = BigInt(acc.amount.toString());
6962
- total += raw;
7022
+ const asNumber = Number(raw);
7023
+ balances.push({
7024
+ voteAccount,
7025
+ balance: Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER
7026
+ });
6963
7027
  } catch {
7028
+ balances.push({ voteAccount, balance: 0 });
6964
7029
  }
6965
7030
  }
6966
7031
  }
6967
- const asNumber = Number(total);
6968
- return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7032
+ return balances;
6969
7033
  }
6970
7034
  });
6971
7035
  }
7036
+ /**
7037
+ * Get total collected collateral for a bond state
7038
+ */
7039
+ async getBondStateTotalCollected(bondType, bondName) {
7040
+ const allBalances = await this.getBondStateValidatorBondBalances(bondType, bondName);
7041
+ return allBalances.reduce((sum, vb) => sum + vb.balance, 0);
7042
+ }
6972
7043
  /**
6973
7044
  * Get bond state stats
6974
7045
  * @param state
@@ -6976,21 +7047,25 @@ var JBondClient = class _JBondClient {
6976
7047
  */
6977
7048
  async getBondStateStats(state) {
6978
7049
  const bondType = anchorToBondType(state.bondType);
6979
- const validatorBonds = await this.getBondValidatorStates(bondType, state.name);
6980
7050
  const totalCollected = await this.getBondStateTotalCollected(
6981
7051
  bondType,
6982
- state.name,
6983
- validatorBonds.map((vb) => vb.voteAccount)
7052
+ state.name
6984
7053
  );
6985
7054
  return {
6986
7055
  totalCollected,
6987
7056
  status: this.getBondStateSessionStatus(state)
6988
7057
  };
6989
7058
  }
7059
+ /**
7060
+ * Get validator bond rent exempt amount
7061
+ */
7062
+ validatorBondRentExempt = async () => {
7063
+ return this.connection.getMinimumBalanceForRentExemption(this.program.account.validatorBond.size);
7064
+ };
6990
7065
  /**
6991
7066
  * Get all validator bonds for a given bond state
6992
7067
  */
6993
- async getBondValidatorStates(bondType, bondName) {
7068
+ async getValidatorBondsForState(bondType, bondName) {
6994
7069
  const [bondState] = this.pda.bondState(bondType, bondName);
6995
7070
  const accounts = await this.program.account.validatorBond.all([
6996
7071
  {
@@ -7033,6 +7108,6 @@ buffer/index.js:
7033
7108
  *)
7034
7109
  */
7035
7110
 
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 };
7111
+ 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
7112
  //# sourceMappingURL=index.mjs.map
7038
7113
  //# sourceMappingURL=index.mjs.map