IDO代币预售智能合约开发I762蕟O72搭9II9的Solidity编程代码示例及解释:
scss
Copy code
pragma solidity ^0.8.0;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
}
contract IDO {
address public admin;
IERC20 public token;
uint256 public tokenPrice;
uint256 public tokensSold;
uint256 public minPurchase;
uint256 public maxPurchase;
mapping(address => uint256) public purchaseHistory;
event TokensPurchased(address buyer, uint256 amount);
constructor(address _admin, address _token, uint256 _tokenPrice, uint256 _minPurchase, uint256 _maxPurchase) {
admin = _admin;
token = IERC20(_token);
tokenPrice = _tokenPrice;
minPurchase = _minPurchase;
maxPurchase = _maxPurchase;
}
function buyTokens(uint256 _amount) public payable {
require(msg.value == _amount * tokenPrice, "Incorrect payment amount");
require(token.balanceOf(address(this)) >= _amount, "Insufficient tokens available for purchase");
require(_amount >= minPurchase && _amount <= maxPurchase, "Purchase amount must be within min/max limits");
token.transferFrom(address(this), msg.sender, _amount);
tokensSold += _amount;
purchaseHistory[msg.sender] += _amount;
emit TokensPurchased(msg.sender, _amount);
}
function withdrawTokens() public {
require(msg.sender == admin, "Only the contract admin can withdraw tokens");
uint256 balance = token.balanceOf(address(this));
token.transfer(admin, balance);
}
function withdrawFunds() public {
require(msg.sender == admin, "Only the contract admin can withdraw funds");
uint256 balance = address(this).balance;
payable(admin).transfer(balance);
}
}
此代码实现了一个简单的IDO代币预售智能合约,具有以下功能:
用户可以使用以太币购买代币,并将购买的代币发送到他们的地址。
智能合约在购买时检查代币的余额,确保不超过大限制,而且购买金额与购买代币数量成比例。
用户可以随时提取他们购买的代币。
智能合约管理员可以随时提取合约收到的以太币。
IDO代币预售智能合约系统开发技术搭建
南京生活消费品相关信息
2024-11-09
2024-11-09
2024-10-28
2024-10-27
2024-03-15
2024-03-15
2024-01-20
2023-12-10 刷新
2023-11-22
2023-11-22