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

feat: add hardhat plugin to support HTS emulation in fork testing #50

Merged
merged 14 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
23 changes: 23 additions & 0 deletions tools/hardhat-forking-plugin-usage-example/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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=''

# For Hedera Previewnet
# Your previewnet account ECDSA hex-encoded private key (Ex: 0x46751179bc8aa9e129d34463e...)
PREVIEWNET_OPERATOR_PRIVATE_KEY=""

# For Hedera Mainnet
# Your mainnet account ECDSA hex-encoded private key (Ex: 0x46751179bc8aa9e129d34463e...)
MAINNET_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=''
# Your Hedera Local Node JSON-RPC endpoint URL (ex: 'http://localhost:7546/')
LOCAL_RPC_URL='http://127.0.0.1:8545/'

# MirrorNode URL - Hedera Token Service data will be retrieved from this URL and used to set up storage
# for the corresponding token proxies.
HEDERA_MIRRORNODE_URL=https://testnet.mirrornode.hedera.com/api/v1/
12 changes: 12 additions & 0 deletions tools/hardhat-forking-plugin-usage-example/.gitignore
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
25 changes: 25 additions & 0 deletions tools/hardhat-forking-plugin-usage-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hashgraph Forking Plugin for HardHat - Usage Example

This example demonstrates how to use the Hedera plugin in the smart contract development process with HardHat.

## Configuration

To begin, you need to start the HardHat fork, which enables the use of the `hardhat_setStorageAt` and `hardhat_setCode` methods:

```bash
npx hardhat node --fork https://testnet.hashio.io/api
```

## Running Tests

To execute the tests, first install the necessary dependencies:

```bash
npm install
```

Then run the tests with the following command:

```bash
npx hardhat test
```
21 changes: 21 additions & 0 deletions tools/hardhat-forking-plugin-usage-example/contracts/IERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/// https://hips.hedera.com/hip/hip-218
/// https://hips.hedera.com/hip/hip-376
interface IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);

function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);

function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
78 changes: 78 additions & 0 deletions tools/hardhat-forking-plugin-usage-example/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*-
*
* Hedera Hardhat Plugin Example Project
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

require("@nomicfoundation/hardhat-toolbox-viem");
require("@nomicfoundation/hardhat-chai-matchers");
require("@hashgraph/hardhat-forking-plugin");

// Import dotenv module to access variables stored in the .env file
require("dotenv").config();

/** @type import('@hashgraph/hardhat-forking-plugin').HeaderaHardhatConfig */
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_RPC_URL,
// 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] : []
}
}
};
Loading