Skip to content

Instantly share code, notes, and snippets.

@rafat
Created January 12, 2024 15:11
Show Gist options
  • Save rafat/d2e6dfc8d0af98fa5a778c563924119c to your computer and use it in GitHub Desktop.
Save rafat/d2e6dfc8d0af98fa5a778c563924119c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.19+commit.7dd6d404.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment