@psf/bch-js 4.20.27 → 4.22.1

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.
@@ -0,0 +1,56 @@
1
+ /*
2
+ Unit tests for eCash library.
3
+ */
4
+
5
+ // Global npm libraries
6
+ const assert = require('chai').assert
7
+ const Ecash = require('../../src/ecash')
8
+ const uut = new Ecash()
9
+
10
+ describe('#eCash', () => {
11
+ describe('#toSatoshi', () => {
12
+ it('should convert XEC to satoshis', () => {
13
+ const xec = 10704.35
14
+
15
+ const result = uut.toSatoshi(xec)
16
+
17
+ assert.equal(result, 1070435)
18
+ })
19
+
20
+ it('should throw an error if input is not a number', () => {
21
+ try {
22
+ uut.toSatoshi('test')
23
+
24
+ assert.fail('Unexpected code path')
25
+ } catch (err) {
26
+ assert.equal(
27
+ err.message,
28
+ 'input must be a floating number representing XEC'
29
+ )
30
+ }
31
+ })
32
+ })
33
+
34
+ describe('#toXec', () => {
35
+ it('should convert satoshis to XEC', () => {
36
+ const sats = 1070435
37
+
38
+ const result = uut.toXec(sats)
39
+
40
+ assert.equal(result, 10704.35)
41
+ })
42
+
43
+ it('should throw an error if input is not a number', () => {
44
+ try {
45
+ uut.toXec('test')
46
+
47
+ assert.fail('Unexpected code path')
48
+ } catch (err) {
49
+ assert.equal(
50
+ err.message,
51
+ 'input must be a floating number representing satoshis'
52
+ )
53
+ }
54
+ })
55
+ })
56
+ })
@@ -0,0 +1,130 @@
1
+ /*
2
+ Mocking data for unit tests for the PSF SLP INDEXER library.
3
+ */
4
+
5
+ 'use strict'
6
+
7
+ const tokenStats = {
8
+ tokenData: {
9
+ type: 1,
10
+ ticker: 'TP03',
11
+ name: 'Test Plugin 03',
12
+ tokenId: '13cad617d523c8eb4ab11fff19c010e0e0a4ea4360b58e0c8c955a45a146a669',
13
+ documentUri: 'fullstack.cash',
14
+ documentHash: '',
15
+ decimals: 0,
16
+ mintBatonIsActive: false,
17
+ tokensInCirculationBN: '1',
18
+ tokensInCirculationStr: '1',
19
+ blockCreated: 722420,
20
+ totalBurned: '0',
21
+ totalMinted: '1',
22
+ txs: [
23
+ {
24
+ txid: '13cad617d523c8eb4ab11fff19c010e0e0a4ea4360b58e0c8c955a45a146a669',
25
+ height: 722420,
26
+ type: 'GENESIS',
27
+ qty: '1'
28
+ }
29
+ ]
30
+ }
31
+ }
32
+
33
+ const txData = {
34
+ txData: {
35
+ txid: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
36
+ hash: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
37
+ version: 2,
38
+ size: 339,
39
+ locktime: 0,
40
+ vin: [
41
+ {
42
+ txid: '8370db30d94761ab9a11b71ecd22541151bf6125c8c613f0f6fab8ab794565a7',
43
+ vout: 0,
44
+ scriptSig: {
45
+ asm: '304402207e9631c53dfc8a9a793d1916469628c6b7c5780c01c2f676d51ef21b0ba4926f022069feb471ec869a49f8d108d0aaba04e7cd36e60a7500109d86537f55698930d4[ALL|FORKID] 02791b19a39165dbd83403d6df268d44fd621da30581b0b6e5cb15a7101ed58851',
46
+ hex: '47304402207e9631c53dfc8a9a793d1916469628c6b7c5780c01c2f676d51ef21b0ba4926f022069feb471ec869a49f8d108d0aaba04e7cd36e60a7500109d86537f55698930d4412102791b19a39165dbd83403d6df268d44fd621da30581b0b6e5cb15a7101ed58851'
47
+ },
48
+ sequence: 4294967295,
49
+ address: 'bitcoincash:qpvsg9vl9a5mlf37a7n3yce6pktdctn73qwgaqm3wq',
50
+ value: 0.00051303,
51
+ tokenQty: 0,
52
+ tokenQtyStr: '0',
53
+ tokenId: null
54
+ }
55
+ ],
56
+ vout: [
57
+ {
58
+ value: 0,
59
+ n: 0,
60
+ scriptPubKey: {
61
+ asm: 'OP_RETURN 5262419 1 47454e45534953 54524f5554 54726f75742773207465737420746f6b656e 74726f757473626c6f672e636f6d 0 2 2 000000174876e800',
62
+ hex: '6a04534c500001010747454e455349530554524f55541254726f75742773207465737420746f6b656e0e74726f757473626c6f672e636f6d4c000102010208000000174876e800',
63
+ type: 'nulldata'
64
+ },
65
+ tokenQtyStr: '0',
66
+ tokenQty: 0
67
+ }
68
+ ],
69
+ hex: '0200000001a7654579abb8faf6f013c6c82561bf51115422cd1eb7119aab6147d930db7083000000006a47304402207e9631c53dfc8a9a793d1916469628c6b7c5780c01c2f676d51ef21b0ba4926f022069feb471ec869a49f8d108d0aaba04e7cd36e60a7500109d86537f55698930d4412102791b19a39165dbd83403d6df268d44fd621da30581b0b6e5cb15a7101ed58851ffffffff040000000000000000476a04534c500001010747454e455349530554524f55541254726f75742773207465737420746f6b656e0e74726f757473626c6f672e636f6d4c000102010208000000174876e80022020000000000001976a914db4d39ceb7794ffe5d06855f249e1d3a7f1b024088ac22020000000000001976a914db4d39ceb7794ffe5d06855f249e1d3a7f1b024088accec20000000000001976a9145904159f2f69bfa63eefa712633a0d96dc2e7e8888ac00000000',
70
+ blockhash: '0000000000000000009f65225a3e12e23a7ea057c869047e0f36563a1f410267',
71
+ confirmations: 97398,
72
+ time: 1581773131,
73
+ blocktime: 1581773131,
74
+ blockheight: 622414,
75
+ isSlpTx: true,
76
+ tokenTxType: 'GENESIS',
77
+ tokenId: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
78
+ tokenType: 1,
79
+ tokenTicker: 'TROUT',
80
+ tokenName: "Trout's test token",
81
+ tokenDecimals: 2,
82
+ tokenUri: 'troutsblog.com',
83
+ tokenDocHash: '',
84
+ isValidSlp: true
85
+ }
86
+ }
87
+ const balance = {
88
+ balance: {
89
+ utxos: [
90
+ {
91
+ txid: 'a24a6a4abf06fabd799ecea4f8fac6a9ff21e6a8dd6169a3c2ebc03665329db9',
92
+ vout: 1,
93
+ type: 'token',
94
+ qty: '1800',
95
+ tokenId: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
96
+ address: 'bitcoincash:qrqy3kj7r822ps6628vwqq5k8hyjl6ey3y4eea2m4s'
97
+ }
98
+ ],
99
+ txs: [
100
+ {
101
+ txid: '078b2c48ed1db0d5d5996f2889b8d847a49200d0a781f6aa6752f740f312688f',
102
+ height: 717796
103
+ },
104
+ {
105
+ txid: 'a24a6a4abf06fabd799ecea4f8fac6a9ff21e6a8dd6169a3c2ebc03665329db9',
106
+ height: 717832
107
+ }
108
+ ],
109
+ balances: [
110
+ {
111
+ tokenId: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
112
+ qty: '1800'
113
+ }
114
+ ]
115
+ }
116
+ }
117
+
118
+ const status = {
119
+ status: {
120
+ startBlockHeight: 543376,
121
+ syncedBlockHeight: 722860,
122
+ chainBlockHeight: 722679
123
+ }
124
+ }
125
+ module.exports = {
126
+ tokenStats,
127
+ txData,
128
+ balance,
129
+ status
130
+ }
@@ -0,0 +1,258 @@
1
+ const chai = require('chai')
2
+ const assert = chai.assert
3
+ const axios = require('axios')
4
+ const sinon = require('sinon')
5
+
6
+ const BCHJS = require('../../src/bch-js')
7
+ const bchjs = new BCHJS()
8
+
9
+ const mockData = require('./fixtures/psf-slp-indexer-mock')
10
+
11
+ describe('#PsfSlpIndexer', () => {
12
+ let sandbox
13
+ beforeEach(() => (sandbox = sinon.createSandbox()))
14
+ afterEach(() => sandbox.restore())
15
+
16
+ describe('#status', () => {
17
+ it('should GET status', async () => {
18
+ // Stub the network call.
19
+ sandbox.stub(axios, 'get').resolves({ data: mockData.status })
20
+
21
+ const result = await bchjs.PsfSlpIndexer.status()
22
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
23
+ assert.property(result, 'status')
24
+ assert.property(result.status, 'startBlockHeight')
25
+ assert.property(result.status, 'syncedBlockHeight')
26
+ assert.property(result.status, 'chainBlockHeight')
27
+ })
28
+
29
+ it('should handle axios error', async () => {
30
+ try {
31
+ // Stub the network call.
32
+ sandbox.stub(axios, 'get').throws(new Error('test error'))
33
+
34
+ await bchjs.PsfSlpIndexer.status()
35
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
36
+ assert.equal(true, false, 'Unexpected result!')
37
+ } catch (err) {
38
+ assert.include(err.message, 'test error')
39
+ }
40
+ })
41
+ it('should handle request error', async () => {
42
+ try {
43
+ // Stub the network call.
44
+ const testErr = new Error()
45
+ testErr.response = { data: { status: 422 } }
46
+ sandbox.stub(axios, 'get').throws(testErr)
47
+
48
+ await bchjs.PsfSlpIndexer.status()
49
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
50
+ assert.equal(true, false, 'Unexpected result!')
51
+ } catch (err) {
52
+ assert.equal(err.status, 422)
53
+ }
54
+ })
55
+ })
56
+
57
+ describe('#balance', () => {
58
+ it('should GET balance', async () => {
59
+ // Stub the network call.
60
+ sandbox.stub(axios, 'post').resolves({ data: mockData.balance })
61
+ const addr = 'bitcoincash:qzmd5vxgh9m22m6fgvm57yd6kjnjl9qnwywsf3583n'
62
+ const result = await bchjs.PsfSlpIndexer.balance(addr)
63
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
64
+ assert.property(result, 'balance')
65
+
66
+ assert.property(result.balance, 'utxos')
67
+ assert.property(result.balance, 'txs')
68
+ assert.property(result.balance, 'balances')
69
+ assert.isArray(result.balance.utxos)
70
+ assert.isArray(result.balance.txs)
71
+ assert.isArray(result.balance.balances)
72
+ })
73
+ it('should throw an error for improper input', async () => {
74
+ try {
75
+ const addr = 12345
76
+
77
+ await bchjs.PsfSlpIndexer.balance(addr)
78
+ assert.equal(true, false, 'Unexpected result!')
79
+ } catch (err) {
80
+ // console.log(`err: `, err)
81
+ assert.include(err.message, 'Input address must be a string.')
82
+ }
83
+ })
84
+ it('should handle axios error', async () => {
85
+ try {
86
+ // Stub the network call.
87
+ sandbox.stub(axios, 'post').throws(new Error('test error'))
88
+
89
+ const addr = 'bitcoincash:qzmd5vxgh9m22m6fgvm57yd6kjnjl9qnwywsf3583n'
90
+
91
+ await bchjs.PsfSlpIndexer.balance(addr)
92
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
93
+ assert.equal(true, false, 'Unexpected result!')
94
+ } catch (err) {
95
+ assert.include(err.message, 'test error')
96
+ }
97
+ })
98
+ it('should handle request error', async () => {
99
+ try {
100
+ // Stub the network call.
101
+ const testErr = new Error()
102
+ testErr.response = { data: { status: 422 } }
103
+ sandbox.stub(axios, 'post').throws(testErr)
104
+
105
+ const addr = 'bitcoincash:qzmd5vxgh9m22m6fgvm57yd6kjnjl9qnwywsf3583n'
106
+
107
+ await bchjs.PsfSlpIndexer.balance(addr)
108
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
109
+ assert.equal(true, false, 'Unexpected result!')
110
+ } catch (err) {
111
+ assert.equal(err.status, 422)
112
+ }
113
+ })
114
+ })
115
+
116
+ describe('#tokenStats', () => {
117
+ it('should GET token stats', async () => {
118
+ // Stub the network call.
119
+ sandbox.stub(axios, 'post').resolves({ data: mockData.tokenStats })
120
+
121
+ const tokenId =
122
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
123
+ const result = await bchjs.PsfSlpIndexer.tokenStats(tokenId)
124
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
125
+ assert.property(result, 'tokenData')
126
+ assert.property(result.tokenData, 'type')
127
+ assert.property(result.tokenData, 'ticker')
128
+ assert.property(result.tokenData, 'name')
129
+ assert.property(result.tokenData, 'tokenId')
130
+ assert.property(result.tokenData, 'documentUri')
131
+ assert.property(result.tokenData, 'documentHash')
132
+ assert.property(result.tokenData, 'decimals')
133
+ assert.property(result.tokenData, 'mintBatonIsActive')
134
+ assert.property(result.tokenData, 'tokensInCirculationBN')
135
+ assert.property(result.tokenData, 'tokensInCirculationStr')
136
+ assert.property(result.tokenData, 'blockCreated')
137
+ assert.property(result.tokenData, 'totalBurned')
138
+ assert.property(result.tokenData, 'totalMinted')
139
+ assert.property(result.tokenData, 'txs')
140
+ })
141
+ it('should throw an error for improper input', async () => {
142
+ try {
143
+ const tokenId = 12345
144
+
145
+ await bchjs.PsfSlpIndexer.tokenStats(tokenId)
146
+ assert.equal(true, false, 'Unexpected result!')
147
+ } catch (err) {
148
+ // console.log(`err: `, err)
149
+ assert.include(err.message, 'Input tokenId must be a string.')
150
+ }
151
+ })
152
+ it('should handle axios error', async () => {
153
+ try {
154
+ // Stub the network call.
155
+ sandbox.stub(axios, 'post').throws(new Error('test error'))
156
+
157
+ const tokenId =
158
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
159
+ await bchjs.PsfSlpIndexer.tokenStats(tokenId)
160
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
161
+ assert.equal(true, false, 'Unexpected result!')
162
+ } catch (err) {
163
+ assert.include(err.message, 'test error')
164
+ }
165
+ })
166
+ it('should handle request error', async () => {
167
+ try {
168
+ // Stub the network call.
169
+ const testErr = new Error()
170
+ testErr.response = { data: { status: 422 } }
171
+ sandbox.stub(axios, 'post').throws(testErr)
172
+
173
+ const tokenId =
174
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
175
+ await bchjs.PsfSlpIndexer.tokenStats(tokenId)
176
+ assert.equal(true, false, 'Unexpected result!')
177
+ } catch (err) {
178
+ assert.equal(err.status, 422)
179
+ }
180
+ })
181
+ })
182
+
183
+ describe('#tx', () => {
184
+ it('should GET transaction data', async () => {
185
+ // Stub the network call.
186
+ sandbox.stub(axios, 'post').resolves({ data: mockData.txData })
187
+
188
+ const txid =
189
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
190
+ const result = await bchjs.PsfSlpIndexer.tx(txid)
191
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
192
+ assert.property(result, 'txData')
193
+ assert.property(result.txData, 'txid')
194
+ assert.property(result.txData, 'hash')
195
+ assert.property(result.txData, 'version')
196
+ assert.property(result.txData, 'size')
197
+ assert.property(result.txData, 'locktime')
198
+ assert.property(result.txData, 'vin')
199
+ assert.property(result.txData, 'vout')
200
+ assert.property(result.txData, 'hex')
201
+ assert.property(result.txData, 'blockhash')
202
+ assert.property(result.txData, 'confirmations')
203
+ assert.property(result.txData, 'time')
204
+ assert.property(result.txData, 'blocktime')
205
+ assert.property(result.txData, 'blockheight')
206
+ assert.property(result.txData, 'isSlpTx')
207
+ assert.property(result.txData, 'tokenTxType')
208
+ assert.property(result.txData, 'tokenId')
209
+ assert.property(result.txData, 'tokenType')
210
+ assert.property(result.txData, 'tokenTicker')
211
+ assert.property(result.txData, 'tokenName')
212
+ assert.property(result.txData, 'tokenDecimals')
213
+ assert.property(result.txData, 'tokenUri')
214
+ assert.property(result.txData, 'tokenDocHash')
215
+ assert.property(result.txData, 'isValidSlp')
216
+ })
217
+ it('should throw an error for improper input', async () => {
218
+ try {
219
+ const txid = 12345
220
+
221
+ await bchjs.PsfSlpIndexer.tx(txid)
222
+ assert.equal(true, false, 'Unexpected result!')
223
+ } catch (err) {
224
+ // console.log(`err: `, err)
225
+ assert.include(err.message, 'Input txid must be a string.')
226
+ }
227
+ })
228
+ it('should handle axios error', async () => {
229
+ try {
230
+ // Stub the network call.
231
+ sandbox.stub(axios, 'post').throws(new Error('test error'))
232
+
233
+ const txid =
234
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
235
+ await bchjs.PsfSlpIndexer.tx(txid)
236
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
237
+ assert.equal(true, false, 'Unexpected result!')
238
+ } catch (err) {
239
+ assert.include(err.message, 'test error')
240
+ }
241
+ })
242
+ it('should handle request error', async () => {
243
+ try {
244
+ // Stub the network call.
245
+ const testErr = new Error()
246
+ testErr.response = { data: { status: 422 } }
247
+ sandbox.stub(axios, 'post').throws(testErr)
248
+
249
+ const txid =
250
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
251
+ await bchjs.PsfSlpIndexer.tx(txid)
252
+ assert.equal(true, false, 'Unexpected result!')
253
+ } catch (err) {
254
+ assert.equal(err.status, 422)
255
+ }
256
+ })
257
+ })
258
+ })