Skip to content

Commit

Permalink
Handle sending max available TON
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Sep 27, 2024
1 parent eeb1ea8 commit b3fba35
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
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

0 comments on commit b3fba35

Please sign in to comment.