@jpool/bond-sdk 0.9.0-next.23 → 0.9.0-next.25
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 +27 -5
- package/dist/index.d.ts +27 -5
- package/dist/index.js +188 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +190 -19
- 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
|
}
|
|
@@ -4807,7 +4797,7 @@ var HistoryManager = class {
|
|
|
4807
4797
|
|
|
4808
4798
|
// src/idl/jbond.json
|
|
4809
4799
|
var jbond_default = {
|
|
4810
|
-
address: "
|
|
4800
|
+
address: "BondQ7KqZreTcW2UbeTNDcLCJQ3aXAtLn2Fm6ftaJDU",
|
|
4811
4801
|
metadata: {
|
|
4812
4802
|
name: "jbond",
|
|
4813
4803
|
version: "0.2.1",
|
|
@@ -6006,7 +5996,7 @@ var jbond_default = {
|
|
|
6006
5996
|
{
|
|
6007
5997
|
name: "BondInitializeData",
|
|
6008
5998
|
docs: [
|
|
6009
|
-
"Data required to initialize a
|
|
5999
|
+
"Data required to initialize a performance bond"
|
|
6010
6000
|
],
|
|
6011
6001
|
type: {
|
|
6012
6002
|
kind: "struct",
|
|
@@ -6905,19 +6895,20 @@ var JBondClient = class _JBondClient {
|
|
|
6905
6895
|
/**
|
|
6906
6896
|
* Get all bond states with total collected collateral
|
|
6907
6897
|
*/
|
|
6908
|
-
async getAllBondStates(bondType,
|
|
6898
|
+
async getAllBondStates(bondType, sessionStatus) {
|
|
6909
6899
|
const bondStates = [];
|
|
6910
6900
|
const bondStateAccounts = await this.program.account.bondState.all();
|
|
6911
|
-
for (const { account: state } of bondStateAccounts) {
|
|
6901
|
+
for (const { publicKey, account: state } of bondStateAccounts) {
|
|
6912
6902
|
if (!sameVariant(state.bondType, bondType)) {
|
|
6913
6903
|
continue;
|
|
6914
6904
|
}
|
|
6915
|
-
if (
|
|
6905
|
+
if (sessionStatus !== void 0 && this.getBondStateSessionStatus(state) !== sessionStatus) {
|
|
6916
6906
|
continue;
|
|
6917
6907
|
}
|
|
6918
6908
|
const bondStateStats = await this.getBondStateStats(state);
|
|
6919
6909
|
bondStates.push(
|
|
6920
6910
|
{
|
|
6911
|
+
publicKey,
|
|
6921
6912
|
bondState: state,
|
|
6922
6913
|
stateStats: bondStateStats
|
|
6923
6914
|
}
|
|
@@ -7068,6 +7059,186 @@ var JBondClient = class _JBondClient {
|
|
|
7068
7059
|
}
|
|
7069
7060
|
});
|
|
7070
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
|
+
console.log("Derived legacy validator bond account:", legacyValidatorBondAccount.toBase58());
|
|
7075
|
+
console.log("Derived new validator bond account:", validatorBond[0].toBase58());
|
|
7076
|
+
console.log("Fetching history for legacy validator bond account:", legacyValidatorBondAccount.toBase58());
|
|
7077
|
+
console.log("Fetching history for new validator bond account:", validatorBond[0].toBase58());
|
|
7078
|
+
const legacySignatures = (await this.connection.getSignaturesForAddress(
|
|
7079
|
+
legacyValidatorBondAccount,
|
|
7080
|
+
{
|
|
7081
|
+
limit: options?.limit || 1e3,
|
|
7082
|
+
before: options?.before,
|
|
7083
|
+
until: options?.until
|
|
7084
|
+
},
|
|
7085
|
+
"confirmed"
|
|
7086
|
+
)).filter((sig) => !sig.err);
|
|
7087
|
+
const newSignatures = (await this.connection.getSignaturesForAddress(
|
|
7088
|
+
validatorBond[0],
|
|
7089
|
+
{
|
|
7090
|
+
limit: options?.limit || 1e3,
|
|
7091
|
+
before: options?.before,
|
|
7092
|
+
until: options?.until
|
|
7093
|
+
},
|
|
7094
|
+
"confirmed"
|
|
7095
|
+
)).filter((sig) => !sig.err);
|
|
7096
|
+
const signatureStrings = legacySignatures.map((sig) => sig.signature);
|
|
7097
|
+
signatureStrings.push(...newSignatures.map((sig) => sig.signature));
|
|
7098
|
+
const signatures = [...legacySignatures, ...newSignatures];
|
|
7099
|
+
const BATCH_SIZE = 100;
|
|
7100
|
+
const allTransactions = [];
|
|
7101
|
+
for (let i = 0; i < signatureStrings.length; i += BATCH_SIZE) {
|
|
7102
|
+
const batch = signatureStrings.slice(i, i + BATCH_SIZE);
|
|
7103
|
+
const transactions = await this.connection.getParsedTransactions(
|
|
7104
|
+
batch,
|
|
7105
|
+
{
|
|
7106
|
+
maxSupportedTransactionVersion: 0,
|
|
7107
|
+
commitment: "confirmed"
|
|
7108
|
+
}
|
|
7109
|
+
);
|
|
7110
|
+
allTransactions.push(...transactions);
|
|
7111
|
+
}
|
|
7112
|
+
const cluster = options?.cluster || "mainnet-beta";
|
|
7113
|
+
const history = [];
|
|
7114
|
+
for (const [idx, tx] of allTransactions.entries()) {
|
|
7115
|
+
const sigInfo = signatures[idx];
|
|
7116
|
+
if (!tx || !tx.meta || tx.meta.err !== null) {
|
|
7117
|
+
continue;
|
|
7118
|
+
}
|
|
7119
|
+
try {
|
|
7120
|
+
const slot = tx.slot || 0;
|
|
7121
|
+
const instructions = tx.transaction.message.instructions;
|
|
7122
|
+
for (const instruction of instructions) {
|
|
7123
|
+
if ("programId" in instruction && instruction.programId.equals(this.options.programId ?? this.program.programId)) {
|
|
7124
|
+
const ixData = instruction;
|
|
7125
|
+
if ("parsed" in ixData && ixData.parsed) {
|
|
7126
|
+
continue;
|
|
7127
|
+
}
|
|
7128
|
+
const data = ixData.data;
|
|
7129
|
+
if (!data) {
|
|
7130
|
+
continue;
|
|
7131
|
+
}
|
|
7132
|
+
let type = null;
|
|
7133
|
+
let amount = 0;
|
|
7134
|
+
try {
|
|
7135
|
+
const dataBuffer = bs58.decode(data);
|
|
7136
|
+
if (dataBuffer.length >= 8) {
|
|
7137
|
+
const discriminator = dataBuffer.subarray(0, 8);
|
|
7138
|
+
const legacyRegisterDiscriminator = this.getInstructionDiscriminator("register");
|
|
7139
|
+
const legacyTopUpDiscriminator = this.getInstructionDiscriminator("topUp");
|
|
7140
|
+
const legacyClaimDiscriminator = this.getInstructionDiscriminator("claim");
|
|
7141
|
+
const legacyWithdrawDiscriminator = this.getInstructionDiscriminator("withdraw");
|
|
7142
|
+
const bondRegisterDiscriminator = this.getInstructionDiscriminator("bondRegister");
|
|
7143
|
+
const bondTopUpDiscriminator = this.getInstructionDiscriminator("bondTopUp");
|
|
7144
|
+
const bondClaimDiscriminator = this.getInstructionDiscriminator("bondClaim");
|
|
7145
|
+
const bondWithdrawDiscriminator = this.getInstructionDiscriminator("bondWithdraw");
|
|
7146
|
+
console.log("Discriminator:", Buffer.from(discriminator).toString("hex"));
|
|
7147
|
+
if (Buffer.from(discriminator).equals(Buffer.from(legacyRegisterDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondRegisterDiscriminator))) {
|
|
7148
|
+
type = "deposit" /* Deposit */;
|
|
7149
|
+
if (dataBuffer.length >= 16 && Buffer.from(discriminator).equals(Buffer.from(legacyRegisterDiscriminator))) {
|
|
7150
|
+
const amountBytes = dataBuffer.subarray(8, 16);
|
|
7151
|
+
const amountBN = new import_bn2.BN(amountBytes, "le");
|
|
7152
|
+
amount = amountBN.toNumber() / LAMPORTS_PER_SOL;
|
|
7153
|
+
} else {
|
|
7154
|
+
amount = 0;
|
|
7155
|
+
}
|
|
7156
|
+
} else if (dataBuffer.length >= 16) {
|
|
7157
|
+
const amountBytes = dataBuffer.subarray(8, 16);
|
|
7158
|
+
const amountBN = new import_bn2.BN(amountBytes, "le");
|
|
7159
|
+
amount = amountBN.toNumber() / LAMPORTS_PER_SOL;
|
|
7160
|
+
if (Buffer.from(discriminator).equals(Buffer.from(legacyTopUpDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondTopUpDiscriminator))) {
|
|
7161
|
+
type = "deposit" /* Deposit */;
|
|
7162
|
+
} else if (Buffer.from(discriminator).equals(Buffer.from(legacyClaimDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondClaimDiscriminator))) {
|
|
7163
|
+
type = "compensation" /* Compensation */;
|
|
7164
|
+
} else if (Buffer.from(discriminator).equals(Buffer.from(legacyWithdrawDiscriminator)) || Buffer.from(discriminator).equals(Buffer.from(bondWithdrawDiscriminator))) {
|
|
7165
|
+
type = "withdrawal" /* Withdrawal */;
|
|
7166
|
+
}
|
|
7167
|
+
}
|
|
7168
|
+
}
|
|
7169
|
+
} catch (e) {
|
|
7170
|
+
console.warn("Failed to decode instruction data:", e);
|
|
7171
|
+
}
|
|
7172
|
+
if (type && (amount > 0 || type === "deposit" /* Deposit */)) {
|
|
7173
|
+
history.push({
|
|
7174
|
+
signature: sigInfo.signature,
|
|
7175
|
+
slot,
|
|
7176
|
+
epoch: slotToEpoch(slot, cluster),
|
|
7177
|
+
type,
|
|
7178
|
+
amount
|
|
7179
|
+
});
|
|
7180
|
+
}
|
|
7181
|
+
}
|
|
7182
|
+
}
|
|
7183
|
+
} catch (error) {
|
|
7184
|
+
console.error(`Error processing transaction ${sigInfo.signature}:`, error);
|
|
7185
|
+
}
|
|
7186
|
+
}
|
|
7187
|
+
return history.toSorted((a, b) => b.slot - a.slot);
|
|
7188
|
+
}
|
|
7189
|
+
/**
|
|
7190
|
+
* Get full transaction history by paginating through results
|
|
7191
|
+
* @param bondType
|
|
7192
|
+
* @param bondName
|
|
7193
|
+
* @param voteAccount
|
|
7194
|
+
* @param pageSize
|
|
7195
|
+
*/
|
|
7196
|
+
async getFullHistory(bondType, bondName, voteAccount, pageSize = 100) {
|
|
7197
|
+
const allHistory = [];
|
|
7198
|
+
let before;
|
|
7199
|
+
while (true) {
|
|
7200
|
+
const batch = await this.getHistory(bondType, bondName, voteAccount, {
|
|
7201
|
+
limit: pageSize,
|
|
7202
|
+
before
|
|
7203
|
+
});
|
|
7204
|
+
if (batch.length === 0) {
|
|
7205
|
+
break;
|
|
7206
|
+
}
|
|
7207
|
+
allHistory.push(...batch);
|
|
7208
|
+
before = batch.at(-1)?.signature;
|
|
7209
|
+
if (batch.length < pageSize) {
|
|
7210
|
+
break;
|
|
7211
|
+
}
|
|
7212
|
+
}
|
|
7213
|
+
return allHistory;
|
|
7214
|
+
}
|
|
7215
|
+
/**
|
|
7216
|
+
* Get instruction discriminator from IDL
|
|
7217
|
+
* @param instructionName
|
|
7218
|
+
* @private
|
|
7219
|
+
*/
|
|
7220
|
+
getInstructionDiscriminator(instructionName) {
|
|
7221
|
+
const legacyDiscriminators = {
|
|
7222
|
+
register: [211, 124, 67, 15, 211, 194, 178, 240],
|
|
7223
|
+
topUp: [181, 157, 89, 67, 143, 182, 52, 72],
|
|
7224
|
+
claim: [62, 198, 214, 193, 213, 159, 108, 210],
|
|
7225
|
+
withdraw: [183, 18, 70, 156, 148, 109, 161, 34]
|
|
7226
|
+
};
|
|
7227
|
+
if (legacyDiscriminators[instructionName]) {
|
|
7228
|
+
return new Uint8Array(legacyDiscriminators[instructionName]);
|
|
7229
|
+
}
|
|
7230
|
+
if (instructionName.startsWith("bond")) {
|
|
7231
|
+
const instruction = this.program.idl.instructions.find((ix) => ix.name === instructionName);
|
|
7232
|
+
if (!instruction) {
|
|
7233
|
+
throw new Error(`Instruction ${instructionName} not found in IDL`);
|
|
7234
|
+
}
|
|
7235
|
+
if (!instruction.discriminator || !Array.isArray(instruction.discriminator)) {
|
|
7236
|
+
throw new Error(`Discriminator not found for instruction ${instructionName}`);
|
|
7237
|
+
}
|
|
7238
|
+
return new Uint8Array(instruction.discriminator);
|
|
7239
|
+
}
|
|
7240
|
+
throw new Error(`Unknown instruction: ${instructionName}`);
|
|
7241
|
+
}
|
|
7071
7242
|
/**
|
|
7072
7243
|
* Get bond state stats
|
|
7073
7244
|
* @param state
|