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

v0.2.0: Revamp data types #109

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
nimcache/
vendor/

# Executables shall be put in an ignored build/ directory
# Ignore dynamic, static libs and libtool archive files
Expand All @@ -9,6 +10,7 @@ build/
*.la
*.exe
*.dll
nimble.paths

node_modules
nohup.out
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![License: Apache](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
![Github action](https://github.com/status-im/nim-web3/workflows/nim-web3%20CI/badge.svg)
![Github action](https://github.com/status-im/nim-web3/workflows/CI/badge.svg)

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

Expand Down
2 changes: 1 addition & 1 deletion ci-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ touch hardhat.config.js
nohup npx hardhat node &
nimble install -y --depsOnly

# Wait until ganache responds
# Wait until hardhat responds
while ! curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}' localhost:8545 2>/dev/null
do
sleep 1
Expand Down
4 changes: 4 additions & 0 deletions config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# begin Nimble config (version 1)
when fileExists("nimble.paths"):
include "nimble.paths"
# end Nimble config
9 changes: 9 additions & 0 deletions nim.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# nim-web3
# Copyright (c) 2019-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

# nim.cfg
@if nimHasWarningObservableStores:
warning[ObservableStores]: off
Expand Down
20 changes: 12 additions & 8 deletions tests/all_tests.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# web3
# Copyright (c) 2018-2022 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
# * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) 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.
# nim-web3
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

{. warning[UnusedImport]:off .}

import
test,
test_primitives,
test_contracts,
test_deposit_contract,
test_ethhexstrings,
test_logs,
test_json_marshalling,
test_signed_tx
test_signed_tx,
test_execution_types
File renamed without changes.
47 changes: 47 additions & 0 deletions tests/helpers/utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import
std/options,
chronos, stint,
stew/byteutils,
../../web3,
../../web3/primitives

proc deployContract*(web3: Web3, code: string, gasPrice = 0): Future[ReceiptObject] {.async.} =
let provider = web3.provider
let accounts = await provider.eth_accounts()

var code = code
var tr: EthSend
tr.`from` = web3.defaultAccount
tr.data = hexToSeqByte(code)
tr.gas = Quantity(3000000).some
if gasPrice != 0:
tr.gasPrice = some(gasPrice.Quantity)

let r = await web3.send(tr)
return await web3.getMinedTransactionReceipt(r)

func ethToWei*(eth: UInt256): UInt256 =
eth * 1000000000000000000.u256

type
BlobData* = DynamicBytes[0, 512]

func conv*(T: type, x: int): T =
type BaseType = distinctBase T
var res: BaseType
when BaseType is seq:
res.setLen(1)
res[^1] = x.byte
T(res)

func address*(x: int): Address =
conv(typeof result, x)

func txhash*(x: int): TxHash =
conv(typeof result, x)

func blob*(x: int): BlobData =
conv(typeof result, x)

func h256*(x: int): Hash256 =
conv(typeof result, x)
9 changes: 9 additions & 0 deletions tests/nim.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# nim-web3
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

# Avoid some rare stack corruption while using exceptions with a SEH-enabled
# toolchain: https://github.com/status-im/nimbus-eth2/issues/3121
@if windows and not vcc:
Expand Down
26 changes: 21 additions & 5 deletions tests/test.nim → tests/test_contracts.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import pkg/unittest2
import ../web3
import chronos, options, json, stint
import test_utils

# nim-web3
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
std/[options, json],
pkg/unittest2,
chronos, stint,
../web3,
./helpers/utils

contract(EncodingTest):
proc setBool(val: Bool)
Expand Down Expand Up @@ -80,6 +90,12 @@ contract(MetaCoin):

const MetaCoinCode = "608060405234801561001057600080fd5b5032600090815260208190526040902061271090556101c2806100346000396000f30060806040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166390b98a118114610050578063f8b2cb4f14610095575b600080fd5b34801561005c57600080fd5b5061008173ffffffffffffffffffffffffffffffffffffffff600435166024356100d5565b604080519115158252519081900360200190f35b3480156100a157600080fd5b506100c373ffffffffffffffffffffffffffffffffffffffff6004351661016e565b60408051918252519081900360200190f35b336000908152602081905260408120548211156100f457506000610168565b336000818152602081815260408083208054879003905573ffffffffffffffffffffffffffffffffffffffff871680845292819020805487019055805186815290519293927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a35060015b92915050565b73ffffffffffffffffffffffffffffffffffffffff16600090815260208190526040902054905600a165627a7a72305820000313ec0ebbff4ffefbe79d615d0ab019d8566100c40eb95a4eee617a87d1090029"

proc `$`(list: seq[Address]): string =
result.add '['
for x in list:
result.add $x
result.add ", "
result.add ']'

suite "Contracts":
setup:
Expand Down
21 changes: 16 additions & 5 deletions tests/test_deposit_contract.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import pkg/unittest2
import ../web3
import chronos, options, json, stint
import test_utils
import ./depositcontract
# nim-web3
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
std/[options, json],
pkg/unittest2,
chronos, stint,
../web3,
./helpers/utils,
./helpers/depositcontract

contract(DepositContract):
proc deposit(pubkey: DynamicBytes[0, 48], withdrawalCredentials: DynamicBytes[0, 32], signature: DynamicBytes[0, 96], deposit_data_root: FixedBytes[32])
Expand Down
11 changes: 10 additions & 1 deletion tests/test_ethhexstrings.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# nim-web3
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
unittest, json,
unittest2, json,
../web3/ethhexstrings

suite "Hex quantity":
Expand Down
137 changes: 137 additions & 0 deletions tests/test_execution_types.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# nim-web3
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
std/typetraits,
pkg/unittest2,
stew/byteutils,
../web3/execution_types,
./helpers/utils

suite "Execution types tests":
let
wd = WithdrawalV1(
index: 1.Quantity,
validatorIndex: 2.Quantity,
address: address(3),
amount: 4.Quantity,
)

payload = ExecutionPayload(
parentHash: h256(1),
feeRecipient: address(2),
stateRoot: h256(3),
receiptsRoot: h256(4),
logsBloom: FixedBytes[256].conv(5),
prevRandao: h256(6),
blockNumber: 7.Quantity,
gasLimit: 8.Quantity,
gasUsed: 9.Quantity,
timestamp: 10.Quantity,
extraData: DynamicBytes[0, 32].conv(11),
baseFeePerGas: 12.u256,
blockHash: h256(13),
transactions: @[TypedTransaction.conv(14)],
withdrawals: some(@[wd]),
blobGasUsed: some(15.Quantity),
excessBlobGas: some(16.Quantity),
)

attr = PayloadAttributes(
timestamp: 1.Quantity,
prevRandao: h256(2),
suggestedFeeRecipient: address(3),
withdrawals: some(@[wd]),
parentBeaconBlockRoot: some(h256(4)),
)

blobs = BlobsBundleV1(
commitments: @[KZGCommitment.conv(1)],
proofs: @[KZGProof.conv(2)],
blobs: @[Blob.conv(3)],
)

response = GetPayloadResponse(
executionPayload: payload,
blockValue: some(1.u256),
blobsBundle: some(blobs),
shouldOverrideBuilder: some(false),
)

test "payload version":
var badv31 = payload
badv31.excessBlobGas = none(Quantity)
var badv32 = payload
badv32.blobGasUsed = none(Quantity)
var v2 = payload
v2.excessBlobGas = none(Quantity)
v2.blobGasUsed = none(Quantity)
var v1 = v2
v1.withdrawals = none(seq[WithdrawalV1])
check badv31.version == Version.V2
check badv32.version == Version.V2
check v2.version == Version.V2
check v1.version == Version.V1
check payload.version == Version.V3

test "attr version":
var v2 = attr
v2.parentBeaconBlockRoot = none(Hash256)
var v1 = v2
v1.withdrawals = none(seq[WithdrawalV1])
check attr.version == Version.V3
check v2.version == Version.V2
check v1.version == Version.V1

test "response version":
var badv31 = response
badv31.blobsBundle = none(BlobsBundleV1)
var badv32 = response
badv32.shouldOverrideBuilder = none(bool)
var v2 = response
v2.blobsBundle = none(BlobsBundleV1)
v2.shouldOverrideBuilder = none(bool)
var v1 = v2
v1.blockValue = none(UInt256)
check badv31.version == Version.V2
check badv32.version == Version.V2
check v2.version == Version.V2
check v1.version == Version.V1
check response.version == Version.V3

test "ExecutionPayload roundtrip":
let v3 = payload.V3
check v3 == v3.executionPayload.V3

let v2 = payload.V2
check v2 == v2.executionPayload.V2

let v1 = payload.V1
check v1 == v1.executionPayload.V1

test "PayloadAttributes roundtrip":
let v3 = attr.V3
check v3 == v3.payloadAttributes.V3

let v2 = attr.V2
check v2 == v2.payloadAttributes.V2

let v1 = attr.V1
check v1 == v1.payloadAttributes.V1

test "GetPayloadResponse roundtrip":
let v3 = response.V3
check v3 == v3.getPayloadResponse.V3

let v2 = response.V2
check v2 == v2.getPayloadResponse.V2

let v1 = response.V1
check v1 == v1.getPayloadResponse.V1

16 changes: 13 additions & 3 deletions tests/test_json_marshalling.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# nim-web3
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
std/typetraits,
unittest, std/json, json_rpc/jsonmarshal, json_serialization,
std/[typetraits, json],
stint,
../web3/[conversions, ethtypes]
unittest2,
json_rpc/jsonmarshal, json_serialization,
../web3/[conversions, eth_api_types]

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

Expand Down
Loading