Skip to content

Commit

Permalink
Fix: Specify send_max when pathfinding with a source amount
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alan Cohen committed Nov 20, 2015
1 parent 849ba99 commit 7514213
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 58 deletions.
110 changes: 55 additions & 55 deletions src/ledger/pathfind-types.js
@@ -1,55 +1,55 @@
/* @flow */
'use strict';

import type {Amount, LaxLaxAmount, RippledAmount, Adjustment, MaxAdjustment,
MinAdjustment} from '../common/types.js';


type Path = {
source: Adjustment | MaxAdjustment,
destination: Adjustment | MinAdjustment,
paths: string
}

export type GetPaths = Array<Path>

export type PathFind = {
source: {
address: string,
amount?: Amount,
currencies?: Array<{currency: string, counterparty?:string}>
},
destination: {
address: string,
amount: LaxLaxAmount
}
}

export type PathFindRequest = {
command: string,
source_account: string,
destination_amount: RippledAmount,
destination_account: string,
source_amount?: RippledAmount,
source_currencies?: Array<string>
}

export type RippledPathsResponse = {
alternatives: Array<{
paths_computed: Array<Array<{
type: number,
type_hex: string,
account?: string,
issuer?: string,
currency?: string
}>>,
source_amount: RippledAmount
}>,
type: string,
destination_account: string,
destination_amount: RippledAmount,
destination_currencies?: Array<string>,
source_account?: string,
source_currencies?: Array<{currency: string}>,
full_reply?: boolean
}
/* @flow */
'use strict';

import type {Amount, LaxLaxAmount, RippledAmount, Adjustment, MaxAdjustment,
MinAdjustment} from '../common/types.js';


type Path = {
source: Adjustment | MaxAdjustment,
destination: Adjustment | MinAdjustment,
paths: string
}

export type GetPaths = Array<Path>

export type PathFind = {
source: {
address: string,
amount?: Amount,
currencies?: Array<{currency: string, counterparty?:string}>
},
destination: {
address: string,
amount: LaxLaxAmount
}
}

export type PathFindRequest = {
command: string,
source_account: string,
destination_amount: RippledAmount,
destination_account: string,
source_currencies?: Array<string>,
send_max?: RippledAmount
}

export type RippledPathsResponse = {
alternatives: Array<{
paths_computed: Array<Array<{
type: number,
type_hex: string,
account?: string,
issuer?: string,
currency?: string
}>>,
source_amount: RippledAmount
}>,
type: string,
destination_account: string,
destination_amount: RippledAmount,
destination_currencies?: Array<string>,
source_account?: string,
source_currencies?: Array<{currency: string}>,
full_reply?: boolean
}
6 changes: 3 additions & 3 deletions src/ledger/pathfind.js
Expand Up @@ -46,9 +46,9 @@ function requestPathFind(connection: Connection, pathfind: PathFind): Promise {
throw new ValidationError('Cannot specify both source.amount'
+ ' and destination.amount.value in getPaths');
}
request.source_amount = toRippledAmount(pathfind.source.amount);
if (request.source_amount.currency && !request.source_amount.issuer) {
request.source_amount.issuer = pathfind.source.address;
request.send_max = toRippledAmount(pathfind.source.amount);
if (request.send_max.currency && !request.send_max.issuer) {
request.send_max.issuer = pathfind.source.address;
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/integration/integration-test.js
Expand Up @@ -289,6 +289,21 @@ describe('integration tests', function() {
});
});

it('getPaths - send all', function() {
const pathfind = requests.getPaths.sendAll;
return this.api.getPaths(pathfind).then(data => {
assert(data && data.length > 0);
assert(_.every(data, path => {
return parseFloat(path.source.amount.value)
<= parseFloat(pathfind.source.amount.value);
}));
const path = data[0];
assert(path && path.source);
assert.strictEqual(path.source.address, pathfind.source.address);
assert(path.paths && path.paths.length > 0);
});
});


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

0 comments on commit 7514213

Please sign in to comment.