Skip to content
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
5ecd053
Add nimbledeps to gitignore
2-towns Jul 18, 2025
3cfe5a7
Add abi encoder
2-towns Jul 18, 2025
5565dbc
Add encoding tests
2-towns Jul 18, 2025
f9991ab
Add AbiEncoder
2-towns Jul 21, 2025
ec1ae61
Add encoding tests
2-towns Jul 21, 2025
85e34d9
Fix async signature
2-towns Jul 21, 2025
0e56f72
Add encoding tests to all tests
2-towns Jul 21, 2025
f1e58ba
Do not bubble the exception
2-towns Jul 21, 2025
303757e
Add proper error handling
2-towns Jul 22, 2025
0b17d13
Use faststream to simplify internal logic
2-towns Jul 23, 2025
b39d301
Partial implementation of the decoding
2-towns Jul 25, 2025
07e5222
Fix tuple encoding and try to provide better docs / tests
2-towns Jul 28, 2025
75d3f34
Update abi util to to take tuple in consideration
2-towns Jul 28, 2025
c0725cc
Add deprecation notice
2-towns Jul 28, 2025
5f761c4
Refactor the dynamic array / seq decoding
2-towns Jul 28, 2025
cb0cdc8
Add decoding
2-towns Jul 29, 2025
2e8a26f
Add ABI encoder / decoder to the readme
2-towns Jul 29, 2025
e668ed9
Fix indent
2-towns Jul 29, 2025
9ed1dfa
Export decoding
2-towns Jul 29, 2025
a6f01fe
Restore previous encoding functions and mark them deprecated
2-towns Jul 29, 2025
bbe92b3
Remove compiler hints and warnings
2-towns Jul 29, 2025
8c99fdc
Use inputStream signature and use readAll
2-towns Jul 29, 2025
c4f9cec
Add custom types
2-towns Jul 29, 2025
008de45
Cleanup
2-towns Jul 29, 2025
03551c4
Add support for serialization
2-towns Jul 30, 2025
0d7d73b
Add support for dontSerialize pragma
2-towns Jul 30, 2025
2c94705
Add readValue compatibility
2-towns Jul 31, 2025
a4fe4a5
Add readme for serialization
2-towns Jul 31, 2025
1a9bb7b
Use enumInstanceSerializedFields to get the serialized fields instead…
2-towns Jul 31, 2025
b0a0c33
Use Abi serialization as main interface
2-towns Jul 31, 2025
582c0fc
Update README
2-towns Jul 31, 2025
fc50389
Update deprecation message and export abi_serialization
2-towns Jul 31, 2025
6b1cb1e
Update deprecation message and export abi_serialization
2-towns Jul 31, 2025
7050707
Add tests for string
2-towns Aug 6, 2025
4c59e6a
Test encode empty string
2-towns Aug 6, 2025
6c0c9f9
Encode object as tuple
2-towns Aug 6, 2025
9b8aa1b
Remove ref object
2-towns Aug 6, 2025
5c43005
Ensure that object are encoded as tuple
2-towns Aug 6, 2025
4829b93
Decode object as tuple
2-towns Aug 6, 2025
ec7874e
Remove compilation hint
2-towns Aug 6, 2025
e080856
Remove unused import
2-towns Aug 6, 2025
2e6fb2d
Replace ABI encoding and decoding by the new encoding method
2-towns Aug 6, 2025
14bd6b9
Improve serialization test
2-towns Aug 7, 2025
0354fdc
Add test more 3 slots
2-towns Aug 7, 2025
f9d8368
Remove unused variables
2-towns Aug 7, 2025
22e9867
Use existing type T to get the arity
2-towns Aug 7, 2025
d6e87c3
Use advance function to point on the right position instead of copyin…
2-towns Aug 7, 2025
c5c0baa
Use local encoder instead of calling Abi.encode
2-towns Aug 7, 2025
3340bb7
Use SomeRange type in testing
2-towns Aug 7, 2025
b3cf234
Add overflow detection when decoding int and provide clearer comments
2-towns Aug 8, 2025
9e942b8
Re organize comments
2-towns Aug 8, 2025
f381d5f
Fix indentation
2-towns Aug 8, 2025
6fec2df
Add raises pragma
2-towns Aug 11, 2025
b75ec43
Remove int / uint support
2-towns Aug 22, 2025
5480654
Use toOpenArray method and replace StUint usage by uint.
2-towns Aug 22, 2025
d2bba2c
Use used pragma
2-towns Aug 22, 2025
13553f4
Reject range with int / uint value
2-towns Aug 22, 2025
0dd3c4d
Apply toOpenArray function
2-towns Aug 22, 2025
f9355a6
Remove unused function
2-towns Aug 22, 2025
2ee569f
Check hardhat version on ci
2-towns Aug 25, 2025
565c359
Update ci with hardhat 3
2-towns Aug 26, 2025
763a399
Remove the intermediate data seq
2-towns Aug 26, 2025
7ccd836
Remove comment
2-towns Aug 26, 2025
4d44970
Remove comment
2-towns Aug 26, 2025
e86e830
Use explicit return
2-towns Aug 28, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build/
*.exe
*.dll
nimble.paths
nimbledeps

node_modules
nohup.out
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,56 @@ Then you should run `./simulator.sh` which runs `hardhat node`

This creates a local simulated Ethereum network on your local machine and the tests will use this for their E2E processing

### ABI encoder / decoder

Implements encoding of parameters according to the Ethereum
[Contract ABI Specification][1] using nim-serialization interface.

Usage
-----

```nim
import
serialization,
stint,
web3/[encoding, decoding]

# encode unsigned integers, booleans, enums
Abi.encode(42'u8)

# encode uint256
Abi.encode(42.u256)

# encode byte arrays and sequences
Abi.encode([1'u8, 2'u8, 3'u8])
Abi.encode(@[1'u8, 2'u8, 3'u8])

# encode tuples
Abi.encode( (42'u8, @[1'u8, 2'u8, 3'u8], true) )

# decode values of different types
Abi.decode(bytes, uint8)
Abi.decode(bytes, UInt256)
Abi.decode(bytes, array[3, uint8])
Abi.decode(bytes, seq[uint8])

# decode tuples
Abi.decode(bytes, (uint32, bool, seq[byte]))

# custom type
type Contract = object
a: uint64
b {.dontSerialize.}: string
c: bool

let encoded = Abi.encode(x)
let decoded = Abi.decode(encoded, Contract)

# encoded.a == decoded.a
# encoded.b == ""
# encoded.c == decoded.c
```

## License

Licensed and distributed under either of
Expand All @@ -46,3 +96,5 @@ or
- Apache License, Version 2.0, ([LICENSE-APACHEv2](LICENSE-APACHEv2) or http://www.apache.org/licenses/LICENSE-2.0)

at your option. This file may not be copied, modified, or distributed except according to those terms.

[1]: https://docs.soliditylang.org/en/latest/abi-spec.html
6 changes: 4 additions & 2 deletions ci-test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash

set -ex
npm install hardhat
touch hardhat.config.js
npm install hardhat@3
npm pkg set type="module"
echo "export default {};" > hardhat.config.js
npx hardhat --version
nohup npx hardhat node &
nimble install -y --depsOnly

Expand Down
6 changes: 5 additions & 1 deletion tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ import
test_signed_tx,
test_execution_types,
test_string_decoder,
test_abi_utils,
test_encoding,
test_decoding,
test_abi_serialization,
test_contract_dsl,
test_execution_api
test_execution_api
67 changes: 67 additions & 0 deletions tests/test_abi_serialization.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import
std/unittest,
serialization,
std/random,
../web3/encoding,
../web3/decoding,
../web3/eth_api_types,
../web3/abi_serialization

type Contract = object
a: uint64
b {.dontSerialize.}: string
c: bool
d: string

type StorageDeal = object
client: array[20, byte]
provider: array[20, byte]
cid: array[32, byte]
size: uint64
duration: uint64
pricePerByte: UInt256
signature: array[65, byte]
metadata: string

proc randomBytes[N: static int](): array[N, byte] =
var a: array[N, byte]
for b in a.mitems:
b = rand(byte)
return a

suite "ABI serialization":
test "encode and decode custom type":
let x = Contract(a: 1, b: "SECRET", c: true, d: "hello")

let encoded = Abi.encode(x)
check encoded == Abi.encode((x.a, x.c, x.d))

let decoded = Abi.decode(encoded, Contract)
check decoded.a == x.a
check decoded.b == ""
check decoded.c == x.c
check decoded.d == x.d

test "encode and decode complex type":
let deal = StorageDeal(
client: randomBytes[20](),
provider: randomBytes[20](),
cid: randomBytes[32](),
size: 1024'u64,
duration: 365'u64,
pricePerByte: 1000.u256,
signature: randomBytes[65](),
metadata: "Sample metadata for storage deal"
)

let encoded = Abi.encode(deal)
let decoded = Abi.decode(encoded, StorageDeal)

check decoded.client == deal.client
check decoded.provider == deal.provider
check decoded.cid == deal.cid
check decoded.size == deal.size
check decoded.duration == deal.duration
check decoded.pricePerByte == deal.pricePerByte
check decoded.signature == deal.signature
check decoded.metadata == deal.metadata
15 changes: 15 additions & 0 deletions tests/test_abi_utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import
std/unittest,
../web3/abi_utils

suite "ABI utils":
test "can determine whether types are dynamic or static":
check static isStatic(uint8)
check static isDynamic(seq[byte])
check static isStatic(array[2, array[2, byte]])
check static isDynamic(array[2, seq[byte]])
check static isStatic((uint8, bool))
check static isDynamic((uint8, seq[byte]))
check static isStatic((uint8, (bool, uint8)))
check static isDynamic((uint8, (bool, seq[byte])))
1 change: 1 addition & 0 deletions tests/test_contract_dsl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import
pkg/unittest2,
stew/byteutils,
stint,
../web3/encoding,
../web3/contract_dsl

type
Expand Down
Loading
Loading