forked from MineBlocksPool/Smart-Contract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineBlocks.sol
More file actions
258 lines (171 loc) · 8.18 KB
/
MineBlocks.sol
File metadata and controls
258 lines (171 loc) · 8.18 KB
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
pragma solidity ^0.4.15;
//Declare owned , structure to admin functions only by the owner
contract owned {
address public owner;
function owned()internal{
owner=msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) revert();
_;
}
//transfer owner property
function transferOwnership(address newOwner) public onlyOwner {
owner = newOwner;
}
}
//Standard token ERC20 structure declaration
//
contract tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData)public ; }
contract token is owned{
/* Public variables of the token */
string public standard = "ERC20 TokenFederalReserve";
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
/* Initializes contract with initial supply tokens to the creator of the contract */
function token(
uint256 initialSupply,
string tokenName,
uint8 decimalUnits,
string tokenSymbol
) public {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
totalSupply = initialSupply; // Update total supply
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
decimals = decimalUnits; // Amount of decimals for display purposes
}
/* Send coins */
function transfer(address _to, uint256 _value) public {
if (balanceOf[msg.sender] < _value) revert(); // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) revert(); // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place
}
/* Allow another contract to spend some tokens in your behalf */
function approve(address _spender, uint256 _value) public onlyOwner returns (bool success) {
allowance[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
/* Approve and then communicate the approved contract in a single tx */
function approveAndCall(address _spender, uint256 _value, bytes _extraData) public onlyOwner
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/* A contract attempts to get the coins */
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
if (balanceOf[_from] < _value) revert(); // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) revert(); // Check for overflows
if (_value > allowance[_from][msg.sender]) revert(); // Check allowance
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
allowance[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
}
contract MineBlocks is owned, token {
//Declare public contract variables
uint256 public buyPrice=1500000000000000;
uint256 public sellPrice=1500000000000000;
uint8 public spread=5;
/* This generates a public event on the blockchain that will notify clients */
/* Initializes contract with initial supply tokens to the creator of the contract */
function MineBlocks(
uint256 initialSupply,
string tokenName,
uint8 decimalUnits,
string tokenSymbol
) public token (initialSupply, tokenName, decimalUnits, tokenSymbol) {}
/* Send coins */
function transfer(address _to, uint256 _value) public {
if (balanceOf[msg.sender] < _value) revert(); // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) revert(); // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place
}
/* A contract attempts to get the coins */
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
if (balanceOf[_from] < _value) revert(); // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) revert(); // Check for overflows
if (_value > allowance[_from][msg.sender]) revert(); // Check allowance
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
allowance[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
//Declare logging events
event LogDeposit(address sender, uint amount);
event LogTransfer(address sender, address to, uint amount);
function status() internal {
//stablish the buy price & sell price with the spread configured in the contract
if(this.balance<=10000000000000000000){
buyPrice=1500000000000000;
}else if(this.balance>10000000000000000000 && this.balance<=100000000000000000000){
buyPrice=2000000000000000;
}else if(this.balance>100000000000000000000 && this.balance<=200000000000000000000){
buyPrice=2500000000000000;
}else if(this.balance>200000000000000000000 && this.balance<=500000000000000000000){
buyPrice=3000000000000000;
}else{
buyPrice=(this.balance/totalSupply)*100;
}
sellPrice=buyPrice-(buyPrice*spread)/100;
}
function buy() public payable {
if (msg.sender.balance < msg.value) revert(); // Check if the sender has enought eth to buy
if (msg.sender.balance + msg.value < msg.sender.balance) revert(); //check for overflows
uint dec=decimals;
uint amount = (msg.value / buyPrice)*(10**dec) ; // calculates the amount
if (amount <= 0) revert(); //check amount overflow
if (balanceOf[msg.sender] + amount < balanceOf[msg.sender]) revert(); // Check for overflows
if (balanceOf[this] < amount) revert(); // checks if it has enough to sell
balanceOf[this] -= amount; // subtracts amount from seller's balance
balanceOf[msg.sender] += amount; // adds the amount to buyer's balance
Transfer(this, msg.sender, amount); //send the tokens to the sendedr
//update status variables of the contract
status();
}
function deposit() public payable returns(bool success) {
// Check for overflows;
if (this.balance + msg.value < this.balance) revert(); // Check for overflows
//executes event to reflect the changes
LogDeposit(msg.sender, msg.value);
//update contract status
status();
return true;
}
function sell(uint256 amount) public {
uint dec=decimals;
var amountbalance = amount*(10**dec);
if (balanceOf[this] + amountbalance < balanceOf[this]) revert(); // Check for overflows
if (balanceOf[msg.sender] < amountbalance ) revert(); // checks if the sender has enough to sell
balanceOf[msg.sender] -= amountbalance; // subtracts the amount from seller's balance
balanceOf[this] += amountbalance; // adds the amount to owner's balance
// Sends ether to the seller. It's important
msg.sender.transfer(amount*sellPrice);
// executes an event reflecting on the change
Transfer(msg.sender, this, amount*(10**dec));
//update contract status
status();
}
function () public payable {
buy(); // Allow to buy tokens sending ether direcly to the contract
}
}