@hawksightco/hawk-sdk
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -51,2 +51,10 @@ import * as client from "@hawksightco/swagger-client"; | ||
/** | ||
* Add a signature on the transaction with provided signature. | ||
* | ||
* @param publicKey The public key of the signer | ||
* @param signature A signature of the signed transaction | ||
* @throws Error if a signer is not required or has already signed the transaction. | ||
*/ | ||
addSignature(publicKey: web3.PublicKey, signature: Uint8Array): void; | ||
/** | ||
* Checks if all required signers have signed the transaction. | ||
@@ -53,0 +61,0 @@ * |
@@ -37,4 +37,4 @@ "use strict"; | ||
const web3 = __importStar(require("@solana/web3.js")); | ||
const bn_js_1 = require("bn.js"); | ||
const functions_1 = require("../functions"); | ||
const bn_js_1 = require("bn.js"); | ||
/** | ||
@@ -45,10 +45,22 @@ * Represents a transaction object in Solana using the web3.js library. | ||
class Transaction { | ||
get txMessage() { return this._txMessage; } | ||
get versionedTransaction() { return this._versionedTransaction; } | ||
get priorityFeeEstimate() { return this._priorityFeeEstimate; } | ||
get txMessage() { | ||
return this._txMessage; | ||
} | ||
get versionedTransaction() { | ||
return this._versionedTransaction; | ||
} | ||
get priorityFeeEstimate() { | ||
return this._priorityFeeEstimate; | ||
} | ||
/** The blockhash of a recent ledger entry */ | ||
get recentBlockhash() { return this.latestBlockhash.blockhash; } | ||
get instructions() { return this._instructions; } | ||
get recentBlockhash() { | ||
return this.latestBlockhash.blockhash; | ||
} | ||
get instructions() { | ||
return this._instructions; | ||
} | ||
/** last valid block height */ | ||
get lastValidBlockHeight() { return this.latestBlockhash.lastValidBlockHeight; } | ||
get lastValidBlockHeight() { | ||
return this.latestBlockhash.lastValidBlockHeight; | ||
} | ||
/** | ||
@@ -81,9 +93,13 @@ * Constructs a new Transaction object. | ||
// Construct main instructions | ||
const mainIxs = txMetadataResponse.mainInstructions.map(ix => { | ||
const mainIxs = txMetadataResponse.mainInstructions.map((ix) => { | ||
return new web3.TransactionInstruction({ | ||
keys: ix.accounts.map(meta => { | ||
return { pubkey: new web3.PublicKey(meta.pubkey), isSigner: meta.isSigner, isWritable: meta.isWritable }; | ||
keys: ix.accounts.map((meta) => { | ||
return { | ||
pubkey: new web3.PublicKey(meta.pubkey), | ||
isSigner: meta.isSigner, | ||
isWritable: meta.isWritable, | ||
}; | ||
}), | ||
programId: new web3.PublicKey(ix.programId), | ||
data: Buffer.from(ix.data, 'base64'), | ||
data: Buffer.from(ix.data, "base64"), | ||
}); | ||
@@ -96,7 +112,6 @@ }); | ||
this.requiredSigners = this.getRequiredSigners(); | ||
if (typeof this.requiredSigners[payerKey.toString()] !== 'boolean') { | ||
if (typeof this.requiredSigners[payerKey.toString()] !== "boolean") { | ||
throw new Error(`Warning: The payer ${payerKey} is not one of the required signers of this transaction.`); | ||
} | ||
} | ||
; | ||
/** | ||
@@ -111,5 +126,5 @@ * Signs the transaction with provided signers. | ||
// First validate all signers | ||
signers.forEach(signer => { | ||
signers.forEach((signer) => { | ||
const key = signer.publicKey.toString(); | ||
if (typeof this.requiredSigners[key] !== 'boolean') { | ||
if (typeof this.requiredSigners[key] !== "boolean") { | ||
throw new Error(`Key ${key} is not a required signer!`); | ||
@@ -122,3 +137,3 @@ } | ||
// If all signers are valid and the process is idempotent or they haven't signed yet, update and sign | ||
signers.forEach(signer => { | ||
signers.forEach((signer) => { | ||
const key = signer.publicKey.toString(); | ||
@@ -130,2 +145,20 @@ this.requiredSigners[key] = true; | ||
/** | ||
* Add a signature on the transaction with provided signature. | ||
* | ||
* @param publicKey The public key of the signer | ||
* @param signature A signature of the signed transaction | ||
* @throws Error if a signer is not required or has already signed the transaction. | ||
*/ | ||
addSignature(publicKey, signature) { | ||
const key = publicKey.toBase58(); | ||
if (typeof this.requiredSigners[key] !== "boolean") { | ||
throw new Error(`Key ${key} is not a required signer!`); | ||
} | ||
if (this.requiredSigners[key]) { | ||
throw new Error(`Key ${key} has already been signed by required signer!`); | ||
} | ||
this.requiredSigners[key] = true; | ||
this.versionedTransaction.addSignature(publicKey, signature); | ||
} | ||
/** | ||
* Checks if all required signers have signed the transaction. | ||
@@ -136,3 +169,3 @@ * | ||
isSignedByRequiredSigners() { | ||
return Object.values(this.requiredSigners).every(isSigned => isSigned); | ||
return Object.values(this.requiredSigners).every((isSigned) => isSigned); | ||
} | ||
@@ -148,6 +181,10 @@ /** | ||
const estimate = yield (0, functions_1.getFeeEstimate)(this.generalUtility, priorityLevel, this.txMetadataResponse); | ||
const priorityFeeEstimate = maxPriorityFee !== undefined && maxPriorityFee > 0 ? Math.round(Math.min(estimate, maxPriorityFee)) : Math.round(estimate); | ||
const priorityFeeEstimate = maxPriorityFee !== undefined && maxPriorityFee > 0 | ||
? Math.round(Math.min(estimate, maxPriorityFee)) | ||
: Math.round(estimate); | ||
// Create priority fee ixs for transaction | ||
const priorityFeeIxs = [ | ||
web3.ComputeBudgetProgram.setComputeUnitLimit({ units: computeUnitLimit }), | ||
web3.ComputeBudgetProgram.setComputeUnitLimit({ | ||
units: computeUnitLimit, | ||
}), | ||
web3.ComputeBudgetProgram.setComputeUnitPrice({ | ||
@@ -161,5 +198,7 @@ // CU * CU PRICE -> 1400000 * feeEstimate.priorityFeeEstimate | ||
.div(new bn_js_1.BN(1000000)) | ||
.add(new bn_js_1.BN(5000)).toNumber() / 1000000000).toString(); | ||
.add(new bn_js_1.BN(5000)) | ||
.toNumber() / 1000000000).toString(); | ||
// Append priority fee instruction at the beginning | ||
this._instructions.unshift(...priorityFeeIxs); | ||
console.log(this._instructions); | ||
// Rebuild versioned transaction | ||
@@ -193,3 +232,3 @@ const blockhash = yield connection.getLatestBlockhash(); | ||
if (simulation.value.unitsConsumed === undefined) { | ||
throw new Error('Unable to calculate compute budget.'); | ||
throw new Error("Unable to calculate compute budget."); | ||
} | ||
@@ -224,5 +263,7 @@ return { | ||
getRequiredSigners() { | ||
const signerKeys = this.instructions.flatMap(ix => ix.keys.filter(meta => meta.isSigner).map(meta => meta.pubkey.toString())); | ||
const signerKeys = this.instructions.flatMap((ix) => ix.keys | ||
.filter((meta) => meta.isSigner) | ||
.map((meta) => meta.pubkey.toString())); | ||
const result = {}; | ||
signerKeys.forEach(key => result[key] = false); | ||
signerKeys.forEach((key) => (result[key] = false)); | ||
return result; | ||
@@ -234,4 +275,5 @@ } | ||
findSetComputeUnitLimitIndex() { | ||
return this.instructions.findIndex(ix => { | ||
const isComputeBudgetProgram = ix.programId.toString() === 'ComputeBudget111111111111111111111111111111'; | ||
return this.instructions.findIndex((ix) => { | ||
const isComputeBudgetProgram = ix.programId.toString() === | ||
"ComputeBudget111111111111111111111111111111"; | ||
const isSetComputeLimitIx = ix.data[0] === 2; | ||
@@ -245,4 +287,5 @@ return isComputeBudgetProgram && isSetComputeLimitIx; | ||
findSetComputeUnitPriceIndex() { | ||
return this.instructions.findIndex(ix => { | ||
const isComputeBudgetProgram = ix.programId.toString() === 'ComputeBudget111111111111111111111111111111'; | ||
return this.instructions.findIndex((ix) => { | ||
const isComputeBudgetProgram = ix.programId.toString() === | ||
"ComputeBudget111111111111111111111111111111"; | ||
const isSetComputeUnitPriceIx = ix.data[0] === 3; | ||
@@ -249,0 +292,0 @@ return isComputeBudgetProgram && isSetComputeUnitPriceIx; |
{ | ||
"name": "@hawksightco/hawk-sdk", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Hawksight v2 SDK", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index.js", |
Sorry, the diff of this file is not supported yet
116505
2066