-
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
Solidity Pragma
authored and
Solidity Pragma
committed
Oct 18, 2024
1 parent
ff12567
commit 8766910
Showing
11 changed files
with
215 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,47 @@ | ||
-include .env | ||
|
||
.PHONY: all test clean deploy fund help install snapshot format anvil | ||
|
||
DEFAULT_ANVIL_KEY := 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 | ||
|
||
help: | ||
@echo "Usage:" | ||
@echo " make deploy [ARGS=...]\n example: make deploy ARGS=\"--network sepolia\"" | ||
@echo "" | ||
@echo " make fund [ARGS=...]\n example: make deploy ARGS=\"--network sepolia\"" | ||
|
||
all: clean remove install update build | ||
|
||
# Clean the repo | ||
clean :; forge clean | ||
|
||
# Remove modules | ||
remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules" | ||
|
||
install :; forge install Cyfrin/[email protected] --no-commit --no-commit && forge install foundry-rs/[email protected] --no-commit && forge install openzeppelin/[email protected] --no-commit | ||
|
||
# Update Dependencies | ||
update:; forge update | ||
|
||
build:; forge build | ||
|
||
test :; forge test | ||
|
||
snapshot :; forge snapshot | ||
|
||
format :; forge fmt | ||
|
||
anvil :; anvil -m 'test test test test test test test test test test test junk' --steps-tracing --block-time 1 | ||
|
||
NETWORK_ARGS := --rpc-url http://localhost:8545 --private-key $(DEFAULT_ANVIL_KEY) --broadcast | ||
|
||
ifeq ($(findstring --network sepolia,$(ARGS)),--network sepolia) | ||
NETWORK_ARGS := --rpc-url $(SEPOLIA_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv | ||
endif | ||
|
||
deploy: | ||
@forge script script/DeployOurToken.s.sol:DeployOurToken $(NETWORK_ARGS) | ||
|
||
|
||
verify: | ||
@forge verify-contract --chain-id 11155111 --num-of-optimizations 200 --watch --constructor-args 0x00000000000000000000000000000000000000000000d3c21bcecceda1000000 --etherscan-api-key $(ETHERSCAN_API_KEY) --compiler-version v0.8.19+commit.7dd6d404 0x089dc24123e0a27d44282a1ccc2fd815989e3300 src/OurToken.sol:OurToken |
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
Submodule openzeppelin-contracts
added at
dbb610
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.18; | ||
|
||
import {Script} from "forge-std/Script.sol"; | ||
import {OurToken} from "../src/OurToken.sol"; | ||
|
||
contract DeployOurToken is Script { | ||
uint256 public constant INITIAL_SUPPLY = 1000 ether; | ||
|
||
function run() external returns (OurToken) { | ||
vm.startBroadcast(); | ||
OurToken ot = new OurToken(INITIAL_SUPPLY); | ||
vm.stopBroadcast(); | ||
return ot; | ||
} | ||
} |
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,31 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.18; | ||
|
||
contract ManualToken { | ||
|
||
mapping(address => uint256) private s_balances; | ||
|
||
function name() public pure returns(string memory) { | ||
return "Manual Token"; | ||
} | ||
|
||
function totalSupply() public pure returns (uint256){ | ||
return 100 ether; // 100000000000000000000 | ||
} | ||
|
||
function decimals() public pure returns (uint8) { | ||
return 18; | ||
} | ||
|
||
function balanceOf(address _owner) public view returns (uint256) { | ||
return s_balances[_owner]; | ||
} | ||
|
||
function transfer(address _to, uint256 _amount) public { | ||
uint256 previousBalances = balanceOf(msg.sender) + balanceOf(_to); | ||
s_balances[msg.sender] -= _amount; | ||
s_balances[_to] += _amount; | ||
require(balanceOf(msg.sender) + balanceOf(_to) == previousBalances); | ||
} | ||
|
||
} |
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.18; | ||
|
||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
contract OurToken is ERC20 { | ||
constructor(uint256 initialSupply) ERC20("OurToken", "OT"){ /** ERC20("OurToken", "OT")の部分はERC20というベースコントラクトのコンストラクタにトークンの名前とシンボルを渡している */ | ||
_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,106 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.19; | ||
|
||
import {DeployOurToken} from "../script/DeployOurToken.s.sol"; | ||
import {OurToken} from "../src/OurToken.sol"; | ||
import {Test, console} from "forge-std/Test.sol"; | ||
import {StdCheats} from "forge-std/StdCheats.sol"; | ||
|
||
interface MintableToken { | ||
function mint(address, uint256) external; | ||
} | ||
|
||
contract OurTokenTest is StdCheats, Test { | ||
OurToken public ourToken; | ||
DeployOurToken public deployer; | ||
|
||
address bob = makeAddr("bob"); | ||
address alice = makeAddr("alice"); | ||
|
||
uint256 public constant STARTING_BALANCE = 100 ether; | ||
|
||
function setUp() public { | ||
deployer = new DeployOurToken(); | ||
ourToken = deployer.run(); | ||
vm.prank(msg.sender); | ||
ourToken.transfer(bob, STARTING_BALANCE); | ||
} | ||
|
||
function testBobBlance() public { | ||
assertEq(STARTING_BALANCE, ourToken.balanceOf(bob)); | ||
} | ||
|
||
function testAllowancesWorks() public { | ||
uint256 initialAllowance = 1000; | ||
// Bob approve Alice to spend token on her behalf | ||
vm.prank(bob); | ||
ourToken.approve(alice, initialAllowance); | ||
|
||
uint256 transferAmount = 500; | ||
|
||
vm.prank(alice); | ||
ourToken.transferFrom(bob, alice, transferAmount); | ||
|
||
assertEq(ourToken.balanceOf(alice), transferAmount); | ||
assertEq(ourToken.balanceOf(bob), STARTING_BALANCE - transferAmount); | ||
} | ||
function testInitialSupply() public { | ||
assertEq(ourToken.totalSupply(), deployer.INITIAL_SUPPLY()); | ||
} | ||
|
||
function testUsersCantMint() public { | ||
vm.expectRevert(); | ||
MintableToken(address(ourToken)).mint(address(this), 1); | ||
} | ||
|
||
|
||
|
||
function testBlanceAfterTransfer() public { | ||
uint256 amount = 1000; | ||
address receiver = address(0x1); | ||
uint256 initialBalance = ourToken.balanceOf(msg.sender); | ||
vm.prank(msg.sender); | ||
ourToken.transfer(receiver, amount); | ||
assertEq(ourToken.balanceOf(msg.sender), initialBalance - amount); | ||
} | ||
|
||
function testTransferFrom() public { | ||
uint256 amount = 1000; | ||
address receiver = address(0x1); | ||
vm.prank(msg.sender); | ||
ourToken.approve(address(this), amount); | ||
ourToken.transferFrom(msg.sender, receiver, amount); | ||
assertEq(ourToken.balanceOf(receiver), amount); | ||
} | ||
|
||
function testFailTransferExceedsBalance() public { | ||
uint256 amount = deployer.INITIAL_SUPPLY() + 1; | ||
vm.prank(msg.sender); | ||
ourToken.transfer(bob, amount); // This should fail | ||
} | ||
|
||
function testFailApproveExceedsBalance() public { | ||
uint256 amount = deployer.INITIAL_SUPPLY() + 1; | ||
vm.prank(msg.sender); | ||
ourToken.approve(bob, amount); // This should fail | ||
vm.prank(bob); | ||
ourToken.transferFrom(msg.sender, bob, amount); | ||
} | ||
|
||
// function testTransferEvent() public { | ||
// uint256 amount = 1000 * 10 ** 18; // Example amount | ||
// vm.prank(msg.sender); | ||
// vm.expectEmit(true, true, false, true); | ||
// emit Transfer(msg.sender, bob, amount); | ||
// ourToken.transfer(bob, amount); | ||
// } | ||
|
||
// function testApprovalEvent() public { | ||
// uint256 amount = 1000 * 10 ** 18; // Example amount | ||
// vm.prank(msg.sender); | ||
// vm.expectEmit(true, true, false, true); | ||
// emit Approval(msg.sender, bob, amount); | ||
// ourToken.approve(bob, amount); | ||
// } | ||
} |