Skip to content

Your first contract

Create src/Counter.sol

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
contract Counter {
uint count;
constructor(uint _count) {
count = _count;
}
function increment() public {
count += 1;
}
function decrement() public {
count -= 1;
}
function getCount() public view returns (uint) {
return count;
}
}

Write the test for it (test/Counter.t.sol)

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "src/Counter.sol";
contract TestCounter is Test {
Counter c;
function setUp() public {
c = new Counter(0);
}
function testValue() public {
assertEq(c.getCount(), uint(0), "ok");
}
function testFailDecrement() public {
c.decrement();
}
}