Skip to content
Open
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
17 changes: 17 additions & 0 deletions web3/encoding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import
std/macros,
stint, ./eth_api_types, stew/[assign2, byteutils]

macro makeEncodingEthereumFuncs(): untyped =
## Creates all the encoding funcs needed to properly interact with Ethereum-like chains
result = newStmtList()
for numBits in [256, 128, 64, 32, 16, 8]:
let identUint = newIdentNode("EthereumUint" & $numBits)

result.add quote do:
func encode*(x: `identUint`): seq[byte] =
## the Ethereum types are created by makeEthereumType macro in eth_api_types.nim
let numTargetBytes = `numBits` div 8
let paddingBytes = 32 - numTargetBytes
## the Ethereum ABI imposes a 32 byte width for every type
let paddingZeros = newSeq[byte](paddingBytes)
paddingZeros & @(stint.toBytesBE(x))

makeEncodingEthereumFuncs()

func encode*[bits: static[int]](x: StUint[bits]): seq[byte] =
@(x.toBytesBE())

Expand Down
14 changes: 14 additions & 0 deletions web3/eth_api_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{.push raises: [].}

import
std/macros,
stint,
./primitives

Expand Down Expand Up @@ -299,6 +300,19 @@ func payload*(args: TransactionArgs): seq[byte] =
func isEIP4844*(args: TransactionArgs): bool =
args.maxFeePerBlobGas.isSome or args.blobVersionedHashes.isSome

macro makeEthereumTypes(): untyped =
## This macro creates all the various types of Eth contracts and maps
## them to the type used for their encoding.
result = newStmtList()
for i in [256, 128, 64, 32, 16, 8]:
let
identUint = newIdentNode("EthereumUint" & $i)

result.add quote do:
type `identUint`* = StUint[`i`]

makeEthereumTypes()

# Backwards compatibility

type
Expand Down
Loading