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

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",
@@ -5008,6 +5008,50 @@ var jbond_default = {
5008
5008
  }
5009
5009
  ]
5010
5010
  },
5011
+ {
5012
+ name: "bond_lock_funds",
5013
+ docs: [
5014
+ "Locks funds for both the current and upcoming epoch.",
5015
+ "Moves collateral from available to locked state, making it unavailable for withdrawal for the current and next epoch.",
5016
+ "# Errors",
5017
+ "Fails if the validator is not active, or if there are insufficient available funds."
5018
+ ],
5019
+ discriminator: [
5020
+ 117,
5021
+ 166,
5022
+ 195,
5023
+ 99,
5024
+ 103,
5025
+ 45,
5026
+ 195,
5027
+ 2
5028
+ ],
5029
+ accounts: [
5030
+ {
5031
+ name: "bond_state",
5032
+ writable: true
5033
+ },
5034
+ {
5035
+ name: "validator_bond",
5036
+ writable: true
5037
+ },
5038
+ {
5039
+ name: "bond_token_account",
5040
+ optional: true
5041
+ },
5042
+ {
5043
+ name: "payer",
5044
+ writable: true,
5045
+ signer: true
5046
+ }
5047
+ ],
5048
+ args: [
5049
+ {
5050
+ name: "amount",
5051
+ type: "u64"
5052
+ }
5053
+ ]
5054
+ },
5011
5055
  {
5012
5056
  name: "bond_register",
5013
5057
  docs: [
@@ -5120,6 +5164,42 @@ var jbond_default = {
5120
5164
  }
5121
5165
  ]
5122
5166
  },
5167
+ {
5168
+ name: "bond_release_funds",
5169
+ docs: [
5170
+ "Releases locked funds after an epoch ends.",
5171
+ "Moves collateral from locked back to available state, allowing withdrawals.",
5172
+ "# Errors",
5173
+ "Fails if the validator is not active, or if there are insufficient locked funds."
5174
+ ],
5175
+ discriminator: [
5176
+ 184,
5177
+ 224,
5178
+ 14,
5179
+ 24,
5180
+ 76,
5181
+ 14,
5182
+ 226,
5183
+ 190
5184
+ ],
5185
+ accounts: [
5186
+ {
5187
+ name: "validator_bond",
5188
+ writable: true
5189
+ },
5190
+ {
5191
+ name: "payer",
5192
+ writable: true,
5193
+ signer: true
5194
+ }
5195
+ ],
5196
+ args: [
5197
+ {
5198
+ name: "amount",
5199
+ type: "u64"
5200
+ }
5201
+ ]
5202
+ },
5123
5203
  {
5124
5204
  name: "bond_set_withdraw_authority",
5125
5205
  docs: [
@@ -5299,6 +5379,42 @@ var jbond_default = {
5299
5379
  }
5300
5380
  ]
5301
5381
  },
5382
+ {
5383
+ name: "bond_update",
5384
+ docs: [
5385
+ "Transitions locked funds between epochs for a validator bond.",
5386
+ "",
5387
+ "This function should be called at epoch boundaries to update the bond's",
5388
+ "`last_update_epoch` and move the `next_epoch_locked` amount into",
5389
+ "`current_epoch_locked`. It ensures that the update only occurs once per epoch.",
5390
+ "",
5391
+ "# Errors",
5392
+ "Returns [`BondError::InvalidEpoch`] if called more than once in the same epoch,",
5393
+ "or if the system clock cannot be accessed."
5394
+ ],
5395
+ discriminator: [
5396
+ 237,
5397
+ 102,
5398
+ 183,
5399
+ 192,
5400
+ 94,
5401
+ 83,
5402
+ 52,
5403
+ 82
5404
+ ],
5405
+ accounts: [
5406
+ {
5407
+ name: "validator_bond",
5408
+ writable: true
5409
+ },
5410
+ {
5411
+ name: "payer",
5412
+ writable: true,
5413
+ signer: true
5414
+ }
5415
+ ],
5416
+ args: []
5417
+ },
5302
5418
  {
5303
5419
  name: "bond_withdraw",
5304
5420
  docs: [
@@ -5899,6 +6015,11 @@ var jbond_default = {
5899
6015
  code: 6023,
5900
6016
  name: "BondNotEnabled",
5901
6017
  msg: "Bond is not enabled"
6018
+ },
6019
+ {
6020
+ code: 6024,
6021
+ name: "InvalidEpoch",
6022
+ msg: "Invalid epoch for this operation"
5902
6023
  }
5903
6024
  ],
5904
6025
  types: [
@@ -6281,33 +6402,76 @@ var jbond_default = {
6281
6402
  fields: [
6282
6403
  {
6283
6404
  name: "state",
6405
+ docs: [
6406
+ "The bond state address"
6407
+ ],
6284
6408
  type: "pubkey"
6285
6409
  },
6286
6410
  {
6287
6411
  name: "identity",
6412
+ docs: [
6413
+ "Validator identity pubkey"
6414
+ ],
6288
6415
  type: "pubkey"
6289
6416
  },
6290
6417
  {
6291
6418
  name: "vote_account",
6419
+ docs: [
6420
+ "Validator vote account pubkey"
6421
+ ],
6292
6422
  type: "pubkey"
6293
6423
  },
6294
6424
  {
6295
6425
  name: "creator",
6426
+ docs: [
6427
+ "The bond creator pubkey"
6428
+ ],
6296
6429
  type: "pubkey"
6297
6430
  },
6298
6431
  {
6299
6432
  name: "withdrawal_authority",
6433
+ docs: [
6434
+ "Optional withdrawal authority pubkey (if set, can withdraw on behalf of the validator,",
6435
+ "otherwise only identity can withdraw)"
6436
+ ],
6300
6437
  type: {
6301
6438
  option: "pubkey"
6302
6439
  }
6303
6440
  },
6304
6441
  {
6305
6442
  name: "created_at",
6443
+ docs: [
6444
+ "Bond creation timestamp"
6445
+ ],
6306
6446
  type: "i64"
6307
6447
  },
6308
6448
  {
6309
6449
  name: "bump",
6450
+ docs: [
6451
+ "Bump for the PDA"
6452
+ ],
6310
6453
  type: "u8"
6454
+ },
6455
+ {
6456
+ name: "next_epoch_locked",
6457
+ docs: [
6458
+ "Amount of collateral locked for the next epoch"
6459
+ ],
6460
+ type: "u64"
6461
+ },
6462
+ {
6463
+ name: "current_epoch_locked",
6464
+ docs: [
6465
+ "Amount of collateral locked for the current epoch"
6466
+ ],
6467
+ type: "u64"
6468
+ },
6469
+ {
6470
+ name: "last_update_epoch",
6471
+ docs: [
6472
+ "The last epoch when the bond was updated"
6473
+ ],
6474
+ type: "u64"
6311
6475
  }
6312
6476
  ]
6313
6477
  }
@@ -6429,6 +6593,20 @@ var NodeWallet = class {
6429
6593
  // src/utils/web3.ts
6430
6594
  init_cjs_shims();
6431
6595
  var SOL_DECIMALS = Math.log10(web3_js.LAMPORTS_PER_SOL);
6596
+ function lamportsToSol(lamports) {
6597
+ if (typeof lamports === "number") {
6598
+ return Math.abs(lamports) / web3_js.LAMPORTS_PER_SOL;
6599
+ }
6600
+ let signMultiplier = 1;
6601
+ if (lamports.isNeg()) {
6602
+ signMultiplier = -1;
6603
+ }
6604
+ const absLamports = lamports.abs();
6605
+ const lamportsString = absLamports.toString(10).padStart(10, "0");
6606
+ const splitIndex = lamportsString.length - SOL_DECIMALS;
6607
+ const solString = `${lamportsString.slice(0, splitIndex)}.${lamportsString.slice(splitIndex)}`;
6608
+ return signMultiplier * Number.parseFloat(solString);
6609
+ }
6432
6610
  function solToLamports(amount) {
6433
6611
  if (Number.isNaN(amount)) {
6434
6612
  return new anchor.BN(0);
@@ -6449,12 +6627,12 @@ function getBondTypeSeed(type) {
6449
6627
  }
6450
6628
 
6451
6629
  // 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 || {});
6630
+ var JBondClientEnv = /* @__PURE__ */ ((JBondClientEnv2) => {
6631
+ JBondClientEnv2["DEV"] = "dev";
6632
+ JBondClientEnv2["STAGE"] = "stage";
6633
+ JBondClientEnv2["PROD"] = "prod";
6634
+ return JBondClientEnv2;
6635
+ })(JBondClientEnv || {});
6458
6636
  var JBondClient = class _JBondClient {
6459
6637
  constructor(provider, options) {
6460
6638
  this.provider = provider;
@@ -6466,6 +6644,12 @@ var JBondClient = class _JBondClient {
6466
6644
  get history() {
6467
6645
  return new HistoryManager(this);
6468
6646
  }
6647
+ /**
6648
+ * Creates a local instance of `JBondClient` for development and testing.
6649
+ */
6650
+ static local(options) {
6651
+ return new _JBondClient(anchor.AnchorProvider.local(), options);
6652
+ }
6469
6653
  /**
6470
6654
  * Creates an instance of `JBondClient` using a provided connection and wallet.
6471
6655
  */
@@ -6577,7 +6761,7 @@ var JBondClient = class _JBondClient {
6577
6761
  return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
6578
6762
  }
6579
6763
  /**
6580
- * Claim compensation
6764
+ * Claim compensation for a validator
6581
6765
  */
6582
6766
  async claimCompensation(props) {
6583
6767
  const ix = await this.getClaimIx(props);
@@ -6838,6 +7022,35 @@ var JBondClient = class _JBondClient {
6838
7022
  });
6839
7023
  return this.program.methods.bondClaim(solToLamports(amount)).accountsPartial(accounts).instruction();
6840
7024
  }
7025
+ /**
7026
+ * Build claim all compensations instructions
7027
+ */
7028
+ async getClaimAllCompensationsIxs(props) {
7029
+ const ixs = [];
7030
+ const validatorBondBalances = await this.getBondStateValidatorBondBalances(
7031
+ props.bondType,
7032
+ props.name
7033
+ );
7034
+ for (const bond_balance of validatorBondBalances) {
7035
+ const amountToClaim = lamportsToSol(bond_balance.balance);
7036
+ if (amountToClaim <= 0) {
7037
+ this.debug("No balance to claim for validator:", bond_balance.voteAccount.toBase58());
7038
+ continue;
7039
+ }
7040
+ this.debug("Amount to claim:", amountToClaim);
7041
+ const ix = await this.getClaimIx({
7042
+ bondType: props.bondType,
7043
+ name: props.name,
7044
+ amount: amountToClaim,
7045
+ collateralType: props.collateralType,
7046
+ reserve: props.reserve,
7047
+ authority: props.authority,
7048
+ voteAccount: bond_balance.voteAccount
7049
+ });
7050
+ ixs.push(ix);
7051
+ }
7052
+ return ixs;
7053
+ }
6841
7054
  /**
6842
7055
  * Build set withdraw authority instruction
6843
7056
  */
@@ -6866,6 +7079,77 @@ var JBondClient = class _JBondClient {
6866
7079
  const authority = this.provider.wallet?.publicKey;
6867
7080
  return this.program.methods.sessionStart(new import_bn.BN(props.duration)).accountsPartial({ bondState, authority }).instruction();
6868
7081
  }
7082
+ async lockFunds(props) {
7083
+ const ix = await this.getLockFundsIx(props);
7084
+ return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
7085
+ }
7086
+ async releaseFunds(props) {
7087
+ const ix = await this.getReleaseFundsIx(props);
7088
+ return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
7089
+ }
7090
+ /**
7091
+ * Update epoch locks
7092
+ */
7093
+ async updateEpochLocks(props) {
7094
+ const { bondType, bondName, voteAccount } = props;
7095
+ const ix = await this.getUpdateEpochLocksIx({ bondType, bondName, voteAccount });
7096
+ return this.provider.sendAndConfirm?.(new web3_js.Transaction().add(ix));
7097
+ }
7098
+ /**
7099
+ * Build update epoch locks instruction
7100
+ */
7101
+ async getUpdateEpochLocksIx(props) {
7102
+ const { bondType, bondName, voteAccount } = props;
7103
+ const [validatorBond, _] = this.pda.validatorBond(bondType, bondName, voteAccount);
7104
+ return this.program.methods.bondUpdate().accountsPartial({ validatorBond }).instruction();
7105
+ }
7106
+ /**
7107
+ * Build lock funds instruction
7108
+ */
7109
+ async getLockFundsIx(props) {
7110
+ const { bondType, name, voteAccount, amount, withdrawAuthority } = props;
7111
+ const [bondState] = this.pda.bondState(bondType, name);
7112
+ let collateralType = props.collateralType;
7113
+ if (!collateralType) {
7114
+ const bondState2 = await this.getBondState(bondType, name);
7115
+ collateralType = bondState2.collateralType;
7116
+ }
7117
+ const [validatorBond] = this.pda.validatorBond(bondType, name, voteAccount);
7118
+ const accounts = {
7119
+ bondState,
7120
+ validatorBond,
7121
+ payer: withdrawAuthority,
7122
+ bondTokenAccount: null
7123
+ };
7124
+ matchVariant(collateralType, {
7125
+ native: () => {
7126
+ },
7127
+ token: (mint) => {
7128
+ const bondTokenAccount = splToken.getAssociatedTokenAddressSync(mint, validatorBond, true);
7129
+ accounts.bondTokenAccount = bondTokenAccount;
7130
+ }
7131
+ });
7132
+ return this.program.methods.bondLockFunds(solToLamports(amount)).accountsPartial(accounts).instruction();
7133
+ }
7134
+ /**
7135
+ * Build release funds instruction
7136
+ */
7137
+ async getReleaseFundsIx(props) {
7138
+ const { bondType, name, voteAccount, amount, withdrawAuthority } = props;
7139
+ const [validatorBond] = this.pda.validatorBond(bondType, name, voteAccount);
7140
+ const accounts = {
7141
+ validatorBond,
7142
+ payer: withdrawAuthority
7143
+ };
7144
+ return this.program.methods.bondReleaseFunds(solToLamports(amount)).accountsPartial(accounts).instruction();
7145
+ }
7146
+ /**
7147
+ * Get available collateral for withdrawal (taking into account locked funds)
7148
+ */
7149
+ async getAvailableCollateral(bondType, bondName, voteAccount) {
7150
+ const validatorBond = await this.getValidatorBond(bondType, bondName, voteAccount);
7151
+ return await this.getValidatorBondBalance(bondType, bondName, voteAccount) - validatorBond.currentEpochLocked.toNumber();
7152
+ }
6869
7153
  /**
6870
7154
  * Get all bond states with total collected collateral
6871
7155
  */
@@ -6923,8 +7207,8 @@ var JBondClient = class _JBondClient {
6923
7207
  if (!accountInfo) {
6924
7208
  return 0;
6925
7209
  }
6926
- const { data, lamports } = accountInfo;
6927
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
7210
+ const { lamports } = accountInfo;
7211
+ const rentExempt = await this.validatorBondRentExempt();
6928
7212
  return Math.max(0, lamports - rentExempt);
6929
7213
  },
6930
7214
  token: async (mint) => {
@@ -6938,12 +7222,13 @@ var JBondClient = class _JBondClient {
6938
7222
  }
6939
7223
  });
6940
7224
  }
6941
- /**
6942
- * Get total collected collateral for a bond state
6943
- */
6944
- async getBondStateTotalCollected(bondType, bondName, votes) {
7225
+ async getBondStateValidatorBondBalances(bondType, bondName) {
6945
7226
  const bondState = await this.getBondState(bondType, bondName);
6946
7227
  const collateralType = bondState.collateralType;
7228
+ const validatorBonds = await this.getValidatorBondsForState(bondType, bondName);
7229
+ if (!validatorBonds.length) {
7230
+ return [];
7231
+ }
6947
7232
  const chunk = (arr, n = 100) => {
6948
7233
  const res = [];
6949
7234
  for (let i = 0; i < arr.length; i += n) {
@@ -6953,54 +7238,53 @@ var JBondClient = class _JBondClient {
6953
7238
  };
6954
7239
  return await matchVariant(collateralType, {
6955
7240
  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
- });
7241
+ const addresses = validatorBonds.map((vb) => vb.publicKey);
6960
7242
  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);
7243
+ const rentExempt = await this.validatorBondRentExempt();
7244
+ return validatorBonds.map((vb, idx) => ({
7245
+ voteAccount: vb.voteAccount,
7246
+ balance: Math.max(0, (accountInfos[idx]?.lamports ?? 0) - rentExempt)
7247
+ }));
6971
7248
  },
6972
7249
  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++) {
7250
+ const items = validatorBonds.map((vb) => ({
7251
+ voteAccount: vb.voteAccount,
7252
+ tokenAccount: splToken.getAssociatedTokenAddressSync(mint, vb.publicKey, true)
7253
+ }));
7254
+ const balances = [];
7255
+ for (const part of chunk(items, 100)) {
7256
+ const infos = await this.connection.getMultipleAccountsInfo(part.map((i) => i.tokenAccount)).catch(() => []);
7257
+ for (let i = 0; i < part.length; i++) {
6984
7258
  const info = infos[i];
6985
- if (!info) {
6986
- continue;
6987
- }
6988
- if (info.data.length !== splToken.ACCOUNT_SIZE) {
7259
+ const voteAccount = part[i].voteAccount;
7260
+ if (!info || info.data.length !== splToken.ACCOUNT_SIZE) {
7261
+ balances.push({ voteAccount, balance: 0 });
6989
7262
  continue;
6990
7263
  }
6991
7264
  try {
6992
7265
  const acc = splToken.AccountLayout.decode(info.data);
6993
7266
  const raw = BigInt(acc.amount.toString());
6994
- total += raw;
7267
+ const asNumber = Number(raw);
7268
+ balances.push({
7269
+ voteAccount,
7270
+ balance: Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER
7271
+ });
6995
7272
  } catch {
7273
+ balances.push({ voteAccount, balance: 0 });
6996
7274
  }
6997
7275
  }
6998
7276
  }
6999
- const asNumber = Number(total);
7000
- return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7277
+ return balances;
7001
7278
  }
7002
7279
  });
7003
7280
  }
7281
+ /**
7282
+ * Get total collected collateral for a bond state
7283
+ */
7284
+ async getBondStateTotalCollected(bondType, bondName) {
7285
+ const allBalances = await this.getBondStateValidatorBondBalances(bondType, bondName);
7286
+ return allBalances.reduce((sum, vb) => sum + vb.balance, 0);
7287
+ }
7004
7288
  /**
7005
7289
  * Get bond state stats
7006
7290
  * @param state
@@ -7008,21 +7292,25 @@ var JBondClient = class _JBondClient {
7008
7292
  */
7009
7293
  async getBondStateStats(state) {
7010
7294
  const bondType = anchorToBondType(state.bondType);
7011
- const validatorBonds = await this.getBondValidatorStates(bondType, state.name);
7012
7295
  const totalCollected = await this.getBondStateTotalCollected(
7013
7296
  bondType,
7014
- state.name,
7015
- validatorBonds.map((vb) => vb.voteAccount)
7297
+ state.name
7016
7298
  );
7017
7299
  return {
7018
7300
  totalCollected,
7019
7301
  status: this.getBondStateSessionStatus(state)
7020
7302
  };
7021
7303
  }
7304
+ /**
7305
+ * Get validator bond rent exempt amount
7306
+ */
7307
+ validatorBondRentExempt = async () => {
7308
+ return this.connection.getMinimumBalanceForRentExemption(this.program.account.validatorBond.size);
7309
+ };
7022
7310
  /**
7023
7311
  * Get all validator bonds for a given bond state
7024
7312
  */
7025
- async getBondValidatorStates(bondType, bondName) {
7313
+ async getValidatorBondsForState(bondType, bondName) {
7026
7314
  const [bondState] = this.pda.bondState(bondType, bondName);
7027
7315
  const accounts = await this.program.account.validatorBond.all([
7028
7316
  {
@@ -7066,13 +7354,13 @@ buffer/index.js:
7066
7354
  */
7067
7355
 
7068
7356
  exports.BOND_STATE_SEED = BOND_STATE_SEED;
7069
- exports.BondClientEnv = BondClientEnv;
7070
7357
  exports.BondTransactionType = BondTransactionType;
7071
7358
  exports.BondType = BondType;
7072
7359
  exports.CROWDFUNDING_BOND_SEED = CROWDFUNDING_BOND_SEED;
7073
7360
  exports.ENV_PROGRAM_ID = ENV_PROGRAM_ID;
7074
7361
  exports.GLOBAL_STATE_SEED = GLOBAL_STATE_SEED;
7075
7362
  exports.JBondClient = JBondClient;
7363
+ exports.JBondClientEnv = JBondClientEnv;
7076
7364
  exports.NodeWallet = NodeWallet;
7077
7365
  exports.STANDARD_BOND_SEED = STANDARD_BOND_SEED;
7078
7366
  exports.SessionStatus = SessionStatus;