Skip to content

Commit

Permalink
feat: add self pairs - ledger back to itself
Browse files Browse the repository at this point in the history
This enables the connector to provide routing for plugins that represent
multiple accounts and to charge a fee in the process.
  • Loading branch information
justmoon committed Nov 7, 2017
1 parent 2125b4b commit 2c3b0f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/lib/ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class Ledgers extends EventEmitter {
if (!tradesFrom && !tradesTo) {
const newLedger = creds.currency + '@' + ledgerPrefix
for (let otherLedgerPrefix of this._ledgers.keys()) {
if (ledgerPrefix === otherLedgerPrefix) continue
const currency = this._ledgers.get(otherLedgerPrefix).currency
const otherLedger = currency + '@' + otherLedgerPrefix
this._pairs.add(newLedger, otherLedger)
Expand Down
13 changes: 8 additions & 5 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ const crypto = require('crypto')
*
* Example:
* getPairs ([1, 2, 3, 4])
* // => [ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ], [ 3, 4 ],
* [ 2, 1 ], [ 3, 1 ], [ 4, 1 ], [ 3, 2 ], [ 4, 2 ], [ 4, 3 ] ]
* => [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 1, 3 ], [ 3, 1 ], [ 1, 4 ], [ 4, 1 ],
* [ 2, 2 ], [ 2, 3 ], [ 3, 2 ], [ 2, 4 ], [ 4, 2 ],
* [ 3, 3 ], [ 3, 4 ], [ 4, 3 ],
* [ 4, 4 ] ]
*
* @param {array} arr Input array
* @return {array[]} Possible pairs
*/
function getPairs (arr) {
return arr.reduce((prev, cur, i) => (
[].concat.apply(prev, arr.slice(i + 1).map((val) => [[cur, val], [val, cur]]))
), [])
return arr.reduce((prev, cur, i) => {
const combinations = arr.slice(i + 1).map(val => [[cur, val], [val, cur]])
return prev.concat(Array.prototype.concat.apply([[cur, cur]], combinations))
}, [])
}

/**
Expand Down
9 changes: 9 additions & 0 deletions test/configSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ describe('ConnectorConfig', function () {
it('should auto-generate pairs', function * () {
const config = loadConnectorConfig()
expect(config.get('tradingPairs')).to.deep.equal([[
'USD@usd-ledger.',
'USD@usd-ledger.'
], [
'USD@usd-ledger.',
'EUR@eur-ledger.'
], [
Expand All @@ -54,12 +57,18 @@ describe('ConnectorConfig', function () {
], [
'AUD@aud-ledger.',
'USD@usd-ledger.'
], [
'EUR@eur-ledger.',
'EUR@eur-ledger.'
], [
'EUR@eur-ledger.',
'AUD@aud-ledger.'
], [
'AUD@aud-ledger.',
'EUR@eur-ledger.'
], [
'AUD@aud-ledger.',
'AUD@aud-ledger.'
]])
})

Expand Down

0 comments on commit 2c3b0f9

Please sign in to comment.