Skip to content

Commit 641f3ec

Browse files
authored
Merge pull request #67 from smartcontractkit/issue#56-response-listener-hangs
Issue#56 response listener hangs
2 parents 560bf57 + 073c381 commit 641f3ec

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

Diff for: .changeset/silly-berries-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chainlink/functions-toolkit': patch
3+
---
4+
5+
Change default minimum confirmations from listenForResponseFromTransaction() to 1.

Diff for: README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,22 @@ functionsRouterAddress,
452452

453453
To listen for a response to a single Functions request, use the `listenForResponseFromTransaction()` method.
454454
Optionally, you can provide:
455-
- timeout after which the listener will throw an error indicating that the time limit was exceeded (default 5 minutes)
456-
- number of block confirmations (default 2)
457-
- frequency of checking if the request is already included on-chain (or if it got moved after a chain re-org) (default 2 seconds)
455+
456+
- timeout after which the listener will throw an error indicating that the time limit was exceeded (default 5 minutes expressed in milliseconds)
457+
- number of block confirmations (default 1, but note that should be 2 or more to for higher confidence in finality, and to protect against reorgs)
458+
- frequency of checking if the request is already included on-chain (or if it got moved after a chain re-org) (default 2 seconds, but note that `checkInterval`s higher than block time could cause this listener to hang as the response will have completed before the next check.)
458459

459460
```
460461
const response: FunctionsResponse = await responseListener.listenForResponseFromTransaction(
461462
txHash: string,
462-
timeout?: number,
463+
timeoutMs?: number, // milliseconds
463464
confirmations?: number,
464465
checkInterval?: number,
465466
)
466467
```
467468

468469
`listenForResponseFromTransaction()` returns a response with the following structure:
470+
469471
```
470472
{
471473
requestId: string // Request ID of the fulfilled request represented as a bytes32 hex string
@@ -480,7 +482,8 @@ const response: FunctionsResponse = await responseListener.listenForResponseFrom
480482

481483
Alternatively, to listen using a request ID, use the `listenForResponse()` method.
482484

483-
**Notes:**
485+
**Notes:**
486+
484487
1. Request ID can change during a chain re-org so it's less reliable than a request transaction hash.
485488
2. If the methods are called after the response is already on chain, it won't be returned correctly.
486489
3. Listening for multiple responses simultaneously is not supported by the above methods and will lead to undefined behavior.
@@ -578,6 +581,7 @@ Any 3rd party imports used in the JavaScript source code are loaded asynchronous
578581
const { format } = await import("npm:date-fns");
579582
return Functions.encodeString(format(new Date(), "yyyy-MM-dd"));
580583
```
584+
581585
```
582586
const { escape } = await import("https://deno.land/std/regexp/mod.ts");
583587
return Functions.encodeString(escape("$hello*world?"));
@@ -720,6 +724,6 @@ const functionsRequestBytesHexString: string = buildRequestCBOR({
720724
})
721725
```
722726

723-
724727
## Browser use
725-
This package can also be used in most modern web browsers. You can import the package in your front-end application, and call the APIs as you would in a back end NodeJs/Deno environment.
728+
729+
This package can also be used in most modern web browsers. You can import the package in your front-end application, and call the APIs as you would in a back end NodeJs/Deno environment.

Diff for: src/ResponseListener.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ export class ResponseListener {
2121
this.functionsRouter = new Contract(functionsRouterAddress, FunctionsRouterSource.abi, provider)
2222
}
2323

24-
public async listenForResponse(requestId: string, timeout = 300000): Promise<FunctionsResponse> {
24+
public async listenForResponse(
25+
requestId: string,
26+
timeoutMs = 300000,
27+
): Promise<FunctionsResponse> {
2528
let expirationTimeout: NodeJS.Timeout
2629
const responsePromise = new Promise<FunctionsResponse>((resolve, reject) => {
2730
expirationTimeout = setTimeout(() => {
2831
reject('Response not received within timeout period')
29-
}, timeout)
32+
}, timeoutMs)
3033

3134
this.functionsRouter.on(
3235
'RequestProcessed',
@@ -60,11 +63,19 @@ export class ResponseListener {
6063
return responsePromise
6164
}
6265

66+
/**
67+
*
68+
* @param txHash Tx hash for the Functions Request
69+
* @param timeoutMs after which the listener throws, indicating the time limit was exceeded (default 5 minutes)
70+
* @param confirmations number of confirmations to wait for before considering the transaction successful (default 1, but recommend 2 or more)
71+
* @param checkIntervalMs frequency of checking if the Tx is included on-chain (or if it got moved after a chain re-org) (default 2 seconds. Intervals longer than block time may cause the listener to wait indefinitely.)
72+
* @returns
73+
*/
6374
public async listenForResponseFromTransaction(
6475
txHash: string,
65-
timeout = 3000000,
66-
confirmations = 2,
67-
checkInterval = 2000,
76+
timeoutMs = 3000000,
77+
confirmations = 1,
78+
checkIntervalMs = 2000,
6879
): Promise<FunctionsResponse> {
6980
return new Promise<FunctionsResponse>((resolve, reject) => {
7081
;(async () => {
@@ -73,14 +84,14 @@ export class ResponseListener {
7384
let checkTimeout: NodeJS.Timeout
7485
const expirationTimeout = setTimeout(() => {
7586
reject('Response not received within timeout period')
76-
}, timeout)
87+
}, timeoutMs)
7788

7889
const check = async () => {
79-
const receipt = await this.provider.waitForTransaction(txHash, confirmations, timeout)
90+
const receipt = await this.provider.waitForTransaction(txHash, confirmations, timeoutMs)
8091
const updatedId = receipt.logs[0].topics[1]
8192
if (updatedId !== requestId) {
8293
requestId = updatedId
83-
const response = await this.listenForResponse(requestId, timeout)
94+
const response = await this.listenForResponse(requestId, timeoutMs)
8495
if (updatedId === requestId) {
8596
// Resolve only if the ID hasn't changed in the meantime
8697
clearTimeout(expirationTimeout)
@@ -91,7 +102,7 @@ export class ResponseListener {
91102
}
92103

93104
// Check periodically if the transaction has been re-orged and requestID changed
94-
checkTimeout = setInterval(check, checkInterval)
105+
checkTimeout = setInterval(check, checkIntervalMs)
95106

96107
check()
97108
})()

0 commit comments

Comments
 (0)