Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[TASK] refactor hash, ledger, and state fields for create payment, se…
Browse files Browse the repository at this point in the history
…t trust line, create order, cancel order, and update settings to be on root response object
  • Loading branch information
boxbag committed Jan 7, 2015
1 parent 4ae2b7e commit fbc8840
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 166 deletions.
14 changes: 13 additions & 1 deletion api/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,19 @@ function placeOrder(request, response, next) {
function validateParams(callback) {
if (!params.order) {
return callback(new InvalidRequestError('Missing parameter: order. Submission must have order object in JSON form'));
} else if (!ripple.UInt160.is_valid(params.account)) {
} else {
if (params.order.taker_gets && params.order.taker_gets.currency !== 'XRP') {
params.order.taker_gets.issuer = params.order.taker_gets.counterparty;
delete params.order.taker_gets.counterparty;
}

if (params.order.taker_pays && params.order.taker_pays.currency !== 'XRP') {
params.order.taker_pays.issuer = params.order.taker_pays.counterparty;
delete params.order.taker_pays.counterparty;
}
}

if (!ripple.UInt160.is_valid(params.account)) {
return callback(new errors.InvalidRequestError('Parameter is not a valid Ripple address: account'));
} else if (!/^buy|sell$/.test(params.order.type)) {
return callback(new InvalidRequestError('Parameter must be "buy" or "sell": type'));
Expand Down
30 changes: 26 additions & 4 deletions api/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ function submitPayment(request, response, next) {
return callback(error);
}

callback(null, { payment: payment });
var result = {
payment: payment
};

_.extend(result, meta);

callback(null, result);
});
}

Expand Down Expand Up @@ -312,15 +318,31 @@ function getPayment(request, response, next) {
validateOptions,
getTransaction,
function (transaction, callback) {
formatPaymentHelper(options.account, transaction, callback);
formatPaymentHelper(options.account, transaction, function (err, payment) {
if (err) {
return callback(err);
}

var result = {
payment: payment
};

_.extend(result, {
hash: transaction.hash || '',
ledger: !_.isUndefined(transaction.inLedger) ? String(transaction.inLedger) : String(transaction.ledger_index),
state: transaction.state || transaction.meta ? (transaction.meta.TransactionResult === 'tesSUCCESS' ? 'validated' : 'failed') : ''
});

callback(null, result);
});
}
];

async.waterfall(steps, function(error, payment) {
async.waterfall(steps, function(error, result) {
if (error) {
next(error);
} else {
respond.success(response, { payment: payment });
respond.success(response, result);
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion api/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function submitTransaction(options, hooks, callback) {
'with the same client_resource_id. If this was not an accidental resubmission ' +
'please submit the transaction again with a unique client_resource_id')
);
}
}

callback(null, transaction);
});
Expand Down
56 changes: 21 additions & 35 deletions lib/tx-to-rest-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function TxToRestConverter() {};
*/

TxToRestConverter.prototype.parsePaymentFromTx = function(tx, options, callback) {

if (typeof options === 'function') {
callback = options;
options = {};
Expand Down Expand Up @@ -104,10 +103,7 @@ TxToRestConverter.prototype.parsePaymentFromTx = function(tx, options, callback)
'incoming' :
'passthrough')) :
''),
state: tx.state || tx.meta ? (tx.meta.TransactionResult === 'tesSUCCESS' ? 'validated' : 'failed') : '',
result: tx.meta ? tx.meta.TransactionResult : '',
ledger: '' + (tx.inLedger || tx.ledger_index),
hash: tx.hash || '',
timestamp: (tx.date ? new Date(ripple.utils.toTimestamp(tx.date)).toISOString() : ''),
fee: utils.dropsToXrp(tx.Fee) || '',
source_balance_changes: [],
Expand Down Expand Up @@ -221,47 +217,36 @@ TxToRestConverter.prototype.parsePaymentsFromPathFind = function(pathfindResults
// Orders

TxToRestConverter.prototype.parseCancelOrderFromTx = function(message, meta, callback) {
var result = {};
_.extend(meta, {
var result = {
order: {}
};

_.extend(result.order, {
account: message.tx_json.Account,
fee: utils.dropsToXrp(message.tx_json.Fee),
offer_sequence: message.tx_json.OfferSequence,
sequence: message.tx_json.Sequence
});

result.order = meta;
_.extend(result, meta);

callback(null, result);
};

TxToRestConverter.prototype.parseSubmitOrderFromTx = function(message, meta, callback) {
var result = {};
_.extend(meta, {
var result = {
order: {}
};

_.extend(result.order, {
account: message.tx_json.Account,
taker_gets: message.tx_json.TakerGets,
taker_pays: message.tx_json.TakerPays,
taker_gets: utils.parseCurrencyAmount(message.tx_json.TakerGets),
taker_pays: utils.parseCurrencyAmount(message.tx_json.TakerPays),
fee: utils.dropsToXrp(message.tx_json.Fee),
type: (message.tx_json.Flags & ripple.Transaction.flags.OfferCreate.Sell) > 0 ? 'sell' : 'buy',
sequence: message.tx_json.Sequence
});

if (_.isString(meta.taker_gets)) {
meta.taker_gets = {
currency: 'XRP',
value: utils.dropsToXrp(meta.taker_gets),
issuer: ''
}
}

if (_.isString(meta.taker_pays)) {
meta.taker_pays = {
currency: 'XRP',
value: utils.dropsToXrp(meta.taker_pays),
issuer: ''
}
}

result.order = meta;
_.extend(result, meta);

callback(null, result);
};
Expand All @@ -275,11 +260,13 @@ const TrustSetResponseFlags = {
};

TxToRestConverter.prototype.parseTrustResponseFromTx = function(message, meta, callback) {
var result = {};
var result = {
trustline: {}
};
var line = message.tx_json.LimitAmount;
var parsedFlags = TxToRestConverter.prototype.parseFlagsFromResponse(message.tx_json.Flags, TrustSetResponseFlags);

_.extend(meta, {
_.extend(result.trustline, {
account: message.tx_json.Account,
limit: line.value,
currency: line.currency,
Expand All @@ -288,8 +275,7 @@ TxToRestConverter.prototype.parseTrustResponseFromTx = function(message, meta, c
account_trustline_frozen: parsedFlags.account_trustline_frozen,
authorized: parsedFlags.authorized ? parsedFlags.authorized : void(0)
});

result.trustline = meta;
_.extend(result, meta);

callback(null, result);
};
Expand Down Expand Up @@ -322,8 +308,8 @@ TxToRestConverter.prototype.parseSettingResponseFromTx = function(params, messag
result.settings[field.name] = params.settings[field.name];
}

_.extend(meta, TxToRestConverter.prototype.parseFlagsFromResponse(message.tx_json.Flags, AccountSetResponseFlags));
_.extend(result.settings, meta);
_.extend(result.settings, TxToRestConverter.prototype.parseFlagsFromResponse(message.tx_json.Flags, AccountSetResponseFlags));
_.extend(result, meta);

callback(null, result);
};
Expand Down
46 changes: 21 additions & 25 deletions test/_payments-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,8 @@ suite('payments', function() {
.end(done);
});


// confirm payment via client resource ID
test('check status url of the reserve_base_xrp transfer from alice to bob', function(done) {

orderlist.create([{command:'tx'}])
var _tx = function(data,ws) {
delete data.id;
Expand Down Expand Up @@ -443,29 +441,28 @@ suite('payments', function() {
no_direct_ripple: false,
partial_payment: false,
direction: 'outgoing',
state: '',
result: '',
ledger: 'undefined',
hash: payment.hash,
timestamp: '',
fee: '0.000012',
source_balance_changes: [],
destination_balance_changes: []
};

store.hash = payment.hash;
store.hash = resp.body.hash;
var keys = Object.keys(statusPayment);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
assert.deepEqual(payment[key], statusPayment[key]);
}
assert.equal(resp.body.hash, '8EA3CF4D854669007058EB45E9860611CC24FEB655895E418A5C8BC5EA901D01');
assert.equal(resp.body.ledger, 'undefined');
assert.equal(resp.body.state, '');
assert.equal(orderlist.test(),true);
orderlist.reset();
})
.end(done);
});


// confirm payment via transaction hash
test('confirm payment via transaction hash', function(done) {
orderlist.create([{command:'tx'}]);
Expand Down Expand Up @@ -493,10 +490,7 @@ suite('payments', function() {
no_direct_ripple: false,
partial_payment: false,
direction: 'outgoing',
state: '',
result: '',
ledger: 'undefined',
hash: store.hash,
timestamp: '',
fee: '0.000012',
source_balance_changes: [],
Expand All @@ -508,13 +502,15 @@ suite('payments', function() {
var key = keys[i];
assert.deepEqual(payment[key], statusPayment[key]);
};
assert.equal(resp.body.hash, '8EA3CF4D854669007058EB45E9860611CC24FEB655895E418A5C8BC5EA901D01');
assert.equal(resp.body.ledger, 'undefined');
assert.equal(resp.body.state, '');
assert.equal(orderlist.test(),true);
orderlist.reset();
})
.end(done);
});


test('check amount bob has',function(done) {
orderlist.create([
{command:'account_info'},
Expand Down Expand Up @@ -1000,11 +996,11 @@ suite('payments', function() {
})
.expect(function(resp) {
assert.equal(resp.status,201);
assert.strictEqual(typeof resp.body.trustline.hash, 'string');
assert.strictEqual(typeof resp.body.trustline.hash, 'string');
assert.strictEqual(typeof resp.body.hash, 'string');
assert.strictEqual(typeof resp.body.ledger, 'string');

delete resp.body.trustline.hash;
delete resp.body.trustline.ledger;
delete resp.body.hash;
delete resp.body.ledger;
assert.deepEqual(resp.body,{
"success": true,
"trustline": {
Expand All @@ -1013,9 +1009,9 @@ suite('payments', function() {
"currency": "USD",
"counterparty": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ",
"account_allows_rippling": true,
"account_trustline_frozen": false,
"state": "pending"
}
"account_trustline_frozen": false
},
"state": "pending"
});
})
.end(done);
Expand All @@ -1034,11 +1030,11 @@ suite('payments', function() {
})
.expect(function(resp) {
assert.equal(resp.status,201);
assert.strictEqual(typeof resp.body.trustline.hash, 'string');
assert.strictEqual(typeof resp.body.trustline.hash, 'string');
assert.strictEqual(typeof resp.body.hash, 'string');
assert.strictEqual(typeof resp.body.ledger, 'string');

delete resp.body.trustline.hash;
delete resp.body.trustline.ledger;
delete resp.body.hash;
delete resp.body.ledger;
assert.deepEqual(resp.body, {
"success": true,
"trustline": {
Expand All @@ -1047,9 +1043,9 @@ suite('payments', function() {
"currency": "USD",
"counterparty": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ",
"account_allows_rippling": true,
"account_trustline_frozen": false,
"state": "pending"
}
"account_trustline_frozen": false
},
"state": "pending"
});
})
.end(done);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = {
SECRET: 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV',
INVALID: 'rxxLy6UWsjzxsQrTATf1bwDYSaJMoTGvfY2Q',
COUNTERPARTY: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
};
};
Loading

0 comments on commit fbc8840

Please sign in to comment.