Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AND-8727 Fixed Сan't send transaction to firefly api #801

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading