codex-storage / nim-ethers

Port of ethers.js to Nim
Other
8 stars 4 forks source link

Support custom errors #69

Closed markspanbroek closed 6 months ago

markspanbroek commented 8 months ago

Adds support for Solidity custom errors.

They can be defined in Nim as follows:

type
  InsufficientBalance = object of SolidityError
    arguments: tuple[available: UInt256, required: UInt256]

And they can be declared on contract functions like this:

proc transfer*(token: Erc20Token, recipient: Address, amount: UInt256)
  {.contract, errors:[InsufficientBalance].}

Which allows you to write error handling code like this:

try:
  await token.transfer(recipient, 100.u256)
except InsufficientBalance as error:
  echo "insufficient balance"
  echo "available balance: ", error.arguments.available
  echo "required balance: ", error.arguments.required

Depends on #70

markspanbroek commented 6 months ago

With the latest changes, this PR has become a breaking change. Confirmable is no longer a type alias for ?TransactionResponse, but its own object type. This means that the return type ?TransactionResponse on a {.contract.} proc is no longer supported. You need to use Confirmable as the return type instead.

emizzle commented 6 months ago

Looks great, Mark! Really well done moving the callback onto Confirmable 👍