-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPyramid.sol
263 lines (226 loc) · 8.04 KB
/
Pyramid.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.4.18;
contract Pyramid {
uint256 constant PRECISION = 0x10000000000000000; // 2^64
int constant CRRN = 1;
int constant CRRD = 2;
int constant LOGC = -0x296ABF784A358468C;
string public constant name = "Pyramid";
string public constant symbol = "PY";
uint8 public constant decimals = 18;
bool public isFlag = false;
uint256 public totalSupply;
mapping(address => uint256) public balanceOfOld;
mapping(address => mapping(address => uint256)) public allowance;
mapping(address => int256) payouts;
int256 totalPayouts;
uint256 earningsPerShare;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
function Pyramid() public {
//owner = msg.sender;
}
function balanceOf(
address _owner
) public constant returns (uint256 balance) {
return balanceOfOld[_owner];
}
function withdraw(uint tokenCount) public returns (bool) {
var balance = dividends(msg.sender);
payouts[msg.sender] += (int256)(balance * PRECISION);
totalPayouts += (int256)(balance * PRECISION);
msg.sender.transfer(balance);
return true;
}
function sellMyTokens() public {
var balance = balanceOf(msg.sender);
transferTokens(msg.sender, address(this), balance);
}
function fund() public payable returns (bool) {
if (msg.value > 0.000001 ether) buy();
else return false;
return true;
}
function buyPrice() public constant returns (uint) {
return getTokensForEther(1 finney);
}
function sellPrice() public constant returns (uint) {
return getEtherForTokens(1 finney);
}
function transferTokens(
address _from,
address _to,
uint256 _value
) internal {
if (balanceOfOld[_from] < _value) revert();
if (_to == address(this)) {
sell(_value);
} else {
int256 payoutDiff = (int256)(earningsPerShare * _value);
balanceOfOld[_from] -= _value;
balanceOfOld[_to] += _value;
payouts[_from] -= payoutDiff;
payouts[_to] += payoutDiff;
}
Transfer(_from, _to, _value);
}
function transfer(address _to, uint256 _value) public {
transferTokens(msg.sender, _to, _value);
}
function transferFrom(address _from, address _to, uint256 _value) public {
var _allowance = allowance[_from][msg.sender];
if (_allowance < _value) revert();
allowance[_from][msg.sender] = _allowance - _value;
transferTokens(_from, _to, _value);
}
function approve(address _spender, uint256 _value) public {
if ((_value != 0) && (allowance[msg.sender][_spender] != 0)) revert();
allowance[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
}
function dividends(
address _owner
) public constant returns (uint256 amount) {
return
(uint256)(
(int256)(earningsPerShare * balanceOfOld[_owner]) -
payouts[_owner]
) / PRECISION;
}
function withdrawOld(address to) public {
var balance = dividends(msg.sender);
payouts[msg.sender] += (int256)(balance * PRECISION);
totalPayouts += (int256)(balance * PRECISION);
to.transfer(balance);
}
function balance() internal constant returns (uint256 amount) {
return this.balance - msg.value;
}
function reserve() public constant returns (uint256 amount) {
return
balance() -
((uint256)(
(int256)(earningsPerShare * totalSupply) - totalPayouts
) / PRECISION) -
1;
}
function buy() internal {
if (msg.value < 0.000001 ether || msg.value > 1000000 ether) revert();
var sender = msg.sender;
var fee = (uint)(msg.value / 10);
var numEther = msg.value - fee;
var numTokens = getTokensForEther(numEther);
var buyerfee = fee * PRECISION;
if (totalSupply > 0) {
var holderreward = ((PRECISION -
((reserve() + numEther) * numTokens * PRECISION) /
(totalSupply + numTokens) /
numEther) * (uint)(CRRD)) / (uint)(CRRD - CRRN);
var holderfee = fee * holderreward;
buyerfee -= holderfee;
var feePerShare = holderfee / totalSupply;
earningsPerShare += feePerShare;
}
totalSupply += numTokens;
balanceOfOld[sender] += numTokens;
var payoutDiff = (int256)((earningsPerShare * numTokens) - buyerfee);
payouts[sender] += payoutDiff;
totalPayouts += payoutDiff;
}
function sell(uint256 amount) internal {
var numEthers = getEtherForTokens(amount);
totalSupply -= amount;
balanceOfOld[msg.sender] -= amount;
var payoutDiff = (int256)(
earningsPerShare * amount + (numEthers * PRECISION)
);
payouts[msg.sender] -= payoutDiff;
totalPayouts -= payoutDiff;
}
function getTokensForEther(
uint256 ethervalue
) public constant returns (uint256 tokens) {
return
fixedExp((fixedLog(reserve() + ethervalue) * CRRN) / CRRD + LOGC) -
totalSupply;
}
function getEtherForTokens(
uint256 tokens
) public constant returns (uint256 ethervalue) {
if (tokens == totalSupply) return reserve();
return
reserve() -
fixedExp(((fixedLog(totalSupply - tokens) - LOGC) * CRRD) / CRRN);
}
int256 constant one = 0x10000000000000000;
uint256 constant sqrt2 = 0x16a09e667f3bcc908;
uint256 constant sqrtdot5 = 0x0b504f333f9de6484;
int256 constant ln2 = 0x0b17217f7d1cf79ac;
int256 constant ln2_64dot5 = 0x2cb53f09f05cc627c8;
int256 constant c1 = 0x1ffffffffff9dac9b;
int256 constant c3 = 0x0aaaaaaac16877908;
int256 constant c5 = 0x0666664e5e9fa0c99;
int256 constant c7 = 0x049254026a7630acf;
int256 constant c9 = 0x038bd75ed37753d68;
int256 constant c11 = 0x03284a0c14610924f;
function fixedLog(uint256 a) internal pure returns (int256 log) {
int32 scale = 0;
while (a > sqrt2) {
a /= 2;
scale++;
}
while (a <= sqrtdot5) {
a *= 2;
scale--;
}
int256 s = (((int256)(a) - one) * one) / ((int256)(a) + one);
var z = (s * s) / one;
return
scale *
ln2 +
((s *
(c1 +
((z *
(c3 +
((z *
(c5 +
((z *
(c7 +
((z * (c9 + ((z * c11) / one))) /
one))) / one))) / one))) /
one))) / one);
}
int256 constant c2 = 0x02aaaaaaaaa015db0;
int256 constant c4 = -0x000b60b60808399d1;
int256 constant c6 = 0x0000455956bccdd06;
int256 constant c8 = -0x000001b893ad04b3a;
function fixedExp(int256 a) internal pure returns (uint256 exp) {
int256 scale = (a + (ln2_64dot5)) / ln2 - 64;
a -= scale * ln2;
int256 z = (a * a) / one;
int256 R = ((int256)(2) * one) +
((z *
(c2 +
((z * (c4 + ((z * (c6 + ((z * c8) / one))) / one))) /
one))) / one);
exp = (uint256)(((R + a) * one) / (R - a));
if (scale >= 0) exp <<= scale;
else exp >>= -scale;
return exp;
}
/*function destroy() external {
selfdestruct(owner);
}*/
function() public payable {
if (msg.value > 0) buy();
else withdrawOld(msg.sender);
}
function flag() public {
if (balanceOf(msg.sender) <= totalSupply) revert();
isFlag = true;
}
}