pragma solidity ^0.6.2;
contract Ico {
mapping(address => uint256) amountMap; // 每個用戶眾籌數(shù)量
uint256 public totalErc20Amount; // 總分發(fā)的ERC20數(shù)量
uint256 public totalEthAmount; // 眾籌的總ETH數(shù)量
uint256 public minValue = 1 ether; // 最小金額限制
address payable owner; // 眾籌發(fā)起人
bool status; // 眾籌狀態(tài)
ERC20 erc20;
constructor() public{
owner = msg.sender;
status = true;
}
// 眾籌ETH
function crowdFunding() public payable{
require(status,"crowd funding is close"); // 確保還在眾籌階段
require(msg.value >= minValue,"crowd funding value must be > 1 ether");
amountMap[msg.sender] += msg.value; // 用戶眾籌金額增加記錄賬本
uint256 beforeTotalEthAmount = totalEthAmount; // 用來防止溢出攻擊
totalEthAmount += msg.value; // 總眾籌資產(chǎn)增加
assert(beforeTotalEthAmount <= totalEthAmount); // 防止溢出攻擊
}
// 項目方獲取眾籌資產(chǎn)
function turnOutEth(uint256 _value,address _ercContractAddress) public onlyOwner{
owner.transfer(address(this).balance); // 所有眾籌ETH轉(zhuǎn)入項目方錢包地址
erc20 = ERC20(_ercContractAddress); // 初始化ERC20代幣合約
bool success = erc20.transferFrom(owner,address(this),_value); // 項目方轉(zhuǎn)入ERC20代幣
require(success,"transferFrom is fail"); // 確保轉(zhuǎn)賬成功
totalErc20Amount = _value; // 更新用戶總分配ERC20數(shù)量
status = false; // 眾籌結(jié)束
}
// 領(lǐng)取代幣
function turnOutErc() public{
require(!status,"crowd funding is not close"); // 確保眾籌結(jié)束
uint256 turnOutAmount = amountMap[msg.sender] * totalErc20Amount / totalEthAmount; // 計算單個用戶能得到的ERC20數(shù)量
bool success = erc20.transfer(msg.sender,turnOutAmount); // 轉(zhuǎn)出ERC20給用戶
require(success,"transfer is fail"); // 確保轉(zhuǎn)賬成功
amountMap[msg.sender] = 0; // 預(yù)防重復(fù)領(lǐng)取
}
// 僅限管理員操作
modifier onlyOwner(){
require(msg.sender == owner, "No authority");
_;
}
}
interface ERC20{
function transfer(address,uint256) external returns(bool);
function transferFrom(address,address,uint256) external returns(bool);
}
CrowdFunding
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 漸變的面目拼圖要我怎么拼? 我是疲乏了還是投降了? 不是不允許自己墜落, 我沒有滴水不進(jìn)的保護(hù)膜。 就是害怕變得面...
- 版本:ios 1.2.1 亮點: 1.app角標(biāo)可以實時更新天氣溫度或選擇空氣質(zhì)量,建議處女座就不要選了,不然老想...