@jpool/bond-sdk 0.3.0-next.7 → 0.3.0-next.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,5 +1,5 @@
1
1
  import { Program, AnchorProvider, BN } from '@coral-xyz/anchor';
2
- import { PublicKey, LAMPORTS_PER_SOL, SYSVAR_EPOCH_SCHEDULE_PUBKEY, Transaction } from '@solana/web3.js';
2
+ import { PublicKey, Transaction, LAMPORTS_PER_SOL, SYSVAR_EPOCH_SCHEDULE_PUBKEY } from '@solana/web3.js';
3
3
  import bs58 from 'bs58';
4
4
 
5
5
  // src/client.ts
@@ -944,7 +944,6 @@ var JBondClient = class _JBondClient {
944
944
  return new this(
945
945
  new AnchorProvider(
946
946
  connection,
947
- // @ts-expect-error support anonymous
948
947
  wallet ?? { publicKey: PublicKey.default },
949
948
  AnchorProvider.defaultOptions()
950
949
  ),
@@ -992,23 +991,75 @@ var JBondClient = class _JBondClient {
992
991
  this.programId
993
992
  );
994
993
  },
995
- validatorBond: (voteAccount) => {
994
+ validatorBond: (vote) => {
996
995
  return PublicKey.findProgramAddressSync(
997
996
  [
998
997
  Buffer.from("validator_bond"),
999
- voteAccount.toBuffer()
998
+ new PublicKey(vote).toBuffer()
1000
999
  ],
1001
1000
  this.programId
1002
1001
  );
1003
1002
  }
1004
1003
  };
1004
+ /**
1005
+ * Initialize the program
1006
+ * Default authority is the provider's wallet
1007
+ */
1008
+ async initialize(props) {
1009
+ const ix = await this.buildInitializeInstruction(props);
1010
+ return await this.provider.sendAndConfirm(new Transaction().add(ix));
1011
+ }
1012
+ /**
1013
+ * Register a new validator
1014
+ * Default creator is the provider's wallet
1015
+ */
1016
+ async registerValidator(props) {
1017
+ const ix = await this.buildRegisterValidatorInstruction(props);
1018
+ return await this.provider.sendAndConfirm(new Transaction().add(ix));
1019
+ }
1020
+ /**
1021
+ * Top up collateral
1022
+ * Default payer is the provider's wallet
1023
+ */
1024
+ async topUpCollateral(props) {
1025
+ const ix = await this.buildTopUpCollateralInstruction(props);
1026
+ return await this.provider.sendAndConfirm(new Transaction().add(ix));
1027
+ }
1028
+ /**
1029
+ * Withdraw collateral
1030
+ * Default withdrawal authority is the provider's wallet
1031
+ * Default destination is the provider's wallet
1032
+ * @return Transaction signature
1033
+ */
1034
+ async withdrawCollateral(props) {
1035
+ const ix = await this.buildWithdrawCollateralInstruction(props);
1036
+ return await this.provider.sendAndConfirm(new Transaction().add(ix));
1037
+ }
1038
+ /**
1039
+ * Claim compensation
1040
+ * Default authority is the provider's wallet
1041
+ * @return Transaction signature
1042
+ */
1043
+ async claimCompensation(props) {
1044
+ const ix = await this.buildClaimInstruction(props);
1045
+ return await this.provider.sendAndConfirm(new Transaction().add(ix));
1046
+ }
1047
+ /**
1048
+ * Set a new authority for the program
1049
+ * Default authority is the provider's wallet
1050
+ * @return Transaction signature
1051
+ */
1052
+ async setAuthority(props) {
1053
+ const ix = await this.buildSetAuthorityInstruction(props);
1054
+ return await this.provider.sendAndConfirm(new Transaction().add(ix));
1055
+ }
1005
1056
  /**
1006
1057
  * Build initialize instruction
1007
- * @param props
1008
1058
  */
1009
1059
  async buildInitializeInstruction(props) {
1010
1060
  const [globalState] = this.pda.globalState();
1011
- const { authority, reserveAddress: reserve } = props;
1061
+ const { reserve } = props;
1062
+ const authority = props.authority ?? this.provider.wallet.publicKey;
1012
1063
  return this.program.methods.initialize().accountsPartial({
1013
1064
  globalState,
1014
1065
  authority,
@@ -1017,217 +1068,128 @@ var JBondClient = class _JBondClient {
1017
1068
  }
1018
1069
  /**
1019
1070
  * Build register validator instruction
1020
- * @param props
1021
1071
  */
1022
1072
  async buildRegisterValidatorInstruction(props) {
1023
- const { creator, identity, voteAccount, initialCollateral, withdrawalAuthority } = props;
1024
- const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
1025
- const accountInfo = await this.connection.getAccountInfo(validatorBondAccountAddress);
1026
- if (accountInfo) {
1027
- throw new Error("Validator bond account already exists");
1028
- }
1073
+ const { identity, voteAccount, initialCollateral, withdrawalAuthority } = props;
1029
1074
  const [globalState] = this.pda.globalState();
1030
- const collateralLamports = new BN(initialCollateral * LAMPORTS_PER_SOL);
1031
- return this.program.methods.register(collateralLamports, withdrawalAuthority ?? null).accountsPartial({
1075
+ const [validatorBond] = this.pda.validatorBond(voteAccount);
1076
+ const lamports = new BN(initialCollateral * LAMPORTS_PER_SOL);
1077
+ const creator = props.creator ?? this.provider.wallet.publicKey;
1078
+ return this.program.methods.register(lamports, withdrawalAuthority ?? null).accountsPartial({
1032
1079
  creator,
1033
1080
  globalState,
1034
- validatorBond: validatorBondAccountAddress,
1081
+ validatorBond,
1035
1082
  identity,
1036
1083
  voteAccount
1037
1084
  }).instruction();
1038
1085
  }
1039
1086
  /**
1040
1087
  * Build top up collateral instruction
1041
- * @param props
1042
1088
  */
1043
- buildTopUpCollateralInstruction(props) {
1044
- const { user, voteAccount, amount } = props;
1045
- const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
1046
- const amountLamports = new BN(amount * LAMPORTS_PER_SOL);
1047
- return this.program.methods.topUp(amountLamports).accountsPartial({
1048
- validatorBond: validatorBondAccountAddress,
1049
- payer: user
1089
+ async buildTopUpCollateralInstruction(props) {
1090
+ const { voteAccount, amount } = props;
1091
+ const [validatorBond] = this.pda.validatorBond(voteAccount);
1092
+ const lamports = new BN(amount * LAMPORTS_PER_SOL);
1093
+ const payer = props.payer ?? this.provider.wallet.publicKey;
1094
+ return this.program.methods.topUp(lamports).accountsPartial({
1095
+ validatorBond,
1096
+ payer
1050
1097
  }).instruction();
1051
1098
  }
1099
+ /**
1100
+ * Build withdraw collateral instruction
1101
+ */
1052
1102
  async buildWithdrawCollateralInstruction(props) {
1053
- const { withdrawalAuthority: user, voteAccount, destination, amount } = props;
1054
- const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
1055
- const amountLamports = new BN(amount * LAMPORTS_PER_SOL);
1056
- return this.program.methods.withdraw(amountLamports).accountsPartial({
1057
- validatorBond: validatorBondAccountAddress,
1058
- withdrawalAuthority: user,
1103
+ const { voteAccount, amount } = props;
1104
+ const [validatorBond] = this.pda.validatorBond(voteAccount);
1105
+ const lamports = new BN(amount * LAMPORTS_PER_SOL);
1106
+ const withdrawalAuthority = props.withdrawalAuthority ?? this.provider.wallet.publicKey;
1107
+ const destination = props.destination ?? this.provider.wallet.publicKey;
1108
+ return this.program.methods.withdraw(lamports).accountsPartial({
1109
+ validatorBond,
1110
+ withdrawalAuthority,
1059
1111
  epochSchedule: SYSVAR_EPOCH_SCHEDULE_PUBKEY,
1060
1112
  destination
1061
1113
  }).instruction();
1062
1114
  }
1063
1115
  /**
1064
- * Build withdraw compensation instruction
1065
- * @param props
1116
+ * Build claim compensation instruction
1066
1117
  */
1067
- async buildWithdrawCompensationInstruction(props) {
1068
- const { authority, voteAccount, amount } = props;
1118
+ async buildClaimInstruction(props) {
1069
1119
  const [globalState] = this.pda.globalState();
1070
- const [validatorBondAccountAddress] = this.pda.validatorBond(voteAccount);
1071
- const amountLamports = new BN(amount * LAMPORTS_PER_SOL);
1072
- const reserveAddress = (await this.getGlobalState())?.reserveAddress;
1073
- if (!reserveAddress) {
1074
- throw new Error("Reserve address is not set in the global state. The program might not be initialized yet.");
1120
+ const [validatorBond] = this.pda.validatorBond(props.voteAccount);
1121
+ const lamports = new BN(props.amount * LAMPORTS_PER_SOL);
1122
+ const authority = props.authority ?? this.provider.wallet.publicKey;
1123
+ let reserve = props.reserve;
1124
+ if (!reserve) {
1125
+ const globalStateData = await this.getGlobalState();
1126
+ reserve = globalStateData.reserve;
1127
+ if (!reserve) {
1128
+ throw new Error("Reserve address is not set in the global state. The program might not be initialized yet.");
1129
+ }
1075
1130
  }
1076
- return this.program.methods.claim(amountLamports).accountsPartial({
1131
+ return this.program.methods.claim(lamports).accountsPartial({
1077
1132
  globalState,
1078
- validatorBond: validatorBondAccountAddress,
1079
- reserve: reserveAddress,
1133
+ validatorBond,
1134
+ reserve,
1080
1135
  authority
1081
1136
  }).instruction();
1082
1137
  }
1083
- /**
1084
- * Build multiple withdraw compensation instructions
1085
- * @param authority
1086
- * @param withdrawals
1087
- */
1088
- buildWithdrawCompensationsInstructions(authority, withdrawals) {
1089
- return Promise.all(
1090
- withdrawals.map(
1091
- ({ voteAccount, amount }) => this.buildWithdrawCompensationInstruction({ authority, voteAccount, amount })
1092
- )
1093
- );
1094
- }
1095
- /**
1096
- * Get validator bond account state
1097
- * @param vote
1098
- */
1099
- async getValidatorBond(vote) {
1100
- const [validatorBondAccountAddress] = this.pda.validatorBond(vote);
1101
- try {
1102
- const account = await this.program.account.validatorBond.fetch(validatorBondAccountAddress);
1103
- return {
1104
- identity: account.identity.toString(),
1105
- voteAccount: account.voteAccount.toString(),
1106
- withdrawalAuthority: account.withdrawalAuthority ? account.withdrawalAuthority.toString() : null,
1107
- totalCompensationAmount: account.totalCompensationAmount.toNumber() / LAMPORTS_PER_SOL,
1108
- isActive: account.isActive,
1109
- createdAt: account.createdAt.toNumber() * 1e3,
1110
- // Convert i64 Unix seconds (BN) to JS milliseconds
1111
- bump: account.bump
1112
- };
1113
- } catch {
1114
- return null;
1115
- }
1116
- }
1117
1138
  /**
1118
1139
  * Build set authority instruction
1119
1140
  */
1120
1141
  async buildSetAuthorityInstruction(props) {
1121
1142
  const [globalState] = this.pda.globalState();
1143
+ const authority = props.authority ?? this.provider.wallet.publicKey;
1122
1144
  return this.program.methods.setAuthority().accountsStrict({
1123
1145
  globalState,
1124
- authority: props.authority,
1146
+ authority,
1125
1147
  newAuthority: props.newAuthority
1126
1148
  }).instruction();
1127
1149
  }
1128
1150
  /**
1129
- * Get the collateral balance of a validator bond account
1130
- * @param vote - The vote account public key
1131
- * @returns The available collateral balance in SOL (excluding rent-exempt amount)
1132
- */
1133
- async getValidatorCollateralBalance(vote) {
1134
- const [validatorBondAccountAddress] = this.pda.validatorBond(vote);
1135
- const accountInfo = await this.connection.getAccountInfo(validatorBondAccountAddress);
1136
- if (!accountInfo) {
1137
- return 0;
1138
- }
1139
- const rentExempt = await this.connection.getMinimumBalanceForRentExemption(
1140
- accountInfo.data.length
1141
- );
1142
- const availableBalance = Math.max(0, accountInfo.lamports - rentExempt);
1143
- return availableBalance / LAMPORTS_PER_SOL;
1144
- }
1145
- /**
1146
- * Get global state
1151
+ * Fetch global state or throw if not found
1147
1152
  */
1148
1153
  async getGlobalState() {
1149
1154
  const [globalState] = this.pda.globalState();
1150
- const account = await this.program.account.globalState.fetch(globalState);
1151
- return {
1152
- authority: account.authority.toString(),
1153
- totalValidators: account.totalValidators,
1154
- totalCompensationAmount: account.totalCompensationAmount.toNumber() / LAMPORTS_PER_SOL,
1155
- reserveAddress: account.reserve.toString()
1156
- };
1155
+ return await this.program.account.globalState.fetch(globalState);
1157
1156
  }
1158
1157
  /**
1159
- * Get current epoch
1158
+ * Fetch validator bond data or null if not found
1159
+ * @param vote
1160
1160
  */
1161
- async getCurrentEpoch() {
1162
- const epochInfo = await this.connection.getEpochInfo();
1163
- return epochInfo.epoch;
1161
+ async getValidatorBond(vote) {
1162
+ const [address] = this.pda.validatorBond(new PublicKey(vote));
1163
+ return await this.program.account.validatorBond.fetchNullable(address);
1164
1164
  }
1165
1165
  /**
1166
- * Helper methods for backward compatibility (can be removed if not needed)
1167
- * @param props
1166
+ * Get validator bond account balance (in SOL)
1167
+ * @param vote - Vote account public key
1168
+ * @return Balance in SOL
1168
1169
  */
1169
- async initialize(props) {
1170
- const { reserveAddress } = props;
1171
- const authority = props.authority ?? this.provider.wallet.publicKey;
1172
- const ix = await this.buildInitializeInstruction({ reserveAddress, authority });
1173
- return await this.provider.sendAndConfirm(
1174
- new Transaction().add(ix),
1175
- []
1176
- );
1177
- }
1178
- // Note: this method will be removed or changed in the future (CLI only)
1179
- async registerValidator(voteAccount, initialCollateral, withdrawalAuthority, identity) {
1180
- const identityPubkey = identity ?? this.provider.wallet.publicKey;
1181
- const ix = await this.buildRegisterValidatorInstruction(
1182
- { creator: identityPubkey, identity: identityPubkey, voteAccount, initialCollateral, withdrawalAuthority }
1183
- );
1184
- return await this.provider.sendAndConfirm(
1185
- new Transaction().add(ix),
1186
- []
1187
- );
1188
- }
1189
- async topUpCollateral(voteAccount, amount, validator) {
1190
- const userPubkey = validator ?? this.provider.wallet.publicKey;
1191
- const ix = await this.buildTopUpCollateralInstruction(
1192
- { user: userPubkey, voteAccount, amount }
1193
- );
1194
- return await this.provider.sendAndConfirm(
1195
- new Transaction().add(ix),
1196
- []
1197
- );
1198
- }
1199
- async withdrawCollateral(voteAccount, destination, amount, withdrawalAuthority) {
1200
- const authorityPubkey = withdrawalAuthority ?? this.provider.wallet.publicKey;
1201
- const ix = await this.buildWithdrawCollateralInstruction(
1202
- { withdrawalAuthority: authorityPubkey, voteAccount, destination, amount }
1203
- );
1204
- return await this.provider.sendAndConfirm(
1205
- new Transaction().add(ix),
1206
- []
1207
- );
1208
- }
1209
- async claimCompensation(voteAccount, amount, authority) {
1210
- const authorityPubkey = authority ?? this.provider.wallet.publicKey;
1211
- const ix = await this.buildWithdrawCompensationInstruction(
1212
- { authority: authorityPubkey, voteAccount, amount }
1213
- );
1214
- return await this.provider.sendAndConfirm(
1215
- new Transaction().add(ix),
1216
- []
1217
- );
1170
+ async getValidatorBondBalance(vote) {
1171
+ const [address] = this.pda.validatorBond(new PublicKey(vote));
1172
+ const accountInfo = await this.connection.getAccountInfo(address);
1173
+ if (!accountInfo) {
1174
+ return 0;
1175
+ }
1176
+ const { data, lamports } = accountInfo;
1177
+ const rentExempt = await this.connection.getMinimumBalanceForRentExemption(data.length);
1178
+ const availableBalance = Math.max(0, lamports - rentExempt);
1179
+ return availableBalance / LAMPORTS_PER_SOL;
1218
1180
  }
1219
1181
  /**
1220
1182
  * Get transaction history grouped by epochs
1221
- * @param voteAccount - The vote account to get history for
1183
+ * @param vote - The vote account to get history for
1222
1184
  * @param epochsCount - Number of recent epochs to return (default: 10)
1223
1185
  * @returns Array of epoch history items sorted by epoch (descending)
1224
1186
  */
1225
- async getHistoryGroupedByEpochs(voteAccount, epochsCount = 10) {
1226
- const currentEpoch = await this.getCurrentEpoch();
1227
- const fullHistory = await this.getFullHistory(voteAccount);
1187
+ async getHistoryGroupedByEpochs(vote, epochsCount = 10) {
1188
+ const epochInfo = await this.connection.getEpochInfo();
1189
+ const fullHistory = await this.getFullHistory(new PublicKey(vote));
1228
1190
  const epochMap = /* @__PURE__ */ new Map();
1229
1191
  for (const item of fullHistory) {
1230
- if (item.epoch < currentEpoch - epochsCount + 1) {
1192
+ if (item.epoch < epochInfo.epoch - epochsCount + 1) {
1231
1193
  continue;
1232
1194
  }
1233
1195
  if (!epochMap.has(item.epoch)) {
@@ -1254,8 +1216,11 @@ var JBondClient = class _JBondClient {
1254
1216
  }
1255
1217
  return [...epochMap.values()].toSorted((a, b) => b.epoch - a.epoch);
1256
1218
  }
1257
- async getHistory(voteAccount, options) {
1258
- const [ValidatorBondAccount] = this.pda.validatorBond(voteAccount);
1219
+ /**
1220
+ * Get transaction history for a specific validator bond account
1221
+ */
1222
+ async getHistory(vote, options) {
1223
+ const [ValidatorBondAccount] = this.pda.validatorBond(new PublicKey(vote));
1259
1224
  const signatures = (await this.connection.getSignaturesForAddress(
1260
1225
  ValidatorBondAccount,
1261
1226
  {