@jpool/bond-sdk 0.1.0-next.2 → 0.1.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3,13 +3,14 @@
3
3
  var anchor = require('@coral-xyz/anchor');
4
4
  var web3_js = require('@solana/web3.js');
5
5
  var bs58 = require('bs58');
6
- var fs = require('fs');
7
6
 
8
7
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
8
 
10
9
  var bs58__default = /*#__PURE__*/_interopDefault(bs58);
11
10
 
12
11
  // src/client.ts
12
+ var PROGRAM_ID = new web3_js.PublicKey("8vrqsFHnDdjMYMwahytRzmmYgRqCMmka8X6DJUXGQzWr");
13
+ var DEV_PROGRAM_ID = new web3_js.PublicKey("8vrqsFHnDdjMYMwahytRzmmYgRqCMmka8X6DJUXGQzWr");
13
14
 
14
15
  // src/helpers.ts
15
16
  var SLOTS_PER_EPOCH_MAINNET = 432e3;
@@ -21,7 +22,7 @@ function slotToEpoch(slot, cluster) {
21
22
 
22
23
  // src/idl/jbond.json
23
24
  var jbond_default = {
24
- address: "4a3YovKEfm4jWhczCzJciHXL1xVkXWfGQjRCaMft7M4G",
25
+ address: "8vrqsFHnDdjMYMwahytRzmmYgRqCMmka8X6DJUXGQzWr",
25
26
  metadata: {
26
27
  name: "jbond",
27
28
  version: "0.1.0",
@@ -252,6 +253,10 @@ var jbond_default = {
252
253
  name: "destination",
253
254
  writable: true
254
255
  },
256
+ {
257
+ name: "epoch_schedule",
258
+ address: "SysvarEpochSchedu1e111111111111111111111111"
259
+ },
255
260
  {
256
261
  name: "system_program",
257
262
  address: "11111111111111111111111111111111"
@@ -320,6 +325,60 @@ var jbond_default = {
320
325
  ],
321
326
  args: []
322
327
  },
328
+ {
329
+ name: "set_authority",
330
+ docs: [
331
+ "Change the bond authority"
332
+ ],
333
+ discriminator: [
334
+ 133,
335
+ 250,
336
+ 37,
337
+ 21,
338
+ 110,
339
+ 163,
340
+ 26,
341
+ 121
342
+ ],
343
+ accounts: [
344
+ {
345
+ name: "global_state",
346
+ writable: true,
347
+ pda: {
348
+ seeds: [
349
+ {
350
+ kind: "const",
351
+ value: [
352
+ 103,
353
+ 108,
354
+ 111,
355
+ 98,
356
+ 97,
357
+ 108,
358
+ 95,
359
+ 115,
360
+ 116,
361
+ 97,
362
+ 116,
363
+ 101
364
+ ]
365
+ }
366
+ ]
367
+ }
368
+ },
369
+ {
370
+ name: "authority",
371
+ signer: true,
372
+ relations: [
373
+ "global_state"
374
+ ]
375
+ },
376
+ {
377
+ name: "new_authority"
378
+ }
379
+ ],
380
+ args: []
381
+ },
323
382
  {
324
383
  name: "withdraw_compensation",
325
384
  docs: [
@@ -544,6 +603,11 @@ var jbond_default = {
544
603
  code: 6008,
545
604
  name: "Unauthorized",
546
605
  msg: "Unauthorized"
606
+ },
607
+ {
608
+ code: 6009,
609
+ name: "WithdrawalLockedNearEpochEnd",
610
+ msg: "Withdrawals are locked near the end of the epoch."
547
611
  }
548
612
  ],
549
613
  types: [
@@ -768,14 +832,21 @@ var NodeWallet = class {
768
832
  };
769
833
 
770
834
  // src/client.ts
835
+ var BondClientEnv = /* @__PURE__ */ ((BondClientEnv2) => {
836
+ BondClientEnv2["DEV"] = "dev";
837
+ BondClientEnv2["PROD"] = "prod";
838
+ return BondClientEnv2;
839
+ })(BondClientEnv || {});
771
840
  var JBondClient = class _JBondClient {
772
841
  connection;
773
842
  program;
774
843
  provider;
775
844
  config;
776
- constructor(config, wallet) {
845
+ options;
846
+ constructor(config, wallet, options) {
777
847
  this.config = config;
778
848
  this.connection = new web3_js.Connection(config.rpcUrl, "confirmed");
849
+ this.options = options ?? {};
779
850
  this.provider = new anchor.AnchorProvider(
780
851
  this.connection,
781
852
  // @ts-expect-error support anonymous
@@ -787,20 +858,42 @@ var JBondClient = class _JBondClient {
787
858
  /**
788
859
  * Creates an instance of `JBondClient` using a provided connection and wallet.
789
860
  */
790
- static fromWallet(config, wallet) {
791
- return new this(config, wallet);
861
+ static fromWallet(config, wallet, options) {
862
+ return new this(config, wallet, options);
792
863
  }
793
864
  /**
794
865
  * Creates an instance of `JBondClient` using the provided connection and keypair.
795
866
  */
796
- static fromKeypair(config, keypair) {
797
- return _JBondClient.fromWallet(config, new NodeWallet(keypair));
867
+ static fromKeypair(config, keypair, options) {
868
+ return _JBondClient.fromWallet(config, new NodeWallet(keypair), options);
869
+ }
870
+ /**
871
+ * Get the current program ID.
872
+ */
873
+ get programId() {
874
+ return this.options.programId ?? PROGRAM_ID;
875
+ }
876
+ /**
877
+ * Set the environment.
878
+ */
879
+ env(env) {
880
+ if (env === "prod" /* PROD */) {
881
+ return this.configure("programId", PROGRAM_ID);
882
+ }
883
+ return this.configure("programId", DEV_PROGRAM_ID);
884
+ }
885
+ /**
886
+ * Configure a specific option.
887
+ */
888
+ configure(key, val) {
889
+ this.options[key] = val;
890
+ return this;
798
891
  }
799
892
  // Get PDA for global state
800
893
  getGlobalStatePDA() {
801
894
  return web3_js.PublicKey.findProgramAddressSync(
802
895
  [Buffer.from("global_state")],
803
- this.program.programId
896
+ this.options.programId ?? this.program.programId
804
897
  );
805
898
  }
806
899
  // Get PDA for validator bond account
@@ -810,16 +903,17 @@ var JBondClient = class _JBondClient {
810
903
  Buffer.from("validator_bond"),
811
904
  voteAccount.toBuffer()
812
905
  ],
813
- this.program.programId
906
+ this.options.programId ?? this.program.programId
814
907
  );
815
908
  }
816
909
  // Build initialize instruction
817
910
  async buildInitializeInstruction(props) {
818
911
  const [globalState] = this.getGlobalStatePDA();
912
+ const { authority, reserveAddress } = props;
819
913
  return this.program.methods.initialize().accountsStrict({
820
914
  globalState,
821
- authority: props.authority,
822
- reserve: this.config.reserveAddress,
915
+ authority,
916
+ reserve: reserveAddress,
823
917
  systemProgram: web3_js.SystemProgram.programId
824
918
  }).instruction();
825
919
  }
@@ -861,19 +955,24 @@ var JBondClient = class _JBondClient {
861
955
  validatorBondAccount: validatorBondAccountAddress,
862
956
  withdrawalAuthority: user,
863
957
  destination,
958
+ epochSchedule: web3_js.SYSVAR_EPOCH_SCHEDULE_PUBKEY,
864
959
  systemProgram: web3_js.SystemProgram.programId
865
960
  }).instruction();
866
961
  }
867
962
  // Build withdraw compensation instruction
868
- buildWithdrawCompensationInstruction(props) {
963
+ async buildWithdrawCompensationInstruction(props) {
869
964
  const { authority, voteAccount, amount } = props;
870
965
  const [globalState] = this.getGlobalStatePDA();
871
966
  const [validatorBondAccountAddress] = this.getValidatorBondPDA(voteAccount);
872
967
  const amountLamports = new anchor.BN(amount * web3_js.LAMPORTS_PER_SOL);
968
+ const reserveAddress = (await this.getGlobalState())?.reserveAddress;
969
+ if (!reserveAddress) {
970
+ throw new Error("Reserve address is not set in the global state. The program might not be initialized yet.");
971
+ }
873
972
  return this.program.methods.withdrawCompensation(amountLamports).accountsStrict({
874
973
  globalState,
875
974
  validatorBondAccount: validatorBondAccountAddress,
876
- reserve: this.config.reserveAddress,
975
+ reserve: reserveAddress,
877
976
  authority,
878
977
  systemProgram: web3_js.SystemProgram.programId
879
978
  }).instruction();
@@ -906,6 +1005,15 @@ var JBondClient = class _JBondClient {
906
1005
  return null;
907
1006
  }
908
1007
  }
1008
+ // Build set authority instruction
1009
+ async buildSetAuthorityInstruction(props) {
1010
+ const [globalState] = this.getGlobalStatePDA();
1011
+ return this.program.methods.setAuthority().accountsStrict({
1012
+ globalState,
1013
+ authority: props.authority,
1014
+ newAuthority: props.newAuthority
1015
+ }).instruction();
1016
+ }
909
1017
  /**
910
1018
  * Get the collateral balance of a validator bond account
911
1019
  * @param voteAccount - The vote account public key
@@ -931,7 +1039,8 @@ var JBondClient = class _JBondClient {
931
1039
  return {
932
1040
  authority: account.authority.toString(),
933
1041
  totalValidators: account.totalValidators,
934
- totalWithdrawn: account.totalWithdrawn.toNumber() / web3_js.LAMPORTS_PER_SOL
1042
+ totalWithdrawn: account.totalWithdrawn.toNumber() / web3_js.LAMPORTS_PER_SOL,
1043
+ reserveAddress: account.reserve.toString()
935
1044
  };
936
1045
  } catch {
937
1046
  return null;
@@ -943,9 +1052,9 @@ var JBondClient = class _JBondClient {
943
1052
  return epochInfo.epoch;
944
1053
  }
945
1054
  // Helper methods for backward compatibility (can be removed if not needed)
946
- async initialize(authority) {
1055
+ async initialize(reserveAddress, authority) {
947
1056
  const authorityPubkey = authority || this.provider.wallet.publicKey;
948
- const ix = await this.buildInitializeInstruction({ authority: authorityPubkey });
1057
+ const ix = await this.buildInitializeInstruction({ authority: authorityPubkey, reserveAddress });
949
1058
  const tx = await this.provider.sendAndConfirm(
950
1059
  new web3_js.Transaction().add(ix),
951
1060
  []
@@ -1070,7 +1179,7 @@ var JBondClient = class _JBondClient {
1070
1179
  const slot = tx.slot || 0;
1071
1180
  const instructions = tx.transaction.message.instructions;
1072
1181
  for (const instruction of instructions) {
1073
- if ("programId" in instruction && instruction.programId.equals(this.program.programId)) {
1182
+ if ("programId" in instruction && instruction.programId.equals(this.options.programId ?? this.program.programId)) {
1074
1183
  const ixData = instruction;
1075
1184
  if ("parsed" in ixData && ixData.parsed) {
1076
1185
  continue;
@@ -1165,38 +1274,10 @@ var JBondClient = class _JBondClient {
1165
1274
  }
1166
1275
  };
1167
1276
 
1168
- // src/common.ts
1169
- var PROGRAM_ID = "4a3YovKEfm4jWhczCzJciHXL1xVkXWfGQjRCaMft7M4G";
1170
- var RESERVE_ADDRESS = "61mS9nEir6jx6cvte6NzQpyrFk3Fj4krMNLuHhi4tjJz";
1171
- function loadConfig() {
1172
- const rpcUrl = process.env.SOLANA_RPC_URL || "https://api.devnet.solana.com";
1173
- const programId = new web3_js.PublicKey(
1174
- PROGRAM_ID
1175
- );
1176
- const reserveAddress = new web3_js.PublicKey(
1177
- RESERVE_ADDRESS
1178
- );
1179
- return {
1180
- rpcUrl,
1181
- programId,
1182
- reserveAddress
1183
- };
1184
- }
1185
- function loadKeypair(keypairPath) {
1186
- try {
1187
- const keypairString = fs.readFileSync(keypairPath, "utf8");
1188
- const keypairData = JSON.parse(keypairString);
1189
- return web3_js.Keypair.fromSecretKey(new Uint8Array(keypairData));
1190
- } catch (error) {
1191
- throw new Error(`Failed to load keypair from ${keypairPath}: ${error}`);
1192
- }
1193
- }
1194
-
1277
+ exports.BondClientEnv = BondClientEnv;
1195
1278
  exports.BondTransactionType = BondTransactionType;
1279
+ exports.DEV_PROGRAM_ID = DEV_PROGRAM_ID;
1196
1280
  exports.JBondClient = JBondClient;
1197
1281
  exports.PROGRAM_ID = PROGRAM_ID;
1198
- exports.RESERVE_ADDRESS = RESERVE_ADDRESS;
1199
- exports.loadConfig = loadConfig;
1200
- exports.loadKeypair = loadKeypair;
1201
1282
  //# sourceMappingURL=index.js.map
1202
1283
  //# sourceMappingURL=index.js.map