feat: Add sendAndConfirmTransaction
This commit is contained in:
parent
feab984cf1
commit
a596e99b4a
|
@ -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})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
// @flow
|
||||
|
||||
// zzz
|
||||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
Loading…
Reference in New Issue