Skip to content

Commit 263ee1b

Browse files
solved issue #10
1 parent 099eb25 commit 263ee1b

File tree

5 files changed

+390
-10
lines changed

5 files changed

+390
-10
lines changed

src/ninsight.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,76 @@ class Ninsight {
314314
else throw error
315315
}
316316
}
317+
/**
318+
* @api Ninsight.details() details()
319+
* @apiName Ninsight Details
320+
* @apiGroup Ninsight
321+
* @apiDescription Return details of address.
322+
*
323+
* @apiExample Example usage:
324+
* (async () => {
325+
* try {
326+
* let details = await bchjs.Ninsight.details('bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c');
327+
* let details = await bchjs.Ninsight.details(['bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c','bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c']);
328+
* console.log(details);
329+
* } catch(error) {
330+
* console.error(error)
331+
* }
332+
* })()
333+
*
334+
* // [
335+
* // {
336+
* //"balance": 0.00001,
337+
* //"balanceSat": 1000,
338+
* //"totalReceived": 0.00001,
339+
* //"totalReceivedSat": 1000,
340+
* //"totalSent": 0,
341+
* //"totalSentSat": 0,
342+
* //"unconfirmedBalance": 0,
343+
* //"unconfirmedBalanceSat": 0,
344+
* //"unconfirmedTxApperances": 0,
345+
* //"txApperances": 1,
346+
* //"transactions": [
347+
* //"5f09d317e24c5d376f737a2711f3bd1d381abdb41743fff3819b4f76382e1eac" ],
348+
* //"legacyAddress": "1FZrK8HohEKyKCTM24NkAzPnX9WD9Ujhw7",
349+
* //"cashAddress": "bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr",
350+
* //"slpAddress": "simpleledger:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ylynt40sa",
351+
* //"currentPage": 0,
352+
* //"pagesTotal": 1
353+
* // }
354+
* // ]
355+
*
356+
*/
357+
async details(address) {
358+
try {
359+
_this._validateParam(address)
360+
return _this._callAxios(address, 'details')
361+
} catch (e) {
362+
_this._handleError(e)
363+
}
364+
}
365+
366+
_validateParam(address) {
367+
if (typeof address !== "string" && !Array.isArray(address))
368+
throw new Error(`Input address must be a string or array of strings.`)
369+
}
370+
371+
_handleError(error) {
372+
if (error.response && error.response.data) throw error.response.data
373+
else throw error
374+
}
375+
376+
async _callAxios(address, type) {
377+
const response = await axios.post(
378+
`${_this.ninsightURL}/address/${type}`,
379+
{
380+
addresses: Array.isArray(address) ? address : [address]
381+
},
382+
_this.axiosOptions
383+
)
384+
//console.log(`SAMPLE: ${response.data}`);
385+
return response.data
386+
}
317387
}
318388

319389
module.exports = Ninsight

test/integration/bchn/ninsight.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,89 @@ describe(`#Ninsight`, () => {
126126
assert.property(result[0], "vout")
127127
})
128128
})
129+
130+
describe(`#detailsAddress`, () => {
131+
it(`should throw an error for improper input`, async () => {
132+
try {
133+
const addr = 12345
134+
135+
await bchjs.Ninsight.details(addr)
136+
assert.equal(true, false, "Unexpected result!")
137+
} catch (err) {
138+
//console.log(`err: `, err)
139+
assert.include(
140+
err.message,
141+
`Input address must be a string or array of strings.`
142+
)
143+
}
144+
})
145+
146+
it(`should GET details for a single address`, async () => {
147+
const addr = "bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7"
148+
149+
const result = await bchjs.Ninsight.details(addr)
150+
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
151+
assert.property(result[0], "balance")
152+
assert.property(result[0], "balanceSat")
153+
assert.property(result[0], "totalReceived")
154+
assert.property(result[0], "totalReceivedSat")
155+
assert.property(result[0], "totalSent")
156+
assert.property(result[0], "totalSentSat")
157+
assert.property(result[0], "unconfirmedBalance")
158+
assert.property(result[0], "unconfirmedBalanceSat")
159+
assert.property(result[0], "unconfirmedTxApperances")
160+
assert.property(result[0], "txApperances")
161+
assert.property(result[0], "transactions")
162+
assert.property(result[0], "legacyAddress")
163+
assert.property(result[0], "cashAddress")
164+
assert.property(result[0], "slpAddress")
165+
assert.property(result[0], "currentPage")
166+
assert.property(result[0], "pagesTotal")
167+
})
168+
169+
it(`should GET details for an array of addresses`, async () => {
170+
const addr = [
171+
"bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7",
172+
"bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr"
173+
]
174+
175+
const result = await bchjs.Ninsight.details(addr)
176+
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
177+
178+
assert.isArray(result)
179+
assert.property(result[0], "balance")
180+
assert.property(result[0], "balanceSat")
181+
assert.property(result[0], "totalReceived")
182+
assert.property(result[0], "totalReceivedSat")
183+
assert.property(result[0], "totalSent")
184+
assert.property(result[0], "totalSentSat")
185+
assert.property(result[0], "unconfirmedBalance")
186+
assert.property(result[0], "unconfirmedBalanceSat")
187+
assert.property(result[0], "unconfirmedTxApperances")
188+
assert.property(result[0], "txApperances")
189+
assert.property(result[0], "transactions")
190+
assert.property(result[0], "legacyAddress")
191+
assert.property(result[1], "cashAddress")
192+
assert.property(result[1], "slpAddress")
193+
assert.property(result[1], "currentPage")
194+
assert.property(result[1], "pagesTotal")
195+
196+
/*
197+
If in any way possible, I would like to refactor this test
198+
according to the DRY principle to share it with the integration tests too
199+
Would that make sense or does that implicate anything unwanted?
200+
201+
assertDetails(result[0])
202+
203+
assertDetails(data) {
204+
assert.property(data, "balance")
205+
assert.property(data, "balanceSat")
206+
assert.property(data, "totalReceived")
207+
assert.property(data, "totalReceivedSat")
208+
assert.property(data, "totalSent")
209+
assert.property(data, "totalSentSat")
210+
}
211+
*/
212+
})
213+
})
129214
})

test/integration/ninsight.js

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const assert = chai.assert
33
const sinon = require("sinon")
44

55
const BCHJS = require("../../src/bch-js")
6-
const bchjs = new BCHJS({ ninsightURL: "https://rest.bitcoin.com/v2" })
6+
const bchjs = new BCHJS({
7+
ninsightURL: "https://rest.bitcoin.com/v2"
8+
})
79

810
describe(`#Ninsight`, () => {
911
let sandbox
@@ -131,6 +133,90 @@ describe(`#Ninsight`, () => {
131133
assert.property(result[0], "vout")
132134
})
133135
})
136+
describe(`#detailsAddress`, () => {
137+
it(`should throw an error for improper input`, async () => {
138+
try {
139+
const addr = 12345
140+
141+
await bchjs.Ninsight.details(addr)
142+
assert.equal(true, false, "Unexpected result!")
143+
} catch (err) {
144+
//console.log(`err: `, err)
145+
assert.include(
146+
err.message,
147+
`Input address must be a string or array of strings.`
148+
)
149+
}
150+
})
151+
152+
it(`should GET details for a single address`, async () => {
153+
const addr = "bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7"
154+
155+
const result = await bchjs.Ninsight.details(addr)
156+
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
157+
assert.property(result[0], "balance")
158+
assert.property(result[0], "balanceSat")
159+
assert.property(result[0], "totalReceived")
160+
assert.property(result[0], "totalReceivedSat")
161+
assert.property(result[0], "totalSent")
162+
assert.property(result[0], "totalSentSat")
163+
assert.property(result[0], "unconfirmedBalance")
164+
assert.property(result[0], "unconfirmedBalanceSat")
165+
assert.property(result[0], "unconfirmedTxApperances")
166+
assert.property(result[0], "txApperances")
167+
assert.property(result[0], "transactions")
168+
assert.property(result[0], "legacyAddress")
169+
assert.property(result[0], "cashAddress")
170+
assert.property(result[0], "slpAddress")
171+
assert.property(result[0], "currentPage")
172+
assert.property(result[0], "pagesTotal")
173+
})
174+
175+
it(`should GET details for an array of addresses`, async () => {
176+
const addr = [
177+
"bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7",
178+
"bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr"
179+
]
180+
181+
const result = await bchjs.Ninsight.details(addr)
182+
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
183+
184+
assert.isArray(result)
185+
assert.property(result[0], "balance")
186+
assert.property(result[0], "balanceSat")
187+
assert.property(result[0], "totalReceived")
188+
assert.property(result[0], "totalReceivedSat")
189+
assert.property(result[0], "totalSent")
190+
assert.property(result[0], "totalSentSat")
191+
assert.property(result[0], "unconfirmedBalance")
192+
assert.property(result[0], "unconfirmedBalanceSat")
193+
assert.property(result[0], "unconfirmedTxApperances")
194+
assert.property(result[0], "txApperances")
195+
assert.property(result[0], "transactions")
196+
assert.property(result[0], "legacyAddress")
197+
assert.property(result[1], "cashAddress")
198+
assert.property(result[1], "slpAddress")
199+
assert.property(result[1], "currentPage")
200+
assert.property(result[1], "pagesTotal")
201+
202+
/*
203+
If in any way possible, I would like to refactor this test
204+
according to the DRY principle to share it with the integration tests too
205+
Would that make sense or does that implicate anything unwanted?
206+
207+
assertDetails(result[0])
208+
209+
assertDetails(data) {
210+
assert.property(data, "balance")
211+
assert.property(data, "balanceSat")
212+
assert.property(data, "totalReceived")
213+
assert.property(data, "totalReceivedSat")
214+
assert.property(data, "totalSent")
215+
assert.property(data, "totalSentSat")
216+
}
217+
*/
218+
})
219+
})
134220
})
135221

136222
function sleep(ms) {

test/unit/fixtures/ninsight-mock.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ const details = {
158158

159159
const detailsPost = [details, details]
160160

161+
const addrDetail =
162+
{
163+
"balance": 0.00001,
164+
"balanceSat": 1000,
165+
"totalReceived": 0.00001,
166+
"totalReceivedSat": 1000,
167+
"totalSent": 0,
168+
"totalSentSat": 0,
169+
"unconfirmedBalance": 0,
170+
"unconfirmedBalanceSat": 0,
171+
"unconfirmedTxApperances": 0,
172+
"txApperances": 1,
173+
"transactions": [
174+
"5f09d317e24c5d376f737a2711f3bd1d381abdb41743fff3819b4f76382e1eac"
175+
],
176+
"legacyAddress": "1FZrK8HohEKyKCTM24NkAzPnX9WD9Ujhw7",
177+
"cashAddress": "bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr",
178+
"slpAddress": "simpleledger:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ylynt40sa",
179+
"currentPage": 0,
180+
"pagesTotal": 1
181+
}
182+
183+
const addrDetailArray = [addrDetail, addrDetail]
184+
161185
module.exports = {
162186
utxo,
163187
utxoPost,
@@ -166,5 +190,7 @@ module.exports = {
166190
transactions,
167191
transactionsPost,
168192
details,
169-
detailsPost
193+
detailsPost,
194+
addrDetail,
195+
addrDetailArray
170196
}

0 commit comments

Comments
 (0)