diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index adae97ae..0f307cd2 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -153,14 +153,12 @@ } }, "ripple-lib-transactionparser": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/ripple-lib-transactionparser/-/ripple-lib-transactionparser-0.5.0.tgz", - "dependencies": { - "bignumber.js": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-1.4.1.tgz" - } - } + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/ripple-lib-transactionparser/-/ripple-lib-transactionparser-0.5.1.tgz" + }, + "ripple-lib-value": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/ripple-lib-value/-/ripple-lib-value-0.1.0.tgz" }, "sjcl-codec": { "version": "0.1.0", diff --git a/package.json b/package.json index c88d6096..d422f3cc 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "lru-cache": "~2.5.0", "ripple-address-codec": "^1.6.0", "ripple-keypairs": "^0.9.0", - "ripple-lib-transactionparser": "^0.5.0", + "ripple-lib-transactionparser": "^0.5.1", + "ripple-lib-value": "0.1.0", "sjcl-codec": "0.1.0", "ws": "~0.7.1" }, diff --git a/src/api/common/schemas/order-change.json b/src/api/common/schemas/order-change.json index c298da51..ef377ac5 100644 --- a/src/api/common/schemas/order-change.json +++ b/src/api/common/schemas/order-change.json @@ -9,6 +9,7 @@ }, "quantity": {"$ref": "balance"}, "totalPrice": {"$ref": "balance"}, + "makerExchangeRate": {"$ref": "value"}, "sequence": {"$ref": "sequence"}, "status": {"enum": ["created", "open", "closed", "canceled"]} }, diff --git a/src/core/amount.js b/src/core/amount.js index fdf99190..0a7a1224 100644 --- a/src/core/amount.js +++ b/src/core/amount.js @@ -8,15 +8,15 @@ const extend = require('extend'); const utils = require('./utils'); const UInt160 = require('./uint160').UInt160; const Currency = require('./currency').Currency; -const Value = require('./value').Value; -const IOUValue = require('./iouvalue').IOUValue; -const XRPValue = require('./xrpvalue').XRPValue; +const {XRPValue, IOUValue} = require('ripple-lib-value'); + +type Value = XRPValue | IOUValue; function Amount(value = new XRPValue(NaN)) { // Json format: // integer : XRP // { 'value' : ..., 'currency' : ..., 'issuer' : ...} - assert(value instanceof Value); + assert(value instanceof XRPValue || value instanceof IOUValue); this._value = value; this._is_native = true; // Default to XRP. Only valid if value is not NaN. @@ -625,7 +625,8 @@ function(quality, counterCurrency, counterIssuer, opts) { } if (this._is_native) { this._set_value( - new XRPValue(nativeAdjusted.round(6, Value.getBNRoundDown()).toString())); + new XRPValue(nativeAdjusted.round(6, XRPValue.getBNRoundDown()) + .toString())); } else { this._set_value(nativeAdjusted); } @@ -723,7 +724,7 @@ Amount.prototype.parse_native = function(j) { // Requires _currency to be set! Amount.prototype.parse_value = function(j) { this._is_native = false; - const newValue = new IOUValue(j, Value.getBNRoundDown()); + const newValue = new IOUValue(j, IOUValue.getBNRoundDown()); this._set_value(newValue); return this; }; diff --git a/src/core/index.js b/src/core/index.js index 04a4109d..597855a7 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -20,7 +20,6 @@ exports.TransactionQueue = require('./transactionqueue').TransactionQueue; exports.convertBase = require('./baseconverter'); exports._test = { - IOUValue: require('./iouvalue').IOUValue, Log: require('./log'), PathFind: require('./pathfind').PathFind, TransactionManager: require('./transactionmanager').TransactionManager, diff --git a/src/core/iouvalue.js b/src/core/iouvalue.js deleted file mode 100644 index babffecd..00000000 --- a/src/core/iouvalue.js +++ /dev/null @@ -1,56 +0,0 @@ -/* @flow */ - -'use strict'; - -const Value = require('./value').Value; -const XRPValue = require('./xrpvalue').XRPValue; -const GlobalBigNumber = require('bignumber.js'); -const BigNumber = GlobalBigNumber.another({ - ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP, - DECIMAL_PLACES: 40 -}); -const rippleUnits = new BigNumber(1e6); - -class IOUValue extends Value { - - constructor(value: string | BigNumber, roundingMode: ?number = null, - base: ?number = null) { - - super(new BigNumber(value, base).toDigits(16, roundingMode)); - } - - multiply(multiplicand: Value) { - if (multiplicand instanceof XRPValue) { - return super.multiply( - new IOUValue( - multiplicand._value.times(rippleUnits))); - } - return super.multiply(multiplicand); - } - - divide(divisor: Value) { - if (divisor instanceof XRPValue) { - return super.divide( - new IOUValue(divisor._value.times(rippleUnits))); - } - return super.divide(divisor); - } - - negate() { - return new IOUValue(this._value.neg()); - } - - _canonicalize(value) { - if (value.isNaN()) { - throw new Error('Invalid result'); - } - return new IOUValue(value.toPrecision(16)); - } - - equals(comparator) { - return (comparator instanceof IOUValue) - && this._value.equals(comparator._value); - } -} - -exports.IOUValue = IOUValue; diff --git a/src/core/orderbook.js b/src/core/orderbook.js index ed080f78..185f7ec0 100644 --- a/src/core/orderbook.js +++ b/src/core/orderbook.js @@ -22,7 +22,7 @@ const Currency = require('./currency').Currency; const AutobridgeCalculator = require('./autobridgecalculator'); const OrderBookUtils = require('./orderbookutils'); const log = require('./log').internal.sub('orderbook'); -const IOUValue = require('./iouvalue').IOUValue; +const {IOUValue} = require('ripple-lib-value'); function _sortOffers(a, b) { const aQuality = OrderBookUtils.getOfferQuality(a, this._currencyGets); diff --git a/src/core/orderbookutils.js b/src/core/orderbookutils.js index f43cc4a9..ec9eb90e 100644 --- a/src/core/orderbookutils.js +++ b/src/core/orderbookutils.js @@ -7,7 +7,7 @@ const Types = require('./serializedtypes'); const Amount = require('./amount').Amount; const Currency = require('./currency').Currency; const UInt160 = require('./uint160').UInt160; -const IOUValue = require('./iouvalue').IOUValue; +const {IOUValue} = require('ripple-lib-value'); const OrderBookUtils = {}; function assertValidNumber(number, message) { diff --git a/src/core/value.js b/src/core/value.js deleted file mode 100644 index eea11495..00000000 --- a/src/core/value.js +++ /dev/null @@ -1,109 +0,0 @@ -/* @flow */ - -'use strict'; - -const GlobalBigNumber = require('bignumber.js'); - -const BigNumber = GlobalBigNumber.another({ - ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP, - DECIMAL_PLACES: 40 -}); - -const assert = require('assert'); - -class Value { - - constructor(value: string | BigNumber) { - if (this.constructor === 'Value') { - throw new Error( - 'Cannot instantiate Value directly, it is an abstract base class'); - } - this._value = new BigNumber(value); - } - - static getBNRoundDown() { - return BigNumber.ROUND_DOWN; - } - - abs() { - const result = this._value.abs(); - return this._canonicalize(result); - } - - add(addend: Value) { - assert(this.constructor === addend.constructor); - const result = this._value.plus(addend._value); - return this._canonicalize(result); - } - - subtract(subtrahend: Value) { - assert(this.constructor === subtrahend.constructor); - const result = this._value.minus(subtrahend._value); - return this._canonicalize(result); - } - - multiply(multiplicand: Value) { - const result = this._value.times(multiplicand._value); - return this._canonicalize(result); - } - - divide(divisor: Value) { - if (divisor.isZero()) { - throw new Error('divide by zero'); - } - const result = this._value.dividedBy(divisor._value); - return this._canonicalize(result); - } - - invert() { - const result = (new BigNumber(this._value)).toPower(-1); - return this._canonicalize(result); - } - - round(decimalPlaces: number, roundingMode: number) { - const result = this._value.round(decimalPlaces, roundingMode); - return this._canonicalize(result); - } - - toFixed(decimalPlaces: number, roundingMode: number) { - return this._value.toFixed(decimalPlaces, roundingMode); - } - - getExponent() { - return this._value.e; - } - - isNaN() { - return this._value.isNaN(); - } - - isZero() { - return this._value.isZero(); - } - - isNegative() { - return this._value.isNegative(); - } - - toString() { - return this._value.toString(); - } - - greaterThan(comparator: Value) { - assert(this.constructor === comparator.constructor); - return this._value.greaterThan(comparator._value); - } - - lessThan(comparator: Value) { - assert(this.constructor === comparator.constructor); - return this._value.lessThan(comparator._value); - } - - comparedTo(comparator: Value) { - assert(this.constructor === comparator.constructor); - return this._value.comparedTo(comparator._value); - } - -} - -exports.Value = Value; diff --git a/src/core/xrpvalue.js b/src/core/xrpvalue.js deleted file mode 100644 index 47107847..00000000 --- a/src/core/xrpvalue.js +++ /dev/null @@ -1,59 +0,0 @@ -/* @flow */ - -'use strict'; - -const GlobalBigNumber = require('bignumber.js'); -const BigNumber = GlobalBigNumber.another({ - ROUNDING_MODE: GlobalBigNumber.ROUND_HALF_UP, - DECIMAL_PLACES: 40 -}); - -const Value = require('./value').Value; -const rippleUnits = new BigNumber(1e6); - -class XRPValue extends Value { - - constructor(value: string | BigNumber) { - super(value); - if (this._value.dp() > 6) { - throw new Error( - 'Value has more than 6 digits of precision past the decimal point, ' - + 'an IOUValue may be being cast to an XRPValue' - ); - } - } - - multiply(multiplicand: Value) { - if (multiplicand instanceof XRPValue) { - return super.multiply( - new XRPValue(multiplicand._value.times(rippleUnits))); - } - return super.multiply(multiplicand); - } - - divide(divisor: Value) { - if (divisor instanceof XRPValue) { - return super.divide( - new XRPValue(divisor._value.times(rippleUnits))); - } - return super.divide(divisor); - } - - negate() { - return new XRPValue(this._value.neg()); - } - - _canonicalize(value) { - if (value.isNaN()) { - throw new Error('Invalid result'); - } - return new XRPValue(value.round(6, BigNumber.ROUND_DOWN)); - } - - equals(comparator) { - return (comparator instanceof XRPValue) - && this._value.equals(comparator._value); - } -} - -exports.XRPValue = XRPValue; diff --git a/test/fixtures/api/responses/get-transaction-order-cancellation.json b/test/fixtures/api/responses/get-transaction-order-cancellation.json index 458433e1..91c83861 100644 --- a/test/fixtures/api/responses/get-transaction-order-cancellation.json +++ b/test/fixtures/api/responses/get-transaction-order-cancellation.json @@ -31,6 +31,7 @@ "currency": "XRP", "value": "0" }, + "makerExchangeRate": "1185000", "sequence": 465, "status": "canceled" } @@ -39,4 +40,4 @@ "ledgerVersion": 14661789, "indexInLedger": 4 } -} +} diff --git a/test/fixtures/api/responses/get-transaction-order.json b/test/fixtures/api/responses/get-transaction-order.json index ea4b15dd..397e32ff 100644 --- a/test/fixtures/api/responses/get-transaction-order.json +++ b/test/fixtures/api/responses/get-transaction-order.json @@ -40,6 +40,7 @@ "currency": "XRP", "value": "0.0002" }, + "makerExchangeRate": "1185000", "sequence": 465, "status": "created" } diff --git a/test/fixtures/api/responses/get-transaction-payment.json b/test/fixtures/api/responses/get-transaction-payment.json index ed14494a..9bba564a 100644 --- a/test/fixtures/api/responses/get-transaction-payment.json +++ b/test/fixtures/api/responses/get-transaction-payment.json @@ -75,6 +75,7 @@ "counterparty": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", "value": "-0.001002" }, + "makerExchangeRate": "1099", "sequence": 58, "status": "open" } diff --git a/test/fixtures/api/responses/get-transactions.json b/test/fixtures/api/responses/get-transactions.json index 22cd3197..a026a502 100644 --- a/test/fixtures/api/responses/get-transactions.json +++ b/test/fixtures/api/responses/get-transactions.json @@ -81,6 +81,7 @@ "counterparty": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", "value": "-0.001002" }, + "makerExchangeRate": "1099", "sequence": 58, "status": "open" } @@ -172,6 +173,7 @@ "counterparty": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", "value": "-0.001002" }, + "makerExchangeRate": "1099", "sequence": 58, "status": "open" } diff --git a/test/fixtures/orderbook.js b/test/fixtures/orderbook.js index 2568f264..4fe2c564 100644 --- a/test/fixtures/orderbook.js +++ b/test/fixtures/orderbook.js @@ -7,7 +7,7 @@ const addresses = require('./addresses'); const Meta = require('ripple-lib').Meta; const SerializedObject = require('ripple-lib').SerializedObject; const Types = require('ripple-lib').types; -const IOUValue = require('ripple-lib')._test.IOUValue; +const IOUValue = require('ripple-lib-value').IOUValue; module.exports.FIAT_BALANCE = '10'; module.exports.NATIVE_BALANCE = '55'; diff --git a/test/orderbook-autobridge-test.js b/test/orderbook-autobridge-test.js index bb4bb51a..2345d8af 100644 --- a/test/orderbook-autobridge-test.js +++ b/test/orderbook-autobridge-test.js @@ -8,7 +8,7 @@ const Remote = require('ripple-lib').Remote; const Currency = require('ripple-lib').Currency; const addresses = require('./fixtures/addresses'); const fixtures = require('./fixtures/orderbook'); -const IOUValue = require('ripple-lib')._test.IOUValue; +const IOUValue = require('ripple-lib-value').IOUValue; describe('OrderBook Autobridging', function() { this.timeout(0); diff --git a/test/orderbook-test.js b/test/orderbook-test.js index fcca2bdc..67bcf4d5 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -9,7 +9,7 @@ const Amount = require('ripple-lib').Amount; const Meta = require('ripple-lib').Meta; const addresses = require('./fixtures/addresses'); const fixtures = require('./fixtures/orderbook'); -const IOUValue = require('ripple-lib')._test.IOUValue; +const IOUValue = require('ripple-lib-value').IOUValue; describe('OrderBook', function() { this.timeout(0);