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

Handle sending max available TON #7682

Merged
merged 1 commit into from
Sep 27, 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 @@ -73,17 +73,17 @@ class TonAdapter(tonKitWrapper: TonKitWrapper) : BaseTonAdapter(tonKitWrapper, 9
override val availableBalance: BigDecimal
get() = balance

private fun getSendAmount(amount: BigDecimal) = when {
amount.compareTo(availableBalance) == 0 -> SendAmount.Max
else -> SendAmount.Amount(amount.movePointRight(decimals).toBigInteger())
}

override suspend fun send(amount: BigDecimal, address: FriendlyAddress, memo: String?) {
tonKit.send(address, SendAmount.Amount(amount.movePointRight(decimals).toBigInteger()), memo)
tonKit.send(address, getSendAmount(amount), memo)
}

override suspend fun estimateFee(amount: BigDecimal, address: FriendlyAddress, memo: String?): BigDecimal {
val estimateFee = tonKit.estimateFee(
address,
SendAmount.Amount(amount.movePointRight(decimals).toBigInteger()),
memo
)

val estimateFee = tonKit.estimateFee(address, getSendAmount(amount), memo)
return estimateFee.toBigDecimal(decimals).stripTrailingZeros()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.ISendTonAdapter
import io.horizontalsystems.bankwallet.core.isNative
import io.horizontalsystems.bankwallet.entities.Wallet
import io.horizontalsystems.bankwallet.modules.amount.AmountValidator
import io.horizontalsystems.bankwallet.modules.xrate.XRateService
Expand All @@ -25,7 +26,12 @@ object SendTonModule {
val amountValidator = AmountValidator()
val coinMaxAllowedDecimals = wallet.token.decimals

val amountService = SendTonAmountService(amountValidator, wallet.coin.code, adapter.availableBalance)
val amountService = SendTonAmountService(
amountValidator = amountValidator,
coinCode = wallet.coin.code,
availableBalance = adapter.availableBalance,
leaveSomeBalanceForFee = wallet.token.type.isNative
)
val addressService = SendTonAddressService(predefinedAddress)
val feeService = SendTonFeeService(adapter)
val xRateService = XRateService(App.marketKit, App.currencyManager.baseCurrency)
Expand Down
Loading