Skip to content

Commit

Permalink
Merge pull request #579 from wltsmrz/balance-sheet
Browse files Browse the repository at this point in the history
Balance sheet
  • Loading branch information
clark800 committed Oct 5, 2015
2 parents 634fe56 + 64baef4 commit 29c37aa
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 1 deletion.
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
}
29 changes: 29 additions & 0 deletions src/api/common/schemas/get-balance-sheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "getBalanceSheet",
"description": "getBalanceSheet response",
"type": "object",
"properties": {
"balances": {
"type": "array",
"items": {"$ref": "amount"}
},
"assets": {
"type": "array",
"items": {"$ref": "amount"}
},
"obligations": {
"type": "array",
"items": {
"type": "object",
"required": ["currency", "value"],
"additionalProperties": false,
"properties": {
"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
68 changes: 68 additions & 0 deletions src/api/ledger/balance-sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'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(balanceSheet) {
const result = {};

if (!_.isUndefined(balanceSheet.balances)) {
result.balances = [];
_.forEach(balanceSheet.balances, (balances, counterparty) => {
_.forEach(balances, (balance) => {
result.balances.push(Object.assign({counterparty}, balance));
});
});
}
if (!_.isUndefined(balanceSheet.assets)) {
result.assets = [];
_.forEach(balanceSheet.assets, (assets, counterparty) => {
_.forEach(assets, (balance) => {
result.assets.push(Object.assign({counterparty}, balance));
});
});
}
if (!_.isUndefined(balanceSheet.obligations)) {
result.obligations = _.map(balanceSheet.obligations, (value, currency) =>
({currency, value}));
}

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, 'getBalanceSheet'));
});

describe('getTransaction', () => {
it('getTransaction - payment', function() {
return this.api.getTransaction(hashes.VALID_TRANSACTION_HASH).then(
Expand Down
54 changes: 54 additions & 0 deletions test/fixtures/api/responses/get-balance-sheet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"balances": [
{
"counterparty": "rKm4uWpg9tfwbVSeATv4KxDe6mpE9yPkgJ",
"currency": "EUR",
"value": "29826.1965999999"
},
{
"counterparty": "rKm4uWpg9tfwbVSeATv4KxDe6mpE9yPkgJ",
"currency": "USD",
"value": "10.0"
},
{
"counterparty": "ra7JkEzrgeKHdzKgo4EUUVBnxggY4z37kt",
"currency": "USD",
"value": "13857.70416"
}
],
"assets": [
{
"counterparty": "r9F6wk8HkXrgYWoJ7fsv4VrUBVoqDVtzkH",
"currency": "BTC",
"value": "5444166510000000e-26"
},
{
"counterparty": "r9F6wk8HkXrgYWoJ7fsv4VrUBVoqDVtzkH",
"currency": "USD",
"value": "100.0"
},
{
"counterparty": "rwmUaXsWtXU4Z843xSYwgt1is97bgY8yj6",
"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/gateway-balances.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'),
gateway_balances: require('./gateway-balances'),
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.gateway_balances));
});

return mock;
};
16 changes: 16 additions & 0 deletions test/remote-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,22 @@ describe('Remote', function() {
});
});

it('Construct gateway_balances request', function() {
const request = remote.requestGatewayBalances({
account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
hotwallet: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5',
strict: true
});

assert.deepEqual(request.message, {
command: 'gateway_balances',
id: undefined,
account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
hotwallet: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5',
strict: true
});
});

it('Construct Payment transaction', function() {
const tx = remote.createTransaction('Payment', {
account: TX_JSON.Account,
Expand Down

0 comments on commit 29c37aa

Please sign in to comment.