From 6e5c9bed4dee1dfa8364d8a47d1785e0e0645c2e Mon Sep 17 00:00:00 2001 From: Estefi Date: Fri, 10 Jun 2022 10:47:23 -0300 Subject: [PATCH] added instructions to Build and test a contract with Forge --- README.md | 45 +++++++++++++++++++++++++- contracts/src/simplestorage.sol | 11 +++++++ contracts/src/test/simplestorage.t.sol | 18 +++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 contracts/src/simplestorage.sol create mode 100644 contracts/src/test/simplestorage.t.sol diff --git a/README.md b/README.md index c46c0d7..0068071 100644 --- a/README.md +++ b/README.md @@ -150,8 +150,51 @@ Node 2 miner account: - Address: `0x77b648683cde1d69544ed6f4f7204e8d51c324db` - Private key: `f71d3dd32649f9bdfc8e4a5232d3f245860243756f96fbe070c31fc44c9293f4` +## Build a contract with Forge -## Test contract +Forge belongs to Foundry, wich it’s a reimplementation of dapptools, a command line tools and smart contract libraries for Ethereum smart contract development. Forge lets us write our tests in Solidity. + +### Dependencies + +**Install Rust** + +``` +asdf install rust 1.59.0 +``` + +**Install Foundry** + +``` +curl -L https://foundry.paradigm.xyz | bash +``` + +Then run `foundryup` in your terminal + +``` +foundryup +``` + +### Build a contract + +To build contracts you need to run: + +``` +forge build --contracts contracts/src/contract.sol +``` + +> We have a proof contract to test that `contracts/src/simplestorage.sol` + +### Test contracts + +To test a contract you need to run: + +``` +forge test --contracts contracts/src/test/contract.t.sol +``` + +> We have a proof test contract to test that `contracts/src/simplestorage.t.sol` + +## Deploy test contract You can then deploy the test contract with diff --git a/contracts/src/simplestorage.sol b/contracts/src/simplestorage.sol new file mode 100644 index 0000000..905b7d0 --- /dev/null +++ b/contracts/src/simplestorage.sol @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.4.16 <0.9.0; +contract SimpleStorage { + uint storedData; +function set(uint x) public { + storedData = x; + } +function get() public view returns (uint retVal) { + return storedData; + } +} diff --git a/contracts/src/test/simplestorage.t.sol b/contracts/src/test/simplestorage.t.sol new file mode 100644 index 0000000..cac4e35 --- /dev/null +++ b/contracts/src/test/simplestorage.t.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.4.16 <0.9.0; +import "../../lib/ds-test/src/test.sol"; +import "../simplestorage.sol"; +contract SimpleStorageTest is DSTest { + SimpleStorage simplestorage; +function setUp() public { + simplestorage = new SimpleStorage(); + } +function testGetInitialValue() public { + assertTrue(simplestorage.get() == 0); + } +function testSetValue() public { + uint x = 300; + simplestorage.set(x); + assertTrue(simplestorage.get() == 300); + } +}