Skip to content

Commit

Permalink
AND-8727 Fixed Сan't send transaction to firefly api
Browse files Browse the repository at this point in the history
  • Loading branch information
iMaks99 committed Oct 10, 2024
1 parent 339fd48 commit d7b10aa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ internal class ChiaProvidersBuilder(

private fun createTangemProvider(): ChiaNetworkProvider? {
return config.chiaTangemApiKey?.letNotBlank {
ChiaJsonRpcProvider(baseUrl = API_CHIA_TANGEM, key = it)
ChiaJsonRpcProvider(
baseUrl = API_CHIA_TANGEM,
key = it,
isRequiredHexPrefixForTx = false,
)
}
}

Expand All @@ -43,6 +47,7 @@ internal class ChiaProvidersBuilder(
ChiaJsonRpcProvider(
baseUrl = if (isTestnet) API_CHIA_FIREACADEMY_TESTNET else API_CHIA_FIREACADEMY,
key = config.chiaFireAcademyApiKey,
isRequiredHexPrefixForTx = true,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tangem.blockchain.blockchains.chia.network

import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.BlockchainSdkError
import com.tangem.blockchain.common.HEX_PREFIX
import com.tangem.blockchain.common.logging.AddHeaderInterceptor
import com.tangem.blockchain.common.toBlockchainSdkError
import com.tangem.blockchain.extensions.Result
Expand All @@ -11,7 +12,12 @@ import com.tangem.blockchain.network.createRetrofitInstance
import java.math.BigDecimal
import java.math.RoundingMode

open class ChiaJsonRpcProvider(override val baseUrl: String, key: String) : ChiaNetworkProvider {
open class ChiaJsonRpcProvider(
override val baseUrl: String,
private val isRequiredHexPrefixForTx: Boolean,
key: String,
) :
ChiaNetworkProvider {

private val api: ChiaApi by lazy {
createRetrofitInstance(
Expand Down Expand Up @@ -62,7 +68,13 @@ open class ChiaJsonRpcProvider(override val baseUrl: String, key: String) : Chia

override suspend fun sendTransaction(transaction: ChiaTransactionBody): SimpleResult {
return try {
val sendResponse = retryIO { api.sendTransaction(transaction) }
val tx = if (isRequiredHexPrefixForTx) {
transaction.appendHexPrefix()
} else {
transaction
}

val sendResponse = retryIO { api.sendTransaction(tx) }
if (sendResponse.success) {
if (sendResponse.status == SUCCESS_STATUS) {
SimpleResult.Success
Expand All @@ -81,6 +93,23 @@ open class ChiaJsonRpcProvider(override val baseUrl: String, key: String) : Chia
}
}

private fun ChiaTransactionBody.appendHexPrefix(): ChiaTransactionBody {
val aggregatedSignature = spendBundle.aggregatedSignature
val coinSpends = spendBundle.coinSpends
return copy(
spendBundle = spendBundle.copy(
aggregatedSignature = "$HEX_PREFIX$aggregatedSignature",
coinSpends = coinSpends.map {
ChiaCoinSpend(
coin = it.coin,
puzzleReveal = "$HEX_PREFIX${it.puzzleReveal}",
solution = "$HEX_PREFIX${it.solution}",
)
},
),
)
}

companion object {
// time in seconds, 1 minute only, 5 minutes gives bad estimate now
private val ESTIMATE_FEE_TARGET_TIMES = listOf(60)
Expand Down

0 comments on commit d7b10aa

Please sign in to comment.