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

Commit

Permalink
[FIX] set SendMax for different source and destination issuers
Browse files Browse the repository at this point in the history
RLJS-134
  • Loading branch information
geertweening committed Dec 16, 2014
1 parent 68d82a3 commit e772609
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/rest-to-tx-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ RestToTxConverter.prototype.convert = function(payment, callback) {
// SendMax
if (payment.source_amount) {
// Only set send max if source and destination currencies are different
if (!(payment.source_amount.currency === payment.destination_amount.currency && payment.source_amount.issuer === payment.source_amount.issuer)) {
if (!(payment.source_amount.currency === payment.destination_amount.currency && payment.source_amount.issuer === payment.destination_amount.issuer)) {
var max_value = bignum(payment.source_amount.value).plus(payment.source_slippage).toString();
if (payment.source_amount.currency === 'XRP') {
transaction.sendMax(utils.xrpToDrops(max_value));
Expand Down
73 changes: 56 additions & 17 deletions test/unit/fixtures/rest-converter.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
var _ = require('lodash');
var addresses = require('./../../fixtures').addresses;

module.exports.paymentRest = {
"source_account": "r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE",
"destination_account": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"source_account": addresses.VALID,
"destination_account": addresses.COUNTERPARTY,
"destination_amount": {
"value": "0.001",
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
"issuer": addresses.COUNTERPARTY
}
};

module.exports.paymentRestXRP = {
"source_account": "r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE",
"destination_account": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"source_account": addresses.VALID,
"destination_account": addresses.COUNTERPARTY,
"destination_amount": {
"value": "1",
"currency": "XRP",
Expand All @@ -19,20 +22,20 @@ module.exports.paymentRestXRP = {
};

module.exports.paymentRestComplex = {
"source_account": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ",
"source_account": addresses.VALID,
"source_tag": "",
"source_amount": {
"value": "10",
"currency": "USD",
"issuer": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ"
"issuer": addresses.VALID
},
"source_slippage": "0",
"destination_account": "rsE6ZLDkXhSvfJHvSqFPhdazsoMgCEC52V",
"destination_account": addresses.COUNTERPARTY,
"destination_tag": "",
"destination_amount": {
"value": "10",
"currency": "USD",
"issuer": "r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ"
"issuer": addresses.VALID
},
"invoice_id": "",
"paths": "[]",
Expand All @@ -44,13 +47,13 @@ module.exports.paymentTx = {
tx_json: {
Flags: 0,
TransactionType: 'Payment',
Account: 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE',
Account: addresses.VALID,
Amount: {
value: '0.001',
currency: 'USD',
issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
issuer: addresses.COUNTERPARTY
},
Destination: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
Destination: addresses.COUNTERPARTY
},
clientID: undefined,
submittedIDs: [ ],
Expand All @@ -67,9 +70,9 @@ module.exports.paymentTxXRP = {
tx_json: {
Flags: 0,
TransactionType: 'Payment',
Account: 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE',
Account: addresses.VALID,
Amount: '1000000',
Destination: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
Destination: addresses.COUNTERPARTY
},
clientID: undefined,
submittedIDs: [ ],
Expand All @@ -86,13 +89,13 @@ module.exports.paymentTxComplex = {
tx_json: {
Flags: 0,
TransactionType: 'Payment',
Account: 'r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ',
Account: addresses.VALID,
Amount: {
value: '10',
currency: 'USD',
issuer: 'r3YHFNkQRJDPc9aCkRojPLwKVwok3ihgBJ'
issuer: addresses.VALID
},
Destination: 'rsE6ZLDkXhSvfJHvSqFPhdazsoMgCEC52V'
Destination: addresses.COUNTERPARTY
},
clientID: undefined,
submittedIDs: [ ],
Expand All @@ -104,3 +107,39 @@ module.exports.paymentTxComplex = {
server: undefined,
finalized: false
};

module.exports.exportsPaymentRestIssuers = function(options) {

options = options || {};
_.defaults(options, {
sourceAccount: addresses.VALID,
destinationAccount: addresses.COUNTERPARTY,
sourceIssuer: addresses.VALID,
destinationIssuer: addresses.COUNTERPARTY,
sourceSlippage: '0',
sourceValue: '10'
});

return {
source_account: options.sourceAccount,
source_tag: '',
source_amount: {
value: options.sourceValue,
currency: 'USD',
issuer: options.sourceIssuer
},
source_slippage: options.sourceSlippage,
destination_account: options.destinationAccount,
destination_tag: '',
destination_amount: {
value: '10',
currency: 'USD',
issuer: options.destinationIssuer
},
invoice_id: '',
paths: '[]',
partial_payment: false,
no_direct_ripple: false
}

};
43 changes: 42 additions & 1 deletion test/unit/rest-converter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ suite('unit - converter - Rest to Tx', function() {
assert.deepEqual(transaction.summary(), fixtures.paymentTxXRP);
done();
});

});

test('convert() -- payment with additional flags', function(done) {
Expand All @@ -28,7 +27,49 @@ suite('unit - converter - Rest to Tx', function() {
assert.deepEqual(transaction.summary(), fixtures.paymentTxComplex);
done();
});
});

test('convert() -- payment with currency that has same issuer for source and destination amount', function(done) {
restToTxConverter.convert(fixtures.exportsPaymentRestIssuers({
sourceIssuer: addresses.VALID,
destinationIssuer: addresses.VALID
}), function(err, transaction) {
assert.strictEqual(err, null);
assert.strictEqual(transaction.tx_json.SendMax, void(0));
done();
});
});

test('convert() -- payment with currency that has different issuers for source and destination amount', function(done) {
restToTxConverter.convert(fixtures.exportsPaymentRestIssuers({
sourceIssuer: addresses.VALID,
destinationIssuer: addresses.COUNTERPARTY
}), function(err, transaction) {
assert.strictEqual(err, null);
assert.deepEqual(transaction.tx_json.SendMax, {
value: '10',
currency: 'USD',
issuer: addresses.VALID
});
done();
});
});

test('convert() -- payment with currency that has different issuers for source and destination amount and a source_slippage of 0.1', function(done) {
restToTxConverter.convert(fixtures.exportsPaymentRestIssuers({
sourceIssuer: addresses.VALID,
destinationIssuer: addresses.COUNTERPARTY,
sourceSlippage: '0.1',
sourceAmount: '10'
}), function(err, transaction) {
assert.strictEqual(err, null);
assert.deepEqual(transaction.tx_json.SendMax, {
value: '10.1',
currency: 'USD',
issuer: addresses.VALID
});
done();
});
});

});

0 comments on commit e772609

Please sign in to comment.