-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy path0x2f5e044ad4adac34c8d8df738fac7743eda1409c-AGO-Agora.sol
199 lines (166 loc) · 7.55 KB
/
0x2f5e044ad4adac34c8d8df738fac7743eda1409c-AGO-Agora.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
pragma solidity ^0.4.8;
contract ERC20Interface {
function totalSupply() constant returns (uint256 totalSupply);
function balanceOf(address _owner) constant returns (uint256 balance);
function transfer(address _to, uint256 _value) returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) returns (bool success);
function approve(address _spender, uint256 _value) returns (bool success);
function allowance(address _owner, address _spender) constant returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract AgoraToken is ERC20Interface {
string public constant name = "Agora";
string public constant symbol = "AGO";
uint8 public constant decimals = 18;
uint256 constant minimumToRaise = 500 ether;
uint256 constant icoStartBlock = 4116800;
uint256 constant icoPremiumEndBlock = icoStartBlock + 78776; // Two weeks
uint256 constant icoEndBlock = icoStartBlock + 315106; // Two months
address owner;
uint256 raised = 0;
uint256 created = 0;
struct BalanceSnapshot {
bool initialized;
uint256 value;
}
mapping(address => uint256) shares;
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
mapping(uint256 => mapping (address => BalanceSnapshot)) balancesAtBlock;
function AgoraToken() {
owner = msg.sender;
}
// ==========================
// ERC20 Logic Implementation
// ==========================
// Returns the balance of an address.
function balanceOf(address _owner) constant returns (uint256 balance) {
return balances[_owner];
}
// Make a transfer of AGO between two addresses.
function transfer(address _to, uint256 _value) returns (bool success) {
// Freeze for dev team
require(msg.sender != owner && _to != owner);
if (balances[msg.sender] >= _value &&
_value > 0 &&
balances[_to] + _value > balances[_to]) {
// We need to register the balance known for the last reference block.
// That way, we can be sure that when the Claimer wants to check the balance
// the system can be protected against double-spending AGO tokens claiming.
uint256 referenceBlockNumber = latestReferenceBlockNumber();
registerBalanceForReference(msg.sender, referenceBlockNumber);
registerBalanceForReference(_to, referenceBlockNumber);
// Standard transfer stuff
balances[msg.sender] -= _value;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
} else { return false; }
}
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
// Freeze for dev team
require(_to != owner);
if(balances[_from] >= _value &&
_value > 0 &&
allowed[_from][msg.sender] >= _value &&
balances[_to] + _value > balances[_to]) {
// Same as `transfer` :
// We need to register the balance known for the last reference block.
// That way, we can be sure that when the Claimer wants to check the balance
// the system can be protected against double-spending AGO tokens claiming.
uint256 referenceBlockNumber = latestReferenceBlockNumber();
registerBalanceForReference(_from, referenceBlockNumber);
registerBalanceForReference(_to, referenceBlockNumber);
// Standard transferFrom stuff
balances[_from] -= _value;
balances[_to] += _value;
allowed[_from][msg.sender] -= _value;
Transfer(msg.sender, _to, _value);
return true;
} else { return false; }
}
// Approve a payment from msg.sender account to another one.
function approve(address _spender, uint256 _value) returns (bool success) {
// Freeze for dev team
require(msg.sender != owner);
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
// Checks the allowance of an account against another one. (Works with approval).
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
// Returns the total supply of token issued.
function totalSupply() constant returns (uint256 totalSupply) { return created; }
// ========================
// ICO Logic Implementation
// ========================
// ICO Status overview. Used for Agora landing page
function icoOverview() constant returns(
uint256 currentlyRaised,
uint256 tokensCreated,
uint256 developersTokens
){
currentlyRaised = raised;
tokensCreated = created;
developersTokens = balances[owner];
}
// Get Agora tokens with a Ether payment.
function buy() payable {
require(block.number > icoStartBlock && block.number < icoEndBlock && msg.sender != owner);
uint256 tokenAmount = msg.value * ((block.number < icoPremiumEndBlock) ? 550 : 500);
shares[msg.sender] += msg.value;
balances[msg.sender] += tokenAmount;
balances[owner] += tokenAmount / 6;
raised += msg.value;
created += tokenAmount;
}
// Method use by the creators. Requires the ICO to be a success.
// Used to retrieve the Ethers raised from the ICO.
// That way, Agora is becoming possible :).
function withdraw(uint256 amount) {
require(block.number > icoEndBlock && raised >= minimumToRaise && msg.sender == owner);
owner.transfer(amount);
}
// Methods use by the ICO investors. Requires the ICO to be a fail.
function refill() {
require(block.number > icoEndBlock && raised < minimumToRaise);
uint256 share = shares[msg.sender];
shares[msg.sender] = 0;
msg.sender.transfer(share);
}
// ============================
// Claimer Logic Implementation
// ============================
// This part is used by the claimer.
// The claimer can ask the balance of an user at a reference block.
// That way, the claimer is protected against double-spending AGO claimings.
// This method is triggered by `transfer` and `transferFrom`.
// It saves the balance known at a reference block only if there is no balance
// saved for this block yet.
// Meaning that this is a the first transaction since the last reference block,
// so this balance can be uses as the reference.
function registerBalanceForReference(address _owner, uint256 referenceBlockNumber) private {
if (balancesAtBlock[referenceBlockNumber][_owner].initialized) { return; }
balancesAtBlock[referenceBlockNumber][_owner].initialized = true;
balancesAtBlock[referenceBlockNumber][_owner].value = balances[_owner];
}
// What is the latest reference block number ?
function latestReferenceBlockNumber() constant returns (uint256 blockNumber) {
return (block.number - block.number % 157553);
}
// What is the balance of an user at a block ?
// If the user have made (or received) a transfer of AGO token since the
// last reference block, its balance will be written in the `balancesAtBlock`
// mapping. So we can retrieve it from here.
// Otherwise, if the user havn't made a transaction since the last reference
// block, the balance of AGO token is still good.
function balanceAtBlock(address _owner, uint256 blockNumber) constant returns (uint256 balance) {
if(balancesAtBlock[blockNumber][_owner].initialized) {
return balancesAtBlock[blockNumber][_owner].value;
}
return balances[_owner];
}
}