Skip to content

Instantly share code, notes, and snippets.

@Physes
Last active July 26, 2017 13:18
Show Gist options
  • Save Physes/df1d779020c6f16658a0123c93270edb to your computer and use it in GitHub Desktop.
Save Physes/df1d779020c6f16658a0123c93270edb to your computer and use it in GitHub Desktop.
Ticker Exchange: a service for registering asset ticker symbols
pragma solidity ^0.4.11;
/**
* Ticker Exchange is a service for registering asset ticker symbols
* and pointing them to a particular resource, for example an ENS
* resolver or a token contract.
*
* Ticker hashes are hierarchical. erc20 token "BNT", which is contained
* within ETH, is found using pathhash("ETH:BNT", ":"). If BNT creates
* sub-token XYZ then those tickers can be registered with "ETH:BNT:XYZ".
*
* Target address of ticker symbol can contain registrars for sub-tickers.
*/
contract TXInterface {
function target (bytes32 ticker) constant returns(address);
function owner (bytes32 ticker) constant returns(address);
function createNewTicker (bytes32 base, bytes32 sub, address owner);
function setTarget (bytes32 ticker, address target);
function setOwner (bytes32 ticker, address newowner);
event NewTicker(bytes32 base, bytes32 sub, address owner);
event NewTarget(bytes32 ticker, address target);
event NewOwner(bytes32 ticker, address owner);
}
contract TX is TXInterface {
mapping(bytes32 => Ticker) tickers;
struct Ticker {
address owner;
address exchange;
address target;
}
modifier only_owner (bytes32 ticker) {
require(msg.sender == tickers[ticker].owner);
_;
}
function TX() {
bytes32 base = 0;
tickers[base].owner = msg.sender;
}
function target (bytes32 ticker) constant returns(address) {
return tickers[ticker].target;
}
function owner (bytes32 ticker) constant returns(address) {
return tickers[ticker].owner;
}
function exchange(bytes32 ticker) constant returns(address) {
return tickers[ticker].exchange;
}
function createNewTicker (bytes32 base, bytes32 sub, address owner)
only_owner(base) {
bytes32 ticker = sha3(base, sub);
require(tickers[ticker].owner == address(0));
tickers[ticker].owner = owner;
NewTicker(base, sub, owner);
}
function initExchange(bytes32 ticker) only_owner(ticker) {
require(tickers[ticker].exchange == address(0));
address exchange = new Exchange(this, ticker);
tickers[ticker].exchange = exchange;
NewExchange(ticker, exchange);
}
function setTarget (bytes32 ticker, address target) only_owner(ticker) {
require(target != address(0));
tickers[ticker].target = target;
NewTarget(ticker, target);
}
function setExchange (bytes32 ticker, address exchange) only_owner(ticker) {
tickers[ticker].exchange = exchange;
NewExchange(ticker, exchange);
}
function setOwner (bytes32 ticker, address newowner) only_owner(ticker) {
require(newowner != address(0));
tickers[ticker].owner = newowner;
NewOwner(ticker,newowner);
}
}
/**
* This is a registrar contract where sub-tickers can be registered.
* For example, the owner of the "ETH" ticker can set "target" in the TX contract
* to a new implementation of an Exchange contract, allowing subtickers to be
* registered: ETH:ANT, ETH:BAT etc. Similiarly, the owner of "ETH:ANT" can deploy
* an Exchange contract to allow it to be used as a base.
*/
contract Exchange {
// the TX contract
TXInterface tx;
// an array of withheld tickers, for example already existing brands
bytes32[] withheld;
// hash of the current ticker.
bytes32 public base;
// whether sub-registrations are allowed
bool public extendable;
// an address to the token in question
address public token;
address owner = tx.owner(base);
modifier check_withheld(bytes32 _withheld) {
for(uint i = 0; i < withheld.length; i++) {
require(withheld[i] != _withheld);
}
_;
}
modifier only_owner {
require(msg.sender == owner);
_;
}
function Exchange( address _tx, bytes32 _base) {
tx = TXInterface(_tx);
base = _base;
extendable = false;
}
function withhold(bytes32[] _withheld) only_owner {
withheld = _withheld;
}
function register(bytes32 _ticker) check_withheld(_ticker) {
tx.createNewTicker(base, _ticker, msg.sender);
}
function setTokenTarget(address _token) only_owner {
token = _token;
}
function extend(bool _extendable) only_owner{
extendable = _extendable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment