ETH Price: $2,519.98 (-3.38%)

Transaction Decoder

Block:
6209713 at Aug-25-2018 07:10:45 AM +UTC
Transaction Fee:
0.000243537 ETH $0.61
Gas Used:
81,179 Gas / 3 Gwei

Account State Difference:

  Address   Before After State Difference Code
0x55C9464B...f1c51e4C4
0.257721452395936887 Eth
Nonce: 1853
0.257477915395936887 Eth
Nonce: 1854
0.000243537
(Spark Pool)
6,566.504901488249823501 Eth6,566.505145025249823501 Eth0.000243537

Execution Trace

0xb80d657ed1d4efe5b273cd9fdbfab2b2ca7bdb14.fdd87e83( )
  • ConversionRates.getRate( token=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, currentBlockNumber=6209713, buy=True, qty=1000000000000000000 ) => ( 6995485977839858249367 )
  • BancorConverter.getReturn( _fromToken=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, _toToken=0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C, _amount=6995485977839858249367 ) => ( 173687126952288555582 )
    • SmartToken.CALL( )
    • SNT.balanceOf( _owner=0x599485Dc0f3D8b308B973B2Db5Cd44baE46D31c4 ) => ( balance=2557907562130769680595213 )
    • SmartToken.balanceOf( 0x599485Dc0f3D8b308B973B2Db5Cd44baE46D31c4 ) => ( 63810143039380647806459 )
    • ContractRegistry.getAddress( _contractName=42616E636F72466F726D756C6100000000000000000000000000000000000000 ) => ( 0xFFd2de852B694F88656e91D9DEfa6b425c454742 )
    • BancorFormula.calculateCrossConnectorReturn( _fromConnectorBalance=2557907562130769680595213, _fromConnectorWeight=500000, _toConnectorBalance=63810143039380647806459, _toConnectorWeight=500000, _amount=6995485977839858249367 ) => ( 174035022963191976343 )
    • BancorConverter.getReturn( _fromToken=0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C, _toToken=0xc0829421C1d260BD3cB3E0F06cfE2D52db2cE315, _amount=173687126952288555582 ) => ( 1001252064567109319 )
      • SmartToken.CALL( )
      • SmartToken.CALL( )
      • ContractRegistry.getAddress( _contractName=42616E636F72466F726D756C6100000000000000000000000000000000000000 ) => ( 0xFFd2de852B694F88656e91D9DEfa6b425c454742 )
      • BancorFormula.calculateSaleReturn( _supply=77302466626506712312115027, _connectorBalance=44562909317441892672041, _connectorWeight=100000, _sellAmount=173687126952288555582 ) => ( 1001252064567109319 )
        File 1 of 8: ConversionRates
        pragma solidity 0.4.18;
        
        interface ConversionRatesInterface {
        
            function recordImbalance(
                ERC20 token,
                int buyAmount,
                uint rateUpdateBlock,
                uint currentBlock
            )
                public;
        
            function getRate(ERC20 token, uint currentBlockNumber, bool buy, uint qty) public view returns(uint);
        }
        
        interface ERC20 {
            function totalSupply() public view returns (uint supply);
            function balanceOf(address _owner) public view returns (uint balance);
            function transfer(address _to, uint _value) public returns (bool success);
            function transferFrom(address _from, address _to, uint _value) public returns (bool success);
            function approve(address _spender, uint _value) public returns (bool success);
            function allowance(address _owner, address _spender) public view returns (uint remaining);
            function decimals() public view returns(uint digits);
            event Approval(address indexed _owner, address indexed _spender, uint _value);
        }
        
        contract PermissionGroups {
        
            address public admin;
            address public pendingAdmin;
            mapping(address=>bool) internal operators;
            mapping(address=>bool) internal alerters;
            address[] internal operatorsGroup;
            address[] internal alertersGroup;
            uint constant internal MAX_GROUP_SIZE = 50;
        
            function PermissionGroups() public {
                admin = msg.sender;
            }
        
            modifier onlyAdmin() {
                require(msg.sender == admin);
                _;
            }
        
            modifier onlyOperator() {
                require(operators[msg.sender]);
                _;
            }
        
            modifier onlyAlerter() {
                require(alerters[msg.sender]);
                _;
            }
        
            function getOperators () external view returns(address[]) {
                return operatorsGroup;
            }
        
            function getAlerters () external view returns(address[]) {
                return alertersGroup;
            }
        
            event TransferAdminPending(address pendingAdmin);
        
            /**
             * @dev Allows the current admin to set the pendingAdmin address.
             * @param newAdmin The address to transfer ownership to.
             */
            function transferAdmin(address newAdmin) public onlyAdmin {
                require(newAdmin != address(0));
                TransferAdminPending(pendingAdmin);
                pendingAdmin = newAdmin;
            }
        
            /**
             * @dev Allows the current admin to set the admin in one tx. Useful initial deployment.
             * @param newAdmin The address to transfer ownership to.
             */
            function transferAdminQuickly(address newAdmin) public onlyAdmin {
                require(newAdmin != address(0));
                TransferAdminPending(newAdmin);
                AdminClaimed(newAdmin, admin);
                admin = newAdmin;
            }
        
            event AdminClaimed( address newAdmin, address previousAdmin);
        
            /**
             * @dev Allows the pendingAdmin address to finalize the change admin process.
             */
            function claimAdmin() public {
                require(pendingAdmin == msg.sender);
                AdminClaimed(pendingAdmin, admin);
                admin = pendingAdmin;
                pendingAdmin = address(0);
            }
        
            event AlerterAdded (address newAlerter, bool isAdd);
        
            function addAlerter(address newAlerter) public onlyAdmin {
                require(!alerters[newAlerter]); // prevent duplicates.
                require(alertersGroup.length < MAX_GROUP_SIZE);
        
                AlerterAdded(newAlerter, true);
                alerters[newAlerter] = true;
                alertersGroup.push(newAlerter);
            }
        
            function removeAlerter (address alerter) public onlyAdmin {
                require(alerters[alerter]);
                alerters[alerter] = false;
        
                for (uint i = 0; i < alertersGroup.length; ++i) {
                    if (alertersGroup[i] == alerter) {
                        alertersGroup[i] = alertersGroup[alertersGroup.length - 1];
                        alertersGroup.length--;
                        AlerterAdded(alerter, false);
                        break;
                    }
                }
            }
        
            event OperatorAdded(address newOperator, bool isAdd);
        
            function addOperator(address newOperator) public onlyAdmin {
                require(!operators[newOperator]); // prevent duplicates.
                require(operatorsGroup.length < MAX_GROUP_SIZE);
        
                OperatorAdded(newOperator, true);
                operators[newOperator] = true;
                operatorsGroup.push(newOperator);
            }
        
            function removeOperator (address operator) public onlyAdmin {
                require(operators[operator]);
                operators[operator] = false;
        
                for (uint i = 0; i < operatorsGroup.length; ++i) {
                    if (operatorsGroup[i] == operator) {
                        operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1];
                        operatorsGroup.length -= 1;
                        OperatorAdded(operator, false);
                        break;
                    }
                }
            }
        }
        
        contract Withdrawable is PermissionGroups {
        
            event TokenWithdraw(ERC20 token, uint amount, address sendTo);
        
            /**
             * @dev Withdraw all ERC20 compatible tokens
             * @param token ERC20 The address of the token contract
             */
            function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin {
                require(token.transfer(sendTo, amount));
                TokenWithdraw(token, amount, sendTo);
            }
        
            event EtherWithdraw(uint amount, address sendTo);
        
            /**
             * @dev Withdraw Ethers
             */
            function withdrawEther(uint amount, address sendTo) external onlyAdmin {
                sendTo.transfer(amount);
                EtherWithdraw(amount, sendTo);
            }
        }
        
        contract VolumeImbalanceRecorder is Withdrawable {
        
            uint constant internal SLIDING_WINDOW_SIZE = 5;
            uint constant internal POW_2_64 = 2 ** 64;
        
            struct TokenControlInfo {
                uint minimalRecordResolution; // can be roughly 1 cent
                uint maxPerBlockImbalance; // in twei resolution
                uint maxTotalImbalance; // max total imbalance (between rate updates)
                                    // before halting trade
            }
        
            mapping(address => TokenControlInfo) internal tokenControlInfo;
        
            struct TokenImbalanceData {
                int  lastBlockBuyUnitsImbalance;
                uint lastBlock;
        
                int  totalBuyUnitsImbalance;
                uint lastRateUpdateBlock;
            }
        
            mapping(address => mapping(uint=>uint)) public tokenImbalanceData;
        
            function VolumeImbalanceRecorder(address _admin) public {
                require(_admin != address(0));
                admin = _admin;
            }
        
            function setTokenControlInfo(
                ERC20 token,
                uint minimalRecordResolution,
                uint maxPerBlockImbalance,
                uint maxTotalImbalance
            )
                public
                onlyAdmin
            {
                tokenControlInfo[token] =
                    TokenControlInfo(
                        minimalRecordResolution,
                        maxPerBlockImbalance,
                        maxTotalImbalance
                    );
            }
        
            function getTokenControlInfo(ERC20 token) public view returns(uint, uint, uint) {
                return (tokenControlInfo[token].minimalRecordResolution,
                        tokenControlInfo[token].maxPerBlockImbalance,
                        tokenControlInfo[token].maxTotalImbalance);
            }
        
            function addImbalance(
                ERC20 token,
                int buyAmount,
                uint rateUpdateBlock,
                uint currentBlock
            )
                internal
            {
                uint currentBlockIndex = currentBlock % SLIDING_WINDOW_SIZE;
                int recordedBuyAmount = int(buyAmount / int(tokenControlInfo[token].minimalRecordResolution));
        
                int prevImbalance = 0;
        
                TokenImbalanceData memory currentBlockData =
                    decodeTokenImbalanceData(tokenImbalanceData[token][currentBlockIndex]);
        
                // first scenario - this is not the first tx in the current block
                if (currentBlockData.lastBlock == currentBlock) {
                    if (uint(currentBlockData.lastRateUpdateBlock) == rateUpdateBlock) {
                        // just increase imbalance
                        currentBlockData.lastBlockBuyUnitsImbalance += recordedBuyAmount;
                        currentBlockData.totalBuyUnitsImbalance += recordedBuyAmount;
                    } else {
                        // imbalance was changed in the middle of the block
                        prevImbalance = getImbalanceInRange(token, rateUpdateBlock, currentBlock);
                        currentBlockData.totalBuyUnitsImbalance = int(prevImbalance) + recordedBuyAmount;
                        currentBlockData.lastBlockBuyUnitsImbalance += recordedBuyAmount;
                        currentBlockData.lastRateUpdateBlock = uint(rateUpdateBlock);
                    }
                } else {
                    // first tx in the current block
                    int currentBlockImbalance;
                    (prevImbalance, currentBlockImbalance) = getImbalanceSinceRateUpdate(token, rateUpdateBlock, currentBlock);
        
                    currentBlockData.lastBlockBuyUnitsImbalance = recordedBuyAmount;
                    currentBlockData.lastBlock = uint(currentBlock);
                    currentBlockData.lastRateUpdateBlock = uint(rateUpdateBlock);
                    currentBlockData.totalBuyUnitsImbalance = int(prevImbalance) + recordedBuyAmount;
                }
        
                tokenImbalanceData[token][currentBlockIndex] = encodeTokenImbalanceData(currentBlockData);
            }
        
            function setGarbageToVolumeRecorder(ERC20 token) internal {
                for (uint i = 0; i < SLIDING_WINDOW_SIZE; i++) {
                    tokenImbalanceData[token][i] = 0x1;
                }
            }
        
            function getImbalanceInRange(ERC20 token, uint startBlock, uint endBlock) internal view returns(int buyImbalance) {
                // check the imbalance in the sliding window
                require(startBlock <= endBlock);
        
                buyImbalance = 0;
        
                for (uint windowInd = 0; windowInd < SLIDING_WINDOW_SIZE; windowInd++) {
                    TokenImbalanceData memory perBlockData = decodeTokenImbalanceData(tokenImbalanceData[token][windowInd]);
        
                    if (perBlockData.lastBlock <= endBlock && perBlockData.lastBlock >= startBlock) {
                        buyImbalance += int(perBlockData.lastBlockBuyUnitsImbalance);
                    }
                }
            }
        
            function getImbalanceSinceRateUpdate(ERC20 token, uint rateUpdateBlock, uint currentBlock)
                internal view
                returns(int buyImbalance, int currentBlockImbalance)
            {
                buyImbalance = 0;
                currentBlockImbalance = 0;
                uint latestBlock = 0;
                int imbalanceInRange = 0;
                uint startBlock = rateUpdateBlock;
                uint endBlock = currentBlock;
        
                for (uint windowInd = 0; windowInd < SLIDING_WINDOW_SIZE; windowInd++) {
                    TokenImbalanceData memory perBlockData = decodeTokenImbalanceData(tokenImbalanceData[token][windowInd]);
        
                    if (perBlockData.lastBlock <= endBlock && perBlockData.lastBlock >= startBlock) {
                        imbalanceInRange += perBlockData.lastBlockBuyUnitsImbalance;
                    }
        
                    if (perBlockData.lastRateUpdateBlock != rateUpdateBlock) continue;
                    if (perBlockData.lastBlock < latestBlock) continue;
        
                    latestBlock = perBlockData.lastBlock;
                    buyImbalance = perBlockData.totalBuyUnitsImbalance;
                    if (uint(perBlockData.lastBlock) == currentBlock) {
                        currentBlockImbalance = perBlockData.lastBlockBuyUnitsImbalance;
                    }
                }
        
                if (buyImbalance == 0) {
                    buyImbalance = imbalanceInRange;
                }
            }
        
            function getImbalance(ERC20 token, uint rateUpdateBlock, uint currentBlock)
                internal view
                returns(int totalImbalance, int currentBlockImbalance)
            {
        
                int resolution = int(tokenControlInfo[token].minimalRecordResolution);
        
                (totalImbalance, currentBlockImbalance) =
                    getImbalanceSinceRateUpdate(
                        token,
                        rateUpdateBlock,
                        currentBlock);
        
                totalImbalance *= resolution;
                currentBlockImbalance *= resolution;
            }
        
            function getMaxPerBlockImbalance(ERC20 token) internal view returns(uint) {
                return tokenControlInfo[token].maxPerBlockImbalance;
            }
        
            function getMaxTotalImbalance(ERC20 token) internal view returns(uint) {
                return tokenControlInfo[token].maxTotalImbalance;
            }
        
            function encodeTokenImbalanceData(TokenImbalanceData data) internal pure returns(uint) {
                // check for overflows
                require(data.lastBlockBuyUnitsImbalance < int(POW_2_64 / 2));
                require(data.lastBlockBuyUnitsImbalance > int(-1 * int(POW_2_64) / 2));
                require(data.lastBlock < POW_2_64);
                require(data.totalBuyUnitsImbalance < int(POW_2_64 / 2));
                require(data.totalBuyUnitsImbalance > int(-1 * int(POW_2_64) / 2));
                require(data.lastRateUpdateBlock < POW_2_64);
        
                // do encoding
                uint result = uint(data.lastBlockBuyUnitsImbalance) & (POW_2_64 - 1);
                result |= data.lastBlock * POW_2_64;
                result |= (uint(data.totalBuyUnitsImbalance) & (POW_2_64 - 1)) * POW_2_64 * POW_2_64;
                result |= data.lastRateUpdateBlock * POW_2_64 * POW_2_64 * POW_2_64;
        
                return result;
            }
        
            function decodeTokenImbalanceData(uint input) internal pure returns(TokenImbalanceData) {
                TokenImbalanceData memory data;
        
                data.lastBlockBuyUnitsImbalance = int(int64(input & (POW_2_64 - 1)));
                data.lastBlock = uint(uint64((input / POW_2_64) & (POW_2_64 - 1)));
                data.totalBuyUnitsImbalance = int(int64((input / (POW_2_64 * POW_2_64)) & (POW_2_64 - 1)));
                data.lastRateUpdateBlock = uint(uint64((input / (POW_2_64 * POW_2_64 * POW_2_64))));
        
                return data;
            }
        }
        
        contract Utils {
        
            ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee);
            uint  constant internal PRECISION = (10**18);
            uint  constant internal MAX_QTY   = (10**28); // 10B tokens
            uint  constant internal MAX_RATE  = (PRECISION * 10**6); // up to 1M tokens per ETH
            uint  constant internal MAX_DECIMALS = 18;
            uint  constant internal ETH_DECIMALS = 18;
            mapping(address=>uint) internal decimals;
        
            function setDecimals(ERC20 token) internal {
                if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS;
                else decimals[token] = token.decimals();
            }
        
            function getDecimals(ERC20 token) internal view returns(uint) {
                if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access
                uint tokenDecimals = decimals[token];
                // technically, there might be token with decimals 0
                // moreover, very possible that old tokens have decimals 0
                // these tokens will just have higher gas fees.
                if(tokenDecimals == 0) return token.decimals();
        
                return tokenDecimals;
            }
        
            function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) {
                require(srcQty <= MAX_QTY);
                require(rate <= MAX_RATE);
        
                if (dstDecimals >= srcDecimals) {
                    require((dstDecimals - srcDecimals) <= MAX_DECIMALS);
                    return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION;
                } else {
                    require((srcDecimals - dstDecimals) <= MAX_DECIMALS);
                    return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals)));
                }
            }
        
            function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) {
                require(dstQty <= MAX_QTY);
                require(rate <= MAX_RATE);
        
                //source quantity is rounded up. to avoid dest quantity being too low.
                uint numerator;
                uint denominator;
                if (srcDecimals >= dstDecimals) {
                    require((srcDecimals - dstDecimals) <= MAX_DECIMALS);
                    numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals)));
                    denominator = rate;
                } else {
                    require((dstDecimals - srcDecimals) <= MAX_DECIMALS);
                    numerator = (PRECISION * dstQty);
                    denominator = (rate * (10**(dstDecimals - srcDecimals)));
                }
                return (numerator + denominator - 1) / denominator; //avoid rounding down errors
            }
        }
        
        contract ConversionRates is ConversionRatesInterface, VolumeImbalanceRecorder, Utils {
        
            // bps - basic rate steps. one step is 1 / 10000 of the rate.
            struct StepFunction {
                int[] x; // quantity for each step. Quantity of each step includes previous steps.
                int[] y; // rate change per quantity step  in bps.
            }
        
            struct TokenData {
                bool listed;  // was added to reserve
                bool enabled; // whether trade is enabled
        
                // position in the compact data
                uint compactDataArrayIndex;
                uint compactDataFieldIndex;
        
                // rate data. base and changes according to quantity and reserve balance.
                // generally speaking. Sell rate is 1 / buy rate i.e. the buy in the other direction.
                uint baseBuyRate;  // in PRECISION units. see KyberConstants
                uint baseSellRate; // PRECISION units. without (sell / buy) spread it is 1 / baseBuyRate
                StepFunction buyRateQtyStepFunction; // in bps. higher quantity - bigger the rate.
                StepFunction sellRateQtyStepFunction;// in bps. higher the qua
                StepFunction buyRateImbalanceStepFunction; // in BPS. higher reserve imbalance - bigger the rate.
                StepFunction sellRateImbalanceStepFunction;
            }
        
            /*
            this is the data for tokenRatesCompactData
            but solidity compiler optimizer is sub-optimal, and cannot write this structure in a single storage write
            so we represent it as bytes32 and do the byte tricks ourselves.
            struct TokenRatesCompactData {
                bytes14 buy;  // change buy rate of token from baseBuyRate in 10 bps
                bytes14 sell; // change sell rate of token from baseSellRate in 10 bps
        
                uint32 blockNumber;
            } */
            uint public validRateDurationInBlocks = 10; // rates are valid for this amount of blocks
            ERC20[] internal listedTokens;
            mapping(address=>TokenData) internal tokenData;
            bytes32[] internal tokenRatesCompactData;
            uint public numTokensInCurrentCompactData = 0;
            address public reserveContract;
            uint constant internal NUM_TOKENS_IN_COMPACT_DATA = 14;
            uint constant internal BYTES_14_OFFSET = (2 ** (8 * NUM_TOKENS_IN_COMPACT_DATA));
            uint constant internal MAX_STEPS_IN_FUNCTION = 10;
            int  constant internal MAX_BPS_ADJUSTMENT = 10 ** 11; // 1B %
            int  constant internal MIN_BPS_ADJUSTMENT = -100 * 100; // cannot go down by more than 100%
        
            function ConversionRates(address _admin) public VolumeImbalanceRecorder(_admin)
                { } // solhint-disable-line no-empty-blocks
        
            function addToken(ERC20 token) public onlyAdmin {
        
                require(!tokenData[token].listed);
                tokenData[token].listed = true;
                listedTokens.push(token);
        
                if (numTokensInCurrentCompactData == 0) {
                    tokenRatesCompactData.length++; // add new structure
                }
        
                tokenData[token].compactDataArrayIndex = tokenRatesCompactData.length - 1;
                tokenData[token].compactDataFieldIndex = numTokensInCurrentCompactData;
        
                numTokensInCurrentCompactData = (numTokensInCurrentCompactData + 1) % NUM_TOKENS_IN_COMPACT_DATA;
        
                setGarbageToVolumeRecorder(token);
        
                setDecimals(token);
            }
        
            function setCompactData(bytes14[] buy, bytes14[] sell, uint blockNumber, uint[] indices) public onlyOperator {
        
                require(buy.length == sell.length);
                require(indices.length == buy.length);
                require(blockNumber <= 0xFFFFFFFF);
        
                uint bytes14Offset = BYTES_14_OFFSET;
        
                for (uint i = 0; i < indices.length; i++) {
                    require(indices[i] < tokenRatesCompactData.length);
                    uint data = uint(buy[i]) | uint(sell[i]) * bytes14Offset | (blockNumber * (bytes14Offset * bytes14Offset));
                    tokenRatesCompactData[indices[i]] = bytes32(data);
                }
            }
        
            function setBaseRate(
                ERC20[] tokens,
                uint[] baseBuy,
                uint[] baseSell,
                bytes14[] buy,
                bytes14[] sell,
                uint blockNumber,
                uint[] indices
            )
                public
                onlyOperator
            {
                require(tokens.length == baseBuy.length);
                require(tokens.length == baseSell.length);
                require(sell.length == buy.length);
                require(sell.length == indices.length);
        
                for (uint ind = 0; ind < tokens.length; ind++) {
                    require(tokenData[tokens[ind]].listed);
                    tokenData[tokens[ind]].baseBuyRate = baseBuy[ind];
                    tokenData[tokens[ind]].baseSellRate = baseSell[ind];
                }
        
                setCompactData(buy, sell, blockNumber, indices);
            }
        
            function setQtyStepFunction(
                ERC20 token,
                int[] xBuy,
                int[] yBuy,
                int[] xSell,
                int[] ySell
            )
                public
                onlyOperator
            {
                require(xBuy.length == yBuy.length);
                require(xSell.length == ySell.length);
                require(xBuy.length <= MAX_STEPS_IN_FUNCTION);
                require(xSell.length <= MAX_STEPS_IN_FUNCTION);
                require(tokenData[token].listed);
        
                tokenData[token].buyRateQtyStepFunction = StepFunction(xBuy, yBuy);
                tokenData[token].sellRateQtyStepFunction = StepFunction(xSell, ySell);
            }
        
            function setImbalanceStepFunction(
                ERC20 token,
                int[] xBuy,
                int[] yBuy,
                int[] xSell,
                int[] ySell
            )
                public
                onlyOperator
            {
                require(xBuy.length == yBuy.length);
                require(xSell.length == ySell.length);
                require(xBuy.length <= MAX_STEPS_IN_FUNCTION);
                require(xSell.length <= MAX_STEPS_IN_FUNCTION);
                require(tokenData[token].listed);
        
                tokenData[token].buyRateImbalanceStepFunction = StepFunction(xBuy, yBuy);
                tokenData[token].sellRateImbalanceStepFunction = StepFunction(xSell, ySell);
            }
        
            function setValidRateDurationInBlocks(uint duration) public onlyAdmin {
                validRateDurationInBlocks = duration;
            }
        
            function enableTokenTrade(ERC20 token) public onlyAdmin {
                require(tokenData[token].listed);
                require(tokenControlInfo[token].minimalRecordResolution != 0);
                tokenData[token].enabled = true;
            }
        
            function disableTokenTrade(ERC20 token) public onlyAlerter {
                require(tokenData[token].listed);
                tokenData[token].enabled = false;
            }
        
            function setReserveAddress(address reserve) public onlyAdmin {
                reserveContract = reserve;
            }
        
            function recordImbalance(
                ERC20 token,
                int buyAmount,
                uint rateUpdateBlock,
                uint currentBlock
            )
                public
            {
                require(msg.sender == reserveContract);
        
                if (rateUpdateBlock == 0) rateUpdateBlock = getRateUpdateBlock(token);
        
                return addImbalance(token, buyAmount, rateUpdateBlock, currentBlock);
            }
        
            /* solhint-disable function-max-lines */
            function getRate(ERC20 token, uint currentBlockNumber, bool buy, uint qty) public view returns(uint) {
                // check if trade is enabled
                if (!tokenData[token].enabled) return 0;
                if (tokenControlInfo[token].minimalRecordResolution == 0) return 0; // token control info not set
        
                // get rate update block
                bytes32 compactData = tokenRatesCompactData[tokenData[token].compactDataArrayIndex];
        
                uint updateRateBlock = getLast4Bytes(compactData);
                if (currentBlockNumber >= updateRateBlock + validRateDurationInBlocks) return 0; // rate is expired
                // check imbalance
                int totalImbalance;
                int blockImbalance;
                (totalImbalance, blockImbalance) = getImbalance(token, updateRateBlock, currentBlockNumber);
        
                // calculate actual rate
                int imbalanceQty;
                int extraBps;
                int8 rateUpdate;
                uint rate;
        
                if (buy) {
                    // start with base rate
                    rate = tokenData[token].baseBuyRate;
        
                    // add rate update
                    rateUpdate = getRateByteFromCompactData(compactData, token, true);
                    extraBps = int(rateUpdate) * 10;
                    rate = addBps(rate, extraBps);
        
                    // compute token qty
                    qty = getTokenQty(token, rate, qty);
                    imbalanceQty = int(qty);
                    totalImbalance += imbalanceQty;
        
                    // add qty overhead
                    extraBps = executeStepFunction(tokenData[token].buyRateQtyStepFunction, int(qty));
                    rate = addBps(rate, extraBps);
        
                    // add imbalance overhead
                    extraBps = executeStepFunction(tokenData[token].buyRateImbalanceStepFunction, totalImbalance);
                    rate = addBps(rate, extraBps);
                } else {
                    // start with base rate
                    rate = tokenData[token].baseSellRate;
        
                    // add rate update
                    rateUpdate = getRateByteFromCompactData(compactData, token, false);
                    extraBps = int(rateUpdate) * 10;
                    rate = addBps(rate, extraBps);
        
                    // compute token qty
                    imbalanceQty = -1 * int(qty);
                    totalImbalance += imbalanceQty;
        
                    // add qty overhead
                    extraBps = executeStepFunction(tokenData[token].sellRateQtyStepFunction, int(qty));
                    rate = addBps(rate, extraBps);
        
                    // add imbalance overhead
                    extraBps = executeStepFunction(tokenData[token].sellRateImbalanceStepFunction, totalImbalance);
                    rate = addBps(rate, extraBps);
                }
        
                if (abs(totalImbalance) >= getMaxTotalImbalance(token)) return 0;
                if (abs(blockImbalance + imbalanceQty) >= getMaxPerBlockImbalance(token)) return 0;
        
                return rate;
            }
            /* solhint-enable function-max-lines */
        
            function getBasicRate(ERC20 token, bool buy) public view returns(uint) {
                if (buy)
                    return tokenData[token].baseBuyRate;
                else
                    return tokenData[token].baseSellRate;
            }
        
            function getCompactData(ERC20 token) public view returns(uint, uint, byte, byte) {
                require(tokenData[token].listed);
        
                uint arrayIndex = tokenData[token].compactDataArrayIndex;
                uint fieldOffset = tokenData[token].compactDataFieldIndex;
        
                return (
                    arrayIndex,
                    fieldOffset,
                    byte(getRateByteFromCompactData(tokenRatesCompactData[arrayIndex], token, true)),
                    byte(getRateByteFromCompactData(tokenRatesCompactData[arrayIndex], token, false))
                );
            }
        
            function getTokenBasicData(ERC20 token) public view returns(bool, bool) {
                return (tokenData[token].listed, tokenData[token].enabled);
            }
        
            /* solhint-disable code-complexity */
            function getStepFunctionData(ERC20 token, uint command, uint param) public view returns(int) {
                if (command == 0) return int(tokenData[token].buyRateQtyStepFunction.x.length);
                if (command == 1) return tokenData[token].buyRateQtyStepFunction.x[param];
                if (command == 2) return int(tokenData[token].buyRateQtyStepFunction.y.length);
                if (command == 3) return tokenData[token].buyRateQtyStepFunction.y[param];
        
                if (command == 4) return int(tokenData[token].sellRateQtyStepFunction.x.length);
                if (command == 5) return tokenData[token].sellRateQtyStepFunction.x[param];
                if (command == 6) return int(tokenData[token].sellRateQtyStepFunction.y.length);
                if (command == 7) return tokenData[token].sellRateQtyStepFunction.y[param];
        
                if (command == 8) return int(tokenData[token].buyRateImbalanceStepFunction.x.length);
                if (command == 9) return tokenData[token].buyRateImbalanceStepFunction.x[param];
                if (command == 10) return int(tokenData[token].buyRateImbalanceStepFunction.y.length);
                if (command == 11) return tokenData[token].buyRateImbalanceStepFunction.y[param];
        
                if (command == 12) return int(tokenData[token].sellRateImbalanceStepFunction.x.length);
                if (command == 13) return tokenData[token].sellRateImbalanceStepFunction.x[param];
                if (command == 14) return int(tokenData[token].sellRateImbalanceStepFunction.y.length);
                if (command == 15) return tokenData[token].sellRateImbalanceStepFunction.y[param];
        
                revert();
            }
            /* solhint-enable code-complexity */
        
            function getRateUpdateBlock(ERC20 token) public view returns(uint) {
                bytes32 compactData = tokenRatesCompactData[tokenData[token].compactDataArrayIndex];
                return getLast4Bytes(compactData);
            }
        
            function getListedTokens() public view returns(ERC20[]) {
                return listedTokens;
            }
        
            function getTokenQty(ERC20 token, uint ethQty, uint rate) internal view returns(uint) {
                uint dstDecimals = getDecimals(token);
                uint srcDecimals = ETH_DECIMALS;
        
                return calcDstQty(ethQty, srcDecimals, dstDecimals, rate);
            }
        
            function getLast4Bytes(bytes32 b) internal pure returns(uint) {
                // cannot trust compiler with not turning bit operations into EXP opcode
                return uint(b) / (BYTES_14_OFFSET * BYTES_14_OFFSET);
            }
        
            function getRateByteFromCompactData(bytes32 data, ERC20 token, bool buy) internal view returns(int8) {
                uint fieldOffset = tokenData[token].compactDataFieldIndex;
                uint byteOffset;
                if (buy)
                    byteOffset = 32 - NUM_TOKENS_IN_COMPACT_DATA + fieldOffset;
                else
                    byteOffset = 4 + fieldOffset;
        
                return int8(data[byteOffset]);
            }
        
            function executeStepFunction(StepFunction f, int x) internal pure returns(int) {
                uint len = f.y.length;
                for (uint ind = 0; ind < len; ind++) {
                    if (x <= f.x[ind]) return f.y[ind];
                }
        
                return f.y[len-1];
            }
        
            function addBps(uint rate, int bps) internal pure returns(uint) {
                require(rate <= MAX_RATE);
                require(bps >= MIN_BPS_ADJUSTMENT);
                require(bps <= MAX_BPS_ADJUSTMENT);
        
                uint maxBps = 100 * 100;
                return (rate * uint(int(maxBps) + bps)) / maxBps;
            }
        
            function abs(int x) internal pure returns(uint) {
                if (x < 0)
                    return uint(-1 * x);
                else
                    return uint(x);
            }
        }

        File 2 of 8: BancorConverter
        pragma solidity ^0.4.21;
        
        /*
            Owned contract interface
        */
        contract IOwned {
            // this function isn't abstract since the compiler emits automatically generated getter functions as external
            function owner() public view returns (address) {}
        
            function transferOwnership(address _newOwner) public;
            function acceptOwnership() public;
        }
        
        /*
            ERC20 Standard Token interface
        */
        contract IERC20Token {
            // these functions aren't abstract since the compiler emits automatically generated getter functions as external
            function name() public view returns (string) {}
            function symbol() public view returns (string) {}
            function decimals() public view returns (uint8) {}
            function totalSupply() public view returns (uint256) {}
            function balanceOf(address _owner) public view returns (uint256) { _owner; }
            function allowance(address _owner, address _spender) public view returns (uint256) { _owner; _spender; }
        
            function transfer(address _to, uint256 _value) public returns (bool success);
            function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
            function approve(address _spender, uint256 _value) public returns (bool success);
        }
        
        /*
            Smart Token interface
        */
        contract ISmartToken is IOwned, IERC20Token {
            function disableTransfers(bool _disable) public;
            function issue(address _to, uint256 _amount) public;
            function destroy(address _from, uint256 _amount) public;
        }
        
        /*
            Contract Registry interface
        */
        contract IContractRegistry {
            function getAddress(bytes32 _contractName) public view returns (address);
        }
        
        /*
            Contract Features interface
        */
        contract IContractFeatures {
            function isSupported(address _contract, uint256 _features) public view returns (bool);
            function enableFeatures(uint256 _features, bool _enable) public;
        }
        
        /*
            Whitelist interface
        */
        contract IWhitelist {
            function isWhitelisted(address _address) public view returns (bool);
        }
        
        /*
            Token Holder interface
        */
        contract ITokenHolder is IOwned {
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount) public;
        }
        
        /*
            Bancor Formula interface
        */
        contract IBancorFormula {
            function calculatePurchaseReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _depositAmount) public view returns (uint256);
            function calculateSaleReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _sellAmount) public view returns (uint256);
            function calculateCrossConnectorReturn(uint256 _fromConnectorBalance, uint32 _fromConnectorWeight, uint256 _toConnectorBalance, uint32 _toConnectorWeight, uint256 _amount) public view returns (uint256);
        }
        
        /*
            Bancor Converter interface
        */
        contract IBancorConverter {
            function getReturn(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount) public view returns (uint256);
            function convert(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256);
            function conversionWhitelist() public view returns (IWhitelist) {}
            // deprecated, backward compatibility
            function change(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256);
        }
        
        /*
            Bancor Network interface
        */
        contract IBancorNetwork {
            function convert(IERC20Token[] _path, uint256 _amount, uint256 _minReturn) public payable returns (uint256);
            function convertFor(IERC20Token[] _path, uint256 _amount, uint256 _minReturn, address _for) public payable returns (uint256);
            function convertForPrioritized2(
                IERC20Token[] _path,
                uint256 _amount,
                uint256 _minReturn,
                address _for,
                uint256 _block,
                uint8 _v,
                bytes32 _r,
                bytes32 _s)
                public payable returns (uint256);
        
            // deprecated, backward compatibility
            function convertForPrioritized(
                IERC20Token[] _path,
                uint256 _amount,
                uint256 _minReturn,
                address _for,
                uint256 _block,
                uint256 _nonce,
                uint8 _v,
                bytes32 _r,
                bytes32 _s)
                public payable returns (uint256);
        }
        
        /*
            Utilities & Common Modifiers
        */
        contract Utils {
            /**
                constructor
            */
            function Utils() public {
            }
        
            // verifies that an amount is greater than zero
            modifier greaterThanZero(uint256 _amount) {
                require(_amount > 0);
                _;
            }
        
            // validates an address - currently only checks that it isn't null
            modifier validAddress(address _address) {
                require(_address != address(0));
                _;
            }
        
            // verifies that the address is different than this contract address
            modifier notThis(address _address) {
                require(_address != address(this));
                _;
            }
        
            // Overflow protected math functions
        
            /**
                @dev returns the sum of _x and _y, asserts if the calculation overflows
        
                @param _x   value 1
                @param _y   value 2
        
                @return sum
            */
            function safeAdd(uint256 _x, uint256 _y) internal pure returns (uint256) {
                uint256 z = _x + _y;
                assert(z >= _x);
                return z;
            }
        
            /**
                @dev returns the difference of _x minus _y, asserts if the subtraction results in a negative number
        
                @param _x   minuend
                @param _y   subtrahend
        
                @return difference
            */
            function safeSub(uint256 _x, uint256 _y) internal pure returns (uint256) {
                assert(_x >= _y);
                return _x - _y;
            }
        
            /**
                @dev returns the product of multiplying _x by _y, asserts if the calculation overflows
        
                @param _x   factor 1
                @param _y   factor 2
        
                @return product
            */
            function safeMul(uint256 _x, uint256 _y) internal pure returns (uint256) {
                uint256 z = _x * _y;
                assert(_x == 0 || z / _x == _y);
                return z;
            }
        }
        
        /*
            Provides support and utilities for contract ownership
        */
        contract Owned is IOwned {
            address public owner;
            address public newOwner;
        
            event OwnerUpdate(address indexed _prevOwner, address indexed _newOwner);
        
            /**
                @dev constructor
            */
            function Owned() public {
                owner = msg.sender;
            }
        
            // allows execution by the owner only
            modifier ownerOnly {
                assert(msg.sender == owner);
                _;
            }
        
            /**
                @dev allows transferring the contract ownership
                the new owner still needs to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new contract owner
            */
            function transferOwnership(address _newOwner) public ownerOnly {
                require(_newOwner != owner);
                newOwner = _newOwner;
            }
        
            /**
                @dev used by a new owner to accept an ownership transfer
            */
            function acceptOwnership() public {
                require(msg.sender == newOwner);
                emit OwnerUpdate(owner, newOwner);
                owner = newOwner;
                newOwner = address(0);
            }
        }
        
        /*
            Provides support and utilities for contract management
            Note that a managed contract must also have an owner
        */
        contract Managed is Owned {
            address public manager;
            address public newManager;
        
            event ManagerUpdate(address indexed _prevManager, address indexed _newManager);
        
            /**
                @dev constructor
            */
            function Managed() public {
                manager = msg.sender;
            }
        
            // allows execution by the manager only
            modifier managerOnly {
                assert(msg.sender == manager);
                _;
            }
        
            // allows execution by either the owner or the manager only
            modifier ownerOrManagerOnly {
                require(msg.sender == owner || msg.sender == manager);
                _;
            }
        
            /**
                @dev allows transferring the contract management
                the new manager still needs to accept the transfer
                can only be called by the contract manager
        
                @param _newManager    new contract manager
            */
            function transferManagement(address _newManager) public ownerOrManagerOnly {
                require(_newManager != manager);
                newManager = _newManager;
            }
        
            /**
                @dev used by a new manager to accept a management transfer
            */
            function acceptManagement() public {
                require(msg.sender == newManager);
                emit ManagerUpdate(manager, newManager);
                manager = newManager;
                newManager = address(0);
            }
        }
        
        /**
            Id definitions for bancor contracts
        
            Can be used in conjunction with the contract registry to get contract addresses
        */
        contract ContractIds {
            // generic
            bytes32 public constant CONTRACT_FEATURES = "ContractFeatures";
        
            // bancor logic
            bytes32 public constant BANCOR_NETWORK = "BancorNetwork";
            bytes32 public constant BANCOR_FORMULA = "BancorFormula";
            bytes32 public constant BANCOR_GAS_PRICE_LIMIT = "BancorGasPriceLimit";
        
            bytes32 public constant BANCOR_CONVERTER_FACTORY = "BancorConverterFactory";
            bytes32 public constant BANCOR_CONVERTER_UPGRADER = "BancorConverterUpgrader";
        
            // tokens
            bytes32 public constant BNT_TOKEN = "BNTToken";
        }
        
        /**
            Id definitions for bancor contract features
        
            Can be used to query the ContractFeatures contract to check whether a certain feature is supported by a contract
        */
        contract FeatureIds {
            // converter features
            uint256 public constant CONVERTER_CONVERSION_WHITELIST = 1 << 0;
        }
        
        /*
            We consider every contract to be a 'token holder' since it's currently not possible
            for a contract to deny receiving tokens.
        
            The TokenHolder's contract sole purpose is to provide a safety mechanism that allows
            the owner to send tokens that were sent to the contract by mistake back to their sender.
        */
        contract TokenHolder is ITokenHolder, Owned, Utils {
            /**
                @dev constructor
            */
            function TokenHolder() public {
            }
        
            /**
                @dev withdraws tokens held by the contract and sends them to an account
                can only be called by the owner
        
                @param _token   ERC20 token contract address
                @param _to      account to receive the new amount
                @param _amount  amount to withdraw
            */
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount)
                public
                ownerOnly
                validAddress(_token)
                validAddress(_to)
                notThis(_to)
            {
                assert(_token.transfer(_to, _amount));
            }
        }
        
        /*
            The smart token controller is an upgradable part of the smart token that allows
            more functionality as well as fixes for bugs/exploits.
            Once it accepts ownership of the token, it becomes the token's sole controller
            that can execute any of its functions.
        
            To upgrade the controller, ownership must be transferred to a new controller, along with
            any relevant data.
        
            The smart token must be set on construction and cannot be changed afterwards.
            Wrappers are provided (as opposed to a single 'execute' function) for each of the token's functions, for easier access.
        
            Note that the controller can transfer token ownership to a new controller that
            doesn't allow executing any function on the token, for a trustless solution.
            Doing that will also remove the owner's ability to upgrade the controller.
        */
        contract SmartTokenController is TokenHolder {
            ISmartToken public token;   // smart token
        
            /**
                @dev constructor
            */
            function SmartTokenController(ISmartToken _token)
                public
                validAddress(_token)
            {
                token = _token;
            }
        
            // ensures that the controller is the token's owner
            modifier active() {
                assert(token.owner() == address(this));
                _;
            }
        
            // ensures that the controller is not the token's owner
            modifier inactive() {
                assert(token.owner() != address(this));
                _;
            }
        
            /**
                @dev allows transferring the token ownership
                the new owner still need to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new token owner
            */
            function transferTokenOwnership(address _newOwner) public ownerOnly {
                token.transferOwnership(_newOwner);
            }
        
            /**
                @dev used by a new owner to accept a token ownership transfer
                can only be called by the contract owner
            */
            function acceptTokenOwnership() public ownerOnly {
                token.acceptOwnership();
            }
        
            /**
                @dev disables/enables token transfers
                can only be called by the contract owner
        
                @param _disable    true to disable transfers, false to enable them
            */
            function disableTokenTransfers(bool _disable) public ownerOnly {
                token.disableTransfers(_disable);
            }
        
            /**
                @dev withdraws tokens held by the controller and sends them to an account
                can only be called by the owner
        
                @param _token   ERC20 token contract address
                @param _to      account to receive the new amount
                @param _amount  amount to withdraw
            */
            function withdrawFromToken(
                IERC20Token _token, 
                address _to, 
                uint256 _amount
            ) 
                public
                ownerOnly
            {
                ITokenHolder(token).withdrawTokens(_token, _to, _amount);
            }
        }
        
        /*
            Bancor Converter v0.9
        
            The Bancor version of the token converter, allows conversion between a smart token and other ERC20 tokens and between different ERC20 tokens and themselves.
        
            ERC20 connector balance can be virtual, meaning that the calculations are based on the virtual balance instead of relying on
            the actual connector balance. This is a security mechanism that prevents the need to keep a very large (and valuable) balance in a single contract.
        
            The converter is upgradable (just like any SmartTokenController).
        
            WARNING: It is NOT RECOMMENDED to use the converter with Smart Tokens that have less than 8 decimal digits
                     or with very small numbers because of precision loss
        
            Open issues:
            - Front-running attacks are currently mitigated by the following mechanisms:
                - minimum return argument for each conversion provides a way to define a minimum/maximum price for the transaction
                - gas price limit prevents users from having control over the order of execution
                - gas price limit check can be skipped if the transaction comes from a trusted, whitelisted signer
              Other potential solutions might include a commit/reveal based schemes
            - Possibly add getters for the connector fields so that the client won't need to rely on the order in the struct
        */
        contract BancorConverter is IBancorConverter, SmartTokenController, Managed, ContractIds, FeatureIds {
            uint32 private constant MAX_WEIGHT = 1000000;
            uint64 private constant MAX_CONVERSION_FEE = 1000000;
        
            struct Connector {
                uint256 virtualBalance;         // connector virtual balance
                uint32 weight;                  // connector weight, represented in ppm, 1-1000000
                bool isVirtualBalanceEnabled;   // true if virtual balance is enabled, false if not
                bool isPurchaseEnabled;         // is purchase of the smart token enabled with the connector, can be set by the owner
                bool isSet;                     // used to tell if the mapping element is defined
            }
        
            string public version = '0.9';
            string public converterType = 'bancor';
        
            IContractRegistry public registry;                  // contract registry contract
            IWhitelist public conversionWhitelist;              // whitelist contract with list of addresses that are allowed to use the converter
            IERC20Token[] public connectorTokens;               // ERC20 standard token addresses
            IERC20Token[] public quickBuyPath;                  // conversion path that's used in order to buy the token with ETH
            mapping (address => Connector) public connectors;   // connector token addresses -> connector data
            uint32 private totalConnectorWeight = 0;            // used to efficiently prevent increasing the total connector weight above 100%
            uint32 public maxConversionFee = 0;                 // maximum conversion fee for the lifetime of the contract,
                                                                // represented in ppm, 0...1000000 (0 = no fee, 100 = 0.01%, 1000000 = 100%)
            uint32 public conversionFee = 0;                    // current conversion fee, represented in ppm, 0...maxConversionFee
            bool public conversionsEnabled = true;              // true if token conversions is enabled, false if not
            IERC20Token[] private convertPath;
        
            // triggered when a conversion between two tokens occurs
            event Conversion(
                address indexed _fromToken,
                address indexed _toToken,
                address indexed _trader,
                uint256 _amount,
                uint256 _return,
                int256 _conversionFee
            );
            // triggered after a conversion with new price data
            event PriceDataUpdate(
                address indexed _connectorToken,
                uint256 _tokenSupply,
                uint256 _connectorBalance,
                uint32 _connectorWeight
            );
            // triggered when the conversion fee is updated
            event ConversionFeeUpdate(uint32 _prevFee, uint32 _newFee);
        
            /**
                @dev constructor
        
                @param  _token              smart token governed by the converter
                @param  _registry           address of a contract registry contract
                @param  _maxConversionFee   maximum conversion fee, represented in ppm
                @param  _connectorToken     optional, initial connector, allows defining the first connector at deployment time
                @param  _connectorWeight    optional, weight for the initial connector
            */
            function BancorConverter(
                ISmartToken _token,
                IContractRegistry _registry,
                uint32 _maxConversionFee,
                IERC20Token _connectorToken,
                uint32 _connectorWeight
            )
                public
                SmartTokenController(_token)
                validAddress(_registry)
                validMaxConversionFee(_maxConversionFee)
            {
                registry = _registry;
                IContractFeatures features = IContractFeatures(registry.getAddress(ContractIds.CONTRACT_FEATURES));
        
                // initialize supported features
                if (features != address(0))
                    features.enableFeatures(FeatureIds.CONVERTER_CONVERSION_WHITELIST, true);
        
                maxConversionFee = _maxConversionFee;
        
                if (_connectorToken != address(0))
                    addConnector(_connectorToken, _connectorWeight, false);
            }
        
            // validates a connector token address - verifies that the address belongs to one of the connector tokens
            modifier validConnector(IERC20Token _address) {
                require(connectors[_address].isSet);
                _;
            }
        
            // validates a token address - verifies that the address belongs to one of the convertible tokens
            modifier validToken(IERC20Token _address) {
                require(_address == token || connectors[_address].isSet);
                _;
            }
        
            // validates maximum conversion fee
            modifier validMaxConversionFee(uint32 _conversionFee) {
                require(_conversionFee >= 0 && _conversionFee <= MAX_CONVERSION_FEE);
                _;
            }
        
            // validates conversion fee
            modifier validConversionFee(uint32 _conversionFee) {
                require(_conversionFee >= 0 && _conversionFee <= maxConversionFee);
                _;
            }
        
            // validates connector weight range
            modifier validConnectorWeight(uint32 _weight) {
                require(_weight > 0 && _weight <= MAX_WEIGHT);
                _;
            }
        
            // validates a conversion path - verifies that the number of elements is odd and that maximum number of 'hops' is 10
            modifier validConversionPath(IERC20Token[] _path) {
                require(_path.length > 2 && _path.length <= (1 + 2 * 10) && _path.length % 2 == 1);
                _;
            }
        
            // allows execution only when conversions aren't disabled
            modifier conversionsAllowed {
                assert(conversionsEnabled);
                _;
            }
        
            // allows execution by the BancorNetwork contract only
            modifier bancorNetworkOnly {
                IBancorNetwork bancorNetwork = IBancorNetwork(registry.getAddress(ContractIds.BANCOR_NETWORK));
                require(msg.sender == address(bancorNetwork));
                _;
            }
        
            /**
                @dev returns the number of connector tokens defined
        
                @return number of connector tokens
            */
            function connectorTokenCount() public view returns (uint16) {
                return uint16(connectorTokens.length);
            }
        
            /*
                @dev allows the owner to update the registry contract address
        
                @param _registry    address of a bancor converter registry contract
            */
            function setRegistry(IContractRegistry _registry)
                public
                ownerOnly
                validAddress(_registry)
                notThis(_registry)
            {
                registry = _registry;
            }
        
            /*
                @dev allows the owner to update & enable the conversion whitelist contract address
                when set, only addresses that are whitelisted are actually allowed to use the converter
                note that the whitelist check is actually done by the BancorNetwork contract
        
                @param _whitelist    address of a whitelist contract
            */
            function setConversionWhitelist(IWhitelist _whitelist)
                public
                ownerOnly
                notThis(_whitelist)
            {
                conversionWhitelist = _whitelist;
            }
        
            /*
                @dev allows the manager to update the quick buy path
        
                @param _path    new quick buy path, see conversion path format in the bancorNetwork contract
            */
            function setQuickBuyPath(IERC20Token[] _path)
                public
                ownerOnly
                validConversionPath(_path)
            {
                quickBuyPath = _path;
            }
        
            /*
                @dev allows the manager to clear the quick buy path
            */
            function clearQuickBuyPath() public ownerOnly {
                quickBuyPath.length = 0;
            }
        
            /**
                @dev returns the length of the quick buy path array
        
                @return quick buy path length
            */
            function getQuickBuyPathLength() public view returns (uint256) {
                return quickBuyPath.length;
            }
        
            /**
                @dev disables the entire conversion functionality
                this is a safety mechanism in case of a emergency
                can only be called by the manager
        
                @param _disable true to disable conversions, false to re-enable them
            */
            function disableConversions(bool _disable) public ownerOrManagerOnly {
                conversionsEnabled = !_disable;
            }
        
            /**
                @dev updates the current conversion fee
                can only be called by the manager
        
                @param _conversionFee new conversion fee, represented in ppm
            */
            function setConversionFee(uint32 _conversionFee)
                public
                ownerOrManagerOnly
                validConversionFee(_conversionFee)
            {
                emit ConversionFeeUpdate(conversionFee, _conversionFee);
                conversionFee = _conversionFee;
            }
        
            /*
                @dev given a return amount, returns the amount minus the conversion fee
        
                @param _amount      return amount
                @param _magnitude   1 for standard conversion, 2 for cross connector conversion
        
                @return return amount minus conversion fee
            */
            function getFinalAmount(uint256 _amount, uint8 _magnitude) public view returns (uint256) {
                return safeMul(_amount, (MAX_CONVERSION_FEE - conversionFee) ** _magnitude) / MAX_CONVERSION_FEE ** _magnitude;
            }
        
            /**
                @dev defines a new connector for the token
                can only be called by the owner while the converter is inactive
        
                @param _token                  address of the connector token
                @param _weight                 constant connector weight, represented in ppm, 1-1000000
                @param _enableVirtualBalance   true to enable virtual balance for the connector, false to disable it
            */
            function addConnector(IERC20Token _token, uint32 _weight, bool _enableVirtualBalance)
                public
                ownerOnly
                inactive
                validAddress(_token)
                notThis(_token)
                validConnectorWeight(_weight)
            {
                require(_token != token && !connectors[_token].isSet && totalConnectorWeight + _weight <= MAX_WEIGHT); // validate input
        
                connectors[_token].virtualBalance = 0;
                connectors[_token].weight = _weight;
                connectors[_token].isVirtualBalanceEnabled = _enableVirtualBalance;
                connectors[_token].isPurchaseEnabled = true;
                connectors[_token].isSet = true;
                connectorTokens.push(_token);
                totalConnectorWeight += _weight;
            }
        
            /**
                @dev updates one of the token connectors
                can only be called by the owner
        
                @param _connectorToken         address of the connector token
                @param _weight                 constant connector weight, represented in ppm, 1-1000000
                @param _enableVirtualBalance   true to enable virtual balance for the connector, false to disable it
                @param _virtualBalance         new connector's virtual balance
            */
            function updateConnector(IERC20Token _connectorToken, uint32 _weight, bool _enableVirtualBalance, uint256 _virtualBalance)
                public
                ownerOnly
                validConnector(_connectorToken)
                validConnectorWeight(_weight)
            {
                Connector storage connector = connectors[_connectorToken];
                require(totalConnectorWeight - connector.weight + _weight <= MAX_WEIGHT); // validate input
        
                totalConnectorWeight = totalConnectorWeight - connector.weight + _weight;
                connector.weight = _weight;
                connector.isVirtualBalanceEnabled = _enableVirtualBalance;
                connector.virtualBalance = _virtualBalance;
            }
        
            /**
                @dev disables purchasing with the given connector token in case the connector token got compromised
                can only be called by the owner
                note that selling is still enabled regardless of this flag and it cannot be disabled by the owner
        
                @param _connectorToken  connector token contract address
                @param _disable         true to disable the token, false to re-enable it
            */
            function disableConnectorPurchases(IERC20Token _connectorToken, bool _disable)
                public
                ownerOnly
                validConnector(_connectorToken)
            {
                connectors[_connectorToken].isPurchaseEnabled = !_disable;
            }
        
            /**
                @dev returns the connector's virtual balance if one is defined, otherwise returns the actual balance
        
                @param _connectorToken  connector token contract address
        
                @return connector balance
            */
            function getConnectorBalance(IERC20Token _connectorToken)
                public
                view
                validConnector(_connectorToken)
                returns (uint256)
            {
                Connector storage connector = connectors[_connectorToken];
                return connector.isVirtualBalanceEnabled ? connector.virtualBalance : _connectorToken.balanceOf(this);
            }
        
            /**
                @dev returns the expected return for converting a specific amount of _fromToken to _toToken
        
                @param _fromToken  ERC20 token to convert from
                @param _toToken    ERC20 token to convert to
                @param _amount     amount to convert, in fromToken
        
                @return expected conversion return amount
            */
            function getReturn(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount) public view returns (uint256) {
                require(_fromToken != _toToken); // validate input
        
                // conversion between the token and one of its connectors
                if (_toToken == token)
                    return getPurchaseReturn(_fromToken, _amount);
                else if (_fromToken == token)
                    return getSaleReturn(_toToken, _amount);
        
                // conversion between 2 connectors
                return getCrossConnectorReturn(_fromToken, _toToken, _amount);
            }
        
            /**
                @dev returns the expected return for buying the token for a connector token
        
                @param _connectorToken  connector token contract address
                @param _depositAmount   amount to deposit (in the connector token)
        
                @return expected purchase return amount
            */
            function getPurchaseReturn(IERC20Token _connectorToken, uint256 _depositAmount)
                public
                view
                active
                validConnector(_connectorToken)
                returns (uint256)
            {
                Connector storage connector = connectors[_connectorToken];
                require(connector.isPurchaseEnabled); // validate input
        
                uint256 tokenSupply = token.totalSupply();
                uint256 connectorBalance = getConnectorBalance(_connectorToken);
                IBancorFormula formula = IBancorFormula(registry.getAddress(ContractIds.BANCOR_FORMULA));
                uint256 amount = formula.calculatePurchaseReturn(tokenSupply, connectorBalance, connector.weight, _depositAmount);
        
                // return the amount minus the conversion fee
                return getFinalAmount(amount, 1);
            }
        
            /**
                @dev returns the expected return for selling the token for one of its connector tokens
        
                @param _connectorToken  connector token contract address
                @param _sellAmount      amount to sell (in the smart token)
        
                @return expected sale return amount
            */
            function getSaleReturn(IERC20Token _connectorToken, uint256 _sellAmount)
                public
                view
                active
                validConnector(_connectorToken)
                returns (uint256)
            {
                Connector storage connector = connectors[_connectorToken];
                uint256 tokenSupply = token.totalSupply();
                uint256 connectorBalance = getConnectorBalance(_connectorToken);
                IBancorFormula formula = IBancorFormula(registry.getAddress(ContractIds.BANCOR_FORMULA));
                uint256 amount = formula.calculateSaleReturn(tokenSupply, connectorBalance, connector.weight, _sellAmount);
        
                // return the amount minus the conversion fee
                return getFinalAmount(amount, 1);
            }
        
            /**
                @dev returns the expected return for selling one of the connector tokens for another connector token
        
                @param _fromConnectorToken  contract address of the connector token to convert from
                @param _toConnectorToken    contract address of the connector token to convert to
                @param _sellAmount          amount to sell (in the from connector token)
        
                @return expected sale return amount (in the to connector token)
            */
            function getCrossConnectorReturn(IERC20Token _fromConnectorToken, IERC20Token _toConnectorToken, uint256 _sellAmount)
                public
                view
                active
                validConnector(_fromConnectorToken)
                validConnector(_toConnectorToken)
                returns (uint256)
            {
                Connector storage fromConnector = connectors[_fromConnectorToken];
                Connector storage toConnector = connectors[_toConnectorToken];
                require(toConnector.isPurchaseEnabled); // validate input
        
                uint256 fromConnectorBalance = getConnectorBalance(_fromConnectorToken);
                uint256 toConnectorBalance = getConnectorBalance(_toConnectorToken);
        
                IBancorFormula formula = IBancorFormula(registry.getAddress(ContractIds.BANCOR_FORMULA));
                uint256 amount = formula.calculateCrossConnectorReturn(fromConnectorBalance, fromConnector.weight, toConnectorBalance, toConnector.weight, _sellAmount);
        
                // return the amount minus the conversion fee
                // the fee is higher (magnitude = 2) since cross connector conversion equals 2 conversions (from / to the smart token)
                return getFinalAmount(amount, 2);
            }
        
            /**
                @dev converts a specific amount of _fromToken to _toToken
        
                @param _fromToken  ERC20 token to convert from
                @param _toToken    ERC20 token to convert to
                @param _amount     amount to convert, in fromToken
                @param _minReturn  if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return conversion return amount
            */
            function convertInternal(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn)
                public
                bancorNetworkOnly
                conversionsAllowed
                greaterThanZero(_minReturn)
                returns (uint256)
            {
                require(_fromToken != _toToken); // validate input
        
                // conversion between the token and one of its connectors
                if (_toToken == token)
                    return buy(_fromToken, _amount, _minReturn);
                else if (_fromToken == token)
                    return sell(_toToken, _amount, _minReturn);
        
                // conversion between 2 connectors
                uint256 amount = getCrossConnectorReturn(_fromToken, _toToken, _amount);
                // ensure the trade gives something in return and meets the minimum requested amount
                require(amount != 0 && amount >= _minReturn);
        
                // update the source token virtual balance if relevant
                Connector storage fromConnector = connectors[_fromToken];
                if (fromConnector.isVirtualBalanceEnabled)
                    fromConnector.virtualBalance = safeAdd(fromConnector.virtualBalance, _amount);
        
                // update the target token virtual balance if relevant
                Connector storage toConnector = connectors[_toToken];
                if (toConnector.isVirtualBalanceEnabled)
                    toConnector.virtualBalance = safeSub(toConnector.virtualBalance, amount);
        
                // ensure that the trade won't deplete the connector balance
                uint256 toConnectorBalance = getConnectorBalance(_toToken);
                assert(amount < toConnectorBalance);
        
                // transfer funds from the caller in the from connector token
                assert(_fromToken.transferFrom(msg.sender, this, _amount));
                // transfer funds to the caller in the to connector token
                // the transfer might fail if the actual connector balance is smaller than the virtual balance
                assert(_toToken.transfer(msg.sender, amount));
        
                // calculate conversion fee and dispatch the conversion event
                // the fee is higher (magnitude = 2) since cross connector conversion equals 2 conversions (from / to the smart token)
                uint256 feeAmount = safeSub(amount, getFinalAmount(amount, 2));
                dispatchConversionEvent(_fromToken, _toToken, _amount, amount, feeAmount);
        
                // dispatch price data updates for the smart token / both connectors
                emit PriceDataUpdate(_fromToken, token.totalSupply(), getConnectorBalance(_fromToken), fromConnector.weight);
                emit PriceDataUpdate(_toToken, token.totalSupply(), getConnectorBalance(_toToken), toConnector.weight);
                return amount;
            }
        
            /**
                @dev converts a specific amount of _fromToken to _toToken
        
                @param _fromToken  ERC20 token to convert from
                @param _toToken    ERC20 token to convert to
                @param _amount     amount to convert, in fromToken
                @param _minReturn  if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return conversion return amount
            */
            function convert(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256) {
                convertPath = [_fromToken, token, _toToken];
                return quickConvert(convertPath, _amount, _minReturn);
            }
        
            /**
                @dev buys the token by depositing one of its connector tokens
        
                @param _connectorToken  connector token contract address
                @param _depositAmount   amount to deposit (in the connector token)
                @param _minReturn       if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return buy return amount
            */
            function buy(IERC20Token _connectorToken, uint256 _depositAmount, uint256 _minReturn) internal returns (uint256) {
                uint256 amount = getPurchaseReturn(_connectorToken, _depositAmount);
                // ensure the trade gives something in return and meets the minimum requested amount
                require(amount != 0 && amount >= _minReturn);
        
                // update virtual balance if relevant
                Connector storage connector = connectors[_connectorToken];
                if (connector.isVirtualBalanceEnabled)
                    connector.virtualBalance = safeAdd(connector.virtualBalance, _depositAmount);
        
                // transfer funds from the caller in the connector token
                assert(_connectorToken.transferFrom(msg.sender, this, _depositAmount));
                // issue new funds to the caller in the smart token
                token.issue(msg.sender, amount);
        
                // calculate conversion fee and dispatch the conversion event
                uint256 feeAmount = safeSub(amount, getFinalAmount(amount, 1));
                dispatchConversionEvent(_connectorToken, token, _depositAmount, amount, feeAmount);
        
                // dispatch price data update for the smart token/connector
                emit PriceDataUpdate(_connectorToken, token.totalSupply(), getConnectorBalance(_connectorToken), connector.weight);
                return amount;
            }
        
            /**
                @dev sells the token by withdrawing from one of its connector tokens
        
                @param _connectorToken  connector token contract address
                @param _sellAmount      amount to sell (in the smart token)
                @param _minReturn       if the conversion results in an amount smaller the minimum return - it is cancelled, must be nonzero
        
                @return sell return amount
            */
            function sell(IERC20Token _connectorToken, uint256 _sellAmount, uint256 _minReturn) internal returns (uint256) {
                require(_sellAmount <= token.balanceOf(msg.sender)); // validate input
        
                uint256 amount = getSaleReturn(_connectorToken, _sellAmount);
                // ensure the trade gives something in return and meets the minimum requested amount
                require(amount != 0 && amount >= _minReturn);
        
                // ensure that the trade will only deplete the connector balance if the total supply is depleted as well
                uint256 tokenSupply = token.totalSupply();
                uint256 connectorBalance = getConnectorBalance(_connectorToken);
                assert(amount < connectorBalance || (amount == connectorBalance && _sellAmount == tokenSupply));
        
                // update virtual balance if relevant
                Connector storage connector = connectors[_connectorToken];
                if (connector.isVirtualBalanceEnabled)
                    connector.virtualBalance = safeSub(connector.virtualBalance, amount);
        
                // destroy _sellAmount from the caller's balance in the smart token
                token.destroy(msg.sender, _sellAmount);
                // transfer funds to the caller in the connector token
                // the transfer might fail if the actual connector balance is smaller than the virtual balance
                assert(_connectorToken.transfer(msg.sender, amount));
        
                // calculate conversion fee and dispatch the conversion event
                uint256 feeAmount = safeSub(amount, getFinalAmount(amount, 1));
                dispatchConversionEvent(token, _connectorToken, _sellAmount, amount, feeAmount);
        
                // dispatch price data update for the smart token/connector
                emit PriceDataUpdate(_connectorToken, token.totalSupply(), getConnectorBalance(_connectorToken), connector.weight);
                return amount;
            }
        
            /**
                @dev converts the token to any other token in the bancor network by following a predefined conversion path
                note that when converting from an ERC20 token (as opposed to a smart token), allowance must be set beforehand
        
                @param _path        conversion path, see conversion path format in the BancorNetwork contract
                @param _amount      amount to convert from (in the initial source token)
                @param _minReturn   if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return tokens issued in return
            */
            function quickConvert(IERC20Token[] _path, uint256 _amount, uint256 _minReturn)
                public
                payable
                validConversionPath(_path)
                returns (uint256)
            {
                return quickConvertPrioritized(_path, _amount, _minReturn, 0x0, 0x0, 0x0, 0x0);
            }
        
            /**
                @dev converts the token to any other token in the bancor network by following a predefined conversion path
                note that when converting from an ERC20 token (as opposed to a smart token), allowance must be set beforehand
        
                @param _path        conversion path, see conversion path format in the BancorNetwork contract
                @param _amount      amount to convert from (in the initial source token)
                @param _minReturn   if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
                @param _block       if the current block exceeded the given parameter - it is cancelled
                @param _v           (signature[128:130]) associated with the signer address and helps validating if the signature is legit
                @param _r           (signature[0:64]) associated with the signer address and helps validating if the signature is legit
                @param _s           (signature[64:128]) associated with the signer address and helps validating if the signature is legit
        
                @return tokens issued in return
            */
            function quickConvertPrioritized(IERC20Token[] _path, uint256 _amount, uint256 _minReturn, uint256 _block, uint8 _v, bytes32 _r, bytes32 _s)
                public
                payable
                validConversionPath(_path)
                returns (uint256)
            {
                IERC20Token fromToken = _path[0];
                IBancorNetwork bancorNetwork = IBancorNetwork(registry.getAddress(ContractIds.BANCOR_NETWORK));
        
                // we need to transfer the source tokens from the caller to the BancorNetwork contract,
                // so it can execute the conversion on behalf of the caller
                if (msg.value == 0) {
                    // not ETH, send the source tokens to the BancorNetwork contract
                    // if the token is the smart token, no allowance is required - destroy the tokens
                    // from the caller and issue them to the BancorNetwork contract
                    if (fromToken == token) {
                        token.destroy(msg.sender, _amount); // destroy _amount tokens from the caller's balance in the smart token
                        token.issue(bancorNetwork, _amount); // issue _amount new tokens to the BancorNetwork contract
                    } else {
                        // otherwise, we assume we already have allowance, transfer the tokens directly to the BancorNetwork contract
                        assert(fromToken.transferFrom(msg.sender, bancorNetwork, _amount));
                    }
                }
        
                // execute the conversion and pass on the ETH with the call
                return bancorNetwork.convertForPrioritized2.value(msg.value)(_path, _amount, _minReturn, msg.sender, _block, _v, _r, _s);
            }
        
            // deprecated, backward compatibility
            function change(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256) {
                return convertInternal(_fromToken, _toToken, _amount, _minReturn);
            }
        
            /**
                @dev helper, dispatches the Conversion event
        
                @param _fromToken       ERC20 token to convert from
                @param _toToken         ERC20 token to convert to
                @param _amount          amount purchased/sold (in the source token)
                @param _returnAmount    amount returned (in the target token)
            */
            function dispatchConversionEvent(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _returnAmount, uint256 _feeAmount) private {
                // fee amount is converted to 255 bits -
                // negative amount means the fee is taken from the source token, positive amount means its taken from the target token
                // currently the fee is always taken from the target token
                // since we convert it to a signed number, we first ensure that it's capped at 255 bits to prevent overflow
                assert(_feeAmount <= 2 ** 255);
                emit Conversion(_fromToken, _toToken, msg.sender, _amount, _returnAmount, int256(_feeAmount));
            }
        
            /**
                @dev fallback, buys the smart token with ETH
                note that the purchase will use the price at the time of the purchase
            */
            function() payable public {
                quickConvert(quickBuyPath, msg.value, 1);
            }
        }

        File 3 of 8: SmartToken
        pragma solidity ^0.4.11;
        
        /*
            Utilities & Common Modifiers
        */
        contract Utils {
            /**
                constructor
            */
            function Utils() {
            }
        
            // verifies that an amount is greater than zero
            modifier greaterThanZero(uint256 _amount) {
                require(_amount > 0);
                _;
            }
        
            // validates an address - currently only checks that it isn't null
            modifier validAddress(address _address) {
                require(_address != 0x0);
                _;
            }
        
            // verifies that the address is different than this contract address
            modifier notThis(address _address) {
                require(_address != address(this));
                _;
            }
        
            // Overflow protected math functions
        
            /**
                @dev returns the sum of _x and _y, asserts if the calculation overflows
        
                @param _x   value 1
                @param _y   value 2
        
                @return sum
            */
            function safeAdd(uint256 _x, uint256 _y) internal constant returns (uint256) {
                uint256 z = _x + _y;
                assert(z >= _x);
                return z;
            }
        
            /**
                @dev returns the difference of _x minus _y, asserts if the subtraction results in a negative number
        
                @param _x   minuend
                @param _y   subtrahend
        
                @return difference
            */
            function safeSub(uint256 _x, uint256 _y) internal constant returns (uint256) {
                assert(_x >= _y);
                return _x - _y;
            }
        
            /**
                @dev returns the product of multiplying _x by _y, asserts if the calculation overflows
        
                @param _x   factor 1
                @param _y   factor 2
        
                @return product
            */
            function safeMul(uint256 _x, uint256 _y) internal constant returns (uint256) {
                uint256 z = _x * _y;
                assert(_x == 0 || z / _x == _y);
                return z;
            }
        }
        
        /*
            Owned contract interface
        */
        contract IOwned {
            // this function isn't abstract since the compiler emits automatically generated getter functions as external
            function owner() public constant returns (address) {}
        
            function transferOwnership(address _newOwner) public;
            function acceptOwnership() public;
        }
        
        /*
            Provides support and utilities for contract ownership
        */
        contract Owned is IOwned {
            address public owner;
            address public newOwner;
        
            event OwnerUpdate(address _prevOwner, address _newOwner);
        
            /**
                @dev constructor
            */
            function Owned() {
                owner = msg.sender;
            }
        
            // allows execution by the owner only
            modifier ownerOnly {
                assert(msg.sender == owner);
                _;
            }
        
            /**
                @dev allows transferring the contract ownership
                the new owner still needs to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new contract owner
            */
            function transferOwnership(address _newOwner) public ownerOnly {
                require(_newOwner != owner);
                newOwner = _newOwner;
            }
        
            /**
                @dev used by a new owner to accept an ownership transfer
            */
            function acceptOwnership() public {
                require(msg.sender == newOwner);
                OwnerUpdate(owner, newOwner);
                owner = newOwner;
                newOwner = 0x0;
            }
        }
        
        /*
            ERC20 Standard Token interface
        */
        contract IERC20Token {
            // these functions aren't abstract since the compiler emits automatically generated getter functions as external
            function name() public constant returns (string) {}
            function symbol() public constant returns (string) {}
            function decimals() public constant returns (uint8) {}
            function totalSupply() public constant returns (uint256) {}
            function balanceOf(address _owner) public constant returns (uint256) { _owner; }
            function allowance(address _owner, address _spender) public constant returns (uint256) { _owner; _spender; }
        
            function transfer(address _to, uint256 _value) public returns (bool success);
            function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
            function approve(address _spender, uint256 _value) public returns (bool success);
        }
        
        /**
            ERC20 Standard Token implementation
        */
        contract ERC20Token is IERC20Token, Utils {
            string public standard = 'Token 0.1';
            string public name = '';
            string public symbol = '';
            uint8 public decimals = 0;
            uint256 public totalSupply = 0;
            mapping (address => uint256) public balanceOf;
            mapping (address => mapping (address => uint256)) public allowance;
        
            event Transfer(address indexed _from, address indexed _to, uint256 _value);
            event Approval(address indexed _owner, address indexed _spender, uint256 _value);
        
            /**
                @dev constructor
        
                @param _name        token name
                @param _symbol      token symbol
                @param _decimals    decimal points, for display purposes
            */
            function ERC20Token(string _name, string _symbol, uint8 _decimals) {
                require(bytes(_name).length > 0 && bytes(_symbol).length > 0); // validate input
        
                name = _name;
                symbol = _symbol;
                decimals = _decimals;
            }
        
            /**
                @dev send coins
                throws on any error rather then return a false flag to minimize user errors
        
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transfer(address _to, uint256 _value)
                public
                validAddress(_to)
                returns (bool success)
            {
                balanceOf[msg.sender] = safeSub(balanceOf[msg.sender], _value);
                balanceOf[_to] = safeAdd(balanceOf[_to], _value);
                Transfer(msg.sender, _to, _value);
                return true;
            }
        
            /**
                @dev an account/contract attempts to get the coins
                throws on any error rather then return a false flag to minimize user errors
        
                @param _from    source address
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transferFrom(address _from, address _to, uint256 _value)
                public
                validAddress(_from)
                validAddress(_to)
                returns (bool success)
            {
                allowance[_from][msg.sender] = safeSub(allowance[_from][msg.sender], _value);
                balanceOf[_from] = safeSub(balanceOf[_from], _value);
                balanceOf[_to] = safeAdd(balanceOf[_to], _value);
                Transfer(_from, _to, _value);
                return true;
            }
        
            /**
                @dev allow another account/contract to spend some tokens on your behalf
                throws on any error rather then return a false flag to minimize user errors
        
                also, to minimize the risk of the approve/transferFrom attack vector
                (see https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/), approve has to be called twice
                in 2 separate transactions - once to change the allowance to 0 and secondly to change it to the new allowance value
        
                @param _spender approved address
                @param _value   allowance amount
        
                @return true if the approval was successful, false if it wasn't
            */
            function approve(address _spender, uint256 _value)
                public
                validAddress(_spender)
                returns (bool success)
            {
                // if the allowance isn't 0, it can only be updated to 0 to prevent an allowance change immediately after withdrawal
                require(_value == 0 || allowance[msg.sender][_spender] == 0);
        
                allowance[msg.sender][_spender] = _value;
                Approval(msg.sender, _spender, _value);
                return true;
            }
        }
        
        /*
            Token Holder interface
        */
        contract ITokenHolder is IOwned {
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount) public;
        }
        
        /*
            We consider every contract to be a 'token holder' since it's currently not possible
            for a contract to deny receiving tokens.
        
            The TokenHolder's contract sole purpose is to provide a safety mechanism that allows
            the owner to send tokens that were sent to the contract by mistake back to their sender.
        */
        contract TokenHolder is ITokenHolder, Owned, Utils {
            /**
                @dev constructor
            */
            function TokenHolder() {
            }
        
            /**
                @dev withdraws tokens held by the contract and sends them to an account
                can only be called by the owner
        
                @param _token   ERC20 token contract address
                @param _to      account to receive the new amount
                @param _amount  amount to withdraw
            */
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount)
                public
                ownerOnly
                validAddress(_token)
                validAddress(_to)
                notThis(_to)
            {
                assert(_token.transfer(_to, _amount));
            }
        }
        
        /*
            Smart Token interface
        */
        contract ISmartToken is IOwned, IERC20Token {
            function disableTransfers(bool _disable) public;
            function issue(address _to, uint256 _amount) public;
            function destroy(address _from, uint256 _amount) public;
        }
        
        /*
            Smart Token v0.3
        
            'Owned' is specified here for readability reasons
        */
        contract SmartToken is ISmartToken, Owned, ERC20Token, TokenHolder {
            string public version = '0.3';
        
            bool public transfersEnabled = true;    // true if transfer/transferFrom are enabled, false if not
        
            // triggered when a smart token is deployed - the _token address is defined for forward compatibility, in case we want to trigger the event from a factory
            event NewSmartToken(address _token);
            // triggered when the total supply is increased
            event Issuance(uint256 _amount);
            // triggered when the total supply is decreased
            event Destruction(uint256 _amount);
        
            /**
                @dev constructor
        
                @param _name       token name
                @param _symbol     token short symbol, minimum 1 character
                @param _decimals   for display purposes only
            */
            function SmartToken(string _name, string _symbol, uint8 _decimals)
                ERC20Token(_name, _symbol, _decimals)
            {
                NewSmartToken(address(this));
            }
        
            // allows execution only when transfers aren't disabled
            modifier transfersAllowed {
                assert(transfersEnabled);
                _;
            }
        
            /**
                @dev disables/enables transfers
                can only be called by the contract owner
        
                @param _disable    true to disable transfers, false to enable them
            */
            function disableTransfers(bool _disable) public ownerOnly {
                transfersEnabled = !_disable;
            }
        
            /**
                @dev increases the token supply and sends the new tokens to an account
                can only be called by the contract owner
        
                @param _to         account to receive the new amount
                @param _amount     amount to increase the supply by
            */
            function issue(address _to, uint256 _amount)
                public
                ownerOnly
                validAddress(_to)
                notThis(_to)
            {
                totalSupply = safeAdd(totalSupply, _amount);
                balanceOf[_to] = safeAdd(balanceOf[_to], _amount);
        
                Issuance(_amount);
                Transfer(this, _to, _amount);
            }
        
            /**
                @dev removes tokens from an account and decreases the token supply
                can be called by the contract owner to destroy tokens from any account or by any holder to destroy tokens from his/her own account
        
                @param _from       account to remove the amount from
                @param _amount     amount to decrease the supply by
            */
            function destroy(address _from, uint256 _amount) public {
                require(msg.sender == _from || msg.sender == owner); // validate input
        
                balanceOf[_from] = safeSub(balanceOf[_from], _amount);
                totalSupply = safeSub(totalSupply, _amount);
        
                Transfer(_from, this, _amount);
                Destruction(_amount);
            }
        
            // ERC20 standard method overrides with some extra functionality
        
            /**
                @dev send coins
                throws on any error rather then return a false flag to minimize user errors
                in addition to the standard checks, the function throws if transfers are disabled
        
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transfer(address _to, uint256 _value) public transfersAllowed returns (bool success) {
                assert(super.transfer(_to, _value));
                return true;
            }
        
            /**
                @dev an account/contract attempts to get the coins
                throws on any error rather then return a false flag to minimize user errors
                in addition to the standard checks, the function throws if transfers are disabled
        
                @param _from    source address
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transferFrom(address _from, address _to, uint256 _value) public transfersAllowed returns (bool success) {
                assert(super.transferFrom(_from, _to, _value));
                return true;
            }
        }

        File 4 of 8: SNT
        pragma solidity ^0.4.11;
        
        
        /// @dev `Owned` is a base level contract that assigns an `owner` that can be
        ///  later changed
        contract Owned {
        
            /// @dev `owner` is the only address that can call a function with this
            /// modifier
            modifier onlyOwner() {
                require(msg.sender == owner);
                _;
            }
        
            address public owner;
        
            /// @notice The Constructor assigns the message sender to be `owner`
            function Owned() {
                owner = msg.sender;
            }
        
            address public newOwner;
        
            /// @notice `owner` can step down and assign some other address to this role
            /// @param _newOwner The address of the new owner. 0x0 can be used to create
            ///  an unowned neutral vault, however that cannot be undone
            function changeOwner(address _newOwner) onlyOwner {
                newOwner = _newOwner;
            }
        
        
            function acceptOwnership() {
                if (msg.sender == newOwner) {
                    owner = newOwner;
                }
            }
        }
        
        // Abstract contract for the full ERC 20 Token standard
        // https://github.com/ethereum/EIPs/issues/20
        
        contract ERC20Token {
            /* This is a slight change to the ERC20 base standard.
            function totalSupply() constant returns (uint256 supply);
            is replaced with:
            uint256 public totalSupply;
            This automatically creates a getter function for the totalSupply.
            This is moved to the base contract since public getter functions are not
            currently recognised as an implementation of the matching abstract
            function by the compiler.
            */
            /// total amount of tokens
            uint256 public totalSupply;
        
            /// @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 `_spender` to spend `_value` tokens
            /// @param _spender The address of the account able to transfer the tokens
            /// @param _value The amount of tokens 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);
        }
        
        
        
        /**
         * Math operations with safety checks
         */
        library SafeMath {
          function mul(uint a, uint b) internal returns (uint) {
            uint c = a * b;
            assert(a == 0 || c / a == b);
            return c;
          }
        
          function div(uint a, uint b) internal returns (uint) {
            // assert(b > 0); // Solidity automatically throws when dividing by 0
            uint c = a / b;
            // assert(a == b * c + a % b); // There is no case in which this doesn't hold
            return c;
          }
        
          function sub(uint a, uint b) internal returns (uint) {
            assert(b <= a);
            return a - b;
          }
        
          function add(uint a, uint b) internal returns (uint) {
            uint c = a + b;
            assert(c >= a);
            return c;
          }
        
          function max64(uint64 a, uint64 b) internal constant returns (uint64) {
            return a >= b ? a : b;
          }
        
          function min64(uint64 a, uint64 b) internal constant returns (uint64) {
            return a < b ? a : b;
          }
        
          function max256(uint256 a, uint256 b) internal constant returns (uint256) {
            return a >= b ? a : b;
          }
        
          function min256(uint256 a, uint256 b) internal constant returns (uint256) {
            return a < b ? a : b;
          }
        }
        
        
        /*
            Copyright 2017, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title DynamicCeiling Contract
        /// @author Jordi Baylina
        /// @dev This contract calculates the ceiling from a series of curves.
        ///  These curves are committed first and revealed later.
        ///  All the curves must be in increasing order and the last curve is marked
        ///  as the last one.
        ///  This contract allows to hide and reveal the ceiling at will of the owner.
        
        
        
        contract DynamicCeiling is Owned {
            using SafeMath for uint256;
        
            struct Curve {
                bytes32 hash;
                // Absolute limit for this curve
                uint256 limit;
                // The funds remaining to be collected are divided by `slopeFactor` smooth ceiling
                // with a long tail where big and small buyers can take part.
                uint256 slopeFactor;
                // This keeps the curve flat at this number, until funds to be collected is less than this
                uint256 collectMinimum;
            }
        
            address public contribution;
        
            Curve[] public curves;
            uint256 public currentIndex;
            uint256 public revealedCurves;
            bool public allRevealed;
        
            /// @dev `contribution` is the only address that can call a function with this
            /// modifier
            modifier onlyContribution {
                require(msg.sender == contribution);
                _;
            }
        
            function DynamicCeiling(address _owner, address _contribution) {
                owner = _owner;
                contribution = _contribution;
            }
        
            /// @notice This should be called by the creator of the contract to commit
            ///  all the curves.
            /// @param _curveHashes Array of hashes of each curve. Each hash is calculated
            ///  by the `calculateHash` method. More hashes than actual curves can be
            ///  committed in order to hide also the number of curves.
            ///  The remaining hashes can be just random numbers.
            function setHiddenCurves(bytes32[] _curveHashes) public onlyOwner {
                require(curves.length == 0);
        
                curves.length = _curveHashes.length;
                for (uint256 i = 0; i < _curveHashes.length; i = i.add(1)) {
                    curves[i].hash = _curveHashes[i];
                }
            }
        
        
            /// @notice Anybody can reveal the next curve if he knows it.
            /// @param _limit Ceiling cap.
            ///  (must be greater or equal to the previous one).
            /// @param _last `true` if it's the last curve.
            /// @param _salt Random number used to commit the curve
            function revealCurve(uint256 _limit, uint256 _slopeFactor, uint256 _collectMinimum,
                                 bool _last, bytes32 _salt) public {
                require(!allRevealed);
        
                require(curves[revealedCurves].hash == calculateHash(_limit, _slopeFactor, _collectMinimum,
                                                                     _last, _salt));
        
                require(_limit != 0 && _slopeFactor != 0 && _collectMinimum != 0);
                if (revealedCurves > 0) {
                    require(_limit >= curves[revealedCurves.sub(1)].limit);
                }
        
                curves[revealedCurves].limit = _limit;
                curves[revealedCurves].slopeFactor = _slopeFactor;
                curves[revealedCurves].collectMinimum = _collectMinimum;
                revealedCurves = revealedCurves.add(1);
        
                if (_last) allRevealed = true;
            }
        
            /// @notice Reveal multiple curves at once
            function revealMulti(uint256[] _limits, uint256[] _slopeFactors, uint256[] _collectMinimums,
                                 bool[] _lasts, bytes32[] _salts) public {
                // Do not allow none and needs to be same length for all parameters
                require(_limits.length != 0 &&
                        _limits.length == _slopeFactors.length &&
                        _limits.length == _collectMinimums.length &&
                        _limits.length == _lasts.length &&
                        _limits.length == _salts.length);
        
                for (uint256 i = 0; i < _limits.length; i = i.add(1)) {
                    revealCurve(_limits[i], _slopeFactors[i], _collectMinimums[i],
                                _lasts[i], _salts[i]);
                }
            }
        
            /// @notice Move to curve, used as a failsafe
            function moveTo(uint256 _index) public onlyOwner {
                require(_index < revealedCurves &&       // No more curves
                        _index == currentIndex.add(1));  // Only move one index at a time
                currentIndex = _index;
            }
        
            /// @return Return the funds to collect for the current point on the curve
            ///  (or 0 if no curves revealed yet)
            function toCollect(uint256 collected) public onlyContribution returns (uint256) {
                if (revealedCurves == 0) return 0;
        
                // Move to the next curve
                if (collected >= curves[currentIndex].limit) {  // Catches `limit == 0`
                    uint256 nextIndex = currentIndex.add(1);
                    if (nextIndex >= revealedCurves) return 0;  // No more curves
                    currentIndex = nextIndex;
                    if (collected >= curves[currentIndex].limit) return 0;  // Catches `limit == 0`
                }
        
                // Everything left to collect from this limit
                uint256 difference = curves[currentIndex].limit.sub(collected);
        
                // Current point on the curve
                uint256 collect = difference.div(curves[currentIndex].slopeFactor);
        
                // Prevents paying too much fees vs to be collected; breaks long tail
                if (collect <= curves[currentIndex].collectMinimum) {
                    if (difference > curves[currentIndex].collectMinimum) {
                        return curves[currentIndex].collectMinimum;
                    } else {
                        return difference;
                    }
                } else {
                    return collect;
                }
            }
        
            /// @notice Calculates the hash of a curve.
            /// @param _limit Ceiling cap.
            /// @param _last `true` if it's the last curve.
            /// @param _salt Random number that will be needed to reveal this curve.
            /// @return The calculated hash of this curve to be used in the `setHiddenCurves` method
            function calculateHash(uint256 _limit, uint256 _slopeFactor, uint256 _collectMinimum,
                                   bool _last, bytes32 _salt) public constant returns (bytes32) {
                return keccak256(_limit, _slopeFactor, _collectMinimum, _last, _salt);
            }
        
            /// @return Return the total number of curves committed
            ///  (can be larger than the number of actual curves on the curve to hide
            ///  the real number of curves)
            function nCurves() public constant returns (uint256) {
                return curves.length;
            }
        
        }
        
        
        /*
            Copyright 2016, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title MiniMeToken Contract
        /// @author Jordi Baylina
        /// @dev This token contract's goal is to make it easy for anyone to clone this
        ///  token using the token distribution at a given block, this will allow DAO's
        ///  and DApps to upgrade their features in a decentralized manner without
        ///  affecting the original token
        /// @dev It is ERC20 compliant, but still needs to under go further testing.
        
        
        /// @dev The token controller contract must implement these functions
        contract TokenController {
            /// @notice Called when `_owner` sends ether to the MiniMe Token contract
            /// @param _owner The address that sent the ether to create tokens
            /// @return True if the ether is accepted, false if it throws
            function proxyPayment(address _owner) payable returns(bool);
        
            /// @notice Notifies the controller about a token transfer allowing the
            ///  controller to react if desired
            /// @param _from The origin of the transfer
            /// @param _to The destination of the transfer
            /// @param _amount The amount of the transfer
            /// @return False if the controller does not authorize the transfer
            function onTransfer(address _from, address _to, uint _amount) returns(bool);
        
            /// @notice Notifies the controller about an approval allowing the
            ///  controller to react if desired
            /// @param _owner The address that calls `approve()`
            /// @param _spender The spender in the `approve()` call
            /// @param _amount The amount in the `approve()` call
            /// @return False if the controller does not authorize the approval
            function onApprove(address _owner, address _spender, uint _amount)
                returns(bool);
        }
        
        contract Controlled {
            /// @notice The address of the controller is the only address that can call
            ///  a function with this modifier
            modifier onlyController { if (msg.sender != controller) throw; _; }
        
            address public controller;
        
            function Controlled() { controller = msg.sender;}
        
            /// @notice Changes the controller of the contract
            /// @param _newController The new controller of the contract
            function changeController(address _newController) onlyController {
                controller = _newController;
            }
        }
        
        contract ApproveAndCallFallBack {
            function receiveApproval(address from, uint256 _amount, address _token, bytes _data);
        }
        
        /// @dev The actual token contract, the default controller is the msg.sender
        ///  that deploys the contract, so usually this token will be deployed by a
        ///  token controller contract, which Giveth will call a "Campaign"
        contract MiniMeToken is Controlled {
        
            string public name;                //The Token's name: e.g. DigixDAO Tokens
            uint8 public decimals;             //Number of decimals of the smallest unit
            string public symbol;              //An identifier: e.g. REP
            string public version = 'MMT_0.1'; //An arbitrary versioning scheme
        
        
            /// @dev `Checkpoint` is the structure that attaches a block number to a
            ///  given value, the block number attached is the one that last changed the
            ///  value
            struct  Checkpoint {
        
                // `fromBlock` is the block number that the value was generated from
                uint128 fromBlock;
        
                // `value` is the amount of tokens at a specific block number
                uint128 value;
            }
        
            // `parentToken` is the Token address that was cloned to produce this token;
            //  it will be 0x0 for a token that was not cloned
            MiniMeToken public parentToken;
        
            // `parentSnapShotBlock` is the block number from the Parent Token that was
            //  used to determine the initial distribution of the Clone Token
            uint public parentSnapShotBlock;
        
            // `creationBlock` is the block number that the Clone Token was created
            uint public creationBlock;
        
            // `balances` is the map that tracks the balance of each address, in this
            //  contract when the balance changes the block number that the change
            //  occurred is also included in the map
            mapping (address => Checkpoint[]) balances;
        
            // `allowed` tracks any extra transfer rights as in all ERC20 tokens
            mapping (address => mapping (address => uint256)) allowed;
        
            // Tracks the history of the `totalSupply` of the token
            Checkpoint[] totalSupplyHistory;
        
            // Flag that determines if the token is transferable or not.
            bool public transfersEnabled;
        
            // The factory used to create new clone tokens
            MiniMeTokenFactory public tokenFactory;
        
        ////////////////
        // Constructor
        ////////////////
        
            /// @notice Constructor to create a MiniMeToken
            /// @param _tokenFactory The address of the MiniMeTokenFactory contract that
            ///  will create the Clone token contracts, the token factory needs to be
            ///  deployed first
            /// @param _parentToken Address of the parent token, set to 0x0 if it is a
            ///  new token
            /// @param _parentSnapShotBlock Block of the parent token that will
            ///  determine the initial distribution of the clone token, set to 0 if it
            ///  is a new token
            /// @param _tokenName Name of the new token
            /// @param _decimalUnits Number of decimals of the new token
            /// @param _tokenSymbol Token Symbol for the new token
            /// @param _transfersEnabled If true, tokens will be able to be transferred
            function MiniMeToken(
                address _tokenFactory,
                address _parentToken,
                uint _parentSnapShotBlock,
                string _tokenName,
                uint8 _decimalUnits,
                string _tokenSymbol,
                bool _transfersEnabled
            ) {
                tokenFactory = MiniMeTokenFactory(_tokenFactory);
                name = _tokenName;                                 // Set the name
                decimals = _decimalUnits;                          // Set the decimals
                symbol = _tokenSymbol;                             // Set the symbol
                parentToken = MiniMeToken(_parentToken);
                parentSnapShotBlock = _parentSnapShotBlock;
                transfersEnabled = _transfersEnabled;
                creationBlock = getBlockNumber();
            }
        
        
        ///////////////////
        // ERC20 Methods
        ///////////////////
        
            /// @notice Send `_amount` tokens to `_to` from `msg.sender`
            /// @param _to The address of the recipient
            /// @param _amount The amount of tokens to be transferred
            /// @return Whether the transfer was successful or not
            function transfer(address _to, uint256 _amount) returns (bool success) {
                if (!transfersEnabled) throw;
                return doTransfer(msg.sender, _to, _amount);
            }
        
            /// @notice Send `_amount` tokens to `_to` from `_from` on the condition it
            ///  is approved by `_from`
            /// @param _from The address holding the tokens being transferred
            /// @param _to The address of the recipient
            /// @param _amount The amount of tokens to be transferred
            /// @return True if the transfer was successful
            function transferFrom(address _from, address _to, uint256 _amount
            ) returns (bool success) {
        
                // The controller of this contract can move tokens around at will,
                //  this is important to recognize! Confirm that you trust the
                //  controller of this contract, which in most situations should be
                //  another open source smart contract or 0x0
                if (msg.sender != controller) {
                    if (!transfersEnabled) throw;
        
                    // The standard ERC 20 transferFrom functionality
                    if (allowed[_from][msg.sender] < _amount) return false;
                    allowed[_from][msg.sender] -= _amount;
                }
                return doTransfer(_from, _to, _amount);
            }
        
            /// @dev This is the actual transfer function in the token contract, it can
            ///  only be called by other functions in this contract.
            /// @param _from The address holding the tokens being transferred
            /// @param _to The address of the recipient
            /// @param _amount The amount of tokens to be transferred
            /// @return True if the transfer was successful
            function doTransfer(address _from, address _to, uint _amount
            ) internal returns(bool) {
        
                   if (_amount == 0) {
                       return true;
                   }
        
                   if (parentSnapShotBlock >= getBlockNumber()) throw;
        
                   // Do not allow transfer to 0x0 or the token contract itself
                   if ((_to == 0) || (_to == address(this))) throw;
        
                   // If the amount being transfered is more than the balance of the
                   //  account the transfer returns false
                   var previousBalanceFrom = balanceOfAt(_from, getBlockNumber());
                   if (previousBalanceFrom < _amount) {
                       return false;
                   }
        
                   // Alerts the token controller of the transfer
                   if (isContract(controller)) {
                       if (!TokenController(controller).onTransfer(_from, _to, _amount))
                       throw;
                   }
        
                   // First update the balance array with the new value for the address
                   //  sending the tokens
                   updateValueAtNow(balances[_from], previousBalanceFrom - _amount);
        
                   // Then update the balance array with the new value for the address
                   //  receiving the tokens
                   var previousBalanceTo = balanceOfAt(_to, getBlockNumber());
                   if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow
                   updateValueAtNow(balances[_to], previousBalanceTo + _amount);
        
                   // An event to make the transfer easy to find on the blockchain
                   Transfer(_from, _to, _amount);
        
                   return true;
            }
        
            /// @param _owner The address that's balance is being requested
            /// @return The balance of `_owner` at the current block
            function balanceOf(address _owner) constant returns (uint256 balance) {
                return balanceOfAt(_owner, getBlockNumber());
            }
        
            /// @notice `msg.sender` approves `_spender` to spend `_amount` tokens on
            ///  its behalf. This is a modified version of the ERC20 approve function
            ///  to be a little bit safer
            /// @param _spender The address of the account able to transfer the tokens
            /// @param _amount The amount of tokens to be approved for transfer
            /// @return True if the approval was successful
            function approve(address _spender, uint256 _amount) returns (bool success) {
                if (!transfersEnabled) throw;
        
                // To change the approve amount you first have to reduce the addresses`
                //  allowance to zero by calling `approve(_spender,0)` if it is not
                //  already 0 to mitigate the race condition described here:
                //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
                if ((_amount!=0) && (allowed[msg.sender][_spender] !=0)) throw;
        
                // Alerts the token controller of the approve function call
                if (isContract(controller)) {
                    if (!TokenController(controller).onApprove(msg.sender, _spender, _amount))
                        throw;
                }
        
                allowed[msg.sender][_spender] = _amount;
                Approval(msg.sender, _spender, _amount);
                return true;
            }
        
            /// @dev This function makes it easy to read the `allowed[]` map
            /// @param _owner The address of the account that owns the token
            /// @param _spender The address of the account able to transfer the tokens
            /// @return Amount of remaining tokens of _owner that _spender is allowed
            ///  to spend
            function allowance(address _owner, address _spender
            ) constant returns (uint256 remaining) {
                return allowed[_owner][_spender];
            }
        
            /// @notice `msg.sender` approves `_spender` to send `_amount` tokens on
            ///  its behalf, and then a function is triggered in the contract that is
            ///  being approved, `_spender`. This allows users to use their tokens to
            ///  interact with contracts in one function call instead of two
            /// @param _spender The address of the contract able to transfer the tokens
            /// @param _amount The amount of tokens to be approved for transfer
            /// @return True if the function call was successful
            function approveAndCall(address _spender, uint256 _amount, bytes _extraData
            ) returns (bool success) {
                if (!approve(_spender, _amount)) throw;
        
                ApproveAndCallFallBack(_spender).receiveApproval(
                    msg.sender,
                    _amount,
                    this,
                    _extraData
                );
        
                return true;
            }
        
            /// @dev This function makes it easy to get the total number of tokens
            /// @return The total number of tokens
            function totalSupply() constant returns (uint) {
                return totalSupplyAt(getBlockNumber());
            }
        
        
        ////////////////
        // Query balance and totalSupply in History
        ////////////////
        
            /// @dev Queries the balance of `_owner` at a specific `_blockNumber`
            /// @param _owner The address from which the balance will be retrieved
            /// @param _blockNumber The block number when the balance is queried
            /// @return The balance at `_blockNumber`
            function balanceOfAt(address _owner, uint _blockNumber) constant
                returns (uint) {
        
                // These next few lines are used when the balance of the token is
                //  requested before a check point was ever created for this token, it
                //  requires that the `parentToken.balanceOfAt` be queried at the
                //  genesis block for that token as this contains initial balance of
                //  this token
                if ((balances[_owner].length == 0)
                    || (balances[_owner][0].fromBlock > _blockNumber)) {
                    if (address(parentToken) != 0) {
                        return parentToken.balanceOfAt(_owner, min(_blockNumber, parentSnapShotBlock));
                    } else {
                        // Has no parent
                        return 0;
                    }
        
                // This will return the expected balance during normal situations
                } else {
                    return getValueAt(balances[_owner], _blockNumber);
                }
            }
        
            /// @notice Total amount of tokens at a specific `_blockNumber`.
            /// @param _blockNumber The block number when the totalSupply is queried
            /// @return The total amount of tokens at `_blockNumber`
            function totalSupplyAt(uint _blockNumber) constant returns(uint) {
        
                // These next few lines are used when the totalSupply of the token is
                //  requested before a check point was ever created for this token, it
                //  requires that the `parentToken.totalSupplyAt` be queried at the
                //  genesis block for this token as that contains totalSupply of this
                //  token at this block number.
                if ((totalSupplyHistory.length == 0)
                    || (totalSupplyHistory[0].fromBlock > _blockNumber)) {
                    if (address(parentToken) != 0) {
                        return parentToken.totalSupplyAt(min(_blockNumber, parentSnapShotBlock));
                    } else {
                        return 0;
                    }
        
                // This will return the expected totalSupply during normal situations
                } else {
                    return getValueAt(totalSupplyHistory, _blockNumber);
                }
            }
        
        ////////////////
        // Clone Token Method
        ////////////////
        
            /// @notice Creates a new clone token with the initial distribution being
            ///  this token at `_snapshotBlock`
            /// @param _cloneTokenName Name of the clone token
            /// @param _cloneDecimalUnits Number of decimals of the smallest unit
            /// @param _cloneTokenSymbol Symbol of the clone token
            /// @param _snapshotBlock Block when the distribution of the parent token is
            ///  copied to set the initial distribution of the new clone token;
            ///  if the block is zero than the actual block, the current block is used
            /// @param _transfersEnabled True if transfers are allowed in the clone
            /// @return The address of the new MiniMeToken Contract
            function createCloneToken(
                string _cloneTokenName,
                uint8 _cloneDecimalUnits,
                string _cloneTokenSymbol,
                uint _snapshotBlock,
                bool _transfersEnabled
                ) returns(address) {
                if (_snapshotBlock == 0) _snapshotBlock = getBlockNumber();
                MiniMeToken cloneToken = tokenFactory.createCloneToken(
                    this,
                    _snapshotBlock,
                    _cloneTokenName,
                    _cloneDecimalUnits,
                    _cloneTokenSymbol,
                    _transfersEnabled
                    );
        
                cloneToken.changeController(msg.sender);
        
                // An event to make the token easy to find on the blockchain
                NewCloneToken(address(cloneToken), _snapshotBlock);
                return address(cloneToken);
            }
        
        ////////////////
        // Generate and destroy tokens
        ////////////////
        
            /// @notice Generates `_amount` tokens that are assigned to `_owner`
            /// @param _owner The address that will be assigned the new tokens
            /// @param _amount The quantity of tokens generated
            /// @return True if the tokens are generated correctly
            function generateTokens(address _owner, uint _amount
            ) onlyController returns (bool) {
                uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber());
                if (curTotalSupply + _amount < curTotalSupply) throw; // Check for overflow
                updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount);
                var previousBalanceTo = balanceOf(_owner);
                if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow
                updateValueAtNow(balances[_owner], previousBalanceTo + _amount);
                Transfer(0, _owner, _amount);
                return true;
            }
        
        
            /// @notice Burns `_amount` tokens from `_owner`
            /// @param _owner The address that will lose the tokens
            /// @param _amount The quantity of tokens to burn
            /// @return True if the tokens are burned correctly
            function destroyTokens(address _owner, uint _amount
            ) onlyController returns (bool) {
                uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber());
                if (curTotalSupply < _amount) throw;
                updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount);
                var previousBalanceFrom = balanceOf(_owner);
                if (previousBalanceFrom < _amount) throw;
                updateValueAtNow(balances[_owner], previousBalanceFrom - _amount);
                Transfer(_owner, 0, _amount);
                return true;
            }
        
        ////////////////
        // Enable tokens transfers
        ////////////////
        
        
            /// @notice Enables token holders to transfer their tokens freely if true
            /// @param _transfersEnabled True if transfers are allowed in the clone
            function enableTransfers(bool _transfersEnabled) onlyController {
                transfersEnabled = _transfersEnabled;
            }
        
        ////////////////
        // Internal helper functions to query and set a value in a snapshot array
        ////////////////
        
            /// @dev `getValueAt` retrieves the number of tokens at a given block number
            /// @param checkpoints The history of values being queried
            /// @param _block The block number to retrieve the value at
            /// @return The number of tokens being queried
            function getValueAt(Checkpoint[] storage checkpoints, uint _block
            ) constant internal returns (uint) {
                if (checkpoints.length == 0) return 0;
        
                // Shortcut for the actual value
                if (_block >= checkpoints[checkpoints.length-1].fromBlock)
                    return checkpoints[checkpoints.length-1].value;
                if (_block < checkpoints[0].fromBlock) return 0;
        
                // Binary search of the value in the array
                uint min = 0;
                uint max = checkpoints.length-1;
                while (max > min) {
                    uint mid = (max + min + 1)/ 2;
                    if (checkpoints[mid].fromBlock<=_block) {
                        min = mid;
                    } else {
                        max = mid-1;
                    }
                }
                return checkpoints[min].value;
            }
        
            /// @dev `updateValueAtNow` used to update the `balances` map and the
            ///  `totalSupplyHistory`
            /// @param checkpoints The history of data being updated
            /// @param _value The new number of tokens
            function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value
            ) internal  {
                if ((checkpoints.length == 0)
                || (checkpoints[checkpoints.length -1].fromBlock < getBlockNumber())) {
                       Checkpoint newCheckPoint = checkpoints[ checkpoints.length++ ];
                       newCheckPoint.fromBlock =  uint128(getBlockNumber());
                       newCheckPoint.value = uint128(_value);
                   } else {
                       Checkpoint oldCheckPoint = checkpoints[checkpoints.length-1];
                       oldCheckPoint.value = uint128(_value);
                   }
            }
        
            /// @dev Internal function to determine if an address is a contract
            /// @param _addr The address being queried
            /// @return True if `_addr` is a contract
            function isContract(address _addr) constant internal returns(bool) {
                uint size;
                if (_addr == 0) return false;
                assembly {
                    size := extcodesize(_addr)
                }
                return size>0;
            }
        
            /// @dev Helper function to return a min betwen the two uints
            function min(uint a, uint b) internal returns (uint) {
                return a < b ? a : b;
            }
        
            /// @notice The fallback function: If the contract's controller has not been
            ///  set to 0, then the `proxyPayment` method is called which relays the
            ///  ether and creates tokens as described in the token controller contract
            function ()  payable {
                if (isContract(controller)) {
                    if (! TokenController(controller).proxyPayment.value(msg.value)(msg.sender))
                        throw;
                } else {
                    throw;
                }
            }
        
        
        //////////
        // Testing specific methods
        //////////
        
            /// @notice This function is overridden by the test Mocks.
            function getBlockNumber() internal constant returns (uint256) {
                return block.number;
            }
        
        //////////
        // Safety Methods
        //////////
        
            /// @notice This method can be used by the controller to extract mistakenly
            ///  sent tokens to this contract.
            /// @param _token The address of the token contract that you want to recover
            ///  set to 0 in case you want to extract ether.
            function claimTokens(address _token) onlyController {
                if (_token == 0x0) {
                    controller.transfer(this.balance);
                    return;
                }
        
                ERC20Token token = ERC20Token(_token);
                uint balance = token.balanceOf(this);
                token.transfer(controller, balance);
                ClaimedTokens(_token, controller, balance);
            }
        
        ////////////////
        // Events
        ////////////////
        
            event ClaimedTokens(address indexed _token, address indexed _controller, uint _amount);
            event Transfer(address indexed _from, address indexed _to, uint256 _amount);
            event NewCloneToken(address indexed _cloneToken, uint _snapshotBlock);
            event Approval(
                address indexed _owner,
                address indexed _spender,
                uint256 _amount
                );
        
        }
        
        
        ////////////////
        // MiniMeTokenFactory
        ////////////////
        
        /// @dev This contract is used to generate clone contracts from a contract.
        ///  In solidity this is the way to create a contract from a contract of the
        ///  same class
        contract MiniMeTokenFactory {
        
            /// @notice Update the DApp by creating a new token with new functionalities
            ///  the msg.sender becomes the controller of this clone token
            /// @param _parentToken Address of the token being cloned
            /// @param _snapshotBlock Block of the parent token that will
            ///  determine the initial distribution of the clone token
            /// @param _tokenName Name of the new token
            /// @param _decimalUnits Number of decimals of the new token
            /// @param _tokenSymbol Token Symbol for the new token
            /// @param _transfersEnabled If true, tokens will be able to be transferred
            /// @return The address of the new token contract
            function createCloneToken(
                address _parentToken,
                uint _snapshotBlock,
                string _tokenName,
                uint8 _decimalUnits,
                string _tokenSymbol,
                bool _transfersEnabled
            ) returns (MiniMeToken) {
                MiniMeToken newToken = new MiniMeToken(
                    this,
                    _parentToken,
                    _snapshotBlock,
                    _tokenName,
                    _decimalUnits,
                    _tokenSymbol,
                    _transfersEnabled
                    );
        
                newToken.changeController(msg.sender);
                return newToken;
            }
        }
        
        
        /*
            Copyright 2017, Jarrad Hope (Status Research & Development GmbH)
        */
        
        
        contract SNT is MiniMeToken {
            // @dev SNT constructor just parametrizes the MiniMeIrrevocableVestedToken constructor
            function SNT(address _tokenFactory)
                    MiniMeToken(
                        _tokenFactory,
                        0x0,                     // no parent token
                        0,                       // no snapshot block number from parent
                        "Status Network Token",  // Token name
                        18,                      // Decimals
                        "SNT",                   // Symbol
                        true                     // Enable transfers
                    ) {}
        }
        
        
        /*
            Copyright 2017, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title StatusContribution Contract
        /// @author Jordi Baylina
        /// @dev This contract will be the SNT controller during the contribution period.
        ///  This contract will determine the rules during this period.
        ///  Final users will generally not interact directly with this contract. ETH will
        ///  be sent to the SNT token contract. The ETH is sent to this contract and from here,
        ///  ETH is sent to the contribution walled and SNTs are mined according to the defined
        ///  rules.
        
        
        contract StatusContribution is Owned, TokenController {
            using SafeMath for uint256;
        
            uint256 constant public failSafeLimit = 300000 ether;
            uint256 constant public maxGuaranteedLimit = 30000 ether;
            uint256 constant public exchangeRate = 10000;
            uint256 constant public maxGasPrice = 50000000000;
            uint256 constant public maxCallFrequency = 100;
        
            MiniMeToken public SGT;
            MiniMeToken public SNT;
            uint256 public startBlock;
            uint256 public endBlock;
        
            address public destEthDevs;
        
            address public destTokensDevs;
            address public destTokensReserve;
            uint256 public maxSGTSupply;
            address public destTokensSgt;
            DynamicCeiling public dynamicCeiling;
        
            address public sntController;
        
            mapping (address => uint256) public guaranteedBuyersLimit;
            mapping (address => uint256) public guaranteedBuyersBought;
        
            uint256 public totalGuaranteedCollected;
            uint256 public totalNormalCollected;
        
            uint256 public finalizedBlock;
            uint256 public finalizedTime;
        
            mapping (address => uint256) public lastCallBlock;
        
            bool public paused;
        
            modifier initialized() {
                require(address(SNT) != 0x0);
                _;
            }
        
            modifier contributionOpen() {
                require(getBlockNumber() >= startBlock &&
                        getBlockNumber() <= endBlock &&
                        finalizedBlock == 0 &&
                        address(SNT) != 0x0);
                _;
            }
        
            modifier notPaused() {
                require(!paused);
                _;
            }
        
            function StatusContribution() {
                paused = false;
            }
        
        
            /// @notice This method should be called by the owner before the contribution
            ///  period starts This initializes most of the parameters
            /// @param _snt Address of the SNT token contract
            /// @param _sntController Token controller for the SNT that will be transferred after
            ///  the contribution finalizes.
            /// @param _startBlock Block when the contribution period starts
            /// @param _endBlock The last block that the contribution period is active
            /// @param _dynamicCeiling Address of the contract that controls the ceiling
            /// @param _destEthDevs Destination address where the contribution ether is sent
            /// @param _destTokensReserve Address where the tokens for the reserve are sent
            /// @param _destTokensSgt Address of the exchanger SGT-SNT where the SNT are sent
            ///  to be distributed to the SGT holders.
            /// @param _destTokensDevs Address where the tokens for the dev are sent
            /// @param _sgt Address of the SGT token contract
            /// @param _maxSGTSupply Quantity of SGT tokens that would represent 10% of status.
            function initialize(
                address _snt,
                address _sntController,
        
                uint256 _startBlock,
                uint256 _endBlock,
        
                address _dynamicCeiling,
        
                address _destEthDevs,
        
                address _destTokensReserve,
                address _destTokensSgt,
                address _destTokensDevs,
        
                address _sgt,
                uint256 _maxSGTSupply
            ) public onlyOwner {
                // Initialize only once
                require(address(SNT) == 0x0);
        
                SNT = MiniMeToken(_snt);
                require(SNT.totalSupply() == 0);
                require(SNT.controller() == address(this));
                require(SNT.decimals() == 18);  // Same amount of decimals as ETH
        
                require(_sntController != 0x0);
                sntController = _sntController;
        
                require(_startBlock >= getBlockNumber());
                require(_startBlock < _endBlock);
                startBlock = _startBlock;
                endBlock = _endBlock;
        
                require(_dynamicCeiling != 0x0);
                dynamicCeiling = DynamicCeiling(_dynamicCeiling);
        
                require(_destEthDevs != 0x0);
                destEthDevs = _destEthDevs;
        
                require(_destTokensReserve != 0x0);
                destTokensReserve = _destTokensReserve;
        
                require(_destTokensSgt != 0x0);
                destTokensSgt = _destTokensSgt;
        
                require(_destTokensDevs != 0x0);
                destTokensDevs = _destTokensDevs;
        
                require(_sgt != 0x0);
                SGT = MiniMeToken(_sgt);
        
                require(_maxSGTSupply >= MiniMeToken(SGT).totalSupply());
                maxSGTSupply = _maxSGTSupply;
            }
        
            /// @notice Sets the limit for a guaranteed address. All the guaranteed addresses
            ///  will be able to get SNTs during the contribution period with his own
            ///  specific limit.
            ///  This method should be called by the owner after the initialization
            ///  and before the contribution starts.
            /// @param _th Guaranteed address
            /// @param _limit Limit for the guaranteed address.
            function setGuaranteedAddress(address _th, uint256 _limit) public initialized onlyOwner {
                require(getBlockNumber() < startBlock);
                require(_limit > 0 && _limit <= maxGuaranteedLimit);
                guaranteedBuyersLimit[_th] = _limit;
                GuaranteedAddress(_th, _limit);
            }
        
            /// @notice If anybody sends Ether directly to this contract, consider he is
            ///  getting SNTs.
            function () public payable notPaused {
                proxyPayment(msg.sender);
            }
        
        
            //////////
            // MiniMe Controller functions
            //////////
        
            /// @notice This method will generally be called by the SNT token contract to
            ///  acquire SNTs. Or directly from third parties that want to acquire SNTs in
            ///  behalf of a token holder.
            /// @param _th SNT holder where the SNTs will be minted.
            function proxyPayment(address _th) public payable notPaused initialized contributionOpen returns (bool) {
                require(_th != 0x0);
                if (guaranteedBuyersLimit[_th] > 0) {
                    buyGuaranteed(_th);
                } else {
                    buyNormal(_th);
                }
                return true;
            }
        
            function onTransfer(address, address, uint256) public returns (bool) {
                return false;
            }
        
            function onApprove(address, address, uint256) public returns (bool) {
                return false;
            }
        
            function buyNormal(address _th) internal {
                require(tx.gasprice <= maxGasPrice);
        
                // Antispam mechanism
                address caller;
                if (msg.sender == address(SNT)) {
                    caller = _th;
                } else {
                    caller = msg.sender;
                }
        
                // Do not allow contracts to game the system
                require(!isContract(caller));
        
                require(getBlockNumber().sub(lastCallBlock[caller]) >= maxCallFrequency);
                lastCallBlock[caller] = getBlockNumber();
        
                uint256 toCollect = dynamicCeiling.toCollect(totalNormalCollected);
        
                uint256 toFund;
                if (msg.value <= toCollect) {
                    toFund = msg.value;
                } else {
                    toFund = toCollect;
                }
        
                totalNormalCollected = totalNormalCollected.add(toFund);
                doBuy(_th, toFund, false);
            }
        
            function buyGuaranteed(address _th) internal {
                uint256 toCollect = guaranteedBuyersLimit[_th];
        
                uint256 toFund;
                if (guaranteedBuyersBought[_th].add(msg.value) > toCollect) {
                    toFund = toCollect.sub(guaranteedBuyersBought[_th]);
                } else {
                    toFund = msg.value;
                }
        
                guaranteedBuyersBought[_th] = guaranteedBuyersBought[_th].add(toFund);
                totalGuaranteedCollected = totalGuaranteedCollected.add(toFund);
                doBuy(_th, toFund, true);
            }
        
            function doBuy(address _th, uint256 _toFund, bool _guaranteed) internal {
                assert(msg.value >= _toFund);  // Not needed, but double check.
                assert(totalCollected() <= failSafeLimit);
        
                if (_toFund > 0) {
                    uint256 tokensGenerated = _toFund.mul(exchangeRate);
                    assert(SNT.generateTokens(_th, tokensGenerated));
                    destEthDevs.transfer(_toFund);
                    NewSale(_th, _toFund, tokensGenerated, _guaranteed);
                }
        
                uint256 toReturn = msg.value.sub(_toFund);
                if (toReturn > 0) {
                    // If the call comes from the Token controller,
                    // then we return it to the token Holder.
                    // Otherwise we return to the sender.
                    if (msg.sender == address(SNT)) {
                        _th.transfer(toReturn);
                    } else {
                        msg.sender.transfer(toReturn);
                    }
                }
            }
        
            // NOTE on Percentage format
            // Right now, Solidity does not support decimal numbers. (This will change very soon)
            //  So in this contract we use a representation of a percentage that consist in
            //  expressing the percentage in "x per 10**18"
            // This format has a precision of 16 digits for a percent.
            // Examples:
            //  3%   =   3*(10**16)
            //  100% = 100*(10**16) = 10**18
            //
            // To get a percentage of a value we do it by first multiplying it by the percentage in  (x per 10^18)
            //  and then divide it by 10**18
            //
            //              Y * X(in x per 10**18)
            //  X% of Y = -------------------------
            //               100(in x per 10**18)
            //
        
        
            /// @notice This method will can be called by the owner before the contribution period
            ///  end or by anybody after the `endBlock`. This method finalizes the contribution period
            ///  by creating the remaining tokens and transferring the controller to the configured
            ///  controller.
            function finalize() public initialized {
                require(getBlockNumber() >= startBlock);
                require(msg.sender == owner || getBlockNumber() > endBlock);
                require(finalizedBlock == 0);
        
                // Do not allow termination until all curves revealed.
                require(dynamicCeiling.allRevealed());
        
                // Allow premature finalization if final limit is reached
                if (getBlockNumber() <= endBlock) {
                    var (,lastLimit,,) = dynamicCeiling.curves(dynamicCeiling.revealedCurves().sub(1));
                    require(totalNormalCollected >= lastLimit);
                }
        
                finalizedBlock = getBlockNumber();
                finalizedTime = now;
        
                uint256 percentageToSgt;
                if (SGT.totalSupply() >= maxSGTSupply) {
                    percentageToSgt = percent(10);  // 10%
                } else {
        
                    //
                    //                           SGT.totalSupply()
                    //  percentageToSgt = 10% * -------------------
                    //                             maxSGTSupply
                    //
                    percentageToSgt = percent(10).mul(SGT.totalSupply()).div(maxSGTSupply);
                }
        
                uint256 percentageToDevs = percent(20);  // 20%
        
        
                //
                //  % To Contributors = 41% + (10% - % to SGT holders)
                //
                uint256 percentageToContributors = percent(41).add(percent(10).sub(percentageToSgt));
        
                uint256 percentageToReserve = percent(29);
        
        
                // SNT.totalSupply() -> Tokens minted during the contribution
                //  totalTokens  -> Total tokens that should be after the allocation
                //                   of devTokens, sgtTokens and reserve
                //  percentageToContributors -> Which percentage should go to the
                //                               contribution participants
                //                               (x per 10**18 format)
                //  percent(100) -> 100% in (x per 10**18 format)
                //
                //                       percentageToContributors
                //  SNT.totalSupply() = -------------------------- * totalTokens  =>
                //                             percent(100)
                //
                //
                //                            percent(100)
                //  =>  totalTokens = ---------------------------- * SNT.totalSupply()
                //                      percentageToContributors
                //
                uint256 totalTokens = SNT.totalSupply().mul(percent(100)).div(percentageToContributors);
        
        
                // Generate tokens for SGT Holders.
        
                //
                //                    percentageToReserve
                //  reserveTokens = ----------------------- * totalTokens
                //                      percentage(100)
                //
                assert(SNT.generateTokens(
                    destTokensReserve,
                    totalTokens.mul(percentageToReserve).div(percent(100))));
        
                //
                //                  percentageToSgt
                //  sgtTokens = ----------------------- * totalTokens
                //                   percentage(100)
                //
                assert(SNT.generateTokens(
                    destTokensSgt,
                    totalTokens.mul(percentageToSgt).div(percent(100))));
        
        
                //
                //                   percentageToDevs
                //  devTokens = ----------------------- * totalTokens
                //                   percentage(100)
                //
                assert(SNT.generateTokens(
                    destTokensDevs,
                    totalTokens.mul(percentageToDevs).div(percent(100))));
        
                SNT.changeController(sntController);
        
                Finalized();
            }
        
            function percent(uint256 p) internal returns (uint256) {
                return p.mul(10**16);
            }
        
            /// @dev Internal function to determine if an address is a contract
            /// @param _addr The address being queried
            /// @return True if `_addr` is a contract
            function isContract(address _addr) constant internal returns (bool) {
                if (_addr == 0) return false;
                uint256 size;
                assembly {
                    size := extcodesize(_addr)
                }
                return (size > 0);
            }
        
        
            //////////
            // Constant functions
            //////////
        
            /// @return Total tokens issued in weis.
            function tokensIssued() public constant returns (uint256) {
                return SNT.totalSupply();
            }
        
            /// @return Total Ether collected.
            function totalCollected() public constant returns (uint256) {
                return totalNormalCollected.add(totalGuaranteedCollected);
            }
        
        
            //////////
            // Testing specific methods
            //////////
        
            /// @notice This function is overridden by the test Mocks.
            function getBlockNumber() internal constant returns (uint256) {
                return block.number;
            }
        
        
            //////////
            // Safety Methods
            //////////
        
            /// @notice This method can be used by the controller to extract mistakenly
            ///  sent tokens to this contract.
            /// @param _token The address of the token contract that you want to recover
            ///  set to 0 in case you want to extract ether.
            function claimTokens(address _token) public onlyOwner {
                if (SNT.controller() == address(this)) {
                    SNT.claimTokens(_token);
                }
                if (_token == 0x0) {
                    owner.transfer(this.balance);
                    return;
                }
        
                ERC20Token token = ERC20Token(_token);
                uint256 balance = token.balanceOf(this);
                token.transfer(owner, balance);
                ClaimedTokens(_token, owner, balance);
            }
        
        
            /// @notice Pauses the contribution if there is any issue
            function pauseContribution() onlyOwner {
                paused = true;
            }
        
            /// @notice Resumes the contribution
            function resumeContribution() onlyOwner {
                paused = false;
            }
        
            event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount);
            event NewSale(address indexed _th, uint256 _amount, uint256 _tokens, bool _guaranteed);
            event GuaranteedAddress(address indexed _th, uint256 _limit);
            event Finalized();
        }
        
        
        /*
            Copyright 2017, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title ContributionWallet Contract
        /// @author Jordi Baylina
        /// @dev This contract will be hold the Ether during the contribution period.
        ///  The idea of this contract is to avoid recycling Ether during the contribution
        ///  period. So all the ETH collected will be locked here until the contribution
        ///  period ends
        
        // @dev Contract to hold sale raised funds during the sale period.
        // Prevents attack in which the Aragon Multisig sends raised ether
        // to the sale contract to mint tokens to itself, and getting the
        // funds back immediately.
        
        
        
        contract ContributionWallet {
        
            // Public variables
            address public multisig;
            uint256 public endBlock;
            StatusContribution public contribution;
        
            // @dev Constructor initializes public variables
            // @param _multisig The address of the multisig that will receive the funds
            // @param _endBlock Block after which the multisig can request the funds
            // @param _contribution Address of the StatusContribution contract
            function ContributionWallet(address _multisig, uint256 _endBlock, address _contribution) {
                require(_multisig != 0x0);
                require(_contribution != 0x0);
                require(_endBlock != 0 && _endBlock <= 4000000);
                multisig = _multisig;
                endBlock = _endBlock;
                contribution = StatusContribution(_contribution);
            }
        
            // @dev Receive all sent funds without any further logic
            function () public payable {}
        
            // @dev Withdraw function sends all the funds to the wallet if conditions are correct
            function withdraw() public {
                require(msg.sender == multisig);              // Only the multisig can request it
                require(block.number > endBlock ||            // Allow after end block
                        contribution.finalizedBlock() != 0);  // Allow when sale is finalized
                multisig.transfer(this.balance);
            }
        
        }
        
        
        /*
            Copyright 2017, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title DevTokensHolder Contract
        /// @author Jordi Baylina
        /// @dev This contract will hold the tokens of the developers.
        ///  Tokens will not be able to be collected until 6 months after the contribution
        ///  period ends. And it will be increasing linearly until 2 years.
        
        
        //  collectable tokens
        //   |                         _/--------   vestedTokens rect
        //   |                       _/
        //   |                     _/
        //   |                   _/
        //   |                 _/
        //   |               _/
        //   |             _/
        //   |           _/
        //   |          |
        //   |        . |
        //   |      .   |
        //   |    .     |
        //   +===+======+--------------+----------> time
        //     Contrib   6 Months       24 Months
        //       End
        
        
        
        contract DevTokensHolder is Owned {
            using SafeMath for uint256;
        
            uint256 collectedTokens;
            StatusContribution contribution;
            MiniMeToken snt;
        
            function DevTokensHolder(address _owner, address _contribution, address _snt) {
                owner = _owner;
                contribution = StatusContribution(_contribution);
                snt = MiniMeToken(_snt);
            }
        
        
            /// @notice The Dev (Owner) will call this method to extract the tokens
            function collectTokens() public onlyOwner {
                uint256 balance = snt.balanceOf(address(this));
                uint256 total = collectedTokens.add(balance);
        
                uint256 finalizedTime = contribution.finalizedTime();
        
                require(finalizedTime > 0 && getTime() > finalizedTime.add(months(6)));
        
                uint256 canExtract = total.mul(getTime().sub(finalizedTime)).div(months(24));
        
                canExtract = canExtract.sub(collectedTokens);
        
                if (canExtract > balance) {
                    canExtract = balance;
                }
        
                collectedTokens = collectedTokens.add(canExtract);
                assert(snt.transfer(owner, canExtract));
        
                TokensWithdrawn(owner, canExtract);
            }
        
            function months(uint256 m) internal returns (uint256) {
                return m.mul(30 days);
            }
        
            function getTime() internal returns (uint256) {
                return now;
            }
        
        
            //////////
            // Safety Methods
            //////////
        
            /// @notice This method can be used by the controller to extract mistakenly
            ///  sent tokens to this contract.
            /// @param _token The address of the token contract that you want to recover
            ///  set to 0 in case you want to extract ether.
            function claimTokens(address _token) public onlyOwner {
                require(_token != address(snt));
                if (_token == 0x0) {
                    owner.transfer(this.balance);
                    return;
                }
        
                ERC20Token token = ERC20Token(_token);
                uint256 balance = token.balanceOf(this);
                token.transfer(owner, balance);
                ClaimedTokens(_token, owner, balance);
            }
        
            event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount);
            event TokensWithdrawn(address indexed _holder, uint256 _amount);
        }
        
        
        /*
            Copyright 2017, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title SGTExchanger Contract
        /// @author Jordi Baylina
        /// @dev This contract will be used to distribute SNT between SGT holders.
        ///  SGT token is not transferable, and we just keep an accounting between all tokens
        ///  deposited and the tokens collected.
        ///  The controllerShip of SGT should be transferred to this contract before the
        ///  contribution period starts.
        
        
        contract SGTExchanger is TokenController, Owned {
            using SafeMath for uint256;
        
            mapping (address => uint256) public collected;
            uint256 public totalCollected;
            MiniMeToken public sgt;
            MiniMeToken public snt;
            StatusContribution public statusContribution;
        
            function SGTExchanger(address _sgt, address _snt, address _statusContribution) {
                sgt = MiniMeToken(_sgt);
                snt = MiniMeToken(_snt);
                statusContribution = StatusContribution(_statusContribution);
            }
        
            /// @notice This method should be called by the SGT holders to collect their
            ///  corresponding SNTs
            function collect() public {
                uint256 finalizedBlock = statusContribution.finalizedBlock();
        
                require(finalizedBlock != 0);
                require(getBlockNumber() > finalizedBlock);
        
                uint256 total = totalCollected.add(snt.balanceOf(address(this)));
        
                uint256 balance = sgt.balanceOfAt(msg.sender, finalizedBlock);
        
                // First calculate how much correspond to him
                uint256 amount = total.mul(balance).div(sgt.totalSupplyAt(finalizedBlock));
        
                // And then subtract the amount already collected
                amount = amount.sub(collected[msg.sender]);
        
                require(amount > 0);  // Notify the user that there are no tokens to exchange
        
                totalCollected = totalCollected.add(amount);
                collected[msg.sender] = collected[msg.sender].add(amount);
        
                assert(snt.transfer(msg.sender, amount));
        
                TokensCollected(msg.sender, amount);
            }
        
            function proxyPayment(address) public payable returns (bool) {
                throw;
            }
        
            function onTransfer(address, address, uint256) public returns (bool) {
                return false;
            }
        
            function onApprove(address, address, uint256) public returns (bool) {
                return false;
            }
        
            //////////
            // Testing specific methods
            //////////
        
            /// @notice This function is overridden by the test Mocks.
            function getBlockNumber() internal constant returns (uint256) {
                return block.number;
            }
        
            //////////
            // Safety Method
            //////////
        
            /// @notice This method can be used by the controller to extract mistakenly
            ///  sent tokens to this contract.
            /// @param _token The address of the token contract that you want to recover
            ///  set to 0 in case you want to extract ether.
            function claimTokens(address _token) public onlyOwner {
                require(_token != address(snt));
                if (_token == 0x0) {
                    owner.transfer(this.balance);
                    return;
                }
        
                ERC20Token token = ERC20Token(_token);
                uint256 balance = token.balanceOf(this);
                token.transfer(owner, balance);
                ClaimedTokens(_token, owner, balance);
            }
        
            event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount);
            event TokensCollected(address indexed _holder, uint256 _amount);
        
        }
        
        /*
            Copyright 2017, Jordi Baylina
        
            This program is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.
        
            This program is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.
        
            You should have received a copy of the GNU General Public License
            along with this program.  If not, see <http://www.gnu.org/licenses/>.
         */
        
        /// @title SNTPlaceholder Contract
        /// @author Jordi Baylina
        /// @dev The SNTPlaceholder contract will take control over the SNT after the contribution
        ///  is finalized and before the Status Network is deployed.
        ///  The contract allows for SNT transfers and transferFrom and implements the
        ///  logic for transferring control of the token to the network when the offering
        ///  asks it to do so.
        
        
        contract SNTPlaceHolder is TokenController, Owned {
            using SafeMath for uint256;
        
            MiniMeToken public snt;
            StatusContribution public contribution;
            uint256 public activationTime;
            address public sgtExchanger;
        
            /// @notice Constructor
            /// @param _owner Trusted owner for this contract.
            /// @param _snt SNT token contract address
            /// @param _contribution StatusContribution contract address
            /// @param _sgtExchanger SGT-SNT Exchange address. (During the first week
            ///  only this exchanger will be able to move tokens)
            function SNTPlaceHolder(address _owner, address _snt, address _contribution, address _sgtExchanger) {
                owner = _owner;
                snt = MiniMeToken(_snt);
                contribution = StatusContribution(_contribution);
                sgtExchanger = _sgtExchanger;
            }
        
            /// @notice The owner of this contract can change the controller of the SNT token
            ///  Please, be sure that the owner is a trusted agent or 0x0 address.
            /// @param _newController The address of the new controller
        
            function changeController(address _newController) public onlyOwner {
                snt.changeController(_newController);
                ControllerChanged(_newController);
            }
        
        
            //////////
            // MiniMe Controller Interface functions
            //////////
        
            // In between the offering and the network. Default settings for allowing token transfers.
            function proxyPayment(address) public payable returns (bool) {
                return false;
            }
        
            function onTransfer(address _from, address, uint256) public returns (bool) {
                return transferable(_from);
            }
        
            function onApprove(address _from, address, uint256) public returns (bool) {
                return transferable(_from);
            }
        
            function transferable(address _from) internal returns (bool) {
                // Allow the exchanger to work from the beginning
                if (activationTime == 0) {
                    uint256 f = contribution.finalizedTime();
                    if (f > 0) {
                        activationTime = f.add(1 weeks);
                    } else {
                        return false;
                    }
                }
                return (getTime() > activationTime) || (_from == sgtExchanger);
            }
        
        
            //////////
            // Testing specific methods
            //////////
        
            /// @notice This function is overrided by the test Mocks.
            function getTime() internal returns (uint256) {
                return now;
            }
        
        
            //////////
            // Safety Methods
            //////////
        
            /// @notice This method can be used by the controller to extract mistakenly
            ///  sent tokens to this contract.
            /// @param _token The address of the token contract that you want to recover
            ///  set to 0 in case you want to extract ether.
            function claimTokens(address _token) public onlyOwner {
                if (snt.controller() == address(this)) {
                    snt.claimTokens(_token);
                }
                if (_token == 0x0) {
                    owner.transfer(this.balance);
                    return;
                }
        
                ERC20Token token = ERC20Token(_token);
                uint256 balance = token.balanceOf(this);
                token.transfer(owner, balance);
                ClaimedTokens(_token, owner, balance);
            }
        
            event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount);
            event ControllerChanged(address indexed _newController);
        }

        File 5 of 8: SmartToken
        pragma solidity ^0.4.11;
        
        /*
            Overflow protected math functions
        */
        contract SafeMath {
            /**
                constructor
            */
            function SafeMath() {
            }
        
            /**
                @dev returns the sum of _x and _y, asserts if the calculation overflows
        
                @param _x   value 1
                @param _y   value 2
        
                @return sum
            */
            function safeAdd(uint256 _x, uint256 _y) internal returns (uint256) {
                uint256 z = _x + _y;
                assert(z >= _x);
                return z;
            }
        
            /**
                @dev returns the difference of _x minus _y, asserts if the subtraction results in a negative number
        
                @param _x   minuend
                @param _y   subtrahend
        
                @return difference
            */
            function safeSub(uint256 _x, uint256 _y) internal returns (uint256) {
                assert(_x >= _y);
                return _x - _y;
            }
        
            /**
                @dev returns the product of multiplying _x by _y, asserts if the calculation overflows
        
                @param _x   factor 1
                @param _y   factor 2
        
                @return product
            */
            function safeMul(uint256 _x, uint256 _y) internal returns (uint256) {
                uint256 z = _x * _y;
                assert(_x == 0 || z / _x == _y);
                return z;
            }
        } 
        
        /*
            Owned contract interface
        */
        contract IOwned {
            // this function isn't abstract since the compiler emits automatically generated getter functions as external
            function owner() public constant returns (address owner) { owner; }
        
            function transferOwnership(address _newOwner) public;
            function acceptOwnership() public;
        }
        
        /*
            Provides support and utilities for contract ownership
        */
        contract Owned is IOwned {
            address public owner;
            address public newOwner;
        
            event OwnerUpdate(address _prevOwner, address _newOwner);
        
            /**
                @dev constructor
            */
            function Owned() {
                owner = msg.sender;
            }
        
            // allows execution by the owner only
            modifier ownerOnly {
                assert(msg.sender == owner);
                _;
            }
        
            /**
                @dev allows transferring the contract ownership
                the new owner still need to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new contract owner
            */
            function transferOwnership(address _newOwner) public ownerOnly {
                require(_newOwner != owner);
                newOwner = _newOwner;
            }
        
            /**
                @dev used by a new owner to accept an ownership transfer
            */
            function acceptOwnership() public {
                require(msg.sender == newOwner);
                OwnerUpdate(owner, newOwner);
                owner = newOwner;
                newOwner = 0x0;
            }
        }
        
        /*
            Token Holder interface
        */
        contract ITokenHolder is IOwned {
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount) public;
        }
        
        /*
            We consider every contract to be a 'token holder' since it's currently not possible
            for a contract to deny receiving tokens.
        
            The TokenHolder's contract sole purpose is to provide a safety mechanism that allows
            the owner to send tokens that were sent to the contract by mistake back to their sender.
        */
        contract TokenHolder is ITokenHolder, Owned {
            /**
                @dev constructor
            */
            function TokenHolder() {
            }
        
            // validates an address - currently only checks that it isn't null
            modifier validAddress(address _address) {
                require(_address != 0x0);
                _;
            }
        
            // verifies that the address is different than this contract address
            modifier notThis(address _address) {
                require(_address != address(this));
                _;
            }
        
            /**
                @dev withdraws tokens held by the contract and sends them to an account
                can only be called by the owner
        
                @param _token   ERC20 token contract address
                @param _to      account to receive the new amount
                @param _amount  amount to withdraw
            */
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount)
                public
                ownerOnly
                validAddress(_token)
                validAddress(_to)
                notThis(_to)
            {
                assert(_token.transfer(_to, _amount));
            }
        }
        
        /*
            ERC20 Standard Token interface
        */
        contract IERC20Token {
            // these functions aren't abstract since the compiler emits automatically generated getter functions as external
            function name() public constant returns (string name) { name; }
            function symbol() public constant returns (string symbol) { symbol; }
            function decimals() public constant returns (uint8 decimals) { decimals; }
            function totalSupply() public constant returns (uint256 totalSupply) { totalSupply; }
            function balanceOf(address _owner) public constant returns (uint256 balance) { _owner; balance; }
            function allowance(address _owner, address _spender) public constant returns (uint256 remaining) { _owner; _spender; remaining; }
        
            function transfer(address _to, uint256 _value) public returns (bool success);
            function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
            function approve(address _spender, uint256 _value) public returns (bool success);
        }
        
        /**
            ERC20 Standard Token implementation
        */
        contract ERC20Token is IERC20Token, SafeMath {
            string public standard = 'Token 0.1';
            string public name = '';
            string public symbol = '';
            uint8 public decimals = 0;
            uint256 public totalSupply = 0;
            mapping (address => uint256) public balanceOf;
            mapping (address => mapping (address => uint256)) public allowance;
        
            event Transfer(address indexed _from, address indexed _to, uint256 _value);
            event Approval(address indexed _owner, address indexed _spender, uint256 _value);
        
            /**
                @dev constructor
        
                @param _name        token name
                @param _symbol      token symbol
                @param _decimals    decimal points, for display purposes
            */
            function ERC20Token(string _name, string _symbol, uint8 _decimals) {
                require(bytes(_name).length > 0 && bytes(_symbol).length > 0); // validate input
        
                name = _name;
                symbol = _symbol;
                decimals = _decimals;
            }
        
            // validates an address - currently only checks that it isn't null
            modifier validAddress(address _address) {
                require(_address != 0x0);
                _;
            }
        
            /**
                @dev send coins
                throws on any error rather then return a false flag to minimize user errors
        
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transfer(address _to, uint256 _value)
                public
                validAddress(_to)
                returns (bool success)
            {
                balanceOf[msg.sender] = safeSub(balanceOf[msg.sender], _value);
                balanceOf[_to] = safeAdd(balanceOf[_to], _value);
                Transfer(msg.sender, _to, _value);
                return true;
            }
        
            /**
                @dev an account/contract attempts to get the coins
                throws on any error rather then return a false flag to minimize user errors
        
                @param _from    source address
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transferFrom(address _from, address _to, uint256 _value)
                public
                validAddress(_from)
                validAddress(_to)
                returns (bool success)
            {
                allowance[_from][msg.sender] = safeSub(allowance[_from][msg.sender], _value);
                balanceOf[_from] = safeSub(balanceOf[_from], _value);
                balanceOf[_to] = safeAdd(balanceOf[_to], _value);
                Transfer(_from, _to, _value);
                return true;
            }
        
            /**
                @dev allow another account/contract to spend some tokens on your behalf
                throws on any error rather then return a false flag to minimize user errors
        
                also, to minimize the risk of the approve/transferFrom attack vector
                (see https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/), approve has to be called twice
                in 2 separate transactions - once to change the allowance to 0 and secondly to change it to the new allowance value
        
                @param _spender approved address
                @param _value   allowance amount
        
                @return true if the approval was successful, false if it wasn't
            */
            function approve(address _spender, uint256 _value)
                public
                validAddress(_spender)
                returns (bool success)
            {
                // if the allowance isn't 0, it can only be updated to 0 to prevent an allowance change immediately after withdrawal
                require(_value == 0 || allowance[msg.sender][_spender] == 0);
        
                allowance[msg.sender][_spender] = _value;
                Approval(msg.sender, _spender, _value);
                return true;
            }
        }
        
        /*
            Smart Token interface
        */
        contract ISmartToken is ITokenHolder, IERC20Token {
            function disableTransfers(bool _disable) public;
            function issue(address _to, uint256 _amount) public;
            function destroy(address _from, uint256 _amount) public;
        }
        
        /*
            Smart Token v0.2
        
            'Owned' is specified here for readability reasons
        */
        contract SmartToken is ISmartToken, ERC20Token, Owned, TokenHolder {
            string public version = '0.2';
        
            bool public transfersEnabled = true;    // true if transfer/transferFrom are enabled, false if not
        
            // triggered when a smart token is deployed - the _token address is defined for forward compatibility, in case we want to trigger the event from a factory
            event NewSmartToken(address _token);
            // triggered when the total supply is increased
            event Issuance(uint256 _amount);
            // triggered when the total supply is decreased
            event Destruction(uint256 _amount);
        
            /**
                @dev constructor
        
                @param _name       token name
                @param _symbol     token short symbol, 1-6 characters
                @param _decimals   for display purposes only
            */
            function SmartToken(string _name, string _symbol, uint8 _decimals)
                ERC20Token(_name, _symbol, _decimals)
            {
                require(bytes(_symbol).length <= 6); // validate input
                NewSmartToken(address(this));
            }
        
            // allows execution only when transfers aren't disabled
            modifier transfersAllowed {
                assert(transfersEnabled);
                _;
            }
        
            /**
                @dev disables/enables transfers
                can only be called by the contract owner
        
                @param _disable    true to disable transfers, false to enable them
            */
            function disableTransfers(bool _disable) public ownerOnly {
                transfersEnabled = !_disable;
            }
        
            /**
                @dev increases the token supply and sends the new tokens to an account
                can only be called by the contract owner
        
                @param _to         account to receive the new amount
                @param _amount     amount to increase the supply by
            */
            function issue(address _to, uint256 _amount)
                public
                ownerOnly
                validAddress(_to)
                notThis(_to)
            {
                totalSupply = safeAdd(totalSupply, _amount);
                balanceOf[_to] = safeAdd(balanceOf[_to], _amount);
        
                Issuance(_amount);
                Transfer(this, _to, _amount);
            }
        
            /**
                @dev removes tokens from an account and decreases the token supply
                can only be called by the contract owner
        
                @param _from       account to remove the amount from
                @param _amount     amount to decrease the supply by
            */
            function destroy(address _from, uint256 _amount)
                public
                ownerOnly
            {
                balanceOf[_from] = safeSub(balanceOf[_from], _amount);
                totalSupply = safeSub(totalSupply, _amount);
        
                Transfer(_from, this, _amount);
                Destruction(_amount);
            }
        
            // ERC20 standard method overrides with some extra functionality
        
            /**
                @dev send coins
                throws on any error rather then return a false flag to minimize user errors
                note that when transferring to the smart token's address, the coins are actually destroyed
        
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transfer(address _to, uint256 _value) public transfersAllowed returns (bool success) {
                assert(super.transfer(_to, _value));
        
                // transferring to the contract address destroys tokens
                if (_to == address(this)) {
                    balanceOf[_to] -= _value;
                    totalSupply -= _value;
                    Destruction(_value);
                }
        
                return true;
            }
        
            /**
                @dev an account/contract attempts to get the coins
                throws on any error rather then return a false flag to minimize user errors
                note that when transferring to the smart token's address, the coins are actually destroyed
        
                @param _from    source address
                @param _to      target address
                @param _value   transfer amount
        
                @return true if the transfer was successful, false if it wasn't
            */
            function transferFrom(address _from, address _to, uint256 _value) public transfersAllowed returns (bool success) {
                assert(super.transferFrom(_from, _to, _value));
        
                // transferring to the contract address destroys tokens
                if (_to == address(this)) {
                    balanceOf[_to] -= _value;
                    totalSupply -= _value;
                    Destruction(_value);
                }
        
                return true;
            }
        }

        File 6 of 8: ContractRegistry
        pragma solidity ^0.4.21;
        
        /*
            Owned contract interface
        */
        contract IOwned {
            // this function isn't abstract since the compiler emits automatically generated getter functions as external
            function owner() public view returns (address) {}
        
            function transferOwnership(address _newOwner) public;
            function acceptOwnership() public;
        }
        
        /*
            Provides support and utilities for contract ownership
        */
        contract Owned is IOwned {
            address public owner;
            address public newOwner;
        
            event OwnerUpdate(address indexed _prevOwner, address indexed _newOwner);
        
            /**
                @dev constructor
            */
            function Owned() public {
                owner = msg.sender;
            }
        
            // allows execution by the owner only
            modifier ownerOnly {
                assert(msg.sender == owner);
                _;
            }
        
            /**
                @dev allows transferring the contract ownership
                the new owner still needs to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new contract owner
            */
            function transferOwnership(address _newOwner) public ownerOnly {
                require(_newOwner != owner);
                newOwner = _newOwner;
            }
        
            /**
                @dev used by a new owner to accept an ownership transfer
            */
            function acceptOwnership() public {
                require(msg.sender == newOwner);
                emit OwnerUpdate(owner, newOwner);
                owner = newOwner;
                newOwner = address(0);
            }
        }
        
        /*
            Contract Registry interface
        */
        contract IContractRegistry {
            function getAddress(bytes32 _contractName) public view returns (address);
        }
        
        /**
            Contract Registry
        
            The contract registry keeps contract addresses by name.
            The owner can update contract addresses so that a contract name always points to the latest version
            of the given contract.
            Other contracts can query the registry to get updated addresses instead of depending on specific
            addresses.
        
            Note that contract names are limited to 32 bytes, UTF8 strings to optimize gas costs
        */
        contract ContractRegistry is IContractRegistry, Owned {
            mapping (bytes32 => address) addresses;
        
            event AddressUpdate(bytes32 indexed _contractName, address _contractAddress);
        
            /**
                @dev constructor
            */
            function ContractRegistry() public {
            }
        
            /**
                @dev returns the address associated with the given contract name
        
                @param _contractName    contract name
        
                @return contract address
            */
            function getAddress(bytes32 _contractName) public view returns (address) {
                return addresses[_contractName];
            }
        
            /**
                @dev registers a new address for the contract name
        
               @param _contractName     contract name
               @param _contractAddress  contract address
            */
            function registerAddress(bytes32 _contractName, address _contractAddress) public ownerOnly {
                require(_contractName.length > 0); // validating input
        
                addresses[_contractName] = _contractAddress;
                emit AddressUpdate(_contractName, _contractAddress);
            }
        }

        File 7 of 8: BancorFormula
        pragma solidity ^0.4.21;
        
        /*
            Utilities & Common Modifiers
        */
        contract Utils {
            /**
                constructor
            */
            function Utils() public {
            }
        
            // verifies that an amount is greater than zero
            modifier greaterThanZero(uint256 _amount) {
                require(_amount > 0);
                _;
            }
        
            // validates an address - currently only checks that it isn't null
            modifier validAddress(address _address) {
                require(_address != address(0));
                _;
            }
        
            // verifies that the address is different than this contract address
            modifier notThis(address _address) {
                require(_address != address(this));
                _;
            }
        
            // Overflow protected math functions
        
            /**
                @dev returns the sum of _x and _y, asserts if the calculation overflows
        
                @param _x   value 1
                @param _y   value 2
        
                @return sum
            */
            function safeAdd(uint256 _x, uint256 _y) internal pure returns (uint256) {
                uint256 z = _x + _y;
                assert(z >= _x);
                return z;
            }
        
            /**
                @dev returns the difference of _x minus _y, asserts if the subtraction results in a negative number
        
                @param _x   minuend
                @param _y   subtrahend
        
                @return difference
            */
            function safeSub(uint256 _x, uint256 _y) internal pure returns (uint256) {
                assert(_x >= _y);
                return _x - _y;
            }
        
            /**
                @dev returns the product of multiplying _x by _y, asserts if the calculation overflows
        
                @param _x   factor 1
                @param _y   factor 2
        
                @return product
            */
            function safeMul(uint256 _x, uint256 _y) internal pure returns (uint256) {
                uint256 z = _x * _y;
                assert(_x == 0 || z / _x == _y);
                return z;
            }
        }
        
        /*
            Bancor Formula interface
        */
        contract IBancorFormula {
            function calculatePurchaseReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _depositAmount) public view returns (uint256);
            function calculateSaleReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _sellAmount) public view returns (uint256);
            function calculateCrossConnectorReturn(uint256 _fromConnectorBalance, uint32 _fromConnectorWeight, uint256 _toConnectorBalance, uint32 _toConnectorWeight, uint256 _amount) public view returns (uint256);
        }
        
        contract BancorFormula is IBancorFormula, Utils {
            string public version = '0.3';
        
            uint256 private constant ONE = 1;
            uint32 private constant MAX_WEIGHT = 1000000;
            uint8 private constant MIN_PRECISION = 32;
            uint8 private constant MAX_PRECISION = 127;
        
            /**
                Auto-generated via 'PrintIntScalingFactors.py'
            */
            uint256 private constant FIXED_1 = 0x080000000000000000000000000000000;
            uint256 private constant FIXED_2 = 0x100000000000000000000000000000000;
            uint256 private constant MAX_NUM = 0x200000000000000000000000000000000;
        
            /**
                Auto-generated via 'PrintLn2ScalingFactors.py'
            */
            uint256 private constant LN2_NUMERATOR   = 0x3f80fe03f80fe03f80fe03f80fe03f8;
            uint256 private constant LN2_DENOMINATOR = 0x5b9de1d10bf4103d647b0955897ba80;
        
            /**
                Auto-generated via 'PrintFunctionOptimalLog.py' and 'PrintFunctionOptimalExp.py'
            */
            uint256 private constant OPT_LOG_MAX_VAL = 0x15bf0a8b1457695355fb8ac404e7a79e3;
            uint256 private constant OPT_EXP_MAX_VAL = 0x800000000000000000000000000000000;
        
            /**
                Auto-generated via 'PrintFunctionBancorFormula.py'
            */
            uint256[128] private maxExpArray;
            function BancorFormula() public {
            //  maxExpArray[  0] = 0x6bffffffffffffffffffffffffffffffff;
            //  maxExpArray[  1] = 0x67ffffffffffffffffffffffffffffffff;
            //  maxExpArray[  2] = 0x637fffffffffffffffffffffffffffffff;
            //  maxExpArray[  3] = 0x5f6fffffffffffffffffffffffffffffff;
            //  maxExpArray[  4] = 0x5b77ffffffffffffffffffffffffffffff;
            //  maxExpArray[  5] = 0x57b3ffffffffffffffffffffffffffffff;
            //  maxExpArray[  6] = 0x5419ffffffffffffffffffffffffffffff;
            //  maxExpArray[  7] = 0x50a2ffffffffffffffffffffffffffffff;
            //  maxExpArray[  8] = 0x4d517fffffffffffffffffffffffffffff;
            //  maxExpArray[  9] = 0x4a233fffffffffffffffffffffffffffff;
            //  maxExpArray[ 10] = 0x47165fffffffffffffffffffffffffffff;
            //  maxExpArray[ 11] = 0x4429afffffffffffffffffffffffffffff;
            //  maxExpArray[ 12] = 0x415bc7ffffffffffffffffffffffffffff;
            //  maxExpArray[ 13] = 0x3eab73ffffffffffffffffffffffffffff;
            //  maxExpArray[ 14] = 0x3c1771ffffffffffffffffffffffffffff;
            //  maxExpArray[ 15] = 0x399e96ffffffffffffffffffffffffffff;
            //  maxExpArray[ 16] = 0x373fc47fffffffffffffffffffffffffff;
            //  maxExpArray[ 17] = 0x34f9e8ffffffffffffffffffffffffffff;
            //  maxExpArray[ 18] = 0x32cbfd5fffffffffffffffffffffffffff;
            //  maxExpArray[ 19] = 0x30b5057fffffffffffffffffffffffffff;
            //  maxExpArray[ 20] = 0x2eb40f9fffffffffffffffffffffffffff;
            //  maxExpArray[ 21] = 0x2cc8340fffffffffffffffffffffffffff;
            //  maxExpArray[ 22] = 0x2af09481ffffffffffffffffffffffffff;
            //  maxExpArray[ 23] = 0x292c5bddffffffffffffffffffffffffff;
            //  maxExpArray[ 24] = 0x277abdcdffffffffffffffffffffffffff;
            //  maxExpArray[ 25] = 0x25daf6657fffffffffffffffffffffffff;
            //  maxExpArray[ 26] = 0x244c49c65fffffffffffffffffffffffff;
            //  maxExpArray[ 27] = 0x22ce03cd5fffffffffffffffffffffffff;
            //  maxExpArray[ 28] = 0x215f77c047ffffffffffffffffffffffff;
            //  maxExpArray[ 29] = 0x1fffffffffffffffffffffffffffffffff;
            //  maxExpArray[ 30] = 0x1eaefdbdabffffffffffffffffffffffff;
            //  maxExpArray[ 31] = 0x1d6bd8b2ebffffffffffffffffffffffff;
                maxExpArray[ 32] = 0x1c35fedd14ffffffffffffffffffffffff;
                maxExpArray[ 33] = 0x1b0ce43b323fffffffffffffffffffffff;
                maxExpArray[ 34] = 0x19f0028ec1ffffffffffffffffffffffff;
                maxExpArray[ 35] = 0x18ded91f0e7fffffffffffffffffffffff;
                maxExpArray[ 36] = 0x17d8ec7f0417ffffffffffffffffffffff;
                maxExpArray[ 37] = 0x16ddc6556cdbffffffffffffffffffffff;
                maxExpArray[ 38] = 0x15ecf52776a1ffffffffffffffffffffff;
                maxExpArray[ 39] = 0x15060c256cb2ffffffffffffffffffffff;
                maxExpArray[ 40] = 0x1428a2f98d72ffffffffffffffffffffff;
                maxExpArray[ 41] = 0x13545598e5c23fffffffffffffffffffff;
                maxExpArray[ 42] = 0x1288c4161ce1dfffffffffffffffffffff;
                maxExpArray[ 43] = 0x11c592761c666fffffffffffffffffffff;
                maxExpArray[ 44] = 0x110a688680a757ffffffffffffffffffff;
                maxExpArray[ 45] = 0x1056f1b5bedf77ffffffffffffffffffff;
                maxExpArray[ 46] = 0x0faadceceeff8bffffffffffffffffffff;
                maxExpArray[ 47] = 0x0f05dc6b27edadffffffffffffffffffff;
                maxExpArray[ 48] = 0x0e67a5a25da4107fffffffffffffffffff;
                maxExpArray[ 49] = 0x0dcff115b14eedffffffffffffffffffff;
                maxExpArray[ 50] = 0x0d3e7a392431239fffffffffffffffffff;
                maxExpArray[ 51] = 0x0cb2ff529eb71e4fffffffffffffffffff;
                maxExpArray[ 52] = 0x0c2d415c3db974afffffffffffffffffff;
                maxExpArray[ 53] = 0x0bad03e7d883f69bffffffffffffffffff;
                maxExpArray[ 54] = 0x0b320d03b2c343d5ffffffffffffffffff;
                maxExpArray[ 55] = 0x0abc25204e02828dffffffffffffffffff;
                maxExpArray[ 56] = 0x0a4b16f74ee4bb207fffffffffffffffff;
                maxExpArray[ 57] = 0x09deaf736ac1f569ffffffffffffffffff;
                maxExpArray[ 58] = 0x0976bd9952c7aa957fffffffffffffffff;
                maxExpArray[ 59] = 0x09131271922eaa606fffffffffffffffff;
                maxExpArray[ 60] = 0x08b380f3558668c46fffffffffffffffff;
                maxExpArray[ 61] = 0x0857ddf0117efa215bffffffffffffffff;
                maxExpArray[ 62] = 0x07ffffffffffffffffffffffffffffffff;
                maxExpArray[ 63] = 0x07abbf6f6abb9d087fffffffffffffffff;
                maxExpArray[ 64] = 0x075af62cbac95f7dfa7fffffffffffffff;
                maxExpArray[ 65] = 0x070d7fb7452e187ac13fffffffffffffff;
                maxExpArray[ 66] = 0x06c3390ecc8af379295fffffffffffffff;
                maxExpArray[ 67] = 0x067c00a3b07ffc01fd6fffffffffffffff;
                maxExpArray[ 68] = 0x0637b647c39cbb9d3d27ffffffffffffff;
                maxExpArray[ 69] = 0x05f63b1fc104dbd39587ffffffffffffff;
                maxExpArray[ 70] = 0x05b771955b36e12f7235ffffffffffffff;
                maxExpArray[ 71] = 0x057b3d49dda84556d6f6ffffffffffffff;
                maxExpArray[ 72] = 0x054183095b2c8ececf30ffffffffffffff;
                maxExpArray[ 73] = 0x050a28be635ca2b888f77fffffffffffff;
                maxExpArray[ 74] = 0x04d5156639708c9db33c3fffffffffffff;
                maxExpArray[ 75] = 0x04a23105873875bd52dfdfffffffffffff;
                maxExpArray[ 76] = 0x0471649d87199aa990756fffffffffffff;
                maxExpArray[ 77] = 0x04429a21a029d4c1457cfbffffffffffff;
                maxExpArray[ 78] = 0x0415bc6d6fb7dd71af2cb3ffffffffffff;
                maxExpArray[ 79] = 0x03eab73b3bbfe282243ce1ffffffffffff;
                maxExpArray[ 80] = 0x03c1771ac9fb6b4c18e229ffffffffffff;
                maxExpArray[ 81] = 0x0399e96897690418f785257fffffffffff;
                maxExpArray[ 82] = 0x0373fc456c53bb779bf0ea9fffffffffff;
                maxExpArray[ 83] = 0x034f9e8e490c48e67e6ab8bfffffffffff;
                maxExpArray[ 84] = 0x032cbfd4a7adc790560b3337ffffffffff;
                maxExpArray[ 85] = 0x030b50570f6e5d2acca94613ffffffffff;
                maxExpArray[ 86] = 0x02eb40f9f620fda6b56c2861ffffffffff;
                maxExpArray[ 87] = 0x02cc8340ecb0d0f520a6af58ffffffffff;
                maxExpArray[ 88] = 0x02af09481380a0a35cf1ba02ffffffffff;
                maxExpArray[ 89] = 0x0292c5bdd3b92ec810287b1b3fffffffff;
                maxExpArray[ 90] = 0x0277abdcdab07d5a77ac6d6b9fffffffff;
                maxExpArray[ 91] = 0x025daf6654b1eaa55fd64df5efffffffff;
                maxExpArray[ 92] = 0x0244c49c648baa98192dce88b7ffffffff;
                maxExpArray[ 93] = 0x022ce03cd5619a311b2471268bffffffff;
                maxExpArray[ 94] = 0x0215f77c045fbe885654a44a0fffffffff;
                maxExpArray[ 95] = 0x01ffffffffffffffffffffffffffffffff;
                maxExpArray[ 96] = 0x01eaefdbdaaee7421fc4d3ede5ffffffff;
                maxExpArray[ 97] = 0x01d6bd8b2eb257df7e8ca57b09bfffffff;
                maxExpArray[ 98] = 0x01c35fedd14b861eb0443f7f133fffffff;
                maxExpArray[ 99] = 0x01b0ce43b322bcde4a56e8ada5afffffff;
                maxExpArray[100] = 0x019f0028ec1fff007f5a195a39dfffffff;
                maxExpArray[101] = 0x018ded91f0e72ee74f49b15ba527ffffff;
                maxExpArray[102] = 0x017d8ec7f04136f4e5615fd41a63ffffff;
                maxExpArray[103] = 0x016ddc6556cdb84bdc8d12d22e6fffffff;
                maxExpArray[104] = 0x015ecf52776a1155b5bd8395814f7fffff;
                maxExpArray[105] = 0x015060c256cb23b3b3cc3754cf40ffffff;
                maxExpArray[106] = 0x01428a2f98d728ae223ddab715be3fffff;
                maxExpArray[107] = 0x013545598e5c23276ccf0ede68034fffff;
                maxExpArray[108] = 0x01288c4161ce1d6f54b7f61081194fffff;
                maxExpArray[109] = 0x011c592761c666aa641d5a01a40f17ffff;
                maxExpArray[110] = 0x0110a688680a7530515f3e6e6cfdcdffff;
                maxExpArray[111] = 0x01056f1b5bedf75c6bcb2ce8aed428ffff;
                maxExpArray[112] = 0x00faadceceeff8a0890f3875f008277fff;
                maxExpArray[113] = 0x00f05dc6b27edad306388a600f6ba0bfff;
                maxExpArray[114] = 0x00e67a5a25da41063de1495d5b18cdbfff;
                maxExpArray[115] = 0x00dcff115b14eedde6fc3aa5353f2e4fff;
                maxExpArray[116] = 0x00d3e7a3924312399f9aae2e0f868f8fff;
                maxExpArray[117] = 0x00cb2ff529eb71e41582cccd5a1ee26fff;
                maxExpArray[118] = 0x00c2d415c3db974ab32a51840c0b67edff;
                maxExpArray[119] = 0x00bad03e7d883f69ad5b0a186184e06bff;
                maxExpArray[120] = 0x00b320d03b2c343d4829abd6075f0cc5ff;
                maxExpArray[121] = 0x00abc25204e02828d73c6e80bcdb1a95bf;
                maxExpArray[122] = 0x00a4b16f74ee4bb2040a1ec6c15fbbf2df;
                maxExpArray[123] = 0x009deaf736ac1f569deb1b5ae3f36c130f;
                maxExpArray[124] = 0x00976bd9952c7aa957f5937d790ef65037;
                maxExpArray[125] = 0x009131271922eaa6064b73a22d0bd4f2bf;
                maxExpArray[126] = 0x008b380f3558668c46c91c49a2f8e967b9;
                maxExpArray[127] = 0x00857ddf0117efa215952912839f6473e6;
            }
        
            /**
                @dev given a token supply, connector balance, weight and a deposit amount (in the connector token),
                calculates the return for a given conversion (in the main token)
        
                Formula:
                Return = _supply * ((1 + _depositAmount / _connectorBalance) ^ (_connectorWeight / 1000000) - 1)
        
                @param _supply              token total supply
                @param _connectorBalance    total connector balance
                @param _connectorWeight     connector weight, represented in ppm, 1-1000000
                @param _depositAmount       deposit amount, in connector token
        
                @return purchase return amount
            */
            function calculatePurchaseReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _depositAmount) public view returns (uint256) {
                // validate input
                require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT);
        
                // special case for 0 deposit amount
                if (_depositAmount == 0)
                    return 0;
        
                // special case if the weight = 100%
                if (_connectorWeight == MAX_WEIGHT)
                    return safeMul(_supply, _depositAmount) / _connectorBalance;
        
                uint256 result;
                uint8 precision;
                uint256 baseN = safeAdd(_depositAmount, _connectorBalance);
                (result, precision) = power(baseN, _connectorBalance, _connectorWeight, MAX_WEIGHT);
                uint256 temp = safeMul(_supply, result) >> precision;
                return temp - _supply;
            }
        
            /**
                @dev given a token supply, connector balance, weight and a sell amount (in the main token),
                calculates the return for a given conversion (in the connector token)
        
                Formula:
                Return = _connectorBalance * (1 - (1 - _sellAmount / _supply) ^ (1 / (_connectorWeight / 1000000)))
        
                @param _supply              token total supply
                @param _connectorBalance    total connector
                @param _connectorWeight     constant connector Weight, represented in ppm, 1-1000000
                @param _sellAmount          sell amount, in the token itself
        
                @return sale return amount
            */
            function calculateSaleReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _sellAmount) public view returns (uint256) {
                // validate input
                require(_supply > 0 && _connectorBalance > 0 && _connectorWeight > 0 && _connectorWeight <= MAX_WEIGHT && _sellAmount <= _supply);
        
                // special case for 0 sell amount
                if (_sellAmount == 0)
                    return 0;
        
                // special case for selling the entire supply
                if (_sellAmount == _supply)
                    return _connectorBalance;
        
                // special case if the weight = 100%
                if (_connectorWeight == MAX_WEIGHT)
                    return safeMul(_connectorBalance, _sellAmount) / _supply;
        
                uint256 result;
                uint8 precision;
                uint256 baseD = _supply - _sellAmount;
                (result, precision) = power(_supply, baseD, MAX_WEIGHT, _connectorWeight);
                uint256 temp1 = safeMul(_connectorBalance, result);
                uint256 temp2 = _connectorBalance << precision;
                return (temp1 - temp2) / result;
            }
        
            /**
                @dev given two connector balances/weights and a sell amount (in the first connector token),
                calculates the return for a conversion from the first connector token to the second connector token (in the second connector token)
        
                Formula:
                Return = _toConnectorBalance * (1 - (_fromConnectorBalance / (_fromConnectorBalance + _amount)) ^ (_fromConnectorWeight / _toConnectorWeight))
        
                @param _fromConnectorBalance    input connector balance
                @param _fromConnectorWeight     input connector weight, represented in ppm, 1-1000000
                @param _toConnectorBalance      output connector balance
                @param _toConnectorWeight       output connector weight, represented in ppm, 1-1000000
                @param _amount                  input connector amount
        
                @return second connector amount
            */
            function calculateCrossConnectorReturn(uint256 _fromConnectorBalance, uint32 _fromConnectorWeight, uint256 _toConnectorBalance, uint32 _toConnectorWeight, uint256 _amount) public view returns (uint256) {
                // validate input
                require(_fromConnectorBalance > 0 && _fromConnectorWeight > 0 && _fromConnectorWeight <= MAX_WEIGHT && _toConnectorBalance > 0 && _toConnectorWeight > 0 && _toConnectorWeight <= MAX_WEIGHT);
        
                // special case for equal weights
                if (_fromConnectorWeight == _toConnectorWeight)
                    return safeMul(_toConnectorBalance, _amount) / safeAdd(_fromConnectorBalance, _amount);
        
                uint256 result;
                uint8 precision;
                uint256 baseN = safeAdd(_fromConnectorBalance, _amount);
                (result, precision) = power(baseN, _fromConnectorBalance, _fromConnectorWeight, _toConnectorWeight);
                uint256 temp1 = safeMul(_toConnectorBalance, result);
                uint256 temp2 = _toConnectorBalance << precision;
                return (temp1 - temp2) / result;
            }
        
            /**
                General Description:
                    Determine a value of precision.
                    Calculate an integer approximation of (_baseN / _baseD) ^ (_expN / _expD) * 2 ^ precision.
                    Return the result along with the precision used.
        
                Detailed Description:
                    Instead of calculating "base ^ exp", we calculate "e ^ (log(base) * exp)".
                    The value of "log(base)" is represented with an integer slightly smaller than "log(base) * 2 ^ precision".
                    The larger "precision" is, the more accurately this value represents the real value.
                    However, the larger "precision" is, the more bits are required in order to store this value.
                    And the exponentiation function, which takes "x" and calculates "e ^ x", is limited to a maximum exponent (maximum value of "x").
                    This maximum exponent depends on the "precision" used, and it is given by "maxExpArray[precision] >> (MAX_PRECISION - precision)".
                    Hence we need to determine the highest precision which can be used for the given input, before calling the exponentiation function.
                    This allows us to compute "base ^ exp" with maximum accuracy and without exceeding 256 bits in any of the intermediate computations.
                    This functions assumes that "_expN < 2 ^ 256 / log(MAX_NUM - 1)", otherwise the multiplication should be replaced with a "safeMul".
            */
            function power(uint256 _baseN, uint256 _baseD, uint32 _expN, uint32 _expD) internal view returns (uint256, uint8) {
                assert(_baseN < MAX_NUM);
        
                uint256 baseLog;
                uint256 base = _baseN * FIXED_1 / _baseD;
                if (base < OPT_LOG_MAX_VAL) {
                    baseLog = optimalLog(base);
                }
                else {
                    baseLog = generalLog(base);
                }
        
                uint256 baseLogTimesExp = baseLog * _expN / _expD;
                if (baseLogTimesExp < OPT_EXP_MAX_VAL) {
                    return (optimalExp(baseLogTimesExp), MAX_PRECISION);
                }
                else {
                    uint8 precision = findPositionInMaxExpArray(baseLogTimesExp);
                    return (generalExp(baseLogTimesExp >> (MAX_PRECISION - precision), precision), precision);
                }
            }
        
            /**
                Compute log(x / FIXED_1) * FIXED_1.
                This functions assumes that "x >= FIXED_1", because the output would be negative otherwise.
            */
            function generalLog(uint256 x) internal pure returns (uint256) {
                uint256 res = 0;
        
                // If x >= 2, then we compute the integer part of log2(x), which is larger than 0.
                if (x >= FIXED_2) {
                    uint8 count = floorLog2(x / FIXED_1);
                    x >>= count; // now x < 2
                    res = count * FIXED_1;
                }
        
                // If x > 1, then we compute the fraction part of log2(x), which is larger than 0.
                if (x > FIXED_1) {
                    for (uint8 i = MAX_PRECISION; i > 0; --i) {
                        x = (x * x) / FIXED_1; // now 1 < x < 4
                        if (x >= FIXED_2) {
                            x >>= 1; // now 1 < x < 2
                            res += ONE << (i - 1);
                        }
                    }
                }
        
                return res * LN2_NUMERATOR / LN2_DENOMINATOR;
            }
        
            /**
                Compute the largest integer smaller than or equal to the binary logarithm of the input.
            */
            function floorLog2(uint256 _n) internal pure returns (uint8) {
                uint8 res = 0;
        
                if (_n < 256) {
                    // At most 8 iterations
                    while (_n > 1) {
                        _n >>= 1;
                        res += 1;
                    }
                }
                else {
                    // Exactly 8 iterations
                    for (uint8 s = 128; s > 0; s >>= 1) {
                        if (_n >= (ONE << s)) {
                            _n >>= s;
                            res |= s;
                        }
                    }
                }
        
                return res;
            }
        
            /**
                The global "maxExpArray" is sorted in descending order, and therefore the following statements are equivalent:
                - This function finds the position of [the smallest value in "maxExpArray" larger than or equal to "x"]
                - This function finds the highest position of [a value in "maxExpArray" larger than or equal to "x"]
            */
            function findPositionInMaxExpArray(uint256 _x) internal view returns (uint8) {
                uint8 lo = MIN_PRECISION;
                uint8 hi = MAX_PRECISION;
        
                while (lo + 1 < hi) {
                    uint8 mid = (lo + hi) / 2;
                    if (maxExpArray[mid] >= _x)
                        lo = mid;
                    else
                        hi = mid;
                }
        
                if (maxExpArray[hi] >= _x)
                    return hi;
                if (maxExpArray[lo] >= _x)
                    return lo;
        
                assert(false);
                return 0;
            }
        
            /**
                This function can be auto-generated by the script 'PrintFunctionGeneralExp.py'.
                It approximates "e ^ x" via maclaurin summation: "(x^0)/0! + (x^1)/1! + ... + (x^n)/n!".
                It returns "e ^ (x / 2 ^ precision) * 2 ^ precision", that is, the result is upshifted for accuracy.
                The global "maxExpArray" maps each "precision" to "((maximumExponent + 1) << (MAX_PRECISION - precision)) - 1".
                The maximum permitted value for "x" is therefore given by "maxExpArray[precision] >> (MAX_PRECISION - precision)".
            */
            function generalExp(uint256 _x, uint8 _precision) internal pure returns (uint256) {
                uint256 xi = _x;
                uint256 res = 0;
        
                xi = (xi * _x) >> _precision; res += xi * 0x3442c4e6074a82f1797f72ac0000000; // add x^02 * (33! / 02!)
                xi = (xi * _x) >> _precision; res += xi * 0x116b96f757c380fb287fd0e40000000; // add x^03 * (33! / 03!)
                xi = (xi * _x) >> _precision; res += xi * 0x045ae5bdd5f0e03eca1ff4390000000; // add x^04 * (33! / 04!)
                xi = (xi * _x) >> _precision; res += xi * 0x00defabf91302cd95b9ffda50000000; // add x^05 * (33! / 05!)
                xi = (xi * _x) >> _precision; res += xi * 0x002529ca9832b22439efff9b8000000; // add x^06 * (33! / 06!)
                xi = (xi * _x) >> _precision; res += xi * 0x00054f1cf12bd04e516b6da88000000; // add x^07 * (33! / 07!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000a9e39e257a09ca2d6db51000000; // add x^08 * (33! / 08!)
                xi = (xi * _x) >> _precision; res += xi * 0x000012e066e7b839fa050c309000000; // add x^09 * (33! / 09!)
                xi = (xi * _x) >> _precision; res += xi * 0x000001e33d7d926c329a1ad1a800000; // add x^10 * (33! / 10!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000002bee513bdb4a6b19b5f800000; // add x^11 * (33! / 11!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000003a9316fa79b88eccf2a00000; // add x^12 * (33! / 12!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000048177ebe1fa812375200000; // add x^13 * (33! / 13!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000005263fe90242dcbacf00000; // add x^14 * (33! / 14!)
                xi = (xi * _x) >> _precision; res += xi * 0x000000000057e22099c030d94100000; // add x^15 * (33! / 15!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000057e22099c030d9410000; // add x^16 * (33! / 16!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000052b6b54569976310000; // add x^17 * (33! / 17!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000004985f67696bf748000; // add x^18 * (33! / 18!)
                xi = (xi * _x) >> _precision; res += xi * 0x000000000000003dea12ea99e498000; // add x^19 * (33! / 19!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000000031880f2214b6e000; // add x^20 * (33! / 20!)
                xi = (xi * _x) >> _precision; res += xi * 0x000000000000000025bcff56eb36000; // add x^21 * (33! / 21!)
                xi = (xi * _x) >> _precision; res += xi * 0x000000000000000001b722e10ab1000; // add x^22 * (33! / 22!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000001317c70077000; // add x^23 * (33! / 23!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000cba84aafa00; // add x^24 * (33! / 24!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000082573a0a00; // add x^25 * (33! / 25!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000005035ad900; // add x^26 * (33! / 26!)
                xi = (xi * _x) >> _precision; res += xi * 0x000000000000000000000002f881b00; // add x^27 * (33! / 27!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000000000001b29340; // add x^28 * (33! / 28!)
                xi = (xi * _x) >> _precision; res += xi * 0x00000000000000000000000000efc40; // add x^29 * (33! / 29!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000000000000007fe0; // add x^30 * (33! / 30!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000000000000000420; // add x^31 * (33! / 31!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000000000000000021; // add x^32 * (33! / 32!)
                xi = (xi * _x) >> _precision; res += xi * 0x0000000000000000000000000000001; // add x^33 * (33! / 33!)
        
                return res / 0x688589cc0e9505e2f2fee5580000000 + _x + (ONE << _precision); // divide by 33! and then add x^1 / 1! + x^0 / 0!
            }
        
            /**
                Return log(x / FIXED_1) * FIXED_1
                Input range: FIXED_1 <= x <= LOG_EXP_MAX_VAL - 1
                Auto-generated via 'PrintFunctionOptimalLog.py'
            */
            function optimalLog(uint256 x) internal pure returns (uint256) {
                uint256 res = 0;
        
                uint256 y;
                uint256 z;
                uint256 w;
        
                if (x >= 0xd3094c70f034de4b96ff7d5b6f99fcd8) {res += 0x40000000000000000000000000000000; x = x * FIXED_1 / 0xd3094c70f034de4b96ff7d5b6f99fcd8;}
                if (x >= 0xa45af1e1f40c333b3de1db4dd55f29a7) {res += 0x20000000000000000000000000000000; x = x * FIXED_1 / 0xa45af1e1f40c333b3de1db4dd55f29a7;}
                if (x >= 0x910b022db7ae67ce76b441c27035c6a1) {res += 0x10000000000000000000000000000000; x = x * FIXED_1 / 0x910b022db7ae67ce76b441c27035c6a1;}
                if (x >= 0x88415abbe9a76bead8d00cf112e4d4a8) {res += 0x08000000000000000000000000000000; x = x * FIXED_1 / 0x88415abbe9a76bead8d00cf112e4d4a8;}
                if (x >= 0x84102b00893f64c705e841d5d4064bd3) {res += 0x04000000000000000000000000000000; x = x * FIXED_1 / 0x84102b00893f64c705e841d5d4064bd3;}
                if (x >= 0x8204055aaef1c8bd5c3259f4822735a2) {res += 0x02000000000000000000000000000000; x = x * FIXED_1 / 0x8204055aaef1c8bd5c3259f4822735a2;}
                if (x >= 0x810100ab00222d861931c15e39b44e99) {res += 0x01000000000000000000000000000000; x = x * FIXED_1 / 0x810100ab00222d861931c15e39b44e99;}
                if (x >= 0x808040155aabbbe9451521693554f733) {res += 0x00800000000000000000000000000000; x = x * FIXED_1 / 0x808040155aabbbe9451521693554f733;}
        
                z = y = x - FIXED_1;
                w = y * y / FIXED_1;
                res += z * (0x100000000000000000000000000000000 - y) / 0x100000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - y) / 0x200000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x099999999999999999999999999999999 - y) / 0x300000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x092492492492492492492492492492492 - y) / 0x400000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x08e38e38e38e38e38e38e38e38e38e38e - y) / 0x500000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x08ba2e8ba2e8ba2e8ba2e8ba2e8ba2e8b - y) / 0x600000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x089d89d89d89d89d89d89d89d89d89d89 - y) / 0x700000000000000000000000000000000; z = z * w / FIXED_1;
                res += z * (0x088888888888888888888888888888888 - y) / 0x800000000000000000000000000000000;
        
                return res;
            }
        
            /**
                Return e ^ (x / FIXED_1) * FIXED_1
                Input range: 0 <= x <= OPT_EXP_MAX_VAL - 1
                Auto-generated via 'PrintFunctionOptimalExp.py'
            */
            function optimalExp(uint256 x) internal pure returns (uint256) {
                uint256 res = 0;
        
                uint256 y;
                uint256 z;
        
                z = y = x % 0x10000000000000000000000000000000;
                z = z * y / FIXED_1; res += z * 0x10e1b3be415a0000; // add y^02 * (20! / 02!)
                z = z * y / FIXED_1; res += z * 0x05a0913f6b1e0000; // add y^03 * (20! / 03!)
                z = z * y / FIXED_1; res += z * 0x0168244fdac78000; // add y^04 * (20! / 04!)
                z = z * y / FIXED_1; res += z * 0x004807432bc18000; // add y^05 * (20! / 05!)
                z = z * y / FIXED_1; res += z * 0x000c0135dca04000; // add y^06 * (20! / 06!)
                z = z * y / FIXED_1; res += z * 0x0001b707b1cdc000; // add y^07 * (20! / 07!)
                z = z * y / FIXED_1; res += z * 0x000036e0f639b800; // add y^08 * (20! / 08!)
                z = z * y / FIXED_1; res += z * 0x00000618fee9f800; // add y^09 * (20! / 09!)
                z = z * y / FIXED_1; res += z * 0x0000009c197dcc00; // add y^10 * (20! / 10!)
                z = z * y / FIXED_1; res += z * 0x0000000e30dce400; // add y^11 * (20! / 11!)
                z = z * y / FIXED_1; res += z * 0x000000012ebd1300; // add y^12 * (20! / 12!)
                z = z * y / FIXED_1; res += z * 0x0000000017499f00; // add y^13 * (20! / 13!)
                z = z * y / FIXED_1; res += z * 0x0000000001a9d480; // add y^14 * (20! / 14!)
                z = z * y / FIXED_1; res += z * 0x00000000001c6380; // add y^15 * (20! / 15!)
                z = z * y / FIXED_1; res += z * 0x000000000001c638; // add y^16 * (20! / 16!)
                z = z * y / FIXED_1; res += z * 0x0000000000001ab8; // add y^17 * (20! / 17!)
                z = z * y / FIXED_1; res += z * 0x000000000000017c; // add y^18 * (20! / 18!)
                z = z * y / FIXED_1; res += z * 0x0000000000000014; // add y^19 * (20! / 19!)
                z = z * y / FIXED_1; res += z * 0x0000000000000001; // add y^20 * (20! / 20!)
                res = res / 0x21c3677c82b40000 + y + FIXED_1; // divide by 20! and then add y^1 / 1! + y^0 / 0!
        
                if ((x & 0x010000000000000000000000000000000) != 0) res = res * 0x1c3d6a24ed82218787d624d3e5eba95f9 / 0x18ebef9eac820ae8682b9793ac6d1e776;
                if ((x & 0x020000000000000000000000000000000) != 0) res = res * 0x18ebef9eac820ae8682b9793ac6d1e778 / 0x1368b2fc6f9609fe7aceb46aa619baed4;
                if ((x & 0x040000000000000000000000000000000) != 0) res = res * 0x1368b2fc6f9609fe7aceb46aa619baed5 / 0x0bc5ab1b16779be3575bd8f0520a9f21f;
                if ((x & 0x080000000000000000000000000000000) != 0) res = res * 0x0bc5ab1b16779be3575bd8f0520a9f21e / 0x0454aaa8efe072e7f6ddbab84b40a55c9;
                if ((x & 0x100000000000000000000000000000000) != 0) res = res * 0x0454aaa8efe072e7f6ddbab84b40a55c5 / 0x00960aadc109e7a3bf4578099615711ea;
                if ((x & 0x200000000000000000000000000000000) != 0) res = res * 0x00960aadc109e7a3bf4578099615711d7 / 0x0002bf84208204f5977f9a8cf01fdce3d;
                if ((x & 0x400000000000000000000000000000000) != 0) res = res * 0x0002bf84208204f5977f9a8cf01fdc307 / 0x0000003c6ab775dd0b95b4cbee7e65d11;
        
                return res;
            }
        }

        File 8 of 8: BancorConverter
        pragma solidity ^0.4.21;
        
        /*
            Owned contract interface
        */
        contract IOwned {
            // this function isn't abstract since the compiler emits automatically generated getter functions as external
            function owner() public view returns (address) {}
        
            function transferOwnership(address _newOwner) public;
            function acceptOwnership() public;
        }
        
        /*
            ERC20 Standard Token interface
        */
        contract IERC20Token {
            // these functions aren't abstract since the compiler emits automatically generated getter functions as external
            function name() public view returns (string) {}
            function symbol() public view returns (string) {}
            function decimals() public view returns (uint8) {}
            function totalSupply() public view returns (uint256) {}
            function balanceOf(address _owner) public view returns (uint256) { _owner; }
            function allowance(address _owner, address _spender) public view returns (uint256) { _owner; _spender; }
        
            function transfer(address _to, uint256 _value) public returns (bool success);
            function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
            function approve(address _spender, uint256 _value) public returns (bool success);
        }
        
        /*
            Smart Token interface
        */
        contract ISmartToken is IOwned, IERC20Token {
            function disableTransfers(bool _disable) public;
            function issue(address _to, uint256 _amount) public;
            function destroy(address _from, uint256 _amount) public;
        }
        
        /*
            Contract Registry interface
        */
        contract IContractRegistry {
            function getAddress(bytes32 _contractName) public view returns (address);
        }
        
        /*
            Contract Features interface
        */
        contract IContractFeatures {
            function isSupported(address _contract, uint256 _features) public view returns (bool);
            function enableFeatures(uint256 _features, bool _enable) public;
        }
        
        /*
            Whitelist interface
        */
        contract IWhitelist {
            function isWhitelisted(address _address) public view returns (bool);
        }
        
        /*
            Token Holder interface
        */
        contract ITokenHolder is IOwned {
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount) public;
        }
        
        /*
            Bancor Formula interface
        */
        contract IBancorFormula {
            function calculatePurchaseReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _depositAmount) public view returns (uint256);
            function calculateSaleReturn(uint256 _supply, uint256 _connectorBalance, uint32 _connectorWeight, uint256 _sellAmount) public view returns (uint256);
            function calculateCrossConnectorReturn(uint256 _fromConnectorBalance, uint32 _fromConnectorWeight, uint256 _toConnectorBalance, uint32 _toConnectorWeight, uint256 _amount) public view returns (uint256);
        }
        
        /*
            Bancor Converter interface
        */
        contract IBancorConverter {
            function getReturn(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount) public view returns (uint256);
            function convert(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256);
            function conversionWhitelist() public view returns (IWhitelist) {}
            // deprecated, backward compatibility
            function change(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256);
        }
        
        /*
            Bancor Network interface
        */
        contract IBancorNetwork {
            function convert(IERC20Token[] _path, uint256 _amount, uint256 _minReturn) public payable returns (uint256);
            function convertFor(IERC20Token[] _path, uint256 _amount, uint256 _minReturn, address _for) public payable returns (uint256);
            function convertForPrioritized2(
                IERC20Token[] _path,
                uint256 _amount,
                uint256 _minReturn,
                address _for,
                uint256 _block,
                uint8 _v,
                bytes32 _r,
                bytes32 _s)
                public payable returns (uint256);
        
            // deprecated, backward compatibility
            function convertForPrioritized(
                IERC20Token[] _path,
                uint256 _amount,
                uint256 _minReturn,
                address _for,
                uint256 _block,
                uint256 _nonce,
                uint8 _v,
                bytes32 _r,
                bytes32 _s)
                public payable returns (uint256);
        }
        
        /*
            Utilities & Common Modifiers
        */
        contract Utils {
            /**
                constructor
            */
            function Utils() public {
            }
        
            // verifies that an amount is greater than zero
            modifier greaterThanZero(uint256 _amount) {
                require(_amount > 0);
                _;
            }
        
            // validates an address - currently only checks that it isn't null
            modifier validAddress(address _address) {
                require(_address != address(0));
                _;
            }
        
            // verifies that the address is different than this contract address
            modifier notThis(address _address) {
                require(_address != address(this));
                _;
            }
        
            // Overflow protected math functions
        
            /**
                @dev returns the sum of _x and _y, asserts if the calculation overflows
        
                @param _x   value 1
                @param _y   value 2
        
                @return sum
            */
            function safeAdd(uint256 _x, uint256 _y) internal pure returns (uint256) {
                uint256 z = _x + _y;
                assert(z >= _x);
                return z;
            }
        
            /**
                @dev returns the difference of _x minus _y, asserts if the subtraction results in a negative number
        
                @param _x   minuend
                @param _y   subtrahend
        
                @return difference
            */
            function safeSub(uint256 _x, uint256 _y) internal pure returns (uint256) {
                assert(_x >= _y);
                return _x - _y;
            }
        
            /**
                @dev returns the product of multiplying _x by _y, asserts if the calculation overflows
        
                @param _x   factor 1
                @param _y   factor 2
        
                @return product
            */
            function safeMul(uint256 _x, uint256 _y) internal pure returns (uint256) {
                uint256 z = _x * _y;
                assert(_x == 0 || z / _x == _y);
                return z;
            }
        }
        
        /*
            Provides support and utilities for contract ownership
        */
        contract Owned is IOwned {
            address public owner;
            address public newOwner;
        
            event OwnerUpdate(address indexed _prevOwner, address indexed _newOwner);
        
            /**
                @dev constructor
            */
            function Owned() public {
                owner = msg.sender;
            }
        
            // allows execution by the owner only
            modifier ownerOnly {
                assert(msg.sender == owner);
                _;
            }
        
            /**
                @dev allows transferring the contract ownership
                the new owner still needs to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new contract owner
            */
            function transferOwnership(address _newOwner) public ownerOnly {
                require(_newOwner != owner);
                newOwner = _newOwner;
            }
        
            /**
                @dev used by a new owner to accept an ownership transfer
            */
            function acceptOwnership() public {
                require(msg.sender == newOwner);
                emit OwnerUpdate(owner, newOwner);
                owner = newOwner;
                newOwner = address(0);
            }
        }
        
        /*
            Provides support and utilities for contract management
            Note that a managed contract must also have an owner
        */
        contract Managed is Owned {
            address public manager;
            address public newManager;
        
            event ManagerUpdate(address indexed _prevManager, address indexed _newManager);
        
            /**
                @dev constructor
            */
            function Managed() public {
                manager = msg.sender;
            }
        
            // allows execution by the manager only
            modifier managerOnly {
                assert(msg.sender == manager);
                _;
            }
        
            // allows execution by either the owner or the manager only
            modifier ownerOrManagerOnly {
                require(msg.sender == owner || msg.sender == manager);
                _;
            }
        
            /**
                @dev allows transferring the contract management
                the new manager still needs to accept the transfer
                can only be called by the contract manager
        
                @param _newManager    new contract manager
            */
            function transferManagement(address _newManager) public ownerOrManagerOnly {
                require(_newManager != manager);
                newManager = _newManager;
            }
        
            /**
                @dev used by a new manager to accept a management transfer
            */
            function acceptManagement() public {
                require(msg.sender == newManager);
                emit ManagerUpdate(manager, newManager);
                manager = newManager;
                newManager = address(0);
            }
        }
        
        /**
            Id definitions for bancor contracts
        
            Can be used in conjunction with the contract registry to get contract addresses
        */
        contract ContractIds {
            // generic
            bytes32 public constant CONTRACT_FEATURES = "ContractFeatures";
        
            // bancor logic
            bytes32 public constant BANCOR_NETWORK = "BancorNetwork";
            bytes32 public constant BANCOR_FORMULA = "BancorFormula";
            bytes32 public constant BANCOR_GAS_PRICE_LIMIT = "BancorGasPriceLimit";
        
            bytes32 public constant BANCOR_CONVERTER_FACTORY = "BancorConverterFactory";
            bytes32 public constant BANCOR_CONVERTER_UPGRADER = "BancorConverterUpgrader";
        
            // tokens
            bytes32 public constant BNT_TOKEN = "BNTToken";
        }
        
        /**
            Id definitions for bancor contract features
        
            Can be used to query the ContractFeatures contract to check whether a certain feature is supported by a contract
        */
        contract FeatureIds {
            // converter features
            uint256 public constant CONVERTER_CONVERSION_WHITELIST = 1 << 0;
        }
        
        /*
            We consider every contract to be a 'token holder' since it's currently not possible
            for a contract to deny receiving tokens.
        
            The TokenHolder's contract sole purpose is to provide a safety mechanism that allows
            the owner to send tokens that were sent to the contract by mistake back to their sender.
        */
        contract TokenHolder is ITokenHolder, Owned, Utils {
            /**
                @dev constructor
            */
            function TokenHolder() public {
            }
        
            /**
                @dev withdraws tokens held by the contract and sends them to an account
                can only be called by the owner
        
                @param _token   ERC20 token contract address
                @param _to      account to receive the new amount
                @param _amount  amount to withdraw
            */
            function withdrawTokens(IERC20Token _token, address _to, uint256 _amount)
                public
                ownerOnly
                validAddress(_token)
                validAddress(_to)
                notThis(_to)
            {
                assert(_token.transfer(_to, _amount));
            }
        }
        
        /*
            The smart token controller is an upgradable part of the smart token that allows
            more functionality as well as fixes for bugs/exploits.
            Once it accepts ownership of the token, it becomes the token's sole controller
            that can execute any of its functions.
        
            To upgrade the controller, ownership must be transferred to a new controller, along with
            any relevant data.
        
            The smart token must be set on construction and cannot be changed afterwards.
            Wrappers are provided (as opposed to a single 'execute' function) for each of the token's functions, for easier access.
        
            Note that the controller can transfer token ownership to a new controller that
            doesn't allow executing any function on the token, for a trustless solution.
            Doing that will also remove the owner's ability to upgrade the controller.
        */
        contract SmartTokenController is TokenHolder {
            ISmartToken public token;   // smart token
        
            /**
                @dev constructor
            */
            function SmartTokenController(ISmartToken _token)
                public
                validAddress(_token)
            {
                token = _token;
            }
        
            // ensures that the controller is the token's owner
            modifier active() {
                assert(token.owner() == address(this));
                _;
            }
        
            // ensures that the controller is not the token's owner
            modifier inactive() {
                assert(token.owner() != address(this));
                _;
            }
        
            /**
                @dev allows transferring the token ownership
                the new owner still need to accept the transfer
                can only be called by the contract owner
        
                @param _newOwner    new token owner
            */
            function transferTokenOwnership(address _newOwner) public ownerOnly {
                token.transferOwnership(_newOwner);
            }
        
            /**
                @dev used by a new owner to accept a token ownership transfer
                can only be called by the contract owner
            */
            function acceptTokenOwnership() public ownerOnly {
                token.acceptOwnership();
            }
        
            /**
                @dev disables/enables token transfers
                can only be called by the contract owner
        
                @param _disable    true to disable transfers, false to enable them
            */
            function disableTokenTransfers(bool _disable) public ownerOnly {
                token.disableTransfers(_disable);
            }
        
            /**
                @dev withdraws tokens held by the controller and sends them to an account
                can only be called by the owner
        
                @param _token   ERC20 token contract address
                @param _to      account to receive the new amount
                @param _amount  amount to withdraw
            */
            function withdrawFromToken(
                IERC20Token _token, 
                address _to, 
                uint256 _amount
            ) 
                public
                ownerOnly
            {
                ITokenHolder(token).withdrawTokens(_token, _to, _amount);
            }
        }
        
        /*
            Bancor Converter v0.9
        
            The Bancor version of the token converter, allows conversion between a smart token and other ERC20 tokens and between different ERC20 tokens and themselves.
        
            ERC20 connector balance can be virtual, meaning that the calculations are based on the virtual balance instead of relying on
            the actual connector balance. This is a security mechanism that prevents the need to keep a very large (and valuable) balance in a single contract.
        
            The converter is upgradable (just like any SmartTokenController).
        
            WARNING: It is NOT RECOMMENDED to use the converter with Smart Tokens that have less than 8 decimal digits
                     or with very small numbers because of precision loss
        
            Open issues:
            - Front-running attacks are currently mitigated by the following mechanisms:
                - minimum return argument for each conversion provides a way to define a minimum/maximum price for the transaction
                - gas price limit prevents users from having control over the order of execution
                - gas price limit check can be skipped if the transaction comes from a trusted, whitelisted signer
              Other potential solutions might include a commit/reveal based schemes
            - Possibly add getters for the connector fields so that the client won't need to rely on the order in the struct
        */
        contract BancorConverter is IBancorConverter, SmartTokenController, Managed, ContractIds, FeatureIds {
            uint32 private constant MAX_WEIGHT = 1000000;
            uint64 private constant MAX_CONVERSION_FEE = 1000000;
        
            struct Connector {
                uint256 virtualBalance;         // connector virtual balance
                uint32 weight;                  // connector weight, represented in ppm, 1-1000000
                bool isVirtualBalanceEnabled;   // true if virtual balance is enabled, false if not
                bool isPurchaseEnabled;         // is purchase of the smart token enabled with the connector, can be set by the owner
                bool isSet;                     // used to tell if the mapping element is defined
            }
        
            string public version = '0.9';
            string public converterType = 'bancor';
        
            IContractRegistry public registry;                  // contract registry contract
            IWhitelist public conversionWhitelist;              // whitelist contract with list of addresses that are allowed to use the converter
            IERC20Token[] public connectorTokens;               // ERC20 standard token addresses
            IERC20Token[] public quickBuyPath;                  // conversion path that's used in order to buy the token with ETH
            mapping (address => Connector) public connectors;   // connector token addresses -> connector data
            uint32 private totalConnectorWeight = 0;            // used to efficiently prevent increasing the total connector weight above 100%
            uint32 public maxConversionFee = 0;                 // maximum conversion fee for the lifetime of the contract,
                                                                // represented in ppm, 0...1000000 (0 = no fee, 100 = 0.01%, 1000000 = 100%)
            uint32 public conversionFee = 0;                    // current conversion fee, represented in ppm, 0...maxConversionFee
            bool public conversionsEnabled = true;              // true if token conversions is enabled, false if not
            IERC20Token[] private convertPath;
        
            // triggered when a conversion between two tokens occurs
            event Conversion(
                address indexed _fromToken,
                address indexed _toToken,
                address indexed _trader,
                uint256 _amount,
                uint256 _return,
                int256 _conversionFee
            );
            // triggered after a conversion with new price data
            event PriceDataUpdate(
                address indexed _connectorToken,
                uint256 _tokenSupply,
                uint256 _connectorBalance,
                uint32 _connectorWeight
            );
            // triggered when the conversion fee is updated
            event ConversionFeeUpdate(uint32 _prevFee, uint32 _newFee);
        
            /**
                @dev constructor
        
                @param  _token              smart token governed by the converter
                @param  _registry           address of a contract registry contract
                @param  _maxConversionFee   maximum conversion fee, represented in ppm
                @param  _connectorToken     optional, initial connector, allows defining the first connector at deployment time
                @param  _connectorWeight    optional, weight for the initial connector
            */
            function BancorConverter(
                ISmartToken _token,
                IContractRegistry _registry,
                uint32 _maxConversionFee,
                IERC20Token _connectorToken,
                uint32 _connectorWeight
            )
                public
                SmartTokenController(_token)
                validAddress(_registry)
                validMaxConversionFee(_maxConversionFee)
            {
                registry = _registry;
                IContractFeatures features = IContractFeatures(registry.getAddress(ContractIds.CONTRACT_FEATURES));
        
                // initialize supported features
                if (features != address(0))
                    features.enableFeatures(FeatureIds.CONVERTER_CONVERSION_WHITELIST, true);
        
                maxConversionFee = _maxConversionFee;
        
                if (_connectorToken != address(0))
                    addConnector(_connectorToken, _connectorWeight, false);
            }
        
            // validates a connector token address - verifies that the address belongs to one of the connector tokens
            modifier validConnector(IERC20Token _address) {
                require(connectors[_address].isSet);
                _;
            }
        
            // validates a token address - verifies that the address belongs to one of the convertible tokens
            modifier validToken(IERC20Token _address) {
                require(_address == token || connectors[_address].isSet);
                _;
            }
        
            // validates maximum conversion fee
            modifier validMaxConversionFee(uint32 _conversionFee) {
                require(_conversionFee >= 0 && _conversionFee <= MAX_CONVERSION_FEE);
                _;
            }
        
            // validates conversion fee
            modifier validConversionFee(uint32 _conversionFee) {
                require(_conversionFee >= 0 && _conversionFee <= maxConversionFee);
                _;
            }
        
            // validates connector weight range
            modifier validConnectorWeight(uint32 _weight) {
                require(_weight > 0 && _weight <= MAX_WEIGHT);
                _;
            }
        
            // validates a conversion path - verifies that the number of elements is odd and that maximum number of 'hops' is 10
            modifier validConversionPath(IERC20Token[] _path) {
                require(_path.length > 2 && _path.length <= (1 + 2 * 10) && _path.length % 2 == 1);
                _;
            }
        
            // allows execution only when conversions aren't disabled
            modifier conversionsAllowed {
                assert(conversionsEnabled);
                _;
            }
        
            // allows execution by the BancorNetwork contract only
            modifier bancorNetworkOnly {
                IBancorNetwork bancorNetwork = IBancorNetwork(registry.getAddress(ContractIds.BANCOR_NETWORK));
                require(msg.sender == address(bancorNetwork));
                _;
            }
        
            /**
                @dev returns the number of connector tokens defined
        
                @return number of connector tokens
            */
            function connectorTokenCount() public view returns (uint16) {
                return uint16(connectorTokens.length);
            }
        
            /*
                @dev allows the owner to update the registry contract address
        
                @param _registry    address of a bancor converter registry contract
            */
            function setRegistry(IContractRegistry _registry)
                public
                ownerOnly
                validAddress(_registry)
                notThis(_registry)
            {
                registry = _registry;
            }
        
            /*
                @dev allows the owner to update & enable the conversion whitelist contract address
                when set, only addresses that are whitelisted are actually allowed to use the converter
                note that the whitelist check is actually done by the BancorNetwork contract
        
                @param _whitelist    address of a whitelist contract
            */
            function setConversionWhitelist(IWhitelist _whitelist)
                public
                ownerOnly
                notThis(_whitelist)
            {
                conversionWhitelist = _whitelist;
            }
        
            /*
                @dev allows the manager to update the quick buy path
        
                @param _path    new quick buy path, see conversion path format in the bancorNetwork contract
            */
            function setQuickBuyPath(IERC20Token[] _path)
                public
                ownerOnly
                validConversionPath(_path)
            {
                quickBuyPath = _path;
            }
        
            /*
                @dev allows the manager to clear the quick buy path
            */
            function clearQuickBuyPath() public ownerOnly {
                quickBuyPath.length = 0;
            }
        
            /**
                @dev returns the length of the quick buy path array
        
                @return quick buy path length
            */
            function getQuickBuyPathLength() public view returns (uint256) {
                return quickBuyPath.length;
            }
        
            /**
                @dev disables the entire conversion functionality
                this is a safety mechanism in case of a emergency
                can only be called by the manager
        
                @param _disable true to disable conversions, false to re-enable them
            */
            function disableConversions(bool _disable) public ownerOrManagerOnly {
                conversionsEnabled = !_disable;
            }
        
            /**
                @dev updates the current conversion fee
                can only be called by the manager
        
                @param _conversionFee new conversion fee, represented in ppm
            */
            function setConversionFee(uint32 _conversionFee)
                public
                ownerOrManagerOnly
                validConversionFee(_conversionFee)
            {
                emit ConversionFeeUpdate(conversionFee, _conversionFee);
                conversionFee = _conversionFee;
            }
        
            /*
                @dev given a return amount, returns the amount minus the conversion fee
        
                @param _amount      return amount
                @param _magnitude   1 for standard conversion, 2 for cross connector conversion
        
                @return return amount minus conversion fee
            */
            function getFinalAmount(uint256 _amount, uint8 _magnitude) public view returns (uint256) {
                return safeMul(_amount, (MAX_CONVERSION_FEE - conversionFee) ** _magnitude) / MAX_CONVERSION_FEE ** _magnitude;
            }
        
            /**
                @dev defines a new connector for the token
                can only be called by the owner while the converter is inactive
        
                @param _token                  address of the connector token
                @param _weight                 constant connector weight, represented in ppm, 1-1000000
                @param _enableVirtualBalance   true to enable virtual balance for the connector, false to disable it
            */
            function addConnector(IERC20Token _token, uint32 _weight, bool _enableVirtualBalance)
                public
                ownerOnly
                inactive
                validAddress(_token)
                notThis(_token)
                validConnectorWeight(_weight)
            {
                require(_token != token && !connectors[_token].isSet && totalConnectorWeight + _weight <= MAX_WEIGHT); // validate input
        
                connectors[_token].virtualBalance = 0;
                connectors[_token].weight = _weight;
                connectors[_token].isVirtualBalanceEnabled = _enableVirtualBalance;
                connectors[_token].isPurchaseEnabled = true;
                connectors[_token].isSet = true;
                connectorTokens.push(_token);
                totalConnectorWeight += _weight;
            }
        
            /**
                @dev updates one of the token connectors
                can only be called by the owner
        
                @param _connectorToken         address of the connector token
                @param _weight                 constant connector weight, represented in ppm, 1-1000000
                @param _enableVirtualBalance   true to enable virtual balance for the connector, false to disable it
                @param _virtualBalance         new connector's virtual balance
            */
            function updateConnector(IERC20Token _connectorToken, uint32 _weight, bool _enableVirtualBalance, uint256 _virtualBalance)
                public
                ownerOnly
                validConnector(_connectorToken)
                validConnectorWeight(_weight)
            {
                Connector storage connector = connectors[_connectorToken];
                require(totalConnectorWeight - connector.weight + _weight <= MAX_WEIGHT); // validate input
        
                totalConnectorWeight = totalConnectorWeight - connector.weight + _weight;
                connector.weight = _weight;
                connector.isVirtualBalanceEnabled = _enableVirtualBalance;
                connector.virtualBalance = _virtualBalance;
            }
        
            /**
                @dev disables purchasing with the given connector token in case the connector token got compromised
                can only be called by the owner
                note that selling is still enabled regardless of this flag and it cannot be disabled by the owner
        
                @param _connectorToken  connector token contract address
                @param _disable         true to disable the token, false to re-enable it
            */
            function disableConnectorPurchases(IERC20Token _connectorToken, bool _disable)
                public
                ownerOnly
                validConnector(_connectorToken)
            {
                connectors[_connectorToken].isPurchaseEnabled = !_disable;
            }
        
            /**
                @dev returns the connector's virtual balance if one is defined, otherwise returns the actual balance
        
                @param _connectorToken  connector token contract address
        
                @return connector balance
            */
            function getConnectorBalance(IERC20Token _connectorToken)
                public
                view
                validConnector(_connectorToken)
                returns (uint256)
            {
                Connector storage connector = connectors[_connectorToken];
                return connector.isVirtualBalanceEnabled ? connector.virtualBalance : _connectorToken.balanceOf(this);
            }
        
            /**
                @dev returns the expected return for converting a specific amount of _fromToken to _toToken
        
                @param _fromToken  ERC20 token to convert from
                @param _toToken    ERC20 token to convert to
                @param _amount     amount to convert, in fromToken
        
                @return expected conversion return amount
            */
            function getReturn(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount) public view returns (uint256) {
                require(_fromToken != _toToken); // validate input
        
                // conversion between the token and one of its connectors
                if (_toToken == token)
                    return getPurchaseReturn(_fromToken, _amount);
                else if (_fromToken == token)
                    return getSaleReturn(_toToken, _amount);
        
                // conversion between 2 connectors
                return getCrossConnectorReturn(_fromToken, _toToken, _amount);
            }
        
            /**
                @dev returns the expected return for buying the token for a connector token
        
                @param _connectorToken  connector token contract address
                @param _depositAmount   amount to deposit (in the connector token)
        
                @return expected purchase return amount
            */
            function getPurchaseReturn(IERC20Token _connectorToken, uint256 _depositAmount)
                public
                view
                active
                validConnector(_connectorToken)
                returns (uint256)
            {
                Connector storage connector = connectors[_connectorToken];
                require(connector.isPurchaseEnabled); // validate input
        
                uint256 tokenSupply = token.totalSupply();
                uint256 connectorBalance = getConnectorBalance(_connectorToken);
                IBancorFormula formula = IBancorFormula(registry.getAddress(ContractIds.BANCOR_FORMULA));
                uint256 amount = formula.calculatePurchaseReturn(tokenSupply, connectorBalance, connector.weight, _depositAmount);
        
                // return the amount minus the conversion fee
                return getFinalAmount(amount, 1);
            }
        
            /**
                @dev returns the expected return for selling the token for one of its connector tokens
        
                @param _connectorToken  connector token contract address
                @param _sellAmount      amount to sell (in the smart token)
        
                @return expected sale return amount
            */
            function getSaleReturn(IERC20Token _connectorToken, uint256 _sellAmount)
                public
                view
                active
                validConnector(_connectorToken)
                returns (uint256)
            {
                Connector storage connector = connectors[_connectorToken];
                uint256 tokenSupply = token.totalSupply();
                uint256 connectorBalance = getConnectorBalance(_connectorToken);
                IBancorFormula formula = IBancorFormula(registry.getAddress(ContractIds.BANCOR_FORMULA));
                uint256 amount = formula.calculateSaleReturn(tokenSupply, connectorBalance, connector.weight, _sellAmount);
        
                // return the amount minus the conversion fee
                return getFinalAmount(amount, 1);
            }
        
            /**
                @dev returns the expected return for selling one of the connector tokens for another connector token
        
                @param _fromConnectorToken  contract address of the connector token to convert from
                @param _toConnectorToken    contract address of the connector token to convert to
                @param _sellAmount          amount to sell (in the from connector token)
        
                @return expected sale return amount (in the to connector token)
            */
            function getCrossConnectorReturn(IERC20Token _fromConnectorToken, IERC20Token _toConnectorToken, uint256 _sellAmount)
                public
                view
                active
                validConnector(_fromConnectorToken)
                validConnector(_toConnectorToken)
                returns (uint256)
            {
                Connector storage fromConnector = connectors[_fromConnectorToken];
                Connector storage toConnector = connectors[_toConnectorToken];
                require(toConnector.isPurchaseEnabled); // validate input
        
                uint256 fromConnectorBalance = getConnectorBalance(_fromConnectorToken);
                uint256 toConnectorBalance = getConnectorBalance(_toConnectorToken);
        
                IBancorFormula formula = IBancorFormula(registry.getAddress(ContractIds.BANCOR_FORMULA));
                uint256 amount = formula.calculateCrossConnectorReturn(fromConnectorBalance, fromConnector.weight, toConnectorBalance, toConnector.weight, _sellAmount);
        
                // return the amount minus the conversion fee
                // the fee is higher (magnitude = 2) since cross connector conversion equals 2 conversions (from / to the smart token)
                return getFinalAmount(amount, 2);
            }
        
            /**
                @dev converts a specific amount of _fromToken to _toToken
        
                @param _fromToken  ERC20 token to convert from
                @param _toToken    ERC20 token to convert to
                @param _amount     amount to convert, in fromToken
                @param _minReturn  if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return conversion return amount
            */
            function convertInternal(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn)
                public
                bancorNetworkOnly
                conversionsAllowed
                greaterThanZero(_minReturn)
                returns (uint256)
            {
                require(_fromToken != _toToken); // validate input
        
                // conversion between the token and one of its connectors
                if (_toToken == token)
                    return buy(_fromToken, _amount, _minReturn);
                else if (_fromToken == token)
                    return sell(_toToken, _amount, _minReturn);
        
                // conversion between 2 connectors
                uint256 amount = getCrossConnectorReturn(_fromToken, _toToken, _amount);
                // ensure the trade gives something in return and meets the minimum requested amount
                require(amount != 0 && amount >= _minReturn);
        
                // update the source token virtual balance if relevant
                Connector storage fromConnector = connectors[_fromToken];
                if (fromConnector.isVirtualBalanceEnabled)
                    fromConnector.virtualBalance = safeAdd(fromConnector.virtualBalance, _amount);
        
                // update the target token virtual balance if relevant
                Connector storage toConnector = connectors[_toToken];
                if (toConnector.isVirtualBalanceEnabled)
                    toConnector.virtualBalance = safeSub(toConnector.virtualBalance, amount);
        
                // ensure that the trade won't deplete the connector balance
                uint256 toConnectorBalance = getConnectorBalance(_toToken);
                assert(amount < toConnectorBalance);
        
                // transfer funds from the caller in the from connector token
                assert(_fromToken.transferFrom(msg.sender, this, _amount));
                // transfer funds to the caller in the to connector token
                // the transfer might fail if the actual connector balance is smaller than the virtual balance
                assert(_toToken.transfer(msg.sender, amount));
        
                // calculate conversion fee and dispatch the conversion event
                // the fee is higher (magnitude = 2) since cross connector conversion equals 2 conversions (from / to the smart token)
                uint256 feeAmount = safeSub(amount, getFinalAmount(amount, 2));
                dispatchConversionEvent(_fromToken, _toToken, _amount, amount, feeAmount);
        
                // dispatch price data updates for the smart token / both connectors
                emit PriceDataUpdate(_fromToken, token.totalSupply(), getConnectorBalance(_fromToken), fromConnector.weight);
                emit PriceDataUpdate(_toToken, token.totalSupply(), getConnectorBalance(_toToken), toConnector.weight);
                return amount;
            }
        
            /**
                @dev converts a specific amount of _fromToken to _toToken
        
                @param _fromToken  ERC20 token to convert from
                @param _toToken    ERC20 token to convert to
                @param _amount     amount to convert, in fromToken
                @param _minReturn  if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return conversion return amount
            */
            function convert(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256) {
                convertPath = [_fromToken, token, _toToken];
                return quickConvert(convertPath, _amount, _minReturn);
            }
        
            /**
                @dev buys the token by depositing one of its connector tokens
        
                @param _connectorToken  connector token contract address
                @param _depositAmount   amount to deposit (in the connector token)
                @param _minReturn       if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return buy return amount
            */
            function buy(IERC20Token _connectorToken, uint256 _depositAmount, uint256 _minReturn) internal returns (uint256) {
                uint256 amount = getPurchaseReturn(_connectorToken, _depositAmount);
                // ensure the trade gives something in return and meets the minimum requested amount
                require(amount != 0 && amount >= _minReturn);
        
                // update virtual balance if relevant
                Connector storage connector = connectors[_connectorToken];
                if (connector.isVirtualBalanceEnabled)
                    connector.virtualBalance = safeAdd(connector.virtualBalance, _depositAmount);
        
                // transfer funds from the caller in the connector token
                assert(_connectorToken.transferFrom(msg.sender, this, _depositAmount));
                // issue new funds to the caller in the smart token
                token.issue(msg.sender, amount);
        
                // calculate conversion fee and dispatch the conversion event
                uint256 feeAmount = safeSub(amount, getFinalAmount(amount, 1));
                dispatchConversionEvent(_connectorToken, token, _depositAmount, amount, feeAmount);
        
                // dispatch price data update for the smart token/connector
                emit PriceDataUpdate(_connectorToken, token.totalSupply(), getConnectorBalance(_connectorToken), connector.weight);
                return amount;
            }
        
            /**
                @dev sells the token by withdrawing from one of its connector tokens
        
                @param _connectorToken  connector token contract address
                @param _sellAmount      amount to sell (in the smart token)
                @param _minReturn       if the conversion results in an amount smaller the minimum return - it is cancelled, must be nonzero
        
                @return sell return amount
            */
            function sell(IERC20Token _connectorToken, uint256 _sellAmount, uint256 _minReturn) internal returns (uint256) {
                require(_sellAmount <= token.balanceOf(msg.sender)); // validate input
        
                uint256 amount = getSaleReturn(_connectorToken, _sellAmount);
                // ensure the trade gives something in return and meets the minimum requested amount
                require(amount != 0 && amount >= _minReturn);
        
                // ensure that the trade will only deplete the connector balance if the total supply is depleted as well
                uint256 tokenSupply = token.totalSupply();
                uint256 connectorBalance = getConnectorBalance(_connectorToken);
                assert(amount < connectorBalance || (amount == connectorBalance && _sellAmount == tokenSupply));
        
                // update virtual balance if relevant
                Connector storage connector = connectors[_connectorToken];
                if (connector.isVirtualBalanceEnabled)
                    connector.virtualBalance = safeSub(connector.virtualBalance, amount);
        
                // destroy _sellAmount from the caller's balance in the smart token
                token.destroy(msg.sender, _sellAmount);
                // transfer funds to the caller in the connector token
                // the transfer might fail if the actual connector balance is smaller than the virtual balance
                assert(_connectorToken.transfer(msg.sender, amount));
        
                // calculate conversion fee and dispatch the conversion event
                uint256 feeAmount = safeSub(amount, getFinalAmount(amount, 1));
                dispatchConversionEvent(token, _connectorToken, _sellAmount, amount, feeAmount);
        
                // dispatch price data update for the smart token/connector
                emit PriceDataUpdate(_connectorToken, token.totalSupply(), getConnectorBalance(_connectorToken), connector.weight);
                return amount;
            }
        
            /**
                @dev converts the token to any other token in the bancor network by following a predefined conversion path
                note that when converting from an ERC20 token (as opposed to a smart token), allowance must be set beforehand
        
                @param _path        conversion path, see conversion path format in the BancorNetwork contract
                @param _amount      amount to convert from (in the initial source token)
                @param _minReturn   if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
        
                @return tokens issued in return
            */
            function quickConvert(IERC20Token[] _path, uint256 _amount, uint256 _minReturn)
                public
                payable
                validConversionPath(_path)
                returns (uint256)
            {
                return quickConvertPrioritized(_path, _amount, _minReturn, 0x0, 0x0, 0x0, 0x0);
            }
        
            /**
                @dev converts the token to any other token in the bancor network by following a predefined conversion path
                note that when converting from an ERC20 token (as opposed to a smart token), allowance must be set beforehand
        
                @param _path        conversion path, see conversion path format in the BancorNetwork contract
                @param _amount      amount to convert from (in the initial source token)
                @param _minReturn   if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
                @param _block       if the current block exceeded the given parameter - it is cancelled
                @param _v           (signature[128:130]) associated with the signer address and helps validating if the signature is legit
                @param _r           (signature[0:64]) associated with the signer address and helps validating if the signature is legit
                @param _s           (signature[64:128]) associated with the signer address and helps validating if the signature is legit
        
                @return tokens issued in return
            */
            function quickConvertPrioritized(IERC20Token[] _path, uint256 _amount, uint256 _minReturn, uint256 _block, uint8 _v, bytes32 _r, bytes32 _s)
                public
                payable
                validConversionPath(_path)
                returns (uint256)
            {
                IERC20Token fromToken = _path[0];
                IBancorNetwork bancorNetwork = IBancorNetwork(registry.getAddress(ContractIds.BANCOR_NETWORK));
        
                // we need to transfer the source tokens from the caller to the BancorNetwork contract,
                // so it can execute the conversion on behalf of the caller
                if (msg.value == 0) {
                    // not ETH, send the source tokens to the BancorNetwork contract
                    // if the token is the smart token, no allowance is required - destroy the tokens
                    // from the caller and issue them to the BancorNetwork contract
                    if (fromToken == token) {
                        token.destroy(msg.sender, _amount); // destroy _amount tokens from the caller's balance in the smart token
                        token.issue(bancorNetwork, _amount); // issue _amount new tokens to the BancorNetwork contract
                    } else {
                        // otherwise, we assume we already have allowance, transfer the tokens directly to the BancorNetwork contract
                        assert(fromToken.transferFrom(msg.sender, bancorNetwork, _amount));
                    }
                }
        
                // execute the conversion and pass on the ETH with the call
                return bancorNetwork.convertForPrioritized2.value(msg.value)(_path, _amount, _minReturn, msg.sender, _block, _v, _r, _s);
            }
        
            // deprecated, backward compatibility
            function change(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256) {
                return convertInternal(_fromToken, _toToken, _amount, _minReturn);
            }
        
            /**
                @dev helper, dispatches the Conversion event
        
                @param _fromToken       ERC20 token to convert from
                @param _toToken         ERC20 token to convert to
                @param _amount          amount purchased/sold (in the source token)
                @param _returnAmount    amount returned (in the target token)
            */
            function dispatchConversionEvent(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _returnAmount, uint256 _feeAmount) private {
                // fee amount is converted to 255 bits -
                // negative amount means the fee is taken from the source token, positive amount means its taken from the target token
                // currently the fee is always taken from the target token
                // since we convert it to a signed number, we first ensure that it's capped at 255 bits to prevent overflow
                assert(_feeAmount <= 2 ** 255);
                emit Conversion(_fromToken, _toToken, msg.sender, _amount, _returnAmount, int256(_feeAmount));
            }
        
            /**
                @dev fallback, buys the smart token with ETH
                note that the purchase will use the price at the time of the purchase
            */
            function() payable public {
                quickConvert(quickBuyPath, msg.value, 1);
            }
        }