fix readRaw disconnecting
This commit is contained in:
+1
-1
@@ -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"
|
||||||
|
|||||||
+19
-23
@@ -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')
|
||||||
@@ -41,7 +41,7 @@ const sendReliably = (api: RippleAPI, signed: any, preparedPayment,): Promise<an
|
|||||||
minLedgerVersion: 25235454
|
minLedgerVersion: 25235454
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
||||||
// Typical error when the tx hasn't been validated yet:
|
// Typical error when the tx hasn't been validated yet:
|
||||||
if ((e as Error).name !== 'MissingLedgerHistoryError') {
|
if ((e as Error).name !== 'MissingLedgerHistoryError') {
|
||||||
//console.log(e)
|
//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) => {
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user