Skip to content

Commit

Permalink
ts: Add optional options.blockhash to Provider.sendAndConfirm (#3070
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kAsky53 authored Aug 28, 2024
1 parent c0464f5 commit a245dce
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The minor version will be incremented upon a breaking change and the patch versi

### Features

- ts: Add optional `options.blockhash` to `Provider.sendAndConfirm` ([#3070](https://github.com/coral-xyz/anchor/pull/3070)).
- ts: Add optional `commitment` parameter to `Program.addEventListener` ([#3052](https://github.com/coral-xyz/anchor/pull/3052)).
- cli, idl: Pass `cargo` args to IDL generation when building program or IDL ([#3059](https://github.com/coral-xyz/anchor/pull/3059)).
- cli: Add checks for incorrect usage of `idl-build` feature ([#3061](https://github.com/coral-xyz/anchor/pull/3061)).
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@coral-xyz/anchor-errors": "^0.30.1",
"@coral-xyz/borsh": "^0.30.1",
"@noble/hashes": "^1.3.1",
"@solana/web3.js": "^1.68.0",
"@solana/web3.js": "^1.69.0",
"bn.js": "^5.1.2",
"bs58": "^4.0.1",
"buffer-layout": "^1.2.2",
Expand Down
93 changes: 68 additions & 25 deletions ts/packages/anchor/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
SendOptions,
VersionedTransaction,
RpcResponseAndContext,
BlockhashWithExpiryBlockHeight,
SignatureResult,
Keypair,
} from "@solana/web3.js";
import { bs58 } from "./utils/bytes/index.js";
Expand All @@ -33,7 +35,7 @@ export default interface Provider {
sendAndConfirm?(
tx: Transaction | VersionedTransaction,
signers?: Signer[],
opts?: ConfirmOptions
opts?: ConfirmOptionsWithBlockhash
): Promise<TransactionSignature>;
sendAll?<T extends Transaction | VersionedTransaction>(
txWithSigners: {
Expand Down Expand Up @@ -136,7 +138,7 @@ export class AnchorProvider implements Provider {
async sendAndConfirm(
tx: Transaction | VersionedTransaction,
signers?: Signer[],
opts?: ConfirmOptions
opts?: ConfirmOptionsWithBlockhash
): Promise<TransactionSignature> {
if (opts === undefined) {
opts = this.opts;
Expand Down Expand Up @@ -350,6 +352,10 @@ export type SendTxRequest = {
signers: Array<Signer | undefined>;
};

export type ConfirmOptionsWithBlockhash = ConfirmOptions & {
blockhash?: BlockhashWithExpiryBlockHeight;
};

/**
* Wallet interface for objects that can be used to sign provider transactions.
* VersionedTransactions sign everything at once
Expand All @@ -371,32 +377,69 @@ export interface Wallet {
async function sendAndConfirmRawTransaction(
connection: Connection,
rawTransaction: Buffer | Uint8Array,
options?: ConfirmOptions
options?: ConfirmOptionsWithBlockhash
): Promise<TransactionSignature> {
const sendOptions = options && {
skipPreflight: options.skipPreflight,
preflightCommitment: options.preflightCommitment || options.commitment,
};

const signature = await connection.sendRawTransaction(
rawTransaction,
sendOptions
);

const status = (
await connection.confirmTransaction(
signature,
options && options.commitment
)
).value;

if (status.err) {
throw new ConfirmError(
`Raw transaction ${signature} failed (${JSON.stringify(status)})`
);
const sendOptions: SendOptions = options
? {
skipPreflight: options.skipPreflight,
preflightCommitment: options.preflightCommitment || options.commitment,
maxRetries: options.maxRetries,
minContextSlot: options.minContextSlot,
}
: {};

let status: SignatureResult;

const startTime = Date.now();
while (Date.now() - startTime < 60_000) {
try {
const signature = await connection.sendRawTransaction(
rawTransaction,
sendOptions
);

if (options?.blockhash) {
if (sendOptions.maxRetries === 0) {
const abortSignal = AbortSignal.timeout(15_000);
status = (
await connection.confirmTransaction(
{ abortSignal, signature, ...options.blockhash },
options && options.commitment
)
).value;
} else {
status = (
await connection.confirmTransaction(
{ signature, ...options.blockhash },
options && options.commitment
)
).value;
}
} else {
status = (
await connection.confirmTransaction(
signature,
options && options.commitment
)
).value;
}

if (status.err) {
throw new ConfirmError(
`Raw transaction ${signature} failed (${JSON.stringify(status)})`
);
}

return signature;
} catch (err) {
if (err.name === "TimeoutError") {
continue;
}
throw err;
}
}

return signature;
throw Error("Transaction failed to confirm in 60s");
}

class ConfirmError extends Error {
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/borsh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"buffer-layout": "^1.2.0"
},
"peerDependencies": {
"@solana/web3.js": "^1.68.0"
"@solana/web3.js": "^1.69.0"
},
"files": [
"dist"
Expand Down
8 changes: 4 additions & 4 deletions ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,10 @@
dependencies:
buffer "~6.0.3"

"@solana/web3.js@^1.32.0", "@solana/web3.js@^1.68.0":
version "1.68.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.68.0.tgz#ded078d6e72f9f4b46e0f69925d4fe5c6cdcef54"
integrity sha512-i41x4ArQrjdy/iQf75vKwZa+Mx1hOVquamHHe+5lNbObF4CT57oHOTvG9m9DTypuip3O9ucLoryywd6LfP2eEw==
"@solana/web3.js@^1.32.0", "@solana/web3.js@^1.69.0":
version "1.69.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.69.0.tgz#1756b1a26087172291c0b5163d3b44d24eef8aa7"
integrity sha512-iU2Q0IG25RITsxBkY1Vkk74LffRokViEcSblz4CGxyt+/V7xSkC2DNM0n0rB3aY/9+FvMiz4l5wHnD9UC4Ac/w==
dependencies:
"@babel/runtime" "^7.12.5"
"@noble/ed25519" "^1.7.0"
Expand Down

0 comments on commit a245dce

Please sign in to comment.