Skip to content

Commit

Permalink
ADD | national-wealth-native and initial hardhat files
Browse files Browse the repository at this point in the history
  • Loading branch information
itstoreall committed Feb 12, 2023
1 parent db120be commit 0b82019
Show file tree
Hide file tree
Showing 34 changed files with 6,236 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# hardhat
# Hardhat
22 changes: 22 additions & 0 deletions national-wealth-hardhat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.DS_Store
.encryptedKey.json

.env
.env.test
.env.local
.env.development.local
.env.test.local
.env.production.local

node_modules
node_modules/
/node_modules

coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts
Empty file.
6 changes: 6 additions & 0 deletions national-wealth-hardhat/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 2,
"semi": false,
"useTabs": false,
"singleQuote": true
}
13 changes: 13 additions & 0 deletions national-wealth-hardhat/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.js
```
29 changes: 29 additions & 0 deletions national-wealth-hardhat/contracts/CitizenAccount.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import "./NationalWealth.sol";

contract CitizenAccount is NationalWealth {
uint256 bank_account;

struct Citizen {
uint256 bank_account;
string name;
}

Citizen[] public citizens;

function store(uint256 _acc) public virtual {
bank_account = _acc;
}

function retrieve() public view returns (uint256) {
return bank_account;
}

function addPerson(string calldata _name, uint256 _acc) public {
citizens.push(Citizen(_acc, _name));
NationalWealth.findCitizenByAccount[_acc] = _name;
}
}
7 changes: 7 additions & 0 deletions national-wealth-hardhat/contracts/NationalWealth.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

contract NationalWealth {
mapping(uint256 => string) public findCitizenByAccount;
}
26 changes: 26 additions & 0 deletions national-wealth-hardhat/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { task } = require('hardhat/config')

require('@nomicfoundation/hardhat-toolbox')
require('dotenv').config()
require('@nomiclabs/hardhat-etherscan')
require('./tasks/tasks')

const GOERLI_RPC_URL = process.env.RPC_URL_ALCHEMY
const PRIVATE_KEY = process.env.PRIVATE_KEY_METAMASK
const ETHERSCAN_API_KEY = process.env.API_KEY_ETHERSCAN

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
defaultNetwork: 'hardhat',
networks: {
goerli: {
url: GOERLI_RPC_URL,
accounts: [PRIVATE_KEY],
chainId: 5,
},
},
solidity: '0.8.17',
etherscan: {
apiKey: ETHERSCAN_API_KEY,
},
}
26 changes: 26 additions & 0 deletions national-wealth-hardhat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "national-wealth-hardhat",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@ethersproject/abi": "^5.4.7",
"@ethersproject/providers": "^5.4.7",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-etherscan": "^3.1.6",
"@typechain/ethers-v5": "^10.1.0",
"@typechain/hardhat": "^6.1.2",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
"ethers": "^5.4.7",
"hardhat": "^2.12.7",
"hardhat-gas-reporter": "^1.0.8",
"prettier": "^2.8.4",
"prettier-plugin-solidity": "^1.1.2",
"solidity-coverage": "^0.8.0",
"typechain": "^8.1.0"
}
}
49 changes: 49 additions & 0 deletions national-wealth-hardhat/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { ethers, run, network } = require('hardhat')

const deploy = async () => {
const CitizenAccountFactory = await ethers.getContractFactory(
'CitizenAccount'
)
console.log('Deploying contract...')
const citizenAccount = await CitizenAccountFactory.deploy()
await citizenAccount.deployed()
console.log('citizenAccount:', citizenAccount.address)

if (network.config.chainId === 5 && process.env.API_KEY_ETHERSCAN) {
await citizenAccount.deployTransaction.wait(6)
await verify(citizenAccount.address, [])
}

const currentValue = await citizenAccount.retrieve()
console.log('Current value:', currentValue)

const txResponse = await citizenAccount.store(7)
await txResponse.wait(1)

const updatedValue = await citizenAccount.retrieve()
console.log('Updated value:', updatedValue)
}

const verify = async (contractAddress, args) => {
console.log('Verifying contract...')

try {
await run('verify:verify', {
address: contractAddress,
constructorArguments: args,
})
} catch (e) {
if (e.message.toLowerCase().includes('already verified')) {
console.log('Already verified!')
} else {
console.log('Error in verify() (deploy.js)', e)
}
}
}

deploy()
.then(() => process.exit(0))
.catch((error) => {
console.error(`Deploy ${error}`)
process.exit(1)
})
19 changes: 19 additions & 0 deletions national-wealth-hardhat/tasks/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { task } = require('hardhat/config')

task(
'block-number',
'Prints the current block number',
async (taskArgs, hre) => {
const blockNumber = await hre.ethers.provider.getBlockNumber()
console.log('Block number:', blockNumber)
}
)

task('accounts', 'Prints the list of accounts', async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners()
for (const account of accounts) {
console.log('account:', account.address)
}
})

module.exports = {}
126 changes: 126 additions & 0 deletions national-wealth-hardhat/test/Lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const {
time,
loadFixture,
} = require("@nomicfoundation/hardhat-network-helpers");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");

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]
);
});
});
});
});
Loading

0 comments on commit 0b82019

Please sign in to comment.