fix: sendAndConfirmTransaction now returns the transaction signature

This commit is contained in:
Michael Vines 2018-11-03 19:02:12 -07:00
parent c424b48cd2
commit 9a043344d5
2 changed files with 8 additions and 4 deletions

View File

@ -232,5 +232,5 @@ declare module '@solana/web3.js' {
from: Account,
transaction: Transaction,
runtimeErrorOk?: boolean,
): Promise<void>;
): Promise<TransactionSignature>;
}

View File

@ -4,6 +4,7 @@ import {Connection} from '../connection';
import {Transaction} from '../transaction';
import {sleep} from './sleep';
import type {Account} from '../account';
import type {TransactionSignature} from '../transaction';
/**
* Sign, send and confirm a transaction
@ -13,12 +14,13 @@ export async function sendAndConfirmTransaction(
from: Account,
transaction: Transaction,
runtimeErrorOk: boolean = false
): Promise<void> {
): Promise<?TransactionSignature> {
let sendRetries = 10;
let signature;
for (;;) {
const start = Date.now();
const signature = await connection.sendTransaction(from, transaction);
signature = await connection.sendTransaction(from, transaction);
// Wait up to a couple seconds for a confirmation
let status = 'SignatureNotFound';
@ -38,7 +40,7 @@ export async function sendAndConfirmTransaction(
if ( (status === 'Confirmed') ||
(status === 'ProgramRuntimeError' && runtimeErrorOk) ) {
return;
break;
}
if (status !== 'AccountInUse' || --sendRetries <= 0) {
@ -48,5 +50,7 @@ export async function sendAndConfirmTransaction(
// Retry in 0..100ms to try to avoid another collision
await sleep(Math.random() * 100);
}
return signature;
}