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 to, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
function decimals() external view returns (uint8);
}
contract ETRXFixedPriceSale {
address public owner;
IERC20 public token;
// tokens per 1 ETH (whole tokens, not 1e18 scaled)
uint256 public rate;
uint256 public weiRaised;
bool public live;
event Contributed(address indexed buyer, uint256 weiAmount, uint256 tokensOut);
event WithdrawETH(address to, uint256 amount);
event WithdrawUnsold(address to, uint256 amount);
modifier onlyOwner(){ require(msg.sender == owner, "not owner"); _; }
constructor(address _token, uint256 _rate) {
owner = msg.sender;
token = IERC20(_token);
rate = _rate; // e.g., 40000 means 1 ETH -> 40,000 ETRX
}
function setLive(bool _live) external onlyOwner { live = _live; }
function setRate(uint256 _rate) external onlyOwner { rate = _rate; }
receive() external payable { contribute(); }
function contribute() public payable {
require(live, "presale not live");
require(msg.value > 0, "no ETH");
uint8 d = token.decimals(); // expected 18; still safe if not
// tokensOut = ETH_in_wei * rate / 1 ether * 10^decimals
uint256 tokensOut = (msg.value * rate * (10 ** d)) / 1 ether;
require(token.balanceOf(address(this)) >= tokensOut, "insufficient ETRX in sale");
weiRaised += msg.value;
require(token.transfer(msg.sender, tokensOut), "token transfer failed");
emit Contributed(msg.sender, msg.value, tokensOut);
}
function withdrawETH(address payable to) external onlyOwner {
uint256 bal = address(this).balance;
(bool ok,) = to.call{value: bal}("");
require(ok, "withdraw failed");
emit WithdrawETH(to, bal);
}
function withdrawUnsold(address to, uint256 amount) external onlyOwner {
require(token.transfer(to, amount), "withdraw unsold failed");
emit WithdrawUnsold(to, amount);
}
}
Submitted on: 2025-09-24 18:27:26
Comments
Log in to comment.
No comments yet.