-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calyptus431.sol
133 lines (113 loc) · 3.95 KB
/
Calyptus431.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
//Could you take a close look at this token swap contract and share any potential issues you identify?
contract TokenSwap {
IERC20 public token1;
IERC20 public token2;
uint256 public rate; // How many token2 for 1 token1
constructor(address _token1, address _token2, uint256 _rate) {
token1 = IERC20(_token1);
token2 = IERC20(_token2);
rate = _rate;
}
function swap(uint256 amount) external {
require(token1.transferFrom(msg.sender, address(this), amount), "Transfer of tokeni failed");
uint256 swapAmount = amount * rate;
require(token2.transfer(msg.sender, swapAmount), "Transfer of token2 failed");
}
function withdraw(address token, uint256 amount) external payable {
IERC20(token).transfer(msg.sender, amount);
}
}
contract Token1 is IERC20 {
uint256 public totalSupply = 1000;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name = "Token 1";
string public symbol = "TOK1";
uint8 public decimals = 18;
constructor() {
balanceOf[msg.sender] = totalSupply;
}
function transfer(address recipient, uint256 amount)
external
returns (bool)
{
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(msg.sender,recipient,amount);
return true;
}
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender,spender,amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount)
external
returns (bool)
{
balanceOf[sender] -= amount;
allowance[sender][msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
return true;
}
function mint(uint256 amount) external {
balanceOf[msg.sender] += amount;
totalSupply += amount;
emit Transfer(address(0), msg.sender, amount);
}
// function burn(uint256 amount) external {
// balanceOf[msg.sender] -= amount;
// totalSupply -= amount;
// emit Transfer(msg.sender, address(0), amount);
// }
}
contract Token2 is IERC20 {
uint256 public totalSupply = 1000;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name = "Token 2";
string public symbol = "TOK2";
uint8 public decimals = 18;
constructor() {
balanceOf[msg.sender] = totalSupply;
}
function transfer(address recipient, uint256 amount)
external
returns (bool)
{
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(msg.sender,recipient,amount);
return true;
}
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender,spender,amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount)
external
returns (bool)
{
balanceOf[sender] -= amount;
allowance[sender][msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
return true;
}
function mint(uint256 amount) external {
balanceOf[msg.sender] += amount;
totalSupply += amount;
emit Transfer(address(0), msg.sender, amount);
}
// function burn(uint256 amount) external {
// balanceOf[msg.sender] -= amount;
// totalSupply -= amount;
// emit Transfer(msg.sender, address(0), amount);
// }
}
// withdraw function is not protected