Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
snowypowers committed Apr 11, 2018
2 parents bd970c9 + e5c3ad2 commit e149976
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 94 deletions.
129 changes: 48 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/transactions/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
26 changes: 18 additions & 8 deletions test/unit/transactions/strategy.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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
let assetBalance

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)
Expand Down Expand Up @@ -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]])
})
})
})

0 comments on commit e149976

Please sign in to comment.