Convert API to promises

This commit is contained in:
Chris Clark
2015-07-31 11:45:53 -07:00
parent 0afca5633d
commit bbd51a03b6
25 changed files with 365 additions and 274 deletions

19
npm-shrinkwrap.json generated
View File

@@ -9,12 +9,12 @@
"resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz" "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz"
}, },
"babel-runtime": { "babel-runtime": {
"version": "5.8.19", "version": "5.8.20",
"resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.19.tgz", "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.20.tgz",
"dependencies": { "dependencies": {
"core-js": { "core-js": {
"version": "0.9.18", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-0.9.18.tgz" "resolved": "https://registry.npmjs.org/core-js/-/core-js-1.0.1.tgz"
} }
} }
}, },
@@ -22,6 +22,16 @@
"version": "2.0.7", "version": "2.0.7",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-2.0.7.tgz" "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-2.0.7.tgz"
}, },
"es6-promisify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-2.0.0.tgz",
"dependencies": {
"es6-promise": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-2.3.0.tgz"
}
}
},
"extend": { "extend": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/extend/-/extend-1.2.1.tgz" "resolved": "https://registry.npmjs.org/extend/-/extend-1.2.1.tgz"
@@ -108,7 +118,6 @@
}, },
"sjcl-extended": { "sjcl-extended": {
"version": "1.0.3", "version": "1.0.3",
"from": "sjcl-extended@git://github.com/ripple/sjcl-extended.git#d8cf8b22e7d97193c54e1f65113e3edcf200ca17",
"resolved": "git://github.com/ripple/sjcl-extended.git#d8cf8b22e7d97193c54e1f65113e3edcf200ca17", "resolved": "git://github.com/ripple/sjcl-extended.git#d8cf8b22e7d97193c54e1f65113e3edcf200ca17",
"dependencies": { "dependencies": {
"sjcl": { "sjcl": {

View File

@@ -18,6 +18,7 @@
"async": "~0.9.0", "async": "~0.9.0",
"babel-runtime": "^5.5.4", "babel-runtime": "^5.5.4",
"bignumber.js": "^2.0.3", "bignumber.js": "^2.0.3",
"es6-promisify": "^2.0.0",
"extend": "~1.2.1", "extend": "~1.2.1",
"https-proxy-agent": "^1.0.0", "https-proxy-agent": "^1.0.0",
"is-my-json-valid": "^2.12.0", "is-my-json-valid": "^2.12.0",

View File

@@ -9,8 +9,10 @@ module.exports = {
dropsToXrp: utils.dropsToXrp, dropsToXrp: utils.dropsToXrp,
xrpToDrops: utils.xrpToDrops, xrpToDrops: utils.xrpToDrops,
toRippledAmount: utils.toRippledAmount, toRippledAmount: utils.toRippledAmount,
wrapCatch: utils.wrapCatch,
composeAsync: utils.composeAsync, composeAsync: utils.composeAsync,
wrapCatch: utils.wrapCatch,
convertExceptions: utils.convertExceptions, convertExceptions: utils.convertExceptions,
convertKeysFromSnakeCaseToCamelCase: utils.convertKeysFromSnakeCaseToCamelCase convertKeysFromSnakeCaseToCamelCase:
utils.convertKeysFromSnakeCaseToCamelCase,
promisify: utils.promisify
}; };

View File

@@ -10,6 +10,6 @@
"address": {"$ref": "address"}, "address": {"$ref": "address"},
"sequence": {"$ref": "sequence"} "sequence": {"$ref": "sequence"}
}, },
"required": ["type", "id", "address", "sequence", "specification", "outcome"], "required": ["type", "id", "address", "sequence", "specification"],
"additionalProperties": false "additionalProperties": false
} }

View File

@@ -4,6 +4,7 @@ const _ = require('lodash');
const BigNumber = require('bignumber.js'); const BigNumber = require('bignumber.js');
const core = require('../../core'); const core = require('../../core');
const errors = require('./errors'); const errors = require('./errors');
const es6promisify = require('es6-promisify');
type Amount = {currency: string, issuer: string, value: string} type Amount = {currency: string, issuer: string, value: string}
@@ -85,13 +86,18 @@ function convertKeysFromSnakeCaseToCamelCase(obj: any): any {
return obj; return obj;
} }
function promisify<T>(asyncFunction: AsyncFunction): Function {
return es6promisify(wrapCatch(asyncFunction));
}
module.exports = { module.exports = {
core, core,
dropsToXrp, dropsToXrp,
xrpToDrops, xrpToDrops,
toRippledAmount, toRippledAmount,
wrapCatch,
composeAsync, composeAsync,
wrapCatch,
convertExceptions, convertExceptions,
convertKeysFromSnakeCaseToCamelCase convertKeysFromSnakeCaseToCamelCase,
promisify
}; };

View File

@@ -18,7 +18,7 @@ function formatAccountInfo(response) {
}); });
} }
function getAccountInfo(account, options, callback) { function getAccountInfoAsync(account, options, callback) {
validate.address(account); validate.address(account);
validate.getAccountInfoOptions(options); validate.getAccountInfoOptions(options);
@@ -31,4 +31,8 @@ function getAccountInfo(account, options, callback) {
composeAsync(formatAccountInfo, callback)); composeAsync(formatAccountInfo, callback));
} }
module.exports = utils.wrapCatch(getAccountInfo); function getAccountInfo(account: string, options={}) {
return utils.promisify(getAccountInfoAsync.bind(this))(account, options);
}
module.exports = getAccountInfo;

View File

@@ -24,7 +24,13 @@ function formatBalances(balances) {
balances.trustlines.map(getTrustlineBalanceAmount)); balances.trustlines.map(getTrustlineBalanceAmount));
} }
function getBalances(account, options, callback) { function getTrustlinesAsync(account, options, callback) {
getTrustlines.bind(this)(account, options)
.then(data => callback(null, data))
.catch(callback);
}
function getBalancesAsync(account, options, callback) {
validate.address(account); validate.address(account);
validate.getBalancesOptions(options); validate.getBalancesOptions(options);
@@ -32,8 +38,12 @@ function getBalances(account, options, callback) {
|| this.remote.getLedgerSequence(); || this.remote.getLedgerSequence();
async.parallel({ async.parallel({
xrp: _.partial(utils.getXRPBalance, this.remote, account, ledgerVersion), xrp: _.partial(utils.getXRPBalance, this.remote, account, ledgerVersion),
trustlines: _.partial(getTrustlines.bind(this), account, options) trustlines: _.partial(getTrustlinesAsync.bind(this), account, options)
}, composeAsync(formatBalances, callback)); }, composeAsync(formatBalances, callback));
} }
module.exports = utils.wrapCatch(getBalances); function getBalances(account: string, options={}) {
return utils.promisify(getBalancesAsync.bind(this))(account, options);
}
module.exports = getBalances;

View File

@@ -62,7 +62,7 @@ function formatBidsAndAsks(orderbook, offers) {
return {bids, asks}; return {bids, asks};
} }
function getOrderbook(account, orderbook, options, callback) { function getOrderbookAsync(account, orderbook, options, callback) {
validate.address(account); validate.address(account);
validate.orderbook(orderbook); validate.orderbook(orderbook);
validate.getOrderbookOptions(options); validate.getOrderbookOptions(options);
@@ -76,4 +76,9 @@ function getOrderbook(account, orderbook, options, callback) {
callback)); callback));
} }
module.exports = utils.wrapCatch(getOrderbook); function getOrderbook(account: string, orderbook: Object, options={}) {
return utils.promisify(getOrderbookAsync.bind(this))(
account, orderbook, options);
}
module.exports = getOrderbook;

View File

@@ -20,7 +20,7 @@ function requestAccountOffers(remote, address, ledgerVersion, options,
}), callback)); }), callback));
} }
function getOrders(account, options, callback) { function getOrdersAsync(account, options, callback) {
validate.address(account); validate.address(account);
validate.getOrdersOptions(options); validate.getOrdersOptions(options);
@@ -33,4 +33,8 @@ function getOrders(account, options, callback) {
(order) => order.properties.sequence), callback)); (order) => order.properties.sequence), callback));
} }
module.exports = utils.wrapCatch(getOrders); function getOrders(account: string, options={}) {
return utils.promisify(getOrdersAsync.bind(this))(account, options);
}
module.exports = getOrders;

View File

@@ -103,7 +103,7 @@ function formatResponse(pathfind, paths) {
} }
} }
function getPaths(pathfind, callback) { function getPathsAsync(pathfind, callback) {
validate.pathfind(pathfind); validate.pathfind(pathfind);
const address = pathfind.source.address; const address = pathfind.source.address;
@@ -113,4 +113,8 @@ function getPaths(pathfind, callback) {
], composeAsync(_.partial(formatResponse, pathfind), callback)); ], composeAsync(_.partial(formatResponse, pathfind), callback));
} }
module.exports = utils.wrapCatch(getPaths); function getPaths(pathfind: Object) {
return utils.promisify(getPathsAsync.bind(this))(pathfind);
}
module.exports = getPaths;

View File

@@ -24,7 +24,7 @@ function formatSettings(response) {
return _.assign({}, parsedFlags, parsedFields); return _.assign({}, parsedFlags, parsedFields);
} }
function getSettings(account, options, callback) { function getSettingsAsync(account, options, callback) {
validate.address(account); validate.address(account);
validate.getSettingsOptions(options); validate.getSettingsOptions(options);
@@ -37,4 +37,8 @@ function getSettings(account, options, callback) {
composeAsync(formatSettings, callback)); composeAsync(formatSettings, callback));
} }
module.exports = utils.wrapCatch(getSettings); function getSettings(account: string, options={}) {
return utils.promisify(getSettingsAsync.bind(this))(account, options);
}
module.exports = getSettings;

View File

@@ -36,7 +36,7 @@ function isTransactionInRange(tx, options) {
|| tx.ledger_index <= options.maxLedgerVersion); || tx.ledger_index <= options.maxLedgerVersion);
} }
function getTransaction(identifier, options, callback) { function getTransactionAsync(identifier, options, callback) {
validate.identifier(identifier); validate.identifier(identifier);
validate.getTransactionOptions(options); validate.getTransactionOptions(options);
@@ -71,4 +71,8 @@ function getTransaction(identifier, options, callback) {
], callbackWrapper); ], callbackWrapper);
} }
module.exports = utils.wrapCatch(getTransaction); function getTransaction(identifier: string, options={}) {
return utils.promisify(getTransactionAsync.bind(this))(identifier, options);
}
module.exports = getTransaction;

View File

@@ -98,27 +98,27 @@ function getTransactionsInternal(remote, address, options, callback) {
utils.getRecursive(getter, options.limit, composeAsync(format, callback)); utils.getRecursive(getter, options.limit, composeAsync(format, callback));
} }
function getTransactions(account, options, callback) { function getTransactionsAsync(account, options, callback) {
validate.address(account); validate.address(account);
validate.getTransactionsOptions(options); validate.getTransactionsOptions(options);
const defaults = {maxLedgerVersion: this.remote.getLedgerSequence()}; const defaults = {maxLedgerVersion: this.remote.getLedgerSequence()};
if (options.start) { if (options.start) {
getTransaction.bind(this)(options.start, {}, (error, tx) => { getTransaction.bind(this)(options.start).then(tx => {
if (error) {
callback(error);
return;
}
const ledgerVersion = tx.outcome.ledgerVersion; const ledgerVersion = tx.outcome.ledgerVersion;
const bound = options.earliestFirst ? const bound = options.earliestFirst ?
{minLedgerVersion: ledgerVersion} : {maxLedgerVersion: ledgerVersion}; {minLedgerVersion: ledgerVersion} : {maxLedgerVersion: ledgerVersion};
const newOptions = _.assign(defaults, options, {startTx: tx}, bound); const newOptions = _.assign(defaults, options, {startTx: tx}, bound);
getTransactionsInternal(this.remote, account, newOptions, callback); getTransactionsInternal(this.remote, account, newOptions, callback);
}); }).catch(callback);
} else { } else {
const newOptions = _.assign(defaults, options); const newOptions = _.assign(defaults, options);
getTransactionsInternal(this.remote, account, newOptions, callback); getTransactionsInternal(this.remote, account, newOptions, callback);
} }
} }
module.exports = utils.wrapCatch(getTransactions); function getTransactions(account: string, options={}) {
return utils.promisify(getTransactionsAsync.bind(this))(account, options);
}
module.exports = getTransactions;

View File

@@ -29,7 +29,7 @@ function getAccountLines(remote, address, ledgerVersion, options, marker, limit,
}); });
} }
function getTrustlines(account: string, options: {currency: string, function getTrustlinesAsync(account: string, options: {currency: string,
counterparty: string, limit: number, ledgerVersion: number}, counterparty: string, limit: number, ledgerVersion: number},
callback: () => void): void { callback: () => void): void {
validate.address(account); validate.address(account);
@@ -42,4 +42,8 @@ function getTrustlines(account: string, options: {currency: string,
utils.getRecursive(getter, options.limit, callback); utils.getRecursive(getter, options.limit, callback);
} }
module.exports = utils.wrapCatch(getTrustlines); function getTrustlines(account: string, options={}) {
return utils.promisify(getTrustlinesAsync.bind(this))(account, options);
}
module.exports = getTrustlines;

View File

@@ -13,7 +13,8 @@ function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max); return Math.min(Math.max(value, min), max);
} }
function getXRPBalance(remote: any, address: string, ledgerVersion?: number, callback: Callback): void { function getXRPBalance(remote: any, address: string, ledgerVersion?: number,
callback: Callback): void {
remote.requestAccountInfo({account: address, ledger: ledgerVersion}, remote.requestAccountInfo({account: address, ledger: ledgerVersion},
composeAsync((data) => dropsToXrp(data.account_data.Balance), callback)); composeAsync((data) => dropsToXrp(data.account_data.Balance), callback));
} }
@@ -22,7 +23,8 @@ type Getter = (marker: ?string, limit: number, callback: Callback) => void
// If the marker is omitted from a response, you have reached the end // If the marker is omitted from a response, you have reached the end
// getter(marker, limit, callback), callback(error, {marker, results}) // getter(marker, limit, callback), callback(error, {marker, results})
function getRecursiveRecur(getter: Getter, marker?: string, limit: number, callback: Callback): void { function getRecursiveRecur(getter: Getter, marker?: string, limit: number,
callback: Callback): void {
getter(marker, limit, (error, data) => { getter(marker, limit, (error, data) => {
if (error) { if (error) {
return callback(error); return callback(error);
@@ -105,7 +107,7 @@ module.exports = {
renameCounterpartyToIssuerInOrder, renameCounterpartyToIssuerInOrder,
getRecursive, getRecursive,
hasCompleteLedgerRange, hasCompleteLedgerRange,
wrapCatch: common.wrapCatch, promisify: common.promisify,
clamp: clamp, clamp: clamp,
common: common common: common
}; };

View File

@@ -8,14 +8,6 @@ const common = require('../common');
// If a ledger is not received in this time, consider the connection offline // If a ledger is not received in this time, consider the connection offline
const CONNECTION_TIMEOUT = 1000 * 30; const CONNECTION_TIMEOUT = 1000 * 30;
function connect(callback: (err: any, data: any) => void): void {
this.remote.connect(callback);
}
function disconnect(callback: (err: any, data: any) => void): void {
this.remote.disconnect(callback);
}
function isUpToDate(remote): boolean { function isUpToDate(remote): boolean {
const server = remote.getServer(); const server = remote.getServer();
return Boolean(server) && (remote._stand_alone return Boolean(server) && (remote._stand_alone
@@ -26,7 +18,7 @@ function isConnected(): boolean {
return Boolean(this.remote._ledger_current_index) && isUpToDate(this.remote); return Boolean(this.remote._ledger_current_index) && isUpToDate(this.remote);
} }
function getServerInfo(callback: (err: any, data: any) => void): void { function getServerInfoAsync(callback: (err: any, data: any) => void): void {
this.remote.requestServerInfo((error, response) => { this.remote.requestServerInfo((error, response) => {
if (error) { if (error) {
const message = _.get(error, ['remote', 'error_message'], error.message); const message = _.get(error, ['remote', 'error_message'], error.message);
@@ -45,6 +37,22 @@ function getLedgerVersion(): number {
return this.remote.getLedgerSequence(); return this.remote.getLedgerSequence();
} }
function connect() {
return common.promisify(callback => {
this.remote.connect(() => callback(null));
})();
}
function disconnect() {
return common.promisify(callback => {
this.remote.disconnect(() => callback(null));
})();
}
function getServerInfo() {
return common.promisify(getServerInfoAsync.bind(this))();
}
module.exports = { module.exports = {
connect, connect,
disconnect, disconnect,

View File

@@ -30,9 +30,14 @@ function createOrderTransaction(account, order) {
return transaction; return transaction;
} }
function prepareOrder(account, order, instructions, callback) { function prepareOrderAsync(account, order, instructions, callback) {
const transaction = createOrderTransaction(account, order); const transaction = createOrderTransaction(account, order);
utils.createTxJSON(transaction, this.remote, instructions, callback); utils.createTxJSON(transaction, this.remote, instructions, callback);
} }
module.exports = utils.wrapCatch(prepareOrder); function prepareOrder(account: string, order: Object, instructions={}) {
return utils.promisify(prepareOrderAsync.bind(this))(
account, order, instructions);
}
module.exports = prepareOrder;

View File

@@ -13,9 +13,16 @@ function createOrderCancellationTransaction(account, sequence) {
return transaction; return transaction;
} }
function prepareOrderCancellation(account, sequence, instructions, callback) { function prepareOrderCancellationAsync(account, sequence, instructions,
callback) {
const transaction = createOrderCancellationTransaction(account, sequence); const transaction = createOrderCancellationTransaction(account, sequence);
utils.createTxJSON(transaction, this.remote, instructions, callback); utils.createTxJSON(transaction, this.remote, instructions, callback);
} }
module.exports = utils.wrapCatch(prepareOrderCancellation); function prepareOrderCancellation(account: string, sequence: number,
instructions={}) {
return utils.promisify(prepareOrderCancellationAsync.bind(this))(
account, sequence, instructions);
}
module.exports = prepareOrderCancellation;

View File

@@ -81,9 +81,14 @@ function createPaymentTransaction(account, payment) {
return transaction; return transaction;
} }
function preparePayment(account, payment, instructions, callback) { function preparePaymentAsync(account, payment, instructions, callback) {
const transaction = createPaymentTransaction(account, payment); const transaction = createPaymentTransaction(account, payment);
utils.createTxJSON(transaction, this.remote, instructions, callback); utils.createTxJSON(transaction, this.remote, instructions, callback);
} }
module.exports = utils.wrapCatch(preparePayment); function preparePayment(account: string, payment: Object, instructions={}) {
return utils.promisify(preparePaymentAsync.bind(this))(
account, payment, instructions);
}
module.exports = preparePayment;

View File

@@ -90,9 +90,14 @@ function createSettingsTransaction(account, settings) {
return transaction; return transaction;
} }
function prepareSettings(account, settings, instructions, callback) { function prepareSettingsAsync(account, settings, instructions, callback) {
const transaction = createSettingsTransaction(account, settings); const transaction = createSettingsTransaction(account, settings);
utils.createTxJSON(transaction, this.remote, instructions, callback); utils.createTxJSON(transaction, this.remote, instructions, callback);
} }
module.exports = utils.wrapCatch(prepareSettings); function prepareSettings(account: string, settings: Object, instructions={}) {
return utils.promisify(prepareSettingsAsync.bind(this))(
account, settings, instructions);
}
module.exports = prepareSettings;

View File

@@ -4,7 +4,8 @@ const utils = require('./utils');
const validate = utils.common.validate; const validate = utils.common.validate;
const Request = utils.common.core.Request; const Request = utils.common.core.Request;
function submit(txBlob: string, callback: (err: any, data: any) => void): void { function submitAsync(txBlob: string, callback: (err: any, data: any) => void):
void {
validate.blob(txBlob); validate.blob(txBlob);
const request = new Request(this.remote, 'submit'); const request = new Request(this.remote, 'submit');
request.message.tx_blob = txBlob; request.message.tx_blob = txBlob;
@@ -14,4 +15,8 @@ function submit(txBlob: string, callback: (err: any, data: any) => void): void {
callback)); callback));
} }
function submit(txBlob: string) {
return utils.promisify(submitAsync.bind(this))(txBlob);
}
module.exports = submit; module.exports = submit;

View File

@@ -32,9 +32,14 @@ function createTrustlineTransaction(account, trustline) {
return transaction; return transaction;
} }
function prepareTrustline(account, trustline, instructions, callback) { function prepareTrustlineAsync(account, trustline, instructions, callback) {
const transaction = createTrustlineTransaction(account, trustline); const transaction = createTrustlineTransaction(account, trustline);
utils.createTxJSON(transaction, this.remote, instructions, callback); utils.createTxJSON(transaction, this.remote, instructions, callback);
} }
module.exports = utils.wrapCatch(prepareTrustline); function prepareTrustline(account: string, trustline: Object, instructions={}) {
return utils.promisify(prepareTrustlineAsync.bind(this))(
account, trustline, instructions);
}
module.exports = prepareTrustline;

View File

@@ -65,6 +65,6 @@ function createTxJSON(transaction: any, remote: any, instructions: any,
module.exports = { module.exports = {
setTransactionBitFlags: setTransactionBitFlags, setTransactionBitFlags: setTransactionBitFlags,
createTxJSON: createTxJSON, createTxJSON: createTxJSON,
wrapCatch: common.wrapCatch, common: common,
common: common promisify: common.promisify
}; };

View File

@@ -31,17 +31,12 @@ const orderbook = {
} }
}; };
function checkResult(expected, schemaName, done, error, response) { function checkResult(expected, schemaName, response) {
if (error) {
done(error);
return;
}
// console.log(JSON.stringify(response, null, 2)); // console.log(JSON.stringify(response, null, 2));
assert.deepEqual(response, expected); assert.deepEqual(response, expected);
if (schemaName) { if (schemaName) {
schemaValidator.schemaValidate(schemaName, response); schemaValidator.schemaValidate(schemaName, response);
} }
done();
} }
function withDeterministicPRNG(f) { function withDeterministicPRNG(f) {
@@ -56,100 +51,97 @@ describe('RippleAPI', function() {
beforeEach(setupAPI.setup); beforeEach(setupAPI.setup);
afterEach(setupAPI.teardown); afterEach(setupAPI.teardown);
it('preparePayment', function(done) { it('preparePayment', function() {
const localInstructions = _.defaults({ const localInstructions = _.defaults({
maxFee: '0.000012' maxFee: '0.000012'
}, instructions); }, instructions);
this.api.preparePayment(address, requests.preparePayment, localInstructions, return this.api.preparePayment(
_.partial(checkResult, responses.preparePayment, 'tx', done)); address, requests.preparePayment, localInstructions).then(
_.partial(checkResult, responses.preparePayment, 'tx'));
}); });
it('preparePayment with all options specified', function(done) { it('preparePayment with all options specified', function() {
const localInstructions = { const localInstructions = {
maxLedgerVersion: this.api.getLedgerVersion() + 100, maxLedgerVersion: this.api.getLedgerVersion() + 100,
fee: '0.000012' fee: '0.000012'
}; };
this.api.preparePayment(address, requests.preparePaymentAllOptions, return this.api.preparePayment(
localInstructions, address, requests.preparePaymentAllOptions, localInstructions).then(
_.partial(checkResult, responses.preparePaymentAllOptions, 'tx', done)); _.partial(checkResult, responses.preparePaymentAllOptions, 'tx'));
}); });
it('preparePayment without counterparty set', function(done) { it('preparePayment without counterparty set', function() {
const localInstructions = _.defaults({ const localInstructions = _.defaults({sequence: 23}, instructions);
sequence: 23 return this.api.preparePayment(
}, instructions); address, requests.preparePaymentNoCounterparty, localInstructions).then(
this.api.preparePayment(address, requests.preparePaymentNoCounterparty, _.partial(checkResult, responses.preparePaymentNoCounterparty, 'tx'));
localInstructions,
_.partial(checkResult, responses.preparePaymentNoCounterparty,
'tx', done));
}); });
it('prepareOrder - buy order', function(done) { it('prepareOrder - buy order', function() {
this.api.prepareOrder(address, requests.prepareOrder, instructions, return this.api.prepareOrder(address, requests.prepareOrder, instructions)
_.partial(checkResult, responses.prepareOrder, 'tx', done)); .then(_.partial(checkResult, responses.prepareOrder, 'tx'));
}); });
it('prepareOrder - sell order', function(done) { it('prepareOrder - sell order', function() {
this.api.prepareOrder(address, requests.prepareOrderSell, instructions, return this.api.prepareOrder(
_.partial(checkResult, responses.prepareOrderSell, 'tx', done)); address, requests.prepareOrderSell, instructions).then(
_.partial(checkResult, responses.prepareOrderSell, 'tx'));
}); });
it('prepareOrderCancellation', function(done) { it('prepareOrderCancellation', function() {
this.api.prepareOrderCancellation(address, 23, instructions, return this.api.prepareOrderCancellation(address, 23, instructions).then(
_.partial(checkResult, responses.prepareOrderCancellation, 'tx', _.partial(checkResult, responses.prepareOrderCancellation, 'tx'));
done));
}); });
it('prepareTrustline', function(done) { it('prepareTrustline', function() {
this.api.prepareTrustline(address, requests.prepareTrustline, return this.api.prepareTrustline(
instructions, _.partial(checkResult, responses.prepareTrustline, address, requests.prepareTrustline, instructions).then(
'tx', done)); _.partial(checkResult, responses.prepareTrustline, 'tx'));
}); });
it('prepareSettings', function(done) { it('prepareSettings', function() {
this.api.prepareSettings(address, requests.prepareSettings, instructions, return this.api.prepareSettings(
_.partial(checkResult, responses.prepareSettings.flags, 'tx', done)); address, requests.prepareSettings, instructions).then(
_.partial(checkResult, responses.prepareSettings.flags, 'tx'));
}); });
it('prepareSettings - regularKey', function(done) { it('prepareSettings - regularKey', function() {
const regularKey = {regularKey: 'rAR8rR8sUkBoCZFawhkWzY4Y5YoyuznwD'}; const regularKey = {regularKey: 'rAR8rR8sUkBoCZFawhkWzY4Y5YoyuznwD'};
this.api.prepareSettings(address, regularKey, instructions, return this.api.prepareSettings(address, regularKey, instructions).then(
_.partial(checkResult, responses.prepareSettings.regularKey, _.partial(checkResult, responses.prepareSettings.regularKey, 'tx'));
'tx', done));
}); });
it('prepareSettings - flag set', function(done) { it('prepareSettings - flag set', function() {
const settings = {requireDestinationTag: true}; const settings = {requireDestinationTag: true};
this.api.prepareSettings(address, settings, instructions, return this.api.prepareSettings(address, settings, instructions).then(
_.partial(checkResult, responses.prepareSettings.flagSet, 'tx', done)); _.partial(checkResult, responses.prepareSettings.flagSet, 'tx'));
}); });
it('prepareSettings - flag clear', function(done) { it('prepareSettings - flag clear', function() {
const settings = {requireDestinationTag: false}; const settings = {requireDestinationTag: false};
this.api.prepareSettings(address, settings, instructions, return this.api.prepareSettings(address, settings, instructions).then(
_.partial(checkResult, responses.prepareSettings.flagClear, 'tx', done)); _.partial(checkResult, responses.prepareSettings.flagClear, 'tx'));
}); });
it('prepareSettings - string field clear', function(done) { it('prepareSettings - string field clear', function() {
const settings = {walletLocator: null}; const settings = {walletLocator: null};
this.api.prepareSettings(address, settings, instructions, return this.api.prepareSettings(address, settings, instructions).then(
_.partial(checkResult, responses.prepareSettings.fieldClear, 'tx', done)); _.partial(checkResult, responses.prepareSettings.fieldClear, 'tx'));
}); });
it('prepareSettings - integer field clear', function(done) { it('prepareSettings - integer field clear', function() {
const settings = {walletSize: null}; const settings = {walletSize: null};
this.api.prepareSettings(address, settings, instructions, (e, data) => { return this.api.prepareSettings(address, settings, instructions)
assert(data); .then(data => {
assert.strictEqual(data.WalletSize, 0); assert(data);
done(e); assert.strictEqual(data.WalletSize, 0);
}); });
}); });
it('prepareSettings - set transferRate', function(done) { it('prepareSettings - set transferRate', function() {
const settings = {transferRate: 1}; const settings = {transferRate: 1};
this.api.prepareSettings(address, settings, instructions, return this.api.prepareSettings(address, settings, instructions).then(
_.partial(checkResult, responses.prepareSettings.setTransferRate, _.partial(checkResult, responses.prepareSettings.setTransferRate, 'tx'));
'tx', done));
}); });
it('sign', function() { it('sign', function() {
@@ -161,255 +153,260 @@ describe('RippleAPI', function() {
}); });
}); });
it('submit', function(done) { it('submit', function() {
this.api.submit(responses.sign.signedTransaction, return this.api.submit(responses.sign.signedTransaction).then(
_.partial(checkResult, responses.submit, 'submit', done)); _.partial(checkResult, responses.submit, 'submit'));
}); });
it('getBalances', function(done) { it('getBalances', function() {
this.api.getBalances(address, {}, return this.api.getBalances(address).then(
_.partial(checkResult, responses.getBalances, 'getBalances', done)); _.partial(checkResult, responses.getBalances, 'getBalances'));
}); });
it('getTransaction - payment', function(done) { it('getTransaction - payment', function() {
this.api.getTransaction(hashes.VALID_TRANSACTION_HASH, {}, return this.api.getTransaction(hashes.VALID_TRANSACTION_HASH).then(
_.partial(checkResult, responses.getTransaction.payment, _.partial(checkResult, responses.getTransaction.payment,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - settings', function(done) { it('getTransaction - settings', function() {
const hash = const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA1B'; '4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA1B';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.settings, _.partial(checkResult, responses.getTransaction.settings,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - order', function(done) { it('getTransaction - order', function() {
const hash = const hash =
'10A6FB4A66EE80BED46AAE4815D7DC43B97E944984CCD5B93BCF3F8538CABC51'; '10A6FB4A66EE80BED46AAE4815D7DC43B97E944984CCD5B93BCF3F8538CABC51';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.order, _.partial(checkResult, responses.getTransaction.order,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - order cancellation', function(done) { it('getTransaction - order cancellation', function() {
const hash = const hash =
'809335DD3B0B333865096217AA2F55A4DF168E0198080B3A090D12D88880FF0E'; '809335DD3B0B333865096217AA2F55A4DF168E0198080B3A090D12D88880FF0E';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.orderCancellation, _.partial(checkResult, responses.getTransaction.orderCancellation,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - trustline set', function(done) { it('getTransaction - trustline set', function() {
const hash = const hash =
'635A0769BD94710A1F6A76CDE65A3BC661B20B798807D1BBBDADCEA26420538D'; '635A0769BD94710A1F6A76CDE65A3BC661B20B798807D1BBBDADCEA26420538D';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.trustline, _.partial(checkResult, responses.getTransaction.trustline,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - trustline frozen off', function(done) { it('getTransaction - trustline frozen off', function() {
const hash = const hash =
'FE72FAD0FA7CA904FB6C633A1666EDF0B9C73B2F5A4555D37EEF2739A78A531B'; 'FE72FAD0FA7CA904FB6C633A1666EDF0B9C73B2F5A4555D37EEF2739A78A531B';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.trustlineFrozenOff, _.partial(checkResult, responses.getTransaction.trustlineFrozenOff,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - not validated', function(done) { it('getTransaction - not validated', function() {
const hash = const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA10'; '4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA10';
this.api.getTransaction(hash, {}, (error, data) => { return this.api.getTransaction(hash).then(
assert.deepEqual(data, responses.getTransaction.notValidated); _.partial(checkResult, responses.getTransaction.notValidated,
done(error); 'getTransaction'));
});
}); });
it('getTransaction - tracking on', function(done) { it('getTransaction - tracking on', function() {
const hash = const hash =
'8925FC8844A1E930E2CC76AD0A15E7665AFCC5425376D548BB1413F484C31B8C'; '8925FC8844A1E930E2CC76AD0A15E7665AFCC5425376D548BB1413F484C31B8C';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.trackingOn, _.partial(checkResult, responses.getTransaction.trackingOn,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - tracking off', function(done) { it('getTransaction - tracking off', function() {
const hash = const hash =
'C8C5E20DFB1BF533D0D81A2ED23F0A3CBD1EF2EE8A902A1D760500473CC9C582'; 'C8C5E20DFB1BF533D0D81A2ED23F0A3CBD1EF2EE8A902A1D760500473CC9C582';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.trackingOff, _.partial(checkResult, responses.getTransaction.trackingOff,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - set regular key', function(done) { it('getTransaction - set regular key', function() {
const hash = const hash =
'278E6687C1C60C6873996210A6523564B63F2844FB1019576C157353B1813E60'; '278E6687C1C60C6873996210A6523564B63F2844FB1019576C157353B1813E60';
this.api.getTransaction(hash, {}, return this.api.getTransaction(hash).then(
_.partial(checkResult, responses.getTransaction.setRegularKey, _.partial(checkResult, responses.getTransaction.setRegularKey,
'getTransaction', done)); 'getTransaction'));
}); });
it('getTransaction - not found in range', function(done) { it('getTransaction - not found in range', function() {
const hash = const hash =
'809335DD3B0B333865096217AA2F55A4DF168E0198080B3A090D12D88880FF0E'; '809335DD3B0B333865096217AA2F55A4DF168E0198080B3A090D12D88880FF0E';
const options = { const options = {
minLedgerVersion: 32570, minLedgerVersion: 32570,
maxLedgerVersion: 32571 maxLedgerVersion: 32571
}; };
this.api.getTransaction(hash, options, (error) => { return this.api.getTransaction(hash, options).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
it('getTransaction - not found by hash', function(done) { it('getTransaction - not found by hash', function() {
this.api.getTransaction(hashes.NOTFOUND_TRANSACTION_HASH, {}, (error) => { const hash = hashes.NOTFOUND_TRANSACTION_HASH;
return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
it('getTransaction - missing ledger history', function(done) { it('getTransaction - missing ledger history', function() {
const hash = hashes.NOTFOUND_TRANSACTION_HASH;
// make gaps in history // make gaps in history
this.api.remote.getServer().emit('message', ledgerClosed); this.api.remote.getServer().emit('message', ledgerClosed);
this.api.getTransaction(hashes.NOTFOUND_TRANSACTION_HASH, {}, (error) => { return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw MissingLedgerHistoryError');
}).catch(error => {
assert(error instanceof this.api.errors.MissingLedgerHistoryError); assert(error instanceof this.api.errors.MissingLedgerHistoryError);
done();
}); });
}); });
it('getTransaction - ledger_index not found', function(done) { it('getTransaction - ledger_index not found', function() {
const hash = const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11'; '4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11';
this.api.getTransaction(hash, {}, (error) => { return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
assert(error.message.indexOf('ledger_index') !== -1); assert(error.message.indexOf('ledger_index') !== -1);
done();
}); });
}); });
it('getTransaction - transaction ledger not found', function(done) { it('getTransaction - transaction ledger not found', function() {
const hash = const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA12'; '4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA12';
this.api.getTransaction(hash, {}, (error) => { return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
assert(error.message.indexOf('ledger not found') !== -1); assert(error.message.indexOf('ledger not found') !== -1);
done();
}); });
}); });
it('getTransaction - ledger missing close time', function(done) { it('getTransaction - ledger missing close time', function() {
const hash = const hash =
'0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A04'; '0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A04';
this.api.getTransaction(hash, {}, (error) => { return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw ApiError');
}).catch(error => {
assert(error instanceof this.api.errors.ApiError); assert(error instanceof this.api.errors.ApiError);
done();
}); });
}); });
it('getTransactions', function(done) { it('getTransactions', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 2}; const options = {types: ['payment', 'order'], initiated: true, limit: 2};
this.api.getTransactions(address, options, return this.api.getTransactions(address, options).then(
_.partial(checkResult, responses.getTransactions, _.partial(checkResult, responses.getTransactions,
'getTransactions', done)); 'getTransactions'));
}); });
it('getTransactions - earliest first', function(done) { it('getTransactions - earliest first', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 2, const options = {types: ['payment', 'order'], initiated: true, limit: 2,
earliestFirst: true earliestFirst: true
}; };
const expected = _.cloneDeep(responses.getTransactions) const expected = _.cloneDeep(responses.getTransactions)
.sort(utils.compareTransactions); .sort(utils.compareTransactions);
this.api.getTransactions(address, options, return this.api.getTransactions(address, options).then(
_.partial(checkResult, expected, 'getTransactions', done)); _.partial(checkResult, expected, 'getTransactions'));
}); });
it('getTransactions - earliest first with start option', function(done) { it('getTransactions - earliest first with start option', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 2, const options = {types: ['payment', 'order'], initiated: true, limit: 2,
start: hashes.VALID_TRANSACTION_HASH, start: hashes.VALID_TRANSACTION_HASH,
earliestFirst: true earliestFirst: true
}; };
this.api.getTransactions(address, options, (error, data) => { return this.api.getTransactions(address, options).then(data => {
assert.strictEqual(data.length, 0); assert.strictEqual(data.length, 0);
done(error);
}); });
}); });
it('getTransactions - gap', function(done) { it('getTransactions - gap', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 2, const options = {types: ['payment', 'order'], initiated: true, limit: 2,
maxLedgerVersion: 348858000 maxLedgerVersion: 348858000
}; };
this.api.getTransactions(address, options, (error) => { return this.api.getTransactions(address, options).then(() => {
assert(false, 'Should throw MissingLedgerHistoryError');
}).catch(error => {
assert(error instanceof this.api.errors.MissingLedgerHistoryError); assert(error instanceof this.api.errors.MissingLedgerHistoryError);
done();
}); });
}); });
it('getTransactions - tx not found', function(done) { it('getTransactions - tx not found', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 2, const options = {types: ['payment', 'order'], initiated: true, limit: 2,
start: hashes.NOTFOUND_TRANSACTION_HASH, start: hashes.NOTFOUND_TRANSACTION_HASH,
counterparty: address counterparty: address
}; };
this.api.getTransactions(address, options, (error) => { return this.api.getTransactions(address, options).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
it('getTransactions - filters', function(done) { it('getTransactions - filters', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 10, const options = {types: ['payment', 'order'], initiated: true, limit: 10,
excludeFailures: true, excludeFailures: true,
counterparty: addresses.ISSUER counterparty: addresses.ISSUER
}; };
this.api.getTransactions(address, options, (error, data) => { return this.api.getTransactions(address, options).then(data => {
assert.strictEqual(data.length, 10); assert.strictEqual(data.length, 10);
assert(_.every(data, t => t.type === 'payment' || t.type === 'order')); assert(_.every(data, t => t.type === 'payment' || t.type === 'order'));
assert(_.every(data, t => t.outcome.result === 'tesSUCCESS')); assert(_.every(data, t => t.outcome.result === 'tesSUCCESS'));
done();
}); });
}); });
it('getTransactions - filters for incoming', function(done) { it('getTransactions - filters for incoming', function() {
const options = {types: ['payment', 'order'], initiated: false, limit: 10, const options = {types: ['payment', 'order'], initiated: false, limit: 10,
excludeFailures: true, excludeFailures: true,
counterparty: addresses.ISSUER counterparty: addresses.ISSUER
}; };
this.api.getTransactions(address, options, (error, data) => { return this.api.getTransactions(address, options).then(data => {
assert.strictEqual(data.length, 10); assert.strictEqual(data.length, 10);
assert(_.every(data, t => t.type === 'payment' || t.type === 'order')); assert(_.every(data, t => t.type === 'payment' || t.type === 'order'));
assert(_.every(data, t => t.outcome.result === 'tesSUCCESS')); assert(_.every(data, t => t.outcome.result === 'tesSUCCESS'));
done();
}); });
}); });
// this is the case where core.RippleError just falls // this is the case where core.RippleError just falls
// through the api to the user // through the api to the user
it('getTransactions - error', function(done) { it('getTransactions - error', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 13}; const options = {types: ['payment', 'order'], initiated: true, limit: 13};
this.api.getTransactions(address, options, (error) => { return this.api.getTransactions(address, options).then(() => {
assert(false, 'Should throw RippleError');
}).catch(error => {
assert(error instanceof RippleError); assert(error instanceof RippleError);
done();
}); });
}); });
// TODO: this doesn't test much, just that it doesn't crash // TODO: this doesn't test much, just that it doesn't crash
it('getTransactions with start option', function(done) { it('getTransactions with start option', function() {
const options = { const options = {
start: hashes.VALID_TRANSACTION_HASH, start: hashes.VALID_TRANSACTION_HASH,
earliestFirst: false, earliestFirst: false,
limit: 2 limit: 2
}; };
this.api.getTransactions(address, options, return this.api.getTransactions(address, options).then(
_.partial(checkResult, responses.getTransactions, _.partial(checkResult, responses.getTransactions, 'getTransactions'));
'getTransactions', done));
}); });
it('getTrustlines', function(done) { it('getTrustlines', function() {
const options = {currency: 'USD'}; const options = {currency: 'USD'};
this.api.getTrustlines(address, options, return this.api.getTrustlines(address, options).then(
_.partial(checkResult, responses.getTrustlines, 'getTrustlines', _.partial(checkResult, responses.getTrustlines, 'getTrustlines'));
done));
}); });
it('generateWallet', function() { it('generateWallet', function() {
@@ -418,29 +415,28 @@ describe('RippleAPI', function() {
}); });
}); });
it('getSettings', function(done) { it('getSettings', function() {
this.api.getSettings(address, {}, return this.api.getSettings(address).then(
_.partial(checkResult, responses.getSettings, 'getSettings', done)); _.partial(checkResult, responses.getSettings, 'getSettings'));
}); });
it('getAccountInfo', function(done) { it('getAccountInfo', function() {
this.api.getAccountInfo(address, {}, return this.api.getAccountInfo(address).then(
_.partial(checkResult, responses.getAccountInfo, 'getAccountInfo', _.partial(checkResult, responses.getAccountInfo, 'getAccountInfo'));
done));
}); });
it('getOrders', function(done) { it('getOrders', function() {
this.api.getOrders(address, {}, return this.api.getOrders(address).then(
_.partial(checkResult, responses.getOrders, 'getOrders', done)); _.partial(checkResult, responses.getOrders, 'getOrders'));
}); });
it('getOrderbook', function(done) { it('getOrderbook', function() {
this.api.getOrderbook(address, orderbook, {}, return this.api.getOrderbook(address, orderbook).then(
_.partial(checkResult, responses.getOrderbook, 'getOrderbook', done)); _.partial(checkResult, responses.getOrderbook, 'getOrderbook'));
}); });
it('getOrderbook - sorted so that best deals come first', function(done) { it('getOrderbook - sorted so that best deals come first', function() {
this.api.getOrderbook(address, orderbook, {}, (error, data) => { return this.api.getOrderbook(address, orderbook).then(data => {
const bidRates = data.bids.map(bid => bid.properties.makerExchangeRate); const bidRates = data.bids.map(bid => bid.properties.makerExchangeRate);
const askRates = data.asks.map(ask => ask.properties.makerExchangeRate); const askRates = data.asks.map(ask => ask.properties.makerExchangeRate);
// makerExchangeRate = quality = takerPays.value/takerGets.value // makerExchangeRate = quality = takerPays.value/takerGets.value
@@ -448,12 +444,11 @@ describe('RippleAPI', function() {
// bids and asks should be sorted so that the best deals come first // bids and asks should be sorted so that the best deals come first
assert.deepEqual(_.sortBy(bidRates, x => Number(x)), bidRates); assert.deepEqual(_.sortBy(bidRates, x => Number(x)), bidRates);
assert.deepEqual(_.sortBy(askRates, x => Number(x)), askRates); assert.deepEqual(_.sortBy(askRates, x => Number(x)), askRates);
done();
}); });
}); });
it('getOrderbook - currency & counterparty are correct', function(done) { it('getOrderbook - currency & counterparty are correct', function() {
this.api.getOrderbook(address, orderbook, {}, (error, data) => { return this.api.getOrderbook(address, orderbook).then(data => {
const orders = _.flatten([data.bids, data.asks]); const orders = _.flatten([data.bids, data.asks]);
_.forEach(orders, order => { _.forEach(orders, order => {
const quantity = order.specification.quantity; const quantity = order.specification.quantity;
@@ -464,30 +459,28 @@ describe('RippleAPI', function() {
assert.strictEqual(totalPrice.currency, counter.currency); assert.strictEqual(totalPrice.currency, counter.currency);
assert.strictEqual(totalPrice.counterparty, counter.counterparty); assert.strictEqual(totalPrice.counterparty, counter.counterparty);
}); });
done();
}); });
}); });
it('getOrderbook - direction is correct for bids and asks', function(done) { it('getOrderbook - direction is correct for bids and asks', function() {
this.api.getOrderbook(address, orderbook, {}, (error, data) => { return this.api.getOrderbook(address, orderbook).then(data => {
assert(_.every(data.bids, bid => bid.specification.direction === 'buy')); assert(_.every(data.bids, bid => bid.specification.direction === 'buy'));
assert( assert(_.every(data.asks, ask => ask.specification.direction === 'sell'));
_.every(data.asks, ask => ask.specification.direction === 'sell'));
done();
}); });
}); });
it('getServerInfo', function(done) { it('getServerInfo', function() {
this.api.getServerInfo( return this.api.getServerInfo().then(
_.partial(checkResult, responses.getServerInfo, 'getServerInfo', done)); _.partial(checkResult, responses.getServerInfo, 'getServerInfo'));
}); });
it('getServerInfo - error', function(done) { it('getServerInfo - error', function() {
this.mockRippled.returnErrorOnServerInfo = true; this.mockRippled.returnErrorOnServerInfo = true;
this.api.getServerInfo((error) => { return this.api.getServerInfo().then(() => {
assert(false, 'Should throw NetworkError');
}).catch(error => {
assert(error instanceof this.api.errors.NetworkError); assert(error instanceof this.api.errors.NetworkError);
assert(error.message.indexOf('too much load') !== -1); assert(error.message.indexOf('too much load') !== -1);
done();
}); });
}); });
@@ -495,58 +488,62 @@ describe('RippleAPI', function() {
assert.strictEqual(this.api.getFee(), '0.000012'); assert.strictEqual(this.api.getFee(), '0.000012');
}); });
it('disconnect & isConnected', function(done) { it('disconnect & isConnected', function() {
assert.strictEqual(this.api.isConnected(), true); assert.strictEqual(this.api.isConnected(), true);
this.api.disconnect(() => { return this.api.disconnect().then(() => {
assert.strictEqual(this.api.isConnected(), false); assert.strictEqual(this.api.isConnected(), false);
done();
}); });
}); });
it('getPaths', function(done) { it('getPaths', function() {
this.api.getPaths(requests.getPaths.normal, return this.api.getPaths(requests.getPaths.normal).then(
_.partial(checkResult, responses.getPaths.XrpToUsd, 'getPaths', done)); _.partial(checkResult, responses.getPaths.XrpToUsd, 'getPaths'));
}); });
// @TODO // @TODO
// need decide what to do with currencies/XRP: // need decide what to do with currencies/XRP:
// if add 'XRP' in currencies, then there will be exception in // if add 'XRP' in currencies, then there will be exception in
// xrpToDrops function (called from toRippledAmount) // xrpToDrops function (called from toRippledAmount)
it('getPaths USD 2 USD', function(done) { it('getPaths USD 2 USD', function() {
this.api.getPaths(requests.getPaths.UsdToUsd, return this.api.getPaths(requests.getPaths.UsdToUsd).then(
_.partial(checkResult, responses.getPaths.UsdToUsd, 'getPaths', done)); _.partial(checkResult, responses.getPaths.UsdToUsd, 'getPaths'));
}); });
it('getPaths XRP 2 XRP', function(done) { it('getPaths XRP 2 XRP', function() {
this.api.getPaths(requests.getPaths.XrpToXrp, return this.api.getPaths(requests.getPaths.XrpToXrp).then(
_.partial(checkResult, responses.getPaths.XrpToXrp, 'getPaths', done)); _.partial(checkResult, responses.getPaths.XrpToXrp, 'getPaths'));
}); });
it('getPaths - XRP 2 XRP - not enough', function(done) { it('getPaths - XRP 2 XRP - not enough', function() {
this.api.getPaths(requests.getPaths.XrpToXrpNotEnough, (error) => { return this.api.getPaths(requests.getPaths.XrpToXrpNotEnough).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
it('getPaths - does not accept currency', function(done) { it('getPaths - does not accept currency', function() {
this.api.getPaths(requests.getPaths.NotAcceptCurrency, (error) => { return this.api.getPaths(requests.getPaths.NotAcceptCurrency).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
it('getPaths - no paths', function(done) { it('getPaths - no paths', function() {
this.api.getPaths(requests.getPaths.NoPaths, (error) => { return this.api.getPaths(requests.getPaths.NoPaths).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
it('getPaths - no paths with source currencies', function(done) { it('getPaths - no paths with source currencies', function() {
this.api.getPaths(requests.getPaths.NoPathsWithCurrencies, (error) => { const pathfind = requests.getPaths.NoPathsWithCurrencies;
return this.api.getPaths(pathfind).then(() => {
assert(false, 'Should throw NotFoundError');
}).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
done();
}); });
}); });
@@ -740,7 +737,7 @@ describe('RippleAPI', function() {
}); });
describe('RippleAPI - offline', function() { describe('RippleAPI - offline', function() {
it('prepareSettings and sign', function(done) { it('prepareSettings and sign', function() {
const api = new RippleAPI(); const api = new RippleAPI();
const secret = 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV'; const secret = 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV';
const settings = requests.prepareSettings; const settings = requests.prepareSettings;
@@ -749,15 +746,10 @@ describe('RippleAPI - offline', function() {
maxLedgerVersion: 8820051, maxLedgerVersion: 8820051,
fee: '0.000012' fee: '0.000012'
}; };
api.prepareSettings(address, settings, instructions, (error, txJSON) => { return api.prepareSettings(address, settings, instructions).then(txJSON => {
if (error) {
done(error);
return;
}
assert.deepEqual(txJSON, responses.prepareSettings.flags); assert.deepEqual(txJSON, responses.prepareSettings.flags);
withDeterministicPRNG(() => { withDeterministicPRNG(() => {
assert.deepEqual(api.sign(txJSON, secret), responses.sign); assert.deepEqual(api.sign(txJSON, secret), responses.sign);
done();
}); });
}); });
}); });

View File

@@ -24,10 +24,10 @@ function getFreePort(callback) {
function setupMockRippledConnection(testcase, port, done) { function setupMockRippledConnection(testcase, port, done) {
testcase.mockRippled = createMockRippled(port); testcase.mockRippled = createMockRippled(port);
testcase.api = new RippleAPI({servers: ['ws://localhost:' + port]}); testcase.api = new RippleAPI({servers: ['ws://localhost:' + port]});
testcase.api.connect(() => { testcase.api.connect().then(() => {
testcase.api.remote.getServer().once('ledger_closed', () => done()); testcase.api.remote.getServer().once('ledger_closed', () => done());
testcase.api.remote.getServer().emit('message', ledgerClosed); testcase.api.remote.getServer().emit('message', ledgerClosed);
}); }).catch(done);
} }
function setup(done) { function setup(done) {
@@ -40,10 +40,10 @@ function setup(done) {
} }
function teardown(done) { function teardown(done) {
this.api.remote.disconnect(() => { this.api.disconnect().then(() => {
this.mockRippled.close(); this.mockRippled.close();
setImmediate(done); setImmediate(done);
}); }).catch(done);
} }
module.exports = { module.exports = {