Skip to content

Commit

Permalink
Merge pull request #393 from tangem/merge_5.3_dev
Browse files Browse the repository at this point in the history
Merge 5.3 dev
  • Loading branch information
kozarezvlad authored Dec 4, 2023
2 parents 5ac35ab + 86cd036 commit fc06d97
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal fun Blockchain.getBitcoinNetworkProviders(
} else {
listOf(
"https://api.ravencoin.org/api/",
"https://ravencoin.network/api/",
"https://explorer.rvn.zelcore.io/api/",
)
}.map(::RavencoinNetworkProvider)
else -> throw IllegalStateException("$this isn't supported")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private fun getNowNodesProvider(
}

private fun getGetBlockProvider(accessToken: String): EthereumJsonRpcProvider =
EthereumJsonRpcProvider(baseUrl = "go.getblock.io/$accessToken")
EthereumJsonRpcProvider(baseUrl = "https://go.getblock.io/$accessToken/")

private fun getInfuraProvider(
baseUrl: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,3 @@ class StellarNetworkService(
)
}
}

fun <T : Any> T.getPrivateProperty(variableName: String): Any? {
return javaClass.getDeclaredField(variableName).let { field ->
field.isAccessible = true
return@let field.get(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tangem.blockchain.blockchains.stellar

import com.tangem.blockchain.common.NetworkProvider
import com.tangem.blockchain.common.toBlockchainSdkError
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.stellar.sdk.Server
import org.stellar.sdk.Transaction
Expand All @@ -13,6 +14,8 @@ import org.stellar.sdk.responses.RootResponse
import org.stellar.sdk.responses.SubmitTransactionResponse
import org.stellar.sdk.responses.operations.OperationResponse
import shadow.okhttp3.OkHttpClient
import com.tangem.blockchain.extensions.Result
import org.stellar.sdk.requests.ErrorResponse
import java.io.IOException

internal class StellarWrapperNetworkProvider(
Expand All @@ -25,34 +28,51 @@ internal class StellarWrapperNetworkProvider(
get() = server.httpClient

@Throws(IOException::class)
fun submitTransaction(transaction: Transaction?): SubmitTransactionResponse {
return server.submitTransaction(transaction)
fun submitTransaction(transaction: Transaction?): Result<SubmitTransactionResponse> {
return runWithErrorHandling { server.submitTransaction(transaction) }
}

fun accountCall(data: String): AccountResponse {
return server.accounts().account(data)
fun accountCall(data: String): Result<AccountResponse> {
return runWithErrorHandling { server.accounts().account(data) }
}

fun rootCall(): RootResponse {
return server.root()
fun rootCall(): Result<RootResponse> {
return runWithErrorHandling { server.root() }
}

fun ledgerCall(ledgerSeq: Long): LedgerResponse {
return server.ledgers().ledger(ledgerSeq)
fun ledgerCall(ledgerSeq: Long): Result<LedgerResponse> {
return runWithErrorHandling { server.ledgers().ledger(ledgerSeq) }
}

fun paymentsCall(accountId: String): Page<OperationResponse> {
return server.payments().forAccount(accountId).order(RequestBuilder.Order.DESC).execute()
fun paymentsCall(accountId: String): Result<Page<OperationResponse>> {
return runWithErrorHandling {
server.payments().forAccount(accountId).order(RequestBuilder.Order.DESC).execute()
}
}

fun feeCall(): FeeStatsResponse {
return server.feeStats().execute()
fun feeCall(): Result<FeeStatsResponse> {
return runWithErrorHandling { server.feeStats().execute() }
}

fun operationsLimit(accountId: String): Page<OperationResponse> {
return server.operations().forAccount(accountId)
.limit(RECORD_LIMIT)
.includeFailed(true)
.execute()
fun operationsLimit(accountId: String): Result<Page<OperationResponse>> {
return runWithErrorHandling {
server.operations().forAccount(accountId)
.limit(RECORD_LIMIT)
.includeFailed(true)
.execute()
}
}
}

private fun <T> runWithErrorHandling(block: () -> T): Result<T> {
return try {
val result = block()
Result.Success(result)
} catch (exception: Exception) {
if (exception is ErrorResponse && exception.code == 404) {
throw exception // handled in NetworkService
} else {
Result.Failure(exception.toBlockchainSdkError())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.tangem.blockchain.common.txhistory

import com.tangem.blockchain.blockchains.bitcoin.BitcoinTransactionHistoryProvider
import com.tangem.blockchain.blockchains.ethereum.EthereumTransactionHistoryProvider
import com.tangem.blockchain.blockchains.tron.TronTransactionHistoryProvider
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.BlockchainSdkConfig
import com.tangem.blockchain.network.blockbook.config.BlockBookConfig
Expand Down Expand Up @@ -30,12 +29,12 @@ internal fun Blockchain.getTransactionHistoryProvider(

Blockchain.Ethereum,
Blockchain.EthereumTestnet,
Blockchain.Arbitrum,
// Blockchain.Arbitrum,
Blockchain.Avalanche,
Blockchain.BSC,
Blockchain.Polygon,
// Blockchain.Polygon,
Blockchain.EthereumPow,
Blockchain.Kava,
// Blockchain.Kava,
-> {
EthereumTransactionHistoryProvider(
blockchain = this,
Expand All @@ -46,15 +45,15 @@ internal fun Blockchain.getTransactionHistoryProvider(
)
}

Blockchain.Tron -> {
TronTransactionHistoryProvider(
blockchain = this,
BlockBookApi(
config = BlockBookConfig.NowNodes(nowNodesCredentials = config.nowNodeCredentials),
blockchain = this,
)
)
}
// Blockchain.Tron -> {
// TronTransactionHistoryProvider(
// blockchain = this,
// BlockBookApi(
// config = BlockBookConfig.NowNodes(nowNodesCredentials = config.nowNodeCredentials),
// blockchain = this,
// )
// )
// }

else -> DefaultTransactionHistoryProvider
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tangem.blockchain.blockchains.ethereum.network.EthereumResponse
import com.tangem.blockchain.common.BlockchainSdkError
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
import org.stellar.sdk.requests.ErrorResponse
import retrofit2.HttpException
import java.io.IOException

Expand Down Expand Up @@ -63,8 +64,10 @@ object ResultChecker {
}

private fun BlockchainSdkError.WrappedThrowable.isNetworkError(): Boolean {
return cause is IOException || cause is HttpException || cause is JsonDataException
return cause is IOException || cause is HttpException || cause is JsonDataException || stellarNetworkError(cause)
}
}


private fun stellarNetworkError(cause: Throwable?): Boolean {
return cause is ErrorResponse && cause.code != 404
}
}

0 comments on commit fc06d97

Please sign in to comment.