feat(amm-ui): default holding selectors by balance

This commit is contained in:
Ricardo Guilherme Schmidt
2026-08-10 11:23:44 -03:00
parent a633adce20
commit d3fcbb61b3
6 changed files with 146 additions and 61 deletions
@@ -30,6 +30,8 @@ Item {
property color borderColor: "#52525b"
property color focusColor: "#f26a21"
property int modelRevision: 0
property bool selectionWasAutomatic: false
property string reconciledCriteriaKey: ""
readonly property bool criteriaReady: root.accountType.length > 0
&& (root.stateField.length === 0
@@ -247,6 +249,16 @@ Item {
if (root.accountIdFor(row).length > 0)
result.push(row)
}
result.sort(function(left, right) {
const balanceOrder = root.compareUnsignedDecimals(
root.valueFor(left, "balanceRaw"),
root.valueFor(right, "balanceRaw"))
if (balanceOrder !== 0)
return -balanceOrder
const leftId = root.accountIdFor(left)
const rightId = root.accountIdFor(right)
return leftId < rightId ? -1 : leftId > rightId ? 1 : 0
})
return result
}
@@ -266,6 +278,22 @@ Item {
return value === undefined || value === null ? "" : String(value)
}
function normalizedUnsignedDecimal(value) {
const text = root.scalarText(value).trim()
if (!/^[0-9]+$/.test(text))
return "0"
const normalized = text.replace(/^0+/, "")
return normalized.length > 0 ? normalized : "0"
}
function compareUnsignedDecimals(left, right) {
const leftText = root.normalizedUnsignedDecimal(left)
const rightText = root.normalizedUnsignedDecimal(right)
if (leftText.length !== rightText.length)
return leftText.length < rightText.length ? -1 : 1
return leftText < rightText ? -1 : leftText > rightText ? 1 : 0
}
function accountIdFor(row) {
return String(root.valueFor(row, "accountId")
|| root.valueFor(row, "displayAddress")
@@ -325,37 +353,54 @@ Item {
return text.length > 14 ? text.slice(0, 7) + "..." + text.slice(-5) : text
}
function setSelection(accountId, createNew) {
function setSelection(accountId, createNew, automatic) {
const nextId = String(accountId || "")
const nextCreate = createNew === true
if (root.selectedAccountId === nextId && root.createNewSelected === nextCreate)
const nextAutomatic = automatic === true
if (root.selectedAccountId === nextId && root.createNewSelected === nextCreate) {
root.selectionWasAutomatic = nextAutomatic
return
}
root.selectionWasAutomatic = nextAutomatic
root.selectedAccountId = nextId
root.createNewSelected = nextCreate
root.selectionChanged(nextId, nextCreate)
}
function criteriaKey() {
return root.accountType + "\u0000"
+ root.stateField + "\u0000"
+ root.scalarText(root.stateValue) + "\u0000"
+ String(root.selectionMode)
}
function reconcileSelection() {
const nextCriteriaKey = root.criteriaKey()
const criteriaChanged = root.reconciledCriteriaKey !== nextCriteriaKey
root.reconciledCriteriaKey = nextCriteriaKey
if (!root.criteriaReady) {
root.setSelection("", false)
root.setSelection("", false, true)
return
}
if (root.selectionValid)
return
if (!criteriaChanged && !root.selectionWasAutomatic) {
if (root.selectionValid)
return
if (root.selectionMode === ProgramAccountSelector.Output
&& root.createNewSelected) {
return
}
}
if (root.selectionMode === ProgramAccountSelector.Input) {
root.setSelection(root.matchingAccounts.length === 1
root.setSelection(root.matchingAccounts.length > 0
? root.accountIdFor(root.matchingAccounts[0]) : "",
false)
false,
true)
return
}
if (root.createNewSelected)
return
if (root.matchingAccounts.length === 0) {
root.setSelection("", true)
} else if (root.matchingAccounts.length === 1) {
root.setSelection(root.accountIdFor(root.matchingAccounts[0]), false)
root.setSelection("", true, true)
} else {
root.setSelection("", false)
root.setSelection(root.accountIdFor(root.matchingAccounts[0]), false, true)
}
}
}
@@ -75,7 +75,7 @@ Item {
compare(selector.selectedBalanceRaw, "120")
}
function test_inputMultipleHoldingsRequiresSelection() {
function test_inputMultipleHoldingsSelectsHighestBalance() {
const selector = createTemporaryObject(selectorComponent, root, {
"sourceModel": [root.holdingA, root.holdingB],
"selectionMode": Wallet.ProgramAccountSelector.Input
@@ -83,12 +83,16 @@ Item {
verify(!!selector, "Component exists")
tryCompare(selector, "showCombo", true)
compare(selector.matchingAccounts.length, 2)
compare(selector.ready, false)
tryCompare(selector, "selectedAccountId", "holding-a")
compare(selector.selectedBalanceRaw, "120")
compare(selector.ready, true)
selector.setSelection("holding-b", false)
compare(selector.selectedAccountId, "holding-b")
compare(selector.selectedBalanceRaw, "80")
compare(selector.ready, true)
selector.reconcileSelection()
compare(selector.selectedAccountId, "holding-b")
}
function test_outputNoHoldingSelectsCreateNew() {
@@ -120,7 +124,7 @@ Item {
compare(selector.ready, true)
}
function test_outputMultipleHoldingsRequiresDestination() {
function test_outputMultipleHoldingsSelectsHighestAndOffersCreateNew() {
const selector = createTemporaryObject(selectorComponent, root, {
"sourceModel": [root.holdingA, root.holdingB],
"selectionMode": Wallet.ProgramAccountSelector.Output
@@ -128,9 +132,50 @@ Item {
verify(!!selector, "Component exists")
tryCompare(selector, "showCombo", true)
compare(selector.choices.length, 3)
tryCompare(selector, "selectedAccountId", "holding-a")
compare(selector.createNewSelected, false)
compare(selector.ready, true)
selector.setSelection("", true)
compare(selector.selectedAccountId, "")
compare(selector.createNewSelected, true)
compare(selector.ready, true)
}
function test_highestBalanceComparisonPreservesU128Precision() {
const selector = createTemporaryObject(selectorComponent, root, {
"sourceModel": [
{
"accountId": "holding-lower",
"accountType": "TokenHolding",
"definitionId": "token-a",
"balanceRaw": "900719925474099299999"
},
{
"accountId": "holding-higher",
"accountType": "TokenHolding",
"definitionId": "token-a",
"balanceRaw": "900719925474099300000"
}
],
"selectionMode": Wallet.ProgramAccountSelector.Input
})
verify(!!selector, "Component exists")
tryCompare(selector, "selectedAccountId", "holding-higher")
compare(selector.matchingAccounts[0].accountId, "holding-higher")
}
function test_automaticCreateNewChangesToHoldingAfterWalletLoads() {
const selector = createTemporaryObject(selectorComponent, root, {
"sourceModel": [],
"selectionMode": Wallet.ProgramAccountSelector.Output
})
verify(!!selector, "Component exists")
tryCompare(selector, "createNewSelected", true)
selector.sourceModel = [root.holdingB, root.holdingA]
tryCompare(selector, "selectedAccountId", "holding-a")
compare(selector.createNewSelected, false)
compare(selector.ready, false)
}
function test_matchesNumericZeroState() {