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

commonMain.dev.icerock.moko.web3.BlockState.kt Maven / Gradle / Ivy

There is a newer version: 0.18.4-ktor2_ionspinbignum
Show newest version
/*
 * Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
 */

package dev.icerock.moko.web3


import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.integer.toBigInteger
import dev.icerock.moko.web3.hex.HexString
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.jvm.JvmInline


object BlockStateSerializer : KSerializer {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
        serialName = "BlockStateStringSerializer",
        kind = PrimitiveKind.STRING
    )

    override fun deserialize(decoder: Decoder): BlockState = when (val string = decoder.decodeString()) {
        "latest" -> BlockState.Latest
        "earliest" -> BlockState.Earliest
        "pending" -> BlockState.Pending
        else -> BlockState.Quantity(string.toBigInteger())
    }

    override fun serialize(encoder: Encoder, value: BlockState) = encoder.encodeString(value.toString())

}

@Serializable(with = BlockStateSerializer::class)
sealed interface BlockState {
    override fun toString(): String

    object Latest : BlockState {
        override fun toString() = "latest"
    }
    object Earliest : BlockState {
        override fun toString() = "earliest"
    }
    object Pending : BlockState {
        override fun toString() = "pending"
    }

    @JvmInline
    value class Quantity(private val blockNumber: BigInteger) : BlockState {
        override fun toString() = HexString(blockNumber.toString(base = 16), strict = false).prefixed
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy