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