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

Adapt encoding to ethereum #186

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions web3/encoding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import
std/macros,
stint, ./eth_api_types, stew/[assign2, byteutils]

func encode*(x: EthereumUint32): seq[byte] =
let numTargetBytes = 32 div 8
let paddingBytes = 32 - numTargetBytes
## the Ethereum ABI imposes a 32 byte width for every type
let paddingZeros = newSeq[byte](paddingBytes)
paddingZeros & @(x.toByteArrayBE())

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

Expand Down
32 changes: 32 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, math],
stint,
./primitives

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

macro makeEthereumTypeEnum(): untyped =
## This macro creates all the various types of Solidity contracts and maps
## them to the type used for their encoding. It also creates an enum to
## identify these types in the contract signatures, along with encoder
## functions used in the generated procedures.
result = newStmtList()
var lastpow2: int
for i in countdown(256, 8, 8):
let
identUint = newIdentNode("EthereumUint" & $i)
identInt = newIdentNode("EthereumInt" & $i)
if ceil(log2(i.float)) == floor(log2(i.float)):
lastpow2 = i

result.add quote do:
type
`identUint`* = StUint[`lastpow2`]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This in general will create some compatible-looking types in Nim which aren't actually compatible Solidity types, e.g., int40 and int48 are not the same in Solidity but they would end up being effectively the same in this mapping. Is that intention? Does it matter?

But this wouldn't hold between int64 and int72: those would map to 64 and 128 respectively, and would not be the same type. So this also becomes a question of, is that ok for your use case? It creates sort of distinctions in the Nim-side integer types which are different than the Solidity ones.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this always creates StInt/StUint types, even for lastpow2 <= 64. Maybe not optimal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would end up being effectively the same in this mapping

This might be an actual problem - ie we should give a compile-time error rather than being incompatible at runtime for "unsupported" integers (or support them properly)

`identInt`* = StInt[`lastpow2`]

let
identUint = ident("EthereumUint")
identInt = ident("EthereumInt")
identBool = ident("EthereumBool")
result.add quote do:
type
`identUint`* = UInt256
`identInt`* = Int256
`identBool`* = distinct Int256

makeEthereumTypeEnum()

# Backwards compatibility

type
Expand Down
Loading