Skip to content

Commit 7514213

Browse files
author
Alan Cohen
committed
Fix: Specify send_max when pathfinding with a source amount
When specifying a fixed sending amount during a pathfind request, the source amount is specified as a `send_max` field. This fixes a bug where the amount was specified as `source_amount`. Leading to strange pathfinding behavior. Example bad behavior (rippled pathfind request/response): ``` { "command": "ripple_path_find", "source_account": "rM21sWyMAJY1oqzgnweDatURxGMurBs7Kf", "destination_account": "raDjTrcEtyMqZT6s3PyKGcikUJqYNXUPPv", "destination_amount": { "currency": "EUR", "issuer": "raDjTrcEtyMqZT6s3PyKGcikUJqYNXUPPv", "value": -1 }, "source_amount": { "currency": "USD", "issuer": "rM21sWyMAJY1oqzgnweDatURxGMurBs7Kf", "value": "9.9" }, "id": 2 } { "id": 2, "result": { "alternatives": [ { "destination_amount": { "currency": "EUR", "issuer": "raDjTrcEtyMqZT6s3PyKGcikUJqYNXUPPv", "value": "147520.7583553951" }, "paths_canonical": [], "paths_computed": [ [ { "account": "r9HqF3wexBb1vtu2DfZKiFuyy3HoTAwUnH", "type": 1, "type_hex": "0000000000000001" }, { "currency": "EUR", "issuer": "raDjTrcEtyMqZT6s3PyKGcikUJqYNXUPPv", "type": 48, "type_hex": "0000000000000030" } ] ], "source_amount": { "currency": "USD", "issuer": "rM21sWyMAJY1oqzgnweDatURxGMurBs7Kf", "value": "160510.6025237665" } } ], "destination_account": "raDjTrcEtyMqZT6s3PyKGcikUJqYNXUPPv", "destination_currencies": [ "EUR", "XRP" ], "ledger_current_index": 2771044, "validated": false }, "status": "success", "type": "response" } ``` https://ripple.com/build/rippled-apis/#ripple-path-find
1 parent 849ba99 commit 7514213

File tree

3 files changed

+73
-58
lines changed

3 files changed

+73
-58
lines changed

src/ledger/pathfind-types.js

+55-55
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
/* @flow */
2-
'use strict';
3-
4-
import type {Amount, LaxLaxAmount, RippledAmount, Adjustment, MaxAdjustment,
5-
MinAdjustment} from '../common/types.js';
6-
7-
8-
type Path = {
9-
source: Adjustment | MaxAdjustment,
10-
destination: Adjustment | MinAdjustment,
11-
paths: string
12-
}
13-
14-
export type GetPaths = Array<Path>
15-
16-
export type PathFind = {
17-
source: {
18-
address: string,
19-
amount?: Amount,
20-
currencies?: Array<{currency: string, counterparty?:string}>
21-
},
22-
destination: {
23-
address: string,
24-
amount: LaxLaxAmount
25-
}
26-
}
27-
28-
export type PathFindRequest = {
29-
command: string,
30-
source_account: string,
31-
destination_amount: RippledAmount,
32-
destination_account: string,
33-
source_amount?: RippledAmount,
34-
source_currencies?: Array<string>
35-
}
36-
37-
export type RippledPathsResponse = {
38-
alternatives: Array<{
39-
paths_computed: Array<Array<{
40-
type: number,
41-
type_hex: string,
42-
account?: string,
43-
issuer?: string,
44-
currency?: string
45-
}>>,
46-
source_amount: RippledAmount
47-
}>,
48-
type: string,
49-
destination_account: string,
50-
destination_amount: RippledAmount,
51-
destination_currencies?: Array<string>,
52-
source_account?: string,
53-
source_currencies?: Array<{currency: string}>,
54-
full_reply?: boolean
55-
}
1+
/* @flow */
2+
'use strict';
3+
4+
import type {Amount, LaxLaxAmount, RippledAmount, Adjustment, MaxAdjustment,
5+
MinAdjustment} from '../common/types.js';
6+
7+
8+
type Path = {
9+
source: Adjustment | MaxAdjustment,
10+
destination: Adjustment | MinAdjustment,
11+
paths: string
12+
}
13+
14+
export type GetPaths = Array<Path>
15+
16+
export type PathFind = {
17+
source: {
18+
address: string,
19+
amount?: Amount,
20+
currencies?: Array<{currency: string, counterparty?:string}>
21+
},
22+
destination: {
23+
address: string,
24+
amount: LaxLaxAmount
25+
}
26+
}
27+
28+
export type PathFindRequest = {
29+
command: string,
30+
source_account: string,
31+
destination_amount: RippledAmount,
32+
destination_account: string,
33+
source_currencies?: Array<string>,
34+
send_max?: RippledAmount
35+
}
36+
37+
export type RippledPathsResponse = {
38+
alternatives: Array<{
39+
paths_computed: Array<Array<{
40+
type: number,
41+
type_hex: string,
42+
account?: string,
43+
issuer?: string,
44+
currency?: string
45+
}>>,
46+
source_amount: RippledAmount
47+
}>,
48+
type: string,
49+
destination_account: string,
50+
destination_amount: RippledAmount,
51+
destination_currencies?: Array<string>,
52+
source_account?: string,
53+
source_currencies?: Array<{currency: string}>,
54+
full_reply?: boolean
55+
}

src/ledger/pathfind.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ function requestPathFind(connection: Connection, pathfind: PathFind): Promise {
4646
throw new ValidationError('Cannot specify both source.amount'
4747
+ ' and destination.amount.value in getPaths');
4848
}
49-
request.source_amount = toRippledAmount(pathfind.source.amount);
50-
if (request.source_amount.currency && !request.source_amount.issuer) {
51-
request.source_amount.issuer = pathfind.source.address;
49+
request.send_max = toRippledAmount(pathfind.source.amount);
50+
if (request.send_max.currency && !request.send_max.issuer) {
51+
request.send_max.issuer = pathfind.source.address;
5252
}
5353
}
5454

test/integration/integration-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ describe('integration tests', function() {
289289
});
290290
});
291291

292+
it('getPaths - send all', function() {
293+
const pathfind = requests.getPaths.sendAll;
294+
return this.api.getPaths(pathfind).then(data => {
295+
assert(data && data.length > 0);
296+
assert(_.every(data, path => {
297+
return parseFloat(path.source.amount.value)
298+
<= parseFloat(pathfind.source.amount.value);
299+
}));
300+
const path = data[0];
301+
assert(path && path.source);
302+
assert.strictEqual(path.source.address, pathfind.source.address);
303+
assert(path.paths && path.paths.length > 0);
304+
});
305+
});
306+
292307

293308
it('generateWallet', function() {
294309
const newWallet = this.api.generateAddress();

0 commit comments

Comments
 (0)