@jpool/bond-sdk 0.9.0-next.24 → 0.9.0-next.26

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.d.mts CHANGED
@@ -1728,8 +1728,6 @@ type TransactionHistoryItem = {
1728
1728
  epoch: number;
1729
1729
  type: BondTransactionType;
1730
1730
  amount: number;
1731
- beforeBalance?: number;
1732
- afterBalance?: number;
1733
1731
  };
1734
1732
  declare enum BondTransactionType {
1735
1733
  Deposit = "deposit",
@@ -2024,6 +2022,29 @@ declare class JBondClient {
2024
2022
  * Get total collected collateral for a bond state
2025
2023
  */
2026
2024
  getBondStateTotalCollected(bondType: BondType, bondName: string, votes: PublicKeyInitData[]): Promise<number>;
2025
+ /**
2026
+ * Get transaction history for a specific validator bond account
2027
+ */
2028
+ getHistory(bondType: BondType, bondName: string, vote: PublicKey, options?: {
2029
+ cluster?: 'mainnet-beta' | 'testnet' | 'devnet';
2030
+ limit?: number;
2031
+ before?: string;
2032
+ until?: string;
2033
+ }): Promise<TransactionHistoryItem[]>;
2034
+ /**
2035
+ * Get full transaction history by paginating through results
2036
+ * @param bondType
2037
+ * @param bondName
2038
+ * @param voteAccount
2039
+ * @param pageSize
2040
+ */
2041
+ getFullHistory(bondType: BondType, bondName: string, voteAccount: PublicKey, pageSize?: number): Promise<TransactionHistoryItem[]>;
2042
+ /**
2043
+ * Get instruction discriminator from IDL
2044
+ * @param instructionName
2045
+ * @private
2046
+ */
2047
+ private getInstructionDiscriminator;
2027
2048
  /**
2028
2049
  * Get bond state stats
2029
2050
  * @param state
package/dist/index.d.ts CHANGED
@@ -1728,8 +1728,6 @@ type TransactionHistoryItem = {
1728
1728
  epoch: number;
1729
1729
  type: BondTransactionType;
1730
1730
  amount: number;
1731
- beforeBalance?: number;
1732
- afterBalance?: number;
1733
1731
  };
1734
1732
  declare enum BondTransactionType {
1735
1733
  Deposit = "deposit",
@@ -2024,6 +2022,29 @@ declare class JBondClient {
2024
2022
  * Get total collected collateral for a bond state
2025
2023
  */
2026
2024
  getBondStateTotalCollected(bondType: BondType, bondName: string, votes: PublicKeyInitData[]): Promise<number>;
2025
+ /**
2026
+ * Get transaction history for a specific validator bond account
2027
+ */
2028
+ getHistory(bondType: BondType, bondName: string, vote: PublicKey, options?: {
2029
+ cluster?: 'mainnet-beta' | 'testnet' | 'devnet';
2030
+ limit?: number;
2031
+ before?: string;
2032
+ until?: string;
2033
+ }): Promise<TransactionHistoryItem[]>;
2034
+ /**
2035
+ * Get full transaction history by paginating through results
2036
+ * @param bondType
2037
+ * @param bondName
2038
+ * @param voteAccount
2039
+ * @param pageSize
2040
+ */
2041
+ getFullHistory(bondType: BondType, bondName: string, voteAccount: PublicKey, pageSize?: number): Promise<TransactionHistoryItem[]>;
2042
+ /**
2043
+ * Get instruction discriminator from IDL
2044
+ * @param instructionName
2045
+ * @private
2046
+ */
2047
+ private getInstructionDiscriminator;
2027
2048
  /**
2028
2049
  * Get bond state stats
2029
2050
  * @param state
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var anchor = require('@coral-xyz/anchor');
4
+ var bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
4
5
  var splStakePool = require('@solana/spl-stake-pool');
5
6
  var splToken = require('@solana/spl-token');
6
7
  var web3_js = require('@solana/web3.js');
@@ -4751,23 +4752,12 @@ var HistoryManager = class {
4751
4752
  if (!type || amount <= 0) {
4752
4753
  continue;
4753
4754
  }
4754
- let beforeBalance;
4755
- let afterBalance;
4756
- const accIdx = tx.transaction.message.accountKeys.findIndex(
4757
- (k) => k.pubkey.equals(validatorBondAccount)
4758
- );
4759
- if (accIdx !== -1 && tx.meta.preBalances && tx.meta.postBalances) {
4760
- beforeBalance = Number(tx.meta.preBalances[accIdx] ?? 0) / web3_js.LAMPORTS_PER_SOL;
4761
- afterBalance = Number(tx.meta.postBalances[accIdx] ?? 0) / web3_js.LAMPORTS_PER_SOL;
4762
- }
4763
4755
  history.push({
4764
4756
  signature: sig,
4765
4757
  slot,
4766
4758
  epoch: slotToEpoch(slot, cluster),
4767
4759
  type,
4768
- amount,
4769
- beforeBalance,
4770
- afterBalance
4760
+ amount
4771
4761
  });
4772
4762
  }
4773
4763
  }
@@ -7105,6 +7095,180 @@ var JBondClient = class _JBondClient {
7105
7095
  }
7106
7096
  });
7107
7097
  }
7098
+ /**
7099
+ * Get transaction history for a specific validator bond account
7100
+ */
7101
+ async getHistory(bondType, bondName, vote, options) {
7102
+ const [legacyValidatorBondAccount] = web3_js.PublicKey.findProgramAddressSync(
7103
+ [
7104
+ Buffer.from("validator_bond" /* ValidatorBond */),
7105
+ new web3_js.PublicKey(vote).toBuffer()
7106
+ ],
7107
+ this.programId
7108
+ );
7109
+ const validatorBond = this.pda.validatorBond(bondType, bondName, vote);
7110
+ const legacySignatures = (await this.connection.getSignaturesForAddress(
7111
+ legacyValidatorBondAccount,
7112
+ {
7113
+ limit: options?.limit || 1e3,
7114
+ before: options?.before,
7115
+ until: options?.until
7116
+ },
7117
+ "confirmed"
7118
+ )).filter((sig) => !sig.err);
7119
+ const newSignatures = (await this.connection.getSignaturesForAddress(
7120
+ validatorBond[0],
7121
+ {
7122
+ limit: options?.limit || 1e3,
7123
+ before: options?.before,
7124
+ until: options?.until
7125
+ },
7126
+ "confirmed"
7127
+ )).filter((sig) => !sig.err);
7128
+ const signatureStrings = legacySignatures.map((sig) => sig.signature);
7129
+ signatureStrings.push(...newSignatures.map((sig) => sig.signature));
7130
+ const signatures = [...legacySignatures, ...newSignatures];
7131
+ const BATCH_SIZE = 100;
7132
+ const allTransactions = [];
7133
+ for (let i = 0; i < signatureStrings.length; i += BATCH_SIZE) {
7134
+ const batch = signatureStrings.slice(i, i + BATCH_SIZE);
7135
+ const transactions = await this.connection.getParsedTransactions(
7136
+ batch,
7137
+ {
7138
+ maxSupportedTransactionVersion: 0,
7139
+ commitment: "confirmed"
7140
+ }
7141
+ );
7142
+ allTransactions.push(...transactions);
7143
+ }
7144
+ const cluster = options?.cluster || "mainnet-beta";
7145
+ const history = [];
7146
+ for (const [idx, tx] of allTransactions.entries()) {
7147
+ const sigInfo = signatures[idx];
7148
+ if (!tx || !tx.meta || tx.meta.err !== null) {
7149
+ continue;
7150
+ }
7151
+ try {
7152
+ const slot = tx.slot || 0;
7153
+ const instructions = tx.transaction.message.instructions;
7154
+ for (const instruction of instructions) {
7155
+ if ("programId" in instruction && instruction.programId.equals(this.options.programId ?? this.program.programId)) {
7156
+ const ixData = instruction;
7157
+ if ("parsed" in ixData && ixData.parsed) {
7158
+ continue;
7159
+ }
7160
+ const data = ixData.data;
7161
+ if (!data) {
7162
+ continue;
7163
+ }
7164
+ let type = null;
7165
+ let amount = 0;
7166
+ try {
7167
+ const dataBuffer = bytes.bs58.decode(data);
7168
+ if (dataBuffer.length >= 8) {
7169
+ const discriminator = dataBuffer.subarray(0, 8);
7170
+ const legacyRegisterDiscriminator = this.getInstructionDiscriminator("register");
7171
+ const legacyTopUpDiscriminator = this.getInstructionDiscriminator("topUp");
7172
+ const legacyClaimDiscriminator = this.getInstructionDiscriminator("claim");
7173
+ const legacyWithdrawDiscriminator = this.getInstructionDiscriminator("withdraw");
7174
+ const bondRegisterDiscriminator = this.getInstructionDiscriminator("bondRegister");
7175
+ const bondTopUpDiscriminator = this.getInstructionDiscriminator("bondTopUp");
7176
+ const bondClaimDiscriminator = this.getInstructionDiscriminator("bondClaim");
7177
+ const bondWithdrawDiscriminator = this.getInstructionDiscriminator("bondWithdraw");
7178
+ console.log("Discriminator:", Buffer.from(discriminator).toString("hex"));
7179
+ if (Buffer.from(discriminator).equals(Buffer.from(legacyRegisterDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondRegisterDiscriminator))) {
7180
+ type = "deposit" /* Deposit */;
7181
+ if (dataBuffer.length >= 16 && Buffer.from(discriminator).equals(Buffer.from(legacyRegisterDiscriminator))) {
7182
+ const amountBytes = dataBuffer.subarray(8, 16);
7183
+ const amountBN = new import_bn2.BN(amountBytes, "le");
7184
+ amount = amountBN.toNumber() / web3_js.LAMPORTS_PER_SOL;
7185
+ }
7186
+ } else if (dataBuffer.length >= 16) {
7187
+ const amountBytes = dataBuffer.subarray(8, 16);
7188
+ const amountBN = new import_bn2.BN(amountBytes, "le");
7189
+ amount = amountBN.toNumber() / web3_js.LAMPORTS_PER_SOL;
7190
+ if (Buffer.from(discriminator).equals(Buffer.from(legacyTopUpDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondTopUpDiscriminator))) {
7191
+ type = "deposit" /* Deposit */;
7192
+ } else if (Buffer.from(discriminator).equals(Buffer.from(legacyClaimDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondClaimDiscriminator))) {
7193
+ type = "compensation" /* Compensation */;
7194
+ } else if (Buffer.from(discriminator).equals(Buffer.from(legacyWithdrawDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondWithdrawDiscriminator))) {
7195
+ type = "withdrawal" /* Withdrawal */;
7196
+ }
7197
+ }
7198
+ }
7199
+ } catch (e) {
7200
+ console.warn("Failed to decode instruction data:", e);
7201
+ }
7202
+ if (type && (amount > 0 || type === "deposit" /* Deposit */)) {
7203
+ history.push({
7204
+ signature: sigInfo.signature,
7205
+ slot,
7206
+ epoch: slotToEpoch(slot, cluster),
7207
+ type,
7208
+ amount
7209
+ });
7210
+ }
7211
+ }
7212
+ }
7213
+ } catch (error) {
7214
+ console.error(`Error processing transaction ${sigInfo.signature}:`, error);
7215
+ }
7216
+ }
7217
+ return history.toSorted((a, b) => b.slot - a.slot);
7218
+ }
7219
+ /**
7220
+ * Get full transaction history by paginating through results
7221
+ * @param bondType
7222
+ * @param bondName
7223
+ * @param voteAccount
7224
+ * @param pageSize
7225
+ */
7226
+ async getFullHistory(bondType, bondName, voteAccount, pageSize = 100) {
7227
+ const allHistory = [];
7228
+ let before;
7229
+ while (true) {
7230
+ const batch = await this.getHistory(bondType, bondName, voteAccount, {
7231
+ limit: pageSize,
7232
+ before
7233
+ });
7234
+ if (batch.length === 0) {
7235
+ break;
7236
+ }
7237
+ allHistory.push(...batch);
7238
+ before = batch.at(-1)?.signature;
7239
+ if (batch.length < pageSize) {
7240
+ break;
7241
+ }
7242
+ }
7243
+ return allHistory;
7244
+ }
7245
+ /**
7246
+ * Get instruction discriminator from IDL
7247
+ * @param instructionName
7248
+ * @private
7249
+ */
7250
+ getInstructionDiscriminator(instructionName) {
7251
+ const legacyDiscriminators = {
7252
+ register: [211, 124, 67, 15, 211, 194, 178, 240],
7253
+ topUp: [181, 157, 89, 67, 143, 182, 52, 72],
7254
+ claim: [62, 198, 214, 193, 213, 159, 108, 210],
7255
+ withdraw: [183, 18, 70, 156, 148, 109, 161, 34]
7256
+ };
7257
+ if (legacyDiscriminators[instructionName]) {
7258
+ return new Uint8Array(legacyDiscriminators[instructionName]);
7259
+ }
7260
+ if (instructionName.startsWith("bond")) {
7261
+ const instruction = this.program.idl.instructions.find((ix) => ix.name === instructionName);
7262
+ if (!instruction) {
7263
+ throw new Error(`Instruction ${instructionName} not found in IDL`);
7264
+ }
7265
+ if (!instruction.discriminator || !Array.isArray(instruction.discriminator)) {
7266
+ throw new Error(`Discriminator not found for instruction ${instructionName}`);
7267
+ }
7268
+ return new Uint8Array(instruction.discriminator);
7269
+ }
7270
+ throw new Error(`Unknown instruction: ${instructionName}`);
7271
+ }
7108
7272
  /**
7109
7273
  * Get bond state stats
7110
7274
  * @param state