Description:
Smart contract deployed on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC20 {
function transfer(address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract TokenSale {
address public owner;
IERC20 public token;
uint256 public rate; // 1 ETH 당 토큰 개수 (token decimals 포함)
event TokensPurchased(address indexed buyer, uint256 ethAmount, uint256 tokenAmount);
event WithdrawnETH(address indexed to, uint256 amount);
event WithdrawnTokens(address indexed to, uint256 amount);
event RateUpdated(uint256 newRate);
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
uint256 private _status;
modifier nonReentrant() {
require(_status != 1, "Reentrancy detected");
_status = 1;
_;
_status = 0;
}
// 하드코딩된 토큰 주소와 rate (수정해서 사용하세요!)
address constant TOKEN_ADDRESS = 0xA01f98deFEA59e079d5Efd2676EC0D16BDfb994b;
uint256 constant INITIAL_RATE = 1000 * 1e18; // 1 ETH = 1000 토큰 (18 decimals 토큰 기준)
constructor() {
owner = msg.sender;
token = IERC20(TOKEN_ADDRESS);
rate = INITIAL_RATE;
_status = 0;
}
receive() external payable {
buyTokens();
}
function buyTokens() public payable nonReentrant {
require(msg.value > 0, "Send ETH to buy tokens");
uint256 tokenAmount = (msg.value * rate) / 1 ether;
require(token.balanceOf(address(this)) >= tokenAmount, "Not enough tokens in contract");
require(token.transfer(msg.sender, tokenAmount), "Token transfer failed");
emit TokensPurchased(msg.sender, msg.value, tokenAmount);
}
function setRate(uint256 newRate) external onlyOwner {
require(newRate > 0, "Rate must be > 0");
rate = newRate;
emit RateUpdated(newRate);
}
function withdrawETH(address payable to) external onlyOwner nonReentrant {
require(to != address(0), "Invalid address");
uint256 balance = address(this).balance;
require(balance > 0, "No ETH to withdraw");
(bool success, ) = to.call{value: balance}("");
require(success, "ETH transfer failed");
emit WithdrawnETH(to, balance);
}
function withdrawTokens(address to, uint256 amount) external onlyOwner nonReentrant {
require(to != address(0), "Invalid address");
require(token.balanceOf(address(this)) >= amount, "Not enough tokens");
require(token.transfer(to, amount), "Token transfer failed");
emit WithdrawnTokens(to, amount);
}
function withdrawAllTokens(address to) external onlyOwner nonReentrant {
require(to != address(0), "Invalid address");
uint256 amount = token.balanceOf(address(this));
require(amount > 0, "No tokens to withdraw");
require(token.transfer(to, amount), "Token transfer failed");
emit WithdrawnTokens(to, amount);
}
}
Submitted on: 2025-09-28 14:49:42
Comments
Log in to comment.
No comments yet.