Skip to content

Commit 16f950b

Browse files
committed
ensure quote in swaps
1 parent f824ade commit 16f950b

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

cadence/contracts/connectors/SwapConnectors.cdc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ access(all) contract SwapConnectors {
195195
inType: reverse ? self.outType() : self.inType(),
196196
outType: reverse ? self.inType() : self.outType(),
197197
inAmount: bestInAmount,
198-
outAmount: bestOutAmount > forDesired ? forDesired : bestOutAmount,
198+
outAmount: bestOutAmount,
199199
swapperIndex: bestIdx
200200
)
201201
}
@@ -204,6 +204,7 @@ access(all) contract SwapConnectors {
204204
/// Selection policy: prefer maximum outAmount across all routes.
205205
access(all) fun quoteOut(forProvided: UFix64, reverse: Bool): {DeFiActions.Quote} {
206206
var bestIdx = 0
207+
var bestInAmount = forProvided
207208
var bestOutAmount = 0.0
208209

209210
for i in InclusiveRange(0, self.swappers.length - 1) {
@@ -213,6 +214,7 @@ access(all) contract SwapConnectors {
213214
if quote.outAmount > bestOutAmount {
214215
bestIdx = i
215216
bestOutAmount = quote.outAmount
217+
bestInAmount = quote.inAmount
216218
} else if quote.outAmount == bestOutAmount && quote.inAmount < bestInAmount {
217219
bestIdx = i
218220
bestInAmount = quote.inAmount
@@ -223,7 +225,7 @@ access(all) contract SwapConnectors {
223225
return MultiSwapperQuote(
224226
inType: reverse ? self.outType() : self.inType(),
225227
outType: reverse ? self.inType() : self.outType(),
226-
inAmount: hasBest ? forProvided : 0.0,
228+
inAmount: bestInAmount,
227229
outAmount: bestOutAmount,
228230
swapperIndex: bestIdx
229231
)
@@ -234,14 +236,16 @@ access(all) contract SwapConnectors {
234236
/// requested and the optimal Swapper used to fulfill the swap.
235237
/// NOTE: providing a Quote does not guarantee the fulfilled swap will enforce the quote's defined outAmount
236238
access(all) fun swap(quote: {DeFiActions.Quote}?, inVault: @{FungibleToken.Vault}): @{FungibleToken.Vault} {
237-
return <-self._swap(quote: quote, from: <-inVault, reverse: false)
239+
let ensuredQuote = quote != nil ? quote : self.quoteOut(forProvided: inVault.balance, reverse: false)
240+
return <-self._swap(quote: ensuredQuote, from: <-inVault, reverse: false)
238241
}
239242
/// Performs a swap taking a Vault of type outVault, outputting a resulting inVault. Implementations may choose
240243
/// to swap along a pre-set path or an optimal path of a set of paths or even set of contained Swappers adapted
241244
/// to use multiple Flow swap protocols.
242245
/// NOTE: providing a Quote does not guarantee the fulfilled swap will enforce the quote's defined outAmount
243246
access(all) fun swapBack(quote: {DeFiActions.Quote}?, residual: @{FungibleToken.Vault}): @{FungibleToken.Vault} {
244-
return <-self._swap(quote: quote, from: <-residual, reverse: true)
247+
let ensureQuote = quote != nil ? quote : self.quoteOut(forProvided: residual.balance, reverse: true)
248+
return <-self._swap(quote: ensureQuote, from: <-residual, reverse: true)
245249
}
246250
/// Swaps the provided Vault in the defined direction. If the quote is not a MultiSwapperQuote, a new quote is
247251
/// requested and the current optimal Swapper used to fulfill the swap.

0 commit comments

Comments
 (0)