feat: Add sendAndConfirmTransaction
This commit is contained in:
32
web3.js/src/util/send-and-confirm-transaction.js
Normal file
32
web3.js/src/util/send-and-confirm-transaction.js
Normal 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})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
6
web3.js/src/util/sleep.js
Normal file
6
web3.js/src/util/sleep.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
// zzz
|
||||||
|
export function sleep(ms: number): Promise<void> {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
Reference in New Issue
Block a user