From f04fbd6443b8600a1c28078006ec9a7983d9efd0 Mon Sep 17 00:00:00 2001 From: Yimin Date: Wed, 23 Mar 2022 12:49:37 +1100 Subject: [PATCH 1/3] Add timeout for getPastEventsFrom --- src/client.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index ce97635..81744d0 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,5 +1,5 @@ import Web3 from "web3"; -import { Contract } from "web3-eth-contract"; +import { Contract, EventData } from "web3-eth-contract"; import { provider } from "web3-core"; import detectEthereumProvider from "@metamask/detect-provider"; import { MetaMaskInpageProvider } from "@metamask/providers"; @@ -10,6 +10,9 @@ import { DelphinusProvider } from "./provider"; export class DelphinusContract { private readonly contract: Contract; private readonly jsonInterface: any; + public static contractAPITimeOut: number = 10000; //10 seconds + protected static timeoutHandlerDict: {[id: number] : NodeJS.Timeout} = {}; + protected static timeOutId: number = 0; constructor( web3Instance: DelphinusWeb3, @@ -36,10 +39,37 @@ export class DelphinusContract { return this.jsonInterface; } + promiseWithTimeout( + promise: Promise, + ms: number, + timeoutError:string + ): Promise { + const timeoutPromise = new Promise((_, reject) => { + var timeoutHandler = setTimeout(() => { + reject(new Error(timeoutError)); + }, ms); + DelphinusContract.timeoutHandlerDict[DelphinusContract.timeOutId++] = timeoutHandler; + }); + + // returns a race between timeout and the passed promise + return Promise.race([promise, timeoutPromise]); + } + async getPastEventsFrom(fromBlock: number) { - return await this.contract.getPastEvents("allEvents", { + const getPastEventsPromise = this.contract.getPastEvents("allEvents", { fromBlock: fromBlock, }); + + try { + const currentTimeOutId = DelphinusContract.timeOutId; + const result = await this.promiseWithTimeout(getPastEventsPromise, DelphinusContract.contractAPITimeOut, `getPastEvents time out after ${DelphinusContract.contractAPITimeOut} milliseconds`); + clearTimeout(DelphinusContract.timeoutHandlerDict[currentTimeOutId]); + return result; + } + catch (e) { + console.log("Exist Process: ", e) + process.exit(1); + } } address() { From 17a7f7010536b998546c7941c766f12adc3197df Mon Sep 17 00:00:00 2001 From: yymone Date: Tue, 22 Mar 2022 20:47:42 -0700 Subject: [PATCH 2/3] Refine code to remove global handler dependency --- src/client.ts | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/client.ts b/src/client.ts index 81744d0..165f8d6 100644 --- a/src/client.ts +++ b/src/client.ts @@ -11,8 +11,6 @@ export class DelphinusContract { private readonly contract: Contract; private readonly jsonInterface: any; public static contractAPITimeOut: number = 10000; //10 seconds - protected static timeoutHandlerDict: {[id: number] : NodeJS.Timeout} = {}; - protected static timeOutId: number = 0; constructor( web3Instance: DelphinusWeb3, @@ -43,16 +41,29 @@ export class DelphinusContract { promise: Promise, ms: number, timeoutError:string - ): Promise { + ){ + var timeoutHandler: NodeJS.Timeout; const timeoutPromise = new Promise((_, reject) => { - var timeoutHandler = setTimeout(() => { + timeoutHandler = setTimeout(() => { reject(new Error(timeoutError)); }, ms); - DelphinusContract.timeoutHandlerDict[DelphinusContract.timeOutId++] = timeoutHandler; }); // returns a race between timeout and the passed promise - return Promise.race([promise, timeoutPromise]); + return Promise.race([promise, timeoutPromise]) + .then( + (value) => { + clearTimeout(timeoutHandler); + return value; + } + ) + .catch( + (error) => { + //throw error; + console.log("Exist Process: ", error) + process.exit(1); + } + ); } async getPastEventsFrom(fromBlock: number) { @@ -60,16 +71,7 @@ export class DelphinusContract { fromBlock: fromBlock, }); - try { - const currentTimeOutId = DelphinusContract.timeOutId; - const result = await this.promiseWithTimeout(getPastEventsPromise, DelphinusContract.contractAPITimeOut, `getPastEvents time out after ${DelphinusContract.contractAPITimeOut} milliseconds`); - clearTimeout(DelphinusContract.timeoutHandlerDict[currentTimeOutId]); - return result; - } - catch (e) { - console.log("Exist Process: ", e) - process.exit(1); - } + return await this.promiseWithTimeout(getPastEventsPromise, DelphinusContract.contractAPITimeOut, `getPastEvents time out after ${DelphinusContract.contractAPITimeOut} milliseconds`); } address() { From fcdeb0bc1082b60f8eae16c6fbf35cb651b40cf9 Mon Sep 17 00:00:00 2001 From: yymone Date: Tue, 22 Mar 2022 20:58:54 -0700 Subject: [PATCH 3/3] refine code to move out process exit --- src/client.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/client.ts b/src/client.ts index 165f8d6..58b185e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -57,13 +57,6 @@ export class DelphinusContract { return value; } ) - .catch( - (error) => { - //throw error; - console.log("Exist Process: ", error) - process.exit(1); - } - ); } async getPastEventsFrom(fromBlock: number) { @@ -71,7 +64,14 @@ export class DelphinusContract { fromBlock: fromBlock, }); - return await this.promiseWithTimeout(getPastEventsPromise, DelphinusContract.contractAPITimeOut, `getPastEvents time out after ${DelphinusContract.contractAPITimeOut} milliseconds`); + try{ + return await this.promiseWithTimeout(getPastEventsPromise, DelphinusContract.contractAPITimeOut, `getPastEvents time out after ${DelphinusContract.contractAPITimeOut} milliseconds`); + } + catch(e) + { + console.log("Exit Process: ", e); + process.exit(1); + } } address() {