forked from solangegueiros/chainlink-bootcamp-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenShop.sol
66 lines (54 loc) · 1.86 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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
// Deploy this contract on Sepolia
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
interface TokenInterface {
function mint(address account, uint256 amount) external;
}
contract TokenShop {
AggregatorV3Interface internal priceFeed;
TokenInterface public minter;
uint256 public tokenPrice = 200; //1 token = 2.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 answer
*/
function getChainlinkDataFeedLatestAnswer() 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(getChainlinkDataFeedLatestAnswer()); //with 8 decimal places
uint256 amountUSD = amountETH * ethUsd / 10**18; //ETH = 18 decimal places
uint256 amountToken = amountUSD / tokenPrice / 10**(8/2); //8 decimal places from ETHUSD / 2 decimal places from token
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);
}
}