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 8de6d91 commit 15549ae
Show file tree
Hide file tree
Showing 2 changed files with 34 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,
providerType = ProviderType.Chia.Tangem,
)
}
}

Expand All @@ -43,6 +47,7 @@ internal class ChiaProvidersBuilder(
ChiaJsonRpcProvider(
baseUrl = if (isTestnet) API_CHIA_FIREACADEMY_TESTNET else API_CHIA_FIREACADEMY,
key = config.chiaFireAcademyApiKey,
providerType = ProviderType.Chia.FireAcademy,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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.network.providers.ProviderType
import com.tangem.blockchain.common.toBlockchainSdkError
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
Expand All @@ -11,7 +13,8 @@ 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, key: String, private val providerType: ProviderType) :
ChiaNetworkProvider {

private val api: ChiaApi by lazy {
createRetrofitInstance(
Expand Down Expand Up @@ -62,7 +65,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 (providerType == ProviderType.Chia.FireAcademy) {
transaction.appendHexPrefix()
} else {
transaction
}

val sendResponse = retryIO { api.sendTransaction(tx) }
if (sendResponse.success) {
if (sendResponse.status == SUCCESS_STATUS) {
SimpleResult.Success
Expand All @@ -81,6 +90,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 15549ae

Please sign in to comment.