@jpool/bond-sdk 0.11.6 → 0.11.8

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
@@ -1,7 +1,6 @@
1
- import { utils, AnchorProvider, Program, BN } from '@coral-xyz/anchor';
2
- import { STAKE_POOL_PROGRAM_ID, StakePoolInstruction, getStakePoolAccount, depositSol } from '@solana/spl-stake-pool';
3
- import { getAssociatedTokenAddressSync, createAssociatedTokenAccountIdempotentInstruction, ACCOUNT_SIZE, AccountLayout } from '@solana/spl-token';
4
- import { PublicKey, LAMPORTS_PER_SOL, Transaction, Keypair, SystemProgram } from '@solana/web3.js';
1
+ import { utils, Program, AnchorProvider, BN } from '@coral-xyz/anchor';
2
+ import { getAssociatedTokenAddressSync, ACCOUNT_SIZE, AccountLayout } from '@solana/spl-token';
3
+ import { PublicKey, Transaction, LAMPORTS_PER_SOL } from '@solana/web3.js';
5
4
 
6
5
  var __create = Object.create;
7
6
  var __defProp = Object.defineProperty;
@@ -4548,13 +4547,17 @@ var require_bn = __commonJS({
4548
4547
  // src/client.ts
4549
4548
  var import_bn = __toESM(require_bn());
4550
4549
 
4550
+ // src/constants.ts
4551
+ var GLOBAL_STATE_SEED = "global_state";
4552
+ var BOND_STATE_SEED = "bond_state";
4553
+ var VALIDATOR_BOND_SEED = "validator_bond";
4554
+ var STANDARD_BOND_SEED = "standard";
4555
+ var CROWDFUNDING_BOND_SEED = "crowdfunding";
4556
+ var ENV_PROGRAM_ID = {
4557
+ // [BondClientEnv.DEV]: new PublicKey('...'),
4558
+ };
4559
+
4551
4560
  // src/types/index.ts
4552
- var JBondClientEnv = /* @__PURE__ */ ((JBondClientEnv2) => {
4553
- JBondClientEnv2["DEV"] = "dev";
4554
- JBondClientEnv2["STAGE"] = "stage";
4555
- JBondClientEnv2["PROD"] = "prod";
4556
- return JBondClientEnv2;
4557
- })(JBondClientEnv || {});
4558
4561
  var SessionStatus = /* @__PURE__ */ ((SessionStatus2) => {
4559
4562
  SessionStatus2[SessionStatus2["Live"] = 0] = "Live";
4560
4563
  SessionStatus2[SessionStatus2["Finished"] = 1] = "Finished";
@@ -4566,26 +4569,13 @@ var BondType = /* @__PURE__ */ ((BondType2) => {
4566
4569
  return BondType2;
4567
4570
  })(BondType || {});
4568
4571
  var BondTransactionType = /* @__PURE__ */ ((BondTransactionType2) => {
4569
- BondTransactionType2["Registered"] = "registered";
4570
4572
  BondTransactionType2["Deposit"] = "deposit";
4571
4573
  BondTransactionType2["Withdrawal"] = "withdrawal";
4572
4574
  BondTransactionType2["Compensation"] = "compensation";
4573
4575
  return BondTransactionType2;
4574
4576
  })(BondTransactionType || {});
4575
4577
 
4576
- // src/constants.ts
4577
- var GLOBAL_STATE_SEED = "global_state";
4578
- var BOND_STATE_SEED = "bond_state";
4579
- var VALIDATOR_BOND_SEED = "validator_bond";
4580
- var STANDARD_BOND_SEED = "standard";
4581
- var CROWDFUNDING_BOND_SEED = "crowdfunding";
4582
- var ENV_PROGRAM_ID = {
4583
- ["dev" /* DEV */]: "Fo17edWRJewZNTRibgx9iTfjywCW6dzS81VwBLmPFVq1",
4584
- ["stage" /* STAGE */]: "Fo17edWRJewZNTRibgx9iTfjywCW6dzS81VwBLmPFVq1",
4585
- ["prod" /* PROD */]: "BondQ7KqZreTcW2UbeTNDcLCJQ3aXAtLn2Fm6ftaJDU"
4586
- };
4587
- var JSOL_MINT = new PublicKey("7Q2afV64in6N6SeZsAAB81TJzwDoD6zpqmHkzi9Dcavn");
4588
- var JPOOL_POOL_ADDRESS = new PublicKey("CtMyWsrUtAwXWiGr9WjHT5fC3p3fgV8cyGpLTo2LJzG1");
4578
+ // src/history.ts
4589
4579
  var { bs58 } = utils.bytes;
4590
4580
  var HistoryManager = class {
4591
4581
  constructor(client) {
@@ -4649,7 +4639,7 @@ var HistoryManager = class {
4649
4639
  continue;
4650
4640
  }
4651
4641
  const { type, amount } = this.decodeInstructionData(ix.data);
4652
- if (!type || amount <= 0 && type !== "registered" /* Registered */) {
4642
+ if (!type || amount <= 0) {
4653
4643
  continue;
4654
4644
  }
4655
4645
  history.push({
@@ -4669,6 +4659,7 @@ var HistoryManager = class {
4669
4659
  before: options?.before,
4670
4660
  until: options?.until
4671
4661
  }, "confirmed");
4662
+ console.log(sigs);
4672
4663
  return sigs.filter((s) => !s.err);
4673
4664
  }
4674
4665
  async fetchParsedTransactions(sigs, batchSize = 100) {
@@ -4690,7 +4681,7 @@ var HistoryManager = class {
4690
4681
  }
4691
4682
  const ix = this.program.idl.instructions.find((i) => i.name === name);
4692
4683
  if (!ix || !Array.isArray(ix.discriminator)) {
4693
- return new Uint8Array();
4684
+ throw new Error(`Discriminator not found for instruction ${name}`);
4694
4685
  }
4695
4686
  const disc = new Uint8Array(ix.discriminator);
4696
4687
  this.discCache.set(name, disc);
@@ -4698,45 +4689,47 @@ var HistoryManager = class {
4698
4689
  }
4699
4690
  decodeInstructionData(data) {
4700
4691
  if (!data) {
4701
- return { type: null, amount: 0 };
4692
+ return {
4693
+ type: null,
4694
+ amount: 0
4695
+ };
4702
4696
  }
4703
4697
  try {
4704
4698
  const buf = bs58.decode(data);
4705
- let type = null;
4706
- const disc = buf.subarray(0, 8);
4707
- const amountBytes = buf.subarray(8, 16);
4708
- let amount = 0;
4709
- let noAmount = false;
4710
- try {
4711
- amount = new BN(amountBytes, "le").toNumber() / LAMPORTS_PER_SOL;
4712
- } catch {
4713
- noAmount = true;
4714
- }
4715
- const bondRegisterDiscriminator = this.getInstructionDiscriminator("bondRegister");
4716
- const bondTopUpDiscriminator = this.getInstructionDiscriminator("bondTopUp");
4717
- const bondClaimDiscriminator = this.getInstructionDiscriminator("bondClaim");
4718
- const bondWithdrawDiscriminator = this.getInstructionDiscriminator("bondWithdraw");
4719
- if (Buffer.from(disc).equals(Buffer.from(bondRegisterDiscriminator))) {
4720
- type = "registered" /* Registered */;
4721
- } else if (!noAmount) {
4722
- if (Buffer.from(disc).equals(Buffer.from(bondTopUpDiscriminator))) {
4723
- type = "deposit" /* Deposit */;
4724
- } else if (Buffer.from(disc).equals(Buffer.from(bondClaimDiscriminator))) {
4725
- type = "compensation" /* Compensation */;
4726
- } else if (Buffer.from(disc).equals(Buffer.from(bondWithdrawDiscriminator))) {
4727
- type = "withdrawal" /* Withdrawal */;
4728
- }
4729
- }
4730
- return { type, amount };
4731
- } catch (error) {
4732
- console.error("Failed to decode instruction data:", error);
4699
+ if (buf.length < 16) {
4700
+ return {
4701
+ type: null,
4702
+ amount: 0
4703
+ };
4704
+ }
4705
+ const disc = buf.slice(0, 8);
4706
+ const amountBytes = buf.slice(8, 16);
4707
+ const amount = new BN(amountBytes, "le").toNumber() / LAMPORTS_PER_SOL;
4708
+ const perfRegister = this.getInstructionDiscriminator("perfBondRegister");
4709
+ const perfTopUp = this.getInstructionDiscriminator("perfBondTopUp");
4710
+ const perfClaim = this.getInstructionDiscriminator("perfBondClaim");
4711
+ const perfWithdraw = this.getInstructionDiscriminator("perfBondWithdraw");
4712
+ if (Buffer.from(disc).equals(Buffer.from(perfRegister))) {
4713
+ return { type: "deposit" /* Deposit */, amount };
4714
+ }
4715
+ if (Buffer.from(disc).equals(Buffer.from(perfTopUp))) {
4716
+ return { type: "deposit" /* Deposit */, amount };
4717
+ }
4718
+ if (Buffer.from(disc).equals(Buffer.from(perfClaim))) {
4719
+ return { type: "compensation" /* Compensation */, amount };
4720
+ }
4721
+ if (Buffer.from(disc).equals(Buffer.from(perfWithdraw))) {
4722
+ return { type: "withdrawal" /* Withdrawal */, amount };
4723
+ }
4724
+ return { type: null, amount: 0 };
4725
+ } catch {
4733
4726
  return { type: null, amount: 0 };
4734
4727
  }
4735
4728
  }
4736
4729
  };
4737
4730
  function slotToEpoch(slot, cluster) {
4738
- const isDevnet = cluster?.includes("devnet") || cluster?.includes("localnet");
4739
- const slotsPerEpoch = isDevnet ? 4e5 : 432e3;
4731
+ const isMainnet = !cluster || cluster === "mainnet-beta";
4732
+ const slotsPerEpoch = isMainnet ? 432e3 : 4e5;
4740
4733
  return Math.floor(slot / slotsPerEpoch);
4741
4734
  }
4742
4735
 
@@ -4745,7 +4738,7 @@ var jbond_default = {
4745
4738
  address: "BondQ7KqZreTcW2UbeTNDcLCJQ3aXAtLn2Fm6ftaJDU",
4746
4739
  metadata: {
4747
4740
  name: "jbond",
4748
- version: "0.2.6",
4741
+ version: "0.2.1",
4749
4742
  spec: "0.1.0",
4750
4743
  description: "Jpool Bond Program"
4751
4744
  },
@@ -4820,10 +4813,7 @@ var jbond_default = {
4820
4813
  },
4821
4814
  {
4822
4815
  name: "authority",
4823
- signer: true,
4824
- relations: [
4825
- "bond_state"
4826
- ]
4816
+ signer: true
4827
4817
  },
4828
4818
  {
4829
4819
  name: "bond_token_account",
@@ -4994,84 +4984,6 @@ var jbond_default = {
4994
4984
  }
4995
4985
  ]
4996
4986
  },
4997
- {
4998
- name: "bond_lock_funds",
4999
- docs: [
5000
- "Locks funds immediately, making them unavailable for withdrawal.",
5001
- "The locked amount takes effect immediately in the current epoch.",
5002
- "",
5003
- "# Errors",
5004
- "Fails if the bond type is not lockable, if the amount is zero,",
5005
- "or if there are insufficient available funds."
5006
- ],
5007
- discriminator: [
5008
- 117,
5009
- 166,
5010
- 195,
5011
- 99,
5012
- 103,
5013
- 45,
5014
- 195,
5015
- 2
5016
- ],
5017
- accounts: [
5018
- {
5019
- name: "bond_state",
5020
- writable: true
5021
- },
5022
- {
5023
- name: "validator_bond",
5024
- writable: true,
5025
- pda: {
5026
- seeds: [
5027
- {
5028
- kind: "const",
5029
- value: [
5030
- 118,
5031
- 97,
5032
- 108,
5033
- 105,
5034
- 100,
5035
- 97,
5036
- 116,
5037
- 111,
5038
- 114,
5039
- 95,
5040
- 98,
5041
- 111,
5042
- 110,
5043
- 100
5044
- ]
5045
- },
5046
- {
5047
- kind: "account",
5048
- path: "bond_state"
5049
- },
5050
- {
5051
- kind: "account",
5052
- path: "validator_bond.vote_account",
5053
- account: "ValidatorBond"
5054
- }
5055
- ]
5056
- }
5057
- },
5058
- {
5059
- name: "bond_token_account",
5060
- optional: true
5061
- },
5062
- {
5063
- name: "payer",
5064
- writable: true,
5065
- signer: true
5066
- }
5067
- ],
5068
- args: [
5069
- {
5070
- name: "amount",
5071
- type: "u64"
5072
- }
5073
- ]
5074
- },
5075
4987
  {
5076
4988
  name: "bond_register",
5077
4989
  docs: [
@@ -5099,34 +5011,7 @@ var jbond_default = {
5099
5011
  accounts: [
5100
5012
  {
5101
5013
  name: "bond_state",
5102
- writable: true,
5103
- pda: {
5104
- seeds: [
5105
- {
5106
- kind: "const",
5107
- value: [
5108
- 98,
5109
- 111,
5110
- 110,
5111
- 100,
5112
- 95,
5113
- 115,
5114
- 116,
5115
- 97,
5116
- 116,
5117
- 101
5118
- ]
5119
- },
5120
- {
5121
- kind: "arg",
5122
- path: "bond_type"
5123
- },
5124
- {
5125
- kind: "arg",
5126
- path: "bond_name"
5127
- }
5128
- ]
5129
- }
5014
+ writable: true
5130
5015
  },
5131
5016
  {
5132
5017
  name: "validator_bond",
@@ -5211,112 +5096,6 @@ var jbond_default = {
5211
5096
  }
5212
5097
  ]
5213
5098
  },
5214
- {
5215
- name: "bond_release_funds",
5216
- docs: [
5217
- "Queues funds for unlock in the next epoch.",
5218
- "The unlock is delayed by one epoch for security - funds remain locked",
5219
- "in the current epoch but will be available after epoch transition.",
5220
- "",
5221
- "# Errors",
5222
- "Fails if the bond type is not lockable, if the amount is zero,",
5223
- "or if the amount exceeds currently locked funds."
5224
- ],
5225
- discriminator: [
5226
- 184,
5227
- 224,
5228
- 14,
5229
- 24,
5230
- 76,
5231
- 14,
5232
- 226,
5233
- 190
5234
- ],
5235
- accounts: [
5236
- {
5237
- name: "bond_state",
5238
- writable: true
5239
- },
5240
- {
5241
- name: "validator_bond",
5242
- writable: true,
5243
- pda: {
5244
- seeds: [
5245
- {
5246
- kind: "const",
5247
- value: [
5248
- 118,
5249
- 97,
5250
- 108,
5251
- 105,
5252
- 100,
5253
- 97,
5254
- 116,
5255
- 111,
5256
- 114,
5257
- 95,
5258
- 98,
5259
- 111,
5260
- 110,
5261
- 100
5262
- ]
5263
- },
5264
- {
5265
- kind: "account",
5266
- path: "bond_state"
5267
- },
5268
- {
5269
- kind: "account",
5270
- path: "validator_bond.vote_account",
5271
- account: "ValidatorBond"
5272
- }
5273
- ]
5274
- }
5275
- },
5276
- {
5277
- name: "payer",
5278
- writable: true,
5279
- signer: true
5280
- }
5281
- ],
5282
- args: [
5283
- {
5284
- name: "amount",
5285
- type: "u64"
5286
- }
5287
- ]
5288
- },
5289
- {
5290
- name: "bond_remove",
5291
- docs: [
5292
- "Removes bond state"
5293
- ],
5294
- discriminator: [
5295
- 121,
5296
- 112,
5297
- 246,
5298
- 177,
5299
- 181,
5300
- 8,
5301
- 18,
5302
- 220
5303
- ],
5304
- accounts: [
5305
- {
5306
- name: "bond_state",
5307
- writable: true
5308
- },
5309
- {
5310
- name: "authority",
5311
- writable: true,
5312
- signer: true,
5313
- relations: [
5314
- "bond_state"
5315
- ]
5316
- }
5317
- ],
5318
- args: []
5319
- },
5320
5099
  {
5321
5100
  name: "bond_set_withdraw_authority",
5322
5101
  docs: [
@@ -5607,127 +5386,30 @@ var jbond_default = {
5607
5386
  ]
5608
5387
  },
5609
5388
  {
5610
- name: "close_validator_bond",
5611
- docs: [
5612
- "Closes a validator bond account and transfers remaining balance to the rent receiver.",
5613
- "",
5614
- "# Security",
5615
- "- Only the validator identity or withdrawal authority can close the bond",
5616
- "- All funds must be unlocked (locked == 0 and pending_unlock == 0)",
5617
- "- For token collateral, closes the associated token account as well",
5618
- "- Decrements the bond state's total_members counter",
5619
- "",
5620
- "# Errors",
5621
- "Fails if the signer is not authorized, if funds are still locked,",
5622
- "or if the rent receiver is not the identity or withdrawal authority."
5623
- ],
5389
+ name: "global_configure",
5624
5390
  discriminator: [
5625
- 51,
5626
- 235,
5627
- 111,
5628
- 163,
5629
- 176,
5630
- 239,
5631
- 95,
5632
- 122
5391
+ 67,
5392
+ 142,
5393
+ 164,
5394
+ 250,
5395
+ 90,
5396
+ 233,
5397
+ 144,
5398
+ 187
5633
5399
  ],
5634
5400
  accounts: [
5635
5401
  {
5636
- name: "bond_state",
5637
- writable: true
5638
- },
5639
- {
5640
- name: "validator_bond",
5402
+ name: "global_state",
5641
5403
  writable: true,
5642
5404
  pda: {
5643
5405
  seeds: [
5644
5406
  {
5645
5407
  kind: "const",
5646
5408
  value: [
5647
- 118,
5648
- 97,
5649
- 108,
5650
- 105,
5651
- 100,
5652
- 97,
5653
- 116,
5654
- 111,
5655
- 114,
5656
- 95,
5657
- 98,
5658
- 111,
5659
- 110,
5660
- 100
5661
- ]
5662
- },
5663
- {
5664
- kind: "account",
5665
- path: "bond_state"
5666
- },
5667
- {
5668
- kind: "account",
5669
- path: "validator_bond.vote_account",
5670
- account: "ValidatorBond"
5671
- }
5672
- ]
5673
- }
5674
- },
5675
- {
5676
- name: "authority",
5677
- writable: true,
5678
- signer: true
5679
- },
5680
- {
5681
- name: "rent_receiver",
5682
- writable: true
5683
- },
5684
- {
5685
- name: "bond_token_account",
5686
- writable: true,
5687
- optional: true
5688
- },
5689
- {
5690
- name: "destination_token_account",
5691
- writable: true,
5692
- optional: true
5693
- },
5694
- {
5695
- name: "system_program",
5696
- address: "11111111111111111111111111111111"
5697
- },
5698
- {
5699
- name: "token_program",
5700
- optional: true,
5701
- address: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
5702
- }
5703
- ],
5704
- args: []
5705
- },
5706
- {
5707
- name: "global_configure",
5708
- discriminator: [
5709
- 67,
5710
- 142,
5711
- 164,
5712
- 250,
5713
- 90,
5714
- 233,
5715
- 144,
5716
- 187
5717
- ],
5718
- accounts: [
5719
- {
5720
- name: "global_state",
5721
- writable: true,
5722
- pda: {
5723
- seeds: [
5724
- {
5725
- kind: "const",
5726
- value: [
5727
- 103,
5728
- 108,
5729
- 111,
5730
- 98,
5409
+ 103,
5410
+ 108,
5411
+ 111,
5412
+ 98,
5731
5413
  97,
5732
5414
  108,
5733
5415
  95,
@@ -5810,6 +5492,80 @@ var jbond_default = {
5810
5492
  ],
5811
5493
  args: []
5812
5494
  },
5495
+ {
5496
+ name: "migrate_bond",
5497
+ docs: [
5498
+ "Migrates validator bond data from legacy structure to new structure."
5499
+ ],
5500
+ discriminator: [
5501
+ 107,
5502
+ 119,
5503
+ 59,
5504
+ 110,
5505
+ 76,
5506
+ 18,
5507
+ 37,
5508
+ 163
5509
+ ],
5510
+ accounts: [
5511
+ {
5512
+ name: "bond_state",
5513
+ writable: true
5514
+ },
5515
+ {
5516
+ name: "legacy_validator_bond",
5517
+ writable: true
5518
+ },
5519
+ {
5520
+ name: "legacy_vote_account"
5521
+ },
5522
+ {
5523
+ name: "validator_bond",
5524
+ writable: true,
5525
+ pda: {
5526
+ seeds: [
5527
+ {
5528
+ kind: "const",
5529
+ value: [
5530
+ 118,
5531
+ 97,
5532
+ 108,
5533
+ 105,
5534
+ 100,
5535
+ 97,
5536
+ 116,
5537
+ 111,
5538
+ 114,
5539
+ 95,
5540
+ 98,
5541
+ 111,
5542
+ 110,
5543
+ 100
5544
+ ]
5545
+ },
5546
+ {
5547
+ kind: "account",
5548
+ path: "bond_state"
5549
+ },
5550
+ {
5551
+ kind: "account",
5552
+ path: "legacy_vote_account"
5553
+ }
5554
+ ]
5555
+ }
5556
+ },
5557
+ {
5558
+ name: "creator",
5559
+ writable: true,
5560
+ signer: true
5561
+ },
5562
+ {
5563
+ name: "system_program",
5564
+ address: "11111111111111111111111111111111"
5565
+ }
5566
+ ],
5567
+ args: []
5568
+ },
5813
5569
  {
5814
5570
  name: "session_finish",
5815
5571
  docs: [
@@ -5920,19 +5676,6 @@ var jbond_default = {
5920
5676
  }
5921
5677
  ],
5922
5678
  events: [
5923
- {
5924
- name: "BondConfigured",
5925
- discriminator: [
5926
- 141,
5927
- 252,
5928
- 43,
5929
- 54,
5930
- 45,
5931
- 157,
5932
- 81,
5933
- 14
5934
- ]
5935
- },
5936
5679
  {
5937
5680
  name: "BondFinished",
5938
5681
  discriminator: [
@@ -5946,32 +5689,6 @@ var jbond_default = {
5946
5689
  191
5947
5690
  ]
5948
5691
  },
5949
- {
5950
- name: "BondInitialized",
5951
- discriminator: [
5952
- 73,
5953
- 68,
5954
- 161,
5955
- 2,
5956
- 214,
5957
- 12,
5958
- 169,
5959
- 53
5960
- ]
5961
- },
5962
- {
5963
- name: "BondRemoved",
5964
- discriminator: [
5965
- 177,
5966
- 200,
5967
- 116,
5968
- 123,
5969
- 1,
5970
- 244,
5971
- 161,
5972
- 240
5973
- ]
5974
- },
5975
5692
  {
5976
5693
  name: "BondStarted",
5977
5694
  discriminator: [
@@ -5986,16 +5703,16 @@ var jbond_default = {
5986
5703
  ]
5987
5704
  },
5988
5705
  {
5989
- name: "CollateralDeposited",
5706
+ name: "CollateralToppedUp",
5990
5707
  discriminator: [
5991
- 244,
5992
- 62,
5993
- 77,
5994
- 11,
5708
+ 189,
5709
+ 137,
5710
+ 204,
5711
+ 58,
5995
5712
  135,
5996
- 112,
5997
- 61,
5998
- 96
5713
+ 182,
5714
+ 19,
5715
+ 176
5999
5716
  ]
6000
5717
  },
6001
5718
  {
@@ -6011,19 +5728,6 @@ var jbond_default = {
6011
5728
  82
6012
5729
  ]
6013
5730
  },
6014
- {
6015
- name: "CompensationBurned",
6016
- discriminator: [
6017
- 177,
6018
- 198,
6019
- 90,
6020
- 188,
6021
- 47,
6022
- 95,
6023
- 20,
6024
- 174
6025
- ]
6026
- },
6027
5731
  {
6028
5732
  name: "CompensationClaimed",
6029
5733
  discriminator: [
@@ -6037,45 +5741,6 @@ var jbond_default = {
6037
5741
  32
6038
5742
  ]
6039
5743
  },
6040
- {
6041
- name: "FundsLocked",
6042
- discriminator: [
6043
- 227,
6044
- 93,
6045
- 162,
6046
- 69,
6047
- 181,
6048
- 4,
6049
- 71,
6050
- 157
6051
- ]
6052
- },
6053
- {
6054
- name: "FundsUnlocked",
6055
- discriminator: [
6056
- 11,
6057
- 240,
6058
- 184,
6059
- 107,
6060
- 86,
6061
- 187,
6062
- 196,
6063
- 128
6064
- ]
6065
- },
6066
- {
6067
- name: "ValidatorBondClosed",
6068
- discriminator: [
6069
- 71,
6070
- 54,
6071
- 59,
6072
- 241,
6073
- 91,
6074
- 35,
6075
- 123,
6076
- 123
6077
- ]
6078
- },
6079
5744
  {
6080
5745
  name: "ValidatorRegistered",
6081
5746
  discriminator: [
@@ -6088,46 +5753,33 @@ var jbond_default = {
6088
5753
  95,
6089
5754
  72
6090
5755
  ]
6091
- },
6092
- {
6093
- name: "WithdrawAuthorityChanged",
6094
- discriminator: [
6095
- 91,
6096
- 149,
6097
- 224,
6098
- 49,
6099
- 187,
6100
- 48,
6101
- 190,
6102
- 100
6103
- ]
6104
5756
  }
6105
5757
  ],
6106
5758
  errors: [
6107
5759
  {
6108
5760
  code: 6e3,
6109
- name: "Forbidden",
6110
- msg: "Forbidden"
5761
+ name: "InvalidVoteAccount",
5762
+ msg: "Invalid vote account"
6111
5763
  },
6112
5764
  {
6113
5765
  code: 6001,
6114
- name: "Unauthorized",
6115
- msg: "Unauthorized"
5766
+ name: "IdentityMismatch",
5767
+ msg: "Identity does not match vote account"
6116
5768
  },
6117
5769
  {
6118
5770
  code: 6002,
6119
- name: "InvalidVoteAccount",
6120
- msg: "Invalid vote account"
5771
+ name: "InsufficientCollateral",
5772
+ msg: "Insufficient collateral amount"
6121
5773
  },
6122
5774
  {
6123
5775
  code: 6003,
6124
- name: "IdentityMismatch",
6125
- msg: "Vote account identity mismatch"
5776
+ name: "TargetAprTooLow",
5777
+ msg: "Target APR is below minimum threshold"
6126
5778
  },
6127
5779
  {
6128
5780
  code: 6004,
6129
- name: "InvalidMint",
6130
- msg: "Invalid mint address"
5781
+ name: "MathOverflow",
5782
+ msg: "Math overflow"
6131
5783
  },
6132
5784
  {
6133
5785
  code: 6005,
@@ -6136,53 +5788,93 @@ var jbond_default = {
6136
5788
  },
6137
5789
  {
6138
5790
  code: 6006,
6139
- name: "InvalidArgument",
6140
- msg: "Invalid argument"
5791
+ name: "ValidatorNotActive",
5792
+ msg: "Validator bond account is not active"
6141
5793
  },
6142
5794
  {
6143
5795
  code: 6007,
6144
- name: "InsufficientBalance",
6145
- msg: "Insufficient balance"
5796
+ name: "ValidatorAlreadyRegistered",
5797
+ msg: "Validator already registered"
6146
5798
  },
6147
5799
  {
6148
5800
  code: 6008,
6149
- name: "MathOverflow",
6150
- msg: "Math overflow"
5801
+ name: "Unauthorized",
5802
+ msg: "Unauthorized"
6151
5803
  },
6152
5804
  {
6153
5805
  code: 6009,
6154
- name: "Overflow",
6155
- msg: "Arithmetic overflow"
5806
+ name: "WithdrawalLockedNearEpochEnd",
5807
+ msg: "Withdrawals are locked near the end of the epoch."
6156
5808
  },
6157
5809
  {
6158
5810
  code: 6010,
6159
- name: "BondNotRemovable",
6160
- msg: "Bond cannot be removed"
5811
+ name: "MissingTokenAccounts",
5812
+ msg: "Missing token accounts for token collateral type"
6161
5813
  },
6162
5814
  {
6163
5815
  code: 6011,
6164
- name: "MissingTokenAccounts",
6165
- msg: "Missing token accounts"
5816
+ name: "MissingTokenProgram",
5817
+ msg: "Token program is required for token collateral type"
6166
5818
  },
6167
5819
  {
6168
5820
  code: 6012,
6169
- name: "MissingTokenProgram",
6170
- msg: "Missing token program"
5821
+ name: "InvalidMint",
5822
+ msg: "Invalid mint for token collateral type"
6171
5823
  },
6172
5824
  {
6173
5825
  code: 6013,
6174
5826
  name: "MissingStakeAccount",
6175
- msg: "Missing stake account"
5827
+ msg: "Stake account is required for stake account collateral type"
6176
5828
  },
6177
5829
  {
6178
5830
  code: 6014,
6179
5831
  name: "MissingStakeProgram",
6180
- msg: "Missing stake program"
5832
+ msg: "Stake program is required for stake account collateral type"
6181
5833
  },
6182
5834
  {
6183
5835
  code: 6015,
5836
+ name: "BondFinished",
5837
+ msg: "Bond is finished (deposits/withdrawals disabled)"
5838
+ },
5839
+ {
5840
+ code: 6016,
5841
+ name: "BondNotFinished",
5842
+ msg: "Bond is not finished"
5843
+ },
5844
+ {
5845
+ code: 6017,
6184
5846
  name: "UnsupportedCollateralType",
6185
5847
  msg: "Unsupported collateral type"
5848
+ },
5849
+ {
5850
+ code: 6018,
5851
+ name: "Overflow",
5852
+ msg: "Math operation overflowed"
5853
+ },
5854
+ {
5855
+ code: 6019,
5856
+ name: "IncorrectBondType",
5857
+ msg: "Incorrect bond type for this operation"
5858
+ },
5859
+ {
5860
+ code: 6020,
5861
+ name: "BondNotFinishable",
5862
+ msg: "This bond type can't be finished"
5863
+ },
5864
+ {
5865
+ code: 6021,
5866
+ name: "InvalidArgument",
5867
+ msg: "Invalid argument provided"
5868
+ },
5869
+ {
5870
+ code: 6022,
5871
+ name: "BondNotDisabled",
5872
+ msg: "Bond is not disabled"
5873
+ },
5874
+ {
5875
+ code: 6023,
5876
+ name: "BondNotEnabled",
5877
+ msg: "Bond is not enabled"
6186
5878
  }
6187
5879
  ],
6188
5880
  types: [
@@ -6216,26 +5908,6 @@ var jbond_default = {
6216
5908
  ]
6217
5909
  }
6218
5910
  },
6219
- {
6220
- name: "BondConfigured",
6221
- type: {
6222
- kind: "struct",
6223
- fields: [
6224
- {
6225
- name: "bond",
6226
- type: "pubkey"
6227
- },
6228
- {
6229
- name: "authority",
6230
- type: "pubkey"
6231
- },
6232
- {
6233
- name: "timestamp",
6234
- type: "i64"
6235
- }
6236
- ]
6237
- }
6238
- },
6239
5911
  {
6240
5912
  name: "BondFinished",
6241
5913
  type: {
@@ -6291,46 +5963,6 @@ var jbond_default = {
6291
5963
  ]
6292
5964
  }
6293
5965
  },
6294
- {
6295
- name: "BondInitialized",
6296
- type: {
6297
- kind: "struct",
6298
- fields: [
6299
- {
6300
- name: "bond",
6301
- type: "pubkey"
6302
- },
6303
- {
6304
- name: "authority",
6305
- type: "pubkey"
6306
- },
6307
- {
6308
- name: "timestamp",
6309
- type: "i64"
6310
- }
6311
- ]
6312
- }
6313
- },
6314
- {
6315
- name: "BondRemoved",
6316
- type: {
6317
- kind: "struct",
6318
- fields: [
6319
- {
6320
- name: "bond",
6321
- type: "pubkey"
6322
- },
6323
- {
6324
- name: "authority",
6325
- type: "pubkey"
6326
- },
6327
- {
6328
- name: "timestamp",
6329
- type: "i64"
6330
- }
6331
- ]
6332
- }
6333
- },
6334
5966
  {
6335
5967
  name: "BondStarted",
6336
5968
  type: {
@@ -6484,7 +6116,7 @@ var jbond_default = {
6484
6116
  }
6485
6117
  },
6486
6118
  {
6487
- name: "CollateralDeposited",
6119
+ name: "CollateralToppedUp",
6488
6120
  type: {
6489
6121
  kind: "struct",
6490
6122
  fields: [
@@ -6554,34 +6186,6 @@ var jbond_default = {
6554
6186
  ]
6555
6187
  }
6556
6188
  },
6557
- {
6558
- name: "CompensationBurned",
6559
- type: {
6560
- kind: "struct",
6561
- fields: [
6562
- {
6563
- name: "validator",
6564
- type: "pubkey"
6565
- },
6566
- {
6567
- name: "amount",
6568
- type: "u64"
6569
- },
6570
- {
6571
- name: "post_balance",
6572
- type: "u64"
6573
- },
6574
- {
6575
- name: "epoch",
6576
- type: "u64"
6577
- },
6578
- {
6579
- name: "timestamp",
6580
- type: "i64"
6581
- }
6582
- ]
6583
- }
6584
- },
6585
6189
  {
6586
6190
  name: "CompensationClaimed",
6587
6191
  type: {
@@ -6610,54 +6214,6 @@ var jbond_default = {
6610
6214
  ]
6611
6215
  }
6612
6216
  },
6613
- {
6614
- name: "FundsLocked",
6615
- type: {
6616
- kind: "struct",
6617
- fields: [
6618
- {
6619
- name: "validator",
6620
- type: "pubkey"
6621
- },
6622
- {
6623
- name: "amount",
6624
- type: "u64"
6625
- },
6626
- {
6627
- name: "total_locked",
6628
- type: "u64"
6629
- },
6630
- {
6631
- name: "epoch",
6632
- type: "u64"
6633
- }
6634
- ]
6635
- }
6636
- },
6637
- {
6638
- name: "FundsUnlocked",
6639
- type: {
6640
- kind: "struct",
6641
- fields: [
6642
- {
6643
- name: "validator",
6644
- type: "pubkey"
6645
- },
6646
- {
6647
- name: "amount",
6648
- type: "u64"
6649
- },
6650
- {
6651
- name: "pending_unlock",
6652
- type: "u64"
6653
- },
6654
- {
6655
- name: "epoch",
6656
- type: "u64"
6657
- }
6658
- ]
6659
- }
6660
- },
6661
6217
  {
6662
6218
  name: "GlobalConfigureData",
6663
6219
  type: {
@@ -6678,71 +6234,11 @@ var jbond_default = {
6678
6234
  kind: "struct",
6679
6235
  fields: [
6680
6236
  {
6681
- name: "authority",
6682
- docs: [
6683
- "The authority allowed to manage the program"
6684
- ],
6685
- type: "pubkey"
6686
- },
6687
- {
6688
- name: "bump",
6689
- docs: [
6690
- "Bump for the PDA"
6691
- ],
6692
- type: "u8"
6693
- }
6694
- ]
6695
- }
6696
- },
6697
- {
6698
- name: "ValidatorBond",
6699
- type: {
6700
- kind: "struct",
6701
- fields: [
6702
- {
6703
- name: "state",
6704
- docs: [
6705
- "The bond state address"
6706
- ],
6707
- type: "pubkey"
6708
- },
6709
- {
6710
- name: "identity",
6711
- docs: [
6712
- "Validator identity pubkey"
6713
- ],
6714
- type: "pubkey"
6715
- },
6716
- {
6717
- name: "vote_account",
6718
- docs: [
6719
- "Validator vote account pubkey"
6720
- ],
6721
- type: "pubkey"
6722
- },
6723
- {
6724
- name: "creator",
6725
- docs: [
6726
- "The bond creator pubkey"
6727
- ],
6728
- type: "pubkey"
6729
- },
6730
- {
6731
- name: "withdrawal_authority",
6732
- docs: [
6733
- "Optional withdrawal authority pubkey (if set, can withdraw on behalf of the validator,",
6734
- "otherwise only identity can withdraw)"
6735
- ],
6736
- type: {
6737
- option: "pubkey"
6738
- }
6739
- },
6740
- {
6741
- name: "created_at",
6237
+ name: "authority",
6742
6238
  docs: [
6743
- "Bond creation timestamp"
6239
+ "The authority allowed to manage the program"
6744
6240
  ],
6745
- type: "i64"
6241
+ type: "pubkey"
6746
6242
  },
6747
6243
  {
6748
6244
  name: "bump",
@@ -6750,38 +6246,21 @@ var jbond_default = {
6750
6246
  "Bump for the PDA"
6751
6247
  ],
6752
6248
  type: "u8"
6753
- },
6754
- {
6755
- name: "locked",
6756
- docs: [
6757
- "Amount of collateral currently locked (unavailable for withdrawal)"
6758
- ],
6759
- type: "u64"
6760
- },
6761
- {
6762
- name: "pending_unlock",
6763
- docs: [
6764
- "Pending unlock amount (will reduce `locked` next epoch)"
6765
- ],
6766
- type: "u64"
6767
- },
6768
- {
6769
- name: "pending_unlock_epoch",
6770
- docs: [
6771
- "Epoch when pending_unlock was queued"
6772
- ],
6773
- type: "u64"
6774
6249
  }
6775
6250
  ]
6776
6251
  }
6777
6252
  },
6778
6253
  {
6779
- name: "ValidatorBondClosed",
6254
+ name: "ValidatorBond",
6780
6255
  type: {
6781
6256
  kind: "struct",
6782
6257
  fields: [
6783
6258
  {
6784
- name: "validator",
6259
+ name: "state",
6260
+ type: "pubkey"
6261
+ },
6262
+ {
6263
+ name: "identity",
6785
6264
  type: "pubkey"
6786
6265
  },
6787
6266
  {
@@ -6789,16 +6268,22 @@ var jbond_default = {
6789
6268
  type: "pubkey"
6790
6269
  },
6791
6270
  {
6792
- name: "rent_receiver",
6271
+ name: "creator",
6793
6272
  type: "pubkey"
6794
6273
  },
6795
6274
  {
6796
- name: "final_balance",
6797
- type: "u64"
6275
+ name: "withdrawal_authority",
6276
+ type: {
6277
+ option: "pubkey"
6278
+ }
6798
6279
  },
6799
6280
  {
6800
- name: "timestamp",
6281
+ name: "created_at",
6801
6282
  type: "i64"
6283
+ },
6284
+ {
6285
+ name: "bump",
6286
+ type: "u8"
6802
6287
  }
6803
6288
  ]
6804
6289
  }
@@ -6809,7 +6294,7 @@ var jbond_default = {
6809
6294
  kind: "struct",
6810
6295
  fields: [
6811
6296
  {
6812
- name: "validator",
6297
+ name: "identity",
6813
6298
  type: "pubkey"
6814
6299
  },
6815
6300
  {
@@ -6822,30 +6307,6 @@ var jbond_default = {
6822
6307
  }
6823
6308
  ]
6824
6309
  }
6825
- },
6826
- {
6827
- name: "WithdrawAuthorityChanged",
6828
- type: {
6829
- kind: "struct",
6830
- fields: [
6831
- {
6832
- name: "validator",
6833
- type: "pubkey"
6834
- },
6835
- {
6836
- name: "old_authority",
6837
- type: {
6838
- option: "pubkey"
6839
- }
6840
- },
6841
- {
6842
- name: "new_authority",
6843
- type: {
6844
- option: "pubkey"
6845
- }
6846
- }
6847
- ]
6848
- }
6849
6310
  }
6850
6311
  ],
6851
6312
  constants: [
@@ -6904,22 +6365,6 @@ function bondTypeToAnchor(type, payload) {
6904
6365
  function anchorToBondType(variant) {
6905
6366
  return fromAnchor(variant);
6906
6367
  }
6907
- async function getStakePoolAccountData(connection, stakePoolAddress) {
6908
- const stakePoolAccount = await getStakePoolAccount(connection, stakePoolAddress);
6909
- if (!stakePoolAccount) {
6910
- throw new Error(`Stake pool account not found: ${stakePoolAddress.toBase58()}`);
6911
- }
6912
- return stakePoolAccount.account.data;
6913
- }
6914
- function lamportsToPoolTokenLamports(lamports, stakePool) {
6915
- if (stakePool.totalLamports.isZero()) {
6916
- throw new Error("Stake pool has zero total lamports");
6917
- }
6918
- if (stakePool.poolTokenSupply.isZero()) {
6919
- throw new Error("Stake pool has zero pool token supply");
6920
- }
6921
- return lamports.mul(stakePool.poolTokenSupply).div(stakePool.totalLamports);
6922
- }
6923
6368
 
6924
6369
  // src/utils/wallet.ts
6925
6370
  function isVersionedTransaction(tx) {
@@ -6952,20 +6397,6 @@ var NodeWallet = class {
6952
6397
  }
6953
6398
  };
6954
6399
  var SOL_DECIMALS = Math.log10(LAMPORTS_PER_SOL);
6955
- function lamportsToSol(lamports) {
6956
- if (typeof lamports === "number") {
6957
- return Math.abs(lamports) / LAMPORTS_PER_SOL;
6958
- }
6959
- let signMultiplier = 1;
6960
- if (lamports.isNeg()) {
6961
- signMultiplier = -1;
6962
- }
6963
- const absLamports = lamports.abs();
6964
- const lamportsString = absLamports.toString(10).padStart(10, "0");
6965
- const splitIndex = lamportsString.length - SOL_DECIMALS;
6966
- const solString = `${lamportsString.slice(0, splitIndex)}.${lamportsString.slice(splitIndex)}`;
6967
- return signMultiplier * Number.parseFloat(solString);
6968
- }
6969
6400
  function solToLamports(amount) {
6970
6401
  if (Number.isNaN(amount)) {
6971
6402
  return new BN(0);
@@ -6986,13 +6417,20 @@ function getBondTypeSeed(type) {
6986
6417
  }
6987
6418
 
6988
6419
  // src/client.ts
6420
+ var JBondClientEnv = /* @__PURE__ */ ((JBondClientEnv2) => {
6421
+ JBondClientEnv2["DEV"] = "dev";
6422
+ JBondClientEnv2["STAGE"] = "stage";
6423
+ JBondClientEnv2["PROD"] = "prod";
6424
+ return JBondClientEnv2;
6425
+ })(JBondClientEnv || {});
6989
6426
  var JBondClient = class _JBondClient {
6990
6427
  constructor(provider, options) {
6991
6428
  this.provider = provider;
6992
6429
  this.options = options ?? {};
6430
+ this.program = new Program(jbond_default, this.provider);
6993
6431
  }
6994
6432
  options;
6995
- _program = null;
6433
+ program;
6996
6434
  get history() {
6997
6435
  return new HistoryManager(this);
6998
6436
  }
@@ -7021,24 +6459,11 @@ var JBondClient = class _JBondClient {
7021
6459
  static fromKeypair(connection, keypair, options) {
7022
6460
  return _JBondClient.fromWallet(connection, new NodeWallet(keypair), options);
7023
6461
  }
7024
- /**
7025
- * Get or create the program instance
7026
- */
7027
- get program() {
7028
- if (!this._program) {
7029
- jbond_default.address = this.programId.toBase58();
7030
- this._program = new Program(jbond_default, this.provider);
7031
- }
7032
- return this._program;
7033
- }
7034
6462
  /**
7035
6463
  * Get the current program ID.
7036
6464
  */
7037
6465
  get programId() {
7038
- if (this.options.programId) {
7039
- return this.options.programId;
7040
- }
7041
- return new PublicKey(ENV_PROGRAM_ID["prod" /* PROD */]);
6466
+ return this.options.programId ?? this.program.programId;
7042
6467
  }
7043
6468
  /**
7044
6469
  * Get the current connection.
@@ -7050,22 +6475,13 @@ var JBondClient = class _JBondClient {
7050
6475
  * Set the environment.
7051
6476
  */
7052
6477
  env(env) {
7053
- const envProgramId = ENV_PROGRAM_ID[env];
7054
- if (!envProgramId) {
7055
- throw new Error(`Invalid environment: ${env}`);
7056
- }
7057
- this._program = null;
7058
- this.debug(`Set environment to ${env} with program ID: ${envProgramId.toString()}`);
7059
- return this.configure("programId", new PublicKey(envProgramId));
6478
+ return this.configure("programId", ENV_PROGRAM_ID[env]);
7060
6479
  }
7061
6480
  /**
7062
6481
  * Configure a specific option.
7063
6482
  */
7064
6483
  configure(key, val) {
7065
6484
  this.options[key] = val;
7066
- if (key === "programId") {
7067
- this._program = null;
7068
- }
7069
6485
  return this;
7070
6486
  }
7071
6487
  /**
@@ -7124,8 +6540,8 @@ var JBondClient = class _JBondClient {
7124
6540
  * Top up collateral
7125
6541
  */
7126
6542
  async topUpCollateral(props) {
7127
- const { instructions, signers } = await this.getTopUpIxs(props);
7128
- return this.provider.sendAndConfirm?.(new Transaction().add(...instructions), signers);
6543
+ const ixs = await this.getTopUpIxs(props);
6544
+ return this.provider.sendAndConfirm?.(new Transaction().add(...ixs));
7129
6545
  }
7130
6546
  /**
7131
6547
  * Withdraw collateral
@@ -7135,14 +6551,7 @@ var JBondClient = class _JBondClient {
7135
6551
  return this.provider.sendAndConfirm?.(new Transaction().add(ix));
7136
6552
  }
7137
6553
  /**
7138
- * Migrate SOL collateral from one bond account to JSOL collateral in another bond account
7139
- */
7140
- async migrateBond(props) {
7141
- const { instructions, signers } = await this.getMigrateBondIx(props);
7142
- return this.provider.sendAndConfirm?.(new Transaction().add(...instructions), signers);
7143
- }
7144
- /**
7145
- * Claim compensation for a validator
6554
+ * Claim compensation
7146
6555
  */
7147
6556
  async claimCompensation(props) {
7148
6557
  const ix = await this.getClaimIx(props);
@@ -7183,14 +6592,6 @@ var JBondClient = class _JBondClient {
7183
6592
  const ix = await this.getBondSessionFinishIx(props);
7184
6593
  return this.provider.sendAndConfirm?.(new Transaction().add(ix));
7185
6594
  }
7186
- async bondRemove(props) {
7187
- const ix = await this.getBondRemoveIx(props);
7188
- return this.provider.sendAndConfirm?.(new Transaction().add(ix));
7189
- }
7190
- async closeValidatorBond(props) {
7191
- const ix = await this.getCloseValidatorBondIx(props);
7192
- return this.provider.sendAndConfirm?.(new Transaction().add(ix));
7193
- }
7194
6595
  /**
7195
6596
  * Build global state initialize instruction
7196
6597
  */
@@ -7301,9 +6702,6 @@ var JBondClient = class _JBondClient {
7301
6702
  collateralType = bondState2.collateralType;
7302
6703
  }
7303
6704
  const instructions = [];
7304
- const signers = [];
7305
- const amountLamports = solToLamports(amount);
7306
- let topUpAmount = amountLamports;
7307
6705
  try {
7308
6706
  await this.getValidatorBond(props.bondType, props.name, props.voteAccount);
7309
6707
  } catch {
@@ -7335,33 +6733,10 @@ var JBondClient = class _JBondClient {
7335
6733
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
7336
6734
  }
7337
6735
  });
7338
- if (props.solUsage) {
7339
- let usesJsolCollateral = false;
7340
- matchVariant(collateralType, {
7341
- token: (mint) => {
7342
- usesJsolCollateral = mint.equals(JSOL_MINT);
7343
- }
7344
- });
7345
- if (usesJsolCollateral) {
7346
- const stakePool = await getStakePoolAccountData(this.connection, JPOOL_POOL_ADDRESS);
7347
- const deposit = await depositSol(
7348
- this.connection,
7349
- JPOOL_POOL_ADDRESS,
7350
- payer,
7351
- amountLamports.toNumber(),
7352
- void 0,
7353
- void 0,
7354
- void 0
7355
- );
7356
- instructions.push(...deposit.instructions);
7357
- signers.push(...deposit.signers);
7358
- topUpAmount = lamportsToPoolTokenLamports(amountLamports, stakePool);
7359
- }
7360
- }
7361
6736
  instructions.push(
7362
- await this.program.methods.bondTopUp(topUpAmount).accountsPartial(accounts).instruction()
6737
+ await this.program.methods.bondTopUp(solToLamports(amount)).accountsPartial(accounts).instruction()
7363
6738
  );
7364
- return { instructions, signers };
6739
+ return instructions;
7365
6740
  }
7366
6741
  /**
7367
6742
  * Build withdraw collateral instruction
@@ -7400,139 +6775,6 @@ var JBondClient = class _JBondClient {
7400
6775
  });
7401
6776
  return this.program.methods.bondWithdraw(solToLamports(amount)).accountsPartial(accounts).instruction();
7402
6777
  }
7403
- /**
7404
- * Build instructions for migrating SOL bond collateral into JSOL bond collateral in a single transaction
7405
- */
7406
- async getMigrateBondIx(props) {
7407
- const {
7408
- bondType,
7409
- voteAccount,
7410
- identity,
7411
- withdrawName = "performance",
7412
- topUpName = "perf",
7413
- stakePoolAddress = JPOOL_POOL_ADDRESS
7414
- } = props;
7415
- const payer = props.payer ?? this.provider.wallet?.publicKey;
7416
- const destination = props.destination ?? payer;
7417
- if (!payer || !destination) {
7418
- throw new Error("Missing payer/destination");
7419
- }
7420
- const withdrawBondState = await this.getBondState(bondType, withdrawName);
7421
- await matchVariant(withdrawBondState.collateralType, {
7422
- native: () => {
7423
- },
7424
- token: () => {
7425
- throw new Error("Withdraw bond collateral type must be native SOL for migration");
7426
- }
7427
- });
7428
- const availableLamports = await this.getAvailableCollateral(bondType, withdrawName, voteAccount);
7429
- if (!availableLamports || availableLamports <= 0) {
7430
- throw new Error("No available balance to migrate");
7431
- }
7432
- const amount = lamportsToSol(availableLamports);
7433
- const amountLamports = solToLamports(amount);
7434
- const withdrawIx = await this.getWithdrawIx({
7435
- bondType,
7436
- name: withdrawName,
7437
- amount,
7438
- voteAccount,
7439
- destination,
7440
- withdrawalAuthority: props.withdrawalAuthority
7441
- });
7442
- const deposit = await this.getDepositSolIxsWithoutBalanceCheck(
7443
- stakePoolAddress,
7444
- payer,
7445
- amountLamports.toNumber()
7446
- );
7447
- const topUpBondState = await this.getBondState(bondType, topUpName);
7448
- const topUpCollateralType = topUpBondState.collateralType;
7449
- const instructions = [withdrawIx, ...deposit.instructions];
7450
- const signers = [...deposit.signers];
7451
- try {
7452
- await this.getValidatorBond(bondType, topUpName, voteAccount);
7453
- } catch {
7454
- instructions.push(
7455
- await this.getRegisterValidatorIx({
7456
- bondType,
7457
- name: topUpName,
7458
- voteAccount,
7459
- identity,
7460
- creator: payer,
7461
- collateralType: topUpCollateralType
7462
- })
7463
- );
7464
- }
7465
- const [topUpBondStateAddress] = this.pda.bondState(bondType, topUpName);
7466
- const [topUpValidatorBond] = this.pda.validatorBond(bondType, topUpName, voteAccount);
7467
- await matchVariant(topUpCollateralType, {
7468
- native: () => {
7469
- throw new Error("Top up bond collateral type must be token for migration");
7470
- },
7471
- token: async (mint) => {
7472
- const stakePool = await getStakePoolAccountData(this.connection, stakePoolAddress);
7473
- const topUpAmount = lamportsToPoolTokenLamports(amountLamports, stakePool);
7474
- const topUpIx = await this.program.methods.bondTopUp(topUpAmount).accountsPartial({
7475
- bondState: topUpBondStateAddress,
7476
- validatorBond: topUpValidatorBond,
7477
- payer,
7478
- payerTokenAccount: getAssociatedTokenAddressSync(mint, payer),
7479
- bondTokenAccount: getAssociatedTokenAddressSync(mint, topUpValidatorBond, true),
7480
- stakeAccount: null,
7481
- stakeProgram: null
7482
- }).instruction();
7483
- instructions.push(topUpIx);
7484
- }
7485
- });
7486
- return { instructions, signers };
7487
- }
7488
- /**
7489
- * Build stake-pool SOL deposit instructions without pre-checking payer balance.
7490
- * This allows composing withdraw->deposit in a single transaction where SOL is obtained by the prior instruction.
7491
- */
7492
- async getDepositSolIxsWithoutBalanceCheck(stakePoolAddress, payer, lamports, destinationTokenAccount, referrerTokenAccount, depositAuthority) {
7493
- const stakePool = await getStakePoolAccountData(this.connection, stakePoolAddress);
7494
- const transferSigner = new Keypair();
7495
- const signers = [transferSigner];
7496
- const instructions = [
7497
- SystemProgram.transfer({
7498
- fromPubkey: payer,
7499
- toPubkey: transferSigner.publicKey,
7500
- lamports
7501
- })
7502
- ];
7503
- let destinationPoolAccount = destinationTokenAccount;
7504
- if (!destinationPoolAccount) {
7505
- const associatedAddress = getAssociatedTokenAddressSync(stakePool.poolMint, payer);
7506
- instructions.push(
7507
- createAssociatedTokenAccountIdempotentInstruction(
7508
- payer,
7509
- associatedAddress,
7510
- payer,
7511
- stakePool.poolMint
7512
- )
7513
- );
7514
- destinationPoolAccount = associatedAddress;
7515
- }
7516
- const [withdrawAuthority] = PublicKey.findProgramAddressSync(
7517
- [stakePoolAddress.toBuffer(), Buffer.from("withdraw")],
7518
- STAKE_POOL_PROGRAM_ID
7519
- );
7520
- instructions.push(
7521
- StakePoolInstruction.depositSol({
7522
- stakePool: stakePoolAddress,
7523
- reserveStake: stakePool.reserveStake,
7524
- fundingAccount: transferSigner.publicKey,
7525
- destinationPoolAccount,
7526
- managerFeeAccount: stakePool.managerFeeAccount,
7527
- referralPoolAccount: referrerTokenAccount ?? destinationPoolAccount,
7528
- poolMint: stakePool.poolMint,
7529
- lamports,
7530
- withdrawAuthority,
7531
- depositAuthority
7532
- })
7533
- );
7534
- return { instructions, signers };
7535
- }
7536
6778
  /**
7537
6779
  * Build claim instruction
7538
6780
  */
@@ -7563,47 +6805,13 @@ var JBondClient = class _JBondClient {
7563
6805
  native: () => {
7564
6806
  },
7565
6807
  token: (mint) => {
6808
+ accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
7566
6809
  accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
7567
- if (reserve.equals(mint)) {
7568
- accounts.reserveTokenAccount = null;
7569
- accounts.destinationTokenAccount = null;
7570
- } else {
7571
- accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
7572
- accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
7573
- }
6810
+ accounts.reserveTokenAccount = getAssociatedTokenAddressSync(mint, reserve, true);
7574
6811
  }
7575
6812
  });
7576
6813
  return this.program.methods.bondClaim(solToLamports(amount)).accountsPartial(accounts).instruction();
7577
6814
  }
7578
- /**
7579
- * Build claim all compensations instructions
7580
- */
7581
- async getClaimAllCompensationsIxs(props) {
7582
- const ixs = [];
7583
- const validatorBondBalances = await this.getBondStateValidatorBondBalances(
7584
- props.bondType,
7585
- props.name
7586
- );
7587
- for (const bond_balance of validatorBondBalances) {
7588
- const amountToClaim = lamportsToSol(bond_balance.balance);
7589
- if (amountToClaim <= 0) {
7590
- this.debug("No balance to claim for validator:", bond_balance.voteAccount.toBase58());
7591
- continue;
7592
- }
7593
- this.debug("Amount to claim:", amountToClaim);
7594
- const ix = await this.getClaimIx({
7595
- bondType: props.bondType,
7596
- name: props.name,
7597
- amount: amountToClaim,
7598
- collateralType: props.collateralType,
7599
- reserve: props.reserve,
7600
- authority: props.authority,
7601
- voteAccount: bond_balance.voteAccount
7602
- });
7603
- ixs.push(ix);
7604
- }
7605
- return ixs;
7606
- }
7607
6815
  /**
7608
6816
  * Build set withdraw authority instruction
7609
6817
  */
@@ -7632,150 +6840,23 @@ var JBondClient = class _JBondClient {
7632
6840
  const authority = this.provider.wallet?.publicKey;
7633
6841
  return this.program.methods.sessionStart(new import_bn.BN(props.duration)).accountsPartial({ bondState, authority }).instruction();
7634
6842
  }
7635
- async lockFunds(props) {
7636
- const ix = await this.getLockFundsIx(props);
7637
- return this.provider.sendAndConfirm?.(new Transaction().add(ix));
7638
- }
7639
- async releaseFunds(props) {
7640
- const ix = await this.getReleaseFundsIx(props);
7641
- return this.provider.sendAndConfirm?.(new Transaction().add(ix));
7642
- }
7643
- /**
7644
- * Build bond remove instruction
7645
- */
7646
- async getBondRemoveIx(props) {
7647
- const { bondType, name } = props;
7648
- const [bondState] = this.pda.bondState(bondType, name);
7649
- const authority = props.authority ?? this.provider.wallet?.publicKey;
7650
- return this.program.methods.bondRemove().accountsPartial({ bondState, authority }).instruction();
7651
- }
7652
- /**
7653
- * Build close validator bond instruction
7654
- */
7655
- async getCloseValidatorBondIx(props) {
7656
- const { bondType, name, voteAccount } = props;
7657
- const [bondState] = this.pda.bondState(bondType, name);
7658
- const [validatorBond] = this.pda.validatorBond(bondType, name, voteAccount);
7659
- const authority = props.authority ?? this.provider.wallet?.publicKey;
7660
- const rentReceiver = props.rentReceiver ?? authority;
7661
- if (!authority || !rentReceiver) {
7662
- throw new Error("Missing authority or rentReceiver");
7663
- }
7664
- let collateralType = props.collateralType;
7665
- if (!collateralType) {
7666
- const state = await this.getBondState(bondType, name);
7667
- collateralType = state.collateralType;
7668
- }
7669
- const accounts = {
7670
- bondState,
7671
- validatorBond,
7672
- authority,
7673
- rentReceiver,
7674
- bondTokenAccount: null,
7675
- destinationTokenAccount: null
7676
- };
7677
- matchVariant(collateralType, {
7678
- native: () => {
7679
- },
7680
- token: (mint) => {
7681
- accounts.bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
7682
- accounts.destinationTokenAccount = getAssociatedTokenAddressSync(mint, rentReceiver, true);
7683
- }
7684
- });
7685
- return this.program.methods.closeValidatorBond().accountsPartial(accounts).instruction();
7686
- }
7687
- /**
7688
- * Build test set last update epoch instruction
7689
- * NOTE!: This instruction is for testing purposes only.
7690
- * It allows direct manipulation of the validator bond's last epoch update.
7691
- * This should not be used in production environments.
7692
- */
7693
- // async getTestSetLastUpdateEpochIx(props: { bondType: BondType, bondName: string, voteAccount: PublicKeyInitData, payer: PublicKey, epoch: number }) {
7694
- // const { bondType, bondName, voteAccount, payer, epoch } = props
7695
- // const [validatorBond, _] = this.pda.validatorBond(bondType, bondName, voteAccount)
7696
- // return this.program.methods
7697
- // .testSetLastUpdateEpoch(new BN(epoch))
7698
- // .accountsPartial({ validatorBond, payer })
7699
- // .instruction()
7700
- // }
7701
- /**
7702
- * Build lock funds instruction
7703
- */
7704
- async getLockFundsIx(props) {
7705
- const { bondType, name, voteAccount, amount, withdrawAuthority } = props;
7706
- const [bondState] = this.pda.bondState(bondType, name);
7707
- let collateralType = props.collateralType;
7708
- if (!collateralType) {
7709
- const bondState2 = await this.getBondState(bondType, name);
7710
- collateralType = bondState2.collateralType;
7711
- }
7712
- const [validatorBond] = this.pda.validatorBond(bondType, name, voteAccount);
7713
- const accounts = {
7714
- bondState,
7715
- validatorBond,
7716
- payer: withdrawAuthority,
7717
- bondTokenAccount: null
7718
- };
7719
- matchVariant(collateralType, {
7720
- native: () => {
7721
- },
7722
- token: (mint) => {
7723
- const bondTokenAccount = getAssociatedTokenAddressSync(mint, validatorBond, true);
7724
- accounts.bondTokenAccount = bondTokenAccount;
7725
- }
7726
- });
7727
- return this.program.methods.bondLockFunds(solToLamports(amount)).accountsPartial(accounts).instruction();
7728
- }
7729
- /**
7730
- * Build release funds instruction
7731
- */
7732
- async getReleaseFundsIx(props) {
7733
- const { bondType, name, voteAccount, amount, withdrawAuthority } = props;
7734
- const [bondState] = this.pda.bondState(bondType, name);
7735
- const [validatorBond] = this.pda.validatorBond(bondType, name, voteAccount);
7736
- const accounts = {
7737
- bondState,
7738
- validatorBond,
7739
- payer: withdrawAuthority
7740
- };
7741
- return this.program.methods.bondReleaseFunds(solToLamports(amount)).accountsPartial(accounts).instruction();
7742
- }
7743
- /**
7744
- * Get available collateral for withdrawal (taking into account locked funds)
7745
- */
7746
- async getAvailableCollateral(bondType, bondName, voteAccount) {
7747
- const validatorBond = await this.getValidatorBond(bondType, bondName, voteAccount);
7748
- return await this.getValidatorBondBalance(bondType, bondName, voteAccount) - validatorBond.locked.toNumber();
7749
- }
7750
6843
  /**
7751
6844
  * Get all bond states with total collected collateral
7752
6845
  */
7753
- async getAllBondStates(bondType, sessionStatus, options) {
7754
- const includeStats = options?.includeStats ?? true;
7755
- const targetBondType = bondTypeToAnchor(bondType);
6846
+ async getAllBondStates(bondType, sessionStatus) {
6847
+ const result = [];
7756
6848
  const accounts = await this.program.account.bondState.all();
7757
- const filteredAccounts = accounts.filter(({ account }) => {
7758
- if (!sameVariant(account.bondType, targetBondType)) {
7759
- return false;
6849
+ for (const { publicKey, account } of accounts) {
6850
+ if (!sameVariant(account.bondType, bondTypeToAnchor(bondType))) {
6851
+ continue;
7760
6852
  }
7761
6853
  if (sessionStatus !== void 0 && this.getBondStateSessionStatus(account) !== sessionStatus) {
7762
- return false;
6854
+ continue;
7763
6855
  }
7764
- return true;
7765
- });
7766
- if (!includeStats) {
7767
- return filteredAccounts.map(({ publicKey, account }) => ({
7768
- publicKey,
7769
- bondState: account,
7770
- stateStats: null
7771
- }));
6856
+ const stateStats = await this.getBondStateStats(account);
6857
+ result.push({ publicKey, bondState: account, stateStats });
7772
6858
  }
7773
- return Promise.all(
7774
- filteredAccounts.map(async ({ publicKey, account }) => {
7775
- const stateStats = await this.getBondStateStats(account);
7776
- return { publicKey, bondState: account, stateStats };
7777
- })
7778
- );
6859
+ return result;
7779
6860
  }
7780
6861
  /**
7781
6862
  * Fetch global state or throw if not found
@@ -7796,28 +6877,16 @@ var JBondClient = class _JBondClient {
7796
6877
  return { publicKey, ...data };
7797
6878
  }
7798
6879
  /**
7799
- * Fetch validator bond data or error if not found
6880
+ * Fetch validator bond data or null if not found
7800
6881
  */
7801
6882
  async getValidatorBond(bondType, bondName, vote) {
7802
6883
  const [publicKey] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
7803
- this.debug(`Fetching validator bond ${publicKey}...`);
7804
6884
  const data = await this.program.account.validatorBond.fetch(publicKey);
7805
6885
  return { publicKey, ...data };
7806
6886
  }
7807
6887
  /**
7808
- * Fetch validator bond data or null if not found
7809
- */
7810
- async getValidatorBondNullable(bondType, bondName, vote) {
7811
- const [publicKey] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
7812
- this.debug(`Fetching validator bond ${publicKey}...`);
7813
- const data = await this.program.account.validatorBond.fetchNullable(publicKey);
7814
- if (!data) {
7815
- return null;
7816
- }
7817
- return { publicKey, ...data };
7818
- }
7819
- /**
7820
- * Get validator bond account balance (in SOL)
6888
+ * Get validator bond account balance, in the collateral's base units
6889
+ * (lamports for native bonds, token base units for token bonds).
7821
6890
  */
7822
6891
  async getValidatorBondBalance(bondType, bondName, vote) {
7823
6892
  const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
@@ -7829,8 +6898,8 @@ var JBondClient = class _JBondClient {
7829
6898
  if (!accountInfo) {
7830
6899
  return 0;
7831
6900
  }
7832
- const { lamports } = accountInfo;
7833
- const rentExempt = await this.validatorBondRentExempt();
6901
+ const { data, lamports } = accountInfo;
6902
+ const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
7834
6903
  return Math.max(0, lamports - rentExempt);
7835
6904
  },
7836
6905
  token: async (mint) => {
@@ -7844,13 +6913,12 @@ var JBondClient = class _JBondClient {
7844
6913
  }
7845
6914
  });
7846
6915
  }
7847
- async getBondStateValidatorBondBalances(bondType, bondName, bondState) {
7848
- const state = bondState ?? await this.getBondState(bondType, bondName);
7849
- const collateralType = state.collateralType;
7850
- const validatorBonds = await this.getValidatorBondsForState(bondType, bondName);
7851
- if (!validatorBonds.length) {
7852
- return [];
7853
- }
6916
+ /**
6917
+ * Get total collected collateral for a bond state
6918
+ */
6919
+ async getBondStateTotalCollected(bondType, bondName, votes) {
6920
+ const bondState = await this.getBondState(bondType, bondName);
6921
+ const collateralType = bondState.collateralType;
7854
6922
  const chunk = (arr, n = 100) => {
7855
6923
  const res = [];
7856
6924
  for (let i = 0; i < arr.length; i += n) {
@@ -7860,79 +6928,76 @@ var JBondClient = class _JBondClient {
7860
6928
  };
7861
6929
  return await matchVariant(collateralType, {
7862
6930
  native: async () => {
7863
- const addresses = validatorBonds.map((vb) => vb.publicKey);
6931
+ const addresses = votes.map((vote) => {
6932
+ const [address] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6933
+ return address;
6934
+ });
7864
6935
  const accountInfos = await this.connection.getMultipleAccountsInfo(addresses);
7865
- const rentExempt = await this.validatorBondRentExempt();
7866
- return validatorBonds.map((vb, idx) => ({
7867
- voteAccount: vb.voteAccount,
7868
- balance: Math.max(0, (accountInfos[idx]?.lamports ?? 0) - rentExempt)
7869
- }));
6936
+ const balances = await Promise.all(
6937
+ accountInfos.map(async (accountInfo) => {
6938
+ if (!accountInfo) {
6939
+ return 0;
6940
+ }
6941
+ const { lamports } = accountInfo;
6942
+ return lamports;
6943
+ })
6944
+ );
6945
+ return balances.reduce((sum, v) => sum + v, 0);
7870
6946
  },
7871
6947
  token: async (mint) => {
7872
- const items = validatorBonds.map((vb) => ({
7873
- voteAccount: vb.voteAccount,
7874
- tokenAccount: getAssociatedTokenAddressSync(mint, vb.publicKey, true)
7875
- }));
7876
- const balances = [];
7877
- for (const part of chunk(items, 100)) {
7878
- const infos = await this.connection.getMultipleAccountsInfo(part.map((i) => i.tokenAccount)).catch(() => []);
7879
- for (let i = 0; i < part.length; i++) {
6948
+ const tokenAccounts = votes.map((vote) => {
6949
+ const [validatorBondAddress] = this.pda.validatorBond(bondType, bondName, new PublicKey(vote));
6950
+ return getAssociatedTokenAddressSync(mint, validatorBondAddress, true);
6951
+ });
6952
+ if (tokenAccounts.length === 0) {
6953
+ return 0;
6954
+ }
6955
+ let total = 0n;
6956
+ for (const part of chunk(tokenAccounts, 100)) {
6957
+ const infos = await this.connection.getMultipleAccountsInfo(part).catch(() => []);
6958
+ for (let i = 0; i < infos.length; i++) {
7880
6959
  const info = infos[i];
7881
- const voteAccount = part[i].voteAccount;
7882
- if (!info || info.data.length !== ACCOUNT_SIZE) {
7883
- balances.push({ voteAccount, balance: 0 });
6960
+ if (!info) {
6961
+ continue;
6962
+ }
6963
+ if (info.data.length !== ACCOUNT_SIZE) {
7884
6964
  continue;
7885
6965
  }
7886
6966
  try {
7887
6967
  const acc = AccountLayout.decode(info.data);
7888
6968
  const raw = BigInt(acc.amount.toString());
7889
- const asNumber = Number(raw);
7890
- balances.push({
7891
- voteAccount,
7892
- balance: Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER
7893
- });
6969
+ total += raw;
7894
6970
  } catch {
7895
- balances.push({ voteAccount, balance: 0 });
7896
6971
  }
7897
6972
  }
7898
6973
  }
7899
- return balances;
6974
+ const asNumber = Number(total);
6975
+ return Number.isFinite(asNumber) ? asNumber : Number.MAX_SAFE_INTEGER;
7900
6976
  }
7901
6977
  });
7902
6978
  }
7903
- /**
7904
- * Get total collected collateral for a bond state
7905
- */
7906
- async getBondStateTotalCollected(bondType, bondName, bondState) {
7907
- const allBalances = await this.getBondStateValidatorBondBalances(bondType, bondName, bondState);
7908
- return allBalances.reduce((sum, vb) => sum + vb.balance, 0);
7909
- }
7910
6979
  /**
7911
6980
  * Get bond state stats
7912
6981
  * @param state
6982
+ * @private
7913
6983
  */
7914
6984
  async getBondStateStats(state) {
7915
6985
  const bondType = anchorToBondType(state.bondType);
6986
+ const validatorBonds = await this.getBondValidatorStates(bondType, state.name);
7916
6987
  const totalCollected = await this.getBondStateTotalCollected(
7917
6988
  bondType,
7918
6989
  state.name,
7919
- state
6990
+ validatorBonds.map((vb) => vb.voteAccount)
7920
6991
  );
7921
6992
  return {
7922
6993
  totalCollected,
7923
6994
  status: this.getBondStateSessionStatus(state)
7924
6995
  };
7925
6996
  }
7926
- /**
7927
- * Get validator bond rent exempt amount
7928
- */
7929
- validatorBondRentExempt = async () => {
7930
- return this.connection.getMinimumBalanceForRentExemption(this.program.account.validatorBond.size);
7931
- };
7932
6997
  /**
7933
6998
  * Get all validator bonds for a given bond state
7934
6999
  */
7935
- async getValidatorBondsForState(bondType, bondName) {
7000
+ async getBondValidatorStates(bondType, bondName) {
7936
7001
  const [bondState] = this.pda.bondState(bondType, bondName);
7937
7002
  const accounts = await this.program.account.validatorBond.all([
7938
7003
  {
@@ -7975,6 +7040,6 @@ buffer/index.js:
7975
7040
  *)
7976
7041
  */
7977
7042
 
7978
- export { BOND_STATE_SEED, BondTransactionType, BondType, CROWDFUNDING_BOND_SEED, ENV_PROGRAM_ID, GLOBAL_STATE_SEED, JBondClient, JBondClientEnv, JPOOL_POOL_ADDRESS, JSOL_MINT, NodeWallet, STANDARD_BOND_SEED, SessionStatus, VALIDATOR_BOND_SEED, lamportsToSol };
7043
+ 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 };
7979
7044
  //# sourceMappingURL=index.mjs.map
7980
7045
  //# sourceMappingURL=index.mjs.map