Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use best rate when ILP packet amount is zero #397

Merged
merged 2 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/lib/quoter.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ class Quoter {
}, liquidityQuote)
}

async findBestPathForSourceAmount (sourceLedger, destination, sourceAmount) {
const hop = this.localTables.findBestHopForSourceAmount(
sourceLedger, destination, sourceAmount)

return {
destinationLedger: hop.bestRoute.nextLedger,
destinationCreditAccount: hop.bestRoute.isLocal ? destination : hop.bestHop,
sourceAmount: sourceAmount.toString(),
destinationAmount: hop.bestRoute.curve.amountAt(sourceAmount).toString()
}
}

/**
* Note that this function, like most of the ilp-connector code, uses the terms
* source, destination, and final, to refer to what we would normally call
Expand Down Expand Up @@ -172,8 +184,7 @@ class Quoter {
destinationLedger: quote.route.nextLedger,
destinationCreditAccount: quote.hop,
sourceAmount: quote.sourceAmount,
destinationAmount: headCurve.amountAt(quote.sourceAmount).toString(),
finalAmount: quote.liquidityCurve.amountAt(quote.sourceAmount).toString()
destinationAmount: headCurve.amountAt(quote.sourceAmount).toString()
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/route-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ class RouteBuilder {
ilpPacket.account, ilpPacket.amount)

const sourceLedger = sourceTransfer.ledger
const nextHop = await this.quoter.findBestPathForFinalAmount(
sourceLedger, ilpPacket.account, ilpPacket.amount)

// If the ILP amount field is zero, it means we should forward the maximum
// we are willing to.
const nextHop = (ilpPacket.amount === '0')
? await this.quoter.findBestPathForSourceAmount(sourceLedger, ilpPacket.account, sourceTransfer.amount)
: await this.quoter.findBestPathForFinalAmount(sourceLedger, ilpPacket.account, ilpPacket.amount)
if (!nextHop) {
log.info('could not find quote for source transfer: ' + JSON.stringify(sourceTransfer))
throw new IncomingTransferError(ilpErrors.F02_Unreachable({
Expand Down
31 changes: 31 additions & 0 deletions test/paymentsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,37 @@ describe('Payments', function () {
})
})

it('uses best rate when ilp packet amount = 0', async function () {
const sendSpy = sinon.spy(this.mockPlugin2, 'sendTransfer')
await this.mockPlugin1.emitAsync('incoming_prepare', {
id: '5857d460-2a46-4545-8311-1539d99e78e8',
direction: 'incoming',
ledger: 'mock.test1.',
amount: '100',
executionCondition: 'ni:///sha-256;I3TZF5S3n0-07JWH0s8ArsxPmVP6s-0d0SqxR6C3Ifk?fpt=preimage-sha-256&cost=6',
expiresAt: (new Date(START_DATE + 1000)).toISOString(),
ilp: packet.serializeIlpPayment({
account: 'mock.test2.bob',
amount: '0'
}).toString('base64')
})

sinon.assert.calledOnce(sendSpy)
sinon.assert.calledWithMatch(sendSpy, {
direction: 'outgoing',
ledger: 'mock.test2.',
to: 'mock.test2.bob',
amount: '94',
executionCondition: 'ni:///sha-256;I3TZF5S3n0-07JWH0s8ArsxPmVP6s-0d0SqxR6C3Ifk?fpt=preimage-sha-256&cost=6',
expiresAt: (new Date(START_DATE)).toISOString(),
noteToSelf: {
source_transfer_id: '5857d460-2a46-4545-8311-1539d99e78e8',
source_transfer_ledger: 'mock.test1.',
source_transfer_amount: '100'
}
})
})

it('supports optimistic mode', async function () {
const sendSpy = sinon.spy(this.mockPlugin2, 'sendTransfer')
await this.mockPlugin1.emitAsync('incoming_transfer', {
Expand Down
17 changes: 17 additions & 0 deletions test/routeBuilderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ describe('RouteBuilder', function () {
assert.deepEqual(destinationTransfer.ilp, ilpPacket)
})

it('uses best rate when ilp packet amount = 0', async function () {
const ilpPacket = packet.serializeIlpPayment({
account: bobB,
amount: '0'
}).toString('base64')
const destinationTransfer = await this.builder.getDestinationTransfer({
id: 'fd7ecefd-8eb8-4e16-b7c8-b67d9d6995f5',
ledger: ledgerA,
direction: 'incoming',
account: aliceA,
amount: '100',
ilp: ilpPacket
})
assert.deepEqual(destinationTransfer.amount, '50')
assert.deepEqual(destinationTransfer.to, 'eur-ledger.bob')
})

it('throws "Insufficient Source Amount" when the amount is too low', async function () {
const ilpPacket = packet.serializeIlpPayment({
account: bobB,
Expand Down