fix(amm): reserve balance for exact-output slippage

This commit is contained in:
Ricardo Guilherme Schmidt 2026-07-17 20:55:59 -03:00
parent 7b595dc5da
commit a8de6b498c
No known key found for this signature in database
GPG Key ID: 1396EA17DE132FFE
2 changed files with 22 additions and 1 deletions

View File

@ -79,11 +79,13 @@ Rectangle {
readonly property string limitText: exactOutput
? swapState.formatTokenAmount(maxSentAmount, sellToken ? sellToken.symbol : "")
: swapState.formatTokenAmount(minReceivedAmount, buyToken ? buyToken.symbol : "")
readonly property real sellBalanceRequirement: exactOutput
? maxSentAmount : parsedSellAmount
readonly property bool hasAmount: editingSide === "sell" ? parsedSellInput > 0 : parsedBuyInput > 0
readonly property bool tokensSelected: sellToken !== null && buyToken !== null
readonly property bool sameTokenSelected: isSameToken(sellToken, buyToken)
readonly property bool insufficientBalance: hasAmount && sellToken !== null && parsedSellAmount > (sellToken.balance || 0)
readonly property bool insufficientBalance: hasAmount && sellToken !== null && sellBalanceRequirement > (sellToken.balance || 0)
readonly property bool insufficientLiquidity: hasAmount && buyToken !== null && parsedBuyAmount > (buyToken.reserve || 0)
readonly property bool canSubmit: tokensSelected && !sameTokenSelected && hasAmount && parsedSellAmount > 0 && parsedBuyAmount > 0 && !insufficientBalance && !insufficientLiquidity

View File

@ -108,6 +108,25 @@ Item {
verify(Number(snapshot.minReceived) < Number(snapshot.buyAmount))
}
function test_exactOutputRequiresBalanceForMaximumInput() {
const page = createTemporaryObject(pageComponent, root)
verify(page)
const card = findChild(page, "swapCard")
verify(card)
card.setToken("sell", page.tokens[0])
card.setToken("buy", page.tokens[1])
card.slippageTolerancePercent = 10
card.buyInput = "11500"
card.editingSide = "buy"
verify(card.parsedSellAmount < card.sellToken.balance)
verify(card.maxSentAmount > card.sellToken.balance)
verify(card.insufficientBalance)
verify(!card.canSubmit)
compare(card.submitButtonText, "Insufficient balance")
}
function test_reselectingOppositeTokenSwapsPair() {
const page = createTemporaryObject(pageComponent, root)
verify(page)