0.1.9-beta • Published 12 months ago

@adisakboonmark/hardhat-task-gen v0.1.9-beta

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

hardhat-task-gen

Overview

hardhat-task-gen is a Hardhat plugin that automatically generates tasks from ABI files. This allows you to easily interact with smart contracts without manually defining Hardhat tasks.

Features

  • Auto-generates Hardhat tasks from contract ABIs
  • Supports overloaded functions with numbered suffixes
  • Allows configurable task prefixes
  • Handles payable functions with optional parameters
  • Provides clear CLI commands for contract interactions

Installation

npm install --save-dev @adisakboonmark/hardhat-task-gen

Usage

  1. Add the plugin to your Hardhat config (hardhat.config.ts or hardhat.config.js):
import "@adisakboonmark/hardhat-task-gen";

// or

require("@adisakboonmark/hardhat-task-gen");
  1. Run the task generation command:
$ npx hardhat task-gen
  1. Use the generated tasks to interact with your contracts:
$ npx hardhat task-gen:MyERC20:transfer --contract-address 0x123...456 --to 0xabc...123 --amount 1000

$ npx hardhat task-gen:MyERC721:name --contract-address 0x123...456

$ npx hardhat task-gen:MyERC1155:balanceOfBatch --contract-address 0x123...456 --accounts '["0xabc...123","0xabc...456"]' --ids '[0,1]'
  1. Run the helper command:
$ npx hardhat task-gen:MyERC1155:balanceOfBatch --help
Hardhat version 2.22.18

Usage: hardhat [GLOBAL OPTIONS] task-gen:MyERC1155:balanceOfBatch --accounts <STRING> [--contract-address <STRING>] --ids <STRING>

OPTIONS:

  --accounts            Parameter of type address[] (for tuple and array, provide a JSON string)
  --contract-address    The contract address
  --ids                 Parameter of type uint256[] (for tuple and array, provide a JSON string)

task-gen:MyERC1155:balanceOfBatch: Calls balanceOfBatch function on MyERC1155

For global options help run: hardhat help

Environment Variables

ABI Directory

The plugin reads ABI files from the directory specified by the environment variable:

process.env.ABI_DIRECTORY || "./artifacts/contracts/"

If ABI_DIRECTORY is not set, it defaults to ./artifacts/contracts/.

Default Contract Address

When executing a generated task, the contract address is determined using:

const contract = new hre.ethers.Contract(
  taskArgs.contractAddress || process.env.TASK_GEN_CONTRACT_ADDRESS,
  contractABI,
  signer
);

If TASK_GEN_CONTRACT_ADDRESS is not set, you must provide the contract address explicitly using --contract-address.

Configuration

You can customize the task generation by modifying your Hardhat config:

module.exports = {
  taskGen: {
    outPath: "./tasks-generated", // Specify the output path for generated tasks.
    clear: true, // Clear existing artifacts before generating the tasks, and it will force-run compilation. 
    runOnCompile: false, // Generate tasks automatically on each compile.
    prefix: "task-gen", // Set a custom task prefix.
  },
};

Example Overloaded Commands

For a contract named MyERC1155 with overloaded functions totalSupply() and totalSupply(uint256):

npx hardhat task-gen:MyERC1155:transfer:1         # Calls the totalSupply() function
npx hardhat task-gen:MyERC1155:transfer:2 --id 0  # Calls the totalSupply(uint256) function

Example Commands Document

Full Version Example Document

📜 Hardhat Contract Commands

AwesomeCat

Function NameParametersOutputsCommand
breedcatA tupleid (uint256),gender (uint8), catB tupleid (uint256),gender (uint8)newCat tupleid (uint256),gender (uint8)npx hardhat task-gen:AwesomeCat:breed:1 --contract-address <contractAddress:optional> --cat-a <catA> --cat-b <catB>
breedcatIdA (uint256), catIdB (uint256)newCat tupleid (uint256),gender (uint8)npx hardhat task-gen:AwesomeCat:breed:2 --contract-address <contractAddress:optional> --cat-id-a <catIdA> --cat-id-b <catIdB>
breedBatchcatIdsA (uint256[]), catIdsB (uint256[])newCats tuple[id (uint256),gender (uint8)]npx hardhat task-gen:AwesomeCat:breedBatch:1 --contract-address <contractAddress:optional> --cat-ids-a <catIdsA> --cat-ids-b <catIdsB>
breedBatchcatsA tuple[id (uint256),gender (uint8)], catsB tuple[id (uint256),gender (uint8)]newCats tuple[id (uint256),gender (uint8)]npx hardhat task-gen:AwesomeCat:breedBatch:2 --contract-address <contractAddress:optional> --cats-a <catsA> --cats-b <catsB>
catscatId (uint256)id (uint256), gender (uint8)npx hardhat task-gen:AwesomeCat:cats --contract-address <contractAddress:optional> --cat-id <catId>
craftGenderoutput1 (uint8)npx hardhat task-gen:AwesomeCat:craftGender --contract-address <contractAddress:optional>
existscatId (uint256)output1 (bool)npx hardhat task-gen:AwesomeCat:exists --contract-address <contractAddress:optional> --cat-id <catId>
mintCatoutput1 tupleid (uint256),gender (uint8)npx hardhat task-gen:AwesomeCat:mintCat --contract-address <contractAddress:optional>
ownersowner (address)id (uint256), gender (uint8)npx hardhat task-gen:AwesomeCat:owners --contract-address <contractAddress:optional> --owner <owner>
parentscatId (uint256)father tupleid (uint256),gender (uint8), mother tupleid (uint256),gender (uint8)npx hardhat task-gen:AwesomeCat:parents --contract-address <contractAddress:optional> --cat-id <catId>

IMPORTANT

Invalid Casing in Parameter Names (#HH208)

Problem:

Parameters like _type, _amount use underscores, which violate camelCase formatting.

Solution:

Fix this issue by removing underscores from all parameter names to ensure camelCase compliance.