forked from Mantis322/Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrowFund.sol
157 lines (122 loc) · 6.29 KB
/
CrowFund.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
pragma solidity ^0.8.17;
// SPDX-License-Identifier: MIT
// Kontrat üzerinde kullanılacak ekstra interface ve fonksiyonları tanımlama / Define extra interfaces and functions to be used on the contract
interface IERC20 {
// Girilen adrese transfer fonksiyonu / Transfer function to the entered address
function transfer(address, uint) external returns (bool);
//Girilen adresten istenilen adrese transfer / Transfer from the entered address to the desired address
function transferFrom(
address,
address,
uint
) external returns (bool);
}
contract CrowFund{
// Gerekli eventlerin tanımlanması / Declaring required events
event Launch(
uint id,
address indexed creator,
uint goal,
uint32 startAt,
uint32 endAt
);
event Cancel(uint id);
event Pledged(uint indexed id,address indexed caller,uint amount);
event Unpledged(uint indexed id, address indexed caller,uint amount);
event Claim(uint id);
event Refund(uint id,address indexed caller, uint amount);
// Kampanya structı için gerekli parametrelerin tanımlanması / Declaring the necessary parameters for the campaign struct
struct Campain{
address creator;
uint goal;
uint pledged;
uint32 startAt;
uint32 endAt;
bool claimed;
}
// token değişkeninin tanımı / Declaring of token variable
IERC20 public immutable token;
// Campains array idsi için değişken tanımlama / Declaring variable for campains array id
uint public count;
// campains arrayini tanımlama (Tıpkı bir array gibi fakat sırasız)(Girilen id'ye göre campain'i getirir)
// Declare campains array (Just like an array but inordered)(Returns campain based on the id entered)
mapping(uint => Campain) public campains;
//Girilen adrese göre kişinin ne kadar yatırım yaptığını gösteren mapping
//According to the entered address, how much the person has invested
mapping(uint => mapping(address => uint)) pledgedAmount;
// _token adresine göre token değişkenine atama yapılması (Interface inherit edilebilirdi.)
// Declaring token variable based on _token address (Interface could be inherited.)
constructor(address _token){
token = IERC20(_token);
}
//campain ekleme fonksiyonu(fonksiyon sonunda Launc eventini harekete geçirir)
//Add campaign function (activates the Launch event at the end of the function)
function launch(uint _goal,uint32 _startAt, uint32 _endAt) external{
require(_startAt >= block.timestamp, "start at < now" );
require(_endAt >= _startAt , "end at < start at");
require(_endAt <= block.timestamp + 90 days,"end at > max duration" );
count+=1;
campains[count] = Campain({
creator: msg.sender,
goal: _goal,
pledged:0,
startAt: _startAt,
endAt: _endAt,
claimed: false
});
emit Launch(count, msg.sender, _goal, _startAt, _endAt);
}
//Girilen parametreye göre campain'i çağırır(Kontrat sahibi değilsen çalıştırmazsın ve campai'in başlamamış olması gerekir)
//Calls campain according to the entered parameter (If you do not creator of tihs contract can't run it and campain must not have started)
function cancel(uint _id) external {
Campain memory campain = campains[_id];
require(campain.creator == msg.sender, "Not owner");
require(campain.startAt < block.timestamp, "started");
delete campains[_id];
emit Cancel(_id);
}
//Girilen parametreye göre bu fonksiyonu çalıştıran kişi bu kontrata transfer yapar
//According to the entered parameter, the person who runs this function transfers to this contract.
function pledged(uint _id, uint _amount) external {
Campain storage campain = campains[_id];
require(campain.startAt <= block.timestamp, "not started");
require(campain.endAt >= block.timestamp,"ended" );
campain.pledged += _amount;
pledgedAmount[_id][msg.sender] += _amount;
token.transferFrom(msg.sender,address(this),_amount);
emit Pledged(_id,msg.sender,_amount);
}
//Girilen parametreye göre bu fonksiyonu çalıştıran kişi bu kontrata yatırdığı tutarı çeker
//According to the entered parameter, the person who runs this function withdraws the amount invested in this contract.
function unpledged(uint _id, uint _amount) external {
Campain storage campain = campains[_id];
require(campain.endAt >= block.timestamp,"ended");
campain.pledged -= _amount;
pledgedAmount[_id][msg.sender] -= _amount;
token.transfer(msg.sender,_amount);
emit Unpledged(_id,msg.sender,_amount);
}
// Kontrat sahibi kampanya bitimi sonunda hedefine ulaştıysa bütün bakiyeyi çekebilir
//If the contract owner has reached his goal at the end of the campaign, he can withdraw the entire balance
function claim(uint _id) external {
Campain storage campain = campains[_id];
require(msg.sender == campain.creator, "not creator");
require(campain.endAt < block.timestamp, "not ended");
require(campain.goal >= campain.pledged, "pledged < goal");
require(!campain.claimed , "claimed");
campain.claimed = true;
token.transfer(campain.creator,campain.pledged);
emit Claim(_id);
}
//Kampanya sonunda istenilen hedefe ulaşılmadıysa bütün tutar yatırım yapan kişilere geri aktarılır
// If the desired goal is not reached at the end of the campaign, the entire amount is transferred back to the investor.
function refund(uint _id) external {
Campain storage campain = campains[_id];
require(campain.endAt <= block.timestamp , "not ended");
require(campain.pledged < campain.goal, "pledged < goal");
uint bal = pledgedAmount[_id][msg.sender];
pledgedAmount[_id][msg.sender] = 0;
token.transfer(msg.sender,bal);
emit Refund(_id,msg.sender,bal);
}
}