feat: use pubsub to confirm transactions (#12095)
This commit is contained in:
@@ -14,6 +14,7 @@ import {MS_PER_SLOT} from './timing';
|
||||
import {Transaction} from './transaction';
|
||||
import {Message} from './message';
|
||||
import {sleep} from './util/sleep';
|
||||
import {promiseTimeout} from './util/promise-timeout';
|
||||
import {toBuffer} from './util/to-buffer';
|
||||
import type {Blockhash} from './blockhash';
|
||||
import type {FeeCalculator} from './fee-calculator';
|
||||
@@ -57,11 +58,11 @@ export type SendOptions = {
|
||||
*
|
||||
* @typedef {Object} ConfirmOptions
|
||||
* @property {boolean | undefined} skipPreflight disable transaction verification step
|
||||
* @property {number | undefined} confirmations desired number of cluster confirmations
|
||||
* @property {Commitment | undefined} commitment desired commitment level
|
||||
*/
|
||||
export type ConfirmOptions = {
|
||||
skipPreflight?: boolean,
|
||||
confirmations?: number,
|
||||
commitment?: Commitment,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1951,39 +1952,74 @@ export class Connection {
|
||||
/**
|
||||
* Confirm the transaction identified by the specified signature.
|
||||
*
|
||||
* If `confirmations` count is not specified, wait for transaction to be finalized.
|
||||
*
|
||||
* If `commitment` is not specified, default to 'max'.
|
||||
*/
|
||||
async confirmTransaction(
|
||||
signature: TransactionSignature,
|
||||
confirmations: ?number,
|
||||
): Promise<RpcResponseAndContext<SignatureStatus | null>> {
|
||||
const start = Date.now();
|
||||
const WAIT_TIMEOUT_MS = 60 * 1000;
|
||||
|
||||
let statusResponse = await this.getSignatureStatus(signature);
|
||||
for (;;) {
|
||||
const status = statusResponse.value;
|
||||
if (status) {
|
||||
// 'status.confirmations === null' implies that the tx has been finalized
|
||||
if (
|
||||
status.err ||
|
||||
status.confirmations === null ||
|
||||
(typeof confirmations === 'number' &&
|
||||
status.confirmations >= confirmations)
|
||||
) {
|
||||
break;
|
||||
}
|
||||
} else if (Date.now() - start >= WAIT_TIMEOUT_MS) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Sleep for approximately one slot
|
||||
await sleep(MS_PER_SLOT);
|
||||
statusResponse = await this.getSignatureStatus(signature);
|
||||
commitment: ?Commitment,
|
||||
): Promise<RpcResponseAndContext<SignatureResult>> {
|
||||
let decodedSignature;
|
||||
try {
|
||||
decodedSignature = bs58.decode(signature);
|
||||
} catch (err) {
|
||||
throw new Error('signature must be base58 encoded: ' + signature);
|
||||
}
|
||||
|
||||
return statusResponse;
|
||||
assert(decodedSignature.length === 64, 'signature has invalid length');
|
||||
|
||||
const start = Date.now();
|
||||
const subscriptionCommitment: Commitment = commitment || 'max';
|
||||
|
||||
let subscriptionId;
|
||||
let response: RpcResponseAndContext<SignatureResult> | null = null;
|
||||
const confirmPromise = new Promise((resolve, reject) => {
|
||||
try {
|
||||
subscriptionId = this.onSignature(
|
||||
signature,
|
||||
(result, context) => {
|
||||
subscriptionId = undefined;
|
||||
response = {
|
||||
context,
|
||||
value: result,
|
||||
};
|
||||
resolve();
|
||||
},
|
||||
subscriptionCommitment,
|
||||
);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
let timeoutMs = 60 * 1000;
|
||||
switch (subscriptionCommitment) {
|
||||
case 'recent':
|
||||
case 'single':
|
||||
case 'singleGossip': {
|
||||
timeoutMs = 10 * 1000;
|
||||
break;
|
||||
}
|
||||
// exhaust enums to ensure full coverage
|
||||
case 'max':
|
||||
case 'root':
|
||||
}
|
||||
|
||||
try {
|
||||
await promiseTimeout(confirmPromise, timeoutMs);
|
||||
} finally {
|
||||
if (subscriptionId) {
|
||||
this.removeSignatureListener(subscriptionId);
|
||||
}
|
||||
}
|
||||
|
||||
if (response === null) {
|
||||
const duration = (Date.now() - start) / 1000;
|
||||
throw new Error(
|
||||
`Transaction was not confirmed in ${duration.toFixed(2)} seconds`,
|
||||
);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user