2. Write contract

2.1 Create Counter.sol

cd contracts
nano Counter.sol

copy this to counter.sol

// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;

/// @title A simple counter contract
contract Counter {
  uint32 private _count;

  /// @notice Returns the current count
  function getCount() external view returns (uint32) {
    return _count;
  }

  /// @notice Increments the counter by a specific value
  function increment(uint32 value) external {
    _count += value;
  }

  /// @notice Decrements the counter by a specific value
  function decrement(uint32 value) external {
    require(_count >= value, "Counter: cannot decrement below zero");
    _count -= value;
  }
}

2.2 Translation

=⇒ output :

2.3 Creat Counter.ts

coppy this to counter.ts

2.4. Run the first test

output :

2.5 Add signers

coppy this to counter.ts

run again

output :

2.6 add deploy fixture

coppy this to Counter.ts import { Counter, Counter__factory } from "../types";

run test again

output:

2.7 test getCount, increment, decrement

coppy this :

output :

Last updated