diff --git a/package.json b/package.json index 06a1180..7b54f85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xrpio", - "version": "0.0.9", + "version": "0.0.10", "repository": { "type": "git", "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git" diff --git a/src/xrpIO/ripple-binding.ts b/src/xrpIO/ripple-binding.ts index 3c73132..675946c 100644 --- a/src/xrpIO/ripple-binding.ts +++ b/src/xrpIO/ripple-binding.ts @@ -12,12 +12,12 @@ const PAYLOAD_SIZE = 925 const debug = false const cloneApi = async (api: RippleAPI): Promise => { - 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 => { } export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise => { - 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 { - 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 => { - 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 => { +export const treeWrite = async (api: RippleAPI, data: string, wallet: Wallet, to: string, format: 'L' | 'N' = 'L'): Promise => { data = await compressB64(data) const chunks = chunkString(data, PAYLOAD_SIZE) const latestSequence = await getLatestSequence(api, wallet.address)