ETH Price: $2,339.19 (-0.92%)
Gas: 0.04 Gwei

Transaction Decoder

Block:
8694702 at Oct-07-2019 11:37:26 AM +UTC
Transaction Fee:
0.000383675 ETH $0.90
Gas Used:
76,735 Gas / 5 Gwei

Account State Difference:

  Address   Before After State Difference Code
(PandaMiner)
258.7345302131548534 Eth258.7349138881548534 Eth0.000383675
0x3926B6Ba...dF331ab4A
0.018011486707046355 Eth
Nonce: 2241
0.017627811707046355 Eth
Nonce: 2242
0.000383675

Execution Trace

0x9ba6c9d09db964771cb4d526188f6af81e9bcf9e.afdbc7b1( )
  • Vyper_contract.getEthToTokenInputPrice( eth_sold=1000000000000000000 ) => ( out=164480466562995219488589 )
    • Vyper_contract.getEthToTokenInputPrice( eth_sold=1000000000000000000 ) => ( out=164480466562995219488589 )
      • AMNToken.balanceOf( _owner=0xE6C198d27a5B71144B40cFa2362ae3166728e0C8 ) => ( balance=3862980404818922187686968 )
      • BancorConverter.getReturn( _fromToken=0x737F98AC8cA59f2C68aD658E3C3d8C8963E40a4c, _toToken=0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C, _amount=164480466562995219488589 ) => ( 475372149897123874437 )
        • SmartToken.CALL( )
        • AMNToken.balanceOf( _owner=0xAA8CEc9CbD7D051BA86d9DEFF1EC0775Bd4B13c5 ) => ( balance=5318256203713122338788571 )
        • SmartToken.balanceOf( 0xAA8CEc9CbD7D051BA86d9DEFF1EC0775Bd4B13c5 ) => ( 15877635095135400981250 )
        • ContractRegistry.getAddress( _contractName=42616E636F72466F726D756C6100000000000000000000000000000000000000 ) => ( 0xFFd2de852B694F88656e91D9DEfa6b425c454742 )
        • BancorFormula.calculateCrossConnectorReturn( _fromConnectorBalance=5318256203713122338788571, _fromConnectorWeight=500000, _toConnectorBalance=15877635095135400981250, _toConnectorWeight=500000, _amount=164480466562995219488589 ) => ( 476324322217236129460 )
        • BancorConverter.getReturn( _fromToken=0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C, _toToken=0xc0829421C1d260BD3cB3E0F06cfE2D52db2cE315, _amount=475372149897123874437 ) => ( 920251908858288989, 0 )
          • SmartToken.CALL( )
          • SmartToken.CALL( )
          • EtherToken.balanceOf( 0xCBc6a023eb975a1e2630223a7959988948E664f3 ) => ( 13255051717803227740725 )
          • ContractRegistry.addressOf( _contractName=42616E636F72466F726D756C6100000000000000000000000000000000000000 ) => ( 0x63bc9acef19b224015cbf75bf0442c57722aF385 )
          • BancorFormula.calculateSaleReturn( _supply=68469138840943963722587247, _reserveBalance=13255051717803227740725, _reserveRatio=100000, _sellAmount=475372149897123874437 ) => ( 920251908858288989 )
            File 1 of 12: Vyper_contract
            # @title Uniswap Exchange Interface V1
            # @notice Source code found at https://github.com/uniswap
            # @notice Use at your own risk
            
            contract Factory():
                def getExchange(token_addr: address) -> address: constant
            
            contract Exchange():
                def getEthToTokenOutputPrice(tokens_bought: uint256) -> uint256(wei): constant
                def ethToTokenTransferInput(min_tokens: uint256, deadline: timestamp, recipient: address) -> uint256: modifying
                def ethToTokenTransferOutput(tokens_bought: uint256, deadline: timestamp, recipient: address) -> uint256(wei): modifying
            
            TokenPurchase: event({buyer: indexed(address), eth_sold: indexed(uint256(wei)), tokens_bought: indexed(uint256)})
            EthPurchase: event({buyer: indexed(address), tokens_sold: indexed(uint256), eth_bought: indexed(uint256(wei))})
            AddLiquidity: event({provider: indexed(address), eth_amount: indexed(uint256(wei)), token_amount: indexed(uint256)})
            RemoveLiquidity: event({provider: indexed(address), eth_amount: indexed(uint256(wei)), token_amount: indexed(uint256)})
            Transfer: event({_from: indexed(address), _to: indexed(address), _value: uint256})
            Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256})
            
            name: public(bytes32)                             # Uniswap V1
            symbol: public(bytes32)                           # UNI-V1
            decimals: public(uint256)                         # 18
            totalSupply: public(uint256)                      # total number of UNI in existence
            balances: uint256[address]                        # UNI balance of an address
            allowances: (uint256[address])[address]           # UNI allowance of one address on another
            token: address(ERC20)                             # address of the ERC20 token traded on this contract
            factory: Factory                                  # interface for the factory that created this contract
            
            # @dev This function acts as a contract constructor which is not currently supported in contracts deployed
            #      using create_with_code_of(). It is called once by the factory during contract creation.
            @public
            def setup(token_addr: address):
                assert (self.factory == ZERO_ADDRESS and self.token == ZERO_ADDRESS) and token_addr != ZERO_ADDRESS
                self.factory = msg.sender
                self.token = token_addr
                self.name = 0x556e697377617020563100000000000000000000000000000000000000000000
                self.symbol = 0x554e492d56310000000000000000000000000000000000000000000000000000
                self.decimals = 18
            
            # @notice Deposit ETH and Tokens (self.token) at current ratio to mint UNI tokens.
            # @dev min_liquidity does nothing when total UNI supply is 0.
            # @param min_liquidity Minimum number of UNI sender will mint if total UNI supply is greater than 0.
            # @param max_tokens Maximum number of tokens deposited. Deposits max amount if total UNI supply is 0.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return The amount of UNI minted.
            @public
            @payable
            def addLiquidity(min_liquidity: uint256, max_tokens: uint256, deadline: timestamp) -> uint256:
                assert deadline > block.timestamp and (max_tokens > 0 and msg.value > 0)
                total_liquidity: uint256 = self.totalSupply
                if total_liquidity > 0:
                    assert min_liquidity > 0
                    eth_reserve: uint256(wei) = self.balance - msg.value
                    token_reserve: uint256 = self.token.balanceOf(self)
                    token_amount: uint256 = msg.value * token_reserve / eth_reserve + 1
                    liquidity_minted: uint256 = msg.value * total_liquidity / eth_reserve
                    assert max_tokens >= token_amount and liquidity_minted >= min_liquidity
                    self.balances[msg.sender] += liquidity_minted
                    self.totalSupply = total_liquidity + liquidity_minted
                    assert self.token.transferFrom(msg.sender, self, token_amount)
                    log.AddLiquidity(msg.sender, msg.value, token_amount)
                    log.Transfer(ZERO_ADDRESS, msg.sender, liquidity_minted)
                    return liquidity_minted
                else:
                    assert (self.factory != ZERO_ADDRESS and self.token != ZERO_ADDRESS) and msg.value >= 1000000000
                    assert self.factory.getExchange(self.token) == self
                    token_amount: uint256 = max_tokens
                    initial_liquidity: uint256 = as_unitless_number(self.balance)
                    self.totalSupply = initial_liquidity
                    self.balances[msg.sender] = initial_liquidity
                    assert self.token.transferFrom(msg.sender, self, token_amount)
                    log.AddLiquidity(msg.sender, msg.value, token_amount)
                    log.Transfer(ZERO_ADDRESS, msg.sender, initial_liquidity)
                    return initial_liquidity
            
            # @dev Burn UNI tokens to withdraw ETH and Tokens at current ratio.
            # @param amount Amount of UNI burned.
            # @param min_eth Minimum ETH withdrawn.
            # @param min_tokens Minimum Tokens withdrawn.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return The amount of ETH and Tokens withdrawn.
            @public
            def removeLiquidity(amount: uint256, min_eth: uint256(wei), min_tokens: uint256, deadline: timestamp) -> (uint256(wei), uint256):
                assert (amount > 0 and deadline > block.timestamp) and (min_eth > 0 and min_tokens > 0)
                total_liquidity: uint256 = self.totalSupply
                assert total_liquidity > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_amount: uint256(wei) = amount * self.balance / total_liquidity
                token_amount: uint256 = amount * token_reserve / total_liquidity
                assert eth_amount >= min_eth and token_amount >= min_tokens
                self.balances[msg.sender] -= amount
                self.totalSupply = total_liquidity - amount
                send(msg.sender, eth_amount)
                assert self.token.transfer(msg.sender, token_amount)
                log.RemoveLiquidity(msg.sender, eth_amount, token_amount)
                log.Transfer(msg.sender, ZERO_ADDRESS, amount)
                return eth_amount, token_amount
            
            # @dev Pricing function for converting between ETH and Tokens.
            # @param input_amount Amount of ETH or Tokens being sold.
            # @param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
            # @param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
            # @return Amount of ETH or Tokens bought.
            @private
            @constant
            def getInputPrice(input_amount: uint256, input_reserve: uint256, output_reserve: uint256) -> uint256:
                assert input_reserve > 0 and output_reserve > 0
                input_amount_with_fee: uint256 = input_amount * 997
                numerator: uint256 = input_amount_with_fee * output_reserve
                denominator: uint256 = (input_reserve * 1000) + input_amount_with_fee
                return numerator / denominator
            
            # @dev Pricing function for converting between ETH and Tokens.
            # @param output_amount Amount of ETH or Tokens being bought.
            # @param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
            # @param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
            # @return Amount of ETH or Tokens sold.
            @private
            @constant
            def getOutputPrice(output_amount: uint256, input_reserve: uint256, output_reserve: uint256) -> uint256:
                assert input_reserve > 0 and output_reserve > 0
                numerator: uint256 = input_reserve * output_amount * 1000
                denominator: uint256 = (output_reserve - output_amount) * 997
                return numerator / denominator + 1
            
            @private
            def ethToTokenInput(eth_sold: uint256(wei), min_tokens: uint256, deadline: timestamp, buyer: address, recipient: address) -> uint256:
                assert deadline >= block.timestamp and (eth_sold > 0 and min_tokens > 0)
                token_reserve: uint256 = self.token.balanceOf(self)
                tokens_bought: uint256 = self.getInputPrice(as_unitless_number(eth_sold), as_unitless_number(self.balance - eth_sold), token_reserve)
                assert tokens_bought >= min_tokens
                assert self.token.transfer(recipient, tokens_bought)
                log.TokenPurchase(buyer, eth_sold, tokens_bought)
                return tokens_bought
            
            # @notice Convert ETH to Tokens.
            # @dev User specifies exact input (msg.value).
            # @dev User cannot specify minimum output or deadline.
            @public
            @payable
            def __default__():
                self.ethToTokenInput(msg.value, 1, block.timestamp, msg.sender, msg.sender)
            
            # @notice Convert ETH to Tokens.
            # @dev User specifies exact input (msg.value) and minimum output.
            # @param min_tokens Minimum Tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of Tokens bought.
            @public
            @payable
            def ethToTokenSwapInput(min_tokens: uint256, deadline: timestamp) -> uint256:
                return self.ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, msg.sender)
            
            # @notice Convert ETH to Tokens and transfers Tokens to recipient.
            # @dev User specifies exact input (msg.value) and minimum output
            # @param min_tokens Minimum Tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output Tokens.
            # @return Amount of Tokens bought.
            @public
            @payable
            def ethToTokenTransferInput(min_tokens: uint256, deadline: timestamp, recipient: address) -> uint256:
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, recipient)
            
            @private
            def ethToTokenOutput(tokens_bought: uint256, max_eth: uint256(wei), deadline: timestamp, buyer: address, recipient: address) -> uint256(wei):
                assert deadline >= block.timestamp and (tokens_bought > 0 and max_eth > 0)
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_sold: uint256 = self.getOutputPrice(tokens_bought, as_unitless_number(self.balance - max_eth), token_reserve)
                # Throws if eth_sold > max_eth
                eth_refund: uint256(wei) = max_eth - as_wei_value(eth_sold, 'wei')
                if eth_refund > 0:
                    send(buyer, eth_refund)
                assert self.token.transfer(recipient, tokens_bought)
                log.TokenPurchase(buyer, as_wei_value(eth_sold, 'wei'), tokens_bought)
                return as_wei_value(eth_sold, 'wei')
            
            # @notice Convert ETH to Tokens.
            # @dev User specifies maximum input (msg.value) and exact output.
            # @param tokens_bought Amount of tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of ETH sold.
            @public
            @payable
            def ethToTokenSwapOutput(tokens_bought: uint256, deadline: timestamp) -> uint256(wei):
                return self.ethToTokenOutput(tokens_bought, msg.value, deadline, msg.sender, msg.sender)
            
            # @notice Convert ETH to Tokens and transfers Tokens to recipient.
            # @dev User specifies maximum input (msg.value) and exact output.
            # @param tokens_bought Amount of tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output Tokens.
            # @return Amount of ETH sold.
            @public
            @payable
            def ethToTokenTransferOutput(tokens_bought: uint256, deadline: timestamp, recipient: address) -> uint256(wei):
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.ethToTokenOutput(tokens_bought, msg.value, deadline, msg.sender, recipient)
            
            @private
            def tokenToEthInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp, buyer: address, recipient: address) -> uint256(wei):
                assert deadline >= block.timestamp and (tokens_sold > 0 and min_eth > 0)
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
                wei_bought: uint256(wei) = as_wei_value(eth_bought, 'wei')
                assert wei_bought >= min_eth
                send(recipient, wei_bought)
                assert self.token.transferFrom(buyer, self, tokens_sold)
                log.EthPurchase(buyer, tokens_sold, wei_bought)
                return wei_bought
            
            
            # @notice Convert Tokens to ETH.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_eth Minimum ETH purchased.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of ETH bought.
            @public
            def tokenToEthSwapInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp) -> uint256(wei):
                return self.tokenToEthInput(tokens_sold, min_eth, deadline, msg.sender, msg.sender)
            
            # @notice Convert Tokens to ETH and transfers ETH to recipient.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_eth Minimum ETH purchased.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @return Amount of ETH bought.
            @public
            def tokenToEthTransferInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp, recipient: address) -> uint256(wei):
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.tokenToEthInput(tokens_sold, min_eth, deadline, msg.sender, recipient)
            
            @private
            def tokenToEthOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp, buyer: address, recipient: address) -> uint256:
                assert deadline >= block.timestamp and eth_bought > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                tokens_sold: uint256 = self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
                # tokens sold is always > 0
                assert max_tokens >= tokens_sold
                send(recipient, eth_bought)
                assert self.token.transferFrom(buyer, self, tokens_sold)
                log.EthPurchase(buyer, tokens_sold, eth_bought)
                return tokens_sold
            
            # @notice Convert Tokens to ETH.
            # @dev User specifies maximum input and exact output.
            # @param eth_bought Amount of ETH purchased.
            # @param max_tokens Maximum Tokens sold.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of Tokens sold.
            @public
            def tokenToEthSwapOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp) -> uint256:
                return self.tokenToEthOutput(eth_bought, max_tokens, deadline, msg.sender, msg.sender)
            
            # @notice Convert Tokens to ETH and transfers ETH to recipient.
            # @dev User specifies maximum input and exact output.
            # @param eth_bought Amount of ETH purchased.
            # @param max_tokens Maximum Tokens sold.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @return Amount of Tokens sold.
            @public
            def tokenToEthTransferOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp, recipient: address) -> uint256:
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.tokenToEthOutput(eth_bought, max_tokens, deadline, msg.sender, recipient)
            
            @private
            def tokenToTokenInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, buyer: address, recipient: address, exchange_addr: address) -> uint256:
                assert (deadline >= block.timestamp and tokens_sold > 0) and (min_tokens_bought > 0 and min_eth_bought > 0)
                assert exchange_addr != self and exchange_addr != ZERO_ADDRESS
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
                wei_bought: uint256(wei) = as_wei_value(eth_bought, 'wei')
                assert wei_bought >= min_eth_bought
                assert self.token.transferFrom(buyer, self, tokens_sold)
                tokens_bought: uint256 = Exchange(exchange_addr).ethToTokenTransferInput(min_tokens_bought, deadline, recipient, value=wei_bought)
                log.EthPurchase(buyer, tokens_sold, wei_bought)
                return tokens_bought
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr).
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (token_addr) bought.
            @public
            def tokenToTokenSwapInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr) and transfers
            #         Tokens (token_addr) to recipient.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (token_addr) bought.
            @public
            def tokenToTokenTransferInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, recipient: address, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, recipient, exchange_addr)
            
            @private
            def tokenToTokenOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, buyer: address, recipient: address, exchange_addr: address) -> uint256:
                assert deadline >= block.timestamp and (tokens_bought > 0 and max_eth_sold > 0)
                assert exchange_addr != self and exchange_addr != ZERO_ADDRESS
                eth_bought: uint256(wei) = Exchange(exchange_addr).getEthToTokenOutputPrice(tokens_bought)
                token_reserve: uint256 = self.token.balanceOf(self)
                tokens_sold: uint256 = self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
                # tokens sold is always > 0
                assert max_tokens_sold >= tokens_sold and max_eth_sold >= eth_bought
                assert self.token.transferFrom(buyer, self, tokens_sold)
                eth_sold: uint256(wei) = Exchange(exchange_addr).ethToTokenTransferOutput(tokens_bought, deadline, recipient, value=eth_bought)
                log.EthPurchase(buyer, tokens_sold, eth_bought)
                return tokens_sold
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr).
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToTokenSwapOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr) and transfers
            #         Tokens (token_addr) to recipient.
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToTokenTransferOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, recipient: address, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, recipient, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token).
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param exchange_addr The address of the exchange for the token being purchased.
            # @return Amount of Tokens (exchange_addr.token) bought.
            @public
            def tokenToExchangeSwapInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, exchange_addr: address) -> uint256:
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token) and transfers
            #         Tokens (exchange_addr.token) to recipient.
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param exchange_addr The address of the exchange for the token being purchased.
            # @return Amount of Tokens (exchange_addr.token) bought.
            @public
            def tokenToExchangeTransferInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, recipient: address, exchange_addr: address) -> uint256:
                assert recipient != self
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, recipient, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token).
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param exchange_addr The address of the exchange for the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToExchangeSwapOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, exchange_addr: address) -> uint256:
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token) and transfers
            #         Tokens (exchange_addr.token) to recipient.
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToExchangeTransferOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, recipient: address, exchange_addr: address) -> uint256:
                assert recipient != self
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, recipient, exchange_addr)
            
            # @notice Public price function for ETH to Token trades with an exact input.
            # @param eth_sold Amount of ETH sold.
            # @return Amount of Tokens that can be bought with input ETH.
            @public
            @constant
            def getEthToTokenInputPrice(eth_sold: uint256(wei)) -> uint256:
                assert eth_sold > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                return self.getInputPrice(as_unitless_number(eth_sold), as_unitless_number(self.balance), token_reserve)
            
            # @notice Public price function for ETH to Token trades with an exact output.
            # @param tokens_bought Amount of Tokens bought.
            # @return Amount of ETH needed to buy output Tokens.
            @public
            @constant
            def getEthToTokenOutputPrice(tokens_bought: uint256) -> uint256(wei):
                assert tokens_bought > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_sold: uint256 = self.getOutputPrice(tokens_bought, as_unitless_number(self.balance), token_reserve)
                return as_wei_value(eth_sold, 'wei')
            
            # @notice Public price function for Token to ETH trades with an exact input.
            # @param tokens_sold Amount of Tokens sold.
            # @return Amount of ETH that can be bought with input Tokens.
            @public
            @constant
            def getTokenToEthInputPrice(tokens_sold: uint256) -> uint256(wei):
                assert tokens_sold > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
                return as_wei_value(eth_bought, 'wei')
            
            # @notice Public price function for Token to ETH trades with an exact output.
            # @param eth_bought Amount of output ETH.
            # @return Amount of Tokens needed to buy output ETH.
            @public
            @constant
            def getTokenToEthOutputPrice(eth_bought: uint256(wei)) -> uint256:
                assert eth_bought > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                return self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
            
            # @return Address of Token that is sold on this exchange.
            @public
            @constant
            def tokenAddress() -> address:
                return self.token
            
            # @return Address of factory that created this exchange.
            @public
            @constant
            def factoryAddress() -> address(Factory):
                return self.factory
            
            # ERC20 compatibility for exchange liquidity modified from
            # https://github.com/ethereum/vyper/blob/master/examples/tokens/ERC20.vy
            @public
            @constant
            def balanceOf(_owner : address) -> uint256:
                return self.balances[_owner]
            
            @public
            def transfer(_to : address, _value : uint256) -> bool:
                self.balances[msg.sender] -= _value
                self.balances[_to] += _value
                log.Transfer(msg.sender, _to, _value)
                return True
            
            @public
            def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
                self.balances[_from] -= _value
                self.balances[_to] += _value
                self.allowances[_from][msg.sender] -= _value
                log.Transfer(_from, _to, _value)
                return True
            
            @public
            def approve(_spender : address, _value : uint256) -> bool:
                self.allowances[msg.sender][_spender] = _value
                log.Approval(msg.sender, _spender, _value)
                return True
            
            @public
            @constant
            def allowance(_owner : address, _spender : address) -> uint256:
                return self.allowances[_owner][_spender]

            File 2 of 12: Vyper_contract
            # @title Uniswap Exchange Interface V1
            # @notice Source code found at https://github.com/uniswap
            # @notice Use at your own risk
            
            contract Factory():
                def getExchange(token_addr: address) -> address: constant
            
            contract Exchange():
                def getEthToTokenOutputPrice(tokens_bought: uint256) -> uint256(wei): constant
                def ethToTokenTransferInput(min_tokens: uint256, deadline: timestamp, recipient: address) -> uint256: modifying
                def ethToTokenTransferOutput(tokens_bought: uint256, deadline: timestamp, recipient: address) -> uint256(wei): modifying
            
            TokenPurchase: event({buyer: indexed(address), eth_sold: indexed(uint256(wei)), tokens_bought: indexed(uint256)})
            EthPurchase: event({buyer: indexed(address), tokens_sold: indexed(uint256), eth_bought: indexed(uint256(wei))})
            AddLiquidity: event({provider: indexed(address), eth_amount: indexed(uint256(wei)), token_amount: indexed(uint256)})
            RemoveLiquidity: event({provider: indexed(address), eth_amount: indexed(uint256(wei)), token_amount: indexed(uint256)})
            Transfer: event({_from: indexed(address), _to: indexed(address), _value: uint256})
            Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256})
            
            name: public(bytes32)                             # Uniswap V1
            symbol: public(bytes32)                           # UNI-V1
            decimals: public(uint256)                         # 18
            totalSupply: public(uint256)                      # total number of UNI in existence
            balances: uint256[address]                        # UNI balance of an address
            allowances: (uint256[address])[address]           # UNI allowance of one address on another
            token: address(ERC20)                             # address of the ERC20 token traded on this contract
            factory: Factory                                  # interface for the factory that created this contract
            
            # @dev This function acts as a contract constructor which is not currently supported in contracts deployed
            #      using create_with_code_of(). It is called once by the factory during contract creation.
            @public
            def setup(token_addr: address):
                assert (self.factory == ZERO_ADDRESS and self.token == ZERO_ADDRESS) and token_addr != ZERO_ADDRESS
                self.factory = msg.sender
                self.token = token_addr
                self.name = 0x556e697377617020563100000000000000000000000000000000000000000000
                self.symbol = 0x554e492d56310000000000000000000000000000000000000000000000000000
                self.decimals = 18
            
            # @notice Deposit ETH and Tokens (self.token) at current ratio to mint UNI tokens.
            # @dev min_liquidity does nothing when total UNI supply is 0.
            # @param min_liquidity Minimum number of UNI sender will mint if total UNI supply is greater than 0.
            # @param max_tokens Maximum number of tokens deposited. Deposits max amount if total UNI supply is 0.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return The amount of UNI minted.
            @public
            @payable
            def addLiquidity(min_liquidity: uint256, max_tokens: uint256, deadline: timestamp) -> uint256:
                assert deadline > block.timestamp and (max_tokens > 0 and msg.value > 0)
                total_liquidity: uint256 = self.totalSupply
                if total_liquidity > 0:
                    assert min_liquidity > 0
                    eth_reserve: uint256(wei) = self.balance - msg.value
                    token_reserve: uint256 = self.token.balanceOf(self)
                    token_amount: uint256 = msg.value * token_reserve / eth_reserve + 1
                    liquidity_minted: uint256 = msg.value * total_liquidity / eth_reserve
                    assert max_tokens >= token_amount and liquidity_minted >= min_liquidity
                    self.balances[msg.sender] += liquidity_minted
                    self.totalSupply = total_liquidity + liquidity_minted
                    assert self.token.transferFrom(msg.sender, self, token_amount)
                    log.AddLiquidity(msg.sender, msg.value, token_amount)
                    log.Transfer(ZERO_ADDRESS, msg.sender, liquidity_minted)
                    return liquidity_minted
                else:
                    assert (self.factory != ZERO_ADDRESS and self.token != ZERO_ADDRESS) and msg.value >= 1000000000
                    assert self.factory.getExchange(self.token) == self
                    token_amount: uint256 = max_tokens
                    initial_liquidity: uint256 = as_unitless_number(self.balance)
                    self.totalSupply = initial_liquidity
                    self.balances[msg.sender] = initial_liquidity
                    assert self.token.transferFrom(msg.sender, self, token_amount)
                    log.AddLiquidity(msg.sender, msg.value, token_amount)
                    log.Transfer(ZERO_ADDRESS, msg.sender, initial_liquidity)
                    return initial_liquidity
            
            # @dev Burn UNI tokens to withdraw ETH and Tokens at current ratio.
            # @param amount Amount of UNI burned.
            # @param min_eth Minimum ETH withdrawn.
            # @param min_tokens Minimum Tokens withdrawn.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return The amount of ETH and Tokens withdrawn.
            @public
            def removeLiquidity(amount: uint256, min_eth: uint256(wei), min_tokens: uint256, deadline: timestamp) -> (uint256(wei), uint256):
                assert (amount > 0 and deadline > block.timestamp) and (min_eth > 0 and min_tokens > 0)
                total_liquidity: uint256 = self.totalSupply
                assert total_liquidity > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_amount: uint256(wei) = amount * self.balance / total_liquidity
                token_amount: uint256 = amount * token_reserve / total_liquidity
                assert eth_amount >= min_eth and token_amount >= min_tokens
                self.balances[msg.sender] -= amount
                self.totalSupply = total_liquidity - amount
                send(msg.sender, eth_amount)
                assert self.token.transfer(msg.sender, token_amount)
                log.RemoveLiquidity(msg.sender, eth_amount, token_amount)
                log.Transfer(msg.sender, ZERO_ADDRESS, amount)
                return eth_amount, token_amount
            
            # @dev Pricing function for converting between ETH and Tokens.
            # @param input_amount Amount of ETH or Tokens being sold.
            # @param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
            # @param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
            # @return Amount of ETH or Tokens bought.
            @private
            @constant
            def getInputPrice(input_amount: uint256, input_reserve: uint256, output_reserve: uint256) -> uint256:
                assert input_reserve > 0 and output_reserve > 0
                input_amount_with_fee: uint256 = input_amount * 997
                numerator: uint256 = input_amount_with_fee * output_reserve
                denominator: uint256 = (input_reserve * 1000) + input_amount_with_fee
                return numerator / denominator
            
            # @dev Pricing function for converting between ETH and Tokens.
            # @param output_amount Amount of ETH or Tokens being bought.
            # @param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
            # @param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
            # @return Amount of ETH or Tokens sold.
            @private
            @constant
            def getOutputPrice(output_amount: uint256, input_reserve: uint256, output_reserve: uint256) -> uint256:
                assert input_reserve > 0 and output_reserve > 0
                numerator: uint256 = input_reserve * output_amount * 1000
                denominator: uint256 = (output_reserve - output_amount) * 997
                return numerator / denominator + 1
            
            @private
            def ethToTokenInput(eth_sold: uint256(wei), min_tokens: uint256, deadline: timestamp, buyer: address, recipient: address) -> uint256:
                assert deadline >= block.timestamp and (eth_sold > 0 and min_tokens > 0)
                token_reserve: uint256 = self.token.balanceOf(self)
                tokens_bought: uint256 = self.getInputPrice(as_unitless_number(eth_sold), as_unitless_number(self.balance - eth_sold), token_reserve)
                assert tokens_bought >= min_tokens
                assert self.token.transfer(recipient, tokens_bought)
                log.TokenPurchase(buyer, eth_sold, tokens_bought)
                return tokens_bought
            
            # @notice Convert ETH to Tokens.
            # @dev User specifies exact input (msg.value).
            # @dev User cannot specify minimum output or deadline.
            @public
            @payable
            def __default__():
                self.ethToTokenInput(msg.value, 1, block.timestamp, msg.sender, msg.sender)
            
            # @notice Convert ETH to Tokens.
            # @dev User specifies exact input (msg.value) and minimum output.
            # @param min_tokens Minimum Tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of Tokens bought.
            @public
            @payable
            def ethToTokenSwapInput(min_tokens: uint256, deadline: timestamp) -> uint256:
                return self.ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, msg.sender)
            
            # @notice Convert ETH to Tokens and transfers Tokens to recipient.
            # @dev User specifies exact input (msg.value) and minimum output
            # @param min_tokens Minimum Tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output Tokens.
            # @return Amount of Tokens bought.
            @public
            @payable
            def ethToTokenTransferInput(min_tokens: uint256, deadline: timestamp, recipient: address) -> uint256:
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.ethToTokenInput(msg.value, min_tokens, deadline, msg.sender, recipient)
            
            @private
            def ethToTokenOutput(tokens_bought: uint256, max_eth: uint256(wei), deadline: timestamp, buyer: address, recipient: address) -> uint256(wei):
                assert deadline >= block.timestamp and (tokens_bought > 0 and max_eth > 0)
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_sold: uint256 = self.getOutputPrice(tokens_bought, as_unitless_number(self.balance - max_eth), token_reserve)
                # Throws if eth_sold > max_eth
                eth_refund: uint256(wei) = max_eth - as_wei_value(eth_sold, 'wei')
                if eth_refund > 0:
                    send(buyer, eth_refund)
                assert self.token.transfer(recipient, tokens_bought)
                log.TokenPurchase(buyer, as_wei_value(eth_sold, 'wei'), tokens_bought)
                return as_wei_value(eth_sold, 'wei')
            
            # @notice Convert ETH to Tokens.
            # @dev User specifies maximum input (msg.value) and exact output.
            # @param tokens_bought Amount of tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of ETH sold.
            @public
            @payable
            def ethToTokenSwapOutput(tokens_bought: uint256, deadline: timestamp) -> uint256(wei):
                return self.ethToTokenOutput(tokens_bought, msg.value, deadline, msg.sender, msg.sender)
            
            # @notice Convert ETH to Tokens and transfers Tokens to recipient.
            # @dev User specifies maximum input (msg.value) and exact output.
            # @param tokens_bought Amount of tokens bought.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output Tokens.
            # @return Amount of ETH sold.
            @public
            @payable
            def ethToTokenTransferOutput(tokens_bought: uint256, deadline: timestamp, recipient: address) -> uint256(wei):
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.ethToTokenOutput(tokens_bought, msg.value, deadline, msg.sender, recipient)
            
            @private
            def tokenToEthInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp, buyer: address, recipient: address) -> uint256(wei):
                assert deadline >= block.timestamp and (tokens_sold > 0 and min_eth > 0)
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
                wei_bought: uint256(wei) = as_wei_value(eth_bought, 'wei')
                assert wei_bought >= min_eth
                send(recipient, wei_bought)
                assert self.token.transferFrom(buyer, self, tokens_sold)
                log.EthPurchase(buyer, tokens_sold, wei_bought)
                return wei_bought
            
            
            # @notice Convert Tokens to ETH.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_eth Minimum ETH purchased.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of ETH bought.
            @public
            def tokenToEthSwapInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp) -> uint256(wei):
                return self.tokenToEthInput(tokens_sold, min_eth, deadline, msg.sender, msg.sender)
            
            # @notice Convert Tokens to ETH and transfers ETH to recipient.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_eth Minimum ETH purchased.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @return Amount of ETH bought.
            @public
            def tokenToEthTransferInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: timestamp, recipient: address) -> uint256(wei):
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.tokenToEthInput(tokens_sold, min_eth, deadline, msg.sender, recipient)
            
            @private
            def tokenToEthOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp, buyer: address, recipient: address) -> uint256:
                assert deadline >= block.timestamp and eth_bought > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                tokens_sold: uint256 = self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
                # tokens sold is always > 0
                assert max_tokens >= tokens_sold
                send(recipient, eth_bought)
                assert self.token.transferFrom(buyer, self, tokens_sold)
                log.EthPurchase(buyer, tokens_sold, eth_bought)
                return tokens_sold
            
            # @notice Convert Tokens to ETH.
            # @dev User specifies maximum input and exact output.
            # @param eth_bought Amount of ETH purchased.
            # @param max_tokens Maximum Tokens sold.
            # @param deadline Time after which this transaction can no longer be executed.
            # @return Amount of Tokens sold.
            @public
            def tokenToEthSwapOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp) -> uint256:
                return self.tokenToEthOutput(eth_bought, max_tokens, deadline, msg.sender, msg.sender)
            
            # @notice Convert Tokens to ETH and transfers ETH to recipient.
            # @dev User specifies maximum input and exact output.
            # @param eth_bought Amount of ETH purchased.
            # @param max_tokens Maximum Tokens sold.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @return Amount of Tokens sold.
            @public
            def tokenToEthTransferOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: timestamp, recipient: address) -> uint256:
                assert recipient != self and recipient != ZERO_ADDRESS
                return self.tokenToEthOutput(eth_bought, max_tokens, deadline, msg.sender, recipient)
            
            @private
            def tokenToTokenInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, buyer: address, recipient: address, exchange_addr: address) -> uint256:
                assert (deadline >= block.timestamp and tokens_sold > 0) and (min_tokens_bought > 0 and min_eth_bought > 0)
                assert exchange_addr != self and exchange_addr != ZERO_ADDRESS
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
                wei_bought: uint256(wei) = as_wei_value(eth_bought, 'wei')
                assert wei_bought >= min_eth_bought
                assert self.token.transferFrom(buyer, self, tokens_sold)
                tokens_bought: uint256 = Exchange(exchange_addr).ethToTokenTransferInput(min_tokens_bought, deadline, recipient, value=wei_bought)
                log.EthPurchase(buyer, tokens_sold, wei_bought)
                return tokens_bought
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr).
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (token_addr) bought.
            @public
            def tokenToTokenSwapInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr) and transfers
            #         Tokens (token_addr) to recipient.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (token_addr) bought.
            @public
            def tokenToTokenTransferInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, recipient: address, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, recipient, exchange_addr)
            
            @private
            def tokenToTokenOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, buyer: address, recipient: address, exchange_addr: address) -> uint256:
                assert deadline >= block.timestamp and (tokens_bought > 0 and max_eth_sold > 0)
                assert exchange_addr != self and exchange_addr != ZERO_ADDRESS
                eth_bought: uint256(wei) = Exchange(exchange_addr).getEthToTokenOutputPrice(tokens_bought)
                token_reserve: uint256 = self.token.balanceOf(self)
                tokens_sold: uint256 = self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
                # tokens sold is always > 0
                assert max_tokens_sold >= tokens_sold and max_eth_sold >= eth_bought
                assert self.token.transferFrom(buyer, self, tokens_sold)
                eth_sold: uint256(wei) = Exchange(exchange_addr).ethToTokenTransferOutput(tokens_bought, deadline, recipient, value=eth_bought)
                log.EthPurchase(buyer, tokens_sold, eth_bought)
                return tokens_sold
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr).
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToTokenSwapOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (token_addr) and transfers
            #         Tokens (token_addr) to recipient.
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToTokenTransferOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, recipient: address, token_addr: address) -> uint256:
                exchange_addr: address = self.factory.getExchange(token_addr)
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, recipient, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token).
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param exchange_addr The address of the exchange for the token being purchased.
            # @return Amount of Tokens (exchange_addr.token) bought.
            @public
            def tokenToExchangeSwapInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, exchange_addr: address) -> uint256:
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token) and transfers
            #         Tokens (exchange_addr.token) to recipient.
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies exact input and minimum output.
            # @param tokens_sold Amount of Tokens sold.
            # @param min_tokens_bought Minimum Tokens (token_addr) purchased.
            # @param min_eth_bought Minimum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param exchange_addr The address of the exchange for the token being purchased.
            # @return Amount of Tokens (exchange_addr.token) bought.
            @public
            def tokenToExchangeTransferInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_bought: uint256(wei), deadline: timestamp, recipient: address, exchange_addr: address) -> uint256:
                assert recipient != self
                return self.tokenToTokenInput(tokens_sold, min_tokens_bought, min_eth_bought, deadline, msg.sender, recipient, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token).
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param exchange_addr The address of the exchange for the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToExchangeSwapOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, exchange_addr: address) -> uint256:
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, msg.sender, exchange_addr)
            
            # @notice Convert Tokens (self.token) to Tokens (exchange_addr.token) and transfers
            #         Tokens (exchange_addr.token) to recipient.
            # @dev Allows trades through contracts that were not deployed from the same factory.
            # @dev User specifies maximum input and exact output.
            # @param tokens_bought Amount of Tokens (token_addr) bought.
            # @param max_tokens_sold Maximum Tokens (self.token) sold.
            # @param max_eth_sold Maximum ETH purchased as intermediary.
            # @param deadline Time after which this transaction can no longer be executed.
            # @param recipient The address that receives output ETH.
            # @param token_addr The address of the token being purchased.
            # @return Amount of Tokens (self.token) sold.
            @public
            def tokenToExchangeTransferOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth_sold: uint256(wei), deadline: timestamp, recipient: address, exchange_addr: address) -> uint256:
                assert recipient != self
                return self.tokenToTokenOutput(tokens_bought, max_tokens_sold, max_eth_sold, deadline, msg.sender, recipient, exchange_addr)
            
            # @notice Public price function for ETH to Token trades with an exact input.
            # @param eth_sold Amount of ETH sold.
            # @return Amount of Tokens that can be bought with input ETH.
            @public
            @constant
            def getEthToTokenInputPrice(eth_sold: uint256(wei)) -> uint256:
                assert eth_sold > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                return self.getInputPrice(as_unitless_number(eth_sold), as_unitless_number(self.balance), token_reserve)
            
            # @notice Public price function for ETH to Token trades with an exact output.
            # @param tokens_bought Amount of Tokens bought.
            # @return Amount of ETH needed to buy output Tokens.
            @public
            @constant
            def getEthToTokenOutputPrice(tokens_bought: uint256) -> uint256(wei):
                assert tokens_bought > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_sold: uint256 = self.getOutputPrice(tokens_bought, as_unitless_number(self.balance), token_reserve)
                return as_wei_value(eth_sold, 'wei')
            
            # @notice Public price function for Token to ETH trades with an exact input.
            # @param tokens_sold Amount of Tokens sold.
            # @return Amount of ETH that can be bought with input Tokens.
            @public
            @constant
            def getTokenToEthInputPrice(tokens_sold: uint256) -> uint256(wei):
                assert tokens_sold > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
                return as_wei_value(eth_bought, 'wei')
            
            # @notice Public price function for Token to ETH trades with an exact output.
            # @param eth_bought Amount of output ETH.
            # @return Amount of Tokens needed to buy output ETH.
            @public
            @constant
            def getTokenToEthOutputPrice(eth_bought: uint256(wei)) -> uint256:
                assert eth_bought > 0
                token_reserve: uint256 = self.token.balanceOf(self)
                return self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
            
            # @return Address of Token that is sold on this exchange.
            @public
            @constant
            def tokenAddress() -> address:
                return self.token
            
            # @return Address of factory that created this exchange.
            @public
            @constant
            def factoryAddress() -> address(Factory):
                return self.factory
            
            # ERC20 compatibility for exchange liquidity modified from
            # https://github.com/ethereum/vyper/blob/master/examples/tokens/ERC20.vy
            @public
            @constant
            def balanceOf(_owner : address) -> uint256:
                return self.balances[_owner]
            
            @public
            def transfer(_to : address, _value : uint256) -> bool:
                self.balances[msg.sender] -= _value
                self.balances[_to] += _value
                log.Transfer(msg.sender, _to, _value)
                return True
            
            @public
            def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
                self.balances[_from] -= _value
                self.balances[_to] += _value
                self.allowances[_from][msg.sender] -= _value
                log.Transfer(_from, _to, _value)
                return True
            
            @public
            def approve(_spender : address, _value : uint256) -> bool:
                self.allowances[msg.sender][_spender] = _value
                log.Approval(msg.sender, _spender, _value)
                return True
            
            @public
            @constant
            def allowance(_owner : address, _spender : address) -> uint256:
                return self.allowances[_owner][_spender]

            File 3 of 12: AMNToken
            pragma solidity ^0.4.19;
            
            // File: zeppelin-solidity/contracts/math/SafeMath.sol
            
            /**
             * @title SafeMath
             * @dev Math operations with safety checks that throw on error
             */
            library SafeMath {
            
              /**
              * @dev Multiplies two numbers, throws on overflow.
              */
              function mul(uint256 a, uint256 b) internal pure returns (uint256) {
                if (a == 0) {
                  return 0;
                }
                uint256 c = a * b;
                assert(c / a == b);
                return c;
              }
            
              /**
              * @dev Integer division of two numbers, truncating the quotient.
              */
              function div(uint256 a, uint256 b) internal pure returns (uint256) {
                // assert(b > 0); // Solidity automatically throws when dividing by 0
                uint256 c = a / b;
                // assert(a == b * c + a % b); // There is no case in which this doesn't hold
                return c;
              }
            
              /**
              * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
              */
              function sub(uint256 a, uint256 b) internal pure returns (uint256) {
                assert(b <= a);
                return a - b;
              }
            
              /**
              * @dev Adds two numbers, throws on overflow.
              */
              function add(uint256 a, uint256 b) internal pure returns (uint256) {
                uint256 c = a + b;
                assert(c >= a);
                return c;
              }
            }
            
            // File: zeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol
            
            /**
             * @title ERC20Basic
             * @dev Simpler version of ERC20 interface
             * @dev see https://github.com/ethereum/EIPs/issues/179
             */
            contract ERC20Basic {
              function totalSupply() public view returns (uint256);
              function balanceOf(address who) public view returns (uint256);
              function transfer(address to, uint256 value) public returns (bool);
              event Transfer(address indexed from, address indexed to, uint256 value);
            }
            
            // File: zeppelin-solidity/contracts/token/ERC20/BasicToken.sol
            
            /**
             * @title Basic token
             * @dev Basic version of StandardToken, with no allowances.
             */
            contract BasicToken is ERC20Basic {
              using SafeMath for uint256;
            
              mapping(address => uint256) balances;
            
              uint256 totalSupply_;
            
              /**
              * @dev total number of tokens in existence
              */
              function totalSupply() public view returns (uint256) {
                return totalSupply_;
              }
            
              /**
              * @dev transfer token for a specified address
              * @param _to The address to transfer to.
              * @param _value The amount to be transferred.
              */
              function transfer(address _to, uint256 _value) public returns (bool) {
                require(_to != address(0));
                require(_value <= balances[msg.sender]);
            
                // SafeMath.sub will throw if there is not enough balance.
                balances[msg.sender] = balances[msg.sender].sub(_value);
                balances[_to] = balances[_to].add(_value);
                Transfer(msg.sender, _to, _value);
                return true;
              }
            
              /**
              * @dev Gets the balance of the specified address.
              * @param _owner The address to query the the balance of.
              * @return An uint256 representing the amount owned by the passed address.
              */
              function balanceOf(address _owner) public view returns (uint256 balance) {
                return balances[_owner];
              }
            
            }
            
            // File: zeppelin-solidity/contracts/token/ERC20/ERC20.sol
            
            /**
             * @title ERC20 interface
             * @dev see https://github.com/ethereum/EIPs/issues/20
             */
            contract ERC20 is ERC20Basic {
              function allowance(address owner, address spender) public view returns (uint256);
              function transferFrom(address from, address to, uint256 value) public returns (bool);
              function approve(address spender, uint256 value) public returns (bool);
              event Approval(address indexed owner, address indexed spender, uint256 value);
            }
            
            // File: zeppelin-solidity/contracts/token/ERC20/StandardToken.sol
            
            /**
             * @title Standard ERC20 token
             *
             * @dev Implementation of the basic standard token.
             * @dev https://github.com/ethereum/EIPs/issues/20
             * @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
             */
            contract StandardToken is ERC20, BasicToken {
            
              mapping (address => mapping (address => uint256)) internal allowed;
            
            
              /**
               * @dev Transfer tokens from one address to another
               * @param _from address The address which you want to send tokens from
               * @param _to address The address which you want to transfer to
               * @param _value uint256 the amount of tokens to be transferred
               */
              function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
                require(_to != address(0));
                require(_value <= balances[_from]);
                require(_value <= allowed[_from][msg.sender]);
            
                balances[_from] = balances[_from].sub(_value);
                balances[_to] = balances[_to].add(_value);
                allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
                Transfer(_from, _to, _value);
                return true;
              }
            
              /**
               * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
               *
               * Beware that changing an allowance with this method brings the risk that someone may use both the old
               * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
               * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
               * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
               * @param _spender The address which will spend the funds.
               * @param _value The amount of tokens to be spent.
               */
              function approve(address _spender, uint256 _value) public returns (bool) {
                allowed[msg.sender][_spender] = _value;
                Approval(msg.sender, _spender, _value);
                return true;
              }
            
              /**
               * @dev Function to check the amount of tokens that an owner allowed to a spender.
               * @param _owner address The address which owns the funds.
               * @param _spender address The address which will spend the funds.
               * @return A uint256 specifying the amount of tokens still available for the spender.
               */
              function allowance(address _owner, address _spender) public view returns (uint256) {
                return allowed[_owner][_spender];
              }
            
              /**
               * @dev Increase the amount of tokens that an owner allowed to a spender.
               *
               * approve should be called when allowed[_spender] == 0. To increment
               * allowed value is better to use this function to avoid 2 calls (and wait until
               * the first transaction is mined)
               * From MonolithDAO Token.sol
               * @param _spender The address which will spend the funds.
               * @param _addedValue The amount of tokens to increase the allowance by.
               */
              function increaseApproval(address _spender, uint _addedValue) public returns (bool) {
                allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
                Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
                return true;
              }
            
              /**
               * @dev Decrease the amount of tokens that an owner allowed to a spender.
               *
               * approve should be called when allowed[_spender] == 0. To decrement
               * allowed value is better to use this function to avoid 2 calls (and wait until
               * the first transaction is mined)
               * From MonolithDAO Token.sol
               * @param _spender The address which will spend the funds.
               * @param _subtractedValue The amount of tokens to decrease the allowance by.
               */
              function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {
                uint oldValue = allowed[msg.sender][_spender];
                if (_subtractedValue > oldValue) {
                  allowed[msg.sender][_spender] = 0;
                } else {
                  allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
                }
                Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
                return true;
              }
            
            }
            
            // File: contracts/AMNToken.sol
            
            contract AMNToken is StandardToken {
            
              string  public constant name = "Amon";
              string  public constant symbol = "AMN";
              uint8   public constant decimals = 18;
              uint256 public constant INITIAL_SUPPLY = (1666666667) * (uint256(10) ** decimals);
            
              /**
                *@dev Constructor that set the token initial parameters
                */
              function AMNToken()
              public
              {
                totalSupply_ = INITIAL_SUPPLY;
                balances[msg.sender] = totalSupply_;
              }
            
            }

            File 4 of 12: 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 5 of 12: SmartToken
            pragma solidity ^0.4.18;
            
            
            /*
                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;
                }
            }
            
            
            /*
                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);
            }
            
            
            /**
                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) public {
                    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;
                }
            }
            
            
            /*
                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);
                    OwnerUpdate(owner, newOwner);
                    owner = newOwner;
                    newOwner = address(0);
                }
            }
            
            
            /*
                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() 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));
                }
            }
            
            
            /*
                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)
                    public
                    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 6 of 12: 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 7 of 12: 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 8 of 12: 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 9 of 12: BancorConverter
            pragma solidity ^0.4.24;
            
            // File: contracts/token/interfaces/IERC20Token.sol
            
            /*
                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);
            }
            
            // File: contracts/utility/interfaces/IWhitelist.sol
            
            /*
                Whitelist interface
            */
            contract IWhitelist {
                function isWhitelisted(address _address) public view returns (bool);
            }
            
            // File: contracts/converter/interfaces/IBancorConverter.sol
            
            /*
                Bancor Converter interface
            */
            contract IBancorConverter {
                function getReturn(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount) public view returns (uint256, uint256);
                function convert(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256);
                function conversionWhitelist() public view returns (IWhitelist) {}
                function conversionFee() public view returns (uint32) {}
                function connectors(address _address) public view returns (uint256, uint32, bool, bool, bool) { _address; }
                function getConnectorBalance(IERC20Token _connectorToken) public view returns (uint256);
                function claimTokens(address _from, uint256 _amount) public;
                // deprecated, backward compatibility
                function change(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount, uint256 _minReturn) public returns (uint256);
            }
            
            // File: contracts/converter/interfaces/IBancorConverterUpgrader.sol
            
            /*
                Bancor Converter Upgrader interface
            */
            contract IBancorConverterUpgrader {
                function upgrade(bytes32 _version) public;
                function upgrade(uint16 _version) public;
            }
            
            // File: contracts/converter/interfaces/IBancorFormula.sol
            
            /*
                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);
            }
            
            // File: contracts/IBancorNetwork.sol
            
            /*
                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 convertForPrioritized3(
                    IERC20Token[] _path,
                    uint256 _amount,
                    uint256 _minReturn,
                    address _for,
                    uint256 _customVal,
                    uint256 _block,
                    uint8 _v,
                    bytes32 _r,
                    bytes32 _s
                ) public payable returns (uint256);
                
                // deprecated, backward compatibility
                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);
            }
            
            // File: contracts/ContractIds.sol
            
            /**
                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";
                bytes32 public constant CONTRACT_REGISTRY = "ContractRegistry";
            
                // 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_UPGRADER = "BancorConverterUpgrader";
                bytes32 public constant BANCOR_CONVERTER_FACTORY = "BancorConverterFactory";
            
                // BNT core
                bytes32 public constant BNT_TOKEN = "BNTToken";
                bytes32 public constant BNT_CONVERTER = "BNTConverter";
            
                // BancorX
                bytes32 public constant BANCOR_X = "BancorX";
                bytes32 public constant BANCOR_X_UPGRADER = "BancorXUpgrader";
            }
            
            // File: contracts/FeatureIds.sol
            
            /**
                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;
            }
            
            // File: contracts/utility/interfaces/IOwned.sol
            
            /*
                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;
            }
            
            // File: contracts/utility/Owned.sol
            
            /*
                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
                */
                constructor() public {
                    owner = msg.sender;
                }
            
                // allows execution by the owner only
                modifier ownerOnly {
                    require(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);
                }
            }
            
            // File: contracts/utility/Managed.sol
            
            /*
                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
                */
                constructor() 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);
                }
            }
            
            // File: contracts/utility/Utils.sol
            
            /*
                Utilities & Common Modifiers
            */
            contract Utils {
                /**
                    constructor
                */
                constructor() 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));
                    _;
                }
            
            }
            
            // File: contracts/utility/SafeMath.sol
            
            /*
                Library for basic math operations with overflow/underflow protection
            */
            library SafeMath {
                /**
                    @dev returns the sum of _x and _y, reverts if the calculation overflows
            
                    @param _x   value 1
                    @param _y   value 2
            
                    @return sum
                */
                function add(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    uint256 z = _x + _y;
                    require(z >= _x);
                    return z;
                }
            
                /**
                    @dev returns the difference of _x minus _y, reverts if the calculation underflows
            
                    @param _x   minuend
                    @param _y   subtrahend
            
                    @return difference
                */
                function sub(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    require(_x >= _y);
                    return _x - _y;
                }
            
                /**
                    @dev returns the product of multiplying _x by _y, reverts if the calculation overflows
            
                    @param _x   factor 1
                    @param _y   factor 2
            
                    @return product
                */
                function mul(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    // gas optimization
                    if (_x == 0)
                        return 0;
            
                    uint256 z = _x * _y;
                    require(z / _x == _y);
                    return z;
                }
            
                  /**
                    @dev Integer division of two numbers truncating the quotient, reverts on division by zero.
            
                    @param _x   dividend
                    @param _y   divisor
            
                    @return quotient
                */
                function div(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    require(_y > 0);
                    uint256 c = _x / _y;
            
                    return c;
                }
            }
            
            // File: contracts/utility/interfaces/IContractRegistry.sol
            
            /*
                Contract Registry interface
            */
            contract IContractRegistry {
                function addressOf(bytes32 _contractName) public view returns (address);
            
                // deprecated, backward compatibility
                function getAddress(bytes32 _contractName) public view returns (address);
            }
            
            // File: contracts/utility/interfaces/IContractFeatures.sol
            
            /*
                Contract Features interface
            */
            contract IContractFeatures {
                function isSupported(address _contract, uint256 _features) public view returns (bool);
                function enableFeatures(uint256 _features, bool _enable) public;
            }
            
            // File: contracts/token/interfaces/ISmartToken.sol
            
            /*
                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;
            }
            
            // File: contracts/utility/interfaces/ITokenHolder.sol
            
            /*
                Token Holder interface
            */
            contract ITokenHolder is IOwned {
                function withdrawTokens(IERC20Token _token, address _to, uint256 _amount) public;
            }
            
            // File: contracts/utility/TokenHolder.sol
            
            /*
                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
                */
                constructor() 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));
                }
            }
            
            // File: contracts/token/SmartTokenController.sol
            
            /*
                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
                */
                constructor(ISmartToken _token)
                    public
                    validAddress(_token)
                {
                    token = _token;
                }
            
                // ensures that the controller is the token's owner
                modifier active() {
                    require(token.owner() == address(this));
                    _;
                }
            
                // ensures that the controller is not the token's owner
                modifier inactive() {
                    require(token.owner() != address(this));
                    _;
                }
            
                /**
                    @dev allows transferring the token ownership
                    the new owner needs 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);
                }
            }
            
            // File: contracts/token/interfaces/IEtherToken.sol
            
            /*
                Ether Token interface
            */
            contract IEtherToken is ITokenHolder, IERC20Token {
                function deposit() public payable;
                function withdraw(uint256 _amount) public;
                function withdrawTo(address _to, uint256 _amount) public;
            }
            
            // File: contracts/bancorx/interfaces/IBancorX.sol
            
            contract IBancorX {
                function xTransfer(bytes32 _toBlockchain, bytes32 _to, uint256 _amount, uint256 _id) public;
                function getXTransferAmount(uint256 _xTransferId, address _for) public view returns (uint256);
            }
            
            // File: contracts/converter/BancorConverter.sol
            
            /*
                Bancor Converter v12
            
                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 {
                using SafeMath for uint256;
            
                
                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 isSaleEnabled;             // is sale of the connector token enabled, can be set by the owner
                    bool isSet;                     // used to tell if the mapping element is defined
                }
            
                uint16 public version = 12;
                string public converterType = 'bancor';
            
                bool public allowRegistryUpdate = true;             // allows the owner to prevent/allow the registry to be updated
                bool public claimTokensEnabled = false;             // allows BancorX contract to claim tokens without allowance (one transaction instread of two)
                IContractRegistry public prevRegistry;              // address of previous registry as security mechanism
                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
                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);
            
                // triggered when conversions are enabled/disabled
                event ConversionsEnable(bool _conversionsEnabled);
            
                /**
                    @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
                */
                constructor(
                    ISmartToken _token,
                    IContractRegistry _registry,
                    uint32 _maxConversionFee,
                    IERC20Token _connectorToken,
                    uint32 _connectorWeight
                )
                    public
                    SmartTokenController(_token)
                    validAddress(_registry)
                    validMaxConversionFee(_maxConversionFee)
                {
                    registry = _registry;
                    prevRegistry = _registry;
                    IContractFeatures features = IContractFeatures(registry.addressOf(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 the total weight is 100%
                modifier maxTotalWeightOnly() {
                    require(totalConnectorWeight == MAX_WEIGHT);
                    _;
                }
            
                // 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.addressOf(ContractIds.BANCOR_NETWORK));
                    require(msg.sender == address(bancorNetwork));
                    _;
                }
            
                // allows execution by the converter upgrader contract only
                modifier converterUpgraderOnly {
                    address converterUpgrader = registry.addressOf(ContractIds.BANCOR_CONVERTER_UPGRADER);
                    require(owner == converterUpgrader);
                    _;
                }
            
                // allows execution only when claim tokens is enabled
                modifier whenClaimTokensEnabled {
                    require(claimTokensEnabled);
                    _;
                }
            
                /**
                    @dev sets the contract registry to whichever address the current registry is pointing to
                 */
                function updateRegistry() public {
                    // require that upgrading is allowed or that the caller is the owner
                    require(allowRegistryUpdate || msg.sender == owner);
            
                    // get the address of whichever registry the current registry is pointing to
                    address newRegistry = registry.addressOf(ContractIds.CONTRACT_REGISTRY);
            
                    // if the new registry hasn't changed or is the zero address, revert
                    require(newRegistry != address(registry) && newRegistry != address(0));
            
                    // set the previous registry as current registry and current registry as newRegistry
                    prevRegistry = registry;
                    registry = IContractRegistry(newRegistry);
                }
            
                /**
                    @dev security mechanism allowing the converter owner to revert to the previous registry,
                    to be used in emergency scenario
                */
                function restoreRegistry() public ownerOrManagerOnly {
                    // set the registry as previous registry
                    registry = prevRegistry;
            
                    // after a previous registry is restored, only the owner can allow future updates
                    allowRegistryUpdate = false;
                }
            
                /**
                    @dev disables the registry update functionality
                    this is a safety mechanism in case of a emergency
                    can only be called by the manager or owner
            
                    @param _disable    true to disable registry updates, false to re-enable them
                */
                function disableRegistryUpdate(bool _disable) public ownerOrManagerOnly {
                    allowRegistryUpdate = !_disable;
                }
            
                /**
                    @dev disables/enables the claim tokens functionality
            
                    @param _enable    true to enable claiming of tokens, false to disable
                 */
                function enableClaimTokens(bool _enable) public ownerOnly {
                    claimTokensEnabled = _enable;
                }
            
                /**
                    @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 & 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 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 {
                    if (conversionsEnabled == _disable) {
                        conversionsEnabled = !_disable;
                        emit ConversionsEnable(conversionsEnabled);
                    }
                }
            
                /**
                    @dev allows transferring the token ownership
                    the new owner needs to accept the transfer
                    can only be called by the contract owner
                    note that token ownership can only be transferred while the owner is the converter upgrader contract
            
                    @param _newOwner    new token owner
                */
                function transferTokenOwnership(address _newOwner)
                    public
                    ownerOnly
                    converterUpgraderOnly
                {
                    super.transferTokenOwnership(_newOwner);
                }
            
                /**
                    @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 _amount.mul((MAX_CONVERSION_FEE - conversionFee) ** _magnitude).div(MAX_CONVERSION_FEE ** _magnitude);
                }
            
                /**
                    @dev withdraws tokens held by the converter and sends them to an account
                    can only be called by the owner
                    note that connector tokens can only be withdrawn by the owner while the converter is inactive
                    unless the owner is the converter upgrader contract
            
                    @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 {
                    address converterUpgrader = registry.addressOf(ContractIds.BANCOR_CONVERTER_UPGRADER);
            
                    // if the token is not a connector token, allow withdrawal
                    // otherwise verify that the converter is inactive or that the owner is the upgrader contract
                    require(!connectors[_token].isSet || token.owner() != address(this) || owner == converterUpgrader);
                    super.withdrawTokens(_token, _to, _amount);
                }
            
                /**
                    @dev allows the BancorX contract to claim BNT from any address (so that users
                    dont have to first give allowance when calling BancorX)
            
                    @param _from      address to claim the BNT from
                    @param _amount    the amount to claim
                 */
                function claimTokens(address _from, uint256 _amount) public whenClaimTokensEnabled {
                    address bancorX = registry.addressOf(ContractIds.BANCOR_X);
            
                    // only the bancorX contract may call this method
                    require(msg.sender == bancorX);
            
                    // destroy the tokens belonging to _from, and issue the same amount to bancorX contract
                    token.destroy(_from, _amount);
                    token.issue(msg.sender, _amount);
                }
            
                /**
                    @dev upgrades the converter to the latest version
                    can only be called by the owner
                    note that the owner needs to call acceptOwnership/acceptManagement on the new converter after the upgrade
                */
                function upgrade() public ownerOnly {
                    IBancorConverterUpgrader converterUpgrader = IBancorConverterUpgrader(registry.addressOf(ContractIds.BANCOR_CONVERTER_UPGRADER));
            
                    transferOwnership(converterUpgrader);
                    converterUpgrader.upgrade(version);
                    acceptOwnership();
                }
            
                /**
                    @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].isSaleEnabled = 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 converting from the given connector token in case the connector token got compromised
                    can only be called by the owner
                    note that converting to the token 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 disableConnectorSale(IERC20Token _connectorToken, bool _disable)
                    public
                    ownerOnly
                    validConnector(_connectorToken)
                {
                    connectors[_connectorToken].isSaleEnabled = !_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 and conversion fee
                */
                function getReturn(IERC20Token _fromToken, IERC20Token _toToken, uint256 _amount) public view returns (uint256, 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 and conversion fee
                */
                function getPurchaseReturn(IERC20Token _connectorToken, uint256 _depositAmount)
                    public
                    view
                    active
                    validConnector(_connectorToken)
                    returns (uint256, uint256)
                {
                    Connector storage connector = connectors[_connectorToken];
                    require(connector.isSaleEnabled); // validate input
            
                    uint256 tokenSupply = token.totalSupply();
                    uint256 connectorBalance = getConnectorBalance(_connectorToken);
                    IBancorFormula formula = IBancorFormula(registry.addressOf(ContractIds.BANCOR_FORMULA));
                    uint256 amount = formula.calculatePurchaseReturn(tokenSupply, connectorBalance, connector.weight, _depositAmount);
                    uint256 finalAmount = getFinalAmount(amount, 1);
            
                    // return the amount minus the conversion fee and the conversion fee
                    return (finalAmount, amount - finalAmount);
                }
            
                /**
                    @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 and conversion fee
                */
                function getSaleReturn(IERC20Token _connectorToken, uint256 _sellAmount)
                    public
                    view
                    active
                    validConnector(_connectorToken)
                    returns (uint256, uint256)
                {
                    Connector storage connector = connectors[_connectorToken];
                    uint256 tokenSupply = token.totalSupply();
                    uint256 connectorBalance = getConnectorBalance(_connectorToken);
                    IBancorFormula formula = IBancorFormula(registry.addressOf(ContractIds.BANCOR_FORMULA));
                    uint256 amount = formula.calculateSaleReturn(tokenSupply, connectorBalance, connector.weight, _sellAmount);
                    uint256 finalAmount = getFinalAmount(amount, 1);
            
                    // return the amount minus the conversion fee and the conversion fee
                    return (finalAmount, amount - finalAmount);
                }
            
                /**
                    @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 and conversion fee (in the to connector token)
                */
                function getCrossConnectorReturn(IERC20Token _fromConnectorToken, IERC20Token _toConnectorToken, uint256 _sellAmount)
                    public
                    view
                    active
                    validConnector(_fromConnectorToken)
                    validConnector(_toConnectorToken)
                    returns (uint256, uint256)
                {
                    Connector storage fromConnector = connectors[_fromConnectorToken];
                    Connector storage toConnector = connectors[_toConnectorToken];
                    require(fromConnector.isSaleEnabled); // validate input
            
                    IBancorFormula formula = IBancorFormula(registry.addressOf(ContractIds.BANCOR_FORMULA));
                    uint256 amount = formula.calculateCrossConnectorReturn(
                        getConnectorBalance(_fromConnectorToken), 
                        fromConnector.weight, 
                        getConnectorBalance(_toConnectorToken), 
                        toConnector.weight, 
                        _sellAmount);
                    uint256 finalAmount = getFinalAmount(amount, 2);
            
                    // return the amount minus the conversion fee and the conversion fee
                    // the fee is higher (magnitude = 2) since cross connector conversion equals 2 conversions (from / to the smart token)
                    return (finalAmount, amount - finalAmount);
                }
            
                /**
                    @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);
            
                    uint256 amount;
                    uint256 feeAmount;
            
                    // conversion between 2 connectors
                    (amount, feeAmount) = 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 = fromConnector.virtualBalance.add(_amount);
            
                    // update the target token virtual balance if relevant
                    Connector storage toConnector = connectors[_toToken];
                    if (toConnector.isVirtualBalanceEnabled)
                        toConnector.virtualBalance = toConnector.virtualBalance.sub(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));
            
                    // dispatch the conversion event
                    // the fee is higher (magnitude = 2) since cross connector conversion equals 2 conversions (from / to the smart token)
                    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;
                    uint256 feeAmount;
                    (amount, feeAmount) = 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 = connector.virtualBalance.add(_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);
            
                    // dispatch the conversion event
                    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;
                    uint256 feeAmount;
                    (amount, feeAmount) = 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 = connector.virtualBalance.sub(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));
            
                    // dispatch the conversion event
                    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
                    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
                    returns (uint256)
                {
                    IERC20Token fromToken = _path[0];
                    IBancorNetwork bancorNetwork = IBancorNetwork(registry.addressOf(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.convertForPrioritized3.value(msg.value)(_path, _amount, _minReturn, msg.sender, _amount, _block, _v, _r, _s);
                }
            
                /**
                    @dev allows a user to convert BNT that was sent from another blockchain into any other
                    token on the BancorNetwork without specifying the amount of BNT to be converted, but
                    rather by providing the xTransferId which allows us to get the amount from BancorX.
            
                    @param _path             conversion path, see conversion path format in the BancorNetwork contract
                    @param _minReturn        if the conversion results in an amount smaller than the minimum return - it is cancelled, must be nonzero
                    @param _conversionId     pre-determined unique (if non zero) id which refers to this transaction 
                    @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 to validate if the signature is legit
                    @param _r                (signature[0:64]) associated with the signer address and helps to validate if the signature is legit
                    @param _s                (signature[64:128]) associated with the signer address and helps to validate if the signature is legit
            
                    @return tokens issued in return
                */
                function completeXConversion(
                    IERC20Token[] _path,
                    uint256 _minReturn,
                    uint256 _conversionId,
                    uint256 _block,
                    uint8 _v,
                    bytes32 _r,
                    bytes32 _s
                )
                    public
                    returns (uint256)
                {
                    IBancorX bancorX = IBancorX(registry.addressOf(ContractIds.BANCOR_X));
                    IBancorNetwork bancorNetwork = IBancorNetwork(registry.addressOf(ContractIds.BANCOR_NETWORK));
            
                    // verify that the first token in the path is BNT
                    require(_path[0] == registry.addressOf(ContractIds.BNT_TOKEN));
            
                    // get conversion amount from BancorX contract
                    uint256 amount = bancorX.getXTransferAmount(_conversionId, msg.sender);
            
                    // send BNT from msg.sender to the BancorNetwork contract
                    token.destroy(msg.sender, amount);
                    token.issue(bancorNetwork, amount);
            
                    return bancorNetwork.convertForPrioritized3(_path, amount, _minReturn, msg.sender, _conversionId, _block, _v, _r, _s);
                }
            
                /**
                    @dev buys the token with all connector tokens using the same percentage
                    i.e. if the caller increases the supply by 10%, it will cost an amount equal to
                    10% of each connector token balance
                    can only be called if the max total weight is exactly 100% and while conversions are enabled
            
                    @param _amount  amount to increase the supply by (in the smart token)
                */
                function fund(uint256 _amount)
                    public
                    maxTotalWeightOnly
                    conversionsAllowed
                {
                    uint256 supply = token.totalSupply();
            
                    // iterate through the connector tokens and transfer a percentage equal to the ratio between _amount
                    // and the total supply in each connector from the caller to the converter
                    IERC20Token connectorToken;
                    uint256 connectorBalance;
                    uint256 connectorAmount;
                    for (uint16 i = 0; i < connectorTokens.length; i++) {
                        connectorToken = connectorTokens[i];
                        connectorBalance = getConnectorBalance(connectorToken);
                        connectorAmount = _amount.mul(connectorBalance).div(supply);
            
                        // update virtual balance if relevant
                        Connector storage connector = connectors[connectorToken];
                        if (connector.isVirtualBalanceEnabled)
                            connector.virtualBalance = connector.virtualBalance.add(connectorAmount);
            
                        // transfer funds from the caller in the connector token
                        assert(connectorToken.transferFrom(msg.sender, this, connectorAmount));
            
                        // dispatch price data update for the smart token/connector
                        emit PriceDataUpdate(connectorToken, supply + _amount, connectorBalance + connectorAmount, connector.weight);
                    }
            
                    // issue new funds to the caller in the smart token
                    token.issue(msg.sender, _amount);
                }
            
                /**
                    @dev sells the token for all connector tokens using the same percentage
                    i.e. if the holder sells 10% of the supply, they will receive 10% of each
                    connector token balance in return
                    can only be called if the max total weight is exactly 100%
                    note that the function can also be called if conversions are disabled
            
                    @param _amount  amount to liquidate (in the smart token)
                */
                function liquidate(uint256 _amount) public maxTotalWeightOnly {
                    uint256 supply = token.totalSupply();
            
                    // destroy _amount from the caller's balance in the smart token
                    token.destroy(msg.sender, _amount);
            
                    // iterate through the connector tokens and send a percentage equal to the ratio between _amount
                    // and the total supply from each connector balance to the caller
                    IERC20Token connectorToken;
                    uint256 connectorBalance;
                    uint256 connectorAmount;
                    for (uint16 i = 0; i < connectorTokens.length; i++) {
                        connectorToken = connectorTokens[i];
                        connectorBalance = getConnectorBalance(connectorToken);
                        connectorAmount = _amount.mul(connectorBalance).div(supply);
            
                        // update virtual balance if relevant
                        Connector storage connector = connectors[connectorToken];
                        if (connector.isVirtualBalanceEnabled)
                            connector.virtualBalance = connector.virtualBalance.sub(connectorAmount);
            
                        // 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, connectorAmount));
            
                        // dispatch price data update for the smart token/connector
                        emit PriceDataUpdate(connectorToken, supply - _amount, connectorBalance - connectorAmount, connector.weight);
                    }
                }
            
                // 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));
                }
            }

            File 10 of 12: EtherToken
            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 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 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;
                }
            }
            
            /*
                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));
                }
            }
            
            /*
                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, 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;
                }
            }
            
            /*
                Ether Token interface
            */
            contract IEtherToken is ITokenHolder, IERC20Token {
                function deposit() public payable;
                function withdraw(uint256 _amount) public;
                function withdrawTo(address _to, uint256 _amount);
            }
            
            /**
                Ether tokenization contract
            
                'Owned' is specified here for readability reasons
            */
            contract EtherToken is IEtherToken, Owned, ERC20Token, TokenHolder {
                // triggered when the total supply is increased
                event Issuance(uint256 _amount);
                // triggered when the total supply is decreased
                event Destruction(uint256 _amount);
            
                /**
                    @dev constructor
                */
                function EtherToken()
                    ERC20Token('Ether Token', 'ETH', 18) {
                }
            
                /**
                    @dev deposit ether in the account
                */
                function deposit() public payable {
                    balanceOf[msg.sender] = safeAdd(balanceOf[msg.sender], msg.value); // add the value to the account balance
                    totalSupply = safeAdd(totalSupply, msg.value); // increase the total supply
            
                    Issuance(msg.value);
                    Transfer(this, msg.sender, msg.value);
                }
            
                /**
                    @dev withdraw ether from the account
            
                    @param _amount  amount of ether to withdraw
                */
                function withdraw(uint256 _amount) public {
                    withdrawTo(msg.sender, _amount);
                }
            
                /**
                    @dev withdraw ether from the account to a target account
            
                    @param _to      account to receive the ether
                    @param _amount  amount of ether to withdraw
                */
                function withdrawTo(address _to, uint256 _amount)
                    public
                    notThis(_to)
                {
                    balanceOf[msg.sender] = safeSub(balanceOf[msg.sender], _amount); // deduct the amount from the account balance
                    totalSupply = safeSub(totalSupply, _amount); // decrease the total supply
                    _to.transfer(_amount); // send the amount to the target account
            
                    Transfer(msg.sender, this, _amount);
                    Destruction(_amount);
                }
            
                // ERC20 standard method overrides with some extra protection
            
                /**
                    @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
                    notThis(_to)
                    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
            
                    @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
                    notThis(_to)
                    returns (bool success)
                {
                    assert(super.transferFrom(_from, _to, _value));
                    return true;
                }
            
                /**
                    @dev deposit ether in the account
                */
                function() public payable {
                    deposit();
                }
            }

            File 11 of 12: ContractRegistry
            pragma solidity ^0.4.24;
            
            // File: contracts/utility/interfaces/IOwned.sol
            
            /*
                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;
            }
            
            // File: contracts/utility/Owned.sol
            
            /*
                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
                */
                constructor() public {
                    owner = msg.sender;
                }
            
                // allows execution by the owner only
                modifier ownerOnly {
                    require(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);
                }
            }
            
            // File: contracts/utility/Utils.sol
            
            /*
                Utilities & Common Modifiers
            */
            contract Utils {
                /**
                    constructor
                */
                constructor() 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;
                }
            }
            
            // File: contracts/utility/interfaces/IContractRegistry.sol
            
            /*
                Contract Registry interface
            */
            contract IContractRegistry {
                function addressOf(bytes32 _contractName) public view returns (address);
            
                // deprecated, backward compatibility
                function getAddress(bytes32 _contractName) public view returns (address);
            }
            
            // File: contracts/ContractIds.sol
            
            /**
                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";
                bytes32 public constant CONTRACT_REGISTRY = "ContractRegistry";
            
                // 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_UPGRADER = "BancorConverterUpgrader";
                bytes32 public constant BANCOR_CONVERTER_FACTORY = "BancorConverterFactory";
            
                // Ids of BNT converter and BNT token
                bytes32 public constant BNT_TOKEN = "BNTToken";
                bytes32 public constant BNT_CONVERTER = "BNTConverter";
            
                // Id of BancorX contract
                bytes32 public constant BANCOR_X = "BancorX";
            }
            
            // File: contracts/utility/ContractRegistry.sol
            
            /**
                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 encoded ASCII strings to optimize gas costs
            */
            contract ContractRegistry is IContractRegistry, Owned, Utils, ContractIds {
                struct RegistryItem {
                    address contractAddress;    // contract address
                    uint256 nameIndex;          // index of the item in the list of contract names
                    bool isSet;                 // used to tell if the mapping element is defined
                }
            
                mapping (bytes32 => RegistryItem) private items;    // name -> RegistryItem mapping
                string[] public contractNames;                      // list of all registered contract names
            
                // triggered when an address pointed to by a contract name is modified
                event AddressUpdate(bytes32 indexed _contractName, address _contractAddress);
            
                /**
                    @dev constructor
                */
                constructor() public {
                    registerAddress(ContractIds.CONTRACT_REGISTRY, address(this));
                }
            
                /**
                    @dev returns the number of items in the registry
            
                    @return number of items
                */
                function itemCount() public view returns (uint256) {
                    return contractNames.length;
                }
            
                /**
                    @dev returns the address associated with the given contract name
            
                    @param _contractName    contract name
            
                    @return contract address
                */
                function addressOf(bytes32 _contractName) public view returns (address) {
                    return items[_contractName].contractAddress;
                }
            
                /**
                    @dev registers a new address for the contract name in the registry
            
                    @param _contractName     contract name
                    @param _contractAddress  contract address
                */
                function registerAddress(bytes32 _contractName, address _contractAddress)
                    public
                    ownerOnly
                    validAddress(_contractAddress)
                {
                    require(_contractName.length > 0); // validate input
            
                    // update the address in the registry
                    items[_contractName].contractAddress = _contractAddress;
            
                    if (!items[_contractName].isSet) {
                        // mark the item as set
                        items[_contractName].isSet = true;
                        // add the contract name to the name list
                        uint256 i = contractNames.push(bytes32ToString(_contractName));
                        // update the item's index in the list
                        items[_contractName].nameIndex = i - 1;
                    }
            
                    // dispatch the address update event
                    emit AddressUpdate(_contractName, _contractAddress);
                }
            
                /**
                    @dev removes an existing contract address from the registry
            
                    @param _contractName contract name
                */
                function unregisterAddress(bytes32 _contractName) public ownerOnly {
                    require(_contractName.length > 0); // validate input
            
                    // remove the address from the registry
                    items[_contractName].contractAddress = address(0);
            
                    // if there are multiple items in the registry, move the last element to the deleted element's position
                    // and modify last element's registryItem.nameIndex in the items collection to point to the right position in contractNames
                    if (contractNames.length > 1) {
                        string memory lastContractNameString = contractNames[contractNames.length - 1];
                        uint256 unregisterIndex = items[_contractName].nameIndex;
            
                        contractNames[unregisterIndex] = lastContractNameString;
                        bytes32 lastContractName = stringToBytes32(lastContractNameString);
                        RegistryItem storage registryItem = items[lastContractName];
                        registryItem.nameIndex = unregisterIndex;
                    }
            
                    // remove the last element from the name list
                    contractNames.length--;
                    // zero the deleted element's index
                    items[_contractName].nameIndex = 0;
            
                    // dispatch the address update event
                    emit AddressUpdate(_contractName, address(0));
                }
            
                /**
                    @dev utility, converts bytes32 to a string
                    note that the bytes32 argument is assumed to be UTF8 encoded ASCII string
            
                    @return string representation of the given bytes32 argument
                */
                function bytes32ToString(bytes32 _bytes) private pure returns (string) {
                    bytes memory byteArray = new bytes(32);
                    for (uint256 i; i < 32; i++) {
                        byteArray[i] = _bytes[i];
                    }
            
                    return string(byteArray);
                }
            
                // @dev utility, converts string to bytes32
                function stringToBytes32(string memory _string) private pure returns (bytes32) {
                    bytes32 result;
                    assembly {
                        result := mload(add(_string,32))
                    }
                    return result;
                }
            
                // deprecated, backward compatibility
                function getAddress(bytes32 _contractName) public view returns (address) {
                    return addressOf(_contractName);
                }
            }

            File 12 of 12: BancorFormula
            pragma solidity 0.4.26;
            
            // File: contracts/converter/interfaces/IBancorFormula.sol
            
            /*
                Bancor Formula interface
            */
            contract IBancorFormula {
                function calculatePurchaseReturn(uint256 _supply, uint256 _reserveBalance, uint32 _reserveRatio, uint256 _depositAmount) public view returns (uint256);
                function calculateSaleReturn(uint256 _supply, uint256 _reserveBalance, uint32 _reserveRatio, uint256 _sellAmount) public view returns (uint256);
                function calculateCrossReserveReturn(uint256 _fromReserveBalance, uint32 _fromReserveRatio, uint256 _toReserveBalance, uint32 _toReserveRatio, uint256 _amount) public view returns (uint256);
                // deprecated, backward compatibility
                function calculateCrossConnectorReturn(uint256 _fromConnectorBalance, uint32 _fromConnectorWeight, uint256 _toConnectorBalance, uint32 _toConnectorWeight, uint256 _amount) public view returns (uint256);
            }
            
            // File: contracts/utility/SafeMath.sol
            
            /**
              * @dev Library for basic math operations with overflow/underflow protection
            */
            library SafeMath {
                /**
                  * @dev returns the sum of _x and _y, reverts if the calculation overflows
                  * 
                  * @param _x   value 1
                  * @param _y   value 2
                  * 
                  * @return sum
                */
                function add(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    uint256 z = _x + _y;
                    require(z >= _x);
                    return z;
                }
            
                /**
                  * @dev returns the difference of _x minus _y, reverts if the calculation underflows
                  * 
                  * @param _x   minuend
                  * @param _y   subtrahend
                  * 
                  * @return difference
                */
                function sub(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    require(_x >= _y);
                    return _x - _y;
                }
            
                /**
                  * @dev returns the product of multiplying _x by _y, reverts if the calculation overflows
                  * 
                  * @param _x   factor 1
                  * @param _y   factor 2
                  * 
                  * @return product
                */
                function mul(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    // gas optimization
                    if (_x == 0)
                        return 0;
            
                    uint256 z = _x * _y;
                    require(z / _x == _y);
                    return z;
                }
            
                  /**
                    * ev Integer division of two numbers truncating the quotient, reverts on division by zero.
                    * 
                    * aram _x   dividend
                    * aram _y   divisor
                    * 
                    * eturn quotient
                */
                function div(uint256 _x, uint256 _y) internal pure returns (uint256) {
                    require(_y > 0);
                    uint256 c = _x / _y;
            
                    return c;
                }
            }
            
            // File: contracts/utility/Utils.sol
            
            /**
              * @dev Utilities & Common Modifiers
            */
            contract Utils {
                /**
                  * constructor
                */
                constructor() 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));
                    _;
                }
            
            }
            
            // File: contracts/converter/BancorFormula.sol
            
            contract BancorFormula is IBancorFormula, Utils {
                using SafeMath for uint256;
            
            
                uint16 public version = 4;
            
                uint256 private constant ONE = 1;
                uint32 private constant MAX_RATIO = 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 'PrintFunctionConstructor.py'
                */
                uint256[128] private maxExpArray;
                constructor() 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, reserve balance, ratio and a deposit amount (in the reserve token),
                  * calculates the return for a given conversion (in the main token)
                  * 
                  * Formula:
                  * Return = _supply * ((1 + _depositAmount / _reserveBalance) ^ (_reserveRatio / 1000000) - 1)
                  * 
                  * @param _supply              token total supply
                  * @param _reserveBalance      total reserve balance
                  * @param _reserveRatio        reserve ratio, represented in ppm, 1-1000000
                  * @param _depositAmount       deposit amount, in reserve token
                  * 
                  * @return purchase return amount
                */
                function calculatePurchaseReturn(uint256 _supply, uint256 _reserveBalance, uint32 _reserveRatio, uint256 _depositAmount) public view returns (uint256) {
                    // validate input
                    require(_supply > 0 && _reserveBalance > 0 && _reserveRatio > 0 && _reserveRatio <= MAX_RATIO);
            
                    // special case for 0 deposit amount
                    if (_depositAmount == 0)
                        return 0;
            
                    // special case if the ratio = 100%
                    if (_reserveRatio == MAX_RATIO)
                        return _supply.mul(_depositAmount) / _reserveBalance;
            
                    uint256 result;
                    uint8 precision;
                    uint256 baseN = _depositAmount.add(_reserveBalance);
                    (result, precision) = power(baseN, _reserveBalance, _reserveRatio, MAX_RATIO);
                    uint256 temp = _supply.mul(result) >> precision;
                    return temp - _supply;
                }
            
                /**
                  * @dev given a token supply, reserve balance, ratio and a sell amount (in the main token),
                  * calculates the return for a given conversion (in the reserve token)
                  * 
                  * Formula:
                  * Return = _reserveBalance * (1 - (1 - _sellAmount / _supply) ^ (1 / (_reserveRatio / 1000000)))
                  * 
                  * @param _supply              token total supply
                  * @param _reserveBalance      total reserve
                  * @param _reserveRatio        constant reserve Ratio, represented in ppm, 1-1000000
                  * @param _sellAmount          sell amount, in the token itself
                  * 
                  * @return sale return amount
                */
                function calculateSaleReturn(uint256 _supply, uint256 _reserveBalance, uint32 _reserveRatio, uint256 _sellAmount) public view returns (uint256) {
                    // validate input
                    require(_supply > 0 && _reserveBalance > 0 && _reserveRatio > 0 && _reserveRatio <= MAX_RATIO && _sellAmount <= _supply);
            
                    // special case for 0 sell amount
                    if (_sellAmount == 0)
                        return 0;
            
                    // special case for selling the entire supply
                    if (_sellAmount == _supply)
                        return _reserveBalance;
            
                    // special case if the ratio = 100%
                    if (_reserveRatio == MAX_RATIO)
                        return _reserveBalance.mul(_sellAmount) / _supply;
            
                    uint256 result;
                    uint8 precision;
                    uint256 baseD = _supply - _sellAmount;
                    (result, precision) = power(_supply, baseD, MAX_RATIO, _reserveRatio);
                    uint256 temp1 = _reserveBalance.mul(result);
                    uint256 temp2 = _reserveBalance << precision;
                    return (temp1 - temp2) / result;
                }
            
                /**
                  * @dev given two reserve balances/ratios and a sell amount (in the first reserve token),
                  * calculates the return for a conversion from the first reserve token to the second reserve token (in the second reserve token)
                  * note that prior to version 4, you should use 'calculateCrossConnectorReturn' instead
                  * 
                  * Formula:
                  * Return = _toReserveBalance * (1 - (_fromReserveBalance / (_fromReserveBalance + _amount)) ^ (_fromReserveRatio / _toReserveRatio))
                  * 
                  * @param _fromReserveBalance      input reserve balance
                  * @param _fromReserveRatio        input reserve ratio, represented in ppm, 1-1000000
                  * @param _toReserveBalance        output reserve balance
                  * @param _toReserveRatio          output reserve ratio, represented in ppm, 1-1000000
                  * @param _amount                  input reserve amount
                  * 
                  * @return second reserve amount
                */
                function calculateCrossReserveReturn(uint256 _fromReserveBalance, uint32 _fromReserveRatio, uint256 _toReserveBalance, uint32 _toReserveRatio, uint256 _amount) public view returns (uint256) {
                    // validate input
                    require(_fromReserveBalance > 0 && _fromReserveRatio > 0 && _fromReserveRatio <= MAX_RATIO && _toReserveBalance > 0 && _toReserveRatio > 0 && _toReserveRatio <= MAX_RATIO);
            
                    // special case for equal ratios
                    if (_fromReserveRatio == _toReserveRatio)
                        return _toReserveBalance.mul(_amount) / _fromReserveBalance.add(_amount);
            
                    uint256 result;
                    uint8 precision;
                    uint256 baseN = _fromReserveBalance.add(_amount);
                    (result, precision) = power(baseN, _fromReserveBalance, _fromReserveRatio, _toReserveRatio);
                    uint256 temp1 = _toReserveBalance.mul(result);
                    uint256 temp2 = _toReserveBalance << precision;
                    return (temp1 - temp2) / result;
                }
            
                /**
                  * @dev 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) {
                    require(_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);
                    }
                }
            
                /**
                  * @dev computes 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;
                }
            
                /**
                  * @dev computes 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;
                }
            
                /**
                  * @dev 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;
            
                    require(false);
                    return 0;
                }
            
                /**
                  * @dev 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!
                }
            
                /**
                  * @dev computes log(x / FIXED_1) * FIXED_1
                  * Input range: FIXED_1 <= x <= LOG_EXP_MAX_VAL - 1
                  * Auto-generated via 'PrintFunctionOptimalLog.py'
                  * Detailed description:
                  * - Rewrite the input as a product of natural exponents and a single residual r, such that 1 < r < 2
                  * - The natural logarithm of each (pre-calculated) exponent is the degree of the exponent
                  * - The natural logarithm of r is calculated via Taylor series for log(1 + x), where x = r - 1
                  * - The natural logarithm of the input is calculated by summing up the intermediate results above
                  * - For example: log(250) = log(e^4 * e^1 * e^0.5 * 1.021692859) = 4 + 1 + 0.5 + log(1 + 0.021692859)
                */
                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;} // add 1 / 2^1
                    if (x >= 0xa45af1e1f40c333b3de1db4dd55f29a7) {res += 0x20000000000000000000000000000000; x = x * FIXED_1 / 0xa45af1e1f40c333b3de1db4dd55f29a7;} // add 1 / 2^2
                    if (x >= 0x910b022db7ae67ce76b441c27035c6a1) {res += 0x10000000000000000000000000000000; x = x * FIXED_1 / 0x910b022db7ae67ce76b441c27035c6a1;} // add 1 / 2^3
                    if (x >= 0x88415abbe9a76bead8d00cf112e4d4a8) {res += 0x08000000000000000000000000000000; x = x * FIXED_1 / 0x88415abbe9a76bead8d00cf112e4d4a8;} // add 1 / 2^4
                    if (x >= 0x84102b00893f64c705e841d5d4064bd3) {res += 0x04000000000000000000000000000000; x = x * FIXED_1 / 0x84102b00893f64c705e841d5d4064bd3;} // add 1 / 2^5
                    if (x >= 0x8204055aaef1c8bd5c3259f4822735a2) {res += 0x02000000000000000000000000000000; x = x * FIXED_1 / 0x8204055aaef1c8bd5c3259f4822735a2;} // add 1 / 2^6
                    if (x >= 0x810100ab00222d861931c15e39b44e99) {res += 0x01000000000000000000000000000000; x = x * FIXED_1 / 0x810100ab00222d861931c15e39b44e99;} // add 1 / 2^7
                    if (x >= 0x808040155aabbbe9451521693554f733) {res += 0x00800000000000000000000000000000; x = x * FIXED_1 / 0x808040155aabbbe9451521693554f733;} // add 1 / 2^8
            
                    z = y = x - FIXED_1;
                    w = y * y / FIXED_1;
                    res += z * (0x100000000000000000000000000000000 - y) / 0x100000000000000000000000000000000; z = z * w / FIXED_1; // add y^01 / 01 - y^02 / 02
                    res += z * (0x0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - y) / 0x200000000000000000000000000000000; z = z * w / FIXED_1; // add y^03 / 03 - y^04 / 04
                    res += z * (0x099999999999999999999999999999999 - y) / 0x300000000000000000000000000000000; z = z * w / FIXED_1; // add y^05 / 05 - y^06 / 06
                    res += z * (0x092492492492492492492492492492492 - y) / 0x400000000000000000000000000000000; z = z * w / FIXED_1; // add y^07 / 07 - y^08 / 08
                    res += z * (0x08e38e38e38e38e38e38e38e38e38e38e - y) / 0x500000000000000000000000000000000; z = z * w / FIXED_1; // add y^09 / 09 - y^10 / 10
                    res += z * (0x08ba2e8ba2e8ba2e8ba2e8ba2e8ba2e8b - y) / 0x600000000000000000000000000000000; z = z * w / FIXED_1; // add y^11 / 11 - y^12 / 12
                    res += z * (0x089d89d89d89d89d89d89d89d89d89d89 - y) / 0x700000000000000000000000000000000; z = z * w / FIXED_1; // add y^13 / 13 - y^14 / 14
                    res += z * (0x088888888888888888888888888888888 - y) / 0x800000000000000000000000000000000;                      // add y^15 / 15 - y^16 / 16
            
                    return res;
                }
            
                /**
                  * @dev computes e ^ (x / FIXED_1) * FIXED_1
                  * input range: 0 <= x <= OPT_EXP_MAX_VAL - 1
                  * auto-generated via 'PrintFunctionOptimalExp.py'
                  * Detailed description:
                  * - Rewrite the input as a sum of binary exponents and a single residual r, as small as possible
                  * - The exponentiation of each binary exponent is given (pre-calculated)
                  * - The exponentiation of r is calculated via Taylor series for e^x, where x = r
                  * - The exponentiation of the input is calculated by multiplying the intermediate results above
                  * - For example: e^5.521692859 = e^(4 + 1 + 0.5 + 0.021692859) = e^4 * e^1 * e^0.5 * e^0.021692859
                */
                function optimalExp(uint256 x) internal pure returns (uint256) {
                    uint256 res = 0;
            
                    uint256 y;
                    uint256 z;
            
                    z = y = x % 0x10000000000000000000000000000000; // get the input modulo 2^(-3)
                    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; // multiply by e^2^(-3)
                    if ((x & 0x020000000000000000000000000000000) != 0) res = res * 0x18ebef9eac820ae8682b9793ac6d1e778 / 0x1368b2fc6f9609fe7aceb46aa619baed4; // multiply by e^2^(-2)
                    if ((x & 0x040000000000000000000000000000000) != 0) res = res * 0x1368b2fc6f9609fe7aceb46aa619baed5 / 0x0bc5ab1b16779be3575bd8f0520a9f21f; // multiply by e^2^(-1)
                    if ((x & 0x080000000000000000000000000000000) != 0) res = res * 0x0bc5ab1b16779be3575bd8f0520a9f21e / 0x0454aaa8efe072e7f6ddbab84b40a55c9; // multiply by e^2^(+0)
                    if ((x & 0x100000000000000000000000000000000) != 0) res = res * 0x0454aaa8efe072e7f6ddbab84b40a55c5 / 0x00960aadc109e7a3bf4578099615711ea; // multiply by e^2^(+1)
                    if ((x & 0x200000000000000000000000000000000) != 0) res = res * 0x00960aadc109e7a3bf4578099615711d7 / 0x0002bf84208204f5977f9a8cf01fdce3d; // multiply by e^2^(+2)
                    if ((x & 0x400000000000000000000000000000000) != 0) res = res * 0x0002bf84208204f5977f9a8cf01fdc307 / 0x0000003c6ab775dd0b95b4cbee7e65d11; // multiply by e^2^(+3)
            
                    return res;
                }
            
                /**
                  * @dev deprecated, backward compatibility
                */
                function calculateCrossConnectorReturn(uint256 _fromConnectorBalance, uint32 _fromConnectorWeight, uint256 _toConnectorBalance, uint32 _toConnectorWeight, uint256 _amount) public view returns (uint256) {
                    return calculateCrossReserveReturn(_fromConnectorBalance, _fromConnectorWeight, _toConnectorBalance, _toConnectorWeight, _amount);
                }
            }