Skip to content

Commit

Permalink
Merge pull request #101 from icerockdev/develop
Browse files Browse the repository at this point in the history
Release 0.17.1
  • Loading branch information
anton6tak authored Jan 19, 2022
2 parents 1728c74 + 1639c2d commit 7570741
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 16 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.17.0")
commonMainApi("dev.icerock.moko:web3:0.17.1")
}
```

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.17.0"
mokoWeb3Version = "0.17.1"
multidexVersion = "2.0.1"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class SmartContract(
)
override fun deserialize(decoder: Decoder): T {
val abiResult = HexString(decoder.decodeString())
println(method)
println(abiResult)
return ABIDecoder.decodeCallDataForOutputs(abiJson, method, abiResult).let(mapper)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
package dev.icerock.moko.web3.requests

import com.soywiz.kbignum.BigInt
import dev.icerock.moko.web3.*
import com.soywiz.kbignum.bi
import dev.icerock.moko.web3.BlockHash
import dev.icerock.moko.web3.BlockInfo
import dev.icerock.moko.web3.BlockState
import dev.icerock.moko.web3.ContractAddress
import dev.icerock.moko.web3.EthereumAddress
import dev.icerock.moko.web3.TransactionHash
import dev.icerock.moko.web3.WalletAddress
import dev.icerock.moko.web3.Web3Executor
import dev.icerock.moko.web3.Web3RpcRequest
import dev.icerock.moko.web3.entity.LogEvent
import dev.icerock.moko.web3.entity.Transaction
import dev.icerock.moko.web3.entity.TransactionReceipt
import dev.icerock.moko.web3.hex.Hex32String
import dev.icerock.moko.web3.hex.HexString
import dev.icerock.moko.web3.requests.polling.shortPollingUntilNotNull
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.JsonElement


suspend fun <T> Web3Executor.executeBatch(vararg requests: Web3RpcRequest<*, T>): List<T> =
Expand Down Expand Up @@ -55,6 +62,24 @@ suspend fun Web3Executor.getGasPrice(): BigInt = executeBatch(Web3Requests.getGa
suspend fun Web3Executor.getEstimateGas(gasPrice: BigInt, to: EthereumAddress = EthereumAddress.AddressZero): BigInt =
executeBatch(Web3Requests.getEstimateGas(gasPrice, to)).first()

suspend fun Web3Executor.getEstimateGas(
from: EthereumAddress,
gasPrice: BigInt,
to: EthereumAddress,
callData: HexString,
value: BigInt = 0.bi
): BigInt =
executeBatch(
Web3Requests.getEstimateGas(
from = from,
gasPrice = gasPrice,
to = to,
callData = callData,
value = value
)
).first()


suspend fun Web3Executor.getBlockNumber(): BigInt = executeBatch(Web3Requests.getBlockNumber()).first()

suspend fun Web3Executor.getBlockByNumber(blockState: BlockState): BlockInfo? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.icerock.moko.web3.hex.Hex32String
import dev.icerock.moko.web3.hex.HexString
import dev.icerock.moko.web3.serializer.BigIntSerializer
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.nullable
Expand All @@ -44,6 +45,7 @@ object Web3Requests {
val to: ContractAddress,
val data: HexString,
)

fun <T> call(
contractAddress: ContractAddress,
callData: HexString,
Expand All @@ -59,6 +61,7 @@ object Web3Requests {
paramsSerializer = JsonElement.serializer(),
resultSerializer = dataDeserializer
)

fun getNativeTransactionCount(
walletAddress: WalletAddress,
blockState: BlockState = BlockState.Pending
Expand All @@ -68,6 +71,7 @@ object Web3Requests {
paramsSerializer = String.serializer(),
resultSerializer = BigIntSerializer
)

fun getTransactionReceipt(
transactionHash: TransactionHash
) = Web3RpcRequest(
Expand All @@ -76,6 +80,7 @@ object Web3Requests {
paramsSerializer = String.serializer(),
resultSerializer = TransactionReceipt.serializer().nullable
)

fun getNativeBalance(
walletAddress: WalletAddress,
blockState: BlockState = BlockState.Latest
Expand All @@ -85,6 +90,7 @@ object Web3Requests {
paramsSerializer = String.serializer(),
resultSerializer = BigIntSerializer
)

fun getTransaction(
transactionHash: TransactionHash
) = Web3RpcRequest(
Expand All @@ -93,18 +99,21 @@ object Web3Requests {
paramsSerializer = String.serializer(),
resultSerializer = Transaction.serializer()
)

fun getGasPrice() = Web3RpcRequest(
method = "eth_gasPrice",
params = listOf(),
paramsSerializer = ListSerializer(Unit.serializer()),
resultSerializer = BigIntSerializer
)

@Serializable
private data class GetEstimateGasObject(
val to: EthereumAddress,
@Serializable(with = BigIntSerializer::class)
val gasPrice: BigInt
)

fun getEstimateGas(
gasPrice: BigInt,
to: EthereumAddress = EthereumAddress.AddressZero,
Expand All @@ -114,18 +123,54 @@ object Web3Requests {
paramsSerializer = GetEstimateGasObject.serializer(),
resultSerializer = BigIntSerializer
)

@Serializable
private data class GetExtendedEstimateGasObject(
val from: EthereumAddress,
val to: EthereumAddress,
@Serializable(with = BigIntSerializer::class)
val gasPrice: BigInt,
@SerialName("data")
val callData: HexString,
@Serializable(with = BigIntSerializer::class)
val value: BigInt
)

fun getEstimateGas(
from: EthereumAddress,
gasPrice: BigInt,
to: EthereumAddress,
callData: HexString,
value: BigInt = 0.bi
): Web3RpcRequest<*, BigInt> = Web3RpcRequest(
method = "eth_estimateGas",
params = listOf(
GetExtendedEstimateGasObject(
from = from,
to = to,
gasPrice = gasPrice,
callData = callData,
value = value
)
),
paramsSerializer = GetExtendedEstimateGasObject.serializer(),
resultSerializer = BigIntSerializer
)

fun getBlockNumber() = Web3RpcRequest(
method = "eth_blockNumber",
params = listOf(),
paramsSerializer = ListSerializer(Unit.serializer()),
resultSerializer = BigIntSerializer
)

fun getBlockByNumber(block: BlockState) = Web3RpcRequest(
method = "eth_getBlockByNumber",
params = listOf(Json.encodeToJsonElement(BlockStateSerializer, block), JsonPrimitive(value = true)),
paramsSerializer = JsonElement.serializer(),
resultSerializer = BlockInfo.serializer().nullable
)

@Serializable
private data class GetLogsObject(
val address: EthereumAddress?,
Expand All @@ -134,6 +179,7 @@ object Web3Requests {
val topics: List<Hex32String>?,
val blockHash: BlockHash?
)

fun getLogs(
address: EthereumAddress? = null,
fromBlock: BlockState? = null,
Expand Down
42 changes: 33 additions & 9 deletions web3/src/commonTest/kotlin/dev.icerock.moko.web3/Web3Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dev.icerock.moko.web3

import com.soywiz.kbignum.BigInt
import com.soywiz.kbignum.bi
import com.soywiz.kbignum.bn
import dev.icerock.moko.web3.contract.ABIEncoder
import dev.icerock.moko.web3.contract.AddressParam
import dev.icerock.moko.web3.contract.SmartContract
Expand Down Expand Up @@ -100,7 +101,9 @@ class Web3Test {
expected = """[{"jsonrpc":"2.0","id":0,"method":"eth_getTransactionReceipt","params":["0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352"]}]""",
actual = body.text
)
respond(content = """[{"jsonrpc":"2.0","id":0,"result":{"blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","contractAddress":null,"cumulativeGasUsed":"0x3ae8d","effectiveGasPrice":"0x1dcd65000","from":"0xde7ec4e4895d7d148906a0dfaaf7f21ac5c5b80c","gasUsed":"0x34725","logs":[{"address":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","data":"0x00000000000000000000000000000000000000000000000000078a9f2e72421c000000000000000000000000000000000000000000000000103938f95c5bb8de00000000000000000000000000000000000000000000007e6f395eb639577b12","logIndex":"0x0","removed":false,"topics":["0x875352fb3fadeb8c0be7cbbe8ff761b308fa7033470cd0287f02f3436fd76cb9"],"transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1"},{"address":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","data":"0x00000000000000000000000000000000000000000000000000000000052f2dd9","logIndex":"0x1","removed":false,"topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x000000000000000000000000de7ec4e4895d7d148906a0dfaaf7f21ac5c5b80c","0x000000000000000000000000d6801a1dffcd0a410336ef88def4320d6df1883e"],"transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1"},{"address":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","data":"0x000000000000000000000000de7ec4e4895d7d148906a0dfaaf7f21ac5c5b80c00000000000000000000000000000000000000000000000000470de4df82000000000000000000000000000000000000000000000000000000000000052f2dd9","logIndex":"0x2","removed":false,"topics":["0xe5b754fb1abb7f01b499791d0b820ae3b6af3424ac1c59768edb53f4ec31a929"],"transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1"}],"logsBloom":"0x00000080000000000000000000000000000000000000000000000000000000000000040000100000800000000000000000000000000000000000000000000000000000000000000000000808000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000080000000000000000000000000000000000000800000000000000001000000000000000000000000000000000000000000000000000000002000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000020200000000004","status":"0x1","to":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1","type":"0x0"}}]""")
respond(
content = """[{"jsonrpc":"2.0","id":0,"result":{"blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","contractAddress":null,"cumulativeGasUsed":"0x3ae8d","effectiveGasPrice":"0x1dcd65000","from":"0xde7ec4e4895d7d148906a0dfaaf7f21ac5c5b80c","gasUsed":"0x34725","logs":[{"address":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","data":"0x00000000000000000000000000000000000000000000000000078a9f2e72421c000000000000000000000000000000000000000000000000103938f95c5bb8de00000000000000000000000000000000000000000000007e6f395eb639577b12","logIndex":"0x0","removed":false,"topics":["0x875352fb3fadeb8c0be7cbbe8ff761b308fa7033470cd0287f02f3436fd76cb9"],"transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1"},{"address":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","data":"0x00000000000000000000000000000000000000000000000000000000052f2dd9","logIndex":"0x1","removed":false,"topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x000000000000000000000000de7ec4e4895d7d148906a0dfaaf7f21ac5c5b80c","0x000000000000000000000000d6801a1dffcd0a410336ef88def4320d6df1883e"],"transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1"},{"address":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","blockHash":"0x3d62f862d25cf7015a485868b07825484fd7a51f77a9e7863fe45ec8a61db01b","blockNumber":"0x60f03b","data":"0x000000000000000000000000de7ec4e4895d7d148906a0dfaaf7f21ac5c5b80c00000000000000000000000000000000000000000000000000470de4df82000000000000000000000000000000000000000000000000000000000000052f2dd9","logIndex":"0x2","removed":false,"topics":["0xe5b754fb1abb7f01b499791d0b820ae3b6af3424ac1c59768edb53f4ec31a929"],"transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1"}],"logsBloom":"0x00000080000000000000000000000000000000000000000000000000000000000000040000100000800000000000000000000000000000000000000000000000000000000000000000000808000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000010000000000000000080000000000000000000000000000000000000800000000000000001000000000000000000000000000000000000000000000000000000002000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000020200000000004","status":"0x1","to":"0xd6801a1dffcd0a410336ef88def4320d6df1883e","transactionHash":"0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352","transactionIndex":"0x1","type":"0x0"}}]"""
)
}
val txHash = TransactionHash("0x627914c8d005ab0dc7f44719dc658af72e534e083867a2a316d4b25555515352")

Expand Down Expand Up @@ -182,7 +185,9 @@ class Web3Test {
}

assertEquals(
expected = "0xa9059cbb0000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d740000000000000000000000000000000000000000000000000000000010001000".let(::HexString),
expected = "0xa9059cbb0000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d740000000000000000000000000000000000000000000000000000000010001000".let(
::HexString
),
actual = result
)
}
Expand Down Expand Up @@ -255,7 +260,9 @@ class Web3Test {
)
}
assertEquals(
expected = "0x170159cd0000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000016345785d8a0000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000016345785d8a0000".let(::HexString),
expected = "0x170159cd0000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000016345785d8a0000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009a0a2498ec7f105ef65586592a5b6d4da3590d74000000000000000000000000000000000000000000000000016345785d8a0000".let(
::HexString
),
actual = result
)
}
Expand Down Expand Up @@ -354,12 +361,14 @@ class Web3Test {
}
}

// @Test
// @Test
fun `short polling test`() {
runTest {
println(Json.decodeFromString(
RpcResponse.serializer(JsonElement.serializer()),
"""{"jsonrpc":"","id":0,"result":null}""").result as JsonNull
println(
Json.decodeFromString(
RpcResponse.serializer(JsonElement.serializer()),
"""{"jsonrpc":"","id":0,"result":null}"""
).result as JsonNull
)
val web3 = Web3("https://rinkeby.infura.io/v3/5a3d2c30cf72450c9e13b0570a737b62")
web3.waitForTransactionReceipt(
Expand All @@ -369,7 +378,7 @@ class Web3Test {
}
}

// @Test
// @Test
fun `new blocks short polling test`() {
runBlocking {
val web3 = Web3("https://rinkeby.infura.io/v3/5a3d2c30cf72450c9e13b0570a737b62")
Expand All @@ -378,7 +387,7 @@ class Web3Test {
}
}

// @Test
// @Test
fun `new logs short polling test`() {
runBlocking {
val web3 = Web3("https://rinkeby.infura.io/v3/5a3d2c30cf72450c9e13b0570a737b62")
Expand All @@ -396,4 +405,19 @@ class Web3Test {
println("GAS Limit: ${web3.getEstimateGas(price)}")
}
}

// @Test
fun legacyExtendedTransactionForming() {
runBlocking {
val web3 = Web3("https://bsc.getblock.io/testnet/?api_key=94c96d69-74f0-40e7-8202-eac4b49e6bfc")
val price = web3.getGasPrice()
val callData =
HexString("0x38ed17390000000000000000000000000000000000000000000000008ac7230489e8000000000000000000000000000000000000000000000000000000019bc096da353600000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000140e21fcfb1e602a1626198d3dbbb58087b59b4e0000000000000000000000000000000000000000000000000000000061e7e45100000000000000000000000000000000000000000000000000000000000000030000000000000000000000009a01bf917477dd9f5d715d188618fc8b7350cd22000000000000000000000000ae13d989dac2f0debff460ac112a837c89baa7cd00000000000000000000000041b5984f45afb2560a0ed72bb69a98e8b32b3cca")
val to = ContractAddress("0xd99d1c33f9fc3444f8101754abc46c52416550d1")
val from = EthereumAddress("0x140e21FcFB1E602A1626198d3DbBB58087b59b4E")
println("GAS Price: $price")
println("GAS Limit: ${web3.getEstimateGas(from, price, to, callData).toBigNum().times(1.1.bn)}")
}
}
}

0 comments on commit 7570741

Please sign in to comment.