From ce127dbc28db47b50fb6a834bf87aea7fa2732a9 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 17 Dec 2024 10:49:22 +0700 Subject: [PATCH] add SimpleStorage smart contract --- .gitmodules | 3 ++ hello_foundry/script/Counter.s.sol | 19 ----------- hello_foundry/src/Counter.sol | 14 -------- hello_foundry/test/Counter.t.sol | 24 -------------- .../.github/workflows/test.yml | 0 {hello_foundry => simple-storage}/.gitignore | 0 {hello_foundry => simple-storage}/README.md | 0 simple-storage/README_PRVKEY_BEST_PRACTICE.md | 21 ++++++++++++ .../foundry.toml | 0 .../lib/forge-std | 0 .../script/DeploySimpleStorage.s.sol | 15 +++++++++ simple-storage/src/SimpleStorage.sol | 33 +++++++++++++++++++ 12 files changed, 72 insertions(+), 57 deletions(-) delete mode 100644 hello_foundry/script/Counter.s.sol delete mode 100644 hello_foundry/src/Counter.sol delete mode 100644 hello_foundry/test/Counter.t.sol rename {hello_foundry => simple-storage}/.github/workflows/test.yml (100%) rename {hello_foundry => simple-storage}/.gitignore (100%) rename {hello_foundry => simple-storage}/README.md (100%) create mode 100644 simple-storage/README_PRVKEY_BEST_PRACTICE.md rename {hello_foundry => simple-storage}/foundry.toml (100%) rename {hello_foundry => simple-storage}/lib/forge-std (100%) create mode 100644 simple-storage/script/DeploySimpleStorage.s.sol create mode 100644 simple-storage/src/SimpleStorage.sol diff --git a/.gitmodules b/.gitmodules index 31c5df6..1631302 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "hello_foundry/lib/forge-std"] path = hello_foundry/lib/forge-std url = https://github.com/foundry-rs/forge-std +[submodule "simple-storage/lib/forge-std"] + path = simple-storage/lib/forge-std + url = https://github.com/foundry-rs/forge-std diff --git a/hello_foundry/script/Counter.s.sol b/hello_foundry/script/Counter.s.sol deleted file mode 100644 index cdc1fe9..0000000 --- a/hello_foundry/script/Counter.s.sol +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {Script, console} from "forge-std/Script.sol"; -import {Counter} from "../src/Counter.sol"; - -contract CounterScript is Script { - Counter public counter; - - function setUp() public {} - - function run() public { - vm.startBroadcast(); - - counter = new Counter(); - - vm.stopBroadcast(); - } -} diff --git a/hello_foundry/src/Counter.sol b/hello_foundry/src/Counter.sol deleted file mode 100644 index aded799..0000000 --- a/hello_foundry/src/Counter.sol +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -contract Counter { - uint256 public number; - - function setNumber(uint256 newNumber) public { - number = newNumber; - } - - function increment() public { - number++; - } -} diff --git a/hello_foundry/test/Counter.t.sol b/hello_foundry/test/Counter.t.sol deleted file mode 100644 index 54b724f..0000000 --- a/hello_foundry/test/Counter.t.sol +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {Test, console} from "forge-std/Test.sol"; -import {Counter} from "../src/Counter.sol"; - -contract CounterTest is Test { - Counter public counter; - - function setUp() public { - counter = new Counter(); - counter.setNumber(0); - } - - function test_Increment() public { - counter.increment(); - assertEq(counter.number(), 1); - } - - function testFuzz_SetNumber(uint256 x) public { - counter.setNumber(x); - assertEq(counter.number(), x); - } -} diff --git a/hello_foundry/.github/workflows/test.yml b/simple-storage/.github/workflows/test.yml similarity index 100% rename from hello_foundry/.github/workflows/test.yml rename to simple-storage/.github/workflows/test.yml diff --git a/hello_foundry/.gitignore b/simple-storage/.gitignore similarity index 100% rename from hello_foundry/.gitignore rename to simple-storage/.gitignore diff --git a/hello_foundry/README.md b/simple-storage/README.md similarity index 100% rename from hello_foundry/README.md rename to simple-storage/README.md diff --git a/simple-storage/README_PRVKEY_BEST_PRACTICE.md b/simple-storage/README_PRVKEY_BEST_PRACTICE.md new file mode 100644 index 0000000..4f73f85 --- /dev/null +++ b/simple-storage/README_PRVKEY_BEST_PRACTICE.md @@ -0,0 +1,21 @@ +# Private Key Best Practice + +Utilize `cast wallet` + +```bash +cast wallet import nameOfWalletOrAddress --interactive +``` + +Then run everything with private key from account + +Previously: + +```bash +forge script script/DeploySimpleStorage.s.sol --rpc-url $RPC_URL --broadcast --private-key $PRIVATE_KEY +``` + +Now: + +```bash +forge script script/DeploySimpleStorage.s.sol --rpc-url http://127.0.0.1:8545 --broadcast --account nameOfAccountGoesHere --sender 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 +``` diff --git a/hello_foundry/foundry.toml b/simple-storage/foundry.toml similarity index 100% rename from hello_foundry/foundry.toml rename to simple-storage/foundry.toml diff --git a/hello_foundry/lib/forge-std b/simple-storage/lib/forge-std similarity index 100% rename from hello_foundry/lib/forge-std rename to simple-storage/lib/forge-std diff --git a/simple-storage/script/DeploySimpleStorage.s.sol b/simple-storage/script/DeploySimpleStorage.s.sol new file mode 100644 index 0000000..70e60ad --- /dev/null +++ b/simple-storage/script/DeploySimpleStorage.s.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.19; + +import {Script} from "forge-std/Script.sol"; +import {SimpleStorage} from "../src/SimpleStorage.sol"; + +contract DeploySimpleStorage is Script { + function run() external returns (SimpleStorage) { + vm.startBroadcast(); + SimpleStorage simpleStorage = new SimpleStorage(); + vm.stopBroadcast(); + return simpleStorage; + } +} diff --git a/simple-storage/src/SimpleStorage.sol b/simple-storage/src/SimpleStorage.sol new file mode 100644 index 0000000..0135a9e --- /dev/null +++ b/simple-storage/src/SimpleStorage.sol @@ -0,0 +1,33 @@ +// I'm a comment! +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.19; + +// pragma solidity ^0.8.0; +// pragma solidity >=0.8.0 <0.9.0; + +contract SimpleStorage { + uint256 myFavoriteNumber; + + struct Person { + uint256 favoriteNumber; + string name; + } + // uint256[] public anArray; + Person[] public listOfPeople; + + mapping(string => uint256) public nameToFavoriteNumber; + + function store(uint256 _favoriteNumber) public { + myFavoriteNumber = _favoriteNumber; + } + + function retrieve() public view returns (uint256) { + return myFavoriteNumber; + } + + function addPerson(string memory _name, uint256 _favoriteNumber) public { + listOfPeople.push(Person(_favoriteNumber, _name)); + nameToFavoriteNumber[_name] = _favoriteNumber; + } +}