Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[83] - Added instructions to Build and test a contract with Forge #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions contracts/src/simplestorage.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions contracts/src/test/simplestorage.t.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}