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

commonMain.dev.icerock.moko.web3.contract.ListParam.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.contract

import com.ionspin.kotlin.bignum.integer.toBigInteger
import dev.icerock.moko.web3.contract.ABIEncoder.PART_SIZE

class ListParam(private val subtypeEncoder: Encoder) : DynamicEncoder> {
    override fun encode(item: List): ByteArray {
        val sizeEncoded = UInt256Param.encode(item.size.toBigInteger())
        return item
            .map(subtypeEncoder::encode)
            .fold(sizeEncoded) { acc, part -> acc + part }
    }
    override fun decode(source: ByteArray): List {
        val chunkedSource = source.toList().chunked(size = PART_SIZE)
        // We drop first element since the first element is always list size
        return chunkedSource.drop(n = 1)
            .map { subtypeEncoder.decode(it.toByteArray()) }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy