Skip to content

Commit

Permalink
Merge pull request #89 from icerockdev/develop
Browse files Browse the repository at this point in the history
Release 0.15.0
  • Loading branch information
anton6tak authored Nov 22, 2021
2 parents 38b4277 + 4271573 commit 425293e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ allprojects {
project build.gradle
```groovy
dependencies {
commonMainApi("dev.icerock.moko:web3:0.14.1")
commonMainApi("dev.icerock.moko:web3:0.15.0")
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ kbignumVersion = "2.2.0"
klockVersion = "2.2.2"
ktorClientVersion = "1.6.1"
mokoTestVersion = "0.4.0"
mokoWeb3Version = "0.14.1"
mokoWeb3Version = "0.15.0"
multidexVersion = "2.0.1"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.soywiz.kbignum.bi
import dev.icerock.moko.web3.contract.MethodEncoder.PART_SIZE

object BytesParam : DynamicEncoder<ByteArray> {
override fun encode(item: ByteArray): ByteArray {
val sizeData = UInt256Param.encode(item.size.bi)
return item
.asIterable()
.chunked(PART_SIZE)
.map {
it.toByteArray() + ByteArray(PART_SIZE - it.size)
}.fold(initial = sizeData) { acc, bytes ->
acc + bytes
}
}

override fun decode(source: ByteArray): ByteArray {
TODO("moko-web3 does not support decoding dynamic params yet")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ object MethodEncoder {
ListParam(StaticEncoders.forType(subtypeAnnotation).encoder)
}
typeAnnotation == "tuple" -> TupleParam(param)
typeAnnotation == "string" -> StringParam
typeAnnotation == "bytes" -> BytesParam
else -> StaticEncoders.forType(typeAnnotation).encoder
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.soywiz.kbignum.bi
import dev.icerock.moko.web3.contract.MethodEncoder.PART_SIZE
import io.ktor.utils.io.core.toByteArray

object StringParam : DynamicEncoder<String> {
override fun encode(item: String): ByteArray {
// here we calculate the size of a string
val size: ByteArray = UInt256Param.encode(item.length.bi)
return item.chunked(PART_SIZE).map {
it.toByteArray() + ByteArray(PART_SIZE - it.length)
}.fold(initial = size) { acc, bytes ->
acc + bytes
}
}

override fun decode(source: ByteArray): String {
TODO("moko-web3 does not support decoding dynamic params yet")
}
}
42 changes: 42 additions & 0 deletions web3/src/commonTest/kotlin/dev.icerock.moko.web3/TestEncoder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.web3

import dev.icerock.moko.web3.contract.MethodEncoder
import dev.icerock.moko.web3.hex.internal.toHex
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals

class TestEncoder {

// Checked, everything works fine
// to check it, please visit https://abi.hashex.org and place there your abi
@Test
fun `test string encoder`() {
val abiJson = createTestAbi(Json)
val callData: String = MethodEncoder.createCallData(
abi = abiJson,
method = "testStringEncoder",
params = listOf("test")
)
val expected = "0xe4e8653c000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000"
assertEquals(expected, callData)
}

// Checked, everything works fine
@Test
fun `test bytes encoder`() {
val abiJson = createTestAbi(Json)
val array = byteArrayOf(0, 11, 22, 33, 44, 55, 66, 77)
val callData: String = MethodEncoder.createCallData(
abi = abiJson,
method = "testBytesEncoder",
params = listOf(array)
)
val expected = "0xe1d2647500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000008000b16212c37424d000000000000000000000000000000000000000000000000"
assertEquals(expected, callData)
}
}
38 changes: 38 additions & 0 deletions web3/src/commonTest/kotlin/dev.icerock.moko.web3/createTestAbi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ private const val testAbiRaw = """
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "stringType",
"type": "string"
}
],
"name": "testStringEncoder",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "bytesType",
"type": "bytes"
}
],
"name": "testBytesEncoder",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
"""
Expand Down

0 comments on commit 425293e

Please sign in to comment.