Skip to content

Update to nim 2 x #103

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

Merged
merged 21 commits into from
Feb 14, 2025
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
nim: [1.6.20, stable]
nim: [2.0.14]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -39,7 +39,7 @@ jobs:
run: npm start &

- name: Build
run: nimble install -y
run: nimble install -y --maximumtaggedversions=2

- name: Test
run: nimble test -y
Expand Down
8 changes: 7 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ Use the [Nimble][2] package manager to add `ethers` to an existing
project. Add the following to its .nimble file:

```nim
requires "ethers >= 1.0.0 & < 2.0.0"
requires "ethers >= 1.1.0 & < 2.0.0"
```

To avoid conflicts with previous versions of `contractabi`, use the following command to install dependencies:

```bash
nimble install --maximumtaggedversions=2
```

Usage
Expand Down
8 changes: 7 additions & 1 deletion config.nims
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
--styleCheck:usages
--styleCheck:error
# Disable styleCheck temporarily because Nim 2.x is checking that
# getopt is used instead of
# https://github.com/status-im/nim-testutils/pull/54/commits/a1b07a11dd6a0c537a72e5ebf70df438c80f920a
#--styleCheck:error

# begin Nimble config (version 1)
when fileExists("nimble.paths"):
include "nimble.paths"
# end Nimble config

when (NimMajor, NimMinor) >= (2, 0):
--mm:refc
12 changes: 6 additions & 6 deletions ethers.nimble
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
version = "1.0.0"
version = "1.1.0"
author = "Nim Ethers Authors"
description = "library for interacting with Ethereum"
license = "MIT"

requires "nim >= 1.6.0"
requires "nim >= 2.0.14"
requires "chronicles >= 0.10.3 & < 0.11.0"
requires "chronos >= 4.0.0 & < 4.1.0"
requires "contractabi >= 0.6.0 & < 0.7.0"
requires "chronos >= 4.0.4 & < 4.1.0"
requires "contractabi >= 0.7.0 & < 0.8.0"
requires "questionable >= 0.10.2 & < 0.11.0"
requires "json_rpc >= 0.5.0 & < 0.6.0"
requires "serde >= 1.2.1 & < 1.3.0"
requires "stint >= 0.8.0 & < 0.9.0"
requires "stint >= 0.8.1 & < 0.9.0"
requires "stew >= 0.2.0 & < 0.3.0"
requires "https://github.com/codex-storage/nim-eth-versioned >= 1.0.0 & < 2.0.0"
requires "eth >= 0.5.0 & < 0.6.0"

task test, "Run the test suite":
# exec "nimble install -d -y"
Expand Down
2 changes: 1 addition & 1 deletion ethers/providers/jsonrpc.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std/tables
import std/uri
import pkg/chronicles
import pkg/eth/common/eth_types_json_serialization
import pkg/eth/common/eth_types except Block, Log, Address, Transaction
import pkg/json_rpc/rpcclient
import pkg/json_rpc/errors
import pkg/serde
Expand Down
6 changes: 3 additions & 3 deletions ethers/signers/wallet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Wallet* = ref object of Signer

proc new*(_: type Wallet, privateKey: PrivateKey): Wallet =
let publicKey = privateKey.toPublicKey()
let address = Address.init(publicKey.toCanonicalAddress())
let address = Address(publicKey.toCanonicalAddress())
Wallet(privateKey: privateKey, publicKey: publicKey, address: address)

proc new*(_: type Wallet, privateKey: PrivateKey, provider: Provider): Wallet =
Expand All @@ -53,13 +53,13 @@ proc createRandom*(_: type Wallet): Wallet =
result = Wallet()
result.privateKey = PrivateKey.random(getRng()[])
result.publicKey = result.privateKey.toPublicKey()
result.address = Address.init(result.publicKey.toCanonicalAddress())
result.address = Address(result.publicKey.toCanonicalAddress())

proc createRandom*(_: type Wallet, provider: Provider): Wallet =
result = Wallet()
result.privateKey = PrivateKey.random(getRng()[])
result.publicKey = result.privateKey.toPublicKey()
result.address = Address.init(result.publicKey.toCanonicalAddress())
result.address = Address(result.publicKey.toCanonicalAddress())
result.provider = some provider

method provider*(wallet: Wallet): Provider {.gcsafe, raises: [SignerError].} =
Expand Down
14 changes: 8 additions & 6 deletions ethers/signers/wallet/signing.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ../../basics
import ../../transaction as ethers
import ../../provider
import ./error
from pkg/eth/common/eth_types import EthAddress

type
Transaction = ethers.Transaction
Expand All @@ -26,15 +27,16 @@ func toSignableTransaction(transaction: Transaction): SignableTransaction =
signable.nonce = nonce.truncate(uint64)
signable.chainId = ChainId(chainId.truncate(uint64))
signable.gasLimit = GasInt(gasLimit.truncate(uint64))
signable.to = some EthAddress(transaction.to)

signable.to = Opt.some(EthAddress(transaction.to))
signable.value = transaction.value
signable.payload = transaction.data

if maxFee =? transaction.maxFee and
maxPriorityFee =? transaction.maxPriorityFee:
signable.txType = TxEip1559
signable.maxFee = GasInt(maxFee.truncate(uint64))
signable.maxPriorityFee = GasInt(maxPriorityFee.truncate(uint64))
signable.maxFeePerGas = GasInt(maxFee.truncate(uint64))
signable.maxPriorityFeePerGas = GasInt(maxPriorityFee.truncate(uint64))
elif gasPrice =? transaction.gasPrice:
signable.txType = TxLegacy
signable.gasPrice = GasInt(gasPrice.truncate(uint64))
Expand All @@ -48,17 +50,17 @@ func sign(key: PrivateKey, transaction: SignableTransaction): seq[byte] =

# Temporary V value, used to signal to the hashing function
# that we'd like to use an EIP-155 signature
transaction.V = int64(uint64(transaction.chainId)) * 2 + 35
transaction.V = uint64(transaction.chainId) * 2 + 35

let hash = transaction.txHashNoSignature().data
let signature = key.sign(SkMessage(hash)).toRaw()

transaction.R = UInt256.fromBytesBE(signature[0..<32])
transaction.S = UInt256.fromBytesBE(signature[32..<64])
transaction.V = int64(signature[64])
transaction.V = uint64(signature[64])

if transaction.txType == TxLegacy:
transaction.V += int64(uint64(transaction.chainId)) * 2 + 35
transaction.V += uint64(transaction.chainId) * 2 + 35

rlp.encode(transaction)

Expand Down