fix readRaw disconnecting
This commit is contained in:
+19
-23
@@ -12,12 +12,12 @@ const PAYLOAD_SIZE = 925
|
||||
const debug = false
|
||||
|
||||
const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
|
||||
try{
|
||||
try {
|
||||
const subApi = new RippleAPI({ server: api.connection['_url'] })
|
||||
await subApi.connect()
|
||||
return subApi
|
||||
}catch(e){
|
||||
if(debug){
|
||||
} catch (e) {
|
||||
if (debug) {
|
||||
console.log("CLONEAPI ERR", e)
|
||||
}
|
||||
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> => {
|
||||
if(debug) console.log("Getting acc info for", accountAddress)
|
||||
if (debug) console.log("Getting acc info for", 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')
|
||||
@@ -41,7 +41,7 @@ const sendReliably = (api: RippleAPI, signed: any, preparedPayment,): Promise<an
|
||||
minLedgerVersion: 25235454
|
||||
})
|
||||
} catch (e) {
|
||||
|
||||
|
||||
// Typical error when the tx hasn't been validated yet:
|
||||
if ((e as Error).name !== 'MissingLedgerHistoryError') {
|
||||
//console.log(e)
|
||||
@@ -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) => {
|
||||
if(debug) console.log("Sending payment with seq", sequence)
|
||||
if (debug) console.log("Sending payment with seq", sequence)
|
||||
const options: Instructions = {
|
||||
maxLedgerVersionOffset: 5,
|
||||
fee: MIN_XRP_FEE,
|
||||
@@ -103,10 +103,10 @@ export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to
|
||||
|
||||
return txHash
|
||||
} catch (error) {
|
||||
if(debug)
|
||||
if (debug)
|
||||
console.log("SENDPAYMENT ERROR", error)
|
||||
throw error
|
||||
}finally{
|
||||
} finally {
|
||||
_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)
|
||||
return resp['tx_json'].hash
|
||||
} catch (error) {
|
||||
if(debug){
|
||||
if (debug) {
|
||||
console.log("WRITERAW ERR", 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> => {
|
||||
while(!api.isConnected()){
|
||||
try{
|
||||
await api.connect()
|
||||
}catch(e){
|
||||
//retry
|
||||
}
|
||||
}
|
||||
api = await cloneApi(api)
|
||||
let tx
|
||||
try{
|
||||
try {
|
||||
tx = await api.getTransaction(hash, {
|
||||
minLedgerVersion: 25235454
|
||||
})
|
||||
}catch(e){
|
||||
// if(debug){
|
||||
console.log("READRAW ERR", e)
|
||||
// }
|
||||
} catch (e) {
|
||||
// if(debug){
|
||||
console.log("READRAW ERR", e)
|
||||
// }
|
||||
throw e
|
||||
} finally {
|
||||
await api.disconnect()
|
||||
}
|
||||
|
||||
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)
|
||||
const chunks = chunkString(data, PAYLOAD_SIZE)
|
||||
const latestSequence = await getLatestSequence(api, wallet.address)
|
||||
|
||||
Reference in New Issue
Block a user