-
Notifications
You must be signed in to change notification settings - Fork 1
/
BecomeHippiesDividend.sol
415 lines (352 loc) · 14.3 KB
/
BecomeHippiesDividend.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
interface AggregatorV3Interface {
function latestRoundData() external view returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
}
contract BecomeHippiesToken {
AggregatorV3Interface private _feedBNBUSD;
struct Dividend {
uint256 amount;
bytes32 hash;
}
struct FundingRound {
uint8 price;
uint256 supply;
}
struct Sponsorship {
address account;
uint256 value;
bool active;
}
uint8 public constant decimals = 18;
string public constant name = "Become Hippies Token";
string public constant symbol = "BHT";
uint8 private constant _fundingStartPrice = 30;
uint8 private constant _fundingPriceDecimals = 4;
uint8 private constant _fundingRoundMaxIndex = 2;
uint8 public constant sponsorshipPercentage = 25;
uint8 public constant transferPercentage = 20;
uint8 public constant fundsPercentage = 70;
uint8 public constant fundingAddPercentage = 30;
uint8 public constant dividendBasePercentage = 10;
uint32 private constant _fundingUSDtarget = 60000;
uint256 private constant _fundingPriceFactor = 10 ** _fundingPriceDecimals;
uint256 public constant minValueDividend = 100 * 10 ** decimals;
uint256 public constant airdropSupply = 2500 * 10 ** decimals;
uint256 public constant fundingRoundSupply = 20000 * 10 ** decimals;
mapping (address => bool) private _addresses;
mapping (address => uint256) private _balances;
mapping (address => uint256) private _dividends;
mapping (address => Sponsorship) private _sponsorships;
mapping (address => address[]) private _sponsorshipsAddresses;
mapping (address => mapping (address => uint256)) private _allowances;
address[] private _beneficiaries;
uint256 private _fundsUSD;
uint256 public maxSupply = 100000 * 10 ** 18;
uint256 public dividend;
uint256 public totalSupply = 0;
uint256 public airdropAmount = 0;
uint8 private _fundingRoundIndex = 0;
address public owner;
bool public isFunding = true;
FundingRound[] private _fundingRounds;
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
constructor () {
_feedBNBUSD = AggregatorV3Interface(0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE);
for (uint8 i = _fundingRoundIndex; i <= _fundingRoundMaxIndex; i++) {
_fundingRounds.push(FundingRound(
_fundingStartPrice + (5 * i),
fundingRoundSupply
));
}
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner access");
_;
}
modifier onlyFunding() {
require(isFunding, "Funding completed");
_;
}
receive() external payable {
if (isFunding) {
addFunds(msg.sender);
} else if (msg.sender == owner) {
addDividends();
} else {
revert("This contract does not accept BNB");
}
}
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
function dividendOf(address account) public view returns (uint256) {
return _dividends[account];
}
function sponsorshipOf(address account) public view returns (uint256) {
return _sponsorships[account].value;
}
function sponsorshipsOf(address account) public view returns (address[] memory) {
return _sponsorshipsAddresses[account];
}
function dividendPercentage() public view returns (uint256) {
return _getFundsUSD() / _fundingUSDtarget / dividendBasePercentage / 10 ** 8;
}
function fundingUSDtarget() public pure returns (uint256) {
return _fundingUSDtarget * 10 ** decimals;
}
function fundsUSD() public view returns (uint256) {
return _getFundsUSD() / 10 ** 8;
}
function fundingStartPrice() public pure returns (uint256) {
return _fundingGetPrice(_fundingStartPrice);
}
function fundingMaxRound() public pure returns (uint8) {
return _fundingRoundMaxIndex + 1;
}
function fundingSupplyOfCurrentRound() public view returns (uint256) {
return isFunding ? _fundingRounds[_fundingRoundIndex].supply : 0;
}
function fundingPriceOfCurrentRound() public view returns (uint256) {
return isFunding ? _fundingGetPrice(_fundingRounds[_fundingRoundIndex].price) : 0;
}
function fundingCurrentRound() public view returns (uint8) {
return isFunding ? _fundingRoundIndex + 1 : 0;
}
function fundingValueFromBNB(uint256 value) public view returns (uint256) {
return isFunding ? _fundingValueFromBNB(value, _fundingRoundIndex) : 0;
}
function fundingValue(uint256 value) public view returns (uint256) {
return isFunding ? _fundingValue(value, _fundingRoundIndex) : 0;
}
function fundingMaxCurrentValue() public view returns (uint256) {
if (!isFunding) {
return 0;
}
uint256 amount = 0;
for (uint8 i = _fundingRoundIndex; i <= _fundingRoundMaxIndex; i++) {
amount += _fundingSupply(i) * _fundingPrice(i) / _fundingPriceFactor;
}
return amount;
}
function fundingMaxValue() public view returns (uint256) {
uint256 amount = 0;
for (uint8 i = 0; i <= _fundingRoundMaxIndex; i++) {
amount += fundingRoundSupply * _fundingPrice(i) / _fundingPriceFactor;
}
return amount;
}
function fundingMaxAmount() public view returns (uint256) {
if (!isFunding) {
return 0;
}
uint supply = 0;
for (uint8 i = _fundingRoundIndex; i <= _fundingRoundMaxIndex; i++) {
supply += _fundingSupply(i);
}
return supply;
}
function fundingSupply() public view returns (uint256) {
return _fundingRounds.length * fundingRoundSupply;
}
function sponsorshipMaxSupply() public view returns (uint256) {
return fundingSupply() * sponsorshipPercentage / 100;
}
function beneficiariesAmount() public view returns (uint256) {
return _beneficiariesAmount(minValueDividend);
}
function endOfFunding() public onlyOwner onlyFunding returns (bool) {
uint256 value = _getCurrentFunds();
payable(owner).transfer(value);
payable(owner).transfer(address(this).balance);
airdropAmount = airdropSupply * totalSupply / maxSupply;
uint256 amount = totalSupply * fundingAddPercentage / 100 + airdropAmount;
_balances[owner] += amount;
emit Transfer(address(this), owner, amount);
totalSupply += amount;
_fundsUSD = _toUSD(value);
isFunding = false;
return true;
}
function addDividends() public payable {
require(msg.value > 0, "Value is empty");
require(!isFunding, "Funding in progress");
dividend += msg.value;
uint256 amount = beneficiariesAmount();
for (uint8 i = 0; i < _beneficiaries.length; i++) {
address user = _beneficiaries[i];
if (_balances[user] >= minValueDividend) {
uint256 value = msg.value * _balances[user] / amount;
payable(user).transfer(value);
_dividends[user] += value;
}
}
}
function addFunds(address sponsorshipAddress) public onlyFunding payable {
require(msg.value <= fundingMaxCurrentValue(), "Value exceeded");
if (
!_sponsorships[msg.sender].active &&
msg.sender != sponsorshipAddress &&
!_isContract(sponsorshipAddress) &&
sponsorshipAddress != owner
) {
_sponsorships[msg.sender] = Sponsorship(sponsorshipAddress, 0, true);
_sponsorshipsAddresses[sponsorshipAddress].push(msg.sender);
}
uint256 amount = _setFunding(msg.value, msg.sender);
totalSupply += amount;
_setBalance(msg.sender, amount);
emit Transfer(address(this), msg.sender, amount);
}
function transfer(address to, uint256 value) public returns (bool) {
require(balanceOf(msg.sender) >= value, "Insufficient balance");
_balances[msg.sender] -= value;
_setBalance(to, value);
emit Transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
require(allowance(from, msg.sender) >= value, "Insufficient allowance");
require(balanceOf(from) >= value, "Insufficient balance");
if (_addresses[from]) {
uint256 amount = value * transferPercentage / 100;
require(balanceOf(from) >= value + amount, "Insufficient balance");
uint256 total = _beneficiariesAmount(0) - _balances[from];
uint256 added = 0;
for (uint8 i = 0; i < _beneficiaries.length; i++) {
address user = _beneficiaries[i];
if (user != from && _balances[user] > 0) {
uint256 add = amount * _balances[user] / total;
_balances[user] += add;
emit Transfer(from, user, add);
added += add;
}
}
_balances[from] -= added;
}
_balances[from] -= value;
_setBalance(to, value);
emit Transfer(from, to, value);
return true;
}
function approve(address spender, uint256 value) public returns (bool) {
_allowances[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function burn(uint256 amount) public {
require(balanceOf(msg.sender) >= amount, "Insufficient balance");
_balances[msg.sender] -= amount;
totalSupply -= amount;
}
function allowance(address from, address sender) public view returns (uint256) {
return _allowances[from][sender];
}
function _toUSD(uint256 value) private view returns (uint256) {
(,int price,,,) = _feedBNBUSD.latestRoundData();
return value * uint256(price);
}
function _getFundsUSD() private view returns (uint256) {
return isFunding ? _toUSD(_getCurrentFunds()) : _fundsUSD;
}
function _getCurrentFunds() private view returns (uint256) {
return address(this).balance * fundsPercentage / 100;
}
function _isContract(address value) private view returns (bool){
uint32 size;
assembly { size := extcodesize(value) }
return size > 0;
}
function _fundingGetPrice(uint8 value) private pure returns (uint256) {
return value * 10 ** (18 - _fundingPriceDecimals);
}
function _fundingSupply(uint8 index) private view returns (uint256) {
return _fundingRounds[index].supply;
}
function _fundingPrice(uint8 index) private view returns (uint8) {
return _fundingRounds[index].price;
}
function _beneficiariesAmount(uint256 minValue) private view returns (uint256) {
uint256 amount = 0;
for (uint8 i = 0; i < _beneficiaries.length; i++) {
address user = _beneficiaries[i];
if (_balances[user] >= minValue)
amount += _balances[user];
}
return amount;
}
function _fundingValueFromBNB(uint256 value, uint8 index) private view returns (uint256) {
uint8 price = _fundingPrice(index);
uint256 supply = _fundingSupply(index);
uint256 amount = value * _fundingPriceFactor / price;
if (amount > supply) {
if (index == _fundingRoundMaxIndex) {
return supply;
}
value -= supply * price / _fundingPriceFactor;
return supply + _fundingValueFromBNB(value, index + 1);
}
return amount;
}
function _fundingValue(uint256 value, uint8 index) private view returns (uint256) {
uint8 price = _fundingPrice(index);
uint256 supply = _fundingSupply(index);
if (value > supply) {
uint256 amount = supply * price / _fundingPriceFactor;
if (index == _fundingRoundMaxIndex) {
return amount;
}
return amount + _fundingValue(value - supply, index + 1);
}
return value * price / _fundingPriceFactor;
}
function _setBalance(address to, uint256 value) private {
_balances[to] += value;
if (!_addresses[to] && to != owner && !_isContract(to)) {
_addresses[to] = true;
_beneficiaries.push(to);
}
}
function _sponsorship(uint256 amount, Sponsorship storage sponsorship) private {
uint256 value = amount * sponsorshipPercentage / 100;
_balances[sponsorship.account] += value;
emit Transfer(address(this), sponsorship.account, value);
totalSupply += value;
sponsorship.value += value;
}
function _setFunding(uint256 value, address sender) private returns (uint256) {
uint8 price = _fundingRounds[_fundingRoundIndex].price;
uint256 supply = _fundingRounds[_fundingRoundIndex].supply;
uint256 amount = value * _fundingPriceFactor / price;
Sponsorship storage sponsorship = _sponsorships[sender];
FundingRound storage round = _fundingRounds[_fundingRoundIndex];
if (amount > supply) {
if (sponsorship.active) {
_sponsorship(supply, sponsorship);
}
round.supply -= supply;
if (_fundingRoundIndex == _fundingRoundMaxIndex) {
return supply;
}
value -= supply * price / _fundingPriceFactor;
_fundingRoundIndex++;
return supply + _setFunding(value, sender);
}
if (sponsorship.active) {
_sponsorship(amount, sponsorship);
}
round.supply -= amount;
if (round.supply == 0) {
_fundingRoundIndex++;
}
return amount;
}
}