feat: add skipPreflight option
This commit is contained in:
committed by
Michael Vines
parent
a6e07e44da
commit
8547ae43ce
@@ -31,6 +31,28 @@ type Context = {
|
||||
slot: number,
|
||||
};
|
||||
|
||||
/**
|
||||
* Options for sending transactions
|
||||
*
|
||||
* @typedef {Object} SendOptions
|
||||
* @property {boolean} skipPreflight disable transaction verification step
|
||||
*/
|
||||
export type SendOptions = {
|
||||
skipPreflight: boolean,
|
||||
};
|
||||
|
||||
/**
|
||||
* Options for confirming transactions
|
||||
*
|
||||
* @typedef {Object} ConfirmOptions
|
||||
* @property {boolean} skipPreflight disable transaction verification step
|
||||
* @property {number} confirmations desired number of cluster confirmations
|
||||
*/
|
||||
export type ConfirmOptions = {
|
||||
confirmations: number,
|
||||
skipPreflight: boolean,
|
||||
};
|
||||
|
||||
/**
|
||||
* RPC Response with extra contextual information
|
||||
*
|
||||
@@ -1663,6 +1685,7 @@ export class Connection {
|
||||
async sendTransaction(
|
||||
transaction: Transaction,
|
||||
signers: Array<Account>,
|
||||
options?: SendOptions,
|
||||
): Promise<TransactionSignature> {
|
||||
if (transaction.nonceInfo) {
|
||||
transaction.sign(...signers);
|
||||
@@ -1696,7 +1719,7 @@ export class Connection {
|
||||
let attempts = 0;
|
||||
const startTime = Date.now();
|
||||
for (;;) {
|
||||
const {blockhash} = await this.getRecentBlockhash();
|
||||
const {blockhash} = await this.getRecentBlockhash('max');
|
||||
|
||||
if (this._blockhashInfo.recentBlockhash != blockhash) {
|
||||
this._blockhashInfo = {
|
||||
@@ -1723,7 +1746,7 @@ export class Connection {
|
||||
}
|
||||
|
||||
const wireTransaction = transaction.serialize();
|
||||
return await this.sendRawTransaction(wireTransaction);
|
||||
return await this.sendRawTransaction(wireTransaction, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1745,9 +1768,13 @@ export class Connection {
|
||||
*/
|
||||
async sendRawTransaction(
|
||||
rawTransaction: Buffer | Uint8Array | Array<number>,
|
||||
options: ?SendOptions,
|
||||
): Promise<TransactionSignature> {
|
||||
const encodedTransaction = bs58.encode(toBuffer(rawTransaction));
|
||||
const result = await this.sendEncodedTransaction(encodedTransaction);
|
||||
const result = await this.sendEncodedTransaction(
|
||||
encodedTransaction,
|
||||
options,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1757,10 +1784,12 @@ export class Connection {
|
||||
*/
|
||||
async sendEncodedTransaction(
|
||||
encodedTransaction: string,
|
||||
options: ?SendOptions,
|
||||
): Promise<TransactionSignature> {
|
||||
const unsafeRes = await this._rpcRequest('sendTransaction', [
|
||||
encodedTransaction,
|
||||
]);
|
||||
const args = [encodedTransaction];
|
||||
const skipPreflight = options && options.skipPreflight;
|
||||
if (skipPreflight) args.push({skipPreflight});
|
||||
const unsafeRes = await this._rpcRequest('sendTransaction', args);
|
||||
const res = SendTransactionRpcResult(unsafeRes);
|
||||
if (res.error) {
|
||||
throw new Error('failed to send transaction: ' + res.error.message);
|
||||
|
@@ -69,7 +69,10 @@ export class Loader {
|
||||
connection,
|
||||
transaction,
|
||||
[payer, program],
|
||||
1,
|
||||
{
|
||||
confirmations: 1,
|
||||
skipPreflight: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -107,7 +110,10 @@ export class Loader {
|
||||
data,
|
||||
});
|
||||
transactions.push(
|
||||
sendAndConfirmTransaction(connection, transaction, [payer, program], 1),
|
||||
sendAndConfirmTransaction(connection, transaction, [payer, program], {
|
||||
confirmations: 1,
|
||||
skipPreflight: true,
|
||||
}),
|
||||
);
|
||||
|
||||
// Delay ~1 tick between write transactions in an attempt to reduce AccountInUse errors
|
||||
@@ -152,7 +158,10 @@ export class Loader {
|
||||
connection,
|
||||
transaction,
|
||||
[payer, program],
|
||||
1,
|
||||
{
|
||||
confirmations: 1,
|
||||
skipPreflight: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
import {Connection} from '../connection';
|
||||
import type {TransactionSignature} from '../transaction';
|
||||
import type {ConfirmOptions} from '../connection';
|
||||
|
||||
/**
|
||||
* Send and confirm a raw transaction
|
||||
@@ -9,12 +10,19 @@ import type {TransactionSignature} from '../transaction';
|
||||
export async function sendAndConfirmRawTransaction(
|
||||
connection: Connection,
|
||||
rawTransaction: Buffer,
|
||||
confirmations: ?number,
|
||||
options?: ConfirmOptions,
|
||||
): Promise<TransactionSignature> {
|
||||
const start = Date.now();
|
||||
const signature = await connection.sendRawTransaction(rawTransaction);
|
||||
const status = (await connection.confirmTransaction(signature, confirmations))
|
||||
.value;
|
||||
const signature = await connection.sendRawTransaction(
|
||||
rawTransaction,
|
||||
options,
|
||||
);
|
||||
const status = (
|
||||
await connection.confirmTransaction(
|
||||
signature,
|
||||
options && options.confirmations,
|
||||
)
|
||||
).value;
|
||||
|
||||
if (status) {
|
||||
if (status.err) {
|
||||
|
@@ -4,6 +4,7 @@ import {Connection} from '../connection';
|
||||
import {Transaction} from '../transaction';
|
||||
import {sleep} from './sleep';
|
||||
import type {Account} from '../account';
|
||||
import type {ConfirmOptions} from '../connection';
|
||||
import type {TransactionSignature} from '../transaction';
|
||||
|
||||
const NUM_SEND_RETRIES = 10;
|
||||
@@ -17,15 +18,22 @@ export async function sendAndConfirmTransaction(
|
||||
connection: Connection,
|
||||
transaction: Transaction,
|
||||
signers: Array<Account>,
|
||||
confirmations: ?number,
|
||||
options?: ConfirmOptions,
|
||||
): Promise<TransactionSignature> {
|
||||
const start = Date.now();
|
||||
let sendRetries = NUM_SEND_RETRIES;
|
||||
|
||||
for (;;) {
|
||||
const signature = await connection.sendTransaction(transaction, signers);
|
||||
const signature = await connection.sendTransaction(
|
||||
transaction,
|
||||
signers,
|
||||
options,
|
||||
);
|
||||
const status = (
|
||||
await connection.confirmTransaction(signature, confirmations)
|
||||
await connection.confirmTransaction(
|
||||
signature,
|
||||
options && options.confirmations,
|
||||
)
|
||||
).value;
|
||||
|
||||
if (status) {
|
||||
|
Reference in New Issue
Block a user