diff --git a/package.json b/package.json index 30de0f6..0e965ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xrpio", - "version": "0.1.4", + "version": "0.1.6", "repository": { "type": "git", "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git" diff --git a/src/util/types.ts b/src/util/types.ts index 487ef7a..c6a9295 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -16,11 +16,14 @@ export type TxHash = string export type Options = { debug?: boolean connectionTimeout?: number - readFreshApi?: boolean + readMaxRetry?: number + readRetryTimeout?: number } export const defaultOptions = { debug: false, connectionTimeout: 100000, - readFreshApi: true + readFreshApi: true, + readMaxRetry: -1, + readRetryTimeout: 1000 } \ No newline at end of file diff --git a/src/xrpIO/xrpl-binding.ts b/src/xrpIO/xrpl-binding.ts index 1d20842..eb642a1 100644 --- a/src/xrpIO/xrpl-binding.ts +++ b/src/xrpIO/xrpl-binding.ts @@ -21,9 +21,10 @@ export class xrpIO { private options: Options = defaultOptions ) { - this.options.debug = this.options.debug ? Boolean(this.options.debug) : defaultOptions.debug - this.options.connectionTimeout = this.options.connectionTimeout ? Number(this.options.connectionTimeout) : defaultOptions.connectionTimeout - this.options.readFreshApi = this.options.readFreshApi ? Boolean(this.options.readFreshApi) : defaultOptions.readFreshApi + this.options.debug = options.debug ? Boolean(options.debug) : defaultOptions.debug + this.options.connectionTimeout = options.connectionTimeout ? Number(options.connectionTimeout) : defaultOptions.connectionTimeout + this.options.readMaxRetry = options.readMaxRetry ? Number(options.readMaxRetry) : defaultOptions.readMaxRetry + this.options.readRetryTimeout = options.readRetryTimeout ? Number(options.readRetryTimeout) : defaultOptions.readRetryTimeout this.api = new Client(server, { connectionTimeout: this.options.connectionTimeout @@ -101,30 +102,22 @@ export class xrpIO { return tx.result.hash } - private async getTransaction(hash: string): Promise { + private async getTransaction(hash: string, retry=0): Promise { this.dbg("Getting Tx", hash) - - if (this.options.readFreshApi) { - let _api = await this.cloneApi() - while (true) { - try { - const response = await _api.request({ - command: 'tx', - transaction: hash, - }) - await _api.disconnect() - return response - } catch (e) { - this.dbg("Retrying to get", hash) - await _api.disconnect() - _api = await this.cloneApi() - } - } - }else{ + try{ return await this.api.request({ command: 'tx', transaction: hash, }) + }catch(e){ + this.dbg(e) + if(this.options.readMaxRetry != -1){ + if(retry >= this.options.readMaxRetry) + console.error("Retry limit exceeded for", hash, ". this is an irrecoverable error") + throw e + } + await new Promise(res => setTimeout(res, this.options.readRetryTimeout)) + return await this.getTransaction(hash, retry+1) } }