com.swmansion.starknet.data.serializers.TransactionTracePolymorphicSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of starknet Show documentation
Show all versions of starknet Show documentation
Starknet SDK for JVM languages
The newest version!
package com.swmansion.starknet.data.serializers
import com.swmansion.starknet.data.types.*
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.*
import java.lang.IllegalArgumentException
internal object TransactionTracePolymorphicSerializer :
JsonContentPolymorphicSerializer(TransactionTrace::class) {
private fun selectInvokeTransactionTraceDeserializer(jsonObject: JsonObject): DeserializationStrategy {
val executeInvocation = jsonObject["execute_invocation"]?.jsonObject ?: throw IllegalStateException("Response from node contains invalid INVOKE_TXN_TRACE: execute_invocation is missing.")
val isReverted = "revert_reason" in executeInvocation
return when (isReverted) {
true -> RevertedInvokeTransactionTrace.serializer()
false -> InvokeTransactionTrace.serializer()
}
}
override fun selectDeserializer(element: JsonElement): DeserializationStrategy {
val jsonObject = element.jsonObject
val typeElement = jsonObject.getOrElse("type") { throw SerializationException("Input element does not contain mandatory field 'type'") }
val type = Json.decodeFromJsonElement(TransactionType.serializer(), typeElement)
return when (type) {
TransactionType.INVOKE -> selectInvokeTransactionTraceDeserializer(jsonObject)
TransactionType.DEPLOY_ACCOUNT -> DeployAccountTransactionTrace.serializer()
TransactionType.L1_HANDLER -> L1HandlerTransactionTrace.serializer()
TransactionType.DECLARE -> DeclareTransactionTrace.serializer()
else -> throw IllegalArgumentException("Unknown transaction trace type '${typeElement.jsonPrimitive.content}'")
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy