Skip to content

Commit dcabb8f

Browse files
committed
v0.2.0: Revamp data types
1 parent 428b931 commit dcabb8f

34 files changed

+1536
-692
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
nimcache/
2+
vendor/
23

34
# Executables shall be put in an ignored build/ directory
45
# Ignore dynamic, static libs and libtool archive files
@@ -9,6 +10,7 @@ build/
910
*.la
1011
*.exe
1112
*.dll
13+
nimble.paths
1214

1315
node_modules
1416
nohup.out

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
44
[![License: Apache](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
55
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
6-
![Github action](https://github.com/status-im/nim-web3/workflows/nim-web3%20CI/badge.svg)
6+
![Github action](https://github.com/status-im/nim-web3/workflows/CI/badge.svg)
77

88
The humble beginnings of a Nim library similar to web3.[js|py]
99

ci-test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ touch hardhat.config.js
66
nohup npx hardhat node &
77
nimble install -y --depsOnly
88

9-
# Wait until ganache responds
9+
# Wait until hardhat responds
1010
while ! curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}' localhost:8545 2>/dev/null
1111
do
1212
sleep 1

config.nims

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# begin Nimble config (version 1)
2+
when fileExists("nimble.paths"):
3+
include "nimble.paths"
4+
# end Nimble config

nim.cfg

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# nim-web3
2+
# Copyright (c) 2019-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
110
# nim.cfg
211
@if nimHasWarningObservableStores:
312
warning[ObservableStores]: off

tests/all_tests.nim

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
# web3
2-
# Copyright (c) 2018-2022 Status Research & Development GmbH
3-
# Licensed and distributed under either of
4-
# * MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
5-
# * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
6-
# at your option. This file may not be copied, modified, or distributed except according to those terms.
1+
# nim-web3
2+
# Copyright (c) 2018-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
79

810
{. warning[UnusedImport]:off .}
911

1012
import
11-
test,
13+
test_primitives,
14+
test_contracts,
1215
test_deposit_contract,
1316
test_ethhexstrings,
1417
test_logs,
1518
test_json_marshalling,
16-
test_signed_tx
19+
test_signed_tx,
20+
test_execution_types
File renamed without changes.

tests/helpers/utils.nim

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import
2+
std/options,
3+
chronos, stint,
4+
stew/byteutils,
5+
../../web3,
6+
../../web3/primitives
7+
8+
proc deployContract*(web3: Web3, code: string, gasPrice = 0): Future[ReceiptObject] {.async.} =
9+
let provider = web3.provider
10+
let accounts = await provider.eth_accounts()
11+
12+
var code = code
13+
var tr: EthSend
14+
tr.`from` = web3.defaultAccount
15+
tr.data = hexToSeqByte(code)
16+
tr.gas = Quantity(3000000).some
17+
if gasPrice != 0:
18+
tr.gasPrice = some(gasPrice.Quantity)
19+
20+
let r = await web3.send(tr)
21+
return await web3.getMinedTransactionReceipt(r)
22+
23+
func ethToWei*(eth: UInt256): UInt256 =
24+
eth * 1000000000000000000.u256
25+
26+
type
27+
BlobData* = DynamicBytes[0, 512]
28+
29+
func conv*(T: type, x: int): T =
30+
type BaseType = distinctBase T
31+
var res: BaseType
32+
when BaseType is seq:
33+
res.setLen(1)
34+
res[^1] = x.byte
35+
T(res)
36+
37+
func address*(x: int): Address =
38+
conv(typeof result, x)
39+
40+
func txhash*(x: int): TxHash =
41+
conv(typeof result, x)
42+
43+
func blob*(x: int): BlobData =
44+
conv(typeof result, x)
45+
46+
func h256*(x: int): Hash256 =
47+
conv(typeof result, x)

tests/nim.cfg

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# nim-web3
2+
# Copyright (c) 2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
110
# Avoid some rare stack corruption while using exceptions with a SEH-enabled
211
# toolchain: https://github.com/status-im/nimbus-eth2/issues/3121
312
@if windows and not vcc:

tests/test.nim tests/test_contracts.nim

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import pkg/unittest2
2-
import ../web3
3-
import chronos, options, json, stint
4-
import test_utils
5-
1+
# nim-web3
2+
# Copyright (c) 2018-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
10+
import
11+
std/[options, json],
12+
pkg/unittest2,
13+
chronos, stint,
14+
../web3,
15+
./helpers/utils
616

717
contract(EncodingTest):
818
proc setBool(val: Bool)
@@ -80,6 +90,12 @@ contract(MetaCoin):
8090

8191
const MetaCoinCode = "608060405234801561001057600080fd5b5032600090815260208190526040902061271090556101c2806100346000396000f30060806040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166390b98a118114610050578063f8b2cb4f14610095575b600080fd5b34801561005c57600080fd5b5061008173ffffffffffffffffffffffffffffffffffffffff600435166024356100d5565b604080519115158252519081900360200190f35b3480156100a157600080fd5b506100c373ffffffffffffffffffffffffffffffffffffffff6004351661016e565b60408051918252519081900360200190f35b336000908152602081905260408120548211156100f457506000610168565b336000818152602081815260408083208054879003905573ffffffffffffffffffffffffffffffffffffffff871680845292819020805487019055805186815290519293927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a35060015b92915050565b73ffffffffffffffffffffffffffffffffffffffff16600090815260208190526040902054905600a165627a7a72305820000313ec0ebbff4ffefbe79d615d0ab019d8566100c40eb95a4eee617a87d1090029"
8292

93+
proc `$`(list: seq[Address]): string =
94+
result.add '['
95+
for x in list:
96+
result.add $x
97+
result.add ", "
98+
result.add ']'
8399

84100
suite "Contracts":
85101
setup:

tests/test_deposit_contract.nim

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import pkg/unittest2
2-
import ../web3
3-
import chronos, options, json, stint
4-
import test_utils
5-
import ./depositcontract
1+
# nim-web3
2+
# Copyright (c) 2018-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
10+
import
11+
std/[options, json],
12+
pkg/unittest2,
13+
chronos, stint,
14+
../web3,
15+
./helpers/utils,
16+
./helpers/depositcontract
617

718
contract(DepositContract):
819
proc deposit(pubkey: DynamicBytes[0, 48], withdrawalCredentials: DynamicBytes[0, 32], signature: DynamicBytes[0, 96], deposit_data_root: FixedBytes[32])

tests/test_ethhexstrings.nim

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
# nim-web3
2+
# Copyright (c) 2018-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
110
import
2-
unittest, json,
11+
unittest2, json,
312
../web3/ethhexstrings
413

514
suite "Hex quantity":

tests/test_execution_types.nim

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# nim-web3
2+
# Copyright (c) 2018-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
10+
import
11+
std/typetraits,
12+
pkg/unittest2,
13+
stew/byteutils,
14+
../web3/execution_types,
15+
./helpers/utils
16+
17+
suite "Execution types tests":
18+
let
19+
wd = WithdrawalV1(
20+
index: 1.Quantity,
21+
validatorIndex: 2.Quantity,
22+
address: address(3),
23+
amount: 4.Quantity,
24+
)
25+
26+
payload = ExecutionPayload(
27+
parentHash: h256(1),
28+
feeRecipient: address(2),
29+
stateRoot: h256(3),
30+
receiptsRoot: h256(4),
31+
logsBloom: FixedBytes[256].conv(5),
32+
prevRandao: h256(6),
33+
blockNumber: 7.Quantity,
34+
gasLimit: 8.Quantity,
35+
gasUsed: 9.Quantity,
36+
timestamp: 10.Quantity,
37+
extraData: DynamicBytes[0, 32].conv(11),
38+
baseFeePerGas: 12.u256,
39+
blockHash: h256(13),
40+
transactions: @[TypedTransaction.conv(14)],
41+
withdrawals: some(@[wd]),
42+
blobGasUsed: some(15.Quantity),
43+
excessBlobGas: some(16.Quantity),
44+
)
45+
46+
attr = PayloadAttributes(
47+
timestamp: 1.Quantity,
48+
prevRandao: h256(2),
49+
suggestedFeeRecipient: address(3),
50+
withdrawals: some(@[wd]),
51+
parentBeaconBlockRoot: some(h256(4)),
52+
)
53+
54+
blobs = BlobsBundleV1(
55+
commitments: @[KZGCommitment.conv(1)],
56+
proofs: @[KZGProof.conv(2)],
57+
blobs: @[Blob.conv(3)],
58+
)
59+
60+
response = GetPayloadResponse(
61+
executionPayload: payload,
62+
blockValue: some(1.u256),
63+
blobsBundle: some(blobs),
64+
shouldOverrideBuilder: some(false),
65+
)
66+
67+
test "payload version":
68+
var badv31 = payload
69+
badv31.excessBlobGas = none(Quantity)
70+
var badv32 = payload
71+
badv32.blobGasUsed = none(Quantity)
72+
var v2 = payload
73+
v2.excessBlobGas = none(Quantity)
74+
v2.blobGasUsed = none(Quantity)
75+
var v1 = v2
76+
v1.withdrawals = none(seq[WithdrawalV1])
77+
check badv31.version == Version.V2
78+
check badv32.version == Version.V2
79+
check v2.version == Version.V2
80+
check v1.version == Version.V1
81+
check payload.version == Version.V3
82+
83+
test "attr version":
84+
var v2 = attr
85+
v2.parentBeaconBlockRoot = none(Hash256)
86+
var v1 = v2
87+
v1.withdrawals = none(seq[WithdrawalV1])
88+
check attr.version == Version.V3
89+
check v2.version == Version.V2
90+
check v1.version == Version.V1
91+
92+
test "response version":
93+
var badv31 = response
94+
badv31.blobsBundle = none(BlobsBundleV1)
95+
var badv32 = response
96+
badv32.shouldOverrideBuilder = none(bool)
97+
var v2 = response
98+
v2.blobsBundle = none(BlobsBundleV1)
99+
v2.shouldOverrideBuilder = none(bool)
100+
var v1 = v2
101+
v1.blockValue = none(UInt256)
102+
check badv31.version == Version.V2
103+
check badv32.version == Version.V2
104+
check v2.version == Version.V2
105+
check v1.version == Version.V1
106+
check response.version == Version.V3
107+
108+
test "ExecutionPayload roundtrip":
109+
let v3 = payload.V3
110+
check v3 == v3.executionPayload.V3
111+
112+
let v2 = payload.V2
113+
check v2 == v2.executionPayload.V2
114+
115+
let v1 = payload.V1
116+
check v1 == v1.executionPayload.V1
117+
118+
test "PayloadAttributes roundtrip":
119+
let v3 = attr.V3
120+
check v3 == v3.payloadAttributes.V3
121+
122+
let v2 = attr.V2
123+
check v2 == v2.payloadAttributes.V2
124+
125+
let v1 = attr.V1
126+
check v1 == v1.payloadAttributes.V1
127+
128+
test "GetPayloadResponse roundtrip":
129+
let v3 = response.V3
130+
check v3 == v3.getPayloadResponse.V3
131+
132+
let v2 = response.V2
133+
check v2 == v2.getPayloadResponse.V2
134+
135+
let v1 = response.V1
136+
check v1 == v1.getPayloadResponse.V1
137+

tests/test_json_marshalling.nim

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
# nim-web3
2+
# Copyright (c) 2018-2023 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
5+
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
6+
# at your option.
7+
# This file may not be copied, modified, or distributed except according to
8+
# those terms.
9+
110
import
2-
std/typetraits,
3-
unittest, std/json, json_rpc/jsonmarshal, json_serialization,
11+
std/[typetraits, json],
412
stint,
5-
../web3/[conversions, ethtypes]
13+
unittest2,
14+
json_rpc/jsonmarshal, json_serialization,
15+
../web3/[conversions, eth_api_types]
616

717
proc `==`(x, y: Quantity): bool {.borrow, noSideEffect.}
818

0 commit comments

Comments
 (0)