Skip to content

Commit 238ce54

Browse files
feat: Hardhat Viem usage example. (#2556)
* feat: Hardhat Viem usage example. Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: Hardhat Viem usage example - remove keys of unknown origin. Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: Hardhat Viem usage example - remove keys of unknown origin. Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: Hardhat Viem usage example - remove keys of unknown origin. Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: Accept Readme change suggestion. Co-authored-by: Luis Mastrangelo <[email protected]> Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: Fix paths in Readme. Signed-off-by: Mariusz Jasuwienas <[email protected]> * feat: Review requests Signed-off-by: Mariusz Jasuwienas <[email protected]> --------- Signed-off-by: Mariusz Jasuwienas <[email protected]> Co-authored-by: Luis Mastrangelo <[email protected]>
1 parent 1303c76 commit 238ce54

File tree

12 files changed

+9449
-0
lines changed

12 files changed

+9449
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# For Hedera Testnet
2+
# The operator private keys start with "0x" and are in hexadecimal format
3+
# Your testnet account ECDSA hex-encoded private key (Ex: 0xb46751179bc8aa9e129d34463e46cd...)
4+
TESTNET_OPERATOR_PRIVATE_KEY='TESTNET_OPERATOR_PRIVATE_KEY'
5+
6+
# For Hedera Local Node
7+
# The operator private keys start with "0x" and are in hexadecimal format
8+
# Your Hedera Local Node ECDSA account alias private key (Ex: 0x105d050185ccb907fba04dd92d...)
9+
LOCAL_NODE_OPERATOR_PRIVATE_KEY='LOCAL_NODE_OPERATOR_PRIVATE_KEY'
10+
# Your Hedera Local Node JSON-RPC endpoint URL (ex: 'http://localhost:7546/')
11+
LOCAL_NODE_ENDPOINT='http://localhost:7546/'
12+
13+
# For Hedera Previewnet
14+
# Your previewnet account ECDSA hex-encoded private key (Ex: 0x46751179bc8aa9e129d34463e...)
15+
PREVIEWNET_OPERATOR_PRIVATE_KEY="YOUR_PREVIEWNET_OPERATOR_PRIVATE_KEY"
16+
17+
# For Hedera Mainnet
18+
# Your mainnet account ECDSA hex-encoded private key (Ex: 0x46751179bc8aa9e129d34463e...)
19+
MAINNET_OPERATOR_PRIVATE_KEY="YOUR_MAINNET_OPERATOR_PRIVATE_KEY"
20+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
.env
3+
coverage
4+
coverage.json
5+
typechain
6+
typechain-types
7+
8+
# Hardhat files
9+
cache
10+
artifacts
11+
12+
.vscode
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Hedera Hardhat Viem Example Project
2+
3+
This Hedera Hardhat Viem Example Project offers boilerplate code for testing and deploying smart contracts via Hardhat with Viem. It includes configuration for both community-hosted and local ([Hedera Local Node](https://github.com/hashgraph/hedera-local-node)) instances of the [Hedera JSON RPC Relay](https://github.com/hashgraph/hedera-json-rpc-relay).
4+
5+
:fire: Check out the step-by-step tutorial [here](https://docs.hedera.com/hedera/tutorials/smart-contracts/deploy-a-smart-contract-using-hardhat-and-hedera-json-rpc-relays).
6+
7+
# Viem vs Ethers.js Comparison
8+
9+
This comparison aims to assist developers in selecting the most suitable library for their specific project requirements. For further information about Viem, please visit [https://viem.sh/docs/introduction](https://viem.sh/docs/introduction).
10+
11+
| Feature | Viem | Ethers.js |
12+
|-----------------------|----------------------------------------------------------------------|-----------------------------------------|
13+
| Performance | [Optimized for speed](https://viem.sh/docs/introduction#performance) | Reliable performance |
14+
| API Design | Intuitive, developer-friendly | Well-designed, widely used |
15+
| Documentation | Comprehensive and clear | Good, but may vary by feature |
16+
| Debugging | Advanced debugging tools | Standard debugging capabilities |
17+
| Ecosystem Integration | Seamless integration with certain tools | Broad integration with many tools |
18+
19+
Viem already includes out-of-the-box configuration for Hedera public networks: [testnet, mainnet, and previewnet](https://github.com/wevm/viem/blob/cc105f801ec69640d3d806d86b35e36002d8c912/src/chains/index.ts#L113-L115).
20+
21+
## Project Files and Folders
22+
23+
- `hardhat.config.js` - This is the configuration file for your Hardhat project development environment. It centralizes and defines various settings like Hedera networks, Solidity compiler versions, plugins, and tasks.
24+
25+
- `/contracts` - This folder holds all the Solidity smart contract files that make up the core logic of your dApp. Contracts are written in `.sol` files.
26+
27+
- `/test` - This folder contains test scripts that help validate your smart contracts' functionality. These tests are crucial for ensuring that your contracts behave as expected.
28+
29+
- `/scripts` - This folder contains essential JavaScript files for tasks such as deploying smart contracts to the Hedera network.
30+
31+
- `.env.example` - This file is contains the environment variables needed by the project. Copy this file to a `.env` file and fill in the actual values before starting the development server or deploying smart contracts. To expedite your test setup and deployment, some variables are pre-filled in this example file.
32+
33+
## Setup
34+
35+
1. Clone this repo to your local machine:
36+
37+
```shell
38+
git clone https://github.com/hashgraph/hedera-json-rpc-relay.git
39+
```
40+
41+
2. Once you've cloned the repository, open your IDE terminal and navigate to the root directory of the project:
42+
43+
```shell
44+
cd hedera-json-rpc-relay/tools/hardhat-viem-example
45+
```
46+
47+
3. Run the following command to install all the necessary dependencies:
48+
49+
```shell
50+
npm install
51+
```
52+
53+
4. Get your Hedera testnet account hex encoded private key from the [Hedera Developer Portal](https://portal.hedera.com/register) and update the `.env.example` `TESTNET_OPERATOR_PRIVATE_KEY`
54+
55+
5. Copy `.env.example` to `.env`
56+
57+
6. Run the test script from the root directory of the project. The default network is set to "local."
58+
59+
```shell
60+
# runs test on default network
61+
npx hardhat test
62+
63+
# runs test on testnet
64+
npx hardhat test --network testnet
65+
```
66+
67+
Expect an output similar to the following:
68+
```shell
69+
RPC
70+
The address 0xe0b73F64b0de6032b193648c08899f20b5A6141D has 10000000000000000000000 weibars
71+
✔ should be able to get the account balance (1678ms)
72+
Greeter deployed to: 0xD9d0c5C0Ff85758BdF05A7636F8036d4D065F5B6
73+
✔ should be able to deploy a contract (11456ms)
74+
Contract call result: initial_msg
75+
✔ should be able to make a contract view call (1249ms)
76+
Updated call result: updated_msg
77+
Contract call result: updated_msg
78+
✔ should be able to make a contract call (6806ms)
79+
80+
81+
4 passing (22s)
82+
```
83+
84+
7. Run the following command to deploy the smart contract.
85+
```shell
86+
# deploys to the default network
87+
npx hardhat deploy-contract
88+
89+
# deploys to testnet
90+
npx hardhat deploy-contract --network testnet
91+
```
92+
93+
# Contributing
94+
Contributions are welcome. Please see the
95+
[contributing guide](https://github.com/hashgraph/.github/blob/main/CONTRIBUTING.md)
96+
to see how you can get involved.
97+
98+
# Code of Conduct
99+
This project is governed by the
100+
[Contributor Covenant Code of Conduct](https://github.com/hashgraph/.github/blob/main/CODE_OF_CONDUCT.md). By
101+
participating, you are expected to uphold this code of conduct. Please report unacceptable behavior
102+
103+
104+
# License
105+
[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.9;
3+
4+
contract Greeter {
5+
string private greeting;
6+
7+
event GreetingSet(string greeting);
8+
9+
//This constructor assigns the initial greeting and emit GreetingSet event
10+
constructor(string memory _greeting) {
11+
greeting = _greeting;
12+
13+
emit GreetingSet(_greeting);
14+
}
15+
16+
//This function returns the current value stored in greeting variable
17+
function greet() public view returns (string memory) {
18+
return greeting;
19+
}
20+
21+
//This function sets the new greeting msg from the one passed down as parameter and emit event
22+
function setGreeting(string memory _greeting) public {
23+
greeting = _greeting;
24+
25+
emit GreetingSet(_greeting);
26+
}
27+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require("@nomicfoundation/hardhat-toolbox-viem");
2+
require("@nomicfoundation/hardhat-chai-matchers");
3+
const { task } = require("hardhat/config");
4+
5+
// Import dotenv module to access variables stored in the .env file
6+
require("dotenv").config();
7+
8+
// Define Hardhat tasks here, which can be accessed in our test file (test/rpc.js) by using hre.run('taskName')
9+
task("show-balance", async () => {
10+
const showBalance = require("./scripts/showBalance");
11+
return showBalance();
12+
});
13+
14+
task("deploy-contract", async () => {
15+
const deployContract = require("./scripts/deployContract");
16+
return deployContract();
17+
});
18+
19+
task("contract-view-call", async (taskArgs) => {
20+
const contractViewCall = require("./scripts/contractViewCall");
21+
return contractViewCall(taskArgs.contractAddress);
22+
});
23+
24+
task("contract-call", async (taskArgs) => {
25+
const contractCall = require("./scripts/contractCall");
26+
return contractCall(taskArgs.contractAddress, taskArgs.msg);
27+
});
28+
29+
/** @type import('hardhat/config').HardhatUserConfig */
30+
module.exports = {
31+
mocha: {
32+
timeout: 3600000
33+
},
34+
solidity: {
35+
version: "0.8.9",
36+
settings: {
37+
optimizer: {
38+
enabled: true,
39+
runs: 500
40+
}
41+
}
42+
},
43+
// This specifies network configurations used when running Hardhat tasks
44+
defaultNetwork: "local",
45+
networks: {
46+
local: {
47+
// Your Hedera Local Node address pulled from the .env file
48+
url: process.env.LOCAL_NODE_ENDPOINT,
49+
// Conditionally assign accounts when private key value is present
50+
accounts: process.env.LOCAL_NODE_OPERATOR_PRIVATE_KEY ? [process.env.LOCAL_NODE_OPERATOR_PRIVATE_KEY] : []
51+
},
52+
testnet: {
53+
// HashIO testnet endpoint
54+
url: 'https://testnet.hashio.io/api',
55+
// Conditionally assign accounts when private key value is present
56+
accounts: process.env.TESTNET_OPERATOR_PRIVATE_KEY ? [process.env.TESTNET_OPERATOR_PRIVATE_KEY] : []
57+
},
58+
59+
/**
60+
* Uncomment the following to add a mainnet network configuration
61+
*/
62+
mainnet: {
63+
// HashIO mainnet endpoint
64+
url: 'https://mainnet.hashio.io/api',
65+
// Conditionally assign accounts when private key value is present
66+
accounts: process.env.MAINNET_OPERATOR_PRIVATE_KEY ? [process.env.MAINNET_OPERATOR_PRIVATE_KEY] : []
67+
},
68+
69+
/**
70+
* Uncomment the following to add a previewnet network configuration
71+
*/
72+
previewnet: {
73+
// HashIO previewnet endpoint
74+
url:'https://previewnet.hashio.io/api',
75+
// Conditionally assign accounts when private key value is present
76+
accounts: process.env.PREVIEWNET_OPERATOR_PRIVATE_KEY ? [process.env.PREVIEWNET_OPERATOR_PRIVATE_KEY] : []
77+
}
78+
}
79+
};

0 commit comments

Comments
 (0)