Browse Source

fix readRaw disconnecting

master
nitowa 2 years ago
parent
commit
ca8498707b
2 changed files with 20 additions and 24 deletions
  1. 1
    1
      package.json
  2. 19
    23
      src/xrpIO/ripple-binding.ts

+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "xrpio",
3
-  "version": "0.0.9",
3
+  "version": "0.0.10",
4 4
   "repository": {
5 5
     "type": "git",
6 6
     "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"

+ 19
- 23
src/xrpIO/ripple-binding.ts View File

@@ -12,12 +12,12 @@ const PAYLOAD_SIZE = 925
12 12
 const debug = false
13 13
 
14 14
 const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
15
-  try{
15
+  try {
16 16
     const subApi = new RippleAPI({ server: api.connection['_url'] })
17 17
     await subApi.connect()
18 18
     return subApi
19
-  }catch(e){
20
-    if(debug){
19
+  } catch (e) {
20
+    if (debug) {
21 21
       console.log("CLONEAPI ERR", e)
22 22
     }
23 23
     return await cloneApi(api)
@@ -25,9 +25,9 @@ const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
25 25
 }
26 26
 
27 27
 export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise<number> => {
28
-  if(debug) console.log("Getting acc info for", accountAddress)
28
+  if (debug) console.log("Getting acc info for", accountAddress)
29 29
   const accountInfo = await api.getAccountInfo(accountAddress, {})
30
-  return Number(accountInfo.sequence-1)
30
+  return Number(accountInfo.sequence - 1)
31 31
 }
32 32
 
33 33
 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
41 41
         minLedgerVersion: 25235454
42 42
       })
43 43
     } catch (e) {
44
-      
44
+
45 45
       // Typical error when the tx hasn't been validated yet:
46 46
       if ((e as Error).name !== 'MissingLedgerHistoryError') {
47 47
         //console.log(e)
@@ -69,7 +69,7 @@ const sendReliably = (api: RippleAPI, signed: any, preparedPayment,): Promise<an
69 69
 })
70 70
 
71 71
 export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to: string, secret: string, sequence: number) => {
72
-  if(debug) console.log("Sending payment with seq", sequence)
72
+  if (debug) console.log("Sending payment with seq", sequence)
73 73
   const options: Instructions = {
74 74
     maxLedgerVersionOffset: 5,
75 75
     fee: MIN_XRP_FEE,
@@ -103,10 +103,10 @@ export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to
103 103
 
104 104
     return txHash
105 105
   } catch (error) {
106
-    if(debug)
106
+    if (debug)
107 107
       console.log("SENDPAYMENT ERROR", error)
108 108
     throw error
109
-  }finally{
109
+  } finally {
110 110
     _api.disconnect()
111 111
   }
112 112
 }
@@ -132,7 +132,7 @@ export const writeRaw = async (api: RippleAPI, data: Memo, from:
132 132
     const resp = await sendPayment(api, [data], from, to, secret, sequence)
133 133
     return resp['tx_json'].hash
134 134
   } catch (error) {
135
-    if(debug){
135
+    if (debug) {
136 136
       console.log("WRITERAW ERR", error);
137 137
     }
138 138
     throw error
@@ -141,23 +141,19 @@ export const writeRaw = async (api: RippleAPI, data: Memo, from:
141 141
 
142 142
 
143 143
 export const readRaw = async (api: RippleAPI, hash: string): Promise<Memo> => {
144
-  while(!api.isConnected()){
145
-    try{
146
-      await api.connect()
147
-    }catch(e){
148
-      //retry
149
-    }
150
-  }
144
+  api = await cloneApi(api)
151 145
   let tx
152
-  try{
146
+  try {
153 147
     tx = await api.getTransaction(hash, {
154 148
       minLedgerVersion: 25235454
155 149
     })
156
-  }catch(e){
157
-//    if(debug){
158
-      console.log("READRAW ERR", e)
159
-//    }
150
+  } catch (e) {
151
+    //    if(debug){
152
+    console.log("READRAW ERR", e)
153
+    //    }
160 154
     throw e
155
+  } finally {
156
+    await api.disconnect()
161 157
   }
162 158
 
163 159
   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)
175 171
   })
176 172
 }
177 173
 
178
-export const treeWrite = async (api: RippleAPI, data: string, wallet: Wallet, to: string, format: 'L'|'N' = 'L'): Promise<string> => {
174
+export const treeWrite = async (api: RippleAPI, data: string, wallet: Wallet, to: string, format: 'L' | 'N' = 'L'): Promise<string> => {
179 175
   data = await compressB64(data)
180 176
   const chunks = chunkString(data, PAYLOAD_SIZE)
181 177
   const latestSequence = await getLatestSequence(api, wallet.address)

Loading…
Cancel
Save