The Counter Contract
Last updated
Last updated
Smart contracts are put at the contracts folder. Create the contract folder using:
mkdir contracts
Once created the contracts folder , create the Counter.sol
file with contents below:
pragma solidity >=0.5.0 <0.7.0;
contract Counter {
uint32 public current_value;
function inc() public {
require(current_value < 10000, "Counter: max value");
current_value = current_value + 1;
}
function dec() public {
require(current_value > 0, "Counter: min value");
current_value = current_value - 1;
}
}
The contract code is quite self explain:
A state variable current_value
which is an unsigned integer.
inc() method, which increase the current_value by one.
dec() method, which decrease the current_value by one.
current_value has a bound of [0, 10000] which was checked in inc and dec methods.
Run command truffle compile
, it will find and compiles the Counter contract. you should looks outputs like:
Compiling your contracts...
===========================
> Compiling ./contracts/Counter.sol
> Artifacts written to ~/counter-dapp/build/contracts
> Compiled successfully using:
- solc: 0.5.2+commit.1df8f40c.Emscripten.clang