mirror of
https://github.com/Xahau/xahau.js.git
synced 2026-04-29 15:37:50 +00:00
Add getBalanceSheet() to RippleAPI
This commit is contained in:
@@ -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'),
|
||||
|
||||
15
src/api/common/schemas/balance-sheet-options.json
Normal file
15
src/api/common/schemas/balance-sheet-options.json
Normal file
@@ -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
src/api/common/schemas/get-balance-sheet.json
Normal file
67
src/api/common/schemas/get-balance-sheet.json
Normal file
@@ -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"],
|
||||
"additionalProperties": false,
|
||||
"currency": {"$ref": "currency"},
|
||||
"value": {"$ref": "value"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"assets": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"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
|
||||
}
|
||||
@@ -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'),
|
||||
|
||||
@@ -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');
|
||||
@@ -66,6 +67,7 @@ _.assign(RippleAPI.prototype, {
|
||||
getTransactions,
|
||||
getTrustlines,
|
||||
getBalances,
|
||||
getBalanceSheet,
|
||||
getPaths,
|
||||
getOrders,
|
||||
getOrderbook,
|
||||
|
||||
69
src/api/ledger/balance-sheet.js
Normal file
69
src/api/ledger/balance-sheet.js
Normal file
@@ -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) => {
|
||||
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;
|
||||
Reference in New Issue
Block a user