-
Notifications
You must be signed in to change notification settings - Fork 31
/
basic_ICO.sol
138 lines (96 loc) · 4.12 KB
/
basic_ICO.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
//*******************************************************************//
//------------------ Contract to Manage Ownership -------------------//
//*******************************************************************//
contract ownable {
address payable public owner;
address payable internal newOwner;
event OwnershipTransferred(address indexed _from, address indexed _to);
constructor() {
owner = payable(msg.sender);
emit OwnershipTransferred(address(0), owner);
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address payable _newOwner) external onlyOwner {
newOwner = _newOwner;
}
//this flow is to prevent transferring ownership to wrong wallet by mistake
function acceptOwnership() external {
require(msg.sender == newOwner);
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
newOwner = payable(0);
}
}
interface tokenInterface
{
function transfer(address _to, uint256 _amount) external returns (bool);
function transferFrom(address _from, address _to, uint256 _amount) external returns (bool);
}
//****************************************************************************//
//--------------------- MAIN CODE STARTS HERE ---------------------//
//****************************************************************************//
contract TokenSale is ownable {
/*===============================
= DATA STORAGE =
===============================*/
// Public variables for private sale
address public tokenContractAddress; // main token address to run ICO on
uint256 public exchangeRate = 100; // exchange rate 1 ETH = 100 tokens
uint256 public icoETHReceived; // how many ETH Received through ICO
uint256 public totalTokenSold; // how many tokens sold
uint256 public minimumContribution = 10**16; // Minimum amount to invest - 0.01 ETH (in 18 decimal format)
/**
* Fallback function. It accepts incoming ETH and issue tokens
*/
receive () payable external {
buyToken();
}
event buyTokenEvent (address sender,uint amount, uint tokenPaid);
function buyToken() payable public returns(uint)
{
//checking conditions
require(msg.value >= minimumContribution, "less then minimum contribution");
//calculating tokens to issue
uint256 tokenTotal = msg.value * exchangeRate;
//updating state variables
icoETHReceived += msg.value;
totalTokenSold += tokenTotal;
//sending tokens. This crowdsale contract must hold enough tokens.
tokenInterface(tokenContractAddress).transfer(msg.sender, tokenTotal);
//send fund to owner
owner.transfer(msg.value);
//logging event
emit buyTokenEvent(msg.sender,msg.value, tokenTotal);
return tokenTotal;
}
// exchange rate => 1 ETH = how many tokens
function setExchangeRate(uint256 _exchangeRatePercent) onlyOwner public returns (bool)
{
exchangeRate = _exchangeRatePercent;
return true;
}
function setMinimumContribution(uint256 _minimumContribution) onlyOwner public returns (bool)
{
minimumContribution = _minimumContribution;
return true;
}
function setTokenContract(address _tokenContract) onlyOwner public returns (bool)
{
tokenContractAddress = _tokenContract;
return true;
}
function manualWithdrawTokens(uint256 tokenAmount) public onlyOwner returns(string memory){
// no need for overflow checking as that will be done in transfer function
tokenInterface(tokenContractAddress).transfer(msg.sender, tokenAmount);
return "Tokens withdrawn to owner wallet";
}
function manualWithdrawFund() public onlyOwner returns(string memory){
owner.transfer(address(this).balance);
return "Fund withdrawn to owner wallet";
}
}