Skip to content

Commit

Permalink
add contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
terrancrypt committed Sep 10, 2024
1 parent 643ef74 commit 58ceaff
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
3 changes: 3 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SEPOLIA_RPC_URL=
ETHERSCAN_API_KEY=
ACCOUNT=
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
12 changes: 12 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-include .env

ANVIL_PRIVATE_KEY := 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

NETWORK_ARGS := --rpc-url http://localhost:8545 --private-key $(ANVIL_PRIVATE_KEY) --broadcast

ifeq ($(findstring --network sepolia,$(ARGS)),--network sepolia)
NETWORK_ARGS := --rpc-url $(SEPOLIA_RPC_URL) --account $(ACCOUNT) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv
endif

deploy:
@forge script script/DeployMyToken.s.sol:DeployMyToken $(NETWORK_ARGS)
19 changes: 0 additions & 19 deletions script/Counter.s.sol

This file was deleted.

16 changes: 16 additions & 0 deletions script/DeployMyToken.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {Script} from "forge-std/Script.sol";
import {MyToken} from "src/MyToken.sol";

contract DeployMyToken is Script {
uint256 public constant INITIAL_SUPPLY = 10000 ether;

function run() external returns (MyToken) {
vm.startBroadcast();
MyToken myToken = new MyToken(INITIAL_SUPPLY);
vm.stopBroadcast();
return myToken;
}
}
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

10 changes: 10 additions & 0 deletions src/MyToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
constructor(uint256 initialSupply) ERC20("My Token", "MTK") {
_mint(msg.sender, initialSupply);
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

67 changes: 67 additions & 0 deletions test/MyTokenTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {Test} from "forge-std/Test.sol";
import {MyToken} from "src/MyToken.sol";
import {DeployMyToken} from "script/DeployMyToken.s.sol";
import {IERC20Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";

contract MyTokenTest is Test {
MyToken myToken;
DeployMyToken deployer;
uint256 public constant INITIAL_SUPPLY = 10000 ether;
address public constant USER = address(1);
address public constant SPENDER = address(2);

function setUp() external {
deployer = new DeployMyToken();
myToken = deployer.run();
}

function test_setUp() public view {
assertEq(myToken.balanceOf(msg.sender), INITIAL_SUPPLY);
}

// Direct transfer
function test_transfer() public {
uint256 balSenderBefore = myToken.balanceOf(msg.sender);
uint256 balUserBefore = myToken.balanceOf(USER);
uint256 transferAmount = 10 ether;

vm.prank(msg.sender);
myToken.transfer(USER, transferAmount);

uint256 balSenderAfter = myToken.balanceOf(msg.sender);
uint256 balUserAfter = myToken.balanceOf(USER);

assertEq(balSenderAfter, balSenderBefore - transferAmount);
assertEq(balUserAfter, balUserBefore + transferAmount);
}

// Indirect transfer
function test_transferFrom() public {
uint256 balSenderBefore = myToken.balanceOf(msg.sender);
uint256 balUserBefore = myToken.balanceOf(USER);
uint256 transferAmount = 10 ether;

// Revert if not approve
vm.expectRevert(
abi.encodeWithSelector(IERC20Errors.ERC20InsufficientAllowance.selector, SPENDER, 0, transferAmount)
);
vm.prank(SPENDER);
myToken.transferFrom(msg.sender, USER, transferAmount);

// Actual Transactions
vm.prank(msg.sender);
myToken.approve(SPENDER, transferAmount);

vm.prank(SPENDER);
myToken.transferFrom(msg.sender, USER, transferAmount);

uint256 balSenderAfter = myToken.balanceOf(msg.sender);
uint256 balUserAfter = myToken.balanceOf(USER);

assertEq(balSenderAfter, balSenderBefore - transferAmount);
assertEq(balUserAfter, balUserBefore + transferAmount);
}
}

0 comments on commit 58ceaff

Please sign in to comment.