add option to not create fresh APIs for every read
This commit is contained in:
+9
-2
@@ -14,6 +14,13 @@ export type PublicKey = string
|
||||
export type Amount = number
|
||||
export type TxHash = string
|
||||
export type Options = {
|
||||
debug: boolean,
|
||||
connectionTimeout: number
|
||||
debug?: boolean
|
||||
connectionTimeout?: number
|
||||
readFreshApi?: boolean
|
||||
}
|
||||
|
||||
export const defaultOptions = {
|
||||
debug: false,
|
||||
connectionTimeout: 100000,
|
||||
readFreshApi: true
|
||||
}
|
||||
+35
-26
@@ -1,4 +1,4 @@
|
||||
import { Memo, Options } from '../util/types'
|
||||
import { defaultOptions, Memo, Options } from '../util/types'
|
||||
import { Client, Payment, TxResponse, Wallet } from 'xrpl'
|
||||
|
||||
import * as zlib from 'zlib'
|
||||
@@ -18,11 +18,13 @@ export class xrpIO {
|
||||
|
||||
constructor(
|
||||
private server: string,
|
||||
private options: Options = {
|
||||
debug: false,
|
||||
connectionTimeout: 100000
|
||||
}
|
||||
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.api = new Client(server, {
|
||||
connectionTimeout: this.options.connectionTimeout
|
||||
})
|
||||
@@ -34,9 +36,9 @@ export class xrpIO {
|
||||
}
|
||||
|
||||
public async disconnect(): Promise<void> {
|
||||
try{
|
||||
try {
|
||||
await this.api.disconnect()
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
console.log("DISCONNECT ERROR", e)
|
||||
}
|
||||
}
|
||||
@@ -46,11 +48,11 @@ export class xrpIO {
|
||||
connectionTimeout: this.options.connectionTimeout
|
||||
})
|
||||
|
||||
while(!_api.isConnected()){
|
||||
try{
|
||||
while (!_api.isConnected()) {
|
||||
try {
|
||||
await _api.connect()
|
||||
return _api
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
this.dbg('CLONEAPI ERR', 'Connection failed', String(e['message']))
|
||||
await _api.disconnect()
|
||||
_api = new Client(this.server, {
|
||||
@@ -101,21 +103,28 @@ export class xrpIO {
|
||||
|
||||
private async getTransaction(hash: string): Promise<TxResponse> {
|
||||
this.dbg("Getting Tx", hash)
|
||||
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()
|
||||
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{
|
||||
return await this.api.request({
|
||||
command: 'tx',
|
||||
transaction: hash,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +145,7 @@ export class xrpIO {
|
||||
data = await compressB64(data)
|
||||
const chunks = chunkString(data, PAYLOAD_SIZE)
|
||||
const latestSequence = await this.getAccountSequence(wallet.address)
|
||||
const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => this.writeRaw({ data: chunk, format: format }, to, secret, latestSequence+Number(i))))
|
||||
const hashes = await Promise.all(Object.entries(chunks).map(([i, chunk]) => this.writeRaw({ data: chunk, format: format }, to, secret, latestSequence + Number(i))))
|
||||
|
||||
if (hashes.length === 1) {
|
||||
return hashes[0]
|
||||
@@ -145,7 +154,7 @@ export class xrpIO {
|
||||
return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N')
|
||||
}
|
||||
|
||||
public async treeRead(hashes: string[]): Promise<string>{
|
||||
public async treeRead(hashes: string[]): Promise<string> {
|
||||
const memos = await Promise.all(hashes.map(hash => this.readRaw(hash)))
|
||||
const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
|
||||
|
||||
@@ -156,7 +165,7 @@ export class xrpIO {
|
||||
return payload
|
||||
}
|
||||
|
||||
public async getAccountSequence(address: string): Promise<number>{
|
||||
public async getAccountSequence(address: string): Promise<number> {
|
||||
this.dbg("Getting acc info for", address)
|
||||
const accountInfo = await this.api.request({
|
||||
command: 'account_info',
|
||||
|
||||
@@ -9,8 +9,6 @@ let sendWallet: Wallet
|
||||
let receiveWallet: Wallet
|
||||
let api: xrpIO
|
||||
|
||||
const ONLY_LARGE_TESTS = true
|
||||
|
||||
describe('XRPIO', () => {
|
||||
before(async function(){
|
||||
this.timeout(15000)
|
||||
|
||||
Reference in New Issue
Block a user