fix: avoid double spend in sendAndConfirmTransaction

This commit is contained in:
Justin Starry
2020-06-15 18:32:57 +08:00
committed by Justin Starry
parent d77818c18b
commit f31f66a7c3
5 changed files with 53 additions and 44 deletions

View File

@@ -6,6 +6,13 @@ import type {ConfirmOptions} from '../connection';
/**
* Send and confirm a raw transaction
*
* If `confirmations` count is not specified, wait for transaction to be finalized.
*
* @param {Connection} connection
* @param {Buffer} rawTransaction
* @param {ConfirmOptions} [options]
* @returns {Promise<TransactionSignature>}
*/
export async function sendAndConfirmRawTransaction(
connection: Connection,

View File

@@ -2,17 +2,20 @@
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;
/**
* Sign, send and confirm a transaction.
*
* If `confirmations` count is not specified, wait for transaction to be finalized.
*
* @param {Connection} connection
* @param {Transaction} transaction
* @param {Array<Account>} signers
* @param {ConfirmOptions} [options]
* @returns {Promise<TransactionSignature>}
*/
export async function sendAndConfirmTransaction(
connection: Connection,
@@ -21,34 +24,25 @@ export async function sendAndConfirmTransaction(
options?: ConfirmOptions,
): Promise<TransactionSignature> {
const start = Date.now();
let sendRetries = NUM_SEND_RETRIES;
const signature = await connection.sendTransaction(
transaction,
signers,
options,
);
const status = (
await connection.confirmTransaction(
signature,
options && options.confirmations,
)
).value;
for (;;) {
const signature = await connection.sendTransaction(
transaction,
signers,
options,
);
const status = (
await connection.confirmTransaction(
signature,
options && options.confirmations,
)
).value;
if (status) {
if (status.err) {
throw new Error(
`Transaction ${signature} failed (${JSON.stringify(status)})`,
);
}
return signature;
if (status) {
if (status.err) {
throw new Error(
`Transaction ${signature} failed (${JSON.stringify(status)})`,
);
}
if (--sendRetries <= 0) break;
// Retry in 0..100ms to try to avoid another AccountInUse collision
await sleep(Math.random() * 100);
return signature;
}
const duration = (Date.now() - start) / 1000;