@psf/bch-js 5.2.3 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/electrumx.js +15 -2
- package/src/util.js +50 -0
package/package.json
CHANGED
package/src/electrumx.js
CHANGED
|
@@ -638,13 +638,24 @@ class ElectrumX {
|
|
|
638
638
|
// Sort confirmed Transactions by the block height
|
|
639
639
|
sortConfTxs (txs, sortingOrder = 'DESCENDING') {
|
|
640
640
|
try {
|
|
641
|
+
// console.log(`sortConfTxs txs: ${JSON.stringify(txs, null, 2)}`)
|
|
642
|
+
|
|
641
643
|
// Filter out unconfirmed transactions, with a height of 0 or less.
|
|
642
644
|
txs = txs.filter(elem => elem.height > 0)
|
|
643
645
|
|
|
644
646
|
if (sortingOrder === 'DESCENDING') {
|
|
645
|
-
|
|
647
|
+
// console.log('Sorting in descending order')
|
|
648
|
+
return txs.sort((a, b) => {
|
|
649
|
+
// console.log(`descending b.height: ${b.height}, a.height: ${a.height}`)
|
|
650
|
+
return b.height - a.height
|
|
651
|
+
})
|
|
646
652
|
}
|
|
647
|
-
|
|
653
|
+
|
|
654
|
+
// console.log('Sorting in ascending order')
|
|
655
|
+
return txs.sort((a, b) => {
|
|
656
|
+
// console.log(`ascending b.height: ${b.height}, a.height: ${a.height}`)
|
|
657
|
+
return a.height - b.height
|
|
658
|
+
})
|
|
648
659
|
} catch (err) {
|
|
649
660
|
console.log('Error in util.js/sortConfTxs()')
|
|
650
661
|
throw err
|
|
@@ -685,6 +696,8 @@ class ElectrumX {
|
|
|
685
696
|
// Substitute zero-conf txs with the current block-height + 1
|
|
686
697
|
async sortAllTxs (txs, sortingOrder = 'DESCENDING') {
|
|
687
698
|
try {
|
|
699
|
+
// console.log(`sortingOrder: ${sortingOrder}`)
|
|
700
|
+
|
|
688
701
|
// Calculate the height of the next block
|
|
689
702
|
const nextBlock = (await this.blockchain.getBlockCount()) + 1
|
|
690
703
|
|
package/src/util.js
CHANGED
|
@@ -150,6 +150,56 @@ class Util {
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
/**
|
|
154
|
+
* @api Util.chunk100() chunk100()
|
|
155
|
+
* @apiName chunk100
|
|
156
|
+
* @apiGroup Util
|
|
157
|
+
* @apiDescription chunk up an array into multiple arrays of 100 elements each.
|
|
158
|
+
* Input: arrayToSlice - a one-dimensional array of elements.
|
|
159
|
+
* Returns a two-dimensional array. An array of 100-element arrays.
|
|
160
|
+
*
|
|
161
|
+
* @apiExample Example usage:
|
|
162
|
+
* (async () => {
|
|
163
|
+
* try {
|
|
164
|
+
* const bigArray = [0,1,2,3,4,5,6,7,8,9,10,...,148, 149, 150]
|
|
165
|
+
*
|
|
166
|
+
* const chunked = bchjs.Util.chunk20(bigArray)
|
|
167
|
+
* console.log(chunked)
|
|
168
|
+
* } catch(error) {
|
|
169
|
+
* console.error(error)
|
|
170
|
+
* }
|
|
171
|
+
* })()
|
|
172
|
+
*
|
|
173
|
+
* // returns
|
|
174
|
+
* [
|
|
175
|
+
* [0,1,2,3,4,5,6,7,8,9,10,11,...,98,99],
|
|
176
|
+
* [100,101,102,...,148,149,150]
|
|
177
|
+
* ]
|
|
178
|
+
*/
|
|
179
|
+
chunk100 (arrayToSlice) {
|
|
180
|
+
try {
|
|
181
|
+
// Validate inputs
|
|
182
|
+
if (!Array.isArray(arrayToSlice)) {
|
|
183
|
+
throw new Error('input must be an array')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let offset = 0
|
|
187
|
+
const result = []
|
|
188
|
+
|
|
189
|
+
// Loop over the array and slice off chunks of 100 elements.
|
|
190
|
+
while (offset < arrayToSlice.length) {
|
|
191
|
+
const chunk = arrayToSlice.slice(offset, offset + 100)
|
|
192
|
+
result.push(chunk)
|
|
193
|
+
offset = offset + 100
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return result
|
|
197
|
+
} catch (err) {
|
|
198
|
+
console.error('Error in chunk100()')
|
|
199
|
+
throw err
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
153
203
|
/**
|
|
154
204
|
* @api Util.sleep() sleep()
|
|
155
205
|
* @apiName sleep
|