@jpool/bond-sdk 0.9.0-next.0 → 0.9.0-next.2

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
@@ -5,6 +5,11 @@ import bs58 from 'bs58';
5
5
  // src/client.ts
6
6
 
7
7
  // src/constants.ts
8
+ var Seeds = /* @__PURE__ */ ((Seeds2) => {
9
+ Seeds2["GlobalState"] = "global_state";
10
+ Seeds2["ValidatorBond"] = "validator_bond";
11
+ return Seeds2;
12
+ })(Seeds || {});
8
13
  var ENV_PROGRAM_ID = {
9
14
  // [BondClientEnv.DEV]: new PublicKey('...'),
10
15
  };
@@ -1074,14 +1079,14 @@ var JBondClient = class _JBondClient {
1074
1079
  pda = {
1075
1080
  globalState: () => {
1076
1081
  return PublicKey.findProgramAddressSync(
1077
- [Buffer.from("global_state")],
1082
+ [Buffer.from("global_state" /* GlobalState */)],
1078
1083
  this.programId
1079
1084
  );
1080
1085
  },
1081
1086
  validatorBond: (vote) => {
1082
1087
  return PublicKey.findProgramAddressSync(
1083
1088
  [
1084
- Buffer.from("validator_bond"),
1089
+ Buffer.from("validator_bond" /* ValidatorBond */),
1085
1090
  new PublicKey(vote).toBuffer()
1086
1091
  ],
1087
1092
  this.programId
@@ -1094,7 +1099,7 @@ var JBondClient = class _JBondClient {
1094
1099
  */
1095
1100
  async initialize(props) {
1096
1101
  const ix = await this.buildInitializeInstruction(props);
1097
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1102
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1098
1103
  }
1099
1104
  /**
1100
1105
  * Register a new validator
@@ -1102,7 +1107,7 @@ var JBondClient = class _JBondClient {
1102
1107
  */
1103
1108
  async registerValidator(props) {
1104
1109
  const ix = await this.buildRegisterValidatorInstruction(props);
1105
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1110
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1106
1111
  }
1107
1112
  /**
1108
1113
  * Top up collateral
@@ -1110,7 +1115,7 @@ var JBondClient = class _JBondClient {
1110
1115
  */
1111
1116
  async topUpCollateral(props) {
1112
1117
  const ix = await this.buildTopUpCollateralInstruction(props);
1113
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1118
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1114
1119
  }
1115
1120
  /**
1116
1121
  * Withdraw collateral
@@ -1120,7 +1125,7 @@ var JBondClient = class _JBondClient {
1120
1125
  */
1121
1126
  async withdrawCollateral(props) {
1122
1127
  const ix = await this.buildWithdrawCollateralInstruction(props);
1123
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1128
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1124
1129
  }
1125
1130
  /**
1126
1131
  * Claim compensation
@@ -1129,7 +1134,7 @@ var JBondClient = class _JBondClient {
1129
1134
  */
1130
1135
  async claimCompensation(props) {
1131
1136
  const ix = await this.buildClaimInstruction(props);
1132
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1137
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1133
1138
  }
1134
1139
  /**
1135
1140
  * Update global configuration for the program
@@ -1139,7 +1144,7 @@ var JBondClient = class _JBondClient {
1139
1144
  */
1140
1145
  async configure(props) {
1141
1146
  const ix = await this.buildConfigureInstruction(props);
1142
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1147
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1143
1148
  }
1144
1149
  /**
1145
1150
  * Set withdrawal authority for validator bond
@@ -1149,7 +1154,7 @@ var JBondClient = class _JBondClient {
1149
1154
  */
1150
1155
  async setWithdrawAuthority(props) {
1151
1156
  const ix = await this.buildSetWithdrawAuthorityInstruction(props);
1152
- return await this.provider.sendAndConfirm(new Transaction().add(ix));
1157
+ return this.provider.sendAndConfirm?.(new Transaction().add(ix));
1153
1158
  }
1154
1159
  /**
1155
1160
  * Build initialize instruction
@@ -1157,7 +1162,7 @@ var JBondClient = class _JBondClient {
1157
1162
  async buildInitializeInstruction(props) {
1158
1163
  const [globalState] = this.pda.globalState();
1159
1164
  const { reserve } = props;
1160
- const authority = props.authority ?? this.provider.wallet.publicKey;
1165
+ const authority = props.authority ?? this.provider.wallet?.publicKey;
1161
1166
  return this.program.methods.initialize().accountsPartial({
1162
1167
  globalState,
1163
1168
  authority,
@@ -1172,7 +1177,7 @@ var JBondClient = class _JBondClient {
1172
1177
  const [globalState] = this.pda.globalState();
1173
1178
  const [validatorBond] = this.pda.validatorBond(voteAccount);
1174
1179
  const lamports = new BN(initialCollateral * LAMPORTS_PER_SOL);
1175
- const creator = props.creator ?? this.provider.wallet.publicKey;
1180
+ const creator = props.creator ?? this.provider.wallet?.publicKey;
1176
1181
  return this.program.methods.register(lamports).accountsPartial({
1177
1182
  creator,
1178
1183
  globalState,
@@ -1188,7 +1193,7 @@ var JBondClient = class _JBondClient {
1188
1193
  const { voteAccount, amount } = props;
1189
1194
  const [validatorBond] = this.pda.validatorBond(voteAccount);
1190
1195
  const lamports = new BN(amount * LAMPORTS_PER_SOL);
1191
- const payer = props.payer ?? this.provider.wallet.publicKey;
1196
+ const payer = props.payer ?? this.provider.wallet?.publicKey;
1192
1197
  return this.program.methods.topUp(lamports).accountsPartial({
1193
1198
  validatorBond,
1194
1199
  payer
@@ -1201,8 +1206,8 @@ var JBondClient = class _JBondClient {
1201
1206
  const { voteAccount, amount } = props;
1202
1207
  const [validatorBond] = this.pda.validatorBond(voteAccount);
1203
1208
  const lamports = new BN(amount * LAMPORTS_PER_SOL);
1204
- const withdrawalAuthority = props.withdrawalAuthority ?? this.provider.wallet.publicKey;
1205
- const destination = props.destination ?? this.provider.wallet.publicKey;
1209
+ const withdrawalAuthority = props.withdrawalAuthority ?? this.provider.wallet?.publicKey;
1210
+ const destination = props.destination ?? this.provider.wallet?.publicKey;
1206
1211
  return this.program.methods.withdraw(lamports).accountsPartial({
1207
1212
  validatorBond,
1208
1213
  withdrawalAuthority,
@@ -1217,7 +1222,7 @@ var JBondClient = class _JBondClient {
1217
1222
  const [globalState] = this.pda.globalState();
1218
1223
  const [validatorBond] = this.pda.validatorBond(props.voteAccount);
1219
1224
  const lamports = new BN(props.amount * LAMPORTS_PER_SOL);
1220
- const authority = props.authority ?? this.provider.wallet.publicKey;
1225
+ const authority = props.authority ?? this.provider.wallet?.publicKey;
1221
1226
  let reserve = props.reserve;
1222
1227
  if (!reserve) {
1223
1228
  const globalStateData = await this.getGlobalState();
@@ -1238,7 +1243,7 @@ var JBondClient = class _JBondClient {
1238
1243
  */
1239
1244
  async buildConfigureInstruction(props) {
1240
1245
  const [globalState] = this.pda.globalState();
1241
- const authority = props.authority ?? this.provider.wallet.publicKey;
1246
+ const authority = props.authority ?? this.provider.wallet?.publicKey;
1242
1247
  return this.program.methods.configure({
1243
1248
  newAuthority: props.newAuthority ?? null,
1244
1249
  newReserve: props.newReserve ?? null
@@ -1253,7 +1258,7 @@ var JBondClient = class _JBondClient {
1253
1258
  async buildSetWithdrawAuthorityInstruction(props) {
1254
1259
  const { voteAccount, newWithdrawAuthority } = props;
1255
1260
  const [validatorBond] = this.pda.validatorBond(voteAccount);
1256
- const identity = props.identity ?? this.provider.wallet.publicKey;
1261
+ const identity = props.identity ?? this.provider.wallet?.publicKey;
1257
1262
  return this.program.methods.setWithdrawAuthority().accountsPartial({
1258
1263
  validatorBond,
1259
1264
  identity,
@@ -1291,167 +1296,82 @@ var JBondClient = class _JBondClient {
1291
1296
  const availableBalance = Math.max(0, lamports - rentExempt);
1292
1297
  return availableBalance / LAMPORTS_PER_SOL;
1293
1298
  }
1294
- /**
1295
- * Get transaction history grouped by epochs
1296
- * @param vote - The vote account to get history for
1297
- * @param epochsCount - Number of recent epochs to return (default: 10)
1298
- * @returns Array of epoch history items sorted by epoch (descending)
1299
- */
1300
- async getHistoryGroupedByEpochs(vote, epochsCount = 10) {
1301
- const epochInfo = await this.connection.getEpochInfo();
1302
- const fullHistory = await this.getFullHistory(new PublicKey(vote));
1303
- const epochMap = /* @__PURE__ */ new Map();
1304
- for (const item of fullHistory) {
1305
- if (item.epoch < epochInfo.epoch - epochsCount + 1) {
1306
- continue;
1307
- }
1308
- if (!epochMap.has(item.epoch)) {
1309
- epochMap.set(item.epoch, {
1310
- epoch: item.epoch,
1311
- deposits: 0,
1312
- withdrawals: 0,
1313
- balanceChange: 0,
1314
- signatures: []
1315
- });
1316
- }
1317
- const epochData = epochMap.get(item.epoch);
1318
- if (item.type === "deposit") {
1319
- epochData.deposits += item.amount;
1320
- } else if (item.type === "withdrawal") {
1321
- epochData.withdrawals += item.amount;
1322
- }
1323
- if (!epochData.signatures.includes(item.signature)) {
1324
- epochData.signatures.push(item.signature);
1325
- }
1326
- }
1327
- for (const epochData of epochMap.values()) {
1328
- epochData.balanceChange = epochData.deposits - epochData.withdrawals;
1329
- }
1330
- return [...epochMap.values()].toSorted((a, b) => b.epoch - a.epoch);
1331
- }
1332
1299
  /**
1333
1300
  * Get transaction history for a specific validator bond account
1334
1301
  */
1335
1302
  async getHistory(vote, options) {
1336
- const [ValidatorBondAccount] = this.pda.validatorBond(new PublicKey(vote));
1337
- const signatures = (await this.connection.getSignaturesForAddress(
1338
- ValidatorBondAccount,
1339
- {
1340
- limit: options?.limit || 1e3,
1341
- before: options?.before,
1342
- until: options?.until
1343
- }
1344
- )).filter((sig) => !sig.err);
1345
- const signatureStrings = signatures.map((sig) => sig.signature);
1346
- const BATCH_SIZE = 100;
1347
- const allTransactions = [];
1348
- for (let i = 0; i < signatureStrings.length; i += BATCH_SIZE) {
1349
- const batch = signatureStrings.slice(i, i + BATCH_SIZE);
1350
- const transactions = await this.connection.getParsedTransactions(
1351
- batch,
1352
- {
1353
- maxSupportedTransactionVersion: 0
1303
+ const cluster = options?.cluster || "mainnet-beta";
1304
+ const [validatorBondAccount] = this.pda.validatorBond(vote);
1305
+ const signatures = await this.fetchSignaturesForValidator(validatorBondAccount, {
1306
+ limit: options?.limit,
1307
+ before: options?.before,
1308
+ until: options?.until
1309
+ });
1310
+ if (signatures.length === 0) {
1311
+ return [];
1312
+ }
1313
+ const signatureStrings = signatures.map((s) => s.signature);
1314
+ const parsedTxs = await this.fetchParsedTransactions(signatureStrings);
1315
+ const txBySig = /* @__PURE__ */ new Map();
1316
+ for (let i = 0; i < signatureStrings.length; i++) {
1317
+ const requestedSig = signatureStrings[i];
1318
+ const tx = parsedTxs[i] ?? null;
1319
+ if (tx && tx.transaction && Array.isArray(tx.transaction.signatures) && tx.transaction.signatures[0]) {
1320
+ const txSig = tx.transaction.signatures[0];
1321
+ if (txSig !== requestedSig) {
1322
+ const found = parsedTxs.find((t) => t && t.transaction && t.transaction.signatures && t.transaction.signatures[0] === requestedSig) ?? null;
1323
+ txBySig.set(requestedSig, found);
1324
+ continue;
1354
1325
  }
1355
- );
1356
- allTransactions.push(...transactions);
1326
+ }
1327
+ txBySig.set(requestedSig, tx);
1357
1328
  }
1358
- const cluster = options?.cluster || "mainnet-beta";
1359
1329
  const history = [];
1360
- for (const [idx, tx] of allTransactions.entries()) {
1361
- const sigInfo = signatures[idx];
1330
+ for (const sigInfo of signatures) {
1331
+ const sig = sigInfo.signature;
1332
+ const tx = txBySig.get(sig) ?? null;
1362
1333
  if (!tx || !tx.meta || tx.meta.err !== null) {
1363
1334
  continue;
1364
1335
  }
1365
1336
  try {
1366
- const slot = tx.slot || 0;
1337
+ const slot = tx.slot ?? 0;
1367
1338
  const instructions = tx.transaction.message.instructions;
1368
1339
  for (const instruction of instructions) {
1369
- if ("programId" in instruction && instruction.programId.equals(this.options.programId ?? this.program.programId)) {
1370
- const ixData = instruction;
1371
- if ("parsed" in ixData && ixData.parsed) {
1372
- continue;
1373
- }
1374
- const data = ixData.data;
1375
- if (!data) {
1376
- continue;
1377
- }
1378
- let type = null;
1379
- let amount = 0;
1380
- try {
1381
- const dataBuffer = bs58.decode(data);
1382
- if (dataBuffer.length >= 16) {
1383
- const discriminator = dataBuffer.slice(0, 8);
1384
- const bondInitDiscriminator = this.getInstructionDiscriminator("register");
1385
- const bondTopUpDiscriminator = this.getInstructionDiscriminator("topUp");
1386
- const withdrawCompensationDiscriminator = this.getInstructionDiscriminator("claim");
1387
- const bondWithdrawDiscriminator = this.getInstructionDiscriminator("withdraw");
1388
- const amountBytes = dataBuffer.slice(8, 16);
1389
- const amountBN = new BN(amountBytes, "le");
1390
- amount = amountBN.toNumber() / LAMPORTS_PER_SOL;
1391
- if (Buffer.from(discriminator).equals(Buffer.from(bondInitDiscriminator))) {
1392
- type = "deposit" /* Deposit */;
1393
- } else if (Buffer.from(discriminator).equals(Buffer.from(bondTopUpDiscriminator))) {
1394
- type = "deposit" /* Deposit */;
1395
- } else if (Buffer.from(discriminator).equals(Buffer.from(withdrawCompensationDiscriminator))) {
1396
- type = "compensation" /* Compensation */;
1397
- } else if (Buffer.from(discriminator).equals(Buffer.from(bondWithdrawDiscriminator))) {
1398
- type = "withdrawal" /* Withdrawal */;
1399
- }
1400
- }
1401
- } catch {
1402
- console.warn("Failed to decode instruction data");
1403
- }
1404
- if (type && amount > 0) {
1405
- let beforeBalance;
1406
- let afterBalance;
1407
- const accountIndex = tx.transaction.message.accountKeys.findIndex(
1408
- (key) => key.pubkey.equals(ValidatorBondAccount)
1409
- );
1410
- if (accountIndex !== -1 && tx.meta.preBalances && tx.meta.postBalances) {
1411
- beforeBalance = Number(tx.meta.preBalances[accountIndex] || 0) / LAMPORTS_PER_SOL;
1412
- afterBalance = Number(tx.meta.postBalances[accountIndex] || 0) / LAMPORTS_PER_SOL;
1413
- }
1414
- history.push({
1415
- signature: sigInfo.signature,
1416
- slot,
1417
- epoch: slotToEpoch(slot, cluster),
1418
- type,
1419
- amount,
1420
- beforeBalance,
1421
- afterBalance
1422
- });
1423
- }
1340
+ if (!("programId" in instruction)) {
1341
+ continue;
1342
+ }
1343
+ if (!instruction.programId.equals(this.options.programId ?? this.program.programId)) {
1344
+ continue;
1345
+ }
1346
+ if (instruction.parsed) {
1347
+ continue;
1424
1348
  }
1349
+ const { type, amount } = this.decodeInstructionData(instruction.data);
1350
+ if (!type || amount <= 0) {
1351
+ continue;
1352
+ }
1353
+ let beforeBalance;
1354
+ let afterBalance;
1355
+ const accountIndex = tx.transaction.message.accountKeys.findIndex((k) => k.pubkey.equals(validatorBondAccount));
1356
+ if (accountIndex !== -1 && tx.meta.preBalances && tx.meta.postBalances) {
1357
+ beforeBalance = Number(tx.meta.preBalances[accountIndex] || 0) / LAMPORTS_PER_SOL;
1358
+ afterBalance = Number(tx.meta.postBalances[accountIndex] || 0) / LAMPORTS_PER_SOL;
1359
+ }
1360
+ history.push({
1361
+ signature: sigInfo.signature,
1362
+ slot,
1363
+ epoch: slotToEpoch(slot, cluster),
1364
+ type,
1365
+ amount,
1366
+ beforeBalance,
1367
+ afterBalance
1368
+ });
1425
1369
  }
1426
1370
  } catch (error) {
1427
1371
  console.error(`Error processing transaction ${sigInfo.signature}:`, error);
1428
1372
  }
1429
1373
  }
1430
- return history.toSorted((a, b) => b.slot - a.slot);
1431
- }
1432
- /**
1433
- * Get full transaction history by paginating through results
1434
- * @param voteAccount
1435
- * @param pageSize
1436
- */
1437
- async getFullHistory(voteAccount, pageSize = 100) {
1438
- const allHistory = [];
1439
- let before;
1440
- while (true) {
1441
- const batch = await this.getHistory(voteAccount, {
1442
- limit: pageSize,
1443
- before
1444
- });
1445
- if (batch.length === 0) {
1446
- break;
1447
- }
1448
- allHistory.push(...batch);
1449
- before = batch.at(-1)?.signature;
1450
- if (batch.length < pageSize) {
1451
- break;
1452
- }
1453
- }
1454
- return allHistory;
1374
+ return history.sort((a, b) => b.slot - a.slot);
1455
1375
  }
1456
1376
  /**
1457
1377
  * Get instruction discriminator from IDL
@@ -1468,8 +1388,71 @@ var JBondClient = class _JBondClient {
1468
1388
  }
1469
1389
  return new Uint8Array(instruction.discriminator);
1470
1390
  }
1391
+ async fetchSignaturesForValidator(validatorBondAccount, options) {
1392
+ const sigs = await this.connection.getSignaturesForAddress(
1393
+ validatorBondAccount,
1394
+ {
1395
+ limit: options?.limit || 1e3,
1396
+ before: options?.before,
1397
+ until: options?.until
1398
+ }
1399
+ );
1400
+ return sigs.filter((s) => !s.err);
1401
+ }
1402
+ // Fetch parsed transactions in batches to avoid rate limits
1403
+ async fetchParsedTransactions(signatureStrings, batchSize = 100) {
1404
+ const allTxs = [];
1405
+ for (let i = 0; i < signatureStrings.length; i += batchSize) {
1406
+ const batch = signatureStrings.slice(i, i + batchSize);
1407
+ const txs = await this.connection.getParsedTransactions(batch, {
1408
+ maxSupportedTransactionVersion: 0
1409
+ });
1410
+ allTxs.push(...txs);
1411
+ }
1412
+ return allTxs;
1413
+ }
1414
+ /**
1415
+ * Decode instruction data to determine transaction type and amount
1416
+ * @param data
1417
+ * @private
1418
+ */
1419
+ decodeInstructionData(data) {
1420
+ if (!data) {
1421
+ return { type: null, amount: 0 };
1422
+ }
1423
+ try {
1424
+ const dataBuffer = bs58.decode(data);
1425
+ if (dataBuffer.length < 16) {
1426
+ return { type: null, amount: 0 };
1427
+ }
1428
+ const discriminator = dataBuffer.slice(0, 8);
1429
+ const amountBytes = dataBuffer.slice(8, 16);
1430
+ const amountBN = new BN(amountBytes, "le");
1431
+ const amount = amountBN.toNumber() / LAMPORTS_PER_SOL;
1432
+ const bondInitDiscriminator = this.getInstructionDiscriminator("register");
1433
+ const bondTopUpDiscriminator = this.getInstructionDiscriminator("topUp");
1434
+ const withdrawCompensationDiscriminator = this.getInstructionDiscriminator("claim");
1435
+ const bondWithdrawDiscriminator = this.getInstructionDiscriminator("withdraw");
1436
+ if (Buffer.from(discriminator).equals(Buffer.from(bondInitDiscriminator))) {
1437
+ return { type: "deposit" /* Deposit */, amount };
1438
+ }
1439
+ if (Buffer.from(discriminator).equals(Buffer.from(bondTopUpDiscriminator))) {
1440
+ return { type: "deposit" /* Deposit */, amount };
1441
+ }
1442
+ if (Buffer.from(discriminator).equals(Buffer.from(withdrawCompensationDiscriminator))) {
1443
+ return { type: "compensation" /* Compensation */, amount };
1444
+ }
1445
+ if (Buffer.from(discriminator).equals(Buffer.from(bondWithdrawDiscriminator))) {
1446
+ return { type: "withdrawal" /* Withdrawal */, amount };
1447
+ }
1448
+ return { type: null, amount: 0 };
1449
+ } catch (err) {
1450
+ console.warn("Failed to decode instruction data", err);
1451
+ return { type: null, amount: 0 };
1452
+ }
1453
+ }
1471
1454
  };
1472
1455
 
1473
- export { BondClientEnv, BondTransactionType, ENV_PROGRAM_ID, JBondClient, NodeWallet };
1456
+ export { BondClientEnv, BondTransactionType, ENV_PROGRAM_ID, JBondClient, NodeWallet, Seeds };
1474
1457
  //# sourceMappingURL=index.mjs.map
1475
1458
  //# sourceMappingURL=index.mjs.map