Skip to content

Commit

Permalink
Push Week 18
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyYap committed May 24, 2023
1 parent 89c270e commit b48e87a
Show file tree
Hide file tree
Showing 11 changed files with 1,185 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Week19/randomness/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
23 changes: 23 additions & 0 deletions Week19/randomness/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you don't wish to use zero-installs
# Documentation here: https://yarnpkg.com/features/zero-installs
!.yarn/cache
#.pnp.*

node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

5 changes: 5 additions & 0 deletions Week19/randomness/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": "hardhat/register",
"timeout": 40000,
"_": ["tests/**/*.ts"]
}
873 changes: 873 additions & 0 deletions Week19/randomness/.yarn/releases/yarn-3.5.1.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Week19/randomness/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.5.1.cjs
13 changes: 13 additions & 0 deletions Week19/randomness/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

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:

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.ts
```
16 changes: 16 additions & 0 deletions Week19/randomness/contracts/NotQuiteRandom.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract NotQuiteRandom {
function getRandomNumber()
public
view
returns (uint256 notQuiteRandomNumber)
{
// TODO: get randomness from block hash
}

function tossCoin() public view returns (bool heads) {
// TODO: make the random number be translated to a boolean
}
}
19 changes: 19 additions & 0 deletions Week19/randomness/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();

for (const account of accounts) {
console.log(account.address);
console.log(await account.getBalance());
}
});

const config: HardhatUserConfig = {
solidity: "0.8.18",
paths: { tests: "tests" },
};

export default config;

86 changes: 86 additions & 0 deletions Week19/randomness/scripts/TestRandom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { ethers } from "ethers";
import { NotQuiteRandom } from "../typechain-types";

async function blockHashRandomness() {
const contractFactory = await ethers.getContractFactory("NotQuiteRandom");
contractFactory.deploy().then(async (result) => {
result.deployed().then(async (contract: NotQuiteRandom) => {
const currentBlock = await ethers.provider.getBlock("latest");
const randomNumber = await contract.getRandomNumber();
console.log(
`Block number: ${currentBlock.number}\nBlock hash: ${currentBlock.hash}\nRandom number from this block hash: ${randomNumber}`
);
await ethers.provider.send("evm_mine", [currentBlock.timestamp + 1]);
const currentBlock2 = await ethers.provider.getBlock("latest");
const randomNumber2 = await contract.getRandomNumber();
console.log(
`Block number: ${currentBlock2.number}\nBlock hash: ${currentBlock2.hash}\nRandom number from this block hash: ${randomNumber2}`
);
await ethers.provider.send("evm_mine", [currentBlock2.timestamp + 1]);
const currentBlock3 = await ethers.provider.getBlock("latest");
const randomNumber3 = await contract.getRandomNumber();
console.log(
`Block number: ${currentBlock3.number}\nBlock hash: ${currentBlock3.hash}\nRandom number from this block hash: ${randomNumber3}`
);
await ethers.provider.send("evm_mine", [currentBlock3.timestamp + 1]);
const currentBlock4 = await ethers.provider.getBlock("latest");
const randomNumber4 = await contract.getRandomNumber();
console.log(
`Block number: ${currentBlock4.number}\nBlock hash: ${currentBlock4.hash}\nRandom number from this block hash: ${randomNumber4}`
);
await ethers.provider.send("evm_mine", [currentBlock4.timestamp + 1]);
const currentBlock5 = await ethers.provider.getBlock("latest");
const randomNumber5 = await contract.getRandomNumber();
console.log(
`Block number: ${currentBlock5.number}\nBlock hash: ${currentBlock5.hash}\nRandom number from this block hash: ${randomNumber5}`
);
});
});
}

async function tossCoin() {
const contractFactory = await ethers.getContractFactory("NotQuiteRandom");
contractFactory.deploy().then(async (result) => {
result.deployed().then(async (contract: NotQuiteRandom) => {
const currentBlock = await ethers.provider.getBlock("latest");
const heads = await contract.tossCoin();
console.log(
`Block number: ${currentBlock.number}\nBlock hash: ${
currentBlock.hash
}\nThe coin landed as: ${heads ? "Heads" : "Tails"}`
);
await ethers.provider.send("evm_mine", [currentBlock.timestamp + 1]);
const currentBlock2 = await ethers.provider.getBlock("latest");
const heads2 = await contract.tossCoin();
console.log(
`Block number: ${currentBlock2.number}\nBlock hash: ${
currentBlock2.hash
}\nThe coin landed as: ${heads2 ? "Heads" : "Tails"}`
);
await ethers.provider.send("evm_mine", [currentBlock2.timestamp + 1]);
const currentBlock3 = await ethers.provider.getBlock("latest");
const heads3 = await contract.tossCoin();
console.log(
`Block number: ${currentBlock3.number}\nBlock hash: ${
currentBlock3.hash
}\nThe coin landed as: ${heads3 ? "Heads" : "Tails"}`
);
await ethers.provider.send("evm_mine", [currentBlock3.timestamp + 1]);
const currentBlock4 = await ethers.provider.getBlock("latest");
const heads4 = await contract.tossCoin();
console.log(
`Block number: ${currentBlock4.number}\nBlock hash: ${
currentBlock4.hash
}\nThe coin landed as: ${heads4 ? "Heads" : "Tails"}`
);
await ethers.provider.send("evm_mine", [currentBlock4.timestamp + 1]);
const currentBlock5 = await ethers.provider.getBlock("latest");
const heads5 = await contract.tossCoin();
console.log(
`Block number: ${currentBlock5.number}\nBlock hash: ${
currentBlock5.hash
}\nThe coin landed as: ${heads5 ? "Heads" : "Tails"}`
);
});
});
}
124 changes: 124 additions & 0 deletions Week19/randomness/tests/Lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { time, loadFixture } from "@nomicfoundation/hardhat-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.address)).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]
);
});
});
});
});
13 changes: 13 additions & 0 deletions Week19/randomness/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"include": ["./scripts", "./tests", "./typechain-types"],
"files": ["./hardhat.config.ts"],
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
}
}

0 comments on commit b48e87a

Please sign in to comment.