Skip to content

Commit 29d26a0

Browse files
wait for tx confirmation
1 parent a7a0ef9 commit 29d26a0

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

sdk/src/network-client.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -961,24 +961,46 @@ class AleoNetworkClient {
961961
* @param {string} solution The string representation of the solution desired to be submitted to the network.
962962
*/
963963
async waitForTransactionConfirmation(
964-
transactionId: string,
965-
checkInterval: number = 2000, // Poll every 2 seconds
966-
timeout: number = 45000 // Timeout after 45 seconds
967-
): Promise<Transaction> {
964+
transactionId: string,
965+
checkInterval: number = 2000,
966+
timeout: number = 45000
967+
): Promise<"accepted" | "rejected"> {
968968
const startTime = Date.now();
969-
970-
return new Promise<Transaction>((resolve, reject) => {
969+
970+
return new Promise((resolve, reject) => {
971971
const interval = setInterval(async () => {
972+
const elapsed = Date.now() - startTime;
973+
974+
if (elapsed > timeout) {
975+
clearInterval(interval);
976+
return reject(new Error("Transaction confirmation timed out"));
977+
}
978+
972979
try {
973-
// Replace with actual Aleo transaction lookup API
974-
const transaction = <Transaction>await this.getTransactionObject(transactionId);
975-
resolve(transaction);
976-
if (Date.now() - startTime > timeout) {
980+
const res = await fetch(`https://api.explorer.provable.com/v1/mainnet/transaction/confirmed/${transactionId}`);
981+
982+
if (!res.ok) {
983+
const text = await res.text();
984+
console.error("Non-OK response:", res.status, text);
985+
clearInterval(interval);
986+
return reject(new Error(`HTTP ${res.status}: ${text}`));
987+
}
988+
989+
const data = await res.json();
990+
991+
if (data?.status === "accepted") {
992+
clearInterval(interval);
993+
return resolve("accepted");
994+
}
995+
996+
if (data?.status === "rejected") {
977997
clearInterval(interval);
978-
reject(new Error("Transaction confirmation timed out"));
998+
return resolve("rejected");
979999
}
980-
} catch (error) {
981-
console.error("Error checking transaction:", error);
1000+
1001+
// no status? keep polling
1002+
} catch (err) {
1003+
console.error("Polling error:", err);
9821004
}
9831005
}, checkInterval);
9841006
});

sdk/tests/network-client.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,27 @@ describe('NodeConnection', () => {
198198
});
199199
});
200200

201+
describe('waitForTransactionConfirmation', () => {
202+
it('should return accepted for a valid tx ID', async () => {
203+
const connection = new AleoNetworkClient("https://api.explorer.provable.com/");
204+
const status = await connection.waitForTransactionConfirmation("at1dl9lze8wscct0dee8x9tjnfmpjvj33hh23jcnp5f0ywjn5552yrsperzl9");
205+
expect(status).to.equal("accepted");
206+
});
207+
208+
it('should return rejected for a rejected tx ID', async () => {
209+
const connection = new AleoNetworkClient("https://api.explorer.provable.com/");
210+
const status = await connection.waitForTransactionConfirmation("at1x5ed0pyjpt3770e0m60psvdvqeww5z5f32t7velrmrjfhvzgysxqll4g6a");
211+
expect(status).to.equal("rejected");
212+
});
213+
214+
it('should throw for an invalid tx ID', async () => {
215+
const connection = new AleoNetworkClient("https://api.explorer.provable.com/");
216+
await expectThrows(() =>
217+
connection.waitForTransactionConfirmation("at1dl9lze8wscct0dee8x9tjnfmpj12345678jcnp5f0ywjn5552yrsperzl9")
218+
);
219+
});
220+
});
221+
201222
describe('findUnspentRecords', () => {
202223
it('should fail if block heights or private keys are incorrectly specified', async () => {
203224
await expectThrows(
@@ -395,3 +416,4 @@ describe('NodeConnection', () => {
395416
});
396417
})
397418
});
419+

0 commit comments

Comments
 (0)