Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(abi): add additional ContractCall#call/traceCall overloads #67

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ethers-abi/src/main/kotlin/io/ethers/abi/call/ContractCall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.ethers.core.types.BlockId
import io.ethers.core.types.BlockOverride
import io.ethers.core.types.Bytes
import io.ethers.core.types.CallRequest
import io.ethers.core.types.Hash
import io.ethers.core.types.tracers.TracerConfig
import io.ethers.core.types.transaction.TransactionSigned
import io.ethers.core.types.transaction.TransactionUnsigned
Expand Down Expand Up @@ -131,6 +132,32 @@ abstract class ReadContractCall<C, B : ReadContractCall<C, B>>(
) {
protected val call = CallRequest().apply { chainId = provider.chainId }

/**
* Execute "eth_call" at the given [blockHash] and return the result of the call. This is a read-only call, and it
* will not modify the blockchain state. Optionally, the [stateOverride] and [blockOverride] can be provided to
* override the state on which the call is executed.
* */
fun call(
blockHash: Hash,
stateOverride: Map<Address, AccountOverride>? = null,
blockOverride: BlockOverride? = null,
): RpcRequest<C, ContractError> {
return call(BlockId.Hash(blockHash), stateOverride, blockOverride)
}

/**
* Execute "eth_call" at the given [blockNumber] and return the result of the call. This is a read-only call, and it
* will not modify the blockchain state. Optionally, the [stateOverride] and [blockOverride] can be provided to
* override the state on which the call is executed.
* */
fun call(
blockNumber: Long,
stateOverride: Map<Address, AccountOverride>? = null,
blockOverride: BlockOverride? = null,
): RpcRequest<C, ContractError> {
return call(BlockId.Number(blockNumber), stateOverride, blockOverride)
}

/**
* Execute "eth_call" at the given [BlockId] and return the result of the call. This is a read-only call, and it
* will not modify the blockchain state. Optionally, the [stateOverride] and [blockOverride] can be provided to
Expand All @@ -147,6 +174,22 @@ abstract class ReadContractCall<C, B : ReadContractCall<C, B>>(
.andThen(::safelyHandleCallResult)
}

/**
* Execute "debug_traceCall" at the given [blockHash] using the provided [TracerConfig], returning the result of
* the tracer. Similar to [call] function, this is a read-only call, and it will not modify the blockchain state.
* */
fun <T> traceCall(blockHash: Hash, config: TracerConfig<T>): RpcRequest<T, RpcError> {
return traceCall(BlockId.Hash(blockHash), config)
}

/**
* Execute "debug_traceCall" at the given [blockNumber] using the provided [TracerConfig], returning the result of
* the tracer. Similar to [call] function, this is a read-only call, and it will not modify the blockchain state.
* */
fun <T> traceCall(blockNumber: Long, config: TracerConfig<T>): RpcRequest<T, RpcError> {
return traceCall(BlockId.Number(blockNumber), config)
}

/**
* Execute "debug_traceCall" at the given [BlockId] using the provided [TracerConfig], returning the result of
* the tracer. Similar to [call] function, this is a read-only call, and it will not modify the blockchain state.
Expand Down
4 changes: 2 additions & 2 deletions ethers-abi/src/test/kotlin/io/ethers/abi/AbiEventTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class AbiEventTest : FunSpec({
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
),
data = Bytes.ZERO,
data = Bytes.EMPTY,
blockHash = Hash("0x8bbd497a03cf0a0690bbb91b38afc539e8552da391cff2d5861abb28a24d3129"),
blockNumber = 18293121,
transactionHash = Hash("0x24ab1ac3496270b0f7719c23e32aa1bac92e15c8f00682a3dd00ebe88d89a9c8"),
Expand Down Expand Up @@ -166,7 +166,7 @@ class AbiEventTest : FunSpec({
Hash("0x000000000000000000000000855f02967ee16e9f18d388b07b4c75211e73e8c2"),
Hash("0x000000000000000000000000ed12310d5a37326e6506209c4838146950166760"),
),
data = Bytes.ZERO,
data = Bytes.EMPTY,
blockHash = Hash("0x32f8ce74b282a9dc63df4bdde2f5a1fc36efc95cf6e5e6e77d5e772b3f32e7da"),
blockNumber = 18351281,
transactionHash = Hash("0xd91f8e2ada54a4ca4c5c78265f916a1e6ef7b318838a32eb967918de0832878a"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fun JsonParser.readAddress(): Address {
fun JsonParser.readBytes(): Bytes {
val arr = readHexByteArray()
if (arr.isEmpty()) {
return Bytes.ZERO
return Bytes.EMPTY
}
return Bytes(arr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Bytes(val value: ByteArray) : RlpEncodable {

companion object : RlpDecodable<Bytes> {
@JvmField
val ZERO = Bytes(ByteArray(0))
val EMPTY = Bytes(ByteArray(0))

override fun rlpDecode(rlp: RlpDecoder): Bytes? {
return rlp.decodeByteArray(::Bytes)
Expand Down
Loading