diff --git a/package-lock.json b/package-lock.json index c3f966007..94b75194a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -819,18 +819,16 @@ "dev": true }, "braces": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.1.tgz", - "integrity": "sha512-SO5lYHA3vO6gz66erVvedSCkp7AKWdv6VcQ2N4ysXfPxdAlxAMMAdwegGGcv1Bqwm7naF1hNdk5d6AAIEHV2nQ==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.2.tgz", + "integrity": "sha512-aNdbnj9P8PjdXU4ybaWLK2IF3jc/EoDYbC7AazW6to3TRsfXxscC9UXOB5iDiEQrkyIbWp2SLQda4+QAa7nc3w==", "dev": true, "requires": { "arr-flatten": "1.1.0", "array-unique": "0.3.2", - "define-property": "1.0.0", "extend-shallow": "2.0.1", "fill-range": "4.0.0", "isobject": "3.0.1", - "kind-of": "6.0.2", "repeat-element": "1.1.2", "snapdragon": "0.8.2", "snapdragon-node": "2.1.1", @@ -838,15 +836,6 @@ "to-regex": "3.0.2" }, "dependencies": { - "define-property": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/define-property/-/define-property-1.0.0.tgz", - "integrity": "sha1-dp66rz9KY6rTr56NMEybvnm/sOY=", - "dev": true, - "requires": { - "is-descriptor": "1.0.2" - } - }, "extend-shallow": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-2.0.1.tgz", @@ -1058,7 +1047,7 @@ "requires": { "arr-diff": "4.0.0", "array-unique": "0.3.2", - "braces": "2.3.1", + "braces": "2.3.2", "define-property": "2.0.2", "extend-shallow": "3.0.2", "extglob": "2.0.4", @@ -1251,9 +1240,9 @@ } }, "axios-mock-adapter": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.14.1.tgz", - "integrity": "sha1-yODuETSVUmdTjVZteuBoviBGcVg=", + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.15.0.tgz", + "integrity": "sha1-+8BoJdgwLJXDM00hAju6mWJV1F0=", "dev": true, "requires": { "deep-equal": "1.0.1" @@ -2370,9 +2359,9 @@ "dev": true }, "browserify-aes": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.1.1.tgz", - "integrity": "sha512-UGnTYAnB2a3YuYKIRy1/4FB2HdM866E0qC46JXvVTYKlBlZlnvfpSfY6OKfXZAkv70eJ2a1SqzpAo5CRhZGDFg==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz", + "integrity": "sha512-+7CHXqGuspUn/Sl5aO7Ea0xWGAtETPXNSAjHo48JfLdPWcMng33Xe4znFvQweqc/uzk5zSOI3H52CYnjCfb5hA==", "dev": true, "requires": { "buffer-xor": "1.0.3", @@ -2389,7 +2378,7 @@ "integrity": "sha1-mYgkSHS/XtTijalWZtzWasj8Njo=", "dev": true, "requires": { - "browserify-aes": "1.1.1", + "browserify-aes": "1.2.0", "browserify-des": "1.0.0", "evp_bytestokey": "1.0.3" } @@ -2657,7 +2646,7 @@ "requires": { "anymatch": "2.0.0", "async-each": "1.0.1", - "braces": "2.3.1", + "braces": "2.3.2", "fsevents": "1.1.3", "glob-parent": "3.1.0", "inherits": "2.0.3", @@ -2676,18 +2665,16 @@ "dev": true }, "braces": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.1.tgz", - "integrity": "sha512-SO5lYHA3vO6gz66erVvedSCkp7AKWdv6VcQ2N4ysXfPxdAlxAMMAdwegGGcv1Bqwm7naF1hNdk5d6AAIEHV2nQ==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.2.tgz", + "integrity": "sha512-aNdbnj9P8PjdXU4ybaWLK2IF3jc/EoDYbC7AazW6to3TRsfXxscC9UXOB5iDiEQrkyIbWp2SLQda4+QAa7nc3w==", "dev": true, "requires": { "arr-flatten": "1.1.0", "array-unique": "0.3.2", - "define-property": "1.0.0", "extend-shallow": "2.0.1", "fill-range": "4.0.0", "isobject": "3.0.1", - "kind-of": "6.0.2", "repeat-element": "1.1.2", "snapdragon": "0.8.2", "snapdragon-node": "2.1.1", @@ -2695,15 +2682,6 @@ "to-regex": "3.0.2" } }, - "define-property": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/define-property/-/define-property-1.0.0.tgz", - "integrity": "sha1-dp66rz9KY6rTr56NMEybvnm/sOY=", - "dev": true, - "requires": { - "is-descriptor": "1.0.2" - } - }, "extend-shallow": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-2.0.1.tgz", @@ -2768,17 +2746,6 @@ "dev": true, "requires": { "kind-of": "3.2.2" - }, - "dependencies": { - "kind-of": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", - "integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=", - "dev": true, - "requires": { - "is-buffer": "1.1.6" - } - } } }, "isobject": { @@ -2786,12 +2753,6 @@ "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", "integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8=", "dev": true - }, - "kind-of": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", - "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==", - "dev": true } } }, @@ -3621,6 +3582,12 @@ "tapable": "1.0.0" } }, + "envinfo": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/envinfo/-/envinfo-4.4.2.tgz", + "integrity": "sha512-5rfRs+m+6pwoKRCFqpsA5+qsLngFms1aWPrxfKbrObCzQaPc3M3yPloZx+BL9UE3dK58cxw36XVQbFRSCCfGSQ==", + "dev": true + }, "errno": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", @@ -5792,6 +5759,16 @@ "integrity": "sha512-YGG3ejvBNHRqu0559EOxxNFihD0AjpvHlC/pdGKd3X3ofe+CoJkYazwNJYTNebqpPKN+VVQbh4ZFn1DivMNuHA==", "dev": true }, + "import-local": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/import-local/-/import-local-1.0.0.tgz", + "integrity": "sha512-vAaZHieK9qjGo58agRBg+bhHX3hoTZU/Oa3GESWLz7t1U62fk63aHuDJJEteXoDeTCcPmUT+z38gkHPZkkmpmQ==", + "dev": true, + "requires": { + "pkg-dir": "2.0.0", + "resolve-cwd": "2.0.0" + } + }, "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", @@ -10868,7 +10845,7 @@ "dev": true, "requires": { "asn1.js": "4.10.1", - "browserify-aes": "1.1.1", + "browserify-aes": "1.2.0", "create-hash": "1.1.3", "evp_bytestokey": "1.0.3", "pbkdf2": "3.0.14" @@ -13285,9 +13262,9 @@ } }, "webpack": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.4.1.tgz", - "integrity": "sha512-iLUJcsEAjaPKWbB32ADr29Pg9fPUVfFEMPK4VXyZGftzhSEFg2BLjHLoBYZ14wdTEA8xqG/hjpuX8qOmabRYvw==", + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.5.0.tgz", + "integrity": "sha512-6GrZsvQJnG7o7mjbfjp6s5CyMfdopjt1A/X8LcYwceis9ySjqBX6Lusso2wNZ06utHj2ZvfL6L3f7hfgVeJP6g==", "dev": true, "requires": { "acorn": "5.5.3", @@ -13342,18 +13319,16 @@ "dev": true }, "braces": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.1.tgz", - "integrity": "sha512-SO5lYHA3vO6gz66erVvedSCkp7AKWdv6VcQ2N4ysXfPxdAlxAMMAdwegGGcv1Bqwm7naF1hNdk5d6AAIEHV2nQ==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-2.3.2.tgz", + "integrity": "sha512-aNdbnj9P8PjdXU4ybaWLK2IF3jc/EoDYbC7AazW6to3TRsfXxscC9UXOB5iDiEQrkyIbWp2SLQda4+QAa7nc3w==", "dev": true, "requires": { "arr-flatten": "1.1.0", "array-unique": "0.3.2", - "define-property": "1.0.0", "extend-shallow": "2.0.1", "fill-range": "4.0.0", "isobject": "3.0.1", - "kind-of": "6.0.2", "repeat-element": "1.1.2", "snapdragon": "0.8.2", "snapdragon-node": "2.1.1", @@ -13361,15 +13336,6 @@ "to-regex": "3.0.2" }, "dependencies": { - "define-property": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/define-property/-/define-property-1.0.0.tgz", - "integrity": "sha1-dp66rz9KY6rTr56NMEybvnm/sOY=", - "dev": true, - "requires": { - "is-descriptor": "1.0.2" - } - }, "extend-shallow": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/extend-shallow/-/extend-shallow-2.0.1.tgz", @@ -13581,7 +13547,7 @@ "requires": { "arr-diff": "4.0.0", "array-unique": "0.3.2", - "braces": "2.3.1", + "braces": "2.3.2", "define-property": "2.0.2", "extend-shallow": "3.0.2", "extglob": "2.0.4", @@ -13668,18 +13634,20 @@ } }, "webpack-cli": { - "version": "2.0.13", - "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-2.0.13.tgz", - "integrity": "sha512-0lnOi3yla8FsZVuMsbfnNRB/8DlfuDugKdekC+4ykydZG0+UOidMi5J5LLWN4c0VJ8PqC19yMXXkYyCq78OuqA==", + "version": "2.0.14", + "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-2.0.14.tgz", + "integrity": "sha512-gRoWaxSi2JWiYsn1QgOTb6ENwIeSvN1YExZ+kJ0STsTZK7bWPElW+BBBv1UnTbvcPC3v7E17mK8hlFX8DOYSGw==", "dev": true, "requires": { "chalk": "2.3.2", "cross-spawn": "6.0.5", "diff": "3.5.0", "enhanced-resolve": "4.0.0", + "envinfo": "4.4.2", "glob-all": "3.1.0", "global-modules": "1.0.0", "got": "8.3.0", + "import-local": "1.0.0", "inquirer": "5.2.0", "interpret": "1.1.0", "jscodeshift": "0.5.0", @@ -13691,11 +13659,10 @@ "p-each-series": "1.0.0", "p-lazy": "1.0.0", "prettier": "1.11.1", - "resolve-cwd": "2.0.0", "supports-color": "5.3.0", "v8-compile-cache": "1.1.2", "webpack-addons": "1.1.5", - "yargs": "11.0.0", + "yargs": "11.1.0", "yeoman-environment": "2.0.6", "yeoman-generator": "2.0.3" }, @@ -13915,9 +13882,9 @@ "dev": true }, "yargs": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-11.0.0.tgz", - "integrity": "sha512-Rjp+lMYQOWtgqojx1dEWorjCofi1YN7AoFvYV7b1gx/7dAAeuI4kN5SZiEvr0ZmsZTOpDRcCqrpI10L31tFkBw==", + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-11.1.0.tgz", + "integrity": "sha512-NwW69J42EsCSanF8kyn5upxvjp5ds+t3+udGBeTbFnERA+lF541DDpMawzo4z6W/QrzNM18D+BPMiOBibnFV5A==", "dev": true, "requires": { "cliui": "4.0.0", diff --git a/package.json b/package.json index 3a83e351a..802073a13 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "@babel/polyfill": "^7.0.0-beta.44", "@babel/preset-env": "^7.0.0-beta.44", "@babel/register": "^7.0.0-beta.44", - "axios-mock-adapter": "^1.14.0", + "axios-mock-adapter": "^1.15.0", "babel-loader": "^8.0.0-beta.0", "babel-plugin-istanbul": "^4.1.6", "chai": "^4.0.2", @@ -21,8 +21,8 @@ "mocha": "^5.0.5", "nyc": "^11.5.0", "typescript": "^2.8.1", - "webpack": "^4.4.1", - "webpack-cli": "^2.0.13", + "webpack": "^4.5.0", + "webpack-cli": "^2.0.14", "zopfli-webpack-plugin": "^0.1.0" }, "name": "@cityofzion/neon-js", diff --git a/src/transactions/strategy.js b/src/transactions/strategy.js index 8001dbc79..e9a3d9789 100644 --- a/src/transactions/strategy.js +++ b/src/transactions/strategy.js @@ -32,9 +32,9 @@ export const biggestFirst = (assetBalance, requiredAmt) => { */ export const balancedApproach = (assetBalance, requiredAmt) => { // Ascending sort first - assetBalance.unspent.sort((a, b) => a.value.sub(b.value)) + assetBalance.unspent.sort((a, b) => a.value.sub(b.value).toNumber()) // Trim off coins larger than requiredAmt - const smallCoins = assetBalance.unspent.filter((c) => c.value <= requiredAmt) + const smallCoins = assetBalance.unspent.filter((c) => c.value.lte(requiredAmt)) if (smallCoins.length === 0) return [assetBalance.unspent[0]] // Check for naive solution const i = smallCoins.findIndex((c) => requiredAmt.eq(c.value)) diff --git a/test/unit/transactions/strategy.js b/test/unit/transactions/strategy.js index f37257a7f..8cc27a23e 100644 --- a/test/unit/transactions/strategy.js +++ b/test/unit/transactions/strategy.js @@ -1,6 +1,6 @@ import { smallestFirst, biggestFirst, balancedApproach } from '../../../src/transactions/strategy' import Coin from '../../../src/wallet/components/Coin' -import {Fixed8} from '../../../src/utils' +import { Fixed8 } from '../../../src/utils' describe('Strategy', function () { let coins @@ -8,13 +8,13 @@ describe('Strategy', function () { beforeEach(() => { coins = [ - Coin({txid: '1', value: 1}), - Coin({txid: '2', value: 2}), - Coin({txid: '3', value: 3}), - Coin({txid: '4', value: 4}), - Coin({txid: '5', value: 5}), - Coin({txid: '6', value: 6}), - Coin({txid: '7', value: 7}) + Coin({ txid: '1', value: 1 }), + Coin({ txid: '2', value: 2 }), + Coin({ txid: '3', value: 3 }), + Coin({ txid: '4', value: 4 }), + Coin({ txid: '5', value: 5 }), + Coin({ txid: '6', value: 6 }), + Coin({ txid: '7', value: 7 }) ] assetBalance = { unspent: coins.slice(0) @@ -45,5 +45,15 @@ describe('Strategy', function () { const result = balancedApproach(assetBalance, new Fixed8(4.1)) result.should.have.members([coins[0], coins[3]]) }) + + it('big amt', () => { + const result = balancedApproach(assetBalance, new Fixed8(20)) + result.should.have.members([coins[6], coins[0], coins[1], coins[2], coins[3], coins[4]]) + }) + + it('all', () => { + const result = balancedApproach(assetBalance, new Fixed8(28)) + result.should.have.members([coins[0], coins[1], coins[2], coins[3], coins[4], coins[5], coins[6]]) + }) }) })