Skip to content

Commit

Permalink
AND-5687 Add hash check in success response
Browse files Browse the repository at this point in the history
  • Loading branch information
Mama1emon committed Dec 28, 2023
1 parent 637257b commit ea0e6e8
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,23 +80,17 @@ 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()))
.url("$requestBaseUrl/sendtx/")
.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<GetUtxoResponseItem> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
}

0 comments on commit ea0e6e8

Please sign in to comment.