feat: update transaction confirming apis
This commit is contained in:
committed by
Michael Vines
parent
3b71ec1ff6
commit
839e93480c
@@ -1,54 +1,34 @@
|
||||
// @flow
|
||||
|
||||
import {Connection} from '../connection';
|
||||
import type {Commitment} from '../connection';
|
||||
import {sleep} from './sleep';
|
||||
import type {TransactionSignature} from '../transaction';
|
||||
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from '../timing';
|
||||
|
||||
/**
|
||||
* Sign, send and confirm a raw transaction
|
||||
* Send and confirm a raw transaction
|
||||
*/
|
||||
export async function sendAndConfirmRawTransaction(
|
||||
connection: Connection,
|
||||
rawTransaction: Buffer,
|
||||
commitment: ?Commitment,
|
||||
confirmations: ?number,
|
||||
): Promise<TransactionSignature> {
|
||||
const start = Date.now();
|
||||
const statusCommitment = commitment || connection.commitment || 'max';
|
||||
let signature = await connection.sendRawTransaction(rawTransaction);
|
||||
const signature = await connection.sendRawTransaction(rawTransaction);
|
||||
const status = (await connection.confirmTransaction(signature, confirmations))
|
||||
.value;
|
||||
|
||||
// Wait up to a couple slots for a confirmation
|
||||
let status = null;
|
||||
let statusRetries = 6;
|
||||
for (;;) {
|
||||
status = (await connection.getSignatureStatus(signature)).value;
|
||||
if (status) {
|
||||
if (statusCommitment === 'max' && status.confirmations === null) {
|
||||
break;
|
||||
} else if (statusCommitment === 'recent') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep for approximately half a slot
|
||||
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
|
||||
|
||||
if (--statusRetries <= 0) {
|
||||
const duration = (Date.now() - start) / 1000;
|
||||
if (status) {
|
||||
if (status.err) {
|
||||
throw new Error(
|
||||
`Raw Transaction '${signature}' was not confirmed in ${duration.toFixed(
|
||||
2,
|
||||
)} seconds (${JSON.stringify(status)})`,
|
||||
`Raw transaction ${signature} failed (${JSON.stringify(status)})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (status && !status.err) {
|
||||
return signature;
|
||||
}
|
||||
|
||||
const duration = (Date.now() - start) / 1000;
|
||||
throw new Error(
|
||||
`Raw transaction ${signature} failed (${JSON.stringify(status)})`,
|
||||
`Raw transaction '${signature}' was not confirmed in ${duration.toFixed(
|
||||
2,
|
||||
)} seconds`,
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user