Updated on 2026-08-14

This commit is contained in:
Tangem 2020-06-01 21:27:02 +03:00
parent 0d54da74a5
commit ace3760f4b
3 changed files with 19 additions and 15 deletions

View file

@ -1,8 +1,8 @@
package com.tangem.blockchain.blockchains.ethereum
import android.util.Log
import com.tangem.blockchain.blockchains.ethereum.network.EthereumNetworkManager
import com.tangem.blockchain.blockchains.ethereum.network.EthereumInfoResponse
import com.tangem.blockchain.blockchains.ethereum.network.EthereumNetworkManager
import com.tangem.blockchain.common.*
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
@ -24,7 +24,11 @@ class EthereumWalletManager(
override suspend fun update() {
val result = networkManager.getInfo(wallet.address, wallet.amounts[AmountType.Token]?.address)
val result = networkManager.getInfo(
wallet.address,
wallet.amounts[AmountType.Token]?.address,
wallet.amounts[AmountType.Token]?.decimals
)
when (result) {
is Result.Failure -> updateError(result.error)
is Result.Success -> updateWallet(result.data)

View file

@ -10,7 +10,6 @@ import com.tangem.blockchain.network.createRetrofitInstance
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.kethereum.ETH_IN_WEI
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
@ -35,6 +34,7 @@ class EthereumNetworkManager(blockchain: Blockchain) {
}
private val provider: EthereumProvider by lazy { EthereumProvider(api, apiKey) }
private val decimals = Blockchain.Ethereum.decimals()
suspend fun sendTransaction(transaction: String): SimpleResult {
return try {
@ -59,19 +59,20 @@ class EthereumNetworkManager(blockchain: Blockchain) {
}
}
suspend fun getInfo(address: String, contractAddress: String? = null): Result<EthereumInfoResponse> {
suspend fun getInfo(address: String, contractAddress: String? = null, tokenDecimals: Int? = null)
: Result<EthereumInfoResponse> {
return try {
coroutineScope {
val balanceResponse = retryIO { async { provider.getBalance(address) } }
val txCountResponse = retryIO { async { provider.getTxCount(address) } }
val pendingTxCountResponse = retryIO { async { provider.getPendingTxCount(address) } }
var tokenBalanceResponse: Deferred<EthereumResponse>? = null
if (contractAddress != null) {
if (contractAddress != null && tokenDecimals != null) {
tokenBalanceResponse = retryIO { async { provider.getTokenBalance(address, contractAddress) } }
}
Result.Success(EthereumInfoResponse(
balanceResponse.await().result!!.parseAmount(),
tokenBalanceResponse?.await()?.result?.parseAmount(),
balanceResponse.await().result!!.parseAmount(decimals),
tokenBalanceResponse?.await()?.result?.parseAmount(tokenDecimals!!),
txCountResponse.await().result?.responseToNumber()?.toLong() ?: 0,
pendingTxCountResponse.await().result?.responseToNumber()?.toLong() ?: 0
))
@ -87,20 +88,19 @@ class EthereumNetworkManager(blockchain: Blockchain) {
val normalFee = minFee.multiply(BigDecimal(1.2)).setScale(0, RoundingMode.HALF_UP)
val priorityFee = minFee.multiply(BigDecimal(1.5)).setScale(0, RoundingMode.HALF_UP)
return listOf(
minFee.convertFeeToEth(),
normalFee.convertFeeToEth(),
priorityFee.convertFeeToEth()
minFee.movePointLeft(decimals),
normalFee.movePointLeft(decimals),
priorityFee.movePointLeft(decimals)
)
}
private fun String.responseToNumber(): BigInteger = this.substring(2).toBigInteger(16)
private fun String.parseAmount(): BigDecimal =
this.responseToNumber().toBigDecimal().divide(ETH_IN_WEI.toBigDecimal())
private fun String.parseAmount(decimals: Int): BigDecimal =
this.responseToNumber().toBigDecimal().movePointLeft(decimals)
private fun BigDecimal.convertFeeToEth(): BigDecimal {
return this.divide(ETH_IN_WEI.toBigDecimal())
.setScale(12, BigDecimal.ROUND_DOWN).stripTrailingZeros()
return this.movePointLeft(decimals).setScale(decimals, BigDecimal.ROUND_DOWN).stripTrailingZeros()
}
}

View file

@ -1,4 +1,4 @@
ext.versions = [
kotlin : '1.3.72',
build_gradle: '3.6.3',
build_gradle: '4.0.0',
]