fix: add sendAndConfirmRawTransaction()

This commit is contained in:
Michael Vines
2018-11-28 11:56:50 -08:00
parent 9f0713d3a7
commit 2962eef2c7
5 changed files with 59 additions and 5 deletions

View File

@@ -0,0 +1,42 @@
// @flow
import {Connection} from '../connection';
import {sleep} from './sleep';
import type {TransactionSignature} from '../transaction';
/**
* Sign, send and confirm a raw transaction
*/
export async function sendAndConfirmRawTransaction(
connection: Connection,
rawTransaction: Buffer,
): Promise<TransactionSignature> {
const start = Date.now();
let signature = await connection.sendRawTransaction(rawTransaction);
// Wait up to a couple seconds for a confirmation
let status = '';
let statusRetries = 4;
for (;;) {
status = await connection.getSignatureStatus(signature);
if (status !== 'SignatureNotFound') {
break;
}
await sleep(500);
if (--statusRetries <= 0) {
const duration = (Date.now() - start) / 1000;
throw new Error(
`Raw Transaction '${signature}' was not confirmed in ${duration.toFixed(
2,
)} seconds (${status})`,
);
}
}
if (status === 'Confirmed') {
return signature;
}
throw new Error(`Raw transaction ${signature} failed (${status})`);
}

View File

@@ -1,5 +1,7 @@
// @flow
import invariant from 'assert';
import {Connection} from '../connection';
import {Transaction} from '../transaction';
import {sleep} from './sleep';
@@ -13,7 +15,7 @@ export async function sendAndConfirmTransaction(
connection: Connection,
transaction: Transaction,
...signers: Array<Account>
): Promise<?TransactionSignature> {
): Promise<TransactionSignature> {
let sendRetries = 10;
let signature;
for (;;) {
@@ -52,5 +54,6 @@ export async function sendAndConfirmTransaction(
await sleep(Math.random() * 100);
}
invariant(signature !== undefined);
return signature;
}