-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenShop.sol
119 lines (53 loc) · 2 KB
/
TokenShop.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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
interface TokenInterface {
function mint(address account, uint256 amount) external returns (bool);
}
contract TokenShop {
AggregatorV3Interface internal priceFeed;
TokenInterface public minter;
uint256 public tokenPrice = 2000; //1 token = 20.00 usd, with 2 decimal places
address public owner;
constructor(address tokenAddress) {
minter = TokenInterface(tokenAddress);
/**
* Network: Sepolia
* Aggregator: ETH/USD
* Address: 0x694AA1769357215DE4FAC081bf1f309aDC325306
*/
priceFeed = AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306);
owner = msg.sender;
}
/**
* Returns the latest price
*/
function getLatestPrice() public view returns (int) {
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return price;
}
function tokenAmount(uint256 amountETH) public view returns (uint256) {
//Sent amountETH, how many usd I have
uint256 ethUsd = uint256(getLatestPrice());
uint256 amountUSD = amountETH * ethUsd / 1000000000000000000; //ETH = 18 decimal places
uint256 amountToken = amountUSD / tokenPrice / 100; //2 decimal places
return amountToken;
}
receive() external payable {
uint256 amountToken = tokenAmount(msg.value);
minter.mint(msg.sender, amountToken);
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function withdraw() external onlyOwner {
payable(owner).transfer(address(this).balance);
}
}