@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 +23 -2
- package/dist/index.d.ts +23 -2
- package/dist/index.js +176 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +178 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Program, AnchorProvider, BN } from '@coral-xyz/anchor';
|
|
2
|
+
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
|
|
2
3
|
import { getStakePoolAccount, STAKE_POOL_PROGRAM_ID, StakePoolInstruction } from '@solana/spl-stake-pool';
|
|
3
4
|
import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync, ACCOUNT_SIZE, AccountLayout } from '@solana/spl-token';
|
|
4
5
|
import { PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js';
|
|
5
|
-
import bs58 from 'bs58';
|
|
6
|
+
import bs58$1 from 'bs58';
|
|
6
7
|
|
|
7
8
|
var __create = Object.create;
|
|
8
9
|
var __defProp = Object.defineProperty;
|
|
@@ -4716,23 +4717,12 @@ var HistoryManager = class {
|
|
|
4716
4717
|
if (!type || amount <= 0) {
|
|
4717
4718
|
continue;
|
|
4718
4719
|
}
|
|
4719
|
-
let beforeBalance;
|
|
4720
|
-
let afterBalance;
|
|
4721
|
-
const accIdx = tx.transaction.message.accountKeys.findIndex(
|
|
4722
|
-
(k) => k.pubkey.equals(validatorBondAccount)
|
|
4723
|
-
);
|
|
4724
|
-
if (accIdx !== -1 && tx.meta.preBalances && tx.meta.postBalances) {
|
|
4725
|
-
beforeBalance = Number(tx.meta.preBalances[accIdx] ?? 0) / LAMPORTS_PER_SOL;
|
|
4726
|
-
afterBalance = Number(tx.meta.postBalances[accIdx] ?? 0) / LAMPORTS_PER_SOL;
|
|
4727
|
-
}
|
|
4728
4720
|
history.push({
|
|
4729
4721
|
signature: sig,
|
|
4730
4722
|
slot,
|
|
4731
4723
|
epoch: slotToEpoch(slot, cluster),
|
|
4732
4724
|
type,
|
|
4733
|
-
amount
|
|
4734
|
-
beforeBalance,
|
|
4735
|
-
afterBalance
|
|
4725
|
+
amount
|
|
4736
4726
|
});
|
|
4737
4727
|
}
|
|
4738
4728
|
}
|
|
@@ -4775,7 +4765,7 @@ var HistoryManager = class {
|
|
|
4775
4765
|
return { type: null, amount: 0 };
|
|
4776
4766
|
}
|
|
4777
4767
|
try {
|
|
4778
|
-
const buf = bs58.decode(data);
|
|
4768
|
+
const buf = bs58$1.decode(data);
|
|
4779
4769
|
if (buf.length < 16) {
|
|
4780
4770
|
return { type: null, amount: 0 };
|
|
4781
4771
|
}
|
|
@@ -7069,6 +7059,180 @@ var JBondClient = class _JBondClient {
|
|
|
7069
7059
|
}
|
|
7070
7060
|
});
|
|
7071
7061
|
}
|
|
7062
|
+
/**
|
|
7063
|
+
* Get transaction history for a specific validator bond account
|
|
7064
|
+
*/
|
|
7065
|
+
async getHistory(bondType, bondName, vote, options) {
|
|
7066
|
+
const [legacyValidatorBondAccount] = PublicKey.findProgramAddressSync(
|
|
7067
|
+
[
|
|
7068
|
+
Buffer.from("validator_bond" /* ValidatorBond */),
|
|
7069
|
+
new PublicKey(vote).toBuffer()
|
|
7070
|
+
],
|
|
7071
|
+
this.programId
|
|
7072
|
+
);
|
|
7073
|
+
const validatorBond = this.pda.validatorBond(bondType, bondName, vote);
|
|
7074
|
+
const legacySignatures = (await this.connection.getSignaturesForAddress(
|
|
7075
|
+
legacyValidatorBondAccount,
|
|
7076
|
+
{
|
|
7077
|
+
limit: options?.limit || 1e3,
|
|
7078
|
+
before: options?.before,
|
|
7079
|
+
until: options?.until
|
|
7080
|
+
},
|
|
7081
|
+
"confirmed"
|
|
7082
|
+
)).filter((sig) => !sig.err);
|
|
7083
|
+
const newSignatures = (await this.connection.getSignaturesForAddress(
|
|
7084
|
+
validatorBond[0],
|
|
7085
|
+
{
|
|
7086
|
+
limit: options?.limit || 1e3,
|
|
7087
|
+
before: options?.before,
|
|
7088
|
+
until: options?.until
|
|
7089
|
+
},
|
|
7090
|
+
"confirmed"
|
|
7091
|
+
)).filter((sig) => !sig.err);
|
|
7092
|
+
const signatureStrings = legacySignatures.map((sig) => sig.signature);
|
|
7093
|
+
signatureStrings.push(...newSignatures.map((sig) => sig.signature));
|
|
7094
|
+
const signatures = [...legacySignatures, ...newSignatures];
|
|
7095
|
+
const BATCH_SIZE = 100;
|
|
7096
|
+
const allTransactions = [];
|
|
7097
|
+
for (let i = 0; i < signatureStrings.length; i += BATCH_SIZE) {
|
|
7098
|
+
const batch = signatureStrings.slice(i, i + BATCH_SIZE);
|
|
7099
|
+
const transactions = await this.connection.getParsedTransactions(
|
|
7100
|
+
batch,
|
|
7101
|
+
{
|
|
7102
|
+
maxSupportedTransactionVersion: 0,
|
|
7103
|
+
commitment: "confirmed"
|
|
7104
|
+
}
|
|
7105
|
+
);
|
|
7106
|
+
allTransactions.push(...transactions);
|
|
7107
|
+
}
|
|
7108
|
+
const cluster = options?.cluster || "mainnet-beta";
|
|
7109
|
+
const history = [];
|
|
7110
|
+
for (const [idx, tx] of allTransactions.entries()) {
|
|
7111
|
+
const sigInfo = signatures[idx];
|
|
7112
|
+
if (!tx || !tx.meta || tx.meta.err !== null) {
|
|
7113
|
+
continue;
|
|
7114
|
+
}
|
|
7115
|
+
try {
|
|
7116
|
+
const slot = tx.slot || 0;
|
|
7117
|
+
const instructions = tx.transaction.message.instructions;
|
|
7118
|
+
for (const instruction of instructions) {
|
|
7119
|
+
if ("programId" in instruction && instruction.programId.equals(this.options.programId ?? this.program.programId)) {
|
|
7120
|
+
const ixData = instruction;
|
|
7121
|
+
if ("parsed" in ixData && ixData.parsed) {
|
|
7122
|
+
continue;
|
|
7123
|
+
}
|
|
7124
|
+
const data = ixData.data;
|
|
7125
|
+
if (!data) {
|
|
7126
|
+
continue;
|
|
7127
|
+
}
|
|
7128
|
+
let type = null;
|
|
7129
|
+
let amount = 0;
|
|
7130
|
+
try {
|
|
7131
|
+
const dataBuffer = bs58.decode(data);
|
|
7132
|
+
if (dataBuffer.length >= 8) {
|
|
7133
|
+
const discriminator = dataBuffer.subarray(0, 8);
|
|
7134
|
+
const legacyRegisterDiscriminator = this.getInstructionDiscriminator("register");
|
|
7135
|
+
const legacyTopUpDiscriminator = this.getInstructionDiscriminator("topUp");
|
|
7136
|
+
const legacyClaimDiscriminator = this.getInstructionDiscriminator("claim");
|
|
7137
|
+
const legacyWithdrawDiscriminator = this.getInstructionDiscriminator("withdraw");
|
|
7138
|
+
const bondRegisterDiscriminator = this.getInstructionDiscriminator("bondRegister");
|
|
7139
|
+
const bondTopUpDiscriminator = this.getInstructionDiscriminator("bondTopUp");
|
|
7140
|
+
const bondClaimDiscriminator = this.getInstructionDiscriminator("bondClaim");
|
|
7141
|
+
const bondWithdrawDiscriminator = this.getInstructionDiscriminator("bondWithdraw");
|
|
7142
|
+
console.log("Discriminator:", Buffer.from(discriminator).toString("hex"));
|
|
7143
|
+
if (Buffer.from(discriminator).equals(Buffer.from(legacyRegisterDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondRegisterDiscriminator))) {
|
|
7144
|
+
type = "deposit" /* Deposit */;
|
|
7145
|
+
if (dataBuffer.length >= 16 && Buffer.from(discriminator).equals(Buffer.from(legacyRegisterDiscriminator))) {
|
|
7146
|
+
const amountBytes = dataBuffer.subarray(8, 16);
|
|
7147
|
+
const amountBN = new import_bn2.BN(amountBytes, "le");
|
|
7148
|
+
amount = amountBN.toNumber() / LAMPORTS_PER_SOL;
|
|
7149
|
+
}
|
|
7150
|
+
} else if (dataBuffer.length >= 16) {
|
|
7151
|
+
const amountBytes = dataBuffer.subarray(8, 16);
|
|
7152
|
+
const amountBN = new import_bn2.BN(amountBytes, "le");
|
|
7153
|
+
amount = amountBN.toNumber() / LAMPORTS_PER_SOL;
|
|
7154
|
+
if (Buffer.from(discriminator).equals(Buffer.from(legacyTopUpDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondTopUpDiscriminator))) {
|
|
7155
|
+
type = "deposit" /* Deposit */;
|
|
7156
|
+
} else if (Buffer.from(discriminator).equals(Buffer.from(legacyClaimDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondClaimDiscriminator))) {
|
|
7157
|
+
type = "compensation" /* Compensation */;
|
|
7158
|
+
} else if (Buffer.from(discriminator).equals(Buffer.from(legacyWithdrawDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondWithdrawDiscriminator))) {
|
|
7159
|
+
type = "withdrawal" /* Withdrawal */;
|
|
7160
|
+
}
|
|
7161
|
+
}
|
|
7162
|
+
}
|
|
7163
|
+
} catch (e) {
|
|
7164
|
+
console.warn("Failed to decode instruction data:", e);
|
|
7165
|
+
}
|
|
7166
|
+
if (type && (amount > 0 || type === "deposit" /* Deposit */)) {
|
|
7167
|
+
history.push({
|
|
7168
|
+
signature: sigInfo.signature,
|
|
7169
|
+
slot,
|
|
7170
|
+
epoch: slotToEpoch(slot, cluster),
|
|
7171
|
+
type,
|
|
7172
|
+
amount
|
|
7173
|
+
});
|
|
7174
|
+
}
|
|
7175
|
+
}
|
|
7176
|
+
}
|
|
7177
|
+
} catch (error) {
|
|
7178
|
+
console.error(`Error processing transaction ${sigInfo.signature}:`, error);
|
|
7179
|
+
}
|
|
7180
|
+
}
|
|
7181
|
+
return history.toSorted((a, b) => b.slot - a.slot);
|
|
7182
|
+
}
|
|
7183
|
+
/**
|
|
7184
|
+
* Get full transaction history by paginating through results
|
|
7185
|
+
* @param bondType
|
|
7186
|
+
* @param bondName
|
|
7187
|
+
* @param voteAccount
|
|
7188
|
+
* @param pageSize
|
|
7189
|
+
*/
|
|
7190
|
+
async getFullHistory(bondType, bondName, voteAccount, pageSize = 100) {
|
|
7191
|
+
const allHistory = [];
|
|
7192
|
+
let before;
|
|
7193
|
+
while (true) {
|
|
7194
|
+
const batch = await this.getHistory(bondType, bondName, voteAccount, {
|
|
7195
|
+
limit: pageSize,
|
|
7196
|
+
before
|
|
7197
|
+
});
|
|
7198
|
+
if (batch.length === 0) {
|
|
7199
|
+
break;
|
|
7200
|
+
}
|
|
7201
|
+
allHistory.push(...batch);
|
|
7202
|
+
before = batch.at(-1)?.signature;
|
|
7203
|
+
if (batch.length < pageSize) {
|
|
7204
|
+
break;
|
|
7205
|
+
}
|
|
7206
|
+
}
|
|
7207
|
+
return allHistory;
|
|
7208
|
+
}
|
|
7209
|
+
/**
|
|
7210
|
+
* Get instruction discriminator from IDL
|
|
7211
|
+
* @param instructionName
|
|
7212
|
+
* @private
|
|
7213
|
+
*/
|
|
7214
|
+
getInstructionDiscriminator(instructionName) {
|
|
7215
|
+
const legacyDiscriminators = {
|
|
7216
|
+
register: [211, 124, 67, 15, 211, 194, 178, 240],
|
|
7217
|
+
topUp: [181, 157, 89, 67, 143, 182, 52, 72],
|
|
7218
|
+
claim: [62, 198, 214, 193, 213, 159, 108, 210],
|
|
7219
|
+
withdraw: [183, 18, 70, 156, 148, 109, 161, 34]
|
|
7220
|
+
};
|
|
7221
|
+
if (legacyDiscriminators[instructionName]) {
|
|
7222
|
+
return new Uint8Array(legacyDiscriminators[instructionName]);
|
|
7223
|
+
}
|
|
7224
|
+
if (instructionName.startsWith("bond")) {
|
|
7225
|
+
const instruction = this.program.idl.instructions.find((ix) => ix.name === instructionName);
|
|
7226
|
+
if (!instruction) {
|
|
7227
|
+
throw new Error(`Instruction ${instructionName} not found in IDL`);
|
|
7228
|
+
}
|
|
7229
|
+
if (!instruction.discriminator || !Array.isArray(instruction.discriminator)) {
|
|
7230
|
+
throw new Error(`Discriminator not found for instruction ${instructionName}`);
|
|
7231
|
+
}
|
|
7232
|
+
return new Uint8Array(instruction.discriminator);
|
|
7233
|
+
}
|
|
7234
|
+
throw new Error(`Unknown instruction: ${instructionName}`);
|
|
7235
|
+
}
|
|
7072
7236
|
/**
|
|
7073
7237
|
* Get bond state stats
|
|
7074
7238
|
* @param state
|