ExchangeLedger
a class for creating ledgers to manage exchange trading of currencies and other arbitrary exchangeable entities
⭐️ Getting Started
Install as a dependency:
$ npm install exchange-ledger
Initialize a ledger with a starting balance:
var ExchangeLedger = var ledger = balance: 'usd': 100 'cny': 100
💸 Trading
Commit a trade from one currency to another, specifying the conversion rate and the amount to be converted:
ledger
(Optional) Apply a percent-based transaction fee to the trade, taken out of the currency being traded to:
ledger
Trades can safely be made to currencies not yet specified on the balance:
ledger
The trade history will be stored on the ledger as an array:
ledgertrades// [ { from: 'usd', to: 'cny', rate: 7, amount: 50},// { from: 'cny', to: 'jpy', rate: 16.4, amount: 100 } ]
There's also easy access to the last trade committed:
ledger// { from: 'cny', to: 'jpy', rate: 16.4, amount: 100 }
📖 Balance and Liquidation
Check what the current balance for all specified currencies:
ledgerbalance// { 'usd': 0, 'cny': 512.5, 'jpy': 1640 } ledgerbalanceusd// 0
Use getLiquidatedTo
to check the liquidated value of the balance for a specific currency, by specifying the associated conversion rate(s) (and optionally the transaction fee(s)):
var currentUSD = ledger// currentUSD == 1332.5// NOTE: This command will not affect the ledger.
Similarly, use liquidateTo
to actually convert the balance on the ledger to a specific currency:
ledger var currentUSD = ledgerbalanceusd// currentUSD == 1332.5
Testing
mocha
is used for unit-testing in test.js
. To run these test, simply:
$ npm test