Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Balance sheet #579

Merged
merged 7 commits into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/common/schema-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function loadSchemas() {
require('./schemas/currency.json'),
require('./schemas/get-account-info.json'),
require('./schemas/get-balances.json'),
require('./schemas/get-balance-sheet'),
require('./schemas/balance-sheet-options.json'),
require('./schemas/get-ledger.json'),
require('./schemas/get-orderbook.json'),
require('./schemas/get-orders.json'),
Expand Down
15 changes: 15 additions & 0 deletions src/api/common/schemas/balance-sheet-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "balance-sheet-options",
"description": "Options for getBalanceSheet",
"type": "object",
"properties": {
"excludeAddresses": {
"type": "array",
"items": {"$ref": "address"},
"uniqueItems": true
},
"ledgerVersion": {"$ref": "ledgerVersion"}
},
"additionalProperties": false
}
67 changes: 67 additions & 0 deletions src/api/common/schemas/get-balance-sheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "get-balance-sheet",
"description": "getBalanceSheet response",
"type": "object",
"properties": {
"balances": {
"type": "array",
"items": {
"type": "object",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to just do {"$ref": "amount"} here.

"properties": {
"required": ["counterparty", "balances"],
"additionalProperties": false,
"counterparty": {"$ref": "address"},
"balances": {
"type": "array",
"items": {
"type": "object",
"properties": {
"required": ["currency", "value"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required and additionalProperties should be outside the properties object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"additionalProperties": false,
"currency": {"$ref": "currency"},
"value": {"$ref": "value"}
}
}
}
}
}
},
"assets": {
"type": "array",
"items": {
"type": "object",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"properties": {
"required": ["counterparty", "assets"],
"additionalProperties": false,
"counterparty": {"$ref": "address"},
"assets": {
"type": "array",
"items": {
"type": "object",
"properties": {
"required": ["currency", "value"],
"additionalProperties": false,
"currency": {"$ref": "currency"},
"value": {"$ref": "value"}
}
}
}
}
}
},
"obligations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"required": ["currency", "value"],
"additionalProperties": false,
"currency": {"$ref": "currency"},
"value": {"$ref": "value"}
}
}
}
},
"additionalProperties": false
}
1 change: 1 addition & 0 deletions src/api/common/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module.exports = {
getAccountInfoOptions: _.partial(validateOptions, 'settings-options'),
getTrustlinesOptions: _.partial(validateOptions, 'trustlines-options'),
getBalancesOptions: _.partial(validateOptions, 'trustlines-options'),
getBalanceSheetOptions: _.partial(validateOptions, 'balance-sheet-options'),
getOrdersOptions: _.partial(validateOptions, 'orders-options'),
getOrderbookOptions: _.partial(validateOptions, 'orders-options'),
getTransactionOptions: _.partial(validateOptions, 'transaction-options'),
Expand Down
2 changes: 2 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getTransaction = require('./ledger/transaction');
const getTransactions = require('./ledger/transactions');
const getTrustlines = require('./ledger/trustlines');
const getBalances = require('./ledger/balances');
const getBalanceSheet = require('./ledger/balance-sheet');
const getPaths = require('./ledger/pathfind');
const getOrders = require('./ledger/orders');
const getOrderbook = require('./ledger/orderbook');
Expand Down Expand Up @@ -66,6 +67,7 @@ _.assign(RippleAPI.prototype, {
getTransactions,
getTrustlines,
getBalances,
getBalanceSheet,
getPaths,
getOrders,
getOrderbook,
Expand Down
69 changes: 69 additions & 0 deletions src/api/ledger/balance-sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const _ = require('lodash');
const utils = require('./utils');
const validate = utils.common.validate;
const composeAsync = utils.common.composeAsync;
const convertErrors = utils.common.convertErrors;

function formatBalanceSheet({balances, obligations, assets}) {
const result = {};

if (!_.isUndefined(balances)) {
result.balances = Object.keys(balances).map((k) => {
return {
counterparty: k,
balances: balances[k]
};
});
}
if (!_.isUndefined(assets)) {
result.assets = Object.keys(assets).map((k) => {
return {
counterparty: k,
assets: assets[k]
};
});
}
if (!_.isUndefined(obligations)) {
result.obligations = Object.keys(obligations).map((k) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lodash map gives you both value and key, so you could do result.obligations = _.map(obligations, (value, currency) => {currency, value}), and similar for balances and assets

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return {currency: k, value: obligations[k]};
});
}

return result;
}

function getBalanceSheetAsync(address, options, callback) {
validate.address(address);
validate.getBalanceSheetOptions(options);

const requestOptions = Object.assign({}, {
account: address,
strict: true,
hotwallet: options.excludeAddresses,
ledger: options.ledgerVersion
});

const requestCallback = composeAsync(
formatBalanceSheet, convertErrors(callback));

this.remote.getLedgerSequence((err, ledgerVersion) => {
if (err) {
callback(err);
return;
}

if (_.isUndefined(requestOptions.ledger)) {
requestOptions.ledger = ledgerVersion;
}

this.remote.requestGatewayBalances(requestOptions, requestCallback);
});
}

function getBalanceSheet(address: string, options = {}) {
return utils.promisify(getBalanceSheetAsync).call(this, address, options);
}

module.exports = getBalanceSheet;
23 changes: 23 additions & 0 deletions src/core/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,29 @@ Remote.prototype.requestConnect = function(ip, port, callback) {
return request;
};

Remote.prototype.requestGatewayBalances = function(options, callback) {
assert(_.isObject(options), 'Options missing');
assert(options.account, 'Account missing');

const request = new Request(this, 'gateway_balances');

request.message.account = UInt160.json_rewrite(options.account);

if (!_.isUndefined(options.hotwallet)) {
request.message.hotwallet = options.hotwallet;
}
if (!_.isUndefined(options.strict)) {
request.message.strict = options.strict;
}
if (!_.isUndefined(options.ledger)) {
request.selectLedger(options.ledger);
}

request.callback(callback);

return request;
};

/**
* Create a Transaction
*
Expand Down
6 changes: 5 additions & 1 deletion test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const orderbook = {
};

function checkResult(expected, schemaName, response) {
// console.log(JSON.stringify(response, null, 2));
assert.deepEqual(response, expected);
if (schemaName) {
schemaValidator.schemaValidate(schemaName, response);
Expand Down Expand Up @@ -202,6 +201,11 @@ describe('RippleAPI', function() {
_.partial(checkResult, responses.getBalances, 'getBalances'));
});

it('getBalanceSheet', function() {
return this.api.getBalanceSheet(address).then(
_.partial(checkResult, responses.getBalanceSheet, undefined));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can hook up the response schema here by replacing undefined with the schema name. Then it will check that the response always matches the expected schema, testing the code against the schema and the schema against the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

});

describe('getTransaction', () => {
it('getTransaction - payment', function() {
return this.api.getTransaction(hashes.VALID_TRANSACTION_HASH).then(
Expand Down
68 changes: 68 additions & 0 deletions test/fixtures/api/responses/get-balance-sheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"balances": [
{
"counterparty": "rKm4uWpg9tfwbVSeATv4KxDe6mpE9yPkgJ",
"balances": [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making balances and object keyed by the counterparty address to avoid the redundant balances (and similar for assets)? This would make it look similar to parseBalanceChanges: https://www.npmjs.com/package/ripple-lib-transactionparser

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to merge the counterparty into the amounts so that balances is just a list of amounts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how I'd write a schema for your first solution. I could make it a large one-dimensional array of amounts (second suggestion) but the current format would seem preferable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use patternProperties instead of properties. I'm kind of on the fence about which option is better; it's nice to have the explicit notation that the address is the counterparty in the second option, but it's also nice to have the lower redundancy in the first option. If you like the first option better, then we can go with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patternProperties will work with required? Seems simpler to write schema for non-variable property names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's between ripple address properties and one-dimensional array of amounts I choose the latter, but I see nothing wrong with the current format

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid naming that suggests that something is a non-whole component of itself, which is a logical contradiction (balances = balances + counterparty). A one-dimensional array of amounts sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
"currency": "EUR",
"value": "29826.1965999999"
},
{
"currency": "USD",
"value": "10.0"
}
]
},
{
"counterparty": "ra7JkEzrgeKHdzKgo4EUUVBnxggY4z37kt",
"balances": [
{
"currency": "USD",
"value": "13857.70416"
}
]
}
],
"assets": [
{
"counterparty": "r9F6wk8HkXrgYWoJ7fsv4VrUBVoqDVtzkH",
"assets": [
{
"currency": "BTC",
"value": "5444166510000000e-26"
},
{
"currency": "USD",
"value": "100.0"
}
]
},
{
"counterparty": "rwmUaXsWtXU4Z843xSYwgt1is97bgY8yj6",
"assets": [
{
"currency": "BTC",
"value": "8700000000000000e-30"
}
]
}
],
"obligations": [
{
"currency": "BTC",
"value": "5908.324927635318"
},
{
"currency": "EUR",
"value": "992471.7419793958"
},
{
"currency": "GBP",
"value": "4991.38706013193"
},
{
"currency": "USD",
"value": "1997134.20229482"
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/api/responses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
generateAddress: require('./generate-address.json'),
getAccountInfo: require('./get-account-info.json'),
getBalances: require('./get-balances.json'),
getBalanceSheet: require('./get-balance-sheet.json'),
getOrderbook: require('./get-orderbook.json'),
getOrders: require('./get-orders.json'),
getPaths: {
Expand Down
52 changes: 52 additions & 0 deletions test/fixtures/api/rippled/balance-sheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"id": 0,
"status": "success",
"type": "response",
"result": {
"account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"assets": {
"r9F6wk8HkXrgYWoJ7fsv4VrUBVoqDVtzkH": [
{
"currency": "BTC",
"value": "5444166510000000e-26"
},
{
"currency": "USD",
"value": "100.0"
}
],
"rwmUaXsWtXU4Z843xSYwgt1is97bgY8yj6": [
{
"currency": "BTC",
"value": "8700000000000000e-30"
}
]
},
"balances": {
"rKm4uWpg9tfwbVSeATv4KxDe6mpE9yPkgJ": [
{
"currency": "EUR",
"value": "29826.1965999999"
},
{
"currency": "USD",
"value": "10.0"
}
],
"ra7JkEzrgeKHdzKgo4EUUVBnxggY4z37kt": [
{
"currency": "USD",
"value": "13857.70416"
}
]
},
"obligations": {
"BTC": "5908.324927635318",
"EUR": "992471.7419793958",
"GBP": "4991.38706013193",
"USD": "1997134.20229482"
},
"ledger_current_index": 9592219,
"validated": true
}
}
1 change: 1 addition & 0 deletions test/fixtures/api/rippled/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
},
account_offers: require('./account-offers'),
account_tx: require('./account-tx'),
balance_sheet: require('./balance-sheet'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the naming here is supposed to match rippled's names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

book_offers: require('./book-offers'),
server_info: require('./server-info'),
server_info_error: require('./server-info-error'),
Expand Down
4 changes: 4 additions & 0 deletions test/mock-rippled.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,9 @@ module.exports = function(port) {
setTimeout(() => conn.send(response), 20);
});

mock.on('request_gateway_balances', function(request, conn) {
conn.send(createResponse(request, fixtures.balance_sheet));
});

return mock;
};
Loading