-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathERC20Token-Contract.sol
47 lines (34 loc) · 1.41 KB
/
ERC20Token-Contract.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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.2 <0.9.0;
// Set path for SafeYorkERC20Token.sol to import
import "./SafeYorkERC20Token.sol";
contract DEX {
IERC20 public token;
event Bought(uint256 amount);
event Sold(uint256 amount);
constructor() {
token = new YorkERC20Token();
}
function buy() payable public {
// Caller can send Ether and get token and exchange 1 token for 1 Ether
uint256 etherAmount = msg.value;
uint256 tokenAmount = etherAmount;
// Check the amount
require(etherAmount > 0, "Error: You need to send some ether to buy tokens.");
require(token.balanceOf(address(this)) >= tokenAmount, "Error: Not enough tokens.");
// Emit the Transfer from YorkERC20Token.sol
// and Bought event from the DEX contract
token.transfer(msg.sender, tokenAmount);
emit Bought(tokenAmount);
}
function sell(uint256 amount) public {
// Check the amount
require(token.balanceOf(msg.sender) >= amount, "Error: You don't have enough tokens to sell. Buy Ether first to get tokens!");
// Caller can send token back in exchange for Ether
// Check if the transfer from the caller address to the contract address
token.transferFrom(msg.sender, address(this), amount);
payable(msg.sender).transfer(amount);
// Emit
emit Sold(amount);
}
}