Skip to content

Commit 2921e1d

Browse files
committed
feat: Add sendAndConfirmTransaction
1 parent d35366d commit 2921e1d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @flow
2+
3+
import {Connection, Transaction} from '..';
4+
5+
import {sleep} from './sleep';
6+
7+
import type {Account} from '..';
8+
9+
/**
10+
* Sign, send and confirm a transaction
11+
*/
12+
export async function sendAndConfirmTransaction(
13+
connection: Connection,
14+
from: Account,
15+
transaction: Transaction,
16+
runtimeErrorOk: boolean = false
17+
): Promise<void> {
18+
const signature = await connection.sendTransaction(from, transaction);
19+
20+
// Wait up to a couple seconds for a confirmation
21+
let i = 4;
22+
for (;;) {
23+
const status = await connection.getSignatureStatus(signature);
24+
if (status == 'Confirmed') return;
25+
if (runtimeErrorOk && status == 'ProgramRuntimeError') return;
26+
await sleep(500);
27+
if (--i < 0) {
28+
throw new Error(`Transaction '${signature}' was not confirmed (${status})`);
29+
}
30+
}
31+
}
32+

src/util/sleep.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @flow
2+
3+
// zzz
4+
export function sleep(ms: number): Promise<void> {
5+
return new Promise(resolve => setTimeout(resolve, ms));
6+
}

0 commit comments

Comments
 (0)