2018-10-06 10:36:59 -07:00
|
|
|
// @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> {
|
2018-10-22 21:46:05 -07:00
|
|
|
const start = Date.now();
|
2018-10-06 10:36:59 -07:00
|
|
|
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) {
|
2018-10-22 21:46:05 -07:00
|
|
|
const duration = (Date.now() - start) / 1000;
|
|
|
|
throw new Error(`Transaction '${signature}' was not confirmed in ${duration.toFixed(2)} seconds (${status})`);
|
2018-10-06 10:36:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|