Browse Source

progress callback for treeWrite

master
nitowa 1 month ago
parent
commit
5277fbd703
2 changed files with 27 additions and 11 deletions
  1. 2
    2
      package-lock.json
  2. 25
    9
      src/xrpIO/xrpl-binding.ts

+ 2
- 2
package-lock.json View File

1
 {
1
 {
2
   "name": "xrpio",
2
   "name": "xrpio",
3
-  "version": "0.2.3",
3
+  "version": "0.3.0",
4
   "lockfileVersion": 2,
4
   "lockfileVersion": 2,
5
   "requires": true,
5
   "requires": true,
6
   "packages": {
6
   "packages": {
7
     "": {
7
     "": {
8
       "name": "xrpio",
8
       "name": "xrpio",
9
-      "version": "0.2.3",
9
+      "version": "0.3.0",
10
       "license": "MIT",
10
       "license": "MIT",
11
       "dependencies": {
11
       "dependencies": {
12
         "axios": "^1.7.7",
12
         "axios": "^1.7.7",

+ 25
- 9
src/xrpIO/xrpl-binding.ts View File

75
     this.dbg("Sending payment", wallet.address, '->', to)
75
     this.dbg("Sending payment", wallet.address, '->', to)
76
 
76
 
77
     const _api = await this.cloneApi()
77
     const _api = await this.cloneApi()
78
+    
78
     const payment: Payment = await _api.autofill({
79
     const payment: Payment = await _api.autofill({
79
       TransactionType: 'Payment',
80
       TransactionType: 'Payment',
80
       Account: wallet.address,
81
       Account: wallet.address,
84
       Memos: [{
85
       Memos: [{
85
         Memo: {
86
         Memo: {
86
           MemoData: hexEncode(data.data || ""),
87
           MemoData: hexEncode(data.data || ""),
87
-          MemoFormat: hexEncode(data.format || ""),
88
+          MemoFormat: hexEncode(data.format || "0"),
88
           MemoType: hexEncode(data.type || "")
89
           MemoType: hexEncode(data.type || "")
89
         }
90
         }
90
       }]
91
       }]
188
     }
189
     }
189
     const tx = await this.getTransaction(hash)
190
     const tx = await this.getTransaction(hash)
190
 
191
 
191
-    
192
-
193
     if(verifyOwner && tx.result.tx_json.Account != verifyOwner){
192
     if(verifyOwner && tx.result.tx_json.Account != verifyOwner){
194
       throw new CannotVerifyOwnerError(hash, tx.result.tx_json.Account, verifyOwner)
193
       throw new CannotVerifyOwnerError(hash, tx.result.tx_json.Account, verifyOwner)
195
     }
194
     }
204
     return memoParsed
203
     return memoParsed
205
   }
204
   }
206
 
205
 
207
-  public async treeWrite(data: string, to: string, secret: string, format: 'L' | 'N' = 'L'): Promise<string> {
206
+  public async treeWrite(data: string, to: string, secret: string, format: string = "0"): Promise<string> {
208
     const wallet = Wallet.fromSecret(secret)
207
     const wallet = Wallet.fromSecret(secret)
209
     data = await compressB64(data)
208
     data = await compressB64(data)
210
     const chunks = chunkString(data, PAYLOAD_SIZE)
209
     const chunks = chunkString(data, PAYLOAD_SIZE)
215
       return hashes[0]
214
       return hashes[0]
216
     }
215
     }
217
 
216
 
218
-    return await this.treeWrite(JSON.stringify(hashes), to, secret, 'N')
217
+    return await this.treeWrite(JSON.stringify(hashes), to, secret, `${hashes.length}`)
219
   }
218
   }
220
 
219
 
221
-  public async treeRead(hashes: string[], verifyOwner?:string): Promise<string> {
220
+  private maxMemos = 0
221
+
222
+  public async treeRead(hashes: string[], verifyOwner?:string, progressCallback:(max:number,finished:number)=>void = ()=>{}): Promise<string> {
222
     const bad_hash = hashes.find(hash => !NON_ZERO_TX_HASH.test(hash))
223
     const bad_hash = hashes.find(hash => !NON_ZERO_TX_HASH.test(hash))
223
     if (bad_hash)
224
     if (bad_hash)
224
       throw new BadTxHashError(bad_hash)
225
       throw new BadTxHashError(bad_hash)
225
 
226
 
226
-    const memos = await Promise.all(hashes.map(hash => this.readRaw(hash, verifyOwner)))
227
+    let count = 0;
228
+
229
+    const memos = await Promise.all(hashes.map(async hash => {
230
+      const memo = await this.readRaw(hash, verifyOwner)
231
+      if(String(memo.format) === '0'){
232
+        count += 1;
233
+        progressCallback(count, this.maxMemos)
234
+        if(count === this.maxMemos){
235
+          this.maxMemos = 0;
236
+        }
237
+      }else{
238
+        this.maxMemos = Math.max(this.maxMemos, Number(memo.format))
239
+        progressCallback(-1, this.maxMemos)
240
+      }
241
+      return memo
242
+    }))
227
     const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
243
     const payload: string = await decompressB64(memos.map(memo => memo.data).join(''))
228
 
244
 
229
-    if (memos.some(memo => memo.format === 'N')) {
230
-      return await this.treeRead(JSON.parse(payload), verifyOwner)
245
+    if (memos.some(memo => memo.format !== '0')) {
246
+      return await this.treeRead(JSON.parse(payload), verifyOwner, progressCallback)
231
     }
247
     }
232
 
248
 
233
     return payload
249
     return payload

Loading…
Cancel
Save