All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.swmansion.starknet.data.serializers.ExecutableTransactionSerializer.kt Maven / Gradle / Ivy

The newest version!
package com.swmansion.starknet.data.serializers

import com.swmansion.starknet.data.types.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*

internal object ExecutableTransactionSerializer : KSerializer {
    override val descriptor = PrimitiveSerialDescriptor("ExecutableTransaction", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): ExecutableTransaction {
        throw SerializationException("Class used for serialization only.")
    }

    override fun serialize(encoder: Encoder, value: ExecutableTransaction) {
        require(encoder is JsonEncoder)

        val jsonObject = when (value) {
            is InvokeTransactionV3 -> encoder.json.encodeToJsonElement(InvokeTransactionV3.serializer(), value).jsonObject
            is InvokeTransactionV1 -> encoder.json.encodeToJsonElement(InvokeTransactionV1.serializer(), value).jsonObject
            is DeclareTransactionV3 -> encoder.json.encodeToJsonElement(DeclareTransactionV3Serializer, value).jsonObject
            is DeclareTransactionV2 -> encoder.json.encodeToJsonElement(DeclareTransactionV2Serializer, value).jsonObject
            is DeclareTransactionV1 -> encoder.json.encodeToJsonElement(DeclareTransactionV1.serializer(), value).jsonObject
            is DeployAccountTransactionV3 -> encoder.json.encodeToJsonElement(DeployAccountTransactionV3.serializer(), value).jsonObject
            is DeployAccountTransactionV1 -> encoder.json.encodeToJsonElement(DeployAccountTransactionV1.serializer(), value).jsonObject
        }
        val result = JsonObject(
            jsonObject.filter { (key, _) -> !transactionIgnoredKeys.contains(key) }.plus("type" to encoder.json.encodeToJsonElement(value.type)),
        )
        encoder.encodeJsonElement(result)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy