Compare commits

..

50 Commits

Author SHA1 Message Date
Geert Weening
1b57cc6d35 [TASK] bump verison to 0.8.2-rc2 2014-09-22 23:59:12 -07:00
Geert Weening
77234f256d [DOC] update release notes 2014-09-22 23:58:49 -07:00
Geert Weening
795d31d2db Merge branch 'release' into develop 2014-09-22 23:50:45 -07:00
wltsmrz
f3f10fd9bd Fix requestLedger arguments 2014-09-18 02:03:29 -07:00
wltsmrz
7100b4be8d Emit server with subscription events 2014-09-18 01:41:23 -07:00
wltsmrz
b1a7200d1b Deprecate account_tx map/reduce/filter 2014-09-18 01:28:43 -07:00
Geert Weening
5d8bb541c6 [TASK] bump version to 0.8.2-rc1 2014-09-17 15:02:44 -07:00
Geert Weening
b51c59b23a [TASK] update release notes 2014-09-17 15:02:21 -07:00
wltsmrz
2cd434e861 Fix orderbook reset on reconnect 2014-09-17 13:42:24 -07:00
wltsmrz
1599eb9629 Add potential missing error handlers 2014-09-17 13:40:49 -07:00
wltsmrz
8ef7481858 Allow mixed letters and numbers in currencies 2014-09-17 13:39:49 -07:00
Geert Weening
344d478b3f [TASK] bump version to 0.8.1 2014-09-15 16:52:23 -07:00
Geert Weening
39b7e27aa6 [TASK] bump version to 0.8.1-rc3 2014-09-12 14:26:25 -07:00
Geert Weening
b1876b4f77 [DOC] update release notes 2014-09-12 14:26:02 -07:00
wltsmrz
db3b41d1ba Merge pull request #167 from ximinez/ripd-549
[BUG] Fix Amount.to_human_full()
2014-09-12 12:16:03 -07:00
Edward Hennis
02b5d14d0f [BUG] Fix Amount.to_human_full()
* Amount.is_native is a function, but was referenced as a bool.
* Duplicate to_text_full tests to test to_human_full.
2014-09-12 10:52:46 -04:00
wltsmrz
0120044c96 Fix undefined fee states when connecting to a rippled that is syncing 2014-09-11 23:44:21 -07:00
wltsmrz
ad6304e857 Merge pull request #166 from geertweening/develop
[DOC] update README install instructions
2014-09-10 18:54:45 -07:00
Geert Weening
7cba84b8cf [DOC] update README install instructions 2014-09-10 18:51:59 -07:00
Geert Weening
5a9a4be163 [TASK] bump version to 0.8.1-rc2 2014-09-10 17:54:11 -07:00
Geert Weening
4d1a31d3c9 Revert "[FOLD] Add postinstall step to build the sjcl.js"
This reverts commit 42c853dbf4.
2014-09-10 17:53:29 -07:00
Geert Weening
6e3ceec4e5 [TASK] update publish scripts 2014-09-10 17:33:52 -07:00
Geert Weening
bc7d3c0af8 [TASK] add release candidate publish script 2014-09-10 17:32:03 -07:00
Geert Weening
519ddee092 [TASK] bump version to 0.8.1-rc1 2014-09-10 17:28:34 -07:00
Geert Weening
3e0fcc5b8b [DOC] update release notes 2014-09-10 17:28:00 -07:00
wltsmrz
b1972985c4 Merge pull request #164 from geertweening/develop
[FIX] return null for _getServer if there's no connected server
2014-09-08 12:04:22 -07:00
Geert Weening
51c42e9257 [FIX] return null for _getServer if there's no connected server 2014-09-08 11:54:48 -07:00
Geert Weening
86dcbcc671 [TASK] add exit_on_error calls to publish script 2014-09-08 11:54:47 -07:00
wltsmrz
3b7cd9d84f Merge pull request #163 from stevenzeiler/wallet
[TASK] Update package.json for ripple-wallet-generator patch.
2014-09-06 02:18:08 -07:00
Steven Zeiler
1073ec6214 [TASK] Update package.json for ripple-wallet-generator patch.
Removed node_modules from git repo of ripple-wallet-generator
2014-09-05 21:19:09 -07:00
wltsmrz
14a5e42a63 Merge pull request #162 from sublimator/develop
Fix #160, Seed.parse_json of hex
2014-09-05 20:56:53 -07:00
Nicholas Dudfield
b4564a86b4 Fix #160, Seed.parse_json of hex 2014-09-06 10:45:26 +07:00
wltsmrz
03386a61e9 Merge pull request #161 from stevenzeiler/wallet
[FEATURE] Add Wallet class that generates wallets.
2014-09-05 18:22:55 -07:00
Steven Zeiler
8bb2623360 [FEATURE] Add Wallet class that generates wallets.
[DOC] Comment out logged wallet.

[FIX] Use var instead of const.
2014-09-05 18:20:46 -07:00
wltsmrz
ab0e4188b3 Merge pull request #159 from ximinez/ripd-549
Clean up
2014-09-05 13:03:48 -07:00
Edward Hennis
42c853dbf4 [FOLD] Add postinstall step to build the sjcl.js 2014-09-05 14:50:29 -04:00
Geert Weening
ce48a1793b Merge branch 'release' into develop 2014-09-05 10:24:05 -07:00
Geert Weening
6177543d98 [TASK] add publish script 2014-09-05 10:23:59 -07:00
Geert Weening
9697bfa817 [TASK] add publish to bower script 2014-09-05 10:23:59 -07:00
Geert Weening
70425ab5c8 [TASK] bump version to 0.8.0 2014-09-05 10:19:58 -07:00
wltsmrz
7cccb451d2 Show field name in serialization failure 2014-09-04 20:13:04 -07:00
wltsmrz
a39fb9d551 Minor cleanup 2014-09-04 19:53:16 -07:00
wltsmrz
8f7cdc6e4f Prevent setting LastLedgerSequence of NaN. Fixes a potential ambiguous serialization error 2014-09-04 19:50:56 -07:00
wltsmrz
8f7e365b03 Wait until remote is connected before emitting ledger_closed events 2014-09-04 18:51:00 -07:00
Edward Hennis
64735e523f Clean up
* Make npm test runnable in Windows.
* Fix paths in README.md
* Ignore all build output files
2014-09-04 20:05:04 -04:00
Geert Weening
f126610219 [TASK] add publish to bower script 2014-09-04 16:11:59 -07:00
Geert Weening
2caef539ce [TASK] bump version to 0.8.0-rc3 2014-09-04 15:43:01 -07:00
Geert Weening
468fb87749 [DOC] update release notes 2014-09-04 15:42:36 -07:00
Geert Weening
4f4808ff15 Merge pull request #158 from justmoon/amount_constants
Amount: Constants should be static fields on the class, not a separate export.
2014-09-04 15:30:12 -07:00
Stefan Thomas
e6bbca7df1 [TASK] Amount: Constants should be static fields on the class, not a separate export. 2014-09-04 15:12:14 -07:00
26 changed files with 839 additions and 4835 deletions

4
.gitignore vendored
View File

@@ -17,7 +17,7 @@
# Ignore object files.
*.o
build/ripple*.js
build/*.js
tags
bin/rippled
Debug/*.*
@@ -51,4 +51,4 @@ test/config.js
npm-debug.log
# Ignore dist folder, build for bower
dist/
dist/

View File

@@ -1,3 +1,31 @@
##0.8.2
+ Currency: Allow mixed letters and numbers in currencies
+ Deprecate account_tx map/reduce/filter
+ Fix: correct requestLedger arguments
+ Fix: missing subscription on error events for some server methods
+ Fix: orderbook reset on reconnect
+ Fix: ripple-lib crashing. Add potential missing error handlers
##0.8.1
+ Wallet: Add Wallet class that generates wallets
+ Make npm test runnable in Windows.
+ Fix several stability issues, see merged PR's for details
+ Fix bug in Amount.to_human_full()
+ Fix undefined fee states when connecting to a rippled that is syncing
##0.8.0
+ Orderbook: Added tracking of offer funds for determining when offers are not funded
@@ -18,6 +46,8 @@
+ Amount: Add a constant for the maximum canonical value that can be expressed as a Ripple value
+ Amount: Make Constants static fields on the class, instead of a seperate export
##0.7.39

View File

@@ -38,7 +38,16 @@ JavaScript client for [rippled](https://github.com/ripple/rippled)
$ npm install ripple-lib
```
**Building ripple-lib for browser use**
**Via bower (for browser use)**
```
$ bower install ripple
```
See the [bower-ripple repo](https://github.com/ripple/bower-ripple) for additional bower instructions
**Building ripple-lib from github**
```
$ git clone https://github.com/ripple/ripple-lib
@@ -46,7 +55,7 @@ JavaScript client for [rippled](https://github.com/ripple/rippled)
$ npm run build
```
Then use the minified `build/ripple-*-min.js` in your webpage
Then use the minified `build/ripple-*-min.js`
##Quickstart
@@ -78,7 +87,7 @@ remote.connect(function() {
2. `cd` into the repository and install dependencies with `npm install`
3. `npm test` or `node_modules\.bin\mocha test\*-test.js`
3. `npm test` or `node_modules/.bin/mocha test/*-test.js`
**Generating code coverage**

File diff suppressed because it is too large Load Diff

View File

@@ -16,6 +16,17 @@ This file provides step-by-step walkthroughs for some of the most common usages
1. [The ripple-lib README](../README.md)
2. [The ripple-lib API Reference](REFERENCE.md)
##Generating a new Ripple Wallet
```js
var Wallet = require('ripple-lib').Wallet;
var wallet = Wallet.generate();
console.log(wallet);
// { address: 'rEf4sbVobiiDGExrNj2PkNHGMA8eS6jWh3',
// secret: 'shFh4a38EZpEdZxrLifEnVPAoBRce' }
```
##Connecting to the Ripple network
1. [Get ripple-lib](README.md#getting-ripple-lib)

View File

@@ -1,6 +1,6 @@
{
"name": "ripple-lib",
"version": "0.8.0-rc2",
"version": "0.8.2-rc2",
"description": "Ripple JavaScript client library",
"files": [
"src/js/*",
@@ -20,7 +20,8 @@
"extend": "~1.2.1",
"lru-cache": "~2.5.0",
"superagent": "^0.18.0",
"gulp-bump": "~0.1.10"
"gulp-bump": "~0.1.10",
"ripple-wallet-generator": "1.0.1"
},
"devDependencies": {
"mocha": "~1.14.0",
@@ -39,7 +40,7 @@
"scripts": {
"build": "node_modules/.bin/gulp",
"pretest": "node_modules/.bin/gulp concat-sjcl",
"test": "./node_modules/.bin/istanbul test -x build/sjcl.js -x src/js/jsbn/* ./node_modules/.bin/_mocha -- --reporter spec test/*-test.js",
"test": "./node_modules/.bin/istanbul test -x build/sjcl.js -x src/js/jsbn/* ./node_modules/mocha/bin/mocha -- --reporter spec test/*-test.js",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
},
"repository": {

43
scripts/publish Normal file
View File

@@ -0,0 +1,43 @@
echo "PUBLISH"
function exit_on_error {
res=$?
[[ ${res:-99} -eq 0 ]] || exit $res
}
rm -rf build
npm install
gulp
npm test
exit_on_error
echo ""
echo "publish to npm"
npm publish
exit_on_error
rm -rf dist
echo ""
echo "publish to bower"
git clone git@github.com:ripple/bower-ripple.git dist
gulp bower
exit_on_error
cd dist
version=$(cat bower.json | grep -Eo '([0-9]\.?)+(-rc[0-9])?')
echo "version: $version"
git add ripple.js ripple-debug.js ripple-min.js bower.json
exit_on_error
git commit -m "[TASK] add v$version"
exit_on_error
git tag "v$version"
exit_on_error
git push origin master
git push --tags origin master
cd ..

43
scripts/publish_rc Normal file
View File

@@ -0,0 +1,43 @@
echo "PUBLISH RELEASE CANDIDATE"
function exit_on_error {
res=$?
[[ ${res:-99} -eq 0 ]] || exit $res
}
rm -rf build
npm install
gulp
npm test
exit_on_error
echo ""
echo "publish rc to npm"
npm publish --tag beta
exit_on_error
rm -rf dist
echo ""
echo "publish to bower"
git clone git@github.com:ripple/bower-ripple.git dist
gulp bower
exit_on_error
cd dist
version=$(cat bower.json | grep -Eo '([0-9]\.?)+(-rc[0-9])?')
echo "version: $version"
git add ripple.js ripple-debug.js ripple-min.js bower.json
exit_on_error
git commit -m "[TASK] add v$version"
exit_on_error
git tag "v$version"
exit_on_error
git push origin master
git push --tags origin master
cd ..

12
scripts/publish_to_bower Normal file
View File

@@ -0,0 +1,12 @@
rm -rf dist
git clone git@github.com:ripple/bower-ripple.git dist
gulp bower
cd dist
version=$(cat bower.json | grep -Eo '([0-9]\.?)+(-rc[0-9])?')
echo "version: $version"
git add ripple.js ripple-debug.js ripple-min.js bower.json
git commit -m "[TASK] add v$version"
git tag "v$version"
git push origin master
git push --tags origin master
cd ..

View File

@@ -12,7 +12,25 @@ var UInt160 = require('./uint160').UInt160;
var Seed = require('./seed').Seed;
var Currency = require('./currency').Currency;
var consts = exports.consts = {
//
// Amount class in the style of Java's BigInteger class
// http://docs.oracle.com/javase/1.3/docs/api/java/math/BigInteger.html
//
function Amount() {
// Json format:
// integer : XRP
// { 'value' : ..., 'currency' : ..., 'issuer' : ...}
this._value = new BigInteger(); // NaN for bad value. Always positive.
this._offset = 0; // Always 0 for XRP.
this._is_native = true; // Default to XRP. Only valid if value is not NaN.
this._is_negative = false;
this._currency = new Currency();
this._issuer = new UInt160();
};
var consts = {
currency_xns: 0,
currency_one: 1,
xns_precision: 6,
@@ -39,24 +57,11 @@ var consts = exports.consts = {
max_value: '9999999999999999e80'
};
// Add constants to Amount class
extend(Amount, consts);
//
// Amount class in the style of Java's BigInteger class
// http://docs.oracle.com/javase/1.3/docs/api/java/math/BigInteger.html
//
function Amount() {
// Json format:
// integer : XRP
// { 'value' : ..., 'currency' : ..., 'issuer' : ...}
this._value = new BigInteger(); // NaN for bad value. Always positive.
this._offset = 0; // Always 0 for XRP.
this._is_native = true; // Default to XRP. Only valid if value is not NaN.
this._is_negative = false;
this._currency = new Currency();
this._issuer = new UInt160();
};
// DEPRECATED: Use Amount instead, e.g. Amount.currency_xns
exports.consts = consts;
// Given '100/USD/mtgox' return the a string with mtgox remapped.
Amount.text_full_rewrite = function(j) {
@@ -136,12 +141,12 @@ Amount.prototype.add = function(v) {
var o2 = v._offset;
while (o1 < o2) {
v1 = v1.divide(consts.bi_10);
v1 = v1.divide(Amount.bi_10);
o1 += 1;
}
while (o2 < o1) {
v2 = v2.divide(consts.bi_10);
v2 = v2.divide(Amount.bi_10);
o2 += 1;
}
@@ -189,22 +194,22 @@ Amount.prototype.multiply = function(v) {
var o2 = v._offset;
if (this.is_native()) {
while (v1.compareTo(consts.bi_man_min_value) < 0) {
v1 = v1.multiply(consts.bi_10);
while (v1.compareTo(Amount.bi_man_min_value) < 0) {
v1 = v1.multiply(Amount.bi_10);
o1 -= 1;
}
}
if (v.is_native()) {
while (v2.compareTo(consts.bi_man_min_value) < 0) {
v2 = v2.multiply(consts.bi_10);
while (v2.compareTo(Amount.bi_man_min_value) < 0) {
v2 = v2.multiply(Amount.bi_10);
o2 -= 1;
}
}
result = new Amount();
result._offset = o1 + o2 + 14;
result._value = v1.multiply(v2).divide(consts.bi_1e14).add(consts.bi_7);
result._value = v1.multiply(v2).divide(Amount.bi_1e14).add(Amount.bi_7);
result._is_native = this._is_native;
result._is_negative = this._is_negative !== v._is_negative;
result._currency = this._currency;
@@ -238,8 +243,8 @@ Amount.prototype.divide = function(d) {
if (_n.is_native()) {
_n = _n.clone();
while (_n._value.compareTo(consts.bi_man_min_value) < 0) {
_n._value = _n._value.multiply(consts.bi_10);
while (_n._value.compareTo(Amount.bi_man_min_value) < 0) {
_n._value = _n._value.multiply(Amount.bi_10);
_n._offset -= 1;
}
}
@@ -249,15 +254,15 @@ Amount.prototype.divide = function(d) {
if (_d.is_native()) {
_d = _d.clone();
while (_d._value.compareTo(consts.bi_man_min_value) < 0) {
_d._value = _d._value.multiply(consts.bi_10);
while (_d._value.compareTo(Amount.bi_man_min_value) < 0) {
_d._value = _d._value.multiply(Amount.bi_10);
_d._offset -= 1;
}
}
result = new Amount();
result._offset = _n._offset - _d._offset - 17;
result._value = _n._value.multiply(consts.bi_1e17).divide(_d._value).add(consts.bi_5);
result._value = _n._value.multiply(Amount.bi_1e17).divide(_d._value).add(Amount.bi_5);
result._is_native = _n._is_native;
result._is_negative = _n._is_negative !== _d._is_negative;
result._currency = _n._currency;
@@ -331,7 +336,7 @@ Amount.prototype.ratio_human = function(denominator, opts) {
// To compensate, we multiply the numerator by 10^xns_precision.
if (denominator._is_native) {
numerator = numerator.clone();
numerator._value = numerator._value.multiply(consts.bi_xns_unit);
numerator._value = numerator._value.multiply(Amount.bi_xns_unit);
numerator.canonicalize();
}
@@ -389,7 +394,7 @@ Amount.prototype.product_human = function(factor, opts) {
//
// See also Amount#ratio_human.
if (factor._is_native) {
product._value = product._value.divide(consts.bi_xns_unit);
product._value = product._value.divide(Amount.bi_xns_unit);
product.canonicalize();
}
@@ -402,7 +407,7 @@ Amount.prototype.product_human = function(factor, opts) {
* @private
*/
Amount.prototype._invert = function() {
this._value = consts.bi_1e32.divide(this._value);
this._value = Amount.bi_1e32.divide(this._value);
this._offset = -32 - this._offset;
this.canonicalize();
@@ -432,12 +437,12 @@ Amount.prototype.canonicalize = function() {
// Normalize _offset to 0.
while (this._offset < 0) {
this._value = this._value.divide(consts.bi_10);
this._value = this._value.divide(Amount.bi_10);
this._offset += 1;
}
while (this._offset > 0) {
this._value = this._value.multiply(consts.bi_10);
this._value = this._value.multiply(Amount.bi_10);
this._offset -= 1;
}
}
@@ -449,13 +454,13 @@ Amount.prototype.canonicalize = function() {
} else {
// Normalize mantissa to valid range.
while (this._value.compareTo(consts.bi_man_min_value) < 0) {
this._value = this._value.multiply(consts.bi_10);
while (this._value.compareTo(Amount.bi_man_min_value) < 0) {
this._value = this._value.multiply(Amount.bi_10);
this._offset -= 1;
}
while (this._value.compareTo(consts.bi_man_max_value) > 0) {
this._value = this._value.divide(consts.bi_10);
while (this._value.compareTo(Amount.bi_man_max_value) > 0) {
this._value = this._value.divide(Amount.bi_10);
this._offset += 1;
}
}
@@ -673,14 +678,14 @@ Amount.prototype.parse_human = function(j, opts) {
fraction += '0';
}
this._is_native = true;
this._value = this._value.multiply(consts.bi_xns_unit).add(new BigInteger(fraction));
this._value = this._value.multiply(Amount.bi_xns_unit).add(new BigInteger(fraction));
} else {
// Other currencies have arbitrary precision
fraction = fraction.replace(/0+$/, '');
precision = fraction.length;
this._is_native = false;
var multiplier = consts.bi_10.clone().pow(precision);
var multiplier = Amount.bi_10.clone().pow(precision);
this._value = this._value.multiply(multiplier).add(new BigInteger(fraction));
this._offset = -precision;
@@ -879,8 +884,8 @@ Amount.prototype.parse_native = function(j) {
this._value = new BigInteger(m[2]);
} else {
// Float notation : values multiplied by 1,000,000.
var int_part = (new BigInteger(m[2])).multiply(consts.bi_xns_unit);
var fraction_part = (new BigInteger(m[3])).multiply(new BigInteger(String(Math.pow(10, 1+consts.xns_precision-m[3].length))));
var int_part = (new BigInteger(m[2])).multiply(Amount.bi_xns_unit);
var fraction_part = (new BigInteger(m[3])).multiply(new BigInteger(String(Math.pow(10, 1+Amount.xns_precision-m[3].length))));
this._value = int_part.add(fraction_part);
}
@@ -889,7 +894,7 @@ Amount.prototype.parse_native = function(j) {
this._offset = 0;
this._is_negative = !!m[1] && this._value.compareTo(BigInteger.ZERO) !== 0;
if (this._value.compareTo(consts.bi_xns_max) > 0) {
if (this._value.compareTo(Amount.bi_xns_max) > 0) {
this._value = NaN;
}
} else {
@@ -931,7 +936,7 @@ Amount.prototype.parse_value = function(j) {
var fraction = new BigInteger(d[3]);
var precision = d[3].length;
this._value = integer.multiply(consts.bi_10.clone().pow(precision)).add(fraction);
this._value = integer.multiply(Amount.bi_10.clone().pow(precision)).add(fraction);
this._offset = -precision;
this._is_negative = !!d[1];
@@ -982,7 +987,7 @@ Amount.prototype.to_text = function(allow_nan) {
var result = NaN;
if (this._is_native) {
if (this.is_valid() && this._value.compareTo(consts.bi_xns_max) <= 0){
if (this.is_valid() && this._value.compareTo(Amount.bi_xns_max) <= 0){
result = this._value.toString();
}
} else if (this.is_zero()) {
@@ -1090,8 +1095,8 @@ Amount.prototype.to_human = function(opts) {
ref = this.applyInterest(opts.reference_date);
}
var order = ref._is_native ? consts.xns_precision : -ref._offset;
var denominator = consts.bi_10.clone().pow(order);
var order = ref._is_native ? Amount.xns_precision : -ref._offset;
var denominator = Amount.bi_10.clone().pow(order);
var int_part = ref._value.divide(denominator).toString();
var fraction_part = ref._value.mod(denominator).toString();
@@ -1174,7 +1179,7 @@ Amount.prototype.to_human_full = function(opts) {
var a = this.to_human(opts);
var c = this._currency.to_human();
var i = this._issuer.to_json(opts);
var o = this.is_native ? (o = a + '/' + c) : (o = a + '/' + c + '/' + i);
var o = this.is_native() ? (o = a + '/' + c) : (o = a + '/' + c + '/' + i);
return o;
};

View File

@@ -50,7 +50,7 @@ Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000';
* \s*$ // end with any amount of whitespace
*
*/
Currency.prototype.human_RE = /^\s*([a-zA-Z]{3}|[0-9]{3})(\s*-\s*[- \w]+)?(\s*\(-?\d+\.?\d*%pa\))?\s*$/;
Currency.prototype.human_RE = /^\s*([a-zA-Z0-9]{3})(\s*-\s*[- \w]+)?(\s*\(-?\d+\.?\d*%pa\))?\s*$/;
Currency.from_json = function(j, shouldInterpretXrpAsIou) {
if (j instanceof this) {

View File

@@ -18,6 +18,7 @@ exports.RippleTxt = require('./rippletxt').RippleTxt;
exports.binformat = require('./binformat');
exports.utils = require('./utils');
exports.Server = require('./server').Server;
exports.Wallet = require('./wallet');
// Important: We do not guarantee any specific version of SJCL or for any
// specific features to be included. The version and configuration may change at

View File

@@ -78,19 +78,20 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) {
});
this.on('unsubscribe', function() {
self._ownerFunds = { };
self.resetCache();
self._remote.removeListener('transaction', updateFundedAmounts);
self._remote.removeListener('transaction', updateTransferRate);
});
this._remote.on('prepare_subscribe', function() {
this._remote.once('prepare_subscribe', function() {
self.subscribe();
});
this._remote.on('disconnect', function() {
self._ownerFunds = { };
self._offerCounts = { };
self._synchronized = false;
self.resetCache();
self._remote.once('prepare_subscribe', function() {
self.subscribe();
});
});
function updateFundedAmounts(message) {
@@ -195,6 +196,16 @@ OrderBook.prototype.unsubscribe = function() {
this.emit('unsubscribe');
};
/**
* Reset cached owner funds, offer counts
*/
OrderBook.prototype.resetCache = function() {
this._ownerFunds = { };
this._offerCounts = { };
this._synchronized = false;
};
/**
* Check that the funds for offer owner have been cached
*
@@ -695,6 +706,9 @@ OrderBook.prototype.requestOffers = function(callback) {
log.info('requested offers', self._key, 'offers: ' + res.offers.length);
}
// Reset offers
self._offers = [ ];
for (var i=0, l=res.offers.length; i<l; i++) {
var offer = res.offers[i];
var fundedAmount;

View File

@@ -15,6 +15,7 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var assert = require('assert');
var LRU = require('lru-cache');
var Server = require('./server').Server;
var Request = require('./request').Request;
@@ -209,7 +210,7 @@ function Remote(opts, trace) {
function listenerAdded(type, listener) {
if (type === 'transaction_all') {
if (!self._transaction_subs && self._connected) {
if (!self._transaction_subs && self.isConnected()) {
self.request_subscribe('transactions').request();
}
self._transaction_subs += 1;
@@ -221,7 +222,7 @@ function Remote(opts, trace) {
function listenerRemoved(type, listener) {
if (type === 'transaction_all') {
self._transaction_subs -= 1;
if (!self._transaction_subs && self._connected) {
if (!self._transaction_subs && self.isConnected()) {
self.request_unsubscribe('transactions').request();
}
}
@@ -243,7 +244,7 @@ function Remote(opts, trace) {
};
if (opts.ping) {
this.once('connect', pingServers);
this.on('connect', pingServers);
}
function reconnect() {
@@ -338,13 +339,11 @@ Remote.isValidLedgerData = function(message) {
return (typeof message === 'object')
&& (typeof message.fee_base === 'number')
&& (typeof message.fee_ref === 'number')
&& (typeof message.fee_base === 'number')
&& (typeof message.ledger_hash === 'string')
&& (typeof message.ledger_index === 'number')
&& (typeof message.ledger_time === 'number')
&& (typeof message.reserve_base === 'number')
&& (typeof message.reserve_inc === 'number')
&& (typeof message.txn_count === 'number');
&& (typeof message.reserve_inc === 'number');
};
/**
@@ -514,12 +513,9 @@ Remote.prototype.reconnect = function() {
log.info('reconnecting');
;(function nextServer(i) {
self._servers[i].reconnect();
if (++i < self._servers.length) {
nextServer(i);
}
})(0);
this._servers.forEach(function(server) {
server.reconnect();
});
return this;
};
@@ -553,12 +549,9 @@ Remote.prototype.connect = function(online) {
this._should_connect = true;
;(function nextServer(i) {
self._servers[i].connect();
if (++i < self._servers.length) {
nextServer(i);
}
})(0);
this._servers.forEach(function(server) {
server.connect();
});
return this;
};
@@ -577,7 +570,7 @@ Remote.prototype.disconnect = function(callback) {
var callback = (typeof callback === 'function') ? callback : function(){};
if (!this._connected) {
if (!this.isConnected()) {
callback();
return this;
}
@@ -622,16 +615,16 @@ Remote.prototype._handleMessage = function(message, server) {
switch (message.type) {
case 'ledgerClosed':
this._handleLedgerClosed(message);
this._handleLedgerClosed(message, server);
break;
case 'serverStatus':
this._handleServerStatus(message);
this._handleServerStatus(message, server);
break;
case 'transaction':
this._handleTransaction(message);
this._handleTransaction(message, server);
break;
case 'path_find':
this._handlePathFind(message);
this._handlePathFind(message, server);
break;
default:
if (this.trace) {
@@ -647,7 +640,7 @@ Remote.prototype._handleMessage = function(message, server) {
* @param {Object} message
*/
Remote.prototype._handleLedgerClosed = function(message) {
Remote.prototype._handleLedgerClosed = function(message, server) {
var self = this;
// XXX If not trusted, need to verify we consider ledger closed.
@@ -660,11 +653,19 @@ Remote.prototype._handleLedgerClosed = function(message) {
var ledgerAdvanced = message.ledger_index >= this._ledger_current_index;
if (ledgerAdvanced) {
if (isNaN(this._ledger_current_index) || ledgerAdvanced) {
this._ledger_time = message.ledger_time;
this._ledger_hash = message.ledger_hash;
this._ledger_current_index = message.ledger_index + 1;
this.emit('ledger_closed', message);
if (this.isConnected()) {
this.emit('ledger_closed', message, server);
} else {
this.once('connect', function() {
// Delay until server is 'online'
self.emit('ledger_closed', message, server);
});
}
}
};
@@ -674,8 +675,8 @@ Remote.prototype._handleLedgerClosed = function(message) {
* @param {Object} message
*/
Remote.prototype._handleServerStatus = function(message) {
this.emit('server_status', message);
Remote.prototype._handleServerStatus = function(message, server) {
this.emit('server_status', message, server);
};
/**
@@ -684,7 +685,7 @@ Remote.prototype._handleServerStatus = function(message) {
* @param {Object} message
*/
Remote.prototype._handleTransaction = function(message) {
Remote.prototype._handleTransaction = function(message, server) {
var self = this;
// XXX If not trusted, need proof.
@@ -731,8 +732,8 @@ Remote.prototype._handleTransaction = function(message) {
});
}
this.emit('transaction', message);
this.emit('transaction_all', message);
this.emit('transaction', message, server);
this.emit('transaction_all', message, server);
};
/**
@@ -741,7 +742,7 @@ Remote.prototype._handleTransaction = function(message) {
* @param {Object} message
*/
Remote.prototype._handlePathFind = function(message) {
Remote.prototype._handlePathFind = function(message, server) {
var self = this;
// Pass the event to the currently open PathFind object
@@ -749,7 +750,7 @@ Remote.prototype._handlePathFind = function(message) {
this._cur_path_find.notify_update(message);
}
this.emit('path_find_all', message);
this.emit('path_find_all', message, server);
};
/**
@@ -819,6 +820,10 @@ Remote.prototype.getServer = function() {
}
var connectedServers = this.getConnectedServers();
if (connectedServers.length === 0 || !connectedServers[0]) {
return null;
}
var server = connectedServers[0];
var cScore = server._score + server._fee;
@@ -861,7 +866,7 @@ Remote.prototype.request = function(request) {
if (!this._servers.length) {
request.emit('error', new Error('No servers available'));
} else if (!this._connected) {
} else if (!this.isConnected()) {
this.once('connect', this.request.bind(this, request));
} else if (request.server === null) {
request.emit('error', new Error('Server does not exist'));
@@ -924,20 +929,19 @@ Remote.prototype.requestServerInfo = function(callback) {
* @return {Request} request
*/
Remote.prototype.requestLedger = function(ledger, options, callback) {
Remote.prototype.requestLedger = function(options, callback) {
// XXX This is a bad command. Some variants don't scale.
// XXX Require the server to be trusted.
//utils.assert(this.trusted);
var request = new Request(this, 'ledger');
if (ledger) {
// DEPRECATED: use .ledger_hash() or .ledger_index()
//console.log('request_ledger: ledger parameter is deprecated');
request.message.ledger = ledger;
}
switch (typeof options) {
case 'undefined': break;
case 'function':
callback = options;
break;
case 'object':
Object.keys(options).forEach(function(o) {
switch (o) {
@@ -945,23 +949,16 @@ Remote.prototype.requestLedger = function(ledger, options, callback) {
case 'expand':
case 'transactions':
case 'accounts':
case 'ledger_index':
case 'ledger_hash':
request.message[o] = true;
break;
}
}, options);
break;
case 'function':
callback = options;
options = void(0);
break;
default:
//DEPRECATED
if (this.trace) {
log.info('request_ledger: full parameter is deprecated');
}
request.message.full = true;
request.ledgerSelect(options);
break;
}
@@ -1332,6 +1329,10 @@ Remote.prototype.requestAccountTx = function(options, callback) {
options.ledger_index_max = options.max_ledger;
}
if (options.binary && options.parseBinary === void(0)) {
options.parseBinary = true;
}
Object.keys(options).forEach(function(o) {
switch (o) {
case 'account':
@@ -1351,101 +1352,32 @@ Remote.prototype.requestAccountTx = function(options, callback) {
}
}, options);
function propertiesFilter(obj, transaction) {
var properties = Object.keys(obj);
return function(transaction) {
var result = properties.every(function(property) {
return transaction.tx[property] === obj[property];
});
return result;
};
};
function parseBinaryTransaction(transaction) {
var tx = { validated: transaction.validated };
tx.meta = new SerializedObject(transaction.meta).to_json();
tx.tx = new SerializedObject(transaction.tx_blob).to_json();
tx.tx.ledger_index = transaction.ledger_index;
tx.tx.hash = Transaction.from_json(tx.tx).hash();
return tx;
};
function accountTxFilter(fn) {
if (typeof fn !== 'function') {
throw new Error('Missing filter function');
request.once('success', function(res) {
if (options.parseBinary) {
res.transactions = res.transactions.map(Remote.parseBinaryTransaction);
}
request.emit('transactions', res);
});
var self = this;
function filterHandler() {
var listeners = self.listeners('success');
self.removeAllListeners('success');
self.once('success', function(res) {
if (options.parseBinary) {
res.transactions = res.transactions.map(parseBinaryTransaction);
}
if (fn !== Boolean) {
res.transactions = res.transactions.filter(fn);
}
if (typeof options.map === 'function') {
res.transactions = res.transactions.map(options.map);
}
if (typeof options.reduce === 'function') {
res.transactions = res.transactions.reduce(options.reduce);
}
if (typeof options.pluck === 'string') {
res = res[options.pluck];
}
listeners.forEach(function(listener) {
listener.call(self, res);
});
});
};
this.once('request', filterHandler);
return this;
};
request.filter = accountTxFilter;
if (typeof options.parseBinary !== 'boolean') {
options.parseBinary = true;
}
if (options.binary || (options.map || options.reduce)) {
options.filter = options.filter || Boolean;
}
if (options.filter) {
switch (options.filter) {
case 'inbound':
request.filter(propertiesFilter({ Destination: options.account }));
break;
case 'outbound':
request.filter(propertiesFilter({ Account: options.account }));
break;
default:
if (typeof options.filter === 'object') {
options.filter = propertiesFilter(options.filter);
}
request.filter(options.filter);
}
}
request.callback(callback);
request.callback(callback, 'transactions');
return request;
};
/**
* @param {Object} transaction
* @return {Transaction}
*/
Remote.parseBinaryTransaction = function(transaction) {
var tx = { validated: transaction.validated };
tx.meta = new SerializedObject(transaction.meta).to_json();
tx.tx = new SerializedObject(transaction.tx_blob).to_json();
tx.tx.ledger_index = transaction.ledger_index;
tx.tx.hash = Transaction.from_json(tx.tx).hash();
return tx;
};
/**
* Request the overall transaction history.
*
@@ -1584,10 +1516,14 @@ Remote.prototype.requestSubmit = function(callback) {
* @api private
*/
Remote.prototype._serverPrepareSubscribe = function(callback) {
Remote.prototype._serverPrepareSubscribe = function(server, callback) {
var self = this;
var feeds = [ 'ledger', 'server' ];
if (typeof server === 'function') {
callback = server;
}
if (this._transaction_subs) {
feeds.push('transactions');
}
@@ -1608,16 +1544,17 @@ Remote.prototype._serverPrepareSubscribe = function(callback) {
self.emit('random', utils.hexToArray(message.random));
}
if (message.ledger_hash && message.ledger_index) {
self._ledger_time = message.ledger_time;
self._ledger_hash = message.ledger_hash;
self._ledger_current_index = message.ledger_index+1;
self.emit('ledger_closed', message);
}
self._handleLedgerClosed(message, server);
self.emit('subscribed');
};
request.on('error', function(err) {
if (self.trace) {
log.info('Initial server subscribe failed', err);
}
});
request.once('success', serverSubscribed);
self.emit('prepare_subscribe', request);
@@ -2215,65 +2152,40 @@ Remote.prototype.requestConnect = function(ip, port, callback) {
/**
* Create a Transaction
*
* @param {String} source
* @param {String} TransactionType
* @param {Object} options
* @param [Function] callback
* @return {Request}
* @return {Transaction}
*/
Remote.prototype.transaction =
Remote.prototype.createTransaction = function(source, options, callback) {
Remote.prototype.createTransaction = function(type, options) {
if (arguments.length === 0) {
// Fallback
return new Transaction(this);
}
assert.strictEqual(typeof type, 'string', 'TransactionType must be a string');
assert.strictEqual(typeof options, 'object', 'Transaction options must be an object');
var transaction = new Transaction(this);
var transactionTypes = {
payment: 'payment',
accountset: 'accountSet',
trustset: 'trustSet',
offercreate: 'offerCreate',
offercancel: 'offerCancel',
claim: 'claim',
passwordfund: 'passwordFund',
passwordset: 'passwordSet',
setregularkey: 'setRegularKey',
walletadd: 'walletAdd',
sign: 'sign'
payment: 'payment',
accountset: 'accountSet',
trustset: 'trustSet',
offercreate: 'offerCreate',
offercancel: 'offerCancel',
setregularkey: 'setRegularKey',
sign: 'sign'
};
var transactionType;
var transactionConstructor = transactionTypes[type.toLowerCase()];
switch (typeof source) {
case 'object':
if (typeof source.type !== 'string') {
throw new Error('Missing transaction type');
}
transactionType = transactionTypes[source.type.toLowerCase()];
if (!transactionType) {
throw new Error('Invalid transaction type: ' + transactionType);
}
transaction = transaction[transactionType](source);
break;
case 'string':
transactionType = transactionTypes[source.toLowerCase()];
if (!transactionType) {
throw new Error('Invalid transaction type: ' + transactionType);
}
transaction = transaction[transactionType](options);
break;
if (!transactionConstructor) {
throw new Error('Invalid transaction type: ' + type);
}
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg === 'function') {
transaction.submit(lastArg);
}
return transaction;
return transaction[transactionConstructor](options);
};
/**
@@ -2286,7 +2198,7 @@ Remote.prototype.createTransaction = function(source, options, callback) {
*/
Remote.prototype.feeTx = function(units) {
var server = this._getServer();
var server = this.getServer();
if (!server) {
throw new Error('No connected servers');
@@ -2305,7 +2217,7 @@ Remote.prototype.feeTx = function(units) {
*/
Remote.prototype.feeTxUnit = function() {
var server = this._getServer();
var server = this.getServer();
if (!server) {
throw new Error('No connected servers');
@@ -2324,7 +2236,7 @@ Remote.prototype.feeTxUnit = function() {
*/
Remote.prototype.reserve = function(owner_count) {
var server = this._getServer();
var server = this.getServer();
if (!server) {
throw new Error('No connected servers');

View File

@@ -33,8 +33,8 @@ Seed.prototype.parse_json = function (j) {
// XXX Should actually always try and continue if it failed.
} else if (j[0] === 's') {
this._value = Base.decode_check(Base.VER_FAMILY_SEED, j);
} else if (j.length === 32) {
this._value = this.parse_hex(j);
} else if (/^[0-9a-fA-f]{32}$/.test(j)) {
this.parse_hex(j);
// XXX Should also try 1751
} else {
this.parse_passphrase(j);

View File

@@ -117,7 +117,7 @@ SerializedType.prototype.parse_varint = function (so) {
*/
function append_byte_array(so, val, bytes) {
if (!isNumber(val)) {
throw new Error('Value is not a number');
throw new Error('Value is not a number', bytes);
}
if (val < 0 || val >= Math.pow(256, bytes)) {
@@ -625,7 +625,13 @@ function serialize(so, field_name, value) {
// Get the serializer class (ST...) for a field based on the type bits.
var serialized_object_type = exports[binformat.types[type_bits]];
//do something with val[keys] and val[keys[i]];
serialized_object_type.serialize(so, value);
try {
serialized_object_type.serialize(so, value);
} catch (e) {
e.message += ' (' + field_name + ')';
throw e;
}
}
//Take the serialized object, figure out what type/field it is, and return the parsing of that.

View File

@@ -134,7 +134,9 @@ function Server(remote, opts) {
}
});
self._request(self._remote.requestServerInfo());
var serverInfoRequest = self._remote.requestServerInfo();
serverInfoRequest.on('error', function() { });
self._request(serverInfoRequest);
});
};
@@ -184,7 +186,7 @@ Server.websocketConstructor = function() {
Server.prototype._setState = function(state) {
if (state !== this._state) {
if (this._remote.trace) {
log.info(this.getHostID(), 'set_state:', state);
log.info(this.getServerID(), 'set_state:', state);
}
this._state = state;
@@ -228,7 +230,7 @@ Server.prototype._checkActivity = function() {
if (delta > (1000 * 25)) {
if (this._remote.trace) {
log.info(this.getHostID(), 'reconnect: activity delta:', delta);
log.info(this.getServerID(), 'reconnect: activity delta:', delta);
}
this.reconnect();
}
@@ -274,7 +276,7 @@ Server.prototype._updateScore = function(type, data) {
if (this._score > 1e3) {
if (this._remote.trace) {
log.info(this.getHostID(), 'reconnect: score:', this._score);
log.info(this.getServerID(), 'reconnect: score:', this._score);
}
this.reconnect();
}
@@ -407,7 +409,7 @@ Server.prototype.connect = function() {
if (ws === self._ws) {
self.emit('socket_open');
// Subscribe to events
self._request(self._remote._serverPrepareSubscribe());
self._request(self._remote._serverPrepareSubscribe(self));
}
};
@@ -622,6 +624,7 @@ Server.prototype._handlePathFind = function(message) {
* Handle subscription response messages. Subscription response
* messages indicate that a connection to the server is ready
*
* @param {Object} message
* @api private
*/
@@ -632,18 +635,19 @@ Server.prototype._handleResponseSubscribe = function(message) {
// servers with incomplete history
return this.reconnect();
}
if (Server.isLoadStatus(message)) {
this._load_base = message.load_base || 256;
this._load_factor = message.load_factor || 256;
this._fee_ref = message.fee_ref;
this._fee_base = message.fee_base;
this._reserve_base = message.reserve_base;
this._reserve_inc = message.reserve_inc;
}
if (message.pubkey_node) {
// pubkey_node is used to identify the server
this._pubkey_node = message.pubkey_node;
}
if (~(Server.onlineStates.indexOf(message.server_status))) {
if (Server.isLoadStatus(message)) {
this._load_base = message.load_base || 256;
this._load_factor = message.load_factor || 256;
this._fee_ref = message.fee_ref || 10;
this._fee_base = message.fee_base || 10;
this._reserve_base = message.reserve_base;
this._reserve_inc = message.reserve_inc;
}
if (~Server.onlineStates.indexOf(message.server_status)) {
this._setState('online');
}
};

View File

@@ -480,7 +480,7 @@ Transaction.prototype.clientID = function(id) {
};
Transaction.prototype.lastLedger = function(sequence) {
if (typeof sequence === 'number') {
if (typeof sequence === 'number' && isFinite(sequence)) {
this._setLastLedger = true;
this.tx_json.LastLedgerSequence = sequence;
}

8
src/js/ripple/wallet.js Normal file
View File

@@ -0,0 +1,8 @@
var sjcl = require('./utils').sjcl;
var WalletGenerator = require('ripple-wallet-generator')({
sjcl: sjcl
});
module.exports = WalletGenerator;

View File

@@ -20,79 +20,151 @@ describe('Amount', function() {
it('1 XRP', function() {
assert.strictEqual(Amount.from_human("1 XRP").to_text_full(), '1/XRP');
});
it('1 XRP human', function() {
assert.strictEqual(Amount.from_human("1 XRP").to_human_full(), '1/XRP');
});
it('0.1 XRP', function() {
assert.strictEqual(Amount.from_human("0.1 XRP").to_text_full(), '0.1/XRP');
});
it('0.1 XRP human', function() {
assert.strictEqual(Amount.from_human("0.1 XRP").to_human_full(), '0.1/XRP');
});
it('0.1 USD', function() {
assert.strictEqual(Amount.from_human("0.1 USD").to_text_full(), '0.1/USD/NaN');
});
it('0.1 USD human', function() {
assert.strictEqual(Amount.from_human("0.1 USD").to_human_full(), '0.1/USD/NaN');
});
it('10000 USD', function() {
assert.strictEqual(Amount.from_human("10000 USD").to_text_full(), '10000/USD/NaN');
});
it('10000 USD human', function() {
assert.strictEqual(Amount.from_human("10000 USD").to_human_full(), '10,000/USD/NaN');
});
it('USD 10000', function() {
assert.strictEqual(Amount.from_human("USD 10000").to_text_full(), '10000/USD/NaN');
});
it('USD 10000 human', function() {
assert.strictEqual(Amount.from_human("USD 10000").to_human_full(), '10,000/USD/NaN');
});
it('12345.6789 XAU', function() {
assert.strictEqual(Amount.from_human("12345.6789 XAU").to_text_full(), '12345.6789/XAU/NaN');
});
it('12345.6789 XAU human', function() {
assert.strictEqual(Amount.from_human("12345.6789 XAU").to_human_full(), '12,345.6789/XAU/NaN');
});
it('12345.6789 015841551A748AD2C1F76FF6ECB0CCCD00000000', function() {
assert.strictEqual(Amount.from_human("12345.6789 015841551A748AD2C1F76FF6ECB0CCCD00000000").to_text_full(), '12345.6789/XAU (-0.5%pa)/NaN');
});
it('12345.6789 015841551A748AD2C1F76FF6ECB0CCCD00000000 human', function() {
assert.strictEqual(Amount.from_human("12345.6789 015841551A748AD2C1F76FF6ECB0CCCD00000000").to_human_full(), '12,345.6789/XAU (-0.5%pa)/NaN');
});
it('12345.6789 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("12345.6789 0000000000000000000000005553440000000000").to_text_full(), '12345.6789/USD/NaN');
});
it('12345.6789 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("12345.6789 0000000000000000000000005553440000000000").to_human_full(), '12,345.6789/USD/NaN');
});
it('10 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("10 0000000000000000000000005553440000000000").to_text_full(), '10/USD/NaN');
});
it('10 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("10 0000000000000000000000005553440000000000").to_human_full(), '10/USD/NaN');
});
it('100 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("100 0000000000000000000000005553440000000000").to_text_full(), '100/USD/NaN');
});
it('100 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("100 0000000000000000000000005553440000000000").to_human_full(), '100/USD/NaN');
});
it('1000 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("1000 0000000000000000000000005553440000000000").to_text_full(), '1000/USD/NaN');
});
it('1000 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("1000 0000000000000000000000005553440000000000").to_human_full(), '1,000/USD/NaN');
});
it('-100 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("-100 0000000000000000000000005553440000000000").to_text_full(), '-100/USD/NaN');
});
it('-100 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("-100 0000000000000000000000005553440000000000").to_human_full(), '-100/USD/NaN');
});
it('-1000 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("-1000 0000000000000000000000005553440000000000").to_text_full(), '-1000/USD/NaN');
});
it('-1000 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("-1000 0000000000000000000000005553440000000000").to_human_full(), '-1,000/USD/NaN');
});
it('-1000.001 0000000000000000000000005553440000000000', function() {
assert.strictEqual(Amount.from_human("-1000.001 0000000000000000000000005553440000000000").to_text_full(), '-1000.001/USD/NaN');
});
it('-1000.001 0000000000000000000000005553440000000000 human', function() {
assert.strictEqual(Amount.from_human("-1000.001 0000000000000000000000005553440000000000").to_human_full(), '-1,000.001/USD/NaN');
});
it('XAU 12345.6789', function() {
assert.strictEqual(Amount.from_human("XAU 12345.6789").to_text_full(), '12345.6789/XAU/NaN');
});
it('XAU 12345.6789 human', function() {
assert.strictEqual(Amount.from_human("XAU 12345.6789").to_human_full(), '12,345.6789/XAU/NaN');
});
it('101 12345.6789', function() {
assert.strictEqual(Amount.from_human("101 12345.6789").to_text_full(), '12345.6789/101/NaN');
});
it('101 12345.6789 human', function() {
assert.strictEqual(Amount.from_human("101 12345.6789").to_human_full(), '12,345.6789/101/NaN');
});
it('12345.6789 101', function() {
assert.strictEqual(Amount.from_human("12345.6789 101").to_text_full(), '12345.6789/101/NaN');
});
it('12345.6789 101 human', function() {
assert.strictEqual(Amount.from_human("12345.6789 101").to_human_full(), '12,345.6789/101/NaN');
});
});
describe('from_json', function() {
it('1 XRP', function() {
assert.strictEqual(Amount.from_json("1/XRP").to_text_full(), "1/XRP/NaN");
});
it('1 XRP human', function() {
assert.strictEqual(Amount.from_json("1/XRP").to_human_full(), "1/XRP/NaN");
});
});
describe('from_number', function() {
it('Number 1', function() {
assert.strictEqual(Amount.from_number(1).to_text_full(), '1/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Number 1 human', function() {
assert.strictEqual(Amount.from_number(1).to_human_full(), '1/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Number 2', function() {
assert.strictEqual(Amount.from_number(2).to_text_full(), '2/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Number 2 human', function() {
assert.strictEqual(Amount.from_number(2).to_human_full(), '2/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Multiply 2 "1" with 3 "1", by product_human', function () {
assert.strictEqual(Amount.from_number(2).product_human(Amount.from_number(3)).to_text_full(), '6/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Multiply 2 "1" with 3 "1", by product_human human', function () {
assert.strictEqual(Amount.from_number(2).product_human(Amount.from_number(3)).to_human_full(), '6/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Multiply 3 USD with 3 "1"', function () {
assert.strictEqual(Amount.from_json('3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_number(3)).to_text_full(), '9/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply 3 USD with 3 "1" human', function () {
assert.strictEqual(Amount.from_json('3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_number(3)).to_human_full(), '9/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply -1 "1" with 3 USD', function () {
assert.strictEqual(Amount.from_number(-1).multiply(Amount.from_json('3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_text_full(), '-3/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Multiply -1 "1" with 3 USD human', function () {
assert.strictEqual(Amount.from_number(-1).multiply(Amount.from_json('3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full(), '-3/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Multiply -1 "1" with 3 USD, by product_human', function () {
assert.strictEqual(Amount.from_number(-1).product_human(Amount.from_json('3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_text_full(), '-3/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Multiply -1 "1" with 3 USD, by product_human human', function () {
assert.strictEqual(Amount.from_number(-1).product_human(Amount.from_json('3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full(), '-3/1/rrrrrrrrrrrrrrrrrrrrBZbvji');
});
});
describe('text_full_rewrite', function() {
it('Number 1', function() {
@@ -162,12 +234,21 @@ describe('Amount', function() {
it('parse dem', function() {
assert.strictEqual(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('parse dem human', function() {
assert.strictEqual(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('parse dem', function() {
assert.strictEqual(Amount.from_json('10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('parse dem human', function() {
assert.strictEqual(Amount.from_json('10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Parse 800/USD/mtgox', function () {
assert.strictEqual('800/USD/'+config.accounts['mtgox'].account, Amount.from_json('800/USD/mtgox').to_text_full());
});
it('Parse 800/USD/mtgox human', function () {
assert.strictEqual('800/USD/'+config.accounts['mtgox'].account, Amount.from_json('800/USD/mtgox').to_human_full());
});
it('Parse native 0', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').to_text_full());
});
@@ -211,7 +292,52 @@ describe('Amount', function() {
assert.strictEqual('0/111/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/111/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full());
});
it('Parse 0.0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function () {
assert.strictEqual('0/XRP/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full());
assert.strictEqual('0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full());
});
it('Parse native 0 human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').to_human_full());
});
it('Parse native 0.0 human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0.0').to_human_full());
});
it('Parse native -0 human', function () {
assert.strictEqual('0/XRP', Amount.from_json('-0').to_human_full());
});
it('Parse native -0.0 human', function () {
assert.strictEqual('0/XRP', Amount.from_json('-0.0').to_human_full());
});
it('Parse native 1000 human', function () {
assert.strictEqual('0.001/XRP', Amount.from_json('1000').to_human_full());
});
it('Parse native 12.3 human', function () {
assert.strictEqual('12.3/XRP', Amount.from_json('12.3').to_human_full());
});
it('Parse native -12.3 human', function () {
assert.strictEqual('-12.3/XRP', Amount.from_json('-12.3').to_human_full());
});
it('Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse 12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('12,300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse 12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse 1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('1.23/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse -0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse -0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse 0.0/111/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('0/111/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/111/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
it('Parse 0.0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () {
assert.strictEqual('0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/12D/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full());
});
});
describe('Amount to_json', function() {
@@ -370,6 +496,144 @@ describe('Amount', function() {
it('Divide EUR by XRP, neg, <1', function () {
assert.strictEqual('-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('2000')).to_text_full());
});
it('Negate native 123 human', function () {
assert.strictEqual('-0.000123/XRP', Amount.from_json('123').negate().to_human_full());
});
it('Negate native -123 human', function () {
assert.strictEqual('0.000123/XRP', Amount.from_json('-123').negate().to_human_full());
});
it('Negate non-native 123 human', function () {
assert.strictEqual('-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').negate().to_human_full());
});
it('Negate non-native -123 human', function () {
assert.strictEqual('123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').negate().to_human_full());
});
it('Clone non-native -123 human', function () {
assert.strictEqual('-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').clone().to_human_full());
});
it('Add XRP to XRP human', function () {
assert.strictEqual('0.0002/XRP', Amount.from_json('150').add(Amount.from_json('50')).to_human_full());
});
it('Add USD to USD human', function () {
assert.strictEqual('200.52/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('150.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').add(Amount.from_json('50.5/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Add 0 USD to 1 USD human', function() {
assert.strictEqual('1' , Amount.from_json('1/USD').add('0/USD').to_human());
});
it('Subtract USD from USD human', function() {
assert.strictEqual('99.52/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('150.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').subtract(Amount.from_json('50.5/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply 0 XRP with 0 XRP human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').multiply(Amount.from_json('0')).to_human_full());
});
it('Multiply 0 USD with 0 XRP human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('0')).to_human_full());
});
it('Multiply 0 XRP with 0 USD human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').multiply(Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply 1 XRP with 0 XRP human', function () {
assert.strictEqual('0/XRP', Amount.from_json('1').multiply(Amount.from_json('0')).to_human_full());
});
it('Multiply 1 USD with 0 XRP human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('0')).to_human_full());
});
it('Multiply 1 XRP with 0 USD human', function () {
assert.strictEqual('0/XRP', Amount.from_json('1').multiply(Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply 0 XRP with 1 XRP human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').multiply(Amount.from_json('1')).to_human_full());
});
it('Multiply 0 USD with 1 XRP human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('1')).to_human_full());
});
it('Multiply 0 XRP with 1 USD human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').multiply(Amount.from_json('1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD human', function () {
assert.equal('0.002/XRP', Amount.from_json('200').multiply(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD human', function () {
assert.strictEqual('0.2/XRP', Amount.from_json('20000').multiply(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD human', function () {
assert.strictEqual('20/XRP', Amount.from_json('2000000').multiply(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD, neg human', function () {
assert.strictEqual('-0.002/XRP', Amount.from_json('200').multiply(Amount.from_json('-10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD, neg, frac human', function () {
assert.strictEqual('-0.222/XRP', Amount.from_json('-6000').multiply(Amount.from_json('37/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply USD with USD human', function () {
assert.strictEqual('20,000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply USD with USD human', function () {
assert.strictEqual('200,000,000,000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('100000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply EUR with USD, result < 1 human', function () {
assert.strictEqual('100,000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply EUR with USD, neg human', function () {
assert.strictEqual('-48,000,000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-24000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply EUR with USD, neg, <1 human', function () {
assert.strictEqual('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('-1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply EUR with XRP, factor < 1 human', function () {
assert.strictEqual('100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('2000')).to_human_full());
});
it('Multiply EUR with XRP, neg human', function () {
assert.strictEqual('-500/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('5')).to_human_full());
});
it('Multiply EUR with XRP, neg, <1 human', function () {
assert.strictEqual('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').multiply(Amount.from_json('2000')).to_human_full());
});
it('Multiply XRP with XRP human', function () {
assert.strictEqual('0.0001/XRP', Amount.from_json('10').multiply(Amount.from_json('10')).to_human_full());
});
it('Divide XRP by USD human', function () {
assert.strictEqual('0.00002/XRP', Amount.from_json('200').divide(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide XRP by USD human', function () {
assert.strictEqual('0.002/XRP', Amount.from_json('20000').divide(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide XRP by USD human', function () {
assert.strictEqual('0.2/XRP', Amount.from_json('2000000').divide(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide XRP by USD, neg human', function () {
assert.strictEqual('-0.00002/XRP', Amount.from_json('200').divide(Amount.from_json('-10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide XRP by USD, neg, frac human', function () {
assert.strictEqual('-0.000162/XRP', Amount.from_json('-6000').divide(Amount.from_json('37/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide USD by USD human', function () {
assert.strictEqual('200/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide USD by USD, fractional human', function () {
assert.strictEqual('57,142.85714285714/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('35/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide USD by USD human', function () {
assert.strictEqual('20/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('100000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide EUR by USD, factor < 1 human', function () {
assert.strictEqual('0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide EUR by USD, neg human', function () {
assert.strictEqual('-12/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-24000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide EUR by USD, neg, <1 human', function () {
assert.strictEqual('-0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('-1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Divide EUR by XRP, result < 1 human', function () {
assert.strictEqual('0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('2000')).to_human_full());
});
it('Divide EUR by XRP, neg human', function () {
assert.strictEqual('-20/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('5')).to_human_full());
});
it('Divide EUR by XRP, neg, <1 human', function () {
assert.strictEqual('-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').divide(Amount.from_json('2000')).to_human_full());
});
it('Divide by zero should throw', function() {
assert.throws(function() {
Amount.from_json(1).divide(Amount.from_json(0));
@@ -607,12 +871,87 @@ describe('Amount', function() {
it('Multiply USD with XAU (dem)', function () {
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_text_full(), '19900.00316303882/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply 0 XRP with 0 XRP human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').product_human(Amount.from_json('0')).to_human_full());
});
it('Multiply 0 USD with 0 XRP human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('0')).to_human_full());
});
it('Multiply 0 XRP with 0 USD human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').product_human(Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply 1 XRP with 0 XRP human', function () {
assert.strictEqual('0/XRP', Amount.from_json('1').product_human(Amount.from_json('0')).to_human_full());
});
it('Multiply 1 USD with 0 XRP human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('0')).to_human_full());
});
it('Multiply 1 XRP with 0 USD human', function () {
assert.strictEqual('0/XRP', Amount.from_json('1').product_human(Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply 0 XRP with 1 XRP human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').product_human(Amount.from_json('1')).to_human_full());
});
it('Multiply 0 USD with 1 XRP human', function () {
assert.strictEqual('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('1')).to_human_full());
});
it('Multiply 0 XRP with 1 USD human', function () {
assert.strictEqual('0/XRP', Amount.from_json('0').product_human(Amount.from_json('1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD human', function () {
assert.equal('0.002/XRP', Amount.from_json('200').product_human(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD human', function () {
assert.strictEqual('0.2/XRP', Amount.from_json('20000').product_human(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD human', function () {
assert.strictEqual('20/XRP', Amount.from_json('2000000').product_human(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD, neg human', function () {
assert.strictEqual('-0.002/XRP', Amount.from_json('200').product_human(Amount.from_json('-10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply XRP with USD, neg, frac human', function () {
assert.strictEqual('-0.222/XRP', Amount.from_json('-6000').product_human(Amount.from_json('37/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply USD with USD human', function () {
assert.strictEqual('20,000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply USD with USD human', function () {
assert.strictEqual('200,000,000,000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('100000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply EUR with USD, result < 1 human', function () {
assert.strictEqual('100,000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full());
});
it('Multiply EUR with USD, neg human', function () {
assert.strictEqual(Amount.from_json('-24000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full(), '-48,000,000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply EUR with USD, neg, <1 human', function () {
assert.strictEqual(Amount.from_json('0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('-1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh')).to_human_full(), '-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply EUR with XRP, factor < 1 human', function () {
assert.strictEqual(Amount.from_json('0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('2000')).to_human_full(), '0.0001/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply EUR with XRP, neg human', function () {
assert.strictEqual(Amount.from_json('-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('5')).to_human_full(), '-0.0005/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply EUR with XRP, neg, <1 human', function () {
assert.strictEqual(Amount.from_json('-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('2000')).to_human_full(), '-0.0001/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Multiply XRP with XRP human', function () {
assert.strictEqual(Amount.from_json('10000000').product_human(Amount.from_json('10')).to_human_full(), '0.0001/XRP');
});
it('Multiply USD with XAU (dem) human', function () {
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').product_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_human_full(), '19,900.00316303882/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
});
describe('ratio_human', function() {
it('Divide USD by XAU (dem)', function () {
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').ratio_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_text_full(), '201.0049931765529/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Divide USD by XAU (dem) human', function () {
assert.strictEqual(Amount.from_json('2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').ratio_human(Amount.from_json('10/015841551A748AD2C1F76FF6ECB0CCCD00000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh'), {reference_date: 443845330 + 31535000}).to_human_full(), '201.0049931765529/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
});
describe('_invert', function() {
@@ -625,6 +964,15 @@ describe('Amount', function() {
it('Invert 0.02', function () {
assert.strictEqual(Amount.from_json('0.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_text_full(), '50/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Invert 1 human', function () {
assert.strictEqual(Amount.from_json('1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_human_full(), '1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Invert 20 human', function () {
assert.strictEqual(Amount.from_json('20/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_human_full(), '0.05/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it('Invert 0.02 human', function () {
assert.strictEqual(Amount.from_json('0.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').invert().to_human_full(), '50/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
});
describe('from_quality', function() {
@@ -658,6 +1006,36 @@ describe('Amount', function() {
it('USD/XAU(dem) inverse', function () {
assert.strictEqual(Amount.from_quality('CDFD3AFB2F8C5DBEF75B081F7C957FF5509563266F28F36C5704A0FB0BAD8800', '015841551A748AD2C1F76FF6ECB0CCCD00000000', 'rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN', {inverse: true, base_currency: 'USD', reference_date: 443845330 + 31535000}).to_text_full(), '0.007675186123263489/XAU (-0.5%pa)/rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN');
});
it('BTC/XRP human', function () {
assert.strictEqual(Amount.from_quality('7B73A610A009249B0CC0D4311E8BA7927B5A34D86634581C5F0FF9FF678E1000', 'XRP', NaN, {base_currency: 'BTC'}).to_human_full(), '44,970/XRP');
});
it('BTC/XRP inverse human', function () {
assert.strictEqual(Amount.from_quality('37AAC93D336021AE94310D0430FFA090F7137C97D473488C4A0918D0DEF8624E', 'XRP', NaN, {inverse: true, base_currency: 'BTC'}).to_human_full(), '39,053.954453/XRP');
});
it('XRP/USD human', function () {
assert.strictEqual(Amount.from_quality('DFA3B6DDAB58C7E8E5D944E736DA4B7046C30E4F460FD9DE4D05DCAA8FE12000', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {base_currency: 'XRP'}).to_human_full(), '0.0165/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
});
it('XRP/USD inverse human', function () {
assert.strictEqual(Amount.from_quality('4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5C22A840E27DCA9B', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {inverse: true, base_currency: 'XRP'}).to_human_full(), '0.010251/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
});
it('BTC/USD human', function () {
assert.strictEqual(Amount.from_quality('6EAB7C172DEFA430DBFAD120FDC373B5F5AF8B191649EC9858038D7EA4C68000', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {base_currency: 'BTC'}).to_human_full(), '1,000/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
});
it('BTC/USD inverse human', function () {
assert.strictEqual(Amount.from_quality('20294C923E80A51B487EB9547B3835FD483748B170D2D0A455071AFD498D0000', 'USD', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', {inverse: true, base_currency: 'BTC'}).to_human_full(), '0.5/USD/rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B');
});
it('XAU(dem)/XRP human', function () {
assert.strictEqual(Amount.from_quality('587322CCBDE0ABD01704769A73A077C32FB39057D813D4165F1FF973CAF997EF', 'XRP', NaN, {base_currency: '015841551A748AD2C1F76FF6ECB0CCCD00000000', reference_date: 443845330 + 31535000}).to_human_full(), '90,452.246928/XRP');
});
it('XAU(dem)/XRP inverse human', function () {
assert.strictEqual(Amount.from_quality('F72C7A9EAE4A45ED1FB547AD037D07B9B965C6E662BEBAFA4A03F2A976804235', 'XRP', NaN, {inverse: true, base_currency: '015841551A748AD2C1F76FF6ECB0CCCD00000000', reference_date: 443845330 + 31535000}).to_human_full(), '90,442.196677/XRP');
});
it('USD/XAU(dem) human', function () {
assert.strictEqual(Amount.from_quality('4743E58E44974B325D42FD2BB683A6E36950F350EE46DD3A521B644B99782F5F', '015841551A748AD2C1F76FF6ECB0CCCD00000000', 'rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN', {base_currency: 'USD', reference_date: 443845330 + 31535000}).to_human_full(), '0.007710100231303007/XAU (-0.5%pa)/rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN');
});
it('USD/XAU(dem) inverse human', function () {
assert.strictEqual(Amount.from_quality('CDFD3AFB2F8C5DBEF75B081F7C957FF5509563266F28F36C5704A0FB0BAD8800', '015841551A748AD2C1F76FF6ECB0CCCD00000000', 'rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN', {inverse: true, base_currency: 'USD', reference_date: 443845330 + 31535000}).to_human_full(), '0.007675186123263489/XAU (-0.5%pa)/rUyPiNcSFFj6uMR2gEaD8jUerQ59G1qvwN');
});
});
describe('apply interest', function() {
@@ -679,5 +1057,23 @@ describe('Amount', function() {
demAmount.set_issuer("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh");
assert.strictEqual(demAmount.to_text_full(), '10.75853086191915/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it ('from_json apply interest 10 XAU human', function() {
var demAmount = Amount.from_json('10/0158415500000000C1F76FF6ECB0BAC600000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
assert.strictEqual(demAmount.to_human_full(), '10/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
demAmount = demAmount.applyInterest(459990264);
assert.strictEqual(demAmount.to_human_full(), '9.294949401870435/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it ('from_json apply interest XAU human', function() {
var demAmount = Amount.from_json('1235.5/0158415500000000C1F76FF6ECB0BAC600000000/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
assert.strictEqual(demAmount.to_human_full(), '1,235.5/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
demAmount = demAmount.applyInterest(459990264);
assert.strictEqual(demAmount.to_human_full(), '1,148.390998601092/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
it ('from_human with reference date human', function() {
var demAmount = Amount.from_human('10 0158415500000000C1F76FF6ECB0BAC600000000', {reference_date:459990264});
demAmount.set_issuer("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh");
assert.strictEqual(demAmount.to_human_full(), '10.75853086191915/XAU (-0.5%pa)/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
});
});
});

View File

@@ -40,8 +40,8 @@ describe('Currency', function() {
});
it('from_json("1D2").to_human()', function() {
var r = currency.from_json("1D2");
assert(!r.is_valid());
assert.strictEqual('XRP', r.to_json());
assert(r.is_valid());
assert.strictEqual('1D2', r.to_json());
});
it('from_json("XAU").to_json() hex', function() {
var r = currency.from_json("XAU");

View File

@@ -82,8 +82,17 @@ describe('Message', function(){
});
it('should throw an error if given an invalid secret key', function(){
// Annoyingly non hex can be fed to the BigInteger(s, 16) constructor and
// it will parse as a number. Before the commit of this comment, this test
// involved a fixture of 32 chars, which was assumed to be hex. The test
// passed, but for the wrong wreasons. There was a bug in Seed.parse_json.
var secret_string = 'badsafRpB5euNL52PZPTSqrE9gvuFwTC';
// Seed.from_json only creates invalid seeds from empty strings or invalid
// base58 starting with an s, which it tries to base 58 decode/check sum.
// The rest will be assumed to be a passphrase.
// This is a bad b58 seed
var secret_string = 'sbadsafRpB5euNL52PZPTSqrE9gvuFwTC';
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
assert.throws(function(){

View File

@@ -1546,7 +1546,7 @@ describe('OrderBook', function() {
]
book.once('model', function(model) {
book.on('model', function(model) {
assert.deepEqual(model, expected);
assert.strictEqual(book._synchronized, true);
done();

View File

@@ -530,6 +530,51 @@ describe('Server', function() {
server.connect();
});
it('Connect - syncing state', function(done) {
// Test that fee and load defaults are not overwritten by
// undefined properties on server subscribe response
var wss = new ws.Server({ port: 5748 });
wss.once('connection', function(ws) {
ws.once('message', function(message) {
var m = JSON.parse(message);
assert.deepEqual(m, {
command: 'subscribe',
id: 0,
streams: [ 'ledger', 'server' ]
});
ws.send(JSON.stringify({
id: 0,
status: 'success',
type: 'response',
result: {
load_base: 256,
load_factor: 256,
server_status: 'syncing'
}
}));
wss.close();
});
});
var server = new Server(new Remote(), 'ws://localhost:5748');
server.once('connect', function() {
assert(server.isConnected());
assert.strictEqual(server._load_base, 256);
assert.strictEqual(server._load_factor, 256);
assert.strictEqual(server._fee_base, 10);
assert.strictEqual(server._fee_ref, 10);
done();
});
server.connect();
});
it('Reconnect', function(done) {
var server = new Server(new Remote(), 'ws://localhost:5748');
server._connected = true;

View File

@@ -26,8 +26,9 @@ describe('Signing', function() {
assert(_isNaN(new Seed().parse_json('').to_json()));
});
it('hex string', function() {
// 32 0s is a valid hex repr of seed bytes
var str = new Array(33).join('0');
assert(_isNaN(new Seed().parse_json(str).to_json()));
assert.strictEqual((new Seed().parse_json(str).to_json()), 'sp6JS7f14BuwFY8Mw6bTtLKWauoUs');
});
it('passphrase', function() {
var str = new Array(60).join('0');

View File

@@ -811,6 +811,10 @@ describe('Transaction', function() {
assert.strictEqual(transaction.tx_json.LastLedgerSequence, void(0));
assert(!transaction._setLastLedger);
transaction.lastLedger(NaN);
assert.strictEqual(transaction.tx_json.LastLedgerSequence, void(0));
assert(!transaction._setLastLedger);
transaction.lastLedger(12);
assert.strictEqual(transaction.tx_json.LastLedgerSequence, 12);
assert(transaction._setLastLedger);