Skip to content

Commit

Permalink
updated: using ts setup with lock contract and tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmandabbakuti committed Nov 3, 2023
1 parent 981d1b1 commit 4fc3469
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 107 deletions.
2 changes: 0 additions & 2 deletions .env.example

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +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
typechain/
typechain-types/
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

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.

> Recommended to use Node.js v16+ and npm v8+
> Rename `env.example` to `.env` and add your env specific keys.
> Recommended to use Node.js v18+ and npm v8+
Try running some of the following tasks:

```shell
npm install

# Set/Read/Remove hardhat config variables
# npx hardhat vars set API_KEY
# npx hardhat vars get API_KEY
# npx hardhat vars DELETE API_KEY
# npx hardhat vars list

# set PRIVATE_KEY
npx hardhat vars set PRIVATE_KEY


# starts local node
npx hardhat node

Expand All @@ -20,8 +28,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

# verify contract
npx hardhat verify --network <deployed network> <deployed contract address> "<constructor1>" "<constructor2>"
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);
}
}
25 changes: 0 additions & 25 deletions hardhat.config.js

This file was deleted.

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

const accounts = vars.has("PRIVATE_KEY") ? [vars.get("PRIVATE_KEY")] : [];

const config: HardhatUserConfig = {
solidity: "0.8.19",
defaultNetwork: "local",
networks: {
local: {
url: "http://127.0.0.1:8545"
},
mumbai: {
url: "https://rpc-mumbai.maticvigil.com",
accounts
}
}
};

export default config;
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "hardhat-boilerplate-minimal",
"version": "1.0.0",
"version": "1.0.1",
"description": "Minimal hardhat boilerplate for Dapp development",
"main": "index.js",
"private": true,
"scripts": {
"test": "npx hardhat test",
"compile": "npx hardhat compile",
Expand All @@ -13,7 +13,6 @@
"license": "ISC",
"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 + 60;
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;
});
127 changes: 127 additions & 0 deletions test/Lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {
time,
loadFixture,
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import { ethers } from "hardhat";

describe("Lock", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployOneYearLockFixture() {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const ONE_GWEI = 1_000_000_000;

const lockedAmount = ONE_GWEI;
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;

// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();

const Lock = await ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });

return { lock, unlockTime, lockedAmount, owner, otherAccount };
}

describe("Deployment", function () {
it("Should set the right unlockTime", async function () {
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);

expect(await lock.unlockTime()).to.equal(unlockTime);
});

it("Should set the right owner", async function () {
const { lock, owner } = await loadFixture(deployOneYearLockFixture);

expect(await lock.owner()).to.equal(owner.address);
});

it("Should receive and store the funds to lock", async function () {
const { lock, lockedAmount } = await loadFixture(
deployOneYearLockFixture
);

expect(await ethers.provider.getBalance(lock.target)).to.equal(
lockedAmount
);
});

it("Should fail if the unlockTime is not in the future", async function () {
// We don't use the fixture here because we want a different deployment
const latestTime = await time.latest();
const Lock = await ethers.getContractFactory("Lock");
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
"Unlock time should be in the future"
);
});
});

describe("Withdrawals", function () {
describe("Validations", function () {
it("Should revert with the right error if called too soon", async function () {
const { lock } = await loadFixture(deployOneYearLockFixture);

await expect(lock.withdraw()).to.be.revertedWith(
"You can't withdraw yet"
);
});

it("Should revert with the right error if called from another account", async function () {
const { lock, unlockTime, otherAccount } = await loadFixture(
deployOneYearLockFixture
);

// We can increase the time in Hardhat Network
await time.increaseTo(unlockTime);

// We use lock.connect() to send a transaction from another account
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
"You aren't the owner"
);
});

it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
const { lock, unlockTime } = await loadFixture(
deployOneYearLockFixture
);

// Transactions are sent using the first signer by default
await time.increaseTo(unlockTime);

await expect(lock.withdraw()).not.to.be.reverted;
});
});

describe("Events", function () {
it("Should emit an event on withdrawals", async function () {
const { lock, unlockTime, lockedAmount } = await loadFixture(
deployOneYearLockFixture
);

await time.increaseTo(unlockTime);

await expect(lock.withdraw())
.to.emit(lock, "Withdrawal")
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
});
});

describe("Transfers", function () {
it("Should transfer the funds to the owner", async function () {
const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
deployOneYearLockFixture
);

await time.increaseTo(unlockTime);

await expect(lock.withdraw()).to.changeEtherBalances(
[owner, lock],
[lockedAmount, -lockedAmount]
);
});
});
});
});
28 changes: 0 additions & 28 deletions test/sample-test.js

This file was deleted.

Loading

0 comments on commit 4fc3469

Please sign in to comment.