commonMain.dev.icerock.moko.web3.contract.ListParam.kt Maven / Gradle / Ivy
/*
* 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