Skip to content

Commit

Permalink
AND-8491 Fix review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeeei committed Oct 23, 2024
1 parent 64e3b1d commit 1d4de1a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.tangem.blockchain.extensions.letNotBlank
internal class CasperProvidersBuilder(
override val providerTypes: List<ProviderType>,
private val config: BlockchainSdkConfig,
private val blockchain: Blockchain,
) : NetworkProvidersBuilder<CasperNetworkProvider>() {

override fun createProviders(blockchain: Blockchain): List<CasperNetworkProvider> {
Expand All @@ -30,7 +31,13 @@ internal class CasperProvidersBuilder(
return createWithPostfixIfContained(
baseUrl = baseUrl,
postfixUrl = POSTFIX_URL,
create = ::CasperRpcNetworkProvider,
create = { baseUrl, postfixUrl ->
CasperRpcNetworkProvider(
baseUrl = baseUrl,
postfixUrl = postfixUrl,
blockchain = blockchain,
)
},
)
}

Expand All @@ -42,6 +49,7 @@ internal class CasperProvidersBuilder(
headerInterceptors = listOf(
AddHeaderInterceptor(mapOf(NowNodeCredentials.headerApiKey to it)),
),
blockchain = blockchain,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class CasperWalletManager(
) : WalletManager(wallet), ReserveAmountProvider {

override val currentHost: String get() = networkProvider.baseUrl
val blockchain = wallet.blockchain
private val blockchain = wallet.blockchain

override suspend fun updateInternal() {
when (val result = networkProvider.getBalance(wallet.address)) {
Expand All @@ -27,10 +27,10 @@ internal class CasperWalletManager(
}

private fun updateWallet(balance: CasperBalance) {
if (balance.balance != wallet.amounts[AmountType.Coin]?.value) {
if (balance.value != wallet.amounts[AmountType.Coin]?.value) {
wallet.recentTransactions.clear()
}
wallet.setCoinValue(balance.balance.movePointLeft(wallet.blockchain.decimals()))
wallet.setCoinValue(balance.value)
}

private fun updateError(error: BlockchainError) {
Expand All @@ -55,7 +55,7 @@ internal class CasperWalletManager(
networkProvider.getBalance(destinationAddress) is Result.Success

companion object {
// according to Casper wallet for nowx
// according to Casper Wallet
private val FEE = 0.1.toBigDecimal()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.tangem.blockchain.blockchains.casper.models

import java.math.BigDecimal

data class CasperBalance(
val balance: BigDecimal,
internal data class CasperBalance(
val value: BigDecimal,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.tangem.blockchain.blockchains.casper.models.CasperBalance
import com.tangem.blockchain.common.NetworkProvider
import com.tangem.blockchain.extensions.Result

interface CasperNetworkProvider : NetworkProvider {
internal interface CasperNetworkProvider : NetworkProvider {
suspend fun getBalance(address: String): Result<CasperBalance>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package com.tangem.blockchain.blockchains.casper.network.converters

import com.tangem.blockchain.blockchains.casper.network.request.CasperQueryBalanceBody

/**
* Converter from address to [CasperQueryBalanceBody]
*
*/
internal object CasperBalanceBodyConverter {

fun convert(address: String): CasperQueryBalanceBody {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ internal class CasperRpcNetworkProvider(
override val baseUrl: String,
private val postfixUrl: String,
headerInterceptors: List<Interceptor> = emptyList(),
private val blockchain: Blockchain,
) : CasperNetworkProvider {

private val api = createRetrofitInstance(baseUrl, headerInterceptors).create(CasperApi::class.java)

override suspend fun getBalance(address: String): Result<CasperBalance> = post(
body = CasperRpcBodyFactory.createQueryBalanceBody(address),
onSuccess = { response: CasperRpcResponseResult.Balance ->
CasperBalance(balance = BigDecimal(response.balance))
CasperBalance(value = BigDecimal(response.balance).movePointLeft(blockchain.decimals()))
},
onFailure = {
// Account is not funded yet
if (it.code == ERROR_CODE_QUERY_FAILED) {
Result.Success(CasperBalance(balance = BigDecimal.ZERO))
Result.Success(CasperBalance(value = BigDecimal.ZERO))
} else {
Result.Failure(toDefaultError(it))
}
Expand All @@ -48,9 +49,11 @@ internal class CasperRpcNetworkProvider(
return try {
when (val response = api.post(body = body, postfixUrl = postfixUrl)) {
is CasperRpcResponse.Success -> {
Result.Success(
data = onSuccess(
moshi.adapter<Data>().fromJsonValue(response.result)!!,
runCatching {
moshi.adapter<Data>().fromJsonValue(response.result)
}.getOrNull()?.let { Result.Success(onSuccess(it)) } ?: Result.Failure(
BlockchainSdkError.UnsupportedOperation(
"Unknown Casper JSON-RPC response result",
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal object CasperWalletManagerAssembly : WalletManagerAssembly<CasperWallet
providers = CasperProvidersBuilder(
providerTypes = input.providerTypes,
config = input.config,
blockchain = blockchain,
).build(blockchain),
),
transactionBuilder = CasperTransactionBuilder(this),
Expand Down

0 comments on commit 1d4de1a

Please sign in to comment.