Skip to content

Commit f85e8ae

Browse files
jasonpaulosDamian Barabonkov
authored andcommitted
Add wait for confirmation function (#469)
* Added waitForConfirmation to v2 API * Renamed timeout to waitRounds * Move to waitForConfirmation function * Change eslint settings * Use in cucumber tests * Delete file from algod folder Co-authored-by: Damian Barabonkov <damianb@mit.edu>
1 parent b39e6f9 commit f85e8ae

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
ts: 'never',
2424
},
2525
],
26+
'import/prefer-default-export': 'off',
2627
'lines-between-class-members': [
2728
'error',
2829
'always',

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export { default as Kmd } from './client/kmd';
233233
export { default as IntDecoding } from './types/intDecoding';
234234
export { default as Account } from './types/account';
235235
export { default as Indexer } from './client/v2/indexer/indexer';
236+
export { waitForConfirmation } from './wait';
236237
export {
237238
isValidAddress,
238239
encodeAddress,

src/wait.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Algodv2 from './client/v2/algod/algod';
2+
3+
/**
4+
* Wait until a transaction has been confirmed or rejected by the network, or
5+
* until 'waitRounds' number of rounds have passed.
6+
* @param client - An Algodv2 client
7+
* @param txid - The ID of the transaction to wait for.
8+
* @param waitRounds - The maximum number of rounds to wait for.
9+
* @returns A promise that, upon success, will resolve to the output of the
10+
* `pendingTransactionInformation` call for the confirmed transaction.
11+
*/
12+
export async function waitForConfirmation(
13+
client: Algodv2,
14+
txid: string,
15+
waitRounds: number
16+
): Promise<Record<string, any>> {
17+
// Wait until the transaction is confirmed or rejected, or until 'waitRounds'
18+
// number of rounds have passed.
19+
20+
const status = await client.status().do();
21+
if (typeof status === 'undefined') {
22+
throw new Error('Unable to get node status');
23+
}
24+
const startRound = status['last-round'] + 1;
25+
let currentRound = startRound;
26+
27+
/* eslint-disable no-await-in-loop */
28+
while (currentRound < startRound + waitRounds) {
29+
const pendingInfo = await client.pendingTransactionInformation(txid).do();
30+
31+
if (pendingInfo['confirmed-round']) {
32+
// Got the completed Transaction
33+
return pendingInfo;
34+
}
35+
36+
if (pendingInfo['pool-error']) {
37+
// If there was a pool error, then the transaction has been rejected!
38+
throw new Error(`Transaction Rejected: ${pendingInfo['pool-error']}`);
39+
}
40+
41+
await client.statusAfterBlock(currentRound).do();
42+
currentRound += 1;
43+
}
44+
/* eslint-enable no-await-in-loop */
45+
throw new Error(`Transaction not confirmed after ${waitRounds} rounds!`);
46+
}

tests/cucumber/steps/steps.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4242,11 +4242,12 @@ module.exports = function getSteps(options) {
42424242
const fundingResponse = await this.v2Client
42434243
.sendRawTransaction(stxKmd)
42444244
.do();
4245-
await this.v2Client.statusAfterBlock(sp.firstRound + 2).do();
4246-
const fundingConfirmation = await this.acl.transactionById(
4247-
fundingResponse.txId
4245+
const info = await algosdk.waitForConfirmation(
4246+
this.v2Client,
4247+
fundingResponse.txId,
4248+
2
42484249
);
4249-
assert.deepStrictEqual(true, 'type' in fundingConfirmation);
4250+
assert.ok(info['confirmed-round'] > 0);
42504251
}
42514252
);
42524253

@@ -4358,10 +4359,12 @@ module.exports = function getSteps(options) {
43584359
);
43594360

43604361
Given('I wait for the transaction to be confirmed.', async function () {
4361-
const sp = await this.v2Client.getTransactionParams().do();
4362-
await this.v2Client.statusAfterBlock(sp.firstRound + 2).do();
4363-
const confirmation = await this.acl.transactionById(this.appTxid.txId);
4364-
assert.deepStrictEqual(true, 'type' in confirmation);
4362+
const info = await algosdk.waitForConfirmation(
4363+
this.v2Client,
4364+
this.appTxid.txId,
4365+
2
4366+
);
4367+
assert.ok(info['confirmed-round'] > 0);
43654368
});
43664369

43674370
Given('I remember the new application ID.', async function () {

0 commit comments

Comments
 (0)