feat: Add sendAndConfirmTransaction

This commit is contained in:
Michael Vines 2018-10-06 10:36:59 -07:00
parent feab984cf1
commit a596e99b4a
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// @flow
import {Connection, Transaction} from '..';
import {sleep} from './sleep';
import type {Account} from '..';
/**
* Sign, send and confirm a transaction
*/
export async function sendAndConfirmTransaction(
connection: Connection,
from: Account,
transaction: Transaction,
runtimeErrorOk: boolean = false
): Promise<void> {
const signature = await connection.sendTransaction(from, transaction);
// Wait up to a couple seconds for a confirmation
let i = 4;
for (;;) {
const status = await connection.getSignatureStatus(signature);
if (status == 'Confirmed') return;
if (runtimeErrorOk && status == 'ProgramRuntimeError') return;
await sleep(500);
if (--i < 0) {
throw new Error(`Transaction '${signature}' was not confirmed (${status})`);
}
}
}

View File

@ -0,0 +1,6 @@
// @flow
// zzz
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}