fix readRaw disconnecting

This commit is contained in:
nitowa
2022-02-12 23:36:07 +01:00
parent ad9fd09f91
commit ca8498707b
2 changed files with 20 additions and 24 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xrpio", "name": "xrpio",
"version": "0.0.9", "version": "0.0.10",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git" "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"
+18 -22
View File
@@ -12,12 +12,12 @@ const PAYLOAD_SIZE = 925
const debug = false const debug = false
const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => { const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
try{ try {
const subApi = new RippleAPI({ server: api.connection['_url'] }) const subApi = new RippleAPI({ server: api.connection['_url'] })
await subApi.connect() await subApi.connect()
return subApi return subApi
}catch(e){ } catch (e) {
if(debug){ if (debug) {
console.log("CLONEAPI ERR", e) console.log("CLONEAPI ERR", e)
} }
return await cloneApi(api) return await cloneApi(api)
@@ -25,9 +25,9 @@ const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
} }
export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise<number> => { export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise<number> => {
if(debug) console.log("Getting acc info for", accountAddress) if (debug) console.log("Getting acc info for", accountAddress)
const accountInfo = await api.getAccountInfo(accountAddress, {}) const accountInfo = await api.getAccountInfo(accountAddress, {})
return Number(accountInfo.sequence-1) return Number(accountInfo.sequence - 1)
} }
const compressB64 = async (data: string) => (await util.promisify(zlib.deflate)(Buffer.from(data, 'utf-8'))).toString('base64') const compressB64 = async (data: string) => (await util.promisify(zlib.deflate)(Buffer.from(data, 'utf-8'))).toString('base64')
@@ -69,7 +69,7 @@ const sendReliably = (api: RippleAPI, signed: any, preparedPayment,): Promise<an
}) })
export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to: string, secret: string, sequence: number) => { export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to: string, secret: string, sequence: number) => {
if(debug) console.log("Sending payment with seq", sequence) if (debug) console.log("Sending payment with seq", sequence)
const options: Instructions = { const options: Instructions = {
maxLedgerVersionOffset: 5, maxLedgerVersionOffset: 5,
fee: MIN_XRP_FEE, fee: MIN_XRP_FEE,
@@ -103,10 +103,10 @@ export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to
return txHash return txHash
} catch (error) { } catch (error) {
if(debug) if (debug)
console.log("SENDPAYMENT ERROR", error) console.log("SENDPAYMENT ERROR", error)
throw error throw error
}finally{ } finally {
_api.disconnect() _api.disconnect()
} }
} }
@@ -132,7 +132,7 @@ export const writeRaw = async (api: RippleAPI, data: Memo, from:
const resp = await sendPayment(api, [data], from, to, secret, sequence) const resp = await sendPayment(api, [data], from, to, secret, sequence)
return resp['tx_json'].hash return resp['tx_json'].hash
} catch (error) { } catch (error) {
if(debug){ if (debug) {
console.log("WRITERAW ERR", error); console.log("WRITERAW ERR", error);
} }
throw error throw error
@@ -141,23 +141,19 @@ export const writeRaw = async (api: RippleAPI, data: Memo, from:
export const readRaw = async (api: RippleAPI, hash: string): Promise<Memo> => { export const readRaw = async (api: RippleAPI, hash: string): Promise<Memo> => {
while(!api.isConnected()){ api = await cloneApi(api)
try{
await api.connect()
}catch(e){
//retry
}
}
let tx let tx
try{ try {
tx = await api.getTransaction(hash, { tx = await api.getTransaction(hash, {
minLedgerVersion: 25235454 minLedgerVersion: 25235454
}) })
}catch(e){ } catch (e) {
// if(debug){ // if(debug){
console.log("READRAW ERR", e) console.log("READRAW ERR", e)
// } // }
throw e throw e
} finally {
await api.disconnect()
} }
if (!tx || !tx.specification || !tx.specification['memos'] || !tx.specification['memos'][0]) { if (!tx || !tx.specification || !tx.specification['memos'] || !tx.specification['memos'][0]) {
@@ -175,7 +171,7 @@ export const subscribe = async (api: RippleAPI, address: string, callback: (tx)
}) })
} }
export const treeWrite = async (api: RippleAPI, data: string, wallet: Wallet, to: string, format: 'L'|'N' = 'L'): Promise<string> => { export const treeWrite = async (api: RippleAPI, data: string, wallet: Wallet, to: string, format: 'L' | 'N' = 'L'): Promise<string> => {
data = await compressB64(data) data = await compressB64(data)
const chunks = chunkString(data, PAYLOAD_SIZE) const chunks = chunkString(data, PAYLOAD_SIZE)
const latestSequence = await getLatestSequence(api, wallet.address) const latestSequence = await getLatestSequence(api, wallet.address)