Skip to content

Commit

Permalink
test: initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed Feb 15, 2024
1 parent cf87f5d commit c4ebcba
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 64 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install bun
uses: oven-sh/setup-bun@v1

- name: Install dependencies
run: bun i

- name: Run Forge build
run: |
forge --version
forge build --sizes
- name: Compile
run: bun compile --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
- name: Run tests
run: bun test
id: test
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

The `NAYM` ERC-20 token.

Features:

* Has an owner that can be changed (`0x0` not allowed).
* Has changeable minter that is set by owner. Only the minter can mint new tokens.
* Anyone can burn their own tokens.
* Supports meta/relay transactions.

## On-chain addresses

_(Live deployed addresses here)_
_TODO: Live deployed addresses here_

## Developer guide

Expand All @@ -19,10 +26,10 @@ Then run:
$ bun i
```

To build the contracts:
To compile the contracts:

```shell
$ bun build
$ bun compile
```

To test:
Expand All @@ -39,9 +46,7 @@ $ bun devnet

### Deployment

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```
_TODO: deployment script_

## License

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"prepare": "husky",
"build": "forge build",
"compile": "forge build",
"tests": "forge test",
"devnet": "anvil",
"commitlint": "commitlint --edit"
Expand Down
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

61 changes: 61 additions & 0 deletions src/NaymToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPLv3
pragma solidity ^0.8.21;

import { ERC20 } from "openzeppelin/token/ERC20/ERC20.sol";
import { Ownable } from "openzeppelin/access/Ownable.sol";

contract NaymToken is ERC20, Ownable {
/**
* @dev The caller account is not authorized to mint.
*/
error UnauthorizedMinter(address account);

/**
* @dev The minter can mint new tokens.
*/
address public minter;

/**
* @dev Throws if called by any account other than the minter.
*/
modifier onlyMinter() {
if(_msgSender() != minter) {
revert UnauthorizedMinter(_msgSender());
}
_;
}

/**
* @dev Constructor
* @param _owner The address of the initial owner of the contract.
* @param _minter The address of the initial minter of the contract.
*/
constructor(address _owner, address _minter) ERC20("Naym", "NAYM") Ownable(_owner) {
minter = _minter;
}

/**
* @dev The owner can set the minter.
* @param _minter The address of the new minter.
*/
function setMinter(address _minter) public onlyOwner {
minter = _minter;
}

/**
* @dev The minter can mint new tokens.
* @param _to The address to which the minted tokens will be sent.
* @param _amount The amount of tokens to mint.
*/
function mint(address _to, uint256 _amount) public onlyMinter {
_mint(_to, _amount);
}

/**
* @dev Burn one's own tokens.
* @param _amount The amount of tokens to burn.
*/
function burn(uint256 _amount) public {
_burn(_msgSender(), _amount);
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

77 changes: 77 additions & 0 deletions test/NaymToken.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: GPLv3
pragma solidity ^0.8.21;

import {Test, console} from "forge-std/Test.sol";
import { Vm } from "forge-std/Vm.sol";

import { NaymToken } from "../src/NaymToken.sol";
import { Ownable } from "openzeppelin/access/Ownable.sol";

contract NaymTokenTest is Test {
NaymToken public t;

address owner1 = address(0x111);
address owner2 = address(0x789);
address minter1 = address(0x123);
address minter2 = address(0x456);

function setUp() public {
t = new NaymToken(owner1, minter1);
}

function test_Init() public {
assertEq(t.name(), "Naym");
assertEq(t.symbol(), "NAYM");
assertEq(t.decimals(), 18);
assertEq(t.totalSupply(), 0);
assertEq(t.owner(), owner1);
assertEq(t.minter(), minter1);
}

function test_ChangeOwner() public {
// pass
vm.prank(owner1);
t.transferOwnership(owner2);
assertEq(t.owner(), owner2);

// not owner
vm.prank(owner1);
vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, owner1));
t.transferOwnership(owner1);

// invalid new owner
vm.prank(owner2);
vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableInvalidOwner.selector, address(0)));
t.transferOwnership(address(0));
}

function test_SetMinter() public {
// unauthorized
vm.prank(address(0x1234));
vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, address(0x1234)));
t.setMinter(minter2);

// authorized
vm.prank(owner1);
t.setMinter(minter2);
assertEq(t.minter(), minter2);

// can set to null address
vm.prank(owner1);
t.setMinter(address(0));
assertEq(t.minter(), address(0));
}

function test_Mint() public {
// unauthorized
vm.prank(address(0x1234));
vm.expectRevert(abi.encodeWithSelector(NaymToken.UnauthorizedMinter.selector, address(0x1234)));
t.mint(address(0x1234), 100);

// authorized
vm.prank(minter1);
t.mint(address(0x1234), 100);
assertEq(t.totalSupply(), 100);
assertEq(t.balanceOf(address(0x1234)), 100);
}
}

0 comments on commit c4ebcba

Please sign in to comment.