'How is hardhat implicitly knowing deployer address?
I am new to using hardhat and I was trying to figure out how it is working. The documentation seems a little bit fuzzy as there are multiple layers of abstractizations and I don't understand how is the ethers.getSigners() using my private key. Is it implicitly using the hardhat config file defined in the root of my directory? Also is hardhat project structure predefined and enforced? If not, how does getContractFactory know where to look for the artifact?
const { ethers } = require("hardhat");
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Account balance:", (await deployer.getBalance()).toString());
const VendingMachine = await ethers.getContractFactory("VendingMachine")
const vendingMachine = await VendingMachine.deploy()
await vendingMachine.deployed()
console.log("Contract deployed to address:", vendingMachine.address)
}
Solution 1:[1]
Hardhat node uses a default mnemonic phrase (docs) that can be derived into multiple pairs of private/public keys (20 by default). These accounts are unlocked at the local node.
Your snippet uses the ethers NPM package (imported by hardhat
) that uses the first unlocked account to send transactions from (including deployment transactions), unless specified otherwise.
The project structure (e.g. contracts
and test
folders) is defined by the Hardhat framework - but you can change the default folder structure to a custom one (docs).
Solution 2:[2]
Hardhat is a great tool that has some premade configuration to make the development easy, hardhat automatically injects the signers data to their version of ethers, in that way you automatically have the default signers or one made with the private keys or mnemonic that you can define in the hardhat config, if you don't select an specific signer when you create a contract instance hardhat is also smart enough to select the first item in the signer array.
About the getContractFactory, hardhat have some predifined routes to store the compiled contract data, but this can be overwrite in the hardhat config if you wish to, that way hardhat knows where to look for the data you are asking for
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | jhonny |