mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 20:25:48 +00:00
Deprecate positional request constructor API
This commit is contained in:
@@ -28,6 +28,3 @@ exports._test = {
|
||||
};
|
||||
|
||||
exports.types = require('./serializedtypes');
|
||||
|
||||
// This patches remote with legacy support for positional arguments
|
||||
require('./legacy-support.js')(exports);
|
||||
|
||||
@@ -1,209 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const log = require('./log');
|
||||
|
||||
function wrapRemote(Remote) {
|
||||
|
||||
/**
|
||||
* Create options object from positional function arguments
|
||||
*
|
||||
* @param {Array} params function parameters
|
||||
* @param {Array} args function arguments
|
||||
* @return {Object} keyed options
|
||||
*/
|
||||
|
||||
function makeOptions(command, params, args) {
|
||||
const result = {};
|
||||
|
||||
log.warn(
|
||||
'DEPRECATED: First argument to ' + command
|
||||
+ ' request constructor must be an object containing'
|
||||
+ ' request properties: '
|
||||
+ params.join(', ')
|
||||
);
|
||||
|
||||
if (_.isFunction(_.last(args))) {
|
||||
result.callback = args.pop();
|
||||
}
|
||||
|
||||
return _.merge(result, _.zipObject(params, args));
|
||||
}
|
||||
|
||||
function addBackwardsCompatibility(compatParams) {
|
||||
const {method,
|
||||
command,
|
||||
positionals = [],
|
||||
mappings = {},
|
||||
hasCallback = true,
|
||||
aliases = []} = compatParams;
|
||||
|
||||
const needsWrapping = positionals.length ||
|
||||
Object.keys(mappings).length;
|
||||
|
||||
function wrapFunction(func) {
|
||||
return function() {
|
||||
const optionsArg = arguments[0];
|
||||
const options = {};
|
||||
|
||||
if (hasCallback) {
|
||||
options.callback = arguments[1];
|
||||
}
|
||||
|
||||
if (_.isPlainObject(optionsArg)) {
|
||||
const mapped = _.transform(optionsArg, (result, v, k) => {
|
||||
const to = mappings[k];
|
||||
result[to !== undefined ? to : k] = v;
|
||||
});
|
||||
_.merge(options, mapped);
|
||||
} else {
|
||||
const args = _.slice(arguments);
|
||||
const positionalOptions = makeOptions(command, positionals, args);
|
||||
_.merge(options, positionalOptions);
|
||||
}
|
||||
return func.call(this, options, options.callback);
|
||||
};
|
||||
}
|
||||
|
||||
const obj = Remote.prototype;
|
||||
// Wrap the function and set the aliases
|
||||
const wrapped = needsWrapping ? wrapFunction(obj[method]) : obj[method];
|
||||
aliases.concat(method).forEach((name) => {
|
||||
obj[name] = wrapped;
|
||||
});
|
||||
}
|
||||
|
||||
const remoteMethods = [
|
||||
{
|
||||
method: 'requestPathFindCreate',
|
||||
command: 'path_find',
|
||||
positionals: ['source_account',
|
||||
'destination_account',
|
||||
'destination_amount',
|
||||
'source_currencies'],
|
||||
mappings: {
|
||||
src_currencies: 'source_currencies',
|
||||
src_account: 'source_account',
|
||||
dst_amount: 'destination_amount',
|
||||
dst_account: 'destination_account'
|
||||
}
|
||||
},
|
||||
{
|
||||
method: 'requestRipplePathFind',
|
||||
command: 'ripple_path_find',
|
||||
positionals: ['source_account',
|
||||
'destination_account',
|
||||
'destination_amount',
|
||||
'source_currencies'],
|
||||
mappings: {
|
||||
src_currencies: 'source_currencies',
|
||||
src_account: 'source_account',
|
||||
dst_amount: 'destination_amount',
|
||||
dst_account: 'destination_account'
|
||||
}
|
||||
},
|
||||
{
|
||||
method: 'createPathFind',
|
||||
aliases: ['pathFind'],
|
||||
command: 'pathfind',
|
||||
positionals: ['src_account',
|
||||
'dst_account',
|
||||
'dst_amount',
|
||||
'src_currencies']
|
||||
},
|
||||
{
|
||||
method: 'requestTransactionEntry',
|
||||
command: 'transaction_entry',
|
||||
positionals: ['hash', 'ledger'],
|
||||
mappings: {ledger_index: 'ledger', ledger_hash: 'ledger'}
|
||||
},
|
||||
{
|
||||
method: 'requestTransaction',
|
||||
command: 'tx',
|
||||
positionals: ['hash', 'ledger'],
|
||||
mappings: {ledger_index: 'ledger', ledger_hash: 'ledger'},
|
||||
aliases: ['requestTx']
|
||||
},
|
||||
{
|
||||
method: 'requestBookOffers',
|
||||
command: 'book_offers',
|
||||
positionals: ['gets', 'pays', 'taker', 'ledger', 'limit'],
|
||||
mappings: {taker_pays: 'pays', taker_gets: 'gets'}
|
||||
},
|
||||
{
|
||||
method: 'createOrderBook',
|
||||
hasCallback: false,
|
||||
command: 'orderbook',
|
||||
positionals: ['currency_gets', 'issuer_gets',
|
||||
'currency_pays', 'issuer_pays']
|
||||
},
|
||||
{
|
||||
method: 'requestTransactionHistory',
|
||||
command: 'tx_history',
|
||||
positionals: ['start'],
|
||||
aliases: ['requestTxHistory']
|
||||
},
|
||||
{
|
||||
method: 'requestWalletAccounts',
|
||||
command: 'wallet_accounts',
|
||||
positionals: ['seed']
|
||||
},
|
||||
{
|
||||
method: 'requestSign',
|
||||
command: 'sign',
|
||||
positionals: ['secret', 'tx_json']
|
||||
},
|
||||
{
|
||||
method: 'accountSeqCache',
|
||||
command: 'accountseqcache',
|
||||
positionals: ['account', 'ledger']
|
||||
},
|
||||
{
|
||||
method: 'requestRippleBalance',
|
||||
command: 'ripplebalance',
|
||||
positionals: ['account', 'issuer', 'currency', 'ledger']
|
||||
},
|
||||
{
|
||||
method: 'requestAccountInfo',
|
||||
command: 'account_info',
|
||||
positionals: ['account', 'ledger', 'peer', 'limit', 'marker']
|
||||
},
|
||||
{
|
||||
method: 'requestAccountCurrencies',
|
||||
command: 'account_currencies',
|
||||
positionals: ['account', 'ledger', 'peer', 'limit', 'marker']
|
||||
},
|
||||
{
|
||||
method: 'requestAccountLines',
|
||||
command: 'account_lines',
|
||||
positionals: ['account', 'peer', 'ledger', 'limit', 'marker']
|
||||
},
|
||||
{
|
||||
method: 'requestAccountOffers',
|
||||
command: 'account_offers',
|
||||
positionals: ['account', 'ledger', 'peer', 'limit', 'marker']
|
||||
},
|
||||
|
||||
{
|
||||
method: 'requestAccountBalance',
|
||||
command: 'account_balance',
|
||||
positionals: ['account', 'ledger']
|
||||
},
|
||||
{
|
||||
method: 'requestAccountFlags',
|
||||
command: 'account_flags',
|
||||
positionals: ['account', 'ledger']
|
||||
},
|
||||
{
|
||||
method: 'requestOwnerCount',
|
||||
command: 'owner_count',
|
||||
positionals: ['account', 'ledger']
|
||||
}
|
||||
];
|
||||
|
||||
remoteMethods.forEach(addBackwardsCompatibility);
|
||||
}
|
||||
|
||||
module.exports = function wrapAPI(index) {
|
||||
wrapRemote(index.Remote);
|
||||
};
|
||||
@@ -1121,6 +1121,7 @@ Remote.prototype.requestTransactionEntry = function(options, callback) {
|
||||
* @return {Request} request
|
||||
*/
|
||||
|
||||
Remote.prototype.requestTx =
|
||||
Remote.prototype.requestTransaction = function(options, callback) {
|
||||
const request = new Request(this, 'tx');
|
||||
request.message.binary = options.binary !== false;
|
||||
@@ -1476,8 +1477,8 @@ Remote.prototype.requestTransactionHistory = function(options, callback) {
|
||||
* Request book_offers
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {Object} options.gets - taker_gets with issuer and currency
|
||||
* @param {Object} options.pays - taker_pays with issuer and currency
|
||||
* @param {Object} options.taker_gets - taker_gets with issuer and currency
|
||||
* @param {Object} options.taker_pays - taker_pays with issuer and currency
|
||||
* @param {String} [options.taker]
|
||||
* @param {String} [options.ledger]
|
||||
* @param {String|Number} [options.limit]
|
||||
@@ -1486,23 +1487,32 @@ Remote.prototype.requestTransactionHistory = function(options, callback) {
|
||||
*/
|
||||
|
||||
Remote.prototype.requestBookOffers = function(options, callback) {
|
||||
const {gets, pays, taker, ledger, limit} = options;
|
||||
const {taker, ledger, limit} = options;
|
||||
let {taker_gets, taker_pays} = options;
|
||||
|
||||
if (taker_gets === undefined) {
|
||||
taker_gets = options.gets;
|
||||
}
|
||||
if (taker_pays === undefined) {
|
||||
taker_pays = options.pays;
|
||||
}
|
||||
|
||||
const request = new Request(this, 'book_offers');
|
||||
|
||||
request.message.taker_gets = {
|
||||
currency: Currency.json_rewrite(gets.currency, {force_hex: true})
|
||||
currency: Currency.json_rewrite(taker_gets.currency, {force_hex: true})
|
||||
};
|
||||
|
||||
if (!Currency.from_json(request.message.taker_gets.currency).is_native()) {
|
||||
request.message.taker_gets.issuer = UInt160.json_rewrite(gets.issuer);
|
||||
request.message.taker_gets.issuer = UInt160.json_rewrite(taker_gets.issuer);
|
||||
}
|
||||
|
||||
request.message.taker_pays = {
|
||||
currency: Currency.json_rewrite(pays.currency, {force_hex: true})
|
||||
currency: Currency.json_rewrite(taker_pays.currency, {force_hex: true})
|
||||
};
|
||||
|
||||
if (!Currency.from_json(request.message.taker_pays.currency).is_native()) {
|
||||
request.message.taker_pays.issuer = UInt160.json_rewrite(pays.issuer);
|
||||
request.message.taker_pays.issuer = UInt160.json_rewrite(taker_pays.issuer);
|
||||
}
|
||||
|
||||
request.message.taker = taker ? taker : UInt160.ACCOUNT_ONE;
|
||||
@@ -1526,6 +1536,7 @@ Remote.prototype.requestBookOffers = function(options, callback) {
|
||||
}
|
||||
|
||||
request.callback(callback);
|
||||
|
||||
return request;
|
||||
};
|
||||
|
||||
|
||||
@@ -1263,39 +1263,27 @@ describe('Remote', function() {
|
||||
});
|
||||
});
|
||||
it('Construct account_currencies request', function() {
|
||||
let request = remote.requestAccountCurrencies({
|
||||
const request = remote.requestAccountCurrencies({
|
||||
account: ADDRESS
|
||||
}, lodash.noop);
|
||||
|
||||
assert.strictEqual(request.message.command, 'account_currencies');
|
||||
assert.strictEqual(request.message.account, ADDRESS);
|
||||
assert.strictEqual(request.requested, true);
|
||||
|
||||
Log.setEngine(Log.engines.none);
|
||||
request = remote.requestAccountCurrencies(ADDRESS, lodash.noop);
|
||||
assert.strictEqual(request.message.command, 'account_currencies');
|
||||
assert.strictEqual(request.message.account, ADDRESS);
|
||||
assert.strictEqual(request.requested, true);
|
||||
});
|
||||
|
||||
it('Construct account_info request', function() {
|
||||
let request = remote.requestAccountInfo({
|
||||
const request = remote.requestAccountInfo({
|
||||
account: ADDRESS
|
||||
}, lodash.noop);
|
||||
|
||||
assert.strictEqual(request.message.command, 'account_info');
|
||||
assert.strictEqual(request.message.account, ADDRESS);
|
||||
assert.strictEqual(request.requested, true);
|
||||
|
||||
Log.setEngine(Log.engines.none);
|
||||
request = remote.requestAccountInfo(ADDRESS, lodash.noop);
|
||||
assert.strictEqual(request.message.command, 'account_info');
|
||||
assert.strictEqual(request.message.account, ADDRESS);
|
||||
assert.strictEqual(request.requested, true);
|
||||
});
|
||||
|
||||
it('Construct account_info request -- with ledger index', function() {
|
||||
let request = remote.requestAccountInfo({
|
||||
const request = remote.requestAccountInfo({
|
||||
account: ADDRESS,
|
||||
ledger: 9592219
|
||||
}, lodash.noop);
|
||||
@@ -1303,14 +1291,6 @@ describe('Remote', function() {
|
||||
assert.strictEqual(request.message.account, ADDRESS);
|
||||
assert.strictEqual(request.message.ledger_index, 9592219);
|
||||
assert.strictEqual(request.requested, true);
|
||||
|
||||
Log.setEngine(Log.engines.none);
|
||||
request = remote.requestAccountInfo(ADDRESS, 9592219, lodash.noop);
|
||||
assert.strictEqual(request.requested, true);
|
||||
|
||||
assert.strictEqual(request.message.command, 'account_info');
|
||||
assert.strictEqual(request.message.account, ADDRESS);
|
||||
assert.strictEqual(request.message.ledger_index, 9592219);
|
||||
});
|
||||
|
||||
it('Construct account_info request -- with ledger hash', function() {
|
||||
@@ -1372,14 +1352,7 @@ describe('Remote', function() {
|
||||
assert.strictEqual(request.requested, true);
|
||||
});
|
||||
it('Construct account owner count request', function() {
|
||||
let request = remote.requestOwnerCount({account: ADDRESS}, lodash.noop);
|
||||
assert.strictEqual(request.message.command, 'ledger_entry');
|
||||
assert.strictEqual(request.message.account_root, ADDRESS);
|
||||
assert.strictEqual(request.requested, true);
|
||||
|
||||
Log.setEngine(Log.engines.none);
|
||||
request = remote.requestOwnerCount(ADDRESS, lodash.noop);
|
||||
|
||||
const request = remote.requestOwnerCount({account: ADDRESS}, lodash.noop);
|
||||
assert.strictEqual(request.message.command, 'ledger_entry');
|
||||
assert.strictEqual(request.message.account_root, ADDRESS);
|
||||
assert.strictEqual(request.requested, true);
|
||||
@@ -1421,7 +1394,7 @@ describe('Remote', function() {
|
||||
assert.strictEqual(request.requested, true);
|
||||
});
|
||||
it('Construct account_lines request -- with limit and marker', function() {
|
||||
let request = remote.requestAccountLines({
|
||||
const request = remote.requestAccountLines({
|
||||
account: ADDRESS,
|
||||
limit: 100,
|
||||
marker: PAGING_MARKER,
|
||||
@@ -1436,26 +1409,6 @@ describe('Remote', function() {
|
||||
ledger_index: 9592219
|
||||
});
|
||||
assert.strictEqual(request.requested, true);
|
||||
|
||||
Log.setEngine(Log.engines.none);
|
||||
request = remote.requestAccountLines(
|
||||
ADDRESS,
|
||||
null,
|
||||
9592219,
|
||||
100,
|
||||
PAGING_MARKER,
|
||||
lodash.noop
|
||||
);
|
||||
|
||||
assert.deepEqual(request.message, {
|
||||
command: 'account_lines',
|
||||
id: undefined,
|
||||
account: ADDRESS,
|
||||
limit: 100,
|
||||
marker: PAGING_MARKER,
|
||||
ledger_index: 9592219
|
||||
});
|
||||
assert.strictEqual(request.requested, true);
|
||||
});
|
||||
it('Construct account_lines request -- with min limit', function() {
|
||||
assert.strictEqual(remote.requestAccountLines({
|
||||
@@ -1668,11 +1621,11 @@ describe('Remote', function() {
|
||||
|
||||
it('Construct book_offers request -- with ledger and limit', function() {
|
||||
const request = remote.requestBookOffers({
|
||||
taker_gets: {
|
||||
gets: {
|
||||
currency: 'USD',
|
||||
issuer: ADDRESS
|
||||
},
|
||||
taker_pays: {
|
||||
pays: {
|
||||
currency: 'XRP'
|
||||
},
|
||||
ledger: LEDGER_HASH,
|
||||
@@ -1926,10 +1879,10 @@ describe('Remote', function() {
|
||||
|
||||
it('Construct ripple_path_find request', function() {
|
||||
const request = remote.requestRipplePathFind({
|
||||
src_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
dst_account: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6',
|
||||
dst_amount: '1/USD/rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
src_currencies: [{
|
||||
source_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
destination_account: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6',
|
||||
destination_amount: '1/USD/rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
source_currencies: [{
|
||||
currency: 'BTC', issuer: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6'
|
||||
}]
|
||||
});
|
||||
@@ -1974,10 +1927,10 @@ describe('Remote', function() {
|
||||
|
||||
it('Construct path_find create request', function() {
|
||||
const request = remote.requestPathFindCreate({
|
||||
src_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
dst_account: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6',
|
||||
dst_amount: '1/USD/rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
src_currencies: [{
|
||||
source_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
destination_account: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6',
|
||||
destination_amount: '1/USD/rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',
|
||||
source_currencies: [{
|
||||
currency: 'BTC', issuer: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6'
|
||||
}]
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user