Skip to content

Commit 89250ee

Browse files
committed
Example of fail
1 parent e207a34 commit 89250ee

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

contracts/ERC20.sol

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.23;
3+
4+
contract ERC20 {
5+
string public name;
6+
string public symbol;
7+
uint8 public constant decimals = 18;
8+
9+
uint256 public totalSupply;
10+
mapping(address => uint256) public balanceOf;
11+
mapping(address => mapping(address => uint256)) public allowance;
12+
13+
event Transfer(address indexed from, address indexed to, uint256 value);
14+
event Approval(address indexed owner, address indexed spender, uint256 value);
15+
16+
constructor(string memory _name, string memory _symbol, uint256 _initialSupply) {
17+
name = _name;
18+
symbol = _symbol;
19+
_mint(msg.sender, _initialSupply);
20+
}
21+
22+
function transfer(address to, uint256 amount) external returns (bool) {
23+
_transfer(msg.sender, to, amount);
24+
return true;
25+
}
26+
27+
function approve(address spender, uint256 amount) external returns (bool) {
28+
allowance[msg.sender][spender] = amount;
29+
emit Approval(msg.sender, spender, amount);
30+
return true;
31+
}
32+
33+
function transferFrom(address from, address to, uint256 amount) external returns (bool) {
34+
uint256 allowed = allowance[from][msg.sender];
35+
require(allowed >= amount, "ERC20: insufficient allowance");
36+
unchecked {
37+
allowance[from][msg.sender] = allowed - amount;
38+
}
39+
_transfer(from, to, amount);
40+
return true;
41+
}
42+
43+
function _transfer(address from, address to, uint256 amount) internal {
44+
require(to != address(0), "ERC20: transfer to zero");
45+
uint256 fromBalance = balanceOf[from];
46+
require(fromBalance >= amount, "ERC20: insufficient balance");
47+
unchecked {
48+
balanceOf[from] = fromBalance - amount;
49+
balanceOf[to] += amount;
50+
}
51+
emit Transfer(from, to, amount);
52+
}
53+
54+
function _mint(address to, uint256 amount) internal {
55+
require(to != address(0), "ERC20: mint to zero");
56+
totalSupply += amount;
57+
balanceOf[to] += amount;
58+
emit Transfer(address(0), to, amount);
59+
}
60+
}

contracts/Example.sol

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.23;
33

4+
import { ERC20 } from "./ERC20.sol";
5+
46
contract Example
57
{
6-
address public weth;
7-
constructor(address _weth)
8+
string public name;
9+
10+
constructor(ERC20 token)
811
{
9-
weth = _weth;
12+
name = token.name();
1013
}
1114
}

0 commit comments

Comments
 (0)