diff --git a/package.json b/package.json index 24d0813..11f5d39 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,8 @@ "test:integration:local:abc": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/", "test:integration:local:bchn": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/", "test:integration:local:testnet": "RESTURL=http://localhost:4000/v5/ mocha --timeout 30000 test/integration/chains/testnet", - "test:integration:decatur:bchn": "export RESTURL=http://192.168.0.36:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/", - "test:integration:decatur:abc": "export RESTURL=http://192.168.0.38:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/", + "test:integration:decatur:bchn": "export RESTURL=http://192.168.2.129:3000/v5/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/", + "test:integration:decatur:abc": "export RESTURL=http://192.168.2.141:3000/v5/ && mocha --timeout 30000 test/integration && mocha --timeout 30000 test/integration/chains/abc/", "test:integration:temp:bchn": "export RESTURL=http://157.90.174.219:3000/v5/ && mocha --timeout 30000 test/integration/", "test:temp": "export RESTURL=http://localhost:3000/v5/ && mocha --timeout 30000 -g '#Encryption' test/integration/", "test:temp2": "mocha --timeout=30000 -g '#TransactionLib' test/unit/", diff --git a/src/bch-js.js b/src/bch-js.js index 67da191..649aba4 100644 --- a/src/bch-js.js +++ b/src/bch-js.js @@ -33,6 +33,7 @@ const Encryption = require('./encryption') const Utxo = require('./utxo') const Transaction = require('./transaction') const DSProof = require('./dsproof') +const Ecash = require('./ecash') // Indexers const Ninsight = require('./ninsight') @@ -118,6 +119,7 @@ class BCHJS { this.Transaction = new Transaction(libConfig) this.DSProof = new DSProof(libConfig) + this.eCash = new Ecash() } } diff --git a/src/ecash.js b/src/ecash.js new file mode 100644 index 0000000..ecc812b --- /dev/null +++ b/src/ecash.js @@ -0,0 +1,47 @@ +/* + Utility library for converting units with eCash. +*/ + +class eCash { + /** + * @api eCash.toSatoshi() toSatoshi() + * @apiName toSatoshi + * @apiGroup eCash + * @apiDescription + * Convert XEC units into satoshi units + * + * @apiExample Example usage: + * // convert 10,704.35 XEC to satoshis: + * bchjs.eCash.toSatoshi(10704.35) + * // 1070435 + */ + toSatoshi (xec) { + if (typeof xec !== 'number') { + throw new Error('input must be a floating number representing XEC') + } + + return Math.floor(xec * 100) + } + + /** + * @api eCash.toXec() toXec() + * @apiName toXec + * @apiGroup eCash + * @apiDescription + * Convert satoshi units to XEC units + * + * @apiExample Example usage: + * // convert 1,070,435 satoshis to XEC: + * bchjs.eCash.toSatoshi(1070435) + * // 10704.35 + */ + toXec (sats) { + if (typeof sats !== 'number') { + throw new Error('input must be a floating number representing satoshis') + } + + return sats / 100 + } +} + +module.exports = eCash diff --git a/src/slp/address.js b/src/slp/address.js index 7163069..c8f6f20 100644 --- a/src/slp/address.js +++ b/src/slp/address.js @@ -36,7 +36,7 @@ class Address extends BCHJSAddress { * bchjs.SLP.Address.toSLPAddress('qzm47qz5ue99y9yl4aca7jnz7dwgdenl85jkfx3znl') * // simpleledger:qzm47qz5ue99y9yl4aca7jnz7dwgdenl857dzayzdp * - * // tesnet legacy + * // testnet legacy * bchjs.SLP.Address.toSLPAddress('msDbtTj7kWXPpYaR7PQmMK84i66fJqQMLx') * // slptest:qzq9je6pntpva3wf6scr7mlnycr54sjgeqauyclpwv * diff --git a/test/integration/blockchain.js b/test/integration/blockchain.js index 0b90788..462067a 100644 --- a/test/integration/blockchain.js +++ b/test/integration/blockchain.js @@ -313,6 +313,14 @@ describe('#blockchain', () => { assert.isString(result) }) }) + + describe('#getBlockchainInfo', () => { + it('should get info about the blockchain', async () => { + const result = await bchjs.Blockchain.getBlockchainInfo() + + console.log(`blockchain info: ${JSON.stringify(result, null, 2)}`) + }) + }) }) function sleep (ms) { diff --git a/test/unit/ecash.js b/test/unit/ecash.js new file mode 100644 index 0000000..43299c3 --- /dev/null +++ b/test/unit/ecash.js @@ -0,0 +1,56 @@ +/* + Unit tests for eCash library. +*/ + +// Global npm libraries +const assert = require('chai').assert +const Ecash = require('../../src/ecash') +const uut = new Ecash() + +describe('#eCash', () => { + describe('#toSatoshi', () => { + it('should convert XEC to satoshis', () => { + const xec = 10704.35 + + const result = uut.toSatoshi(xec) + + assert.equal(result, 1070435) + }) + + it('should throw an error if input is not a number', () => { + try { + uut.toSatoshi('test') + + assert.fail('Unexpected code path') + } catch (err) { + assert.equal( + err.message, + 'input must be a floating number representing XEC' + ) + } + }) + }) + + describe('#toXec', () => { + it('should convert satoshis to XEC', () => { + const sats = 1070435 + + const result = uut.toXec(sats) + + assert.equal(result, 10704.35) + }) + + it('should throw an error if input is not a number', () => { + try { + uut.toXec('test') + + assert.fail('Unexpected code path') + } catch (err) { + assert.equal( + err.message, + 'input must be a floating number representing satoshis' + ) + } + }) + }) +})