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, from: Account,
transaction: Transaction, transaction: Transaction,
runtimeErrorOk?: boolean, runtimeErrorOk?: boolean,
): Promise<void>; ): Promise<TransactionSignature>;
} }

View File

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