2017-08-20 14:05:35 +00:00
|
|
|
//! Token Registry contract.
|
|
|
|
//! By Gav Wood (Ethcore), 2016.
|
|
|
|
//! Released under the Apache Licence 2.
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
pragma solidity ^0.4.0;
|
2017-08-20 14:05:35 +00:00
|
|
|
|
|
|
|
// From Owned.sol
|
|
|
|
contract Owned {
|
|
|
|
modifier only_owner { if (msg.sender != owner) return; _; }
|
|
|
|
|
|
|
|
event NewOwner(address indexed old, address indexed current);
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function setOwner(address _new) only_owner { NewOwner(owner, _new); owner = _new; }
|
2017-08-20 14:05:35 +00:00
|
|
|
|
|
|
|
address public owner = msg.sender;
|
|
|
|
}
|
|
|
|
|
|
|
|
contract TokenReg is Owned {
|
|
|
|
struct Token {
|
|
|
|
address addr;
|
|
|
|
string tla;
|
|
|
|
uint base;
|
|
|
|
string name;
|
|
|
|
address owner;
|
|
|
|
mapping (bytes32 => bytes32) meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
modifier when_fee_paid { if (msg.value < fee) return; _; }
|
|
|
|
modifier when_address_free(address _addr) { if (mapFromAddress[_addr] != 0) return; _; }
|
|
|
|
modifier when_tla_free(string _tla) { if (mapFromTLA[_tla] != 0) return; _; }
|
|
|
|
modifier when_is_tla(string _tla) { if (bytes(_tla).length != 3) return; _; }
|
|
|
|
modifier when_has_tla(string _tla) { if (mapFromTLA[_tla] == 0) return; _; }
|
|
|
|
modifier only_token_owner(uint _id) { if (tokens[_id].owner != msg.sender) return; _; }
|
|
|
|
|
|
|
|
event Registered(string indexed tla, uint indexed id, address addr, string name);
|
|
|
|
event Unregistered(string indexed tla, uint indexed id);
|
|
|
|
event MetaChanged(uint indexed id, bytes32 indexed key, bytes32 value);
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function register(address _addr, string _tla, uint _base, string _name) payable returns (bool) {
|
2017-08-20 14:05:35 +00:00
|
|
|
return registerAs(_addr, _tla, _base, _name, msg.sender);
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function registerAs(address _addr, string _tla, uint _base, string _name, address _owner) payable when_fee_paid when_address_free(_addr) when_is_tla(_tla) when_tla_free(_tla) returns (bool) {
|
2017-08-20 14:05:35 +00:00
|
|
|
tokens.push(Token(_addr, _tla, _base, _name, _owner));
|
|
|
|
mapFromAddress[_addr] = tokens.length;
|
|
|
|
mapFromTLA[_tla] = tokens.length;
|
|
|
|
Registered(_tla, tokens.length - 1, _addr, _name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function unregister(uint _id) only_owner {
|
2017-08-20 14:05:35 +00:00
|
|
|
Unregistered(tokens[_id].tla, _id);
|
|
|
|
delete mapFromAddress[tokens[_id].addr];
|
|
|
|
delete mapFromTLA[tokens[_id].tla];
|
|
|
|
delete tokens[_id];
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function setFee(uint _fee) only_owner {
|
2017-08-20 14:05:35 +00:00
|
|
|
fee = _fee;
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function tokenCount() constant returns (uint) { return tokens.length; }
|
|
|
|
function token(uint _id) constant returns (address addr, string tla, uint base, string name, address owner) {
|
2017-08-20 14:05:35 +00:00
|
|
|
var t = tokens[_id];
|
|
|
|
addr = t.addr;
|
|
|
|
tla = t.tla;
|
|
|
|
base = t.base;
|
|
|
|
name = t.name;
|
|
|
|
owner = t.owner;
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function fromAddress(address _addr) constant returns (uint id, string tla, uint base, string name, address owner) {
|
2017-08-20 14:05:35 +00:00
|
|
|
id = mapFromAddress[_addr] - 1;
|
|
|
|
var t = tokens[id];
|
|
|
|
tla = t.tla;
|
|
|
|
base = t.base;
|
|
|
|
name = t.name;
|
|
|
|
owner = t.owner;
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function fromTLA(string _tla) constant returns (uint id, address addr, uint base, string name, address owner) {
|
2017-08-20 14:05:35 +00:00
|
|
|
id = mapFromTLA[_tla] - 1;
|
|
|
|
var t = tokens[id];
|
|
|
|
addr = t.addr;
|
|
|
|
base = t.base;
|
|
|
|
name = t.name;
|
|
|
|
owner = t.owner;
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function meta(uint _id, bytes32 _key) constant returns (bytes32) {
|
2017-08-20 14:05:35 +00:00
|
|
|
return tokens[_id].meta[_key];
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function setMeta(uint _id, bytes32 _key, bytes32 _value) only_token_owner(_id) {
|
2017-08-20 14:05:35 +00:00
|
|
|
tokens[_id].meta[_key] = _value;
|
|
|
|
MetaChanged(_id, _key, _value);
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:45:18 +00:00
|
|
|
function drain() only_owner {
|
|
|
|
if (!msg.sender.send(this.balance))
|
|
|
|
throw;
|
2017-08-20 14:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mapping (address => uint) mapFromAddress;
|
|
|
|
mapping (string => uint) mapFromTLA;
|
|
|
|
Token[] tokens;
|
|
|
|
uint public fee = 0;
|
|
|
|
}
|