Skip to content

Commit

Permalink
updated: migrating to ts boilerplate with lock contract and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmandabbakuti committed Nov 3, 2023
1 parent ce06e5d commit 71cf353
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 159 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ yarn.lock
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
# should NOT be excluded as they contain compiler settings and other important
# information for Eclipse / Flash Builder.
.DS_Store

#Hardhat files
cache
artifacts/
coverage/
coverage.json
.DS_Store
typechain/
typechain-types/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# hardhat-boilerplate
# Hardhat Boilerplate

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts with balances.

Expand All @@ -8,7 +8,7 @@ This project demonstrates a basic Hardhat use case. It comes with a sample contr
Try running some of the following tasks:

```shell
```bash
npm install

# starts local node
Expand All @@ -26,8 +26,8 @@ npx hardhat compile
# deploy contract defined in tasks on specified network
npx hardhat deploy --network local

# deploy contract in scripts/deploy.js on specified network
npx hardhat run scripts/deploy.js --network local
# deploy contract in scripts/deploy.ts on specified network
npx hardhat run scripts/deploy.ts --network local

#check linter issues using solhint plugin
npx hardhat check
Expand Down
18 changes: 0 additions & 18 deletions contracts/Greeter.sol

This file was deleted.

34 changes: 34 additions & 0 deletions contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
}
80 changes: 0 additions & 80 deletions hardhat.config.js

This file was deleted.

95 changes: 95 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "dotenv/config";

const accounts = process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [];

// https://hardhat.org/guides/create-task.html
task(
"accounts",
"Prints the list of accounts with balances",
async (_, hre) => {
const accounts = await hre.ethers.getSigners();
const provider = hre.ethers.provider;

for (const account of accounts) {
const balance = await provider.getBalance(account.address);
console.log(
`${account.address} - ${hre.ethers.formatEther(balance)} ETH`
);
}
}
);

task("deploy", "Deploys Contract", async (_, hre) => {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 300;
const lockedAmount = hre.ethers.parseEther("0.001");

const lockInstance = await hre.ethers.deployContract("Lock", [unlockTime], {
value: lockedAmount
});

await lockInstance.waitForDeployment();
console.log("contract deployed at:", lockInstance.target);
});

task("balance", "Prints an account's balance")
.addParam("account", "The account's address")
.setAction(async ({ account }, hre) => {
const provider = hre.ethers.provider;
const balance = await provider.getBalance(account);
console.log(hre.ethers.formatEther(balance), "ETH");
});

const config: HardhatUserConfig = {
defaultNetwork: "local",
networks: {
hardhat: {
chainId: 1337
},
local: {
url: "http://127.0.0.1:8545"
},
mumbai: {
url:
process.env.POLYGON_MUMBAI_RPC_URL ||
"https://rpc-mumbai.maticvigil.com",
accounts
},
polygon: {
url:
process.env.POLYGON_MAINNET_RPC_URL ||
"https://rpc-mainnet.maticvigil.com",
accounts
}
},
etherscan: {
// API key for Polygonscan
apiKey: process.env.ETHERSCAN_API_KEY
},
gasReporter: {
enabled: true,
currency: "USD"
},
solidity: {
version: "0.8.19",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 20000
}
};

export default config;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hardhat-boilerplate",
"version": "1.2.0",
"description": "hardhat and ethersjs boilerplate for Dapp development",
"version": "1.2.2",
"description": "Hardhat boilerplate for Dapp development",
"private": true,
"scripts": {
"test": "npx hardhat test",
Expand All @@ -15,6 +15,6 @@
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^3.0.0",
"dotenv": "^16.3.1",
"hardhat": "^2.17.4"
"hardhat": "^2.19.0"
}
}
25 changes: 0 additions & 25 deletions scripts/deploy.js

This file was deleted.

31 changes: 31 additions & 0 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ethers } from "hardhat";

async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 300;
const lockedAmount = ethers.parseEther("0.001");

const lockInstance = await ethers.deployContract("Lock", [unlockTime], {
value: lockedAmount
});

await lockInstance.waitForDeployment();
return lockInstance;
}

main()
.then(async (lockInstance) => {
console.log("Lock Contract deployed to:", lockInstance.target);
// Read from the contract
const unlockTime = await lockInstance.unlockTime();
console.log("Unlock time:", unlockTime.toString());

// Write to the contract
// const tx = await lockInstance.withdraw();
// await tx.wait();
// console.log("Withdrawn!");
})
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
Loading

0 comments on commit 71cf353

Please sign in to comment.