-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
643ef74
commit 58ceaff
Showing
10 changed files
with
112 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SEPOLIA_RPC_URL= | ||
ETHERSCAN_API_KEY= | ||
ACCOUNT= |
Submodule openzeppelin-contracts
added at
dbb610
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |