forked from hashgraph/hedera-json-rpc-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Hardhat Viem usage example. (hashgraph#2556)
* feat: Hardhat Viem usage example. Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> * feat: Hardhat Viem usage example - remove keys of unknown origin. Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> * feat: Hardhat Viem usage example - remove keys of unknown origin. Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> * feat: Hardhat Viem usage example - remove keys of unknown origin. Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> * feat: Accept Readme change suggestion. Co-authored-by: Luis Mastrangelo <acuarica@users.noreply.github.com> Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> * feat: Fix paths in Readme. Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> * feat: Review requests Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> --------- Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com> Co-authored-by: Luis Mastrangelo <acuarica@users.noreply.github.com>
- Loading branch information
1 parent
1303c76
commit 238ce54
Showing
12 changed files
with
9,449 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# For Hedera Testnet | ||
# The operator private keys start with "0x" and are in hexadecimal format | ||
# Your testnet account ECDSA hex-encoded private key (Ex: 0xb46751179bc8aa9e129d34463e46cd...) | ||
TESTNET_OPERATOR_PRIVATE_KEY='TESTNET_OPERATOR_PRIVATE_KEY' | ||
|
||
# For Hedera Local Node | ||
# The operator private keys start with "0x" and are in hexadecimal format | ||
# Your Hedera Local Node ECDSA account alias private key (Ex: 0x105d050185ccb907fba04dd92d...) | ||
LOCAL_NODE_OPERATOR_PRIVATE_KEY='LOCAL_NODE_OPERATOR_PRIVATE_KEY' | ||
# Your Hedera Local Node JSON-RPC endpoint URL (ex: 'http://localhost:7546/') | ||
LOCAL_NODE_ENDPOINT='http://localhost:7546/' | ||
|
||
# For Hedera Previewnet | ||
# Your previewnet account ECDSA hex-encoded private key (Ex: 0x46751179bc8aa9e129d34463e...) | ||
PREVIEWNET_OPERATOR_PRIVATE_KEY="YOUR_PREVIEWNET_OPERATOR_PRIVATE_KEY" | ||
|
||
# For Hedera Mainnet | ||
# Your mainnet account ECDSA hex-encoded private key (Ex: 0x46751179bc8aa9e129d34463e...) | ||
MAINNET_OPERATOR_PRIVATE_KEY="YOUR_MAINNET_OPERATOR_PRIVATE_KEY" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
node_modules | ||
.env | ||
coverage | ||
coverage.json | ||
typechain | ||
typechain-types | ||
|
||
# Hardhat files | ||
cache | ||
artifacts | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Hedera Hardhat Viem Example Project | ||
|
||
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). | ||
|
||
: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). | ||
|
||
# Viem vs Ethers.js Comparison | ||
|
||
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). | ||
|
||
| Feature | Viem | Ethers.js | | ||
|-----------------------|----------------------------------------------------------------------|-----------------------------------------| | ||
| Performance | [Optimized for speed](https://viem.sh/docs/introduction#performance) | Reliable performance | | ||
| API Design | Intuitive, developer-friendly | Well-designed, widely used | | ||
| Documentation | Comprehensive and clear | Good, but may vary by feature | | ||
| Debugging | Advanced debugging tools | Standard debugging capabilities | | ||
| Ecosystem Integration | Seamless integration with certain tools | Broad integration with many tools | | ||
|
||
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). | ||
|
||
## Project Files and Folders | ||
|
||
- `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. | ||
|
||
- `/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. | ||
|
||
- `/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. | ||
|
||
- `/scripts` - This folder contains essential JavaScript files for tasks such as deploying smart contracts to the Hedera network. | ||
|
||
- `.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. | ||
|
||
## Setup | ||
|
||
1. Clone this repo to your local machine: | ||
|
||
```shell | ||
git clone https://github.com/hashgraph/hedera-json-rpc-relay.git | ||
``` | ||
|
||
2. Once you've cloned the repository, open your IDE terminal and navigate to the root directory of the project: | ||
|
||
```shell | ||
cd hedera-json-rpc-relay/tools/hardhat-viem-example | ||
``` | ||
|
||
3. Run the following command to install all the necessary dependencies: | ||
|
||
```shell | ||
npm install | ||
``` | ||
|
||
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` | ||
|
||
5. Copy `.env.example` to `.env` | ||
|
||
6. Run the test script from the root directory of the project. The default network is set to "local." | ||
|
||
```shell | ||
# runs test on default network | ||
npx hardhat test | ||
|
||
# runs test on testnet | ||
npx hardhat test --network testnet | ||
``` | ||
|
||
Expect an output similar to the following: | ||
```shell | ||
RPC | ||
The address 0xe0b73F64b0de6032b193648c08899f20b5A6141D has 10000000000000000000000 weibars | ||
✔ should be able to get the account balance (1678ms) | ||
Greeter deployed to: 0xD9d0c5C0Ff85758BdF05A7636F8036d4D065F5B6 | ||
✔ should be able to deploy a contract (11456ms) | ||
Contract call result: initial_msg | ||
✔ should be able to make a contract view call (1249ms) | ||
Updated call result: updated_msg | ||
Contract call result: updated_msg | ||
✔ should be able to make a contract call (6806ms) | ||
|
||
|
||
4 passing (22s) | ||
``` | ||
|
||
7. Run the following command to deploy the smart contract. | ||
```shell | ||
# deploys to the default network | ||
npx hardhat deploy-contract | ||
|
||
# deploys to testnet | ||
npx hardhat deploy-contract --network testnet | ||
``` | ||
|
||
# Contributing | ||
Contributions are welcome. Please see the | ||
[contributing guide](https://github.com/hashgraph/.github/blob/main/CONTRIBUTING.md) | ||
to see how you can get involved. | ||
|
||
# Code of Conduct | ||
This project is governed by the | ||
[Contributor Covenant Code of Conduct](https://github.com/hashgraph/.github/blob/main/CODE_OF_CONDUCT.md). By | ||
participating, you are expected to uphold this code of conduct. Please report unacceptable behavior | ||
to [oss@hedera.com](mailto:oss@hedera.com). | ||
|
||
# License | ||
[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.9; | ||
|
||
contract Greeter { | ||
string private greeting; | ||
|
||
event GreetingSet(string greeting); | ||
|
||
//This constructor assigns the initial greeting and emit GreetingSet event | ||
constructor(string memory _greeting) { | ||
greeting = _greeting; | ||
|
||
emit GreetingSet(_greeting); | ||
} | ||
|
||
//This function returns the current value stored in greeting variable | ||
function greet() public view returns (string memory) { | ||
return greeting; | ||
} | ||
|
||
//This function sets the new greeting msg from the one passed down as parameter and emit event | ||
function setGreeting(string memory _greeting) public { | ||
greeting = _greeting; | ||
|
||
emit GreetingSet(_greeting); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require("@nomicfoundation/hardhat-toolbox-viem"); | ||
require("@nomicfoundation/hardhat-chai-matchers"); | ||
const { task } = require("hardhat/config"); | ||
|
||
// Import dotenv module to access variables stored in the .env file | ||
require("dotenv").config(); | ||
|
||
// Define Hardhat tasks here, which can be accessed in our test file (test/rpc.js) by using hre.run('taskName') | ||
task("show-balance", async () => { | ||
const showBalance = require("./scripts/showBalance"); | ||
return showBalance(); | ||
}); | ||
|
||
task("deploy-contract", async () => { | ||
const deployContract = require("./scripts/deployContract"); | ||
return deployContract(); | ||
}); | ||
|
||
task("contract-view-call", async (taskArgs) => { | ||
const contractViewCall = require("./scripts/contractViewCall"); | ||
return contractViewCall(taskArgs.contractAddress); | ||
}); | ||
|
||
task("contract-call", async (taskArgs) => { | ||
const contractCall = require("./scripts/contractCall"); | ||
return contractCall(taskArgs.contractAddress, taskArgs.msg); | ||
}); | ||
|
||
/** @type import('hardhat/config').HardhatUserConfig */ | ||
module.exports = { | ||
mocha: { | ||
timeout: 3600000 | ||
}, | ||
solidity: { | ||
version: "0.8.9", | ||
settings: { | ||
optimizer: { | ||
enabled: true, | ||
runs: 500 | ||
} | ||
} | ||
}, | ||
// This specifies network configurations used when running Hardhat tasks | ||
defaultNetwork: "local", | ||
networks: { | ||
local: { | ||
// Your Hedera Local Node address pulled from the .env file | ||
url: process.env.LOCAL_NODE_ENDPOINT, | ||
// Conditionally assign accounts when private key value is present | ||
accounts: process.env.LOCAL_NODE_OPERATOR_PRIVATE_KEY ? [process.env.LOCAL_NODE_OPERATOR_PRIVATE_KEY] : [] | ||
}, | ||
testnet: { | ||
// HashIO testnet endpoint | ||
url: 'https://testnet.hashio.io/api', | ||
// Conditionally assign accounts when private key value is present | ||
accounts: process.env.TESTNET_OPERATOR_PRIVATE_KEY ? [process.env.TESTNET_OPERATOR_PRIVATE_KEY] : [] | ||
}, | ||
|
||
/** | ||
* Uncomment the following to add a mainnet network configuration | ||
*/ | ||
mainnet: { | ||
// HashIO mainnet endpoint | ||
url: 'https://mainnet.hashio.io/api', | ||
// Conditionally assign accounts when private key value is present | ||
accounts: process.env.MAINNET_OPERATOR_PRIVATE_KEY ? [process.env.MAINNET_OPERATOR_PRIVATE_KEY] : [] | ||
}, | ||
|
||
/** | ||
* Uncomment the following to add a previewnet network configuration | ||
*/ | ||
previewnet: { | ||
// HashIO previewnet endpoint | ||
url:'https://previewnet.hashio.io/api', | ||
// Conditionally assign accounts when private key value is present | ||
accounts: process.env.PREVIEWNET_OPERATOR_PRIVATE_KEY ? [process.env.PREVIEWNET_OPERATOR_PRIVATE_KEY] : [] | ||
} | ||
} | ||
}; |
Oops, something went wrong.