Skip to content

Commit

Permalink
Check SOL balance when sending SPL
Browse files Browse the repository at this point in the history
- Show message from RPC Error when sending SOL/SPL
- Increment custom snackbar's max lines to 4
  • Loading branch information
omurovch committed May 22, 2024
1 parent e6bd075 commit b188204
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ class SolanaAdapter(kitWrapper: SolanaKitWrapper) : BaseSolanaAdapter(kitWrapper
is SolanaKit.SyncState.Syncing -> AdapterState.Syncing()
}

private fun scaleDown(amount: BigDecimal, decimals: Int = decimal): BigDecimal {
return amount.movePointLeft(decimals).stripTrailingZeros()
}
companion object {
const val decimal = 9

private fun scaleUp(amount: BigDecimal, decimals: Int = decimal): BigInteger {
return amount.movePointRight(decimals).toBigInteger()
}
private fun scaleDown(amount: BigDecimal, decimals: Int = decimal): BigDecimal {
return amount.movePointLeft(decimals).stripTrailingZeros()
}

private fun balanceInBigDecimal(balance: Long?, decimal: Int): BigDecimal {
balance?.toBigDecimal()?.let {
return scaleDown(it, decimal)
} ?: return BigDecimal.ZERO
}
private fun scaleUp(amount: BigDecimal, decimals: Int = decimal): BigInteger {
return amount.movePointRight(decimals).toBigInteger()
}

companion object {
const val decimal = 9
fun balanceInBigDecimal(balance: Long?, decimal: Int): BigDecimal {
balance?.toBigDecimal()?.let {
return scaleDown(it, decimal)
} ?: return BigDecimal.ZERO
}

fun clear(walletId: String) {
SolanaKit.clear(App.instance, walletId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fun SendConfirmationScreen(
}

is SendResult.Failed -> {
HudHelper.showErrorMessage(view, sendResult.caution.getString())
HudHelper.showErrorMessage(view, sendResult.caution.getDescription() ?: sendResult.caution.getString())
}

null -> Unit
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.ISendSolanaAdapter
import io.horizontalsystems.bankwallet.core.adapters.SolanaAdapter
import io.horizontalsystems.bankwallet.core.isNative
import io.horizontalsystems.bankwallet.entities.Wallet
import io.horizontalsystems.bankwallet.modules.amount.AmountValidator
Expand All @@ -12,6 +13,7 @@ import io.horizontalsystems.bankwallet.modules.xrate.XRateService
import io.horizontalsystems.marketkit.models.BlockchainType
import io.horizontalsystems.marketkit.models.TokenQuery
import io.horizontalsystems.marketkit.models.TokenType
import io.horizontalsystems.solanakit.SolanaKit
import java.math.RoundingMode

object SendSolanaModule {
Expand All @@ -35,14 +37,17 @@ object SendSolanaModule {
adapter.availableBalance.setScale(coinMaxAllowedDecimals, RoundingMode.DOWN),
wallet.token.type.isNative,
)
val solToken = App.coinManager.getToken(TokenQuery(BlockchainType.Solana, TokenType.Native)) ?: throw IllegalArgumentException()
val balance = App.solanaKitManager.solanaKitWrapper?.solanaKit?.balance ?: 0L
val solBalance = SolanaAdapter.balanceInBigDecimal(balance, solToken.decimals) - SolanaKit.accountRentAmount
val addressService = SendSolanaAddressService(predefinedAddress)
val xRateService = XRateService(App.marketKit, App.currencyManager.baseCurrency)
val feeToken = App.coinManager.getToken(TokenQuery(BlockchainType.Solana, TokenType.Native)) ?: throw IllegalArgumentException()

SendSolanaViewModel(
wallet,
wallet.token,
feeToken,
solToken,
solBalance,
adapter,
xRateService,
amountService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import cash.z.ecc.android.sdk.ext.collectWith
import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.EvmError
import io.horizontalsystems.bankwallet.core.HSCaution
import io.horizontalsystems.bankwallet.core.ISendSolanaAdapter
import io.horizontalsystems.bankwallet.core.LocalizedException
Expand All @@ -17,11 +18,13 @@ import io.horizontalsystems.bankwallet.entities.Wallet
import io.horizontalsystems.bankwallet.modules.amount.SendAmountService
import io.horizontalsystems.bankwallet.modules.contacts.ContactsRepository
import io.horizontalsystems.bankwallet.modules.send.SendConfirmationData
import io.horizontalsystems.bankwallet.modules.send.SendErrorInsufficientBalance
import io.horizontalsystems.bankwallet.modules.send.SendResult
import io.horizontalsystems.bankwallet.modules.send.SendUiState
import io.horizontalsystems.bankwallet.modules.xrate.XRateService
import io.horizontalsystems.bankwallet.ui.compose.TranslatableString
import io.horizontalsystems.marketkit.models.Token
import io.horizontalsystems.marketkit.models.TokenType
import io.horizontalsystems.solanakit.SolanaKit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -33,6 +36,7 @@ class SendSolanaViewModel(
val wallet: Wallet,
val sendToken: Token,
val feeToken: Token,
val solBalance: BigDecimal,
val adapter: ISendSolanaAdapter,
private val xRateService: XRateService,
private val amountService: SendAmountService,
Expand Down Expand Up @@ -117,14 +121,19 @@ class SendSolanaViewModel(
}

private suspend fun send() = withContext(Dispatchers.IO) {
if (!hasConnection()){
if (!hasConnection()) {
sendResult = SendResult.Failed(createCaution(UnknownHostException()))
return@withContext
}

try {
sendResult = SendResult.Sending

val totalSolAmount = (if (sendToken.type == TokenType.Native) decimalAmount else BigDecimal.ZERO) + SolanaKit.fee

if (totalSolAmount > solBalance)
throw EvmError.InsufficientBalanceWithFee

adapter.send(decimalAmount, addressState.evmAddress!!)

sendResult = SendResult.Sent
Expand All @@ -136,7 +145,8 @@ class SendSolanaViewModel(
private fun createCaution(error: Throwable) = when (error) {
is UnknownHostException -> HSCaution(TranslatableString.ResString(R.string.Hud_Text_NoInternet))
is LocalizedException -> HSCaution(TranslatableString.ResString(error.errorTextRes))
else -> HSCaution(TranslatableString.PlainString(error.message ?: ""))
is EvmError.InsufficientBalanceWithFee -> SendErrorInsufficientBalance(feeToken.coin.code)
else -> HSCaution(TranslatableString.PlainString(error.cause?.message ?: error.message ?: ""))
}

private fun handleUpdatedAmountState(amountState: SendAmountService.State) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/res/layout/view_custom_snackbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
android:layout_weight="1"
android:fontFamily="sans-serif-medium"
android:textSize="14sp"
android:maxLines="2"
android:maxLines="4"
tools:text="Snackbar Text" />

<ImageView
Expand Down

0 comments on commit b188204

Please sign in to comment.