-
Notifications
You must be signed in to change notification settings - Fork 524
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
Balance sheet #579
Changes from 2 commits
323e402
806a4e8
772f79a
78eeb40
607777f
ff2ac6c
64baef4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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", | ||
"properties": { | ||
"required": ["counterparty", "balances"], | ||
"additionalProperties": false, | ||
"counterparty": {"$ref": "address"}, | ||
"balances": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"required": ["currency", "value"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lodash map gives you both value and key, so you could do There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can hook up the response schema here by replacing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"balances": [ | ||
{ | ||
"counterparty": "rKm4uWpg9tfwbVSeATv4KxDe6mpE9yPkgJ", | ||
"balances": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about making There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option would be to merge the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. patternProperties will work with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} | ||
] | ||
} |
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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ module.exports = { | |
}, | ||
account_offers: require('./account-offers'), | ||
account_tx: require('./account-tx'), | ||
balance_sheet: require('./balance-sheet'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the naming here is supposed to match rippled's names There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'), | ||
|
There was a problem hiding this comment.
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.