From fbec1b9afc1bcd137d03c35f276c22a9abddb3ac Mon Sep 17 00:00:00 2001 From: hakymulla Date: Thu, 27 Jun 2024 08:23:49 +0100 Subject: [PATCH 1/3] Mock Test Contract --- .gitmodules | 3 +++ ethereum/foundry.toml | 5 ++-- ethereum/script/DeployMockStorage.s.sol | 28 ++++++++++++++++++++++ ethereum/src/Mock/MockStorage.sol | 32 +++++++++++++++++++++++++ ethereum/test/MockStorageTest.t.sol | 27 +++++++++++++++++++++ 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 100644 ethereum/script/DeployMockStorage.s.sol create mode 100644 ethereum/src/Mock/MockStorage.sol create mode 100644 ethereum/test/MockStorageTest.t.sol diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..9abc38c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ethereum/lib/forge-std"] + path = ethereum/lib/forge-std + url = https://github.com/foundry-rs/forge-std diff --git a/ethereum/foundry.toml b/ethereum/foundry.toml index bd0dbc7..3726fe5 100644 --- a/ethereum/foundry.toml +++ b/ethereum/foundry.toml @@ -3,6 +3,7 @@ src = "src" out = "out" libs = ["lib"] fs_permissions = [{ access = "read-write", path = "./logs" }] +remappings = ["@forge-std/=lib/forge-std/"] -[dependencies] # <= Dependencies will be added under this config -forge-std = { version = "1.8.2", url = "https://github.com/foundry-rs/foundry.git" } +[profile.dependencies] # <= Dependencies will be added under this config +forge-std = { version = "1.8.2", url = "https://github.com/foundry-rs/foundry.git" } \ No newline at end of file diff --git a/ethereum/script/DeployMockStorage.s.sol b/ethereum/script/DeployMockStorage.s.sol new file mode 100644 index 0000000..5680a6c --- /dev/null +++ b/ethereum/script/DeployMockStorage.s.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +import {Script} from "@forge-std/src/Script.sol"; +import {MockStorage} from "../src/Mock/MockStorage.sol"; + +contract DeployMockStorage is Script{ + + uint256 public val1 = 1000; + uint256 public val2 = 2000; + address public addr = 0x9D7f74d0C41E726EC95884E0e97Fa6129e3b5E99; + address public addr2 = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; + + function run() external returns (MockStorage) { + + vm.startBroadcast(); + MockStorage mockStorage = new MockStorage(); + mockStorage.setValue(val1); + mockStorage.setMapValues(val1, addr); + mockStorage.setMapValues(val2, addr2); + mockStorage.setArrValues(addr); + mockStorage.setArrValues(addr2); + vm.stopBroadcast(); + + return mockStorage; + } + +} \ No newline at end of file diff --git a/ethereum/src/Mock/MockStorage.sol b/ethereum/src/Mock/MockStorage.sol new file mode 100644 index 0000000..c935cdd --- /dev/null +++ b/ethereum/src/Mock/MockStorage.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +contract MockStorage { + uint256 public value; + mapping(uint256 => address) public mapValues; + address[] public arrValues; + + function setValue(uint256 _value) public { + value = _value; + } + + function getValue() public view returns (uint256) { + return value; + } + + function setMapValues(uint256 _value, address _add) public { + mapValues[_value] = _add; + } + + function getMapValues(uint256 _value) public view returns (address) { + return mapValues[_value]; + } + + function setArrValues(address _value) public { + arrValues.push(_value); + } + + function getArrValues(uint256 i) public view returns (address) { + return arrValues[i]; + } +} diff --git a/ethereum/test/MockStorageTest.t.sol b/ethereum/test/MockStorageTest.t.sol new file mode 100644 index 0000000..97849d1 --- /dev/null +++ b/ethereum/test/MockStorageTest.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +import {Script} from "@forge-std/src/Script.sol"; +import {MockStorage} from "../src/Mock/MockStorage.sol"; +import {DeployMockStorage} from "../script/DeployMockStorage.s.sol"; +import {Test, console} from "@forge-std/src/Test.sol"; + +contract MockStorageTest is Test{ + MockStorage mockStorage; + + function setUp() external { + DeployMockStorage deployer = new DeployMockStorage(); + mockStorage = deployer.run(); + } + + + function testMockStorage() public view { + + assert( mockStorage.getValue() == 1000); + assert( mockStorage.getMapValues(1000) == 0x9D7f74d0C41E726EC95884E0e97Fa6129e3b5E99); + assert( mockStorage.getMapValues(2000) == 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4); + assert( mockStorage.getArrValues(0) == 0x9D7f74d0C41E726EC95884E0e97Fa6129e3b5E99); + assert( mockStorage.getArrValues(1) == 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4); + + } +} \ No newline at end of file From 76d8836a0e9f87768b67dc47e183dbec53c575d4 Mon Sep 17 00:00:00 2001 From: hakymulla Date: Tue, 2 Jul 2024 07:19:34 +0100 Subject: [PATCH 2/3] updated toml file --- ethereum/foundry.toml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ethereum/foundry.toml b/ethereum/foundry.toml index 3726fe5..9038c8b 100644 --- a/ethereum/foundry.toml +++ b/ethereum/foundry.toml @@ -1,9 +1,12 @@ + +# Full reference https://github.com/foundry-rs/foundry/tree/master/crates/config + [profile.default] +script = "script" +solc = "0.8.26" src = "src" -out = "out" -libs = ["lib"] -fs_permissions = [{ access = "read-write", path = "./logs" }] -remappings = ["@forge-std/=lib/forge-std/"] +test = "test" +libs = ["dependencies"] -[profile.dependencies] # <= Dependencies will be added under this config +[dependencies] forge-std = { version = "1.8.2", url = "https://github.com/foundry-rs/foundry.git" } \ No newline at end of file From fda38f9ed79073679f7f5978cffb7ae18506d51b Mon Sep 17 00:00:00 2001 From: hakymulla Date: Tue, 2 Jul 2024 09:30:51 +0100 Subject: [PATCH 3/3] MockStorage update on README --- ethereum/README.md | 7 +++++++ ethereum/foundry.toml | 1 + ethereum/script/DeployMockStorage.s.sol | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ethereum/README.md b/ethereum/README.md index 9265b45..5349919 100644 --- a/ethereum/README.md +++ b/ethereum/README.md @@ -51,6 +51,13 @@ $ anvil $ forge script script/Counter.s.sol:CounterScript --rpc-url --private-key ``` +### Deploy MockStorage Contract + +```shell +$ forge script script/DeployMockStorage.s.sol:DeployMockStorage --broadcast --rpc-url ${ETH_RPC_URL} +``` + + ### Cast ```shell diff --git a/ethereum/foundry.toml b/ethereum/foundry.toml index 9038c8b..17d8832 100644 --- a/ethereum/foundry.toml +++ b/ethereum/foundry.toml @@ -7,6 +7,7 @@ solc = "0.8.26" src = "src" test = "test" libs = ["dependencies"] +fs_permissions = [{ access = "read-write", path = "./logs" }] [dependencies] forge-std = { version = "1.8.2", url = "https://github.com/foundry-rs/foundry.git" } \ No newline at end of file diff --git a/ethereum/script/DeployMockStorage.s.sol b/ethereum/script/DeployMockStorage.s.sol index 5680a6c..b5b5497 100644 --- a/ethereum/script/DeployMockStorage.s.sol +++ b/ethereum/script/DeployMockStorage.s.sol @@ -13,7 +13,9 @@ contract DeployMockStorage is Script{ function run() external returns (MockStorage) { - vm.startBroadcast(); + uint256 deployerPrivateKey = vm.envUint("ACCOUNT_PRIVATE_KEY"); + + vm.startBroadcast(deployerPrivateKey); MockStorage mockStorage = new MockStorage(); mockStorage.setValue(val1); mockStorage.setMapValues(val1, addr);