@stabbleorg/anchor-contrib
Advanced tools
Comparing version 1.4.14 to 1.4.15
import { Provider } from "@coral-xyz/anchor"; | ||
import { AddressLookupTableAccount, BlockhashWithExpiryBlockHeight, Commitment, PublicKey, SendOptions, Signer, TokenAccountBalancePair, TransactionInstruction, TransactionSignature, VersionedTransaction } from "@solana/web3.js"; | ||
import { AddressLookupTableAccount, BlockhashWithExpiryBlockHeight, Commitment, PublicKey, SendOptions, Signer, TokenAccountBalancePair, Transaction, TransactionInstruction, TransactionSignature, VersionedTransaction } from "@solana/web3.js"; | ||
import { PriorityLevel } from "./helius"; | ||
import { AddressWithTransactionInstruction, FloatLike, TransactionWithBlockhash } from "./types"; | ||
export interface UseWalletProvider extends Provider { | ||
sendAndConfirmWithBlockhash(transaction: VersionedTransaction, signers?: Signer[], sendOptions?: SendOptions, blockhash?: BlockhashWithExpiryBlockHeight): Promise<TransactionSignature>; | ||
sendAndConfirmWithBlockhash(transaction: VersionedTransaction | Transaction, signers?: Signer[], sendOptions?: SendOptions, blockhash?: BlockhashWithExpiryBlockHeight): Promise<TransactionSignature>; | ||
} | ||
@@ -21,2 +21,3 @@ export declare class WalletContext<T extends Provider = Provider> { | ||
sendSmartTransaction(instructions: TransactionInstruction[], signers?: Signer[], altAccounts?: AddressLookupTableAccount[], priorityLevel?: PriorityLevel, commitment?: Commitment): Promise<TransactionSignature>; | ||
sendTransactionFromBuffer(txBuffer: Buffer | Uint8Array, signers?: Signer[], commitment?: Commitment): Promise<TransactionSignature>; | ||
} |
@@ -274,3 +274,3 @@ "use strict"; | ||
instructions.unshift(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ | ||
units: Math.trunc(sim.unitsConsumed * 1.1), // additional 10% buffer to estimated CU | ||
units: Math.trunc(sim.unitsConsumed * 1.05), // additional 5% buffer to estimated CU | ||
})); | ||
@@ -357,2 +357,66 @@ minPriorityFee = Math.ceil(helius_1.MINIMUM_PRIORITY_FEE / sim.unitsConsumed); | ||
}; | ||
WalletContext.prototype.sendTransactionFromBuffer = function (txBuffer_1) { | ||
return __awaiter(this, arguments, void 0, function (txBuffer, signers, commitment) { | ||
var _a, blockhash, minContextSlot, transaction, sendOptions, txSigned, txBuff, startTime, signature, abortSignal, err_4; | ||
if (signers === void 0) { signers = []; } | ||
if (commitment === void 0) { commitment = "confirmed"; } | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, this.provider.connection.getLatestBlockhashAndContext()]; | ||
case 1: | ||
_a = _b.sent(), blockhash = _a.value, minContextSlot = _a.context.slot; | ||
try { | ||
transaction = web3_js_1.Transaction.from(txBuffer); | ||
} | ||
catch (error) { | ||
transaction = web3_js_1.VersionedTransaction.deserialize(txBuffer); | ||
} | ||
sendOptions = { | ||
maxRetries: 0, | ||
preflightCommitment: commitment, | ||
skipPreflight: false, | ||
minContextSlot: minContextSlot, | ||
}; | ||
if ("sendAndConfirmWithBlockhash" in this.provider) { | ||
return [2 /*return*/, this.provider.sendAndConfirmWithBlockhash(transaction, signers, sendOptions, blockhash)]; | ||
} | ||
if (!(this.provider instanceof anchor_1.AnchorProvider)) return [3 /*break*/, 10]; | ||
if (transaction instanceof web3_js_1.VersionedTransaction) { | ||
transaction.sign(signers); // v0 | ||
} | ||
else { | ||
transaction.partialSign.apply(transaction, signers); // legacy | ||
} | ||
return [4 /*yield*/, this.provider.wallet.signTransaction(transaction)]; | ||
case 2: | ||
txSigned = _b.sent(); | ||
txBuff = txSigned.serialize(); | ||
startTime = Date.now(); | ||
_b.label = 3; | ||
case 3: | ||
if (!(Date.now() - startTime < helius_1.SMART_TRANSACTION_TIMEOUT)) return [3 /*break*/, 9]; | ||
_b.label = 4; | ||
case 4: | ||
_b.trys.push([4, 7, , 8]); | ||
return [4 /*yield*/, this.provider.connection.sendRawTransaction(txBuff, sendOptions)]; | ||
case 5: | ||
signature = _b.sent(); | ||
abortSignal = AbortSignal.timeout(helius_1.SMART_TRANSACTION_RETRY_TIMEOUT); | ||
return [4 /*yield*/, this.provider.connection.confirmTransaction(__assign({ abortSignal: abortSignal, signature: signature }, blockhash), commitment)]; | ||
case 6: | ||
_b.sent(); | ||
return [2 /*return*/, signature]; | ||
case 7: | ||
err_4 = _b.sent(); | ||
if (err_4.name === "TimeoutError") { | ||
return [3 /*break*/, 3]; | ||
} | ||
throw err_4; | ||
case 8: return [3 /*break*/, 3]; | ||
case 9: throw Error("Transaction failed to confirm in 60s"); | ||
case 10: throw Error("Please connect your wallet"); | ||
} | ||
}); | ||
}); | ||
}; | ||
return WalletContext; | ||
@@ -359,0 +423,0 @@ }()); |
{ | ||
"name": "@stabbleorg/anchor-contrib", | ||
"version": "1.4.14", | ||
"version": "1.4.15", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
@@ -25,2 +25,3 @@ import bs58 from "bs58"; | ||
TokenAccountBalancePair, | ||
Transaction, | ||
TransactionInstruction, | ||
@@ -47,3 +48,3 @@ TransactionMessage, | ||
sendAndConfirmWithBlockhash( | ||
transaction: VersionedTransaction, | ||
transaction: VersionedTransaction | Transaction, | ||
signers?: Signer[], | ||
@@ -248,3 +249,3 @@ sendOptions?: SendOptions, | ||
ComputeBudgetProgram.setComputeUnitLimit({ | ||
units: Math.trunc(sim.unitsConsumed * 1.1), // additional 10% buffer to estimated CU | ||
units: Math.trunc(sim.unitsConsumed * 1.05), // additional 5% buffer to estimated CU | ||
}), | ||
@@ -339,2 +340,65 @@ ); | ||
} | ||
async sendTransactionFromBuffer( | ||
txBuffer: Buffer | Uint8Array, | ||
signers: Signer[] = [], | ||
commitment: Commitment = "confirmed", | ||
): Promise<TransactionSignature> { | ||
const { | ||
value: blockhash, | ||
context: { slot: minContextSlot }, | ||
} = await this.provider.connection.getLatestBlockhashAndContext(); | ||
let transaction; | ||
try { | ||
transaction = Transaction.from(txBuffer); | ||
} catch (error) { | ||
transaction = VersionedTransaction.deserialize(txBuffer); | ||
} | ||
const sendOptions: SendOptions = { | ||
maxRetries: 0, | ||
preflightCommitment: commitment, | ||
skipPreflight: false, | ||
minContextSlot, | ||
}; | ||
if ("sendAndConfirmWithBlockhash" in this.provider) { | ||
return (this.provider as UseWalletProvider).sendAndConfirmWithBlockhash( | ||
transaction, | ||
signers, | ||
sendOptions, | ||
blockhash, | ||
); | ||
} | ||
if (this.provider instanceof AnchorProvider) { | ||
if (transaction instanceof VersionedTransaction) { | ||
transaction.sign(signers); // v0 | ||
} else { | ||
transaction.partialSign(...signers); // legacy | ||
} | ||
const txSigned = await this.provider.wallet.signTransaction(transaction); | ||
const txBuff = txSigned.serialize(); | ||
const startTime = Date.now(); | ||
while (Date.now() - startTime < SMART_TRANSACTION_TIMEOUT) { | ||
try { | ||
const signature = await this.provider.connection.sendRawTransaction(txBuff, sendOptions); | ||
const abortSignal = AbortSignal.timeout(SMART_TRANSACTION_RETRY_TIMEOUT); | ||
await this.provider.connection.confirmTransaction({ abortSignal, signature, ...blockhash }, commitment); | ||
return signature; | ||
} catch (err: any) { | ||
if (err.name === "TimeoutError") { | ||
continue; | ||
} | ||
throw err; | ||
} | ||
} | ||
throw Error("Transaction failed to confirm in 60s"); | ||
} | ||
throw Error("Please connect your wallet"); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
65689
1131