-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from icerockdev/develop
Release 0.15.0
- Loading branch information
Showing
7 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
web3/src/commonMain/kotlin/dev.icerock.moko.web3/contract/BytesParam.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
web3/src/commonMain/kotlin/dev.icerock.moko.web3/contract/StringParam.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
web3/src/commonTest/kotlin/dev.icerock.moko.web3/TestEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters