diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/BlockBookNetworkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/BlockBookNetworkProvider.kt index 459731fa4..133013e66 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/BlockBookNetworkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/BlockBookNetworkProvider.kt @@ -73,8 +73,12 @@ class BlockBookNetworkProvider( override suspend fun sendTransaction(transaction: String): SimpleResult { return try { - withContext(Dispatchers.IO) { api.sendTransaction(transaction) } - SimpleResult.Success + val response = withContext(Dispatchers.IO) { api.sendTransaction(transaction) } + if (response.result.isNotBlank()) { + SimpleResult.Success + } else { + SimpleResult.Failure(BlockchainSdkError.FailedToSendException) + } } catch (e: Exception) { SimpleResult.Failure(e.toBlockchainSdkError()) } diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/BlockBookApi.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/BlockBookApi.kt index 88ce1992e..e131a36ae 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/BlockBookApi.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/BlockBookApi.kt @@ -13,6 +13,7 @@ import com.tangem.blockchain.network.blockbook.network.requests.GetFeeRequest import com.tangem.blockchain.network.blockbook.network.responses.GetAddressResponse import com.tangem.blockchain.network.blockbook.network.responses.GetFeeResponse import com.tangem.blockchain.network.blockbook.network.responses.GetUtxoResponseItem +import com.tangem.blockchain.network.blockbook.network.responses.SendTransactionResponse import okhttp3.* import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody.Companion.toRequestBody @@ -79,9 +80,9 @@ internal class BlockBookApi(private val config: BlockBookConfig, private val blo .unpack() } - suspend fun sendTransaction(txHex: String) { + suspend fun sendTransaction(txHex: String): SendTransactionResponse { val requestBaseUrl = config.getRequestBaseUrl(BlockBookRequest.SendTransaction, blockchain) - val response = client + return client .newCall( request = Request.Builder() .post(txHex.toRequestBody(TEXT_PLAIN_MEDIA_TYPE.toMediaTypeOrNull())) @@ -89,13 +90,7 @@ internal class BlockBookApi(private val config: BlockBookConfig, private val blo .build(), ) .await() - - val responseBody = response.body?.string() - if (response.isSuccessful && responseBody != null) { - return - } else { - throw IOException("Response is null") - } + .unpack() } suspend fun getUtxo(address: String): List { diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/responses/SendTransactionResponse.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/responses/SendTransactionResponse.kt new file mode 100644 index 000000000..c85322901 --- /dev/null +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockbook/network/responses/SendTransactionResponse.kt @@ -0,0 +1,6 @@ +package com.tangem.blockchain.network.blockbook.network.responses + +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class SendTransactionResponse(val result: String) diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairApi.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairApi.kt index 98076370e..67a432ae4 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairApi.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairApi.kt @@ -1,6 +1,7 @@ package com.tangem.blockchain.network.blockchair import com.squareup.moshi.JsonClass +import com.tangem.blockchain.network.blockchair.response.SendTransactionResponse import retrofit2.http.* interface BlockchairApi { @@ -31,7 +32,7 @@ interface BlockchairApi { @Body sendBody: BlockchairBody, @Query("key") key: String?, @Header("authorizationToken") authorizationToken: String?, - ) + ): SendTransactionResponse @GET("erc-20/{contract_address}/dashboards/address/{address}") suspend fun getTokenHolderData( diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairNetworkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairNetworkProvider.kt index 2bb83565e..3e1079b7e 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairNetworkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/BlockchairNetworkProvider.kt @@ -6,6 +6,7 @@ import com.tangem.blockchain.blockchains.bitcoin.network.BitcoinFee import com.tangem.blockchain.blockchains.bitcoin.network.BitcoinNetworkProvider import com.tangem.blockchain.common.BasicTransactionData import com.tangem.blockchain.common.Blockchain +import com.tangem.blockchain.common.BlockchainSdkError import com.tangem.blockchain.common.toBlockchainSdkError import com.tangem.blockchain.extensions.Result import com.tangem.blockchain.extensions.SimpleResult @@ -144,10 +145,15 @@ open class BlockchairNetworkProvider( override suspend fun sendTransaction(transaction: String): SimpleResult { return try { - makeRequestUsingKeyOnlyWhenNeeded { + val response = makeRequestUsingKeyOnlyWhenNeeded { api.sendTransaction(BlockchairBody(transaction), apiKey, authorizationToken) } - SimpleResult.Success + + if (response.transactionData.hash.isNotBlank()) { + SimpleResult.Success + } else { + SimpleResult.Failure(BlockchainSdkError.FailedToSendException) + } } catch (exception: Exception) { SimpleResult.Failure(exception.toBlockchainSdkError()) } diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/response/SendTransactionResponse.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/response/SendTransactionResponse.kt new file mode 100644 index 000000000..3c952f595 --- /dev/null +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockchair/response/SendTransactionResponse.kt @@ -0,0 +1,15 @@ +package com.tangem.blockchain.network.blockchair.response + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class SendTransactionResponse( + @Json(name = "data") val transactionData: TransactionData, +) { + + @JsonClass(generateAdapter = true) + data class TransactionData( + @Json(name = "transaction_hash") val hash: String, + ) +} diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherApi.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherApi.kt index 09cefdc64..fcdd2c700 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherApi.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherApi.kt @@ -1,6 +1,7 @@ package com.tangem.blockchain.network.blockcypher import com.squareup.moshi.JsonClass +import com.tangem.blockchain.network.blockcypher.response.SendTransactionResponse import retrofit2.http.* interface BlockcypherApi { @@ -16,7 +17,7 @@ interface BlockcypherApi { @Headers("Content-Type: application/json") @POST("txs/push") - suspend fun sendTransaction(@Body body: BlockcypherSendBody, @Query("token") token: String): BlockcypherTx + suspend fun sendTransaction(@Body body: BlockcypherSendBody, @Query("token") token: String): SendTransactionResponse } @JsonClass(generateAdapter = true) diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherNetworkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherNetworkProvider.kt index 2e66f6e2f..a19783a0f 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherNetworkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherNetworkProvider.kt @@ -113,11 +113,17 @@ class BlockcypherNetworkProvider( BlockchainSdkError.CustomError("Send transaction request is unavailable without a token"), ) } + return try { - retryIO { + val response = retryIO { api.sendTransaction(BlockcypherSendBody(transaction), getToken()!!) } - SimpleResult.Success + + if (response.transactionData.hash.isNotBlank()) { + SimpleResult.Success + } else { + SimpleResult.Failure(BlockchainSdkError.FailedToSendException) + } } catch (exception: Exception) { SimpleResult.Failure(exception.toBlockchainSdkError()) } diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherResponse.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherResponse.kt index f02e440ae..a7bdb4e9d 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherResponse.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/BlockcypherResponse.kt @@ -45,12 +45,6 @@ data class BlockcypherTxref( var received: String? = null, ) -@JsonClass(generateAdapter = true) -data class BlockcypherTx( - @Json(name = "hex") - val hex: String? = null, -) - @JsonClass(generateAdapter = true) data class BlockcypherFee( @Json(name = "low_fee_per_kb") diff --git a/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/response/SendTransactionResponse.kt b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/response/SendTransactionResponse.kt new file mode 100644 index 000000000..3a3d11bc8 --- /dev/null +++ b/blockchain/src/main/java/com/tangem/blockchain/network/blockcypher/response/SendTransactionResponse.kt @@ -0,0 +1,15 @@ +package com.tangem.blockchain.network.blockcypher.response + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class SendTransactionResponse( + @Json(name = "tx") val transactionData: TransactionData, +) { + + @JsonClass(generateAdapter = true) + data class TransactionData( + @Json(name = "hash") val hash: String, + ) +}