fix: sendAndConfirmTransaction now returns the transaction signature
This commit is contained in:
parent
c424b48cd2
commit
9a043344d5
|
@ -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>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue