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

Ton connect tx confirm screen #7811

Merged
merged 2 commits into from
Dec 18, 2024
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 @@ -6,6 +6,7 @@ import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.UnsupportedAccountException
import io.horizontalsystems.bankwallet.core.adapters.TonTransactionRecord
import io.horizontalsystems.bankwallet.core.providers.Translator
import io.horizontalsystems.bankwallet.core.stats.StatSection
import io.horizontalsystems.bankwallet.entities.Account
import io.horizontalsystems.bankwallet.entities.AccountType
import io.horizontalsystems.bankwallet.entities.CurrencyValue
Expand All @@ -14,6 +15,7 @@ import io.horizontalsystems.bankwallet.modules.transactionInfo.TransactionInfoVi
import io.horizontalsystems.bankwallet.modules.transactionInfo.TransactionInfoViewItem.Value
import io.horizontalsystems.bankwallet.modules.transactionInfo.TransactionViewItemFactoryHelper
import io.horizontalsystems.bankwallet.modules.transactions.TransactionStatus
import io.horizontalsystems.bankwallet.modules.transactions.TransactionViewItem
import io.horizontalsystems.core.BackgroundManager
import io.horizontalsystems.core.BackgroundManagerState
import io.horizontalsystems.hdwalletkit.Curve
Expand Down Expand Up @@ -155,7 +157,8 @@ object TonHelper {
action: TonTransactionRecord.Action,
rates: Map<String, CurrencyValue>,
blockchainType: BlockchainType,
hideAmount: Boolean
hideAmount: Boolean,
showHistoricalRate: Boolean
): List<TransactionInfoViewItem> {

val itemsForAction = mutableListOf<TransactionInfoViewItem>()
Expand All @@ -170,6 +173,7 @@ object TonHelper {
hideAmount = hideAmount,
sentToSelf = actionType.sentToSelf,
blockchainType = blockchainType,
showHistoricalRate = showHistoricalRate
)
)
actionType.comment?.let {
Expand All @@ -190,6 +194,7 @@ object TonHelper {
coinPrice = rates[actionType.value.coinUid],
hideAmount = hideAmount,
blockchainType = blockchainType,
showHistoricalRate = showHistoricalRate
)
)
actionType.comment?.let {
Expand Down Expand Up @@ -240,20 +245,44 @@ object TonHelper {
}

is TonTransactionRecord.Action.Type.ContractDeploy -> {
// case let .contractDeploy(interfaces):
// viewItems = [
// .actionTitle(iconName: nil, iconDimmed: false, title: "transactions.contract_deploy".localized, subTitle: interfaces.joined(separator: ", ")),
// ]
itemsForAction.add(
TransactionInfoViewItem.Transaction(
leftValue = Translator.getString(R.string.Transactions_ContractDeploy),
rightValue = actionType.interfaces.joinToString(),
icon = null,
)
)

}

is TonTransactionRecord.Action.Type.ContractCall -> {
// case let .contractCall(address, value, operation):
// viewItems = [
// .actionTitle(iconName: record.source.blockchainType.iconPlain32, iconDimmed: false, title: "transactions.contract_call".localized, subTitle: operation),
// .to(value: address, valueTitle: nil, contactAddress: nil)
// ]
//
// viewItems.append(contentsOf: sendSection(source: record.source, transactionValue: value, to: nil, rates: item.rates, balanceHidden: balanceHidden))
itemsForAction.add(
TransactionInfoViewItem.Transaction(
leftValue = Translator.getString(R.string.Transactions_ContractCall),
rightValue = actionType.operation,
icon = TransactionViewItem.Icon.Platform(blockchainType).iconRes,
)
)

itemsForAction.add(
TransactionInfoViewItem.Address(
Translator.getString(R.string.TransactionInfo_To),
actionType.address,
false,
blockchainType,
StatSection.AddressTo
)
)

itemsForAction.addAll(
TransactionViewItemFactoryHelper.getSendSectionItems(
value = actionType.value,
toAddress = null,
coinPrice = rates[actionType.value.coinUid],
hideAmount = hideAmount,
blockchainType = blockchainType,
)
)
}

is TonTransactionRecord.Action.Type.Unsupported -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package io.horizontalsystems.bankwallet.modules.tonconnect

import androidx.activity.ComponentActivity
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -22,23 +18,13 @@ import androidx.navigation.NavController
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.AppLogger
import io.horizontalsystems.bankwallet.core.adapters.TonTransactionRecord
import io.horizontalsystems.bankwallet.modules.amount.AmountInputType
import io.horizontalsystems.bankwallet.modules.confirm.ConfirmTransactionScreen
import io.horizontalsystems.bankwallet.modules.fee.HSFeeRaw
import io.horizontalsystems.bankwallet.modules.main.MainActivityViewModel
import io.horizontalsystems.bankwallet.modules.multiswap.TokenRowPure
import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme
import io.horizontalsystems.bankwallet.modules.transactionInfo.TransactionInfoSection
import io.horizontalsystems.bankwallet.ui.compose.components.ButtonPrimaryDefault
import io.horizontalsystems.bankwallet.ui.compose.components.ButtonPrimaryYellow
import io.horizontalsystems.bankwallet.ui.compose.components.CellUniversalLawrenceSection
import io.horizontalsystems.bankwallet.ui.compose.components.HFillSpacer
import io.horizontalsystems.bankwallet.ui.compose.components.TextImportantError
import io.horizontalsystems.bankwallet.ui.compose.components.VSpacer
import io.horizontalsystems.bankwallet.ui.compose.components.cell.CellUniversal
import io.horizontalsystems.bankwallet.ui.compose.components.cell.SectionUniversalLawrence
import io.horizontalsystems.bankwallet.ui.compose.components.subhead1_leah
import io.horizontalsystems.bankwallet.ui.compose.components.subhead2_grey
import io.horizontalsystems.core.SnackbarDuration
import io.horizontalsystems.core.helpers.HudHelper
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -113,98 +99,9 @@ fun TonConnectSendRequestScreen(navController: NavController) {
TextImportantError(text = error.message ?: error.javaClass.simpleName)
}

Crossfade(uiState.tonTransactionRecord) { transactionRecord ->
Column {
if (transactionRecord != null) {
SectionUniversalLawrence {
transactionRecord.actions.forEach { action ->
when (val actionType = action.type) {
is TonTransactionRecord.Action.Type.Swap -> {
val valueIn = actionType.valueIn
val valueInDecimalValue = valueIn.decimalValue
val valueInDecimals = valueIn.decimals
val amountFormatted = if (valueInDecimalValue != null && valueInDecimals != null) {
App.numberFormatter.formatCoinFull(
valueInDecimalValue.abs(),
valueIn.coinCode,
valueInDecimals
)
} else {
null
}
TokenRowPure(
fiatAmount = null,
borderTop = false,
currency = uiState.currency,
title = stringResource(R.string.Send_Confirmation_YouSend),
amountColor = ComposeAppTheme.colors.leah,
imageUrl = valueIn.coinIconUrl,
alternativeImageUrl = valueIn.alternativeCoinIconUrl,
imagePlaceholder = valueIn.coinIconPlaceholder,
badge = valueIn.badge,
amountFormatted = amountFormatted
)

val valueOut = actionType.valueOut
val valueOutDecimalValue = valueOut.decimalValue
val valueOutDecimals = valueOut.decimals
val valueOutAmountFormatted = if (valueOutDecimalValue != null && valueOutDecimals != null) {
App.numberFormatter.formatCoinFull(
valueOutDecimalValue,
valueOut.coinCode,
valueOutDecimals
)
} else {
null
}

TokenRowPure(
fiatAmount = null,
currency = uiState.currency,
title = stringResource(R.string.Swap_ToAmountTitle),
amountColor = ComposeAppTheme.colors.remus,
imageUrl = valueOut.coinIconUrl,
alternativeImageUrl = valueOut.alternativeCoinIconUrl,
imagePlaceholder = valueOut.coinIconPlaceholder,
badge = valueOut.badge,
amountFormatted = valueOutAmountFormatted,
)
}
// is TonTransactionRecord.Action.Type.Burn -> {}
// is TonTransactionRecord.Action.Type.ContractCall -> {}
// is TonTransactionRecord.Action.Type.ContractDeploy -> {}
// is TonTransactionRecord.Action.Type.Mint -> {}
// is TonTransactionRecord.Action.Type.Receive -> {}
// is TonTransactionRecord.Action.Type.Send -> {}
// is TonTransactionRecord.Action.Type.Unsupported -> {}
else -> {
CellUniversal(borderTop = false) {
subhead2_grey(text = stringResource(R.string.Send_Confirmation_Action))
HFillSpacer(minWidth = 16.dp)
subhead1_leah(text = actionType.javaClass.simpleName)
}
}
}
}
}

Spacer(modifier = Modifier.height(28.dp))

val fee = transactionRecord.fee
CellUniversalLawrenceSection(
listOf {
HSFeeRaw(
coinCode = fee.coinCode,
coinDecimal = fee.decimals,
fee = fee.value,
amountInputType = AmountInputType.COIN,
rate = null,
navController = navController
)
}
)
}
}
uiState.itemSections.forEach { items ->
TransactionInfoSection(items, navController, { null })
VSpacer(12.dp)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import io.horizontalsystems.bankwallet.core.IAccountManager
import io.horizontalsystems.bankwallet.core.ViewModelUiState
import io.horizontalsystems.bankwallet.core.adapters.TonTransactionRecord
import io.horizontalsystems.bankwallet.core.managers.TonConnectManager
import io.horizontalsystems.bankwallet.core.managers.TonHelper
import io.horizontalsystems.bankwallet.core.managers.TonKitWrapper
import io.horizontalsystems.bankwallet.core.managers.toTonWalletFullAccess
import io.horizontalsystems.bankwallet.core.meta
import io.horizontalsystems.bankwallet.entities.Currency
import io.horizontalsystems.bankwallet.modules.transactionInfo.TransactionInfoViewItem
import io.horizontalsystems.bankwallet.modules.transactions.TransactionSource
import io.horizontalsystems.marketkit.models.BlockchainType
import io.horizontalsystems.marketkit.models.TokenQuery
Expand All @@ -30,6 +32,8 @@ class TonConnectSendRequestViewModel(
private val transactionSigner = tonConnectManager.transactionSigner
private val tonConnectKit = App.tonConnectManager.kit
private var tonTransactionRecord: TonTransactionRecord? = null
private var itemSections: List<List<TransactionInfoViewItem>> = listOf()

private var currency = App.currencyManager.baseCurrency

private var tonWallet: TonWallet.FullAccess? = null
Expand All @@ -41,7 +45,8 @@ class TonConnectSendRequestViewModel(
currency = currency,
error = error,
confirmEnabled = sendRequestEntity != null && tonWallet != null,
rejectEnabled = sendRequestEntity != null
rejectEnabled = sendRequestEntity != null,
itemSections = itemSections
)

init {
Expand Down Expand Up @@ -81,7 +86,7 @@ class TonConnectSendRequestViewModel(
error = TonConnectSendRequestError.Other("Token Ton not found")
return
}

val transactionSource = TransactionSource(token.blockchain, account, token.type.meta)

val tonTransactionConverter = tonConnectManager.adapterFactory.tonTransactionConverter(
Expand All @@ -90,6 +95,13 @@ class TonConnectSendRequestViewModel(
)

tonTransactionRecord = tonTransactionConverter?.createTransactionRecord(tonEvent)

tonTransactionRecord?.let {
itemSections = it.actions.map { action ->
TonHelper.getViewItemsForAction(action, mapOf(), BlockchainType.Ton, false, showHistoricalRate = false)
}
}

}

fun confirm() {
Expand Down Expand Up @@ -125,4 +137,5 @@ data class TonConnectSendRequestUiState(
val error: TonConnectSendRequestError?,
val confirmEnabled: Boolean,
val rejectEnabled: Boolean,
val itemSections: List<List<TransactionInfoViewItem>>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ fun TransactionInfoSection(
}
}

else -> {
//do nothing
is TransactionInfoViewItem.Description -> {

}
is TransactionInfoViewItem.WarningMessage -> {

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sealed class TransactionInfoViewItem {
}

enum class AmountType {
YouSent, YouGot, Received, Sent, Approved;
YouSent, YouGot, Received, Sent, Approved, Minted;
}

data class ColoredValue(val value: String, val color: ColorName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ class TransactionInfoViewItemFactory(
is TonTransactionRecord -> {
transaction.actions.forEach { action ->
itemSections.add(
TonHelper.getViewItemsForAction(action, rates, blockchainType, transactionItem.hideAmount)
TonHelper.getViewItemsForAction(
action,
rates,
blockchainType,
transactionItem.hideAmount,
true
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ object TransactionViewItemFactoryHelper {
hideAmount: Boolean,
nftMetadata: Map<NftUid, NftAssetBriefMetadata> = mapOf(),
blockchainType: BlockchainType,
showHistoricalRate: Boolean = true
): List<TransactionInfoViewItem> {
val mint = fromAddress == zeroAddress
val title: String =
Expand All @@ -256,7 +257,8 @@ object TransactionViewItemFactoryHelper {
}

else -> {
amount = getAmount(coinPrice, value, true, hideAmount, AmountType.Received)
val amountType = if (mint) AmountType.Minted else AmountType.Received
amount = getAmount(coinPrice, value, true, hideAmount, amountType)
rate = getHistoricalRate(coinPrice, value)
}
}
Expand All @@ -283,7 +285,9 @@ object TransactionViewItemFactoryHelper {
}
}

rate?.let { items.add(it) }
if (showHistoricalRate) {
rate?.let { items.add(it) }
}

return items
}
Expand All @@ -297,6 +301,7 @@ object TransactionViewItemFactoryHelper {
sentToSelf: Boolean = false,
nftMetadata: Map<NftUid, NftAssetBriefMetadata> = mapOf(),
blockchainType: BlockchainType,
showHistoricalRate: Boolean = true
): List<TransactionInfoViewItem> {
val burn = toAddress == zeroAddress

Expand Down Expand Up @@ -349,7 +354,9 @@ object TransactionViewItemFactoryHelper {
}
}

rate?.let { items.add(it) }
if (showHistoricalRate) {
rate?.let { items.add(it) }
}

return items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fun TransactionAmountCell(
AmountType.Received -> stringResource(R.string.TransactionInfo_Received)
AmountType.Sent -> stringResource(R.string.TransactionInfo_Sent)
AmountType.Approved -> stringResource(R.string.TransactionInfo_Approved)
AmountType.Minted -> stringResource(R.string.TransactionInfo_Minted)
}
RowUniversal(
modifier = Modifier.padding(horizontal = 16.dp),
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@
<string name="TransactionInfo_YouGot">You Got</string>
<string name="TransactionInfo_YouSent">You Sent</string>
<string name="TransactionInfo_Received">Received</string>
<string name="TransactionInfo_Minted">Minted</string>
<string name="TransactionInfo_Sent">Sent</string>
<string name="TransactionInfo_Approved">Approved</string>
<string name="TransactionInfo_SpeedUp">Speed Up</string>
Expand Down
Loading