Skip to content

Commit c25390a

Browse files
committed
Complete lession
- Learn how to use storage, call from other contract and inheritance
1 parent f4b4e2a commit c25390a

14 files changed

+8041
-0
lines changed

storage-factory/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
.env
3+
coverage
4+
coverage.json
5+
typechain
6+
7+
#Hardhat files
8+
cache
9+
artifacts

storage-factory/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Storage Factory
2+
3+
Ref: https://github.com/PatrickAlphaC/storage-factory-fcc
4+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.4;
4+
5+
import "./SimpleStorage.sol";
6+
7+
contract ExtraStorage is SimpleStorage {
8+
function store(uint256 _favoriteNumber) public virtual override {
9+
// number field in SimpleStorage.
10+
number = _favoriteNumber + 5;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.4;
4+
5+
contract SimpleStorage {
6+
uint256 number;
7+
8+
struct People {
9+
uint256 number;
10+
string name;
11+
}
12+
13+
People[] public people;
14+
15+
mapping(string => uint256) public nameToNumber;
16+
17+
function store(uint256 _number) public virtual {
18+
number = _number;
19+
}
20+
21+
function retrieve() public view returns (uint256) {
22+
return number;
23+
}
24+
25+
function addPerson(string memory _name, uint256 _number) public {
26+
people.push(People(_number, _name));
27+
nameToNumber[_name] = _number;
28+
}
29+
30+
function getNameToNumber(string memory _name)
31+
public
32+
view
33+
returns (uint256)
34+
{
35+
return nameToNumber[_name];
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.4;
4+
5+
import "./SimpleStorage.sol";
6+
7+
contract StorageFactory {
8+
SimpleStorage[] public simpleStorageArray;
9+
10+
function createSimpleStorageContract() public {
11+
SimpleStorage simpleStorage = new SimpleStorage();
12+
simpleStorageArray.push(simpleStorage);
13+
}
14+
15+
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber)
16+
public
17+
{
18+
// Address
19+
// ABI
20+
// SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).store(_simpleStorageNumber);
21+
simpleStorageArray[_simpleStorageIndex].store(_simpleStorageNumber);
22+
}
23+
24+
function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) {
25+
// return SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).retrieve();
26+
return simpleStorageArray[_simpleStorageIndex].retrieve();
27+
}
28+
}

storage-factory/hardhat.config.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require("@nomiclabs/hardhat-waffle");
2+
3+
// This is a sample Hardhat task. To learn how to create your own go to
4+
// https://hardhat.org/guides/create-task.html
5+
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
6+
const accounts = await hre.ethers.getSigners();
7+
8+
for (const account of accounts) {
9+
console.log(account.address);
10+
}
11+
});
12+
13+
// You need to export an object to set up your config
14+
// Go to https://hardhat.org/config/ to learn more
15+
16+
/**
17+
* @type import('hardhat/config').HardhatUserConfig
18+
*/
19+
module.exports = {
20+
solidity: "0.8.4",
21+
};

storage-factory/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "storage-factory",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"devDependencies": {
7+
"@nomiclabs/hardhat-ethers": "^2.0.0",
8+
"@nomiclabs/hardhat-waffle": "^2.0.0",
9+
"chai": "^4.2.0",
10+
"ethereum-waffle": "^3.0.0",
11+
"ethers": "^5.0.0",
12+
"hardhat": "2.9.3"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const hre = require("hardhat");
2+
3+
const main = async () => {
4+
const Contract = await hre.ethers.getContractFactory("SimpleStorage");
5+
const contract = await Contract.deploy();
6+
await contract.deployed();
7+
8+
console.log("Contract deployed to:", contract.address);
9+
};
10+
11+
main()
12+
.then(() => process.exit(0))
13+
.catch((error) => {
14+
console.error(error);
15+
process.exit(1);
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const hre = require("hardhat");
2+
3+
const main = async () => {
4+
const Contract = await hre.ethers.getContractFactory("StorageFactory");
5+
const contract = await Contract.deploy();
6+
await contract.deployed();
7+
8+
console.log("Contract deployed to:", contract.address);
9+
10+
const createTx = await contract.createSimpleStorageContract();
11+
createTx.wait();
12+
13+
const storage_0 = await contract.sfGet(0);
14+
console.log("Storage 0 : ", storage_0);
15+
16+
const storeTx = await contract.sfStore(0, 20);
17+
storeTx.wait();
18+
19+
const storage_0_new = await contract.sfGet(0);
20+
console.log("Storage 0 (Updated) : ", storage_0_new);
21+
};
22+
23+
main()
24+
.then(() => process.exit(0))
25+
.catch((error) => {
26+
console.error(error);
27+
process.exit(1);
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const hre = require("hardhat");
2+
3+
const main = async () => {
4+
const Contract = await hre.ethers.getContractFactory("ExtraStorage");
5+
const contract = await Contract.deploy();
6+
await contract.deployed();
7+
8+
console.log("Contract deployed to:", contract.address);
9+
10+
const tx = await contract.store(10);
11+
tx.wait();
12+
13+
const number = await contract.retrieve();
14+
console.log("Number is ", number);
15+
};
16+
17+
main()
18+
.then(() => process.exit(0))
19+
.catch((error) => {
20+
console.error(error);
21+
process.exit(1);
22+
});
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { expect } = require("chai");
2+
const { ethers } = require("hardhat");
3+
4+
describe("ExtraStorage", function () {
5+
let contract;
6+
7+
before(async () => {
8+
const ExtraStorage = await ethers.getContractFactory("ExtraStorage");
9+
contract = await ExtraStorage.deploy();
10+
await contract.deployed();
11+
});
12+
13+
it("should call store and plus number + 5", async () => {
14+
const tx = await contract.store(10);
15+
tx.wait();
16+
17+
const number = await contract.retrieve();
18+
expect(number).to.equal(15);
19+
});
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { expect } = require("chai");
2+
const { ethers } = require("hardhat");
3+
4+
describe("SimpleStorage", function () {
5+
let contract;
6+
7+
before(async () => {
8+
const SimpleStorage = await ethers.getContractFactory("SimpleStorage");
9+
contract = await SimpleStorage.deploy();
10+
await contract.deployed();
11+
});
12+
13+
it("should return a new number once it's changed", async () => {
14+
const NUMBER = 20;
15+
const tx = await contract.store(NUMBER);
16+
await tx.wait();
17+
18+
const number = await contract.retrieve();
19+
expect(number).to.equal(NUMBER);
20+
});
21+
22+
it("should add a person and get number from a name", async () => {
23+
let name = "Chuck Norris";
24+
let number = 10;
25+
26+
const tx = await contract.addPerson(name, number);
27+
await tx.wait();
28+
29+
const expected = await contract.getNameToNumber(name);
30+
expect(expected).to.equal(number);
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { expect } = require("chai");
2+
const { ethers } = require("hardhat");
3+
4+
describe("StorageFactory", function () {
5+
let contract;
6+
7+
before(async () => {
8+
const StorageFactory = await ethers.getContractFactory("StorageFactory");
9+
contract = await StorageFactory.deploy();
10+
await contract.deployed();
11+
});
12+
13+
it("should set a a number with sfStore", async () => {
14+
const createTx = await contract.createSimpleStorageContract();
15+
createTx.wait();
16+
17+
const storeTx = await contract.sfStore(0, 20);
18+
storeTx.wait();
19+
20+
const result = await contract.sfGet(0);
21+
expect(result).to.equal(20);
22+
});
23+
});

0 commit comments

Comments
 (0)