diff --git a/package.json b/package.json index 335032a..ebe4819 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test:integration:local:bchn": "export RESTURL=http://localhost:3000/v4/ && mocha --timeout 30000 test/integration/ && mocha --timeout 30000 test/integration/chains/bchn/", "test:integration:local:testnet": "RESTURL=http://localhost:4000/v4/ mocha --timeout 30000 test/integration/chains/testnet", "test:temp": "export RESTURL=https://bchn.fullstack.cash/v4/ && mocha --timeout 30000 -g '#UTXO' test/integration/chains/bchn/", - "test:temp2": "mocha --timeout=30000 -g '#utxo' test/unit/", + "test:temp2": "mocha --timeout=30000 -g '#Util' test/unit/", "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "nyc --reporter=html mocha --timeout 25000 test/unit/", "docs": "./node_modules/.bin/apidoc -i src/ -o docs", diff --git a/src/util.js b/src/util.js index ab1a675..441480d 100644 --- a/src/util.js +++ b/src/util.js @@ -27,6 +27,41 @@ class Util { _this = this } + /** + * @api util.floor8() floor8() + * @apiName floor8 + * @apiGroup Util + * @apiDescription Round a number down to 8 decimal places. + * + * + * @apiExample Example usage: + * (async () => { + * try { + * const num = 1.234567891111 + * const result = bchjs.Util.floor8(num) + * console.log(result) + * } catch(error) { + * console.error(error) + * } + * })() + * + * // returns + * 1.23456789 + */ + // floor8 - round to 8 decimal places + // Takes a number and returns it, rounded to the nearest 8 decimal place. + floor8 (num) { + const thisNum = Number(num) + + if (isNaN(thisNum)) throw new Error('input must be a number') + + let tempNum = thisNum * 100000000 + tempNum = Math.floor(tempNum) + tempNum = tempNum / 100000000 + + return tempNum + } + /** * @api util.validateAddress() validateAddress() * @apiName Validate Address. diff --git a/src/utxo.js b/src/utxo.js index 40b233c..34c4149 100644 --- a/src/utxo.js +++ b/src/utxo.js @@ -280,6 +280,38 @@ class UTXO { } } + /** + * @api Utxo.findBiggestUtxo() findBiggestUtxo() + * @apiName findBiggestUtxo + * @apiGroup UTXO + * @apiDescription Get the biggest UTXO in an array. + * + * Given an array of BCH UTXOs, this method will return the biggest UTXO. + * This is often the simplest way to pick a UTXO for generating a transaction. + * + * @apiExample Example usage: + * (async () => { + * try { + * const utxos = await bchjs.Utxo.get('bitcoincash:qq54fgjn3hz0357n8a6guy4demw9xfkjk5jcj0xr0z'); + * const utxo = bchjs.Utxo.findBiggestUtxo(utxos[0].bchUtxos) + * console.log(utxo); + * } catch(error) { + * console.error(error) + * } + * })() + * + * // returns + * { + * "height": 655431, + * "tx_hash": "7a091716f8137e94f87e7760648cd34a17e32754ef95f7c7bda38a635c9b2b1b", + * "tx_pos": 0, + * "value": 800, + * "txid": "7a091716f8137e94f87e7760648cd34a17e32754ef95f7c7bda38a635c9b2b1b", + * "vout": 0, + * "isValid": false, + * "satoshis": 800 + * } + */ // Returns the utxo with the biggest balance from an array of utxos. findBiggestUtxo (utxos) { let largestAmount = 0 diff --git a/test/integration/chains/bchn/utxo-integration.js b/test/integration/chains/bchn/utxo-integration.js index 43e5b8a..d8b6006 100644 --- a/test/integration/chains/bchn/utxo-integration.js +++ b/test/integration/chains/bchn/utxo-integration.js @@ -112,7 +112,7 @@ describe('#UTXO', () => { // console.log(`utxos: ${JSON.stringify(utxos, null, 2)}`) const result = bchjs.Utxo.findBiggestUtxo(utxos[0].bchUtxos) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) + console.log(`result: ${JSON.stringify(result, null, 2)}`) assert.property(result, 'satoshis') assert.equal(result.satoshis, 800) diff --git a/test/unit/util.js b/test/unit/util.js index dda4500..55562e0 100644 --- a/test/unit/util.js +++ b/test/unit/util.js @@ -1,4 +1,5 @@ const assert = require('assert') +const assert2 = require('chai').assert const axios = require('axios') const BCHJS = require('../../src/bch-js') const bchjs = new BCHJS() @@ -32,4 +33,37 @@ describe('#Util', () => { .then(done, done) }) }) + + describe('#floor8', () => { + it('should floor a number to 8 decimals', () => { + const num = 1.234567891111 + + const result = bchjs.Util.floor8(num) + // console.log(result) + + assert2.equal(result, 1.23456789) + }) + + it('should throw an error for non-number input', () => { + try { + const num = 'string' + + bchjs.Util.floor8(num) + + assert2.equal(true, false, 'Unexpected result') + } catch (err) { + // console.log(err) + assert2.include(err.message, 'input must be a number') + } + }) + + it('should not effect a number with less than 8 decimals', () => { + const num = 1.23 + + const result = bchjs.Util.floor8(num) + // console.log(result) + + assert2.equal(result, 1.23) + }) + }) })