Skip to content

Commit

Permalink
Merge pull request #389 from tangem/bugfix/AND-5376_fix_stellar_provi…
Browse files Browse the repository at this point in the history
…der_switching

AND-5376 Fixed stellar provider switching
  • Loading branch information
Yoggam1 authored Nov 29, 2023
2 parents b0c9918 + 7f02ab9 commit a2a1d6c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
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 @@ -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 a2a1d6c

Please sign in to comment.