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
 {
1
 {
2
   "name": "xrpio",
2
   "name": "xrpio",
3
-  "version": "0.0.9",
3
+  "version": "0.0.10",
4
   "repository": {
4
   "repository": {
5
     "type": "git",
5
     "type": "git",
6
     "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"
6
     "url": "https://gitea.nitowa.xyz/npm-packages/xrpio.git"

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

12
 const debug = false
12
 const debug = false
13
 
13
 
14
 const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
14
 const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
15
-  try{
15
+  try {
16
     const subApi = new RippleAPI({ server: api.connection['_url'] })
16
     const subApi = new RippleAPI({ server: api.connection['_url'] })
17
     await subApi.connect()
17
     await subApi.connect()
18
     return subApi
18
     return subApi
19
-  }catch(e){
20
-    if(debug){
19
+  } catch (e) {
20
+    if (debug) {
21
       console.log("CLONEAPI ERR", e)
21
       console.log("CLONEAPI ERR", e)
22
     }
22
     }
23
     return await cloneApi(api)
23
     return await cloneApi(api)
25
 }
25
 }
26
 
26
 
27
 export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise<number> => {
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
   const accountInfo = await api.getAccountInfo(accountAddress, {})
29
   const accountInfo = await api.getAccountInfo(accountAddress, {})
30
-  return Number(accountInfo.sequence-1)
30
+  return Number(accountInfo.sequence - 1)
31
 }
31
 }
32
 
32
 
33
 const compressB64 = async (data: string) => (await util.promisify(zlib.deflate)(Buffer.from(data, 'utf-8'))).toString('base64')
33
 const compressB64 = async (data: string) => (await util.promisify(zlib.deflate)(Buffer.from(data, 'utf-8'))).toString('base64')
41
         minLedgerVersion: 25235454
41
         minLedgerVersion: 25235454
42
       })
42
       })
43
     } catch (e) {
43
     } catch (e) {
44
-      
44
+
45
       // Typical error when the tx hasn't been validated yet:
45
       // Typical error when the tx hasn't been validated yet:
46
       if ((e as Error).name !== 'MissingLedgerHistoryError') {
46
       if ((e as Error).name !== 'MissingLedgerHistoryError') {
47
         //console.log(e)
47
         //console.log(e)
69
 })
69
 })
70
 
70
 
71
 export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to: string, secret: string, sequence: number) => {
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
   const options: Instructions = {
73
   const options: Instructions = {
74
     maxLedgerVersionOffset: 5,
74
     maxLedgerVersionOffset: 5,
75
     fee: MIN_XRP_FEE,
75
     fee: MIN_XRP_FEE,
103
 
103
 
104
     return txHash
104
     return txHash
105
   } catch (error) {
105
   } catch (error) {
106
-    if(debug)
106
+    if (debug)
107
       console.log("SENDPAYMENT ERROR", error)
107
       console.log("SENDPAYMENT ERROR", error)
108
     throw error
108
     throw error
109
-  }finally{
109
+  } finally {
110
     _api.disconnect()
110
     _api.disconnect()
111
   }
111
   }
112
 }
112
 }
132
     const resp = await sendPayment(api, [data], from, to, secret, sequence)
132
     const resp = await sendPayment(api, [data], from, to, secret, sequence)
133
     return resp['tx_json'].hash
133
     return resp['tx_json'].hash
134
   } catch (error) {
134
   } catch (error) {
135
-    if(debug){
135
+    if (debug) {
136
       console.log("WRITERAW ERR", error);
136
       console.log("WRITERAW ERR", error);
137
     }
137
     }
138
     throw error
138
     throw error
141
 
141
 
142
 
142
 
143
 export const readRaw = async (api: RippleAPI, hash: string): Promise<Memo> => {
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
   let tx
145
   let tx
152
-  try{
146
+  try {
153
     tx = await api.getTransaction(hash, {
147
     tx = await api.getTransaction(hash, {
154
       minLedgerVersion: 25235454
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
     throw e
154
     throw e
155
+  } finally {
156
+    await api.disconnect()
161
   }
157
   }
162
 
158
 
163
   if (!tx || !tx.specification || !tx.specification['memos'] || !tx.specification['memos'][0]) {
159
   if (!tx || !tx.specification || !tx.specification['memos'] || !tx.specification['memos'][0]) {
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
   data = await compressB64(data)
175
   data = await compressB64(data)
180
   const chunks = chunkString(data, PAYLOAD_SIZE)
176
   const chunks = chunkString(data, PAYLOAD_SIZE)
181
   const latestSequence = await getLatestSequence(api, wallet.address)
177
   const latestSequence = await getLatestSequence(api, wallet.address)

Loading…
Cancel
Save