// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "./a_IcoToken.sol";
contract IcoExchange{
using SafeMath for uint256;
// ico的状态定义
enum IcoState {Before,Start,End}
IcoState public icoState = IcoState.Before;
address private owner;
// ico的token地址
IcoToken public icoToken ;
uint256 public totalSupply= 1000000 (10*18);
uint256 public exchangeTokenTotal = 0;
uint256 public rate =2;
constructor(){
// 申明ico属主
// 创建ico token
owner = msg.sender;
icoToken=new IcoToken(totalSupply);
}
event ReceiveICO(address from,uint256 ethValue,uint256 icoTokenValue);
event StartICO(address who);
event EndICO(address who,uint256 allEth,uint256 exchangeTokenTotal);
// 判断ico属主
modifier isOwner(){
require(owner== msg.sender,"must the owner");
_;
}
// 判断当前状态
modifier inBefore(){
require(icoState==IcoState.Before,"icoState is not Before");
_;
}
modifier inStart(){
require(icoState==IcoState.Start,"icoState is not Start");
_;
}
// ico的状态控制
function startICO() public isOwner inBefore {
icoState = IcoState.Start;
emit StartICO(msg.sender);
}
function endICO() public isOwner inStart{
emit EndICO(msg.sender,address(this).balance,exchangeTokenTotal);
// eth提取
payable(owner).transfer(address(this).balance);
// 剩余代币返还给ico的发起人
icoToken.transfer(owner,totalSupply.sub(exchangeTokenTotal));
icoState = IcoState.End;
}
function inICO() public inStart payable{
require(msg.value>0,"inICO can't <=0");
// 发送代币给对方
uint256 bossTokenValue = rate.mul(msg.value);
require(totalSupply.sub(exchangeTokenTotal)>bossTokenValue,"icoTOken balance not enough");
icoToken.transfer(msg.sender,bossTokenValue);
exchangeTokenTotal = exchangeTokenTotal.add(bossTokenValue);
emit ReceiveICO(msg.sender,msg.value,bossTokenValue);
}
receive() external payable{
inICO();
}
}