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

test: waffle tool example #2640

Merged
merged 4 commits into from
Jul 11, 2024
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
5 changes: 5 additions & 0 deletions tools/waffle-example/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The operator private keys start with "0x" and are in hexadecimal format
# Your account ECDSA hex-encoded private key (Ex: 0xb46751179bc8aa9e129d34463e46cd...)
# All available local node private keys can be found here https://github.com/hashgraph/hedera-local-node#commands
OPERATOR_PRIVATE_KEY=0x105d050185ccb907fba04dd92d8de9e32c18305e097ab41dadda21489a211524
RELAY_ENDPOINT='http://localhost:7546'
2 changes: 2 additions & 0 deletions tools/waffle-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cache
build
3 changes: 3 additions & 0 deletions tools/waffle-example/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"spec": "test/**/*.test.mjs"
}
64 changes: 64 additions & 0 deletions tools/waffle-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Waffle example

Simple scripts for testing matchers provided by Waffle tool.

## Configuration

Create `.env` file based on `.env.example`

```
OPERATOR_PRIVATE_KEY=
RELAY_ENDPOINT=
```

- `OPERATOR_PRIVATE_KEY` is your account ECDSA hex-encoded private key.
- `RELAY_ENDPOINT` is a path to your JSON RPC Api. `https://testnet.hashio.io/api` for testnet. Remember to start your Hedera local node if you want to use the http://localhost:7546 endpoint.

## Setup & Install

In the project directory:
1. Run `npm install`
2. Run `npm run build`
2. Run `npm run test`

# Waffle
[Waffle](https://ethereum-waffle.readthedocs.io/) is a library designed for testing Smart Contracts.

#### Features

Waffle provides several features that enhance the testing experience for developers:

- Includes new Chai matchers specifically designed for Smart Contract testing.
- Enables the use of mocked wallets and Smart Contracts for isolated and controlled testing environments.
- Allows the use of fixtures to define reusable setups for tests, reducing boilerplate code and simplifying complex test setups.

#### Dependencies

Waffle uses **ethers.js** to perform operations such as deploying Smart Contracts and sending transactions. It leverages **Mocha** and **Chai** as the foundational testing tools, while adding its own custom matchers tailored for Smart Contracts.

## Installation:
To install Waffle, you need to add the `ethereum-waffle` node module to your project. You can do this using the following command:

```shell
npm install --save-dev ethereum-waffle
```

## Hedera Smart Contracts Development

Waffle utilizes `ethers.js` to communicate with the JSON-RPC API.

A sample usage is documented in this [example](https://github.com/hashgraph/hedera-json-rpc-relay/tree/main/tools/hardhat-example).

Both [contract deployment](https://github.com/hashgraph/hedera-json-rpc-relay/blob/main/tools/hardhat-example/scripts/deployContract.js) and [calls](https://github.com/hashgraph/hedera-json-rpc-relay/blob/main/tools/hardhat-example/scripts/contractCall.js) are performed using `ethers.js` in the same way Waffle does.

The [Chai matchers](https://ethereum-waffle.readthedocs.io/en/latest/matchers.html) provided by Waffle are not dependent on any specific network. They can be used to easily detect common scenarios in Smart Contract testing, such as whether an event was emitted by the Smart Contract or if an operation was reverted.

There are also matchers for Chai that test raw strings such as addresses and keys. These matchers assume that only ECDSA keys are correct.

However, the mocks provided by Waffle are designed to simulate Ethereum network behavior, which means they may not replicate some Hedera-specific traits, such as `ecrecover` behavior for non-ECDSA keys or after private key changes. The wallet mock will only support wallets utilizing ECDSA keys.
se7enarianelabs marked this conversation as resolved.
Show resolved Hide resolved

The mock Waffle provider allows users to imitate basic ENS operations, such as registering domains and retrieving the address of a domain. The Hedera Name Service (HNS) at its core level provides similar functionalities, so this mock can be used as its replacement.

### Known issues

Waffle fixtures are not supported, more information: https://github.com/hashgraph/hedera-smart-contracts/issues/702
14 changes: 14 additions & 0 deletions tools/waffle-example/contracts/SampleContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract SampleContract {
event Notification(string message);

function emitEvent() public {
emit Notification("Hello world!");
}

function revertableFunction() pure public {
assert(false);
}
}
Loading
Loading