forked from PAY-IT-FORWARD/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCoin.sol
198 lines (161 loc) · 6.05 KB
/
BasicCoin.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
//! The basic-coin ECR20-compliant token contract.
//!
//! Copyright 2016 Gavin Wood, Parity Technologies Ltd.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! you may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//! http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.
pragma solidity ^0.4.1;
import "Owned.sol";
import "Token.sol";
// TokenReg interface
contract TokenReg {
function register(address _addr, string _tla, uint _base, string _name) payable returns (bool);
function registerAs(address _addr, string _tla, uint _base, string _name, address _owner) payable returns (bool);
function unregister(uint _id);
function setFee(uint _fee);
function tokenCount() constant returns (uint);
function token(uint _id) constant returns (address addr, string tla, uint base, string name, address owner);
function fromAddress(address _addr) constant returns (uint id, string tla, uint base, string name, address owner);
function fromTLA(string _tla) constant returns (uint id, address addr, uint base, string name, address owner);
function meta(uint _id, bytes32 _key) constant returns (bytes32);
function setMeta(uint _id, bytes32 _key, bytes32 _value);
function drain();
uint public fee;
}
// BasicCoin, ECR20 tokens that all belong to the owner for sending around
contract BasicCoin is Owned, Token {
// this is as basic as can be, only the associated balance & allowances
struct Account {
uint balance;
mapping (address => uint) allowanceOf;
}
// the balance should be available
modifier when_owns(address _owner, uint _amount) {
if (accounts[_owner].balance < _amount) throw;
_;
}
// an allowance should be available
modifier when_has_allowance(address _owner, address _spender, uint _amount) {
if (accounts[_owner].allowanceOf[_spender] < _amount) throw;
_;
}
// no ETH should be sent with the transaction
modifier when_no_eth {
if (msg.value > 0) throw;
_;
}
// a value should be > 0
modifier when_non_zero(uint _value) {
if (_value == 0) throw;
_;
}
// the base, tokens denoted in micros
uint constant public base = 1000000;
// available token supply
uint public totalSupply;
// storage and mapping of all balances & allowances
mapping (address => Account) accounts;
// constructor sets the parameters of execution, _totalSupply is all units
function BasicCoin(uint _totalSupply, address _owner) when_no_eth when_non_zero(_totalSupply) {
totalSupply = _totalSupply;
owner = _owner;
accounts[_owner].balance = totalSupply;
}
// balance of a specific address
function balanceOf(address _who) constant returns (uint256) {
return accounts[_who].balance;
}
// transfer
function transfer(address _to, uint256 _value) when_no_eth when_owns(msg.sender, _value) returns (bool) {
Transfer(msg.sender, _to, _value);
accounts[msg.sender].balance -= _value;
accounts[_to].balance += _value;
return true;
}
// transfer via allowance
function transferFrom(address _from, address _to, uint256 _value) when_no_eth when_owns(_from, _value) when_has_allowance(_from, msg.sender, _value) returns (bool) {
Transfer(_from, _to, _value);
accounts[_from].allowanceOf[msg.sender] -= _value;
accounts[_from].balance -= _value;
accounts[_to].balance += _value;
return true;
}
// approve allowances
function approve(address _spender, uint256 _value) when_no_eth returns (bool) {
Approval(msg.sender, _spender, _value);
accounts[msg.sender].allowanceOf[_spender] += _value;
return true;
}
// available allowance
function allowance(address _owner, address _spender) constant returns (uint256) {
return accounts[_owner].allowanceOf[_spender];
}
// no default function, simple contract only, entry-level users
function() {
throw;
}
}
// Manages BasicCoin instances, including the deployment & registration
contract BasicCoinManager is Owned {
// a structure wrapping a deployed BasicCoin
struct Coin {
address coin;
address owner;
address tokenreg;
}
// a new BasicCoin has been deployed
event Created(address indexed owner, address indexed tokenreg, address indexed coin);
// a list of all the deployed BasicCoins
Coin[] coins;
// all BasicCoins for a specific owner
mapping (address => uint[]) ownedCoins;
// the base, tokens denoted in micros (matches up with BasicCoin interface above)
uint constant public base = 1000000;
// return the number of deployed
function count() constant returns (uint) {
return coins.length;
}
// get a specific deployment
function get(uint _index) constant returns (address coin, address owner, address tokenreg) {
Coin c = coins[_index];
coin = c.coin;
owner = c.owner;
tokenreg = c.tokenreg;
}
// returns the number of coins for a specific owner
function countByOwner(address _owner) constant returns (uint) {
return ownedCoins[_owner].length;
}
// returns a specific index by owner
function getByOwner(address _owner, uint _index) constant returns (address coin, address owner, address tokenreg) {
return get(ownedCoins[_owner][_index]);
}
// deploy a new BasicCoin on the blockchain
function deploy(uint _totalSupply, string _tla, string _name, address _tokenreg) payable returns (bool) {
TokenReg tokenreg = TokenReg(_tokenreg);
BasicCoin coin = new BasicCoin(_totalSupply, msg.sender);
uint ownerCount = countByOwner(msg.sender);
uint fee = tokenreg.fee();
ownedCoins[msg.sender].length = ownerCount + 1;
ownedCoins[msg.sender][ownerCount] = coins.length;
coins.push(Coin(coin, msg.sender, tokenreg));
tokenreg.registerAs.value(fee)(coin, _tla, base, _name, msg.sender);
Created(msg.sender, tokenreg, coin);
return true;
}
// owner can withdraw all collected funds
function drain() only_owner {
if (!msg.sender.send(this.balance)) {
throw;
}
}
}