Skip to content

Instantly share code, notes, and snippets.

@fromjyk
Created December 3, 2018 03:36
Show Gist options
  • Save fromjyk/47788f99a7acfd8dc61c1382928b0939 to your computer and use it in GitHub Desktop.
Save fromjyk/47788f99a7acfd8dc61c1382928b0939 to your computer and use it in GitHub Desktop.
pragma solidity >=0.4.22 <0.6.0;
interface token {
function transfer(address receiver, uint amount) external;
}
contract Crowdsale {
address public beneficiary;
uint public fundingGoal;
uint public amountRaised;
uint public deadline;
uint public price;
token public tokenReward;
mapping(address => uint256) public balanceOf;
bool fundingGoalReached = false;
bool crowdsaleClosed = false;
event GoalReached(address recipient, uint totalAmountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
/*
* Constructor
* 소유자 설정
*/
constructor(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint durationInMinutes,
uint etherCostOfEachToken,
address addressOfTokenUsedAsReward
) public {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
tokenReward = token(addressOfTokenUsedAsReward);
}
/*
* 폴백 함수(되돌림)
* 이름없는 함수는 누군가가 컨트랙트에 자금을 보낼 때마다 호출되는 기본 함수입니다.
*/
function () payable external {
require(!crowdsaleClosed);
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, amount / price);
emit FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() { if (now >= deadline) _; }
/*
* 목표 도달 여부 확인
* 목표 또는 시간 제한에 도달했는지 확인하고 캠페인을 종료합니다.
*/
function checkGoalReached() public afterDeadline {
if (amountRaised >= fundingGoal){
fundingGoalReached = true;
emit GoalReached(beneficiary, amountRaised);
}
crowdsaleClosed = true;
}
/*
* 자금 출금
* 목표 또는 시간 제한에 도달했는지 확인하고, 그렇다면 자금 조달 목표에 도달했는지 확인합니다.
* 전체 금액을 수령인에게 보냅니다. 목표에 도달하지 못하면 각 기여자는 철회 할 수 있습니다.
* ‘the amount’ 그들이 기여한 금액
*/
function safeWithdrawal() public afterDeadline {
if (!fundingGoalReached) {
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if (amount > 0) {
if (msg.sender.send(amount)) {
emit FundTransfer(msg.sender, amount, false);
} else {
balanceOf[msg.sender] = amount;
}
}
}
if (fundingGoalReached && beneficiary == msg.sender) {
if (msg.sender.send(amountRaised)) {
emit FundTransfer(beneficiary, amountRaised, false);
} else {
//우리가 수령인에게 자금을 보내지 못하면, 자금 조달자의 잔액을 풀어줍니다.
fundingGoalReached = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment