Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
hardhat-cli-utils
Advanced tools
This repository contains many predefined hardhat tasks as hardhat subcommands to help you play with smart contracts by CLI.
Task | Description |
---|---|
deploy | Deploy contract. |
read | Call (read) the method of contract on given address |
write | Call (write) the method of contract on given address |
nonce | Get the nonce of given account |
bytecode | Get the runtime byte code on given contract address |
initcode | Get the creation byte code (excluding constructor parameters part) |
dig | Resolve given ENS domain name |
npm install --save-dev hardhat-cli-utils
And add the following statement to your hardhat.config.js
:
const hcu = require("hardhat-cli-utils");
hcu.registerAll();
Or, if you are using TypeScript, add this to your hardhat.config.ts
:
import hcu from "hardhat-cli-utils";
hcu.registerAll();
hcu.registerAll
will register all sub-commands predefined in this package.
There are also additional ways to register sub-commands more specifically.
You can use hcu.register
to explicitly specify which commands you want to register. For exmaple,
hcu.register(['read', 'write', 'dig', 'deploy']); // Register these 4 only
You can use hcu.registerExcluding
to resigster all sub-commands apart from what you list. For example, you may perfer the deploy
sub-command provided by hardhat-deploy instead of this package, so you write the following statement to avoid confliction
hcu.registerExcluding(['deploy']);
Let's suppose you have Box
contract in your hardhat project.
contract Box {
uint private value;
constructor(uint initValue) {
value = initValue;
}
function getValue() public view returns(uint) {
return value;
}
function setValue(uint newValue) public {
value = newValue;
}
}
And you have set up a test-net environment, in our example, Goerli.
// hardhat.config.js or hardhat.config.js
const config = {
networks: {
goerli: {
url: "REPLACE_THIS_WITH_YOUR_RPC_URL",
accounts: ["REPLACE_THIS_WITH_YOUR_PRIVATE_KEY"]
},
}
}
Run with npx hardhat --network <network> deploy <class> [...params]
.
On success, the address of contract created is outputted.
$ npx hardhat --network goerli deploy Box 123
0xb62208Cf91f8654D1Baa7Af12d1C6f7ad655dcd8
Run with npx hardhat --network <network> read <class> <address> <method> [...params]
.
On success, the returned result is showed.
$ npx hardhat --network goerli read Box 0xb62208Cf91f8654D1Baa7Af12d1C6f7ad655dcd8 getValue
Return: BigNumber { value: "123" }
Run with npx hardhat --network <network> write <class> <address> <method> [...params]
.
On success, it output the link to view this transaction on Etherscan . If there are events emitted during this transaction, then the events are showed.
$ npx hardhat --network goerli write Box 0xb62208Cf91f8654D1Baa7Af12d1C6f7ad655dcd8 setValue 456
View this tx at https://goerli.etherscan.io/tx/0x32e051dc4f3d20395b00b3715a23e908d5eaa4010ba237214f385676ff3c80bd
No event emitted.
Done!
Run with npx hardhat --network <network> nonce <address>
.
Output the nonce of given account.
$ npx hardhat --network goerli nonce 0xb62208Cf91f8654D1Baa7Af12d1C6f7ad655dcd8
1
For the creation byte code of a contract of given class, run npx hardhat initcode <class>
.
$ npx hardhat initcode Box
0x608060405234801561001057600080fd5b5060405161010a38038061010a83398101604081905261002f91610037565b600055610050565b60006020828403121561004957600080fd5b5051919050565b60ac8061005e6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c8063209652551460375780635524107714604c575b600080fd5b60005460405190815260200160405180910390f35b605c6057366004605e565b600055565b005b600060208284031215606f57600080fd5b503591905056fea2646970667358221220b23e771e24a1806f389a28918dce80947911d0a0243543ea7af07c813207c99564736f6c63430008090033
Contracts with Libraries
Some contracts need to be linked with libraries before they are deployed. In such case, the output will not be pure hex string. Instead, the output contains a placeholder in which the library contract address should be filled. For example, an output may be like this: (Notice the two $ symbols in bytecode)
0x608060405234801561001057600080fd5b50610192806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063165c4a1614610030575b600080fd5b61004361003e3660046100fd565b610064565b604080519283526001600160a01b0390911660208301520160405180910390f35b6040516332292b2760e21b81526004810183905260248101829052600090819073__$118fb7ce387586ecccae2b5c8d4a11c6b1$__9063c8a4ac9c90604401604080518083038186803b1580156100ba57600080fd5b505af41580156100ce573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100f2919061011f565b915091509250929050565b6000806040838503121561011057600080fd5b50508035926020909101359150565b6000806040838503121561013257600080fd5b825160208401519092506001600160a01b038116811461015157600080fd5b80915050925092905056fea26469706673582212207fee896a627c16dca0cfa37edb26a96e129bc36672aabb59ce8f9ab5cdc7143664736f6c63430008090033
After replacing __$118fb7ce387586ecccae2b5c8d4a11c6b1$__
with the library contract address, you get the actual creation code.
For the runtime byte code of a contract at given address, run npx hardhat --network <network> bytecode <address>
.
$ npx hardhat --network goerli bytecode 0xb62208Cf91f8654D1Baa7Af12d1C6f7ad655dcd8
# The output is omitted...
For the runtime byte code of a contract of given class, run npx hardhat bytecode <class>
.
$ npx hardhat bytecode Box
0x6080604052348015600f57600080fd5b506004361060325760003560e01c8063209652551460375780635524107714604c575b600080fd5b60005460405190815260200160405180910390f35b605c6057366004605e565b600055565b005b600060208284031215606f57600080fd5b503591905056fea2646970667358221220b23e771e24a1806f389a28918dce80947911d0a0243543ea7af07c813207c99564736f6c63430008090033
Contracts with Libraries
Similar to the situation of the initcode
command, if your contract need to be linked with libraries, the output will contain an address placeholder.
$ npx hardhat --network goerli dig example.eth
0x9548650b6A1775d3d411D625207519b7Fe227A5a
Well, there are also many other awesome packages extending hardhat CLI, such as
verify
hardhat-etherscan can help you verify and publish contract source code on Etherscan.
Or, any contribution to this repository is welcome :-)
FAQs
Hardhat command line utilities
The npm package hardhat-cli-utils receives a total of 0 weekly downloads. As such, hardhat-cli-utils popularity was classified as not popular.
We found that hardhat-cli-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.