Skip to content

Commit

Permalink
add Prague engine API support types (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Mar 27, 2024
1 parent 4d80530 commit 85e34e8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ jobs:
include:
- target:
os: linux
builder: ubuntu-20.04
builder: ubuntu-latest
shell: bash
- target:
os: macos
builder: macos-12
builder: macos-latest
shell: bash
- target:
os: windows
builder: windows-2019
builder: windows-latest
shell: msys2 {0}

defaults:
Expand Down
20 changes: 10 additions & 10 deletions web3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type
data*: seq[byte]
sender*: TSender

proc getValue(params: RequestParamsRx, field: string, FieldType: type):
func getValue(params: RequestParamsRx, field: string, FieldType: type):
Result[FieldType, string] {.gcsafe, raises: [].} =
try:
for param in params.named:
Expand All @@ -86,7 +86,7 @@ proc getValue(params: RequestParamsRx, field: string, FieldType: type):
except CatchableError as exc:
return err(exc.msg)

proc toJsonString(params: RequestParamsRx):
func toJsonString(params: RequestParamsRx):
Result[JsonString, string] {.gcsafe, raises: [].} =
try:
let res = JrpcSys.encode(params.toTx)
Expand All @@ -111,7 +111,7 @@ proc handleSubscriptionNotification(w: Web3, params: RequestParamsRx):

ok()

proc newWeb3*(provider: RpcClient): Web3 =
func newWeb3*(provider: RpcClient): Web3 =
result = Web3(provider: provider)
result.subscriptions = initTable[string, Subscription]()
let w3 = result
Expand Down Expand Up @@ -221,7 +221,7 @@ proc subscribeForLogs*(w: Web3, options: FilterOptions,
else:
result.historicalEventsProcessed = true

proc addAddressAndSignatureToOptions(options: FilterOptions, address: Address, topic: Topic): FilterOptions =
func addAddressAndSignatureToOptions(options: FilterOptions, address: Address, topic: Topic): FilterOptions =
result = options
if result.address.kind == slkNull:
result.address = AddressOrList(kind: slkSingle, single: address)
Expand Down Expand Up @@ -429,16 +429,16 @@ proc exec*[T](c: ContractInvocation[T, Web3SenderImpl], value = 0.u256, gas = 30
#let response = waitFor w3.eth.eth_sendTransaction(cc)
#echo response

proc contractSender*(web3: Web3, T: typedesc, toAddress: Address): Sender[T] =
func contractSender*(web3: Web3, T: typedesc, toAddress: Address): Sender[T] =
Sender[T](sender: Web3SenderImpl(web3: web3, contractAddress: toAddress))

proc createMutableContractInvocation*(sender: Web3SenderImpl, ReturnType: typedesc, data: sink seq[byte]): ContractInvocation[ReturnType, Web3SenderImpl] {.inline.} =
func createMutableContractInvocation*(sender: Web3SenderImpl, ReturnType: typedesc, data: sink seq[byte]): ContractInvocation[ReturnType, Web3SenderImpl] {.inline.} =
ContractInvocation[ReturnType, Web3SenderImpl](sender: sender, data: data)

proc createImmutableContractInvocation*(sender: Web3SenderImpl, ReturnType: typedesc, data: sink seq[byte]): ContractInvocation[ReturnType, Web3SenderImpl] {.inline.} =
func createImmutableContractInvocation*(sender: Web3SenderImpl, ReturnType: typedesc, data: sink seq[byte]): ContractInvocation[ReturnType, Web3SenderImpl] {.inline.} =
ContractInvocation[ReturnType, Web3SenderImpl](sender: sender, data: data)

proc contractInstance*(
func contractInstance*(
web3: Web3, T: typedesc, toAddress: Address): AsyncSender[T] =
AsyncSender[T](
sender: Web3AsyncSenderImpl(
Expand Down Expand Up @@ -499,7 +499,7 @@ proc isDeployed*(s: Sender, atBlock: RtBlockIdentifier): Future[bool] {.async.}
proc subscribe*[TContract](s: Sender[TContract], t: typedesc, cb: proc): Future[Subscription] {.inline.} =
subscribe(s, t, FilterOptions(), cb, SubscriptionErrorHandler nil)

proc copy[T](s: AsyncSender[T]): AsyncSender[T] =
func copy[T](s: AsyncSender[T]): AsyncSender[T] =
result = s
result.sender.new()
result.sender[] = s.sender[]
Expand All @@ -518,4 +518,4 @@ macro adjust*(s: AsyncSender, modifications: varargs[untyped]): untyped =
let fieldVal = s[1]
result[1].add quote do:
`cp`.sender.`fieldName` = `fieldVal`
result[1].add(cp)
result[1].add(cp)
6 changes: 5 additions & 1 deletion web3/conversions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ derefType(ReceiptObject).useDefaultSerializationIn JrpcConv
#------------------------------------------------------------------------------

WithdrawalV1.useDefaultSerializationIn JrpcConv
DepositReceiptV1.useDefaultSerializationIn JrpcConv
ExitV1.useDefaultSerializationIn JrpcConv
ExecutionPayloadV1.useDefaultSerializationIn JrpcConv
ExecutionPayloadV2.useDefaultSerializationIn JrpcConv
ExecutionPayloadV1OrV2.useDefaultSerializationIn JrpcConv
ExecutionPayloadV3.useDefaultSerializationIn JrpcConv
ExecutionPayloadV4.useDefaultSerializationIn JrpcConv
BlobsBundleV1.useDefaultSerializationIn JrpcConv
ExecutionPayloadBodyV1.useDefaultSerializationIn JrpcConv
PayloadAttributesV1.useDefaultSerializationIn JrpcConv
Expand All @@ -71,6 +74,7 @@ TransitionConfigurationV1.useDefaultSerializationIn JrpcConv
GetPayloadV2Response.useDefaultSerializationIn JrpcConv
GetPayloadV2ResponseExact.useDefaultSerializationIn JrpcConv
GetPayloadV3Response.useDefaultSerializationIn JrpcConv
GetPayloadV4Response.useDefaultSerializationIn JrpcConv

#------------------------------------------------------------------------------
# execution_types
Expand Down Expand Up @@ -400,4 +404,4 @@ func `$`*(v: TypedTransaction): string {.inline.} =
func `$`*(v: RlpEncodedBytes): string {.inline.} =
"0x" & distinctBase(v).toHex

{.pop.}
{.pop.}
6 changes: 4 additions & 2 deletions web3/engine_api.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# nim-web3
# Copyright (c) 2022-2023 Status Research & Development GmbH
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
Expand Down Expand Up @@ -28,13 +28,15 @@ createRpcSigsFromNim(RpcClient):
proc engine_newPayloadV1(payload: ExecutionPayloadV1): PayloadStatusV1
proc engine_newPayloadV2(payload: ExecutionPayloadV2): PayloadStatusV1
proc engine_newPayloadV3(payload: ExecutionPayloadV3, expectedBlobVersionedHashes: seq[VersionedHash], parentBeaconBlockRoot: FixedBytes[32]): PayloadStatusV1
proc engine_newPayloadV4(payload: ExecutionPayloadV4, expectedBlobVersionedHashes: seq[VersionedHash], parentBeaconBlockRoot: FixedBytes[32]): PayloadStatusV1
proc engine_forkchoiceUpdatedV1(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Option[PayloadAttributesV1]): ForkchoiceUpdatedResponse
proc engine_forkchoiceUpdatedV2(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Option[PayloadAttributesV2]): ForkchoiceUpdatedResponse
proc engine_forkchoiceUpdatedV3(forkchoiceState: ForkchoiceStateV1, payloadAttributes: Option[PayloadAttributesV3]): ForkchoiceUpdatedResponse
proc engine_getPayloadV1(payloadId: PayloadID): ExecutionPayloadV1
proc engine_getPayloadV2(payloadId: PayloadID): GetPayloadV2Response
proc engine_getPayloadV2_exact(payloadId: PayloadID): GetPayloadV2ResponseExact
proc engine_getPayloadV3(payloadId: PayloadID): GetPayloadV3Response
proc engine_getPayloadV4(payloadId: PayloadID): GetPayloadV4Response
proc engine_exchangeTransitionConfigurationV1(transitionConfiguration: TransitionConfigurationV1): TransitionConfigurationV1
proc engine_getPayloadBodiesByHashV1(hashes: seq[BlockHash]): seq[Option[ExecutionPayloadBodyV1]]
proc engine_getPayloadBodiesByRangeV1(start: Quantity, count: Quantity): seq[Option[ExecutionPayloadBodyV1]]
Expand Down Expand Up @@ -115,4 +117,4 @@ template newPayload*(
template exchangeCapabilities*(
rpcClient: RpcClient,
methods: seq[string]): Future[seq[string]] =
engine_exchangeCapabilities(rpcClient, methods)
engine_exchangeCapabilities(rpcClient, methods)
50 changes: 47 additions & 3 deletions web3/engine_api_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ type
address*: Address
amount*: Quantity

# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md#depositreceiptv1
DepositReceiptV1* = object
pubkey*: FixedBytes[48]
withdrawalCredentials*: FixedBytes[32]
amount*: Quantity
signature*: FixedBytes[96]
index*: Quantity

# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md#exitv1
ExitV1* = object
sourceAddress*: Address
validatorPublicKey*: FixedBytes[48]

# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/paris.md#executionpayloadv1
ExecutionPayloadV1* = object
parentHash*: Hash256
Expand Down Expand Up @@ -110,10 +123,33 @@ type
blobGasUsed*: Quantity
excessBlobGas*: Quantity

# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md#executionpayloadv4
ExecutionPayloadV4* = object
parentHash*: Hash256
feeRecipient*: Address
stateRoot*: Hash256
receiptsRoot*: Hash256
logsBloom*: FixedBytes[256]
prevRandao*: FixedBytes[32]
blockNumber*: Quantity
gasLimit*: Quantity
gasUsed*: Quantity
timestamp*: Quantity
extraData*: DynamicBytes[0, 32]
baseFeePerGas*: UInt256
blockHash*: Hash256
transactions*: seq[TypedTransaction]
withdrawals*: seq[WithdrawalV1]
blobGasUsed*: Quantity
excessBlobGas*: Quantity
depositReceipts*: seq[DepositReceiptV1]
exits*: seq[ExitV1]

SomeExecutionPayload* =
ExecutionPayloadV1 |
ExecutionPayloadV2 |
ExecutionPayloadV3
ExecutionPayloadV3 |
ExecutionPayloadV4

# https://github.com/ethereum/execution-apis/blob/ee3df5bc38f28ef35385cefc9d9ca18d5e502778/src/engine/cancun.md#blobsbundlev1
BlobsBundleV1* = object
Expand Down Expand Up @@ -210,10 +246,18 @@ type
blobsBundle*: BlobsBundleV1
shouldOverrideBuilder*: bool

# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md#response-1
GetPayloadV4Response* = object
executionPayload*: ExecutionPayloadV3
blockValue*: UInt256
blobsBundle*: BlobsBundleV1
shouldOverrideBuilder*: bool

SomeGetPayloadResponse* =
ExecutionPayloadV1 |
GetPayloadV2Response |
GetPayloadV3Response
GetPayloadV3Response |
GetPayloadV4Response

const
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/common.md#errors
Expand All @@ -232,4 +276,4 @@ const
{.push raises: [].}

template `==`*(a, b: TypedTransaction): bool =
distinctBase(a) == distinctBase(b)
distinctBase(a) == distinctBase(b)

0 comments on commit 85e34e8

Please sign in to comment.