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