-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #579 from wltsmrz/balance-sheet
Balance sheet
- Loading branch information
Showing
14 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters