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

Fix/bestorders overflow #20

Merged
merged 15 commits into from
Dec 22, 2024
50 changes: 37 additions & 13 deletions atomic_defi_design/Dex/Constants/General.qml
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,14 @@ QtObject {
return formatFiat("", value, API.app.settings_pg.current_currency)
}

function formatFiat(received, amount, fiat, precision=2) {
function formatFiat(received, amount, fiat, sf=2) {
if (privacy_mode) return ''
if (precision == 2 && fiat == "BTC") {
precision = 8
if (sf == 2 && fiat == "BTC") {
sf = 8
}
return diffPrefix(received) +
(fiat === API.app.settings_pg.current_fiat ? API.app.settings_pg.current_fiat_sign : API.app.settings_pg.current_currency_sign)
+ " " + (amount < 1E5 ? formatDouble(parseFloat(amount), precision, true) : nFormatter(parseFloat(amount), precision))
+ " " + (amount < 1E5 ? formatDouble(parseFloat(amount), sf, true) : nFormatter(parseFloat(amount), sf))
}

function formatPercent(value, show_prefix=true) {
Expand All @@ -621,7 +621,7 @@ QtObject {
return (show_prefix ? prefix : '') + parseFloat(value).toFixed(3) + ' %'
}

readonly property int amountPrecision: 8
readonly property int defaultPrecision: 8
readonly property int sliderDigitLimit: 9
readonly property int recommendedPrecision: -1337

Expand All @@ -631,17 +631,38 @@ QtObject {

function getRecommendedPrecision(v, limit) {
const lim = limit || sliderDigitLimit
return Math.min(Math.max(lim - getDigitCount(v), 0), amountPrecision)
return Math.min(Math.max(lim - getDigitCount(v), 0), defaultPrecision)
}

function formatDouble(v, precision, trail_zeros) {
/**
* Converts a float into a readable string with K, M, B, etc.
* @param {number} num - The number to format.
* @param {number} decimals - The number of decimal places to include (default is 2).
* @param {number} extra_decimals - The number of decimal places to include if no suffix (default is 8).
* @returns {string} - The formatted string.
*/
function formatNumber(num, decimals = 2, extra_decimals = 8) {
if (isNaN(num) || num === null) return "0";

const suffixes = ['', 'K', 'M', 'B', 'T']; // Add more as needed for larger numbers
const tier = Math.floor(Math.log10(Math.abs(num)) / 3); // Determine the tier (e.g., thousands, millions)

if (tier === 0) return num.toFixed(extra_decimals); // No suffix needed

const scaled = num / Math.pow(10, tier * 3);
const suffix = suffixes[tier] || `e${tier * 3}`; // Use scientific notation if beyond defined suffixes

return `${scaled.toFixed(decimals)}${suffix}`;
}

function formatDouble(v, sf = defaultPrecision, trail_zeros = true) {
if(v === '') return "0"
if(precision === recommendedPrecision) precision = getRecommendedPrecision(v)
if(sf === recommendedPrecision) sf = getRecommendedPrecision(v)

if(precision === 0) return parseInt(v).toString()
if(sf === 0) return parseInt(v).toString()

// Remove more than n decimals, then convert to string without trailing zeros
const full_double = parseFloat(v).toFixed(precision || amountPrecision)
const full_double = parseFloat(v).toFixed(sf || defaultPrecision)

return trail_zeros ? full_double : full_double.replace(/\.?0+$/,"")
}
Expand All @@ -654,9 +675,12 @@ QtObject {
return parseFloat(formatDouble(value, 2))
}

function formatCrypto(received, amount, ticker, fiat_amount, fiat, precision, trail_zeros) {
if (privacy_mode) return ''
return diffPrefix(received) + ticker + " " + formatDouble(amount, precision, trail_zeros) + (fiat_amount ? " (" + formatFiat("", fiat_amount, fiat) + ")" : "")
function formatCrypto(received, amount, ticker, fiat_amount, fiat, sf, trail_zeros) {
if (privacy_mode) {
return ""
}
const prefix = diffPrefix(received)
return prefix + ticker + " " + formatDouble(amount, sf, trail_zeros) + (fiat_amount ? " (" + formatFiat("", fiat_amount, fiat) + ")" : "")
}

function formatFullCrypto(received, amount, ticker, fiat_amount, fiat, use_full_ticker) {
Expand Down
6 changes: 3 additions & 3 deletions atomic_defi_design/Dex/Exchange/Trade/SimpleView/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Item

Column
{
width: root.currentSubPage === subPages.Trade ? _simpleTrade.best ? 600 : 450 : 450
width: root.currentSubPage === subPages.Trade ? _simpleTrade.best ? 720 : 450 : 450
y: 60
spacing: 30
anchors.horizontalCenter: parent.horizontalCenter
Expand Down Expand Up @@ -133,7 +133,7 @@ Item
id: _swipeSimplifiedView
currentIndex: root.currentSubPage
anchors.horizontalCenter: parent.horizontalCenter
width: 600
width: 720
height: 650
clip: true
interactive: false
Expand All @@ -144,7 +144,7 @@ Item
{
id: subTradePage
height: _simpleTrade.height
width: _simpleTrade.best ? 600 : _simpleTrade.coinSelection ? 450 : 380
width: _simpleTrade.best ? 720 : _simpleTrade.coinSelection ? 450 : 380
anchors.horizontalCenter: parent.horizontalCenter
radius: 20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ DexListView

property int _rowWidth: width
property int _rowHeight: 40
property int _tokenColumnSize: 90
property int _quantityColumnSize: 90
property int _tokenColumnSize: 120
property int _quantityColumnSize: 95
property int _quantityInBaseColumnSize: 120
property int _fiatVolumeColumnSize: 80
property int _cexRateColumnSize: 60
property int _fiatVolumeColumnSize: 95
property int _cexRateColumnSize: 75

headerPositioning: ListView.OverlayHeader
reuseItems: true
Expand Down Expand Up @@ -187,7 +187,7 @@ DexListView
{
Layout.preferredWidth: _quantityColumnSize
horizontalAlignment: Text.AlignRight
text_value: parseFloat(General.formatDouble(rel_max_volume, General.amountPrecision, true)).toFixed(8)
text_value: Constants.General.formatNumber(rel_max_volume)
font.pixelSize: 14
opacity: !_isCoinEnabled? .3 : 1
}
Expand All @@ -196,7 +196,7 @@ DexListView
{
Layout.preferredWidth: _quantityInBaseColumnSize
horizontalAlignment: Text.AlignRight
text_value: parseFloat(General.formatDouble(base_max_volume, General.amountPrecision, true)).toFixed(8)
text_value: Constants.General.formatNumber(base_max_volume)
font.pixelSize: 14
opacity: !_isCoinEnabled? .3 : 1
}
Expand All @@ -206,16 +206,16 @@ DexListView
Layout.preferredWidth: _fiatVolumeColumnSize
horizontalAlignment: Text.AlignRight
// TODO: Adjust fiat to left/right sign based on region
text_value: parseFloat(price_fiat).toFixed(2)+Constants.API.app.settings_pg.current_fiat_sign
text_value: Constants.General.formatFiat("", price_fiat, Constants.API.app.settings_pg.current_fiat_sign)
opacity: !_isCoinEnabled? .3 : 1
}

DexLabel
DexLabel // Order CEX Rate
{
Layout.preferredWidth: _cexRateColumnSize
horizontalAlignment: Text.AlignRight
color: cex_rates=== "0" ? Qt.darker(DexTheme.foregroundColor) : parseFloat(cex_rates)>0? DexTheme.warningColor : DexTheme.okColor
text_value: cex_rates=== "0" ? "N/A" : parseFloat(cex_rates)>0? "+"+parseFloat(cex_rates).toFixed(2)+"%" : parseFloat(cex_rates).toFixed(2)+"%"
text_value: Constants.General.getCexRate(cex_rates)
opacity: !_isCoinEnabled? .3 : 1
}

Expand Down
4 changes: 2 additions & 2 deletions atomic_defi_design/Dex/Exchange/Trade/SimpleView/Trade.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ClipRRect // Trade Card
Component.onCompleted: _fromValue.forceActiveFocus()
onBestChanged: if (best) Constants.API.app.trading_pg.orderbook.refresh_best_orders()

width: bestOrderSimplified.visible ? 600 : coinSelection ? 450 : 380
width: bestOrderSimplified.visible ? 720 : coinSelection ? 450 : 380
height: swap_card_content.height + 15
radius: 20

Expand Down Expand Up @@ -865,7 +865,7 @@ ClipRRect // Trade Card
tradeCard: _tradeCard
anchors.fill: parent
anchors.topMargin: 10
visible: _tradeCard.width == 600
visible: _tradeCard.width == 720

onSelectedOrderChanged:
{
Expand Down
Loading