Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class GaslessTxRequest(
@SerializedName("fee_asset_id")
val feeAssetId: String,
@SerializedName("fee_amount")
val feeAmount: String,
val feeAmount: String?,
@SerializedName("chain_id")
val chainId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ import one.mixin.android.ui.home.web3.trade.SwapViewModel
import one.mixin.android.ui.wallet.alert.components.cardBackground
import java.math.BigDecimal

private fun displayBalance(balance: String?, isWeb3: Boolean): String {
if (balance.isNullOrBlank()) return "0"
return if (isWeb3) {
balance.toBigDecimalOrNull()?.stripTrailingZeros()?.toPlainString() ?: balance
} else {
balance.numberFormat8()
}
}

@Composable
fun InputArea(
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -95,11 +104,7 @@ fun InputArea(
)
Spacer(modifier = Modifier.width(4.dp))
Text(
text = if (token.isWeb3) {
balance?.numberFormat8() ?: "0"
} else {
balance?.numberFormat8() ?: "0"
},
text = displayBalance(balance = balance, isWeb3 = token.isWeb3),
style = TextStyle(
fontSize = 12.sp,
color = MixinAppTheme.colors.textAssist,
Expand Down Expand Up @@ -143,4 +148,4 @@ fun InputArea(

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ enum class FocusedField { NONE, IN_AMOUNT, OUT_AMOUNT, PRICE }

private const val MAX_DISPLAY_ORDER_COUNT: Int = 10

private fun formatBalanceInput(balance: String?, isWeb3: Boolean): String {
val amount = balance?.toBigDecimalOrNull() ?: return ""
if (amount <= BigDecimal.ZERO) return ""
return if (isWeb3) {
amount.stripTrailingZeros().toPlainString()
} else {
amount.setScale(8, RoundingMode.DOWN).stripTrailingZeros().toPlainString()
}
}

private fun formatLimitOrderAmount(value: String, isWeb3: Boolean): String {
val amount = value.toBigDecimalOrNull() ?: return value
return if (isWeb3) {
amount.stripTrailingZeros().toPlainString()
} else {
amount.setScale(8, RoundingMode.DOWN).stripTrailingZeros().toPlainString()
}
}

enum class ExpiryOption(@get:StringRes val labelRes: Int) {
NEVER(R.string.expiry_never), MIN_10(R.string.expiry_10_min), HOUR_1(R.string.expiry_1_hour), DAY_1(R.string.expiry_1_day), DAY_3(R.string.expiry_3_days), WEEK_1(R.string.expiry_1_week), MONTH_1(R.string.expiry_1_month), YEAR_1(R.string.expiry_1_year);

Expand All @@ -119,6 +138,7 @@ fun LimitOrderContent(
inMixin: Boolean,
initialAmount: String?,
lastOrderTime: Long?,
reviewing: Boolean,
onSelectToken: (Boolean, SelectTokenType) -> Unit,
onLimitReview: (SwapToken, SwapToken, CreateLimitOrderResponse) -> Unit,
onDeposit: (SwapToken) -> Unit,
Expand Down Expand Up @@ -297,12 +317,7 @@ fun LimitOrderContent(
}
}
}, onDeposit = onDeposit, onMax = {
val balance = fromBalance?.toBigDecimalOrNull() ?: BigDecimal.ZERO
if (balance > BigDecimal.ZERO) {
inputText = balance.setScale(8, RoundingMode.DOWN).stripTrailingZeros().toPlainString()
} else {
inputText = ""
}
inputText = formatBalanceInput(fromBalance, fromToken?.isWeb3 == true)
if (inputText.isNotBlank()) {
val fromAmount = inputText.toBigDecimalOrNull()
val standardPrice = limitPriceText.toBigDecimalOrNull()
Expand Down Expand Up @@ -347,12 +362,7 @@ fun LimitOrderContent(
},
onDeposit = null,
onMax = {
val balance = toBalance?.toBigDecimalOrNull() ?: BigDecimal.ZERO
if (balance > BigDecimal.ZERO) {
outputText = balance.setScale(8, RoundingMode.DOWN).stripTrailingZeros().toPlainString()
} else {
outputText = ""
}
outputText = formatBalanceInput(toBalance, toToken?.isWeb3 == true)
if (outputText.isNotBlank()) {
val toAmount = outputText.toBigDecimalOrNull()
val standardPrice = limitPriceText.toBigDecimalOrNull()
Expand Down Expand Up @@ -405,14 +415,15 @@ fun LimitOrderContent(
val isPriceValid = limitPriceText.toBigDecimalOrNull()?.let { it > BigDecimal.ZERO } == true
val isOutputValid = outputText.toBigDecimalOrNull()?.let { it > BigDecimal.ZERO } == true
val isEnabled = isInputValid && isPriceValid && isOutputValid && checkBalance == true && toToken != null
val isBusy = isSubmitting || reviewing
Button(
modifier = Modifier
.fillMaxWidth()
.height(48.dp),
onClick = {
keyboardController?.hide()
focusManager.clearFocus()
if (isButtonEnabled && toToken != null) {
if (isButtonEnabled && !isBusy && toToken != null) {
isButtonEnabled = false
isSubmitting = true
keyboardController?.hide()
Expand All @@ -429,8 +440,8 @@ fun LimitOrderContent(
viewModel.getAddressesByChainId(Web3Signer.currentWalletId, toTokenValue.chain.chainId)?.destination
} else null

val scaledAmount = inputText.toBigDecimalOrNull()?.setScale(8, RoundingMode.DOWN)?.stripTrailingZeros()?.toPlainString() ?: inputText
val scaledExpected = outputText.toBigDecimalOrNull()?.setScale(8, RoundingMode.DOWN)?.stripTrailingZeros()?.toPlainString() ?: outputText
val scaledAmount = formatLimitOrderAmount(inputText, fromTokenValue.isWeb3)
val scaledExpected = formatLimitOrderAmount(outputText, toTokenValue.isWeb3)
val request = LimitOrderRequest(
walletId = walletId,
assetId = fromTokenValue.assetId,
Expand All @@ -456,7 +467,7 @@ fun LimitOrderContent(
}
}
},
enabled = isEnabled,
enabled = isEnabled && !isBusy,
colors = ButtonDefaults.outlinedButtonColors(
backgroundColor = if (isEnabled) MixinAppTheme.colors.accent else MixinAppTheme.colors.backgroundGrayLight,
),
Expand All @@ -474,7 +485,7 @@ fun LimitOrderContent(
.height(24.dp),
contentAlignment = Alignment.Center
) {
if (isSubmitting) {
if (isBusy) {
CircularProgressIndicator(
modifier = Modifier
.width(18.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,17 +792,18 @@ class TradeFragment : BaseFragment() {
return
}
}
if (!ensureWeb3FeeSufficient(
val feeCheckResult = ensureWeb3FeeSufficient(
from = from,
destination = order.depositDestination,
amount = order.order.payAmount,
allowGasless = false,
).isSufficient
) {
allowGasless = true,
includeSwapPreviewData = true,
)
if (!feeCheckResult.isSufficient) {
reviewing = false
return
}
LimitTransferBottomSheetDialogFragment.newInstance(order, from, to, senderWalletId).apply {
LimitTransferBottomSheetDialogFragment.newInstance(order, from, to, senderWalletId, feeCheckResult.swapPreviewData).apply {
setOnDone {
initialAmount = null
lastOrderTime = System.currentTimeMillis()
Expand Down Expand Up @@ -848,7 +849,7 @@ class TradeFragment : BaseFragment() {
assetId = token.assetId,
amount = amount,
feeAssetId = token.assetId,
feeAmount = BigDecimal.ZERO.toPlainString(),
feeAmount = null,
chainId = token.chainId,
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ fun TradePage(
inMixin,
initialAmount,
lastOrderTime,
reviewing,
{ isReverse, type -> onSelectToken(isReverse, type, true) },
onLimitReview,
onDeposit,
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/one/mixin/android/ui/wallet/InputFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class InputFragment : BaseFragment(R.layout.fragment_input), OnReceiveSelectionC
}
lifecycleScope.launch(
CoroutineExceptionHandler { _, error ->
ErrorHandler.Companion.handleError(error)
ErrorHandler.handleError(error)
alertDialog.dismiss()
},
) {
Expand Down Expand Up @@ -751,16 +751,18 @@ class InputFragment : BaseFragment(R.layout.fragment_input), OnReceiveSelectionC
}
TransferType.BIOMETRIC_ITEM -> {
assetBiometricItem?.let { item ->
when {
item is WithdrawBiometricItem -> {
when (item) {
is WithdrawBiometricItem -> {
// isFeeWaived todo check is my wallet
titleView.setLabel(getString(R.string.Send_To_Title), addressLabel, "")
}
item is AddressTransferBiometricItem -> {

is AddressTransferBiometricItem -> {
titleView.setLabel(getString(R.string.Send_To_Title), null, (if (toAddress == null) item.address else "$toAddress${addressTag?.let { ":$it" } ?: ""}").formatPublicKey(16))
renderTitle(toAddress ?: item.address, addressTag)
}
item is TransferBiometricItem -> {

is TransferBiometricItem -> {
titleView.setSubTitle(getString(R.string.Send_To_Title), item.users) {
showUserList(item.users)
}
Expand Down Expand Up @@ -1062,7 +1064,7 @@ class InputFragment : BaseFragment(R.layout.fragment_input), OnReceiveSelectionC
if (isAdded) {
currentNote = note
binding.contentTextView.text =
if (note.isNotEmpty()) note else getString(R.string.add_a_note)
note.ifEmpty { getString(R.string.add_a_note) }
}
}
}
Expand Down Expand Up @@ -1478,14 +1480,14 @@ class InputFragment : BaseFragment(R.layout.fragment_input), OnReceiveSelectionC
}

private suspend fun refreshFee() {
when {
transferType == TransferType.ADDRESS -> {
when (transferType) {
TransferType.ADDRESS -> {
refreshFee(token!!)
}
transferType == TransferType.WEB3 -> {
TransferType.WEB3 -> {
refreshWeb3Fees(web3Token!!)
}
transferType == TransferType.BIOMETRIC_ITEM && assetBiometricItem is WithdrawBiometricItem -> {
TransferType.BIOMETRIC_ITEM if assetBiometricItem is WithdrawBiometricItem -> {
refreshFee(token!!)
}
else -> {
Expand Down
Loading
Loading