Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/tonkeeper/android into dev
Browse files Browse the repository at this point in the history
polstianka committed Oct 4, 2024
2 parents 012c2e0 + 7b45ad5 commit cf8f8c7
Showing 10 changed files with 77 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ data class RechargeMethodEntity(


fun fromTon(amount: BigDecimal): Coins {
return Coins.of(amount.divide(rate.toBigDecimal(), decimals, RoundingMode.HALF_UP))
return Coins.of(amount.divide(rate.toBigDecimal(), decimals, RoundingMode.HALF_UP), decimals)
}

fun fromTon(amount: String) = fromTon(amount.toBigDecimal())
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ internal class LocalDataSource(
}

fun getPreferGasless(testnet: Boolean): Boolean {
return prefs.getBoolean(gaslessKey(testnet), true)
return prefs.getBoolean(gaslessKey(testnet), false)
}

fun setPreferGasless(testnet: Boolean, value: Boolean) {
Original file line number Diff line number Diff line change
@@ -119,11 +119,12 @@ data class TransferEntity(

private fun getWalletTransfer(
privateKey: PrivateKeyEd25519?,
excessesAddress: AddrStd
excessesAddress: AddrStd,
jettonAmount: Coins?
): WalletTransfer {
val builder = WalletTransferBuilder()
builder.bounceable = bounceable
builder.body = body(privateKey, excessesAddress)
builder.body = body(privateKey, excessesAddress, jettonAmount)
builder.sendMode = sendMode
if (isNft) {
builder.coins = coins
@@ -142,10 +143,11 @@ data class TransferEntity(
private fun getGifts(
privateKey: PrivateKeyEd25519?,
excessesAddress: AddrStd,
additionalGifts: List<WalletTransfer>
additionalGifts: List<WalletTransfer>,
jettonAmount: Coins?
): Array<WalletTransfer> {
val gifts = mutableListOf<WalletTransfer>()
gifts.add(getWalletTransfer(privateKey, excessesAddress))
gifts.add(getWalletTransfer(privateKey, excessesAddress, jettonAmount))
gifts.addAll(additionalGifts)
return gifts.toTypedArray()
}
@@ -155,14 +157,16 @@ data class TransferEntity(
internalMessage: Boolean = false,
excessesAddress: AddrStd? = null,
additionalGifts: List<WalletTransfer> = emptyList(),
jettonAmount: Coins? = null,
): Cell {
return contract.createTransferUnsignedBody(
validUntil = validUntil,
seqNo = seqno,
gifts = getGifts(
privateKey = privateKey,
excessesAddress = excessesAddress ?: contract.address,
additionalGifts = additionalGifts
additionalGifts = additionalGifts,
jettonAmount = jettonAmount,
),
internalMessage = internalMessage,
)
@@ -237,21 +241,22 @@ data class TransferEntity(
return contract.createTransferMessageCell(contract.address, seqno, signedBody)
}

private fun body(privateKey: PrivateKeyEd25519?, excessesAddress: AddrStd): Cell? {
private fun body(privateKey: PrivateKeyEd25519?, excessesAddress: AddrStd, jettonAmount: Coins?): Cell? {
if (isNft) {
return nftBody(privateKey, excessesAddress)
} else if (!isTon) {
return jettonBody(privateKey, excessesAddress)
return jettonBody(privateKey, excessesAddress, jettonAmount)
}
return getCommentForwardPayload(privateKey)
}

private fun jettonBody(
privateKey: PrivateKeyEd25519?,
excessesAddress: AddrStd
excessesAddress: AddrStd,
jettonAmount: Coins?
): Cell {
return TonTransferHelper.jetton(
coins = coins,
coins = jettonAmount?.toGrams() ?: coins,
toAddress = destination,
responseAddress = excessesAddress,
queryId = queryId,
@@ -271,6 +276,7 @@ data class TransferEntity(

fun toSignedMessage(
privateKey: PrivateKeyEd25519 = fakePrivateKey,
jettonAmount: Coins? = null,
internalMessage: Boolean,
excessesAddress: AddrStd? = null,
additionalGifts: List<WalletTransfer> = emptyList()
@@ -284,6 +290,7 @@ data class TransferEntity(
internalMessage = internalMessage,
excessesAddress = excessesAddress,
additionalGifts = additionalGifts,
jettonAmount = jettonAmount,
),
)
}
Original file line number Diff line number Diff line change
@@ -25,6 +25,8 @@ class CoinEditText @JvmOverloads constructor(

private val suffixDrawable = SuffixDrawable(context)

private lateinit var formattingConfig: CoinFormattingConfig

var doOnValueChange: ((Double) -> Unit)? = null

var suffix: String?
@@ -38,15 +40,22 @@ class CoinEditText @JvmOverloads constructor(
setMaxLength(24)
setRightDrawable(suffixDrawable)
compoundDrawablePadding = 38.dp
val formattingConfig = CoinFormattingConfig(decimals = 9)
setFormattingTextWatcher(CoinFormattingTextWatcher(formattingConfig))
setFormattingInputFilter(CoinFormattingFilter(formattingConfig))
setDecimals(9)
doAfterTextChanged {
val value = getValue()
doOnValueChange?.invoke(value)
}
}

val decimals: Int
get() = formattingConfig.decimals

fun setDecimals(decimals: Int) {
formattingConfig = CoinFormattingConfig(decimals = decimals)
setFormattingTextWatcher(CoinFormattingTextWatcher(formattingConfig))
setFormattingInputFilter(CoinFormattingFilter(formattingConfig))
}

fun getValue(): Double {
val text = text.toString()
if (text.isEmpty()) {
Original file line number Diff line number Diff line change
@@ -154,8 +154,8 @@ class BatteryRechargeScreen(wallet: WalletEntity): BaseListWalletScreen<ScreenCo
finish()
}

private fun sign(request: SignRequestEntity) {
viewModel.sign(request).catch {
private fun sign(request: SignRequestEntity, forceRelayer: Boolean) {
viewModel.sign(request, forceRelayer).catch {
showError(it.bestMessage)
}.onEach {
postDelayed(1000) {
@@ -166,7 +166,7 @@ class BatteryRechargeScreen(wallet: WalletEntity): BaseListWalletScreen<ScreenCo

private fun onEvent(event: BatteryRechargeEvent) {
when (event) {
is BatteryRechargeEvent.Sign -> sign(event.request)
is BatteryRechargeEvent.Sign -> sign(event.request, event.forceRelayer)
is BatteryRechargeEvent.Error -> showError()
is BatteryRechargeEvent.MaxAmountError -> {
val message = requireContext().getString(
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ class BatteryRechargeViewModel(
private val promoStateFlow = MutableStateFlow<PromoState>(PromoState.Default)

private val _amountFlow = MutableStateFlow(0.0)
private val amountFlow = _amountFlow.map { Coins.of(it) }
private val amountFlow = combine(_amountFlow, tokenFlow) { amount, token -> Coins.of(amount, token.decimals) }

private val _addressFlow = MutableStateFlow("")

@@ -198,11 +198,13 @@ class BatteryRechargeViewModel(
uiItems.add(
Item.Amount(
symbol = token.symbol,
decimals = token.decimals,
formattedRemaining = CurrencyFormatter.format(
currency = token.symbol, value = remainingBalance
),
formattedMinAmount = CurrencyFormatter.format(
currency = token.symbol, value = minAmount
currency = token.symbol, value = minAmount,
customScale = token.decimals,
),
isInsufficientBalance = remainingBalance.isNegative,
isLessThanMin = isLessThanMin,
@@ -285,6 +287,7 @@ class BatteryRechargeViewModel(
destinationFlow
) { (wallet, token), destination ->
val rechargeMethod = getRechargeMethod(wallet, token)
val batteryBalance = getBatteryBalance(wallet)
val config = getBatteryConfig(wallet)
val batteryMaxInputAmount = rechargeMethod.fromTon(api.config.batteryMaxInputAmount)

@@ -321,6 +324,15 @@ class BatteryRechargeViewModel(
false -> TonNetwork.MAINNET
}

val forceRelayer = when {
token.isTon -> false
batteryBalance.balance.value > BigDecimal.ZERO -> true
rechargeMethod.minBootstrapValue != null -> {
amount.value > rechargeMethod.minBootstrapValue!!.toBigDecimal()
}
else -> false
}

if (token.isTon) {
val request = SignRequestEntity(
fromValue = wallet.contract.address.toAccountId(),
@@ -335,7 +347,7 @@ class BatteryRechargeViewModel(
),
network = network,
)
_eventFlow.tryEmit(BatteryRechargeEvent.Sign(request))
_eventFlow.tryEmit(BatteryRechargeEvent.Sign(request, forceRelayer))
} else {
val queryId = TransferEntity.newWalletQueryId()
val customPayload = if (token.isCompressed) {
@@ -365,7 +377,7 @@ class BatteryRechargeViewModel(
),
network = network,
)
_eventFlow.tryEmit(BatteryRechargeEvent.Sign(request))
_eventFlow.tryEmit(BatteryRechargeEvent.Sign(request, forceRelayer))
}
}.catch {
_eventFlow.tryEmit(BatteryRechargeEvent.Error)
@@ -505,8 +517,8 @@ class BatteryRechargeViewModel(
}
}

fun sign(request: SignRequestEntity) = flow {
val boc = SendTransactionScreen.run(context, wallet, request, forceRelayer = true)
fun sign(request: SignRequestEntity, forceRelayer: Boolean) = flow {
val boc = SendTransactionScreen.run(context, wallet, request, forceRelayer = forceRelayer)
emit(boc)
}.flowOn(Dispatchers.IO)
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import com.tonapps.icu.Coins
import com.tonapps.wallet.data.core.entity.SignRequestEntity

sealed class BatteryRechargeEvent {
data class Sign(val request: SignRequestEntity) : BatteryRechargeEvent()
data class Sign(val request: SignRequestEntity, val forceRelayer: Boolean) : BatteryRechargeEvent()
data object Error : BatteryRechargeEvent()
data class MaxAmountError(val maxAmount: Coins, val currency: String) : BatteryRechargeEvent()
}
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ sealed class Item(type: Int) : BaseListItem(type) {

data class Amount(
val symbol: String,
val decimals: Int,
val formattedRemaining: CharSequence,
val formattedMinAmount: CharSequence,
val isInsufficientBalance: Boolean,
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ class AmountHolder(
override fun onBind(item: Item.Amount) {
amountView.doOnValueChange = onValueChange
amountView.suffix = item.symbol
if (amountView.decimals != item.decimals) {
amountView.setDecimals(item.decimals)
}
currencyView.text = item.formattedCharges
applyAvailable(item.formattedRemaining, item.formattedMinAmount, item.isInsufficientBalance, item.isLessThanMin)
amountView.focus()
Original file line number Diff line number Diff line change
@@ -590,9 +590,14 @@ class SendViewModel(
): Pair<Coins, Boolean> {
val message = transfer.toSignedMessage(
internalMessage = true,
jettonAmount = if (transfer.max) {
Coins.of(1, transfer.token.decimals)
} else {
null
},
additionalGifts = listOf(
transfer.gaslessInternalGift(
jettonAmount = Coins.ONE,
jettonAmount = Coins.of(1, transfer.token.decimals),
batteryAddress = excessesAddress
)
),
@@ -606,12 +611,20 @@ class SendViewModel(
testnet = wallet.testnet,
) ?: return calculateFeeDefault(transfer, true)

val gaslessFee = Coins.ofNano(commission, transfer.token.decimals)

val tokenBalance = selectedTokenFlow.value.balance.value

if (gaslessFee > tokenBalance) {
return calculateFeeDefault(transfer, false)
}

sendTransferType = SendTransferType.Gasless(
excessesAddress = excessesAddress,
gaslessFee = Coins.ofNano(commission, transfer.token.decimals)
gaslessFee = gaslessFee
)

return Pair(Coins.ofNano(commission, transfer.token.decimals), true)
return Pair(gaslessFee, true)
}

private suspend fun calculateFeeDefault(
@@ -803,6 +816,11 @@ class SendViewModel(
internalMessage = internalMessage,
additionalGifts = additionalGifts,
excessesAddress = excessesAddress,
jettonAmount = if (transfer.max && sendTransferType is SendTransferType.Gasless) {
transfer.amount - (sendTransferType as SendTransferType.Gasless).gaslessFee
} else {
null
}
),
seqNo = transfer.seqno,
ledgerTransaction = transfer.getLedgerTransaction()

0 comments on commit cf8f8c7

Please sign in to comment.