This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.
Try running some of the following tasks:
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.ts
npm install --save-dev hardhat
npx hardhat
- Compiling
npx hardhat compile
- Testing
npx hardhat test
- Deploy to test local network
npx hardhat run scripts/deploy-lending-pool.ts --network localhost
- Run a local node
npx hardhat node
- Interaction with the network
npx hardhat console
example:
const LendingPool = await ethers.getContractFactory("LendingPool");
const lendingPool = await LendingPool.deploy();
console.log(await lendingPool.lendFrom('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 100));
console.log(await lendingPool.totalLendingAmount());
get events:
const LendingPool = await ethers.getContractFactory("LendingPool");
const lendingPool = await LendingPool.deploy();
console.log(await lendingPool.lendFrom('0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199', 100));
const receiveFilter = lendingPool.filters.Lend('0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199', null);
const signer = await ethers.getSigner();
const logs = await signer.provider.getLogs(receiveFilter);
console.log(logs);
- Don't specify default values without necessary:
uint a; <-- by default has 0 as default and it's cheaper
uint a = 0; <--- the same, it has 0 as default, but it a bit costly because of necessary operation.
- Try to define variables with the same type on by one, like:
uint128 a; <-- they will be kept in the memory more efficiently (it's like a packing into a word into the memory)
uint128 b;
uint256 c;
incorrect:
uint128 a;
uint256 c; <--- we break the chain
uint128 b;
-
Better to use
uint
instead ofuint8
e.g, because theuint
might occupy the whole word in the memory, anduint8
will only occupy the some part of the word and will be cut. But for arrays better to use smaller uint size likeuint8
-
We need to try use static data for state vars and do not calculate them dynamically. eg:
bytes32 hash = 0x....... <- consumes low amount of gas (a correct way)
bytes32 hash = keccak256(abi.encodePacked("test")) <-- will consume more gas (incorrect way)
- Don't use temp variables without necessary. eg:
address _from = msg.sender; <- here we introduce a not necessary variable, which also consumes gas (incorrect way)
require(_from != address(0), "zero address");
require(msg.sender != address(0), "zero address"); <-- we don't use any extra vars here (a correct way)
-
If it possible use
mapping
insteadarrays
(especially array with dynamic length), because it's cheaper. -
Don't use a big amount of small functions, better to use bigger functions.
-
Don't frequently change a state variable. eg, in cycles. Better to use a temp variable and once the cycle is finished working we can change that state variable assigning the temp var's value it it.
-
Don't store big amount of data in smart contracts. The better way is use some off chain approaches.