Port of ethers.js to Nim
Go to file
Eric 43500c63d7
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions

json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)

* Add ethers implementation for setMethodHandler

Was removed in json-rpc

* More json conversion updates

* Fix json_rpc.call returning JsonString instead of JsonNode

* Update exceptions

Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures

* Start fixing tests (mainly conversion fixes)

* rename sender to `from`, update json error logging, add more conversions

* Refactor exceptions for providers and signers, fix more tests

- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others

* remove raises from async annotation for dynamic dispatch

- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method

* Catch EstimateGasError before ProviderError

EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught

* clean up - all tests passing

* support nim 2.0

* lock in chronos version

* Add serde options to the json util, along with tests

next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib

* bump json-rpc to 0.4.0 and fix test

* fix: specify raises for getAddress and sendTransaction

Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.

* handle exceptions during jsonrpc init

There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution

* cleanup

* deserialize key defaults to serialize key

* Add more tests for OptIn/OptOut/Strict modes, fix logic

* use nim-serde instead of json util

Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`

* Move hash* shim to its own module

* address PR feedback

- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment

* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
.github/workflows Test with Nim 1.6.16 2023-12-12 09:17:52 +01:00
ethers Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64) 2024-02-19 16:50:46 +11:00
testmodule Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64) 2024-02-19 16:50:46 +11:00
testnode prevent stuck transactions by async locking nonce sequencing (+ estimate gas) (#55) 2023-10-25 10:42:25 +11:00
.editorconfig Project setup 2022-01-17 17:04:14 +01:00
.gitignore feat: erc20 module (#38) 2023-03-29 13:41:44 +02:00
License.md Project setup 2022-01-17 17:04:14 +01:00
Readme.md version 0.7.1 2023-12-12 09:28:52 +01:00
config.nims enables stylecheck (#36) 2023-03-09 10:58:54 +01:00
ethers.nim Include wallet in library 2022-08-08 12:40:36 +02:00
ethers.nimble Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64) 2024-02-19 16:50:46 +11:00
nim.cfg Fix: use websock instead of news 2022-06-30 09:35:30 +02:00

Readme.md

Nim Ethers

A port of the ethers.js library to Nim. Allows you to connect to an Ethereum node.

This is very much a work in progress; expect to see many things that are incomplete or wrong. Use at your own risk.

Installation

Use the Nimble package manager to add ethers to an existing project. Add the following to its .nimble file:

requires "ethers >= 0.7.1 & < 0.8.0"

Usage

To connect to an Ethereum node, you require a Provider. Currently, only a JSON-RPC provider is supported:

import ethers
import chronos

let provider = JsonRpcProvider.new("ws://localhost:8545")
let accounts = await provider.listAccounts()

To interact with a smart contract, you need to define the contract functions in Nim. For example, to interact with an ERC20 token, you could define the following:

type Erc20 = ref object of Contract

proc totalSupply(token: Erc20): UInt256 {.contract, view.}
proc balanceOf(token: Erc20, account: Address): UInt256 {.contract, view.}
proc transfer(token: Erc20, recipient: Address, amount: UInt256) {.contract.}
proc allowance(token: Erc20, owner, spender: Address): UInt256 {.contract, view.}
proc approve(token: Erc20, spender: Address, amount: UInt256) {.contract.}
proc transferFrom(token: Erc20, sender, recipient: Address, amount: UInt256) {.contract.}

Notice how some functions are annotated with a {.view.} pragma. This indicates that the function does not modify the blockchain. See also the Solidity documentation on state mutability

Now that you've defined the contract interface, you can create an instance of it using its deployed address:

let address = Address.init("0x.....")
let token = Erc20.new(address, provider)

The functions that you defined earlier can now be called asynchronously:

let supply = await token.totalSupply()
let balance = await token.balanceOf(accounts[0])

These invocations do not yet change the state of the blockchain, even when we invoke those functions that lack a {.view.} pragma. To allow these changes to happen, we require an instance of a Signer first.

For example, to use the 4th account on the Ethereum node to sign transactions, you'd instantiate the signer as follows:

let signer = provider.getSigner(accounts[3])

And then connect the contract and signer:

let writableToken = token.connect(signer)

This allows you to make changes to the state of the blockchain:

await writableToken.transfer(accounts[7], 42.u256)

Which transfers 42 tokens from account 3 to account 7

And lastly, don't forget to close the provider when you're done:

await provider.close()

Events

You can subscribe to events that are emitted by a smart contract. For instance, to get notified about token transfers you define the Transfer event:

type Transfer = object of Event
  sender {.indexed.}: Address
  receiver {.indexed.}: Address
  value: UInt256

Notice that Transfer inherits from Event, and that some event parameters are marked with {.indexed.} to match the definition in Solidity.

Note that valid types of indexed parameters are:

uint8 | uint16 | uint32 | uint64 | UInt256 | UInt128 |
int8 | int16 | int32 | int64 | Int256 | Int128 |
bool | Address | array[ 1..32, byte]

Distinct types of valid types are also supported for indexed fields, eg:

type
  DistinctAlias = distinct array[32, byte]
  MyEvent = object of Event
    a {.indexed.}: DistinctAlias
    b: DistinctAlias # also allowed for non-indexed fields

You can now subscribe to Transfer events by calling subscribe on the contract instance.

proc handleTransfer(transfer: Transfer) =
  echo "received transfer: ", transfer

let subscription = await token.subscribe(Transfer, handleTransfer)

When a Transfer event is emitted, the handleTransfer proc that you just defined will be called.

When you're no longer interested in these events, you can unsubscribe:

await subscription.unsubscribe()

Utilities

This library ships with some optional modules that provides convenience utilities for you such as:

  • ethers/erc20 module provides you with ERC20 token implementation and its events

Contribution

If you want to run the tests, then before running nimble test, you have to have installed NodeJS and started a testing node:

$ cd testnode
$ npm ci
$ npm start

Thanks

This library is inspired by the great work done by the ethers.js (no affiliation) and nim-web3 developers.