forked from PAY-IT-FORWARD/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GavCoin.sol
203 lines (169 loc) · 7.73 KB
/
GavCoin.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
pragma solidity ^0.4.0;
contract Token {
/// Get the total amount of tokens in the system.
function totalSupply() constant returns (uint256 total);
/// @param _owner The address from which the balance will be retrieved
/// @return The balance
function balanceOf(address _owner) constant returns (uint256 balance);
/// @notice send `_value` token to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transfer(address _to, uint256 _value) returns (bool success);
/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of token to be transferred
/// @return Whether the transfer was successful or not
function transferFrom(address _from, address _to, uint256 _value) returns (bool success);
/// @notice `msg.sender` approves `_addr` to spend `_value` tokens
/// @param _spender The address of the account able to transfer the tokens
/// @param _value The amount of wei to be approved for transfer
/// @return Whether the approval was successful or not
function approve(address _spender, uint256 _value) returns (bool success);
/// @param _owner The address of the account owning tokens
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens allowed to spent
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 GavCoin {
struct Receipt {
uint units;
uint32 activation;
}
struct Account {
uint balance;
mapping (uint => Receipt) receipt;
mapping (address => uint) allowanceOf;
}
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Buyin(address indexed buyer, uint indexed price, uint indexed amount);
event Refund(address indexed buyer, uint indexed price, uint indexed amount);
event NewTranch(uint indexed price);
modifier when_owns(address _owner, uint _amount) { if (accounts[_owner].balance < _amount) return; _; }
modifier when_has_allowance(address _owner, address _spender, uint _amount) { if (accounts[_owner].allowanceOf[_spender] < _amount) return; _; }
modifier when_have_active_receipt(uint _price, uint _units) { if (accounts[msg.sender].receipt[_price].units < _units || now < accounts[msg.sender].receipt[_price].activation) return; _; }
function balanceOf(address _who) constant returns (uint) { return accounts[_who].balance; }
function transfer(address _to, uint256 _value) when_owns(msg.sender, _value) returns (bool success) {
Transfer(msg.sender, _to, _value);
accounts[msg.sender].balance -= _value;
accounts[_to].balance += _value;
return true;
}
function transferFrom(address _from, address _to, uint256 _value) when_owns(_from, _value) when_has_allowance(_from, msg.sender, _value) returns (bool success) {
Transfer(_from, _to, _value);
accounts[_from].allowanceOf[msg.sender] -= _value;
accounts[_from].balance -= _value;
accounts[_to].balance += _value;
return true;
}
function approve(address _spender, uint256 _value) returns (bool success) {
Approval(msg.sender, _spender, _value);
accounts[msg.sender].allowanceOf[_spender] += _value;
return true;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return accounts[_owner].allowanceOf[_spender];
}
/// Simple buyin.
function() payable { buyinInternal(msg.sender, 2 ** 255); }
/// Extended buyin.
function buyin(address _who, uint _maxPrice) payable { buyinInternal(_who, _maxPrice); }
function refund(uint _price, uint _units) when_have_active_receipt(_price, _units) when_owns(msg.sender, _units) returns (bool) {
Refund(msg.sender, _price, _units);
accounts[msg.sender].balance -= _units;
totalSupply += _units;
accounts[msg.sender].receipt[_price].units -= _units;
if (accounts[msg.sender].receipt[_price].units == 0)
delete accounts[msg.sender].receipt[_price];
if (!msg.sender.send(_units * _price / base))
throw;
return true;
}
function buyinInternal(address _who, uint _maxPrice) internal {
var leftToSpend = msg.value;
while (leftToSpend > 0 && price <= _maxPrice) {
// How much the remaining tokens of this tranch cost to buy
var maxCanSpend = price * remaining / base;
// How much we will spend - the mininum of what's left in the tranch
// to buy and what we have remaining
var spend = leftToSpend > maxCanSpend ? maxCanSpend : leftToSpend;
// The number of units we get for spending that
var units = spend * base / price;
// Provide tokens and a purchase receipt
accounts[msg.sender].balance += units;
accounts[msg.sender].receipt[price].units += units;
accounts[msg.sender].receipt[price].activation = uint32(now) + refundActivationPeriod;
totalSupply += units;
Buyin(msg.sender, price, units);
// Reduce the amounts remaining
leftToSpend -= spend;
remaining -= units;
// If this is the end of the tranch...
if (remaining == 0) {
// ...Increment price and reset remaining
price += tranchStep;
remaining = tokensPerTranch * base;
NewTranch(price);
}
}
}
uint public totalSupply;
mapping (address => Account) accounts;
uint constant base = 1000000; // tokens are subdivisible by 1000000
uint constant tranchStep = 1 finney; // raise price by 1 finney / tranch
uint constant tokensPerTranch = 100; // 100 tokens per tranch
uint public price = 1 finney; // begin at 1 finney / token
uint public remaining = tokensPerTranch * base;
uint32 constant refundActivationPeriod = 7 days;
/// GAMBLING SUBSYSTEM
/*
struct Bet {
uint amount;
uint8 odds;
uint number;
}
modifier when_bet(bytes32 _commitment) { if (bets[_commitment].amount == 0) return; }
modifier when_no_bet(bytes32 _commitment) { if (bets[_commitment].amount != 0) return; }
modifier when_modest(uint _amount, uint8 _odds) { if (_amount * 255 / uint(_odds) > winningsLimitPerBlock) return; }
event BetPlaced(bytes32 indexed commitment, uint amount, uint8 odds, uint block);
event BetCollected(bytes32 indexed commitment, uint winnings);
function bet(uint _amount, uint8 _odds, bytes32 _commitment, bytes32 _recycle) when_owns(msg.sender, _amount) when_no_bet(_commitment) when_modest(_amount, _odds) {
if (_recycle != 0) {
delete bets[sha3(_recycle)];
}
accounts[msg.sender].balance -= _amount;
totalSupply -= _amount;
var potentialWinnings = _amount * 255 / _odds;
if (currentBettingBlock < block.number + 1) {
currentBettingBlock = block.number + 1;
currentPotentialWinnings = 0;
}
else if (currentPotentialWinnings + potentialWinnings > winningsLimitPerBlock) {
currentBettingBlock++;
currentPotentialWinnings = 0;
}
currentPotentialWinnings += potentialWinnings;
bets[_commitment] = Bet(_amount, _odds, currentBettingBlock);
BetPlaced(_commitment, _amount, _odds, currentBettingBlock);
}
function collect(bytes32 _ticket) when_bet(sha3(_ticket)) {
var commitment = sha3(_ticket);
Bet b = bets[commitment];
delete bets[commitment];
if (uint8(block.blockhash(b.number) ^ _ticket) > 255 - b.odds) {
uint winnings = b.amount * 255 / b.odds;
accounts[msg.sender].balance += winnings;
totalSupply += winnings;
BetCollected(commitment, winnings);
}
}
mapping (bytes32 => Bet) bets;
uint currentBettingBlock = 0;
uint currentPotentialWinnings = 0;
uint constant winningsLimitPerBlock = 5 ether;
*/
}