Skip to content

Commit d7b527c

Browse files
authored
Merge pull request #167 from onflow/lionel/fix-multiswapper-p1-p2
Fix MultiSwapper partial quoteIn tie-break
2 parents 991ca54 + 4f5ba4a commit d7b527c

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

cadence/contracts/connectors/SwapConnectors.cdc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ access(all) contract SwapConnectors {
183183
} else if !hasFull {
184184
// partial coverage group (only when no full route found)
185185
// in this case, prefer maximum outAmount
186-
if quote.outAmount > bestOutAmount {
186+
if quote.outAmount > bestOutAmount
187+
|| (quote.outAmount == bestOutAmount && quote.inAmount < bestInAmount) {
187188
bestIdx = i
188189
bestInAmount = quote.inAmount
189190
bestOutAmount = quote.outAmount

cadence/tests/SwapConnectorsMultiSwapper_test.cdc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ fun testQuoteOutCapLimitsRoute() {
190190
Test.assertEqual(5.0, quote.outAmount) // 10.0 * 0.5
191191
}
192192

193+
/// quoteIn — when two partial routes tie on outAmount, the one with the lower
194+
/// inAmount should win.
195+
///
196+
access(all)
197+
fun testQuoteInPartialTieBreaksOnLowerInAmount() {
198+
let forDesired = 10.0
199+
let configs = [
200+
makeConfig(priceRatio: 0.5, maxOut: 5.0),
201+
makeConfig(priceRatio: 1.0, maxOut: 5.0)
202+
]
203+
204+
let result = executeScript(
205+
"./scripts/multi-swapper/mock_quote_in.cdc",
206+
[testTokenAccount.address, configs, inVaultType, outVaultType, forDesired, false]
207+
)
208+
Test.expect(result, Test.beSucceeded())
209+
let quote = result.returnValue! as! SwapConnectors.MultiSwapperQuote
210+
211+
Test.assertEqual(1, quote.swapperIndex)
212+
Test.assertEqual(5.0, quote.inAmount)
213+
Test.assertEqual(5.0, quote.outAmount)
214+
}
215+
193216
/// quoteOut — four swappers: maximize outAmount first, then minimize inAmount as tiebreaker.
194217
///
195218
/// S0 (suboptimal): priceRatio=1.0, maxOut=100.0 → outAmount=100.0, inAmount=100.0

0 commit comments

Comments
 (0)