@hawksightco/hawk-sdk 0.0.18 → 0.0.20
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.
|
@@ -47,6 +47,14 @@ export declare class Transaction {
|
|
|
47
47
|
* @throws Error if a signer is not required or has already signed the transaction when not idempotent.
|
|
48
48
|
*/
|
|
49
49
|
sign(signers: web3.Signer[], idempotent?: boolean): void;
|
|
50
|
+
/**
|
|
51
|
+
* Add a signature on the transaction with provided signature.
|
|
52
|
+
*
|
|
53
|
+
* @param publicKey The public key of the signer
|
|
54
|
+
* @param signature A signature of the signed transaction
|
|
55
|
+
* @throws Error if a signer is not required or has already signed the transaction.
|
|
56
|
+
*/
|
|
57
|
+
addSignature(publicKey: web3.PublicKey, signature: Uint8Array): void;
|
|
50
58
|
/**
|
|
51
59
|
* Checks if all required signers have signed the transaction.
|
|
52
60
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transaction.d.ts","sourceRoot":"","sources":["../../../src/classes/Transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,6BAA6B,CAAC;AACtD,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"Transaction.d.ts","sourceRoot":"","sources":["../../../src/classes/Transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,6BAA6B,CAAC;AACtD,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AAGxC,OAAO,EACL,4BAA4B,EAC5B,2BAA2B,EAC5B,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;GAGG;AACH,qBAAa,WAAW;IA+CpB,QAAQ,CAAC,kBAAkB,EAAE,2BAA2B;IACxD,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS;IACjC,OAAO,CAAC,eAAe;IACvB,QAAQ,CAAC,IAAI,EAAE,I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AAC,EAAE,MAAM,GACtB,OAAO,CAAC,IAAI,CAAC,sBAAsB,EAAE,CAAC;IA2CzC;;;;;OAKG;IACG,mBAAmB,CACvB,UAAU,EAAE,IAAI,CAAC,UAAU,GAC1B,OAAO,CAAC,4BAA4B,CAAC;IA4BxC;;OAEG;IACH,gBAAgB,CACd,eAAe,EAAE,IAAI,CAAC,8BAA8B,GACnD,CAAC,IAAI,CAAC,kBAAkB,EAAE,IAAI,CAAC,oBAAoB,CAAC;IAcvD;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAW1B;;OAEG;IACH,OAAO,CAAC,4BAA4B;IAUpC;;OAEG;IACH,OAAO,CAAC,4BAA4B;IAUpC;;OAEG;IACH,OAAO,CAAC,oBAAoB;CAW7B"}
|
|
@@ -34,21 +34,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
exports.Transaction = void 0;
|
|
36
36
|
const web3 = __importStar(require("@solana/web3.js"));
|
|
37
|
-
const functions_1 = require("../functions");
|
|
38
37
|
const bn_js_1 = require("bn.js");
|
|
38
|
+
const functions_1 = require("../functions");
|
|
39
39
|
/**
|
|
40
40
|
* Represents a transaction object in Solana using the web3.js library.
|
|
41
41
|
* This class encapsulates the logic for creating, signing, and verifying signatures of transactions.
|
|
42
42
|
*/
|
|
43
43
|
class Transaction {
|
|
44
|
-
get txMessage() {
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
get txMessage() {
|
|
45
|
+
return this._txMessage;
|
|
46
|
+
}
|
|
47
|
+
get versionedTransaction() {
|
|
48
|
+
return this._versionedTransaction;
|
|
49
|
+
}
|
|
50
|
+
get priorityFeeEstimate() {
|
|
51
|
+
return this._priorityFeeEstimate;
|
|
52
|
+
}
|
|
47
53
|
/** The blockhash of a recent ledger entry */
|
|
48
|
-
get recentBlockhash() {
|
|
49
|
-
|
|
54
|
+
get recentBlockhash() {
|
|
55
|
+
return this.latestBlockhash.blockhash;
|
|
56
|
+
}
|
|
57
|
+
get instructions() {
|
|
58
|
+
return this._instructions;
|
|
59
|
+
}
|
|
50
60
|
/** last valid block height */
|
|
51
|
-
get lastValidBlockHeight() {
|
|
61
|
+
get lastValidBlockHeight() {
|
|
62
|
+
return this.latestBlockhash.lastValidBlockHeight;
|
|
63
|
+
}
|
|
52
64
|
/**
|
|
53
65
|
* Constructs a new Transaction object.
|
|
54
66
|
*
|
|
@@ -76,13 +88,17 @@ class Transaction {
|
|
|
76
88
|
/** Estimated fee in SOL for priority fee when addPriorityFee() method is called. */
|
|
77
89
|
this._priorityFeeEstimate = "";
|
|
78
90
|
// Construct main instructions
|
|
79
|
-
const mainIxs = txMetadataResponse.mainInstructions.map(ix => {
|
|
91
|
+
const mainIxs = txMetadataResponse.mainInstructions.map((ix) => {
|
|
80
92
|
return new web3.TransactionInstruction({
|
|
81
|
-
keys: ix.accounts.map(meta => {
|
|
82
|
-
return {
|
|
93
|
+
keys: ix.accounts.map((meta) => {
|
|
94
|
+
return {
|
|
95
|
+
pubkey: new web3.PublicKey(meta.pubkey),
|
|
96
|
+
isSigner: meta.isSigner,
|
|
97
|
+
isWritable: meta.isWritable,
|
|
98
|
+
};
|
|
83
99
|
}),
|
|
84
100
|
programId: new web3.PublicKey(ix.programId),
|
|
85
|
-
data: Buffer.from(ix.data,
|
|
101
|
+
data: Buffer.from(ix.data, "base64"),
|
|
86
102
|
});
|
|
87
103
|
});
|
|
88
104
|
this._instructions = [...mainIxs];
|
|
@@ -90,11 +106,10 @@ class Transaction {
|
|
|
90
106
|
this._txMessage = txMessage;
|
|
91
107
|
this._versionedTransaction = versionedTransaction;
|
|
92
108
|
this.requiredSigners = this.getRequiredSigners();
|
|
93
|
-
if (typeof this.requiredSigners[payerKey.toString()] !==
|
|
109
|
+
if (typeof this.requiredSigners[payerKey.toString()] !== "boolean") {
|
|
94
110
|
throw new Error(`Warning: The payer ${payerKey} is not one of the required signers of this transaction.`);
|
|
95
111
|
}
|
|
96
112
|
}
|
|
97
|
-
;
|
|
98
113
|
/**
|
|
99
114
|
* Signs the transaction with provided signers.
|
|
100
115
|
*
|
|
@@ -104,9 +119,9 @@ class Transaction {
|
|
|
104
119
|
*/
|
|
105
120
|
sign(signers, idempotent = false) {
|
|
106
121
|
// First validate all signers
|
|
107
|
-
signers.forEach(signer => {
|
|
122
|
+
signers.forEach((signer) => {
|
|
108
123
|
const key = signer.publicKey.toString();
|
|
109
|
-
if (typeof this.requiredSigners[key] !==
|
|
124
|
+
if (typeof this.requiredSigners[key] !== "boolean") {
|
|
110
125
|
throw new Error(`Key ${key} is not a required signer!`);
|
|
111
126
|
}
|
|
112
127
|
if (!idempotent && this.requiredSigners[key]) {
|
|
@@ -114,19 +129,37 @@ class Transaction {
|
|
|
114
129
|
}
|
|
115
130
|
});
|
|
116
131
|
// If all signers are valid and the process is idempotent or they haven't signed yet, update and sign
|
|
117
|
-
signers.forEach(signer => {
|
|
132
|
+
signers.forEach((signer) => {
|
|
118
133
|
const key = signer.publicKey.toString();
|
|
119
134
|
this.requiredSigners[key] = true;
|
|
120
135
|
});
|
|
121
136
|
this.versionedTransaction.sign(signers);
|
|
122
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Add a signature on the transaction with provided signature.
|
|
140
|
+
*
|
|
141
|
+
* @param publicKey The public key of the signer
|
|
142
|
+
* @param signature A signature of the signed transaction
|
|
143
|
+
* @throws Error if a signer is not required or has already signed the transaction.
|
|
144
|
+
*/
|
|
145
|
+
addSignature(publicKey, signature) {
|
|
146
|
+
const key = publicKey.toBase58();
|
|
147
|
+
if (typeof this.requiredSigners[key] !== "boolean") {
|
|
148
|
+
throw new Error(`Key ${key} is not a required signer!`);
|
|
149
|
+
}
|
|
150
|
+
if (this.requiredSigners[key]) {
|
|
151
|
+
throw new Error(`Key ${key} has already been signed by required signer!`);
|
|
152
|
+
}
|
|
153
|
+
this.requiredSigners[key] = true;
|
|
154
|
+
this.versionedTransaction.addSignature(publicKey, signature);
|
|
155
|
+
}
|
|
123
156
|
/**
|
|
124
157
|
* Checks if all required signers have signed the transaction.
|
|
125
158
|
*
|
|
126
159
|
* @returns Boolean indicating whether all required signers have signed.
|
|
127
160
|
*/
|
|
128
161
|
isSignedByRequiredSigners() {
|
|
129
|
-
return Object.values(this.requiredSigners).every(isSigned => isSigned);
|
|
162
|
+
return Object.values(this.requiredSigners).every((isSigned) => isSigned);
|
|
130
163
|
}
|
|
131
164
|
/**
|
|
132
165
|
* Add priority fee instructions (compute budget)
|
|
@@ -137,10 +170,14 @@ class Transaction {
|
|
|
137
170
|
this.removePriorityFeeIxs();
|
|
138
171
|
// Then get fee estimate by simulating the transaction
|
|
139
172
|
const estimate = yield (0, functions_1.getFeeEstimate)(this.generalUtility, priorityLevel, this.txMetadataResponse);
|
|
140
|
-
const priorityFeeEstimate = maxPriorityFee !== undefined && maxPriorityFee > 0
|
|
173
|
+
const priorityFeeEstimate = maxPriorityFee !== undefined && maxPriorityFee > 0
|
|
174
|
+
? Math.round(Math.min(estimate, maxPriorityFee))
|
|
175
|
+
: Math.round(estimate);
|
|
141
176
|
// Create priority fee ixs for transaction
|
|
142
177
|
const priorityFeeIxs = [
|
|
143
|
-
web3.ComputeBudgetProgram.setComputeUnitLimit({
|
|
178
|
+
web3.ComputeBudgetProgram.setComputeUnitLimit({
|
|
179
|
+
units: computeUnitLimit,
|
|
180
|
+
}),
|
|
144
181
|
web3.ComputeBudgetProgram.setComputeUnitPrice({
|
|
145
182
|
// CU * CU PRICE -> 1400000 * feeEstimate.priorityFeeEstimate
|
|
146
183
|
microLamports: priorityFeeEstimate,
|
|
@@ -149,7 +186,8 @@ class Transaction {
|
|
|
149
186
|
this._priorityFeeEstimate = (new bn_js_1.BN(priorityFeeEstimate)
|
|
150
187
|
.mul(new bn_js_1.BN(computeUnitLimit))
|
|
151
188
|
.div(new bn_js_1.BN(1000000))
|
|
152
|
-
.add(new bn_js_1.BN(5000))
|
|
189
|
+
.add(new bn_js_1.BN(5000))
|
|
190
|
+
.toNumber() / 1000000000).toString();
|
|
153
191
|
// Append priority fee instruction at the beginning
|
|
154
192
|
this._instructions.unshift(...priorityFeeIxs);
|
|
155
193
|
// Rebuild versioned transaction
|
|
@@ -180,7 +218,7 @@ class Transaction {
|
|
|
180
218
|
sigVerify: false,
|
|
181
219
|
});
|
|
182
220
|
if (simulation.value.unitsConsumed === undefined) {
|
|
183
|
-
throw new Error(
|
|
221
|
+
throw new Error("Unable to calculate compute budget.");
|
|
184
222
|
}
|
|
185
223
|
return {
|
|
186
224
|
err: simulation.value.err,
|
|
@@ -210,17 +248,20 @@ class Transaction {
|
|
|
210
248
|
* @returns A record of signer public keys mapped to a boolean indicating whether they have signed.
|
|
211
249
|
*/
|
|
212
250
|
getRequiredSigners() {
|
|
213
|
-
const signerKeys = this.instructions.flatMap(ix => ix.keys
|
|
251
|
+
const signerKeys = this.instructions.flatMap((ix) => ix.keys
|
|
252
|
+
.filter((meta) => meta.isSigner)
|
|
253
|
+
.map((meta) => meta.pubkey.toString()));
|
|
214
254
|
const result = {};
|
|
215
|
-
signerKeys.forEach(key => result[key] = false);
|
|
255
|
+
signerKeys.forEach((key) => (result[key] = false));
|
|
216
256
|
return result;
|
|
217
257
|
}
|
|
218
258
|
/**
|
|
219
259
|
* Find setComputeUnitLimit index within the instructions
|
|
220
260
|
*/
|
|
221
261
|
findSetComputeUnitLimitIndex() {
|
|
222
|
-
return this.instructions.findIndex(ix => {
|
|
223
|
-
const isComputeBudgetProgram = ix.programId.toString() ===
|
|
262
|
+
return this.instructions.findIndex((ix) => {
|
|
263
|
+
const isComputeBudgetProgram = ix.programId.toString() ===
|
|
264
|
+
"ComputeBudget111111111111111111111111111111";
|
|
224
265
|
const isSetComputeLimitIx = ix.data[0] === 2;
|
|
225
266
|
return isComputeBudgetProgram && isSetComputeLimitIx;
|
|
226
267
|
});
|
|
@@ -229,8 +270,9 @@ class Transaction {
|
|
|
229
270
|
* Find setComputeUnitPrice index within the instructions
|
|
230
271
|
*/
|
|
231
272
|
findSetComputeUnitPriceIndex() {
|
|
232
|
-
return this.instructions.findIndex(ix => {
|
|
233
|
-
const isComputeBudgetProgram = ix.programId.toString() ===
|
|
273
|
+
return this.instructions.findIndex((ix) => {
|
|
274
|
+
const isComputeBudgetProgram = ix.programId.toString() ===
|
|
275
|
+
"ComputeBudget111111111111111111111111111111";
|
|
234
276
|
const isSetComputeUnitPriceIx = ix.data[0] === 3;
|
|
235
277
|
return isComputeBudgetProgram && isSetComputeUnitPriceIx;
|
|
236
278
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hawksightco/hawk-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"description": "Hawksight v2 SDK",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"repository": "https://github.com/ghabxph/hawk-api-client.git",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"scripts": {
|
|
28
28
|
"start": "npx ts-node src/index.ts",
|
|
29
29
|
"test": "npx jest --silent=false",
|
|
30
|
-
"build": "tsc --outDir dist/"
|
|
30
|
+
"build": "tsc --outDir dist/",
|
|
31
|
+
"prepublishOnly": "npm run build"
|
|
31
32
|
}
|
|
32
33
|
}
|