text: from web3 import Web3
  from .constants import NETWORK_ADDRESS_MODULES_BY_CHAIN_ID
  
  
  KYBER_NETWORK_PROXY_ABI = [
      {
          "constant": False,
          "inputs": [{"name": "alerter", "type": "address"}],
          "name": "removeAlerter",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "enabled",
          "outputs": [{"name": "", "type": "bool"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "pendingAdmin",
          "outputs": [{"name": "", "type": "address"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "getOperators",
          "outputs": [{"name": "", "type": "address[]"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [
              {"name": "src", "type": "address"},
              {"name": "srcAmount", "type": "uint256"},
              {"name": "dest", "type": "address"},
              {"name": "destAddress", "type": "address"},
              {"name": "maxDestAmount", "type": "uint256"},
              {"name": "minConversionRate", "type": "uint256"},
              {"name": "walletId", "type": "address"},
              {"name": "hint", "type": "bytes"},
          ],
          "name": "tradeWithHint",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": True,
          "stateMutability": "payable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [
              {"name": "token", "type": "address"},
              {"name": "srcAmount", "type": "uint256"},
              {"name": "minConversionRate", "type": "uint256"},
          ],
          "name": "swapTokenToEther",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [
              {"name": "token", "type": "address"},
              {"name": "amount", "type": "uint256"},
              {"name": "sendTo", "type": "address"},
          ],
          "name": "withdrawToken",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "maxGasPrice",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "newAlerter", "type": "address"}],
          "name": "addAlerter",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "kyberNetworkContract",
          "outputs": [{"name": "", "type": "address"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [{"name": "user", "type": "address"}],
          "name": "getUserCapInWei",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [
              {"name": "src", "type": "address"},
              {"name": "srcAmount", "type": "uint256"},
              {"name": "dest", "type": "address"},
              {"name": "minConversionRate", "type": "uint256"},
          ],
          "name": "swapTokenToToken",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "newAdmin", "type": "address"}],
          "name": "transferAdmin",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [],
          "name": "claimAdmin",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [
              {"name": "token", "type": "address"},
              {"name": "minConversionRate", "type": "uint256"},
          ],
          "name": "swapEtherToToken",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": True,
          "stateMutability": "payable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "newAdmin", "type": "address"}],
          "name": "transferAdminQuickly",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "getAlerters",
          "outputs": [{"name": "", "type": "address[]"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [
              {"name": "src", "type": "address"},
              {"name": "dest", "type": "address"},
              {"name": "srcQty", "type": "uint256"},
          ],
          "name": "getExpectedRate",
          "outputs": [
              {"name": "expectedRate", "type": "uint256"},
              {"name": "slippageRate", "type": "uint256"},
          ],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [{"name": "user", "type": "address"}, {"name": "token", "type": "address"}],
          "name": "getUserCapInTokenWei",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "newOperator", "type": "address"}],
          "name": "addOperator",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "_kyberNetworkContract", "type": "address"}],
          "name": "setKyberNetworkContract",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "operator", "type": "address"}],
          "name": "removeOperator",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [{"name": "field", "type": "bytes32"}],
          "name": "info",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [
              {"name": "src", "type": "address"},
              {"name": "srcAmount", "type": "uint256"},
              {"name": "dest", "type": "address"},
              {"name": "destAddress", "type": "address"},
              {"name": "maxDestAmount", "type": "uint256"},
              {"name": "minConversionRate", "type": "uint256"},
              {"name": "walletId", "type": "address"},
          ],
          "name": "trade",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": True,
          "stateMutability": "payable",
          "type": "function",
      },
      {
          "constant": False,
          "inputs": [{"name": "amount", "type": "uint256"}, {"name": "sendTo", "type": "address"}],
          "name": "withdrawEther",
          "outputs": [],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [{"name": "token", "type": "address"}, {"name": "user", "type": "address"}],
          "name": "getBalance",
          "outputs": [{"name": "", "type": "uint256"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "constant": True,
          "inputs": [],
          "name": "admin",
          "outputs": [{"name": "", "type": "address"}],
          "payable": False,
          "stateMutability": "view",
          "type": "function",
      },
      {
          "inputs": [{"name": "_admin", "type": "address"}],
          "payable": False,
          "stateMutability": "nonpayable",
          "type": "constructor",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": True, "name": "trader", "type": "address"},
              {"indexed": False, "name": "src", "type": "address"},
              {"indexed": False, "name": "dest", "type": "address"},
              {"indexed": False, "name": "actualSrcAmount", "type": "uint256"},
              {"indexed": False, "name": "actualDestAmount", "type": "uint256"},
          ],
          "name": "ExecuteTrade",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": False, "name": "newNetworkContract", "type": "address"},
              {"indexed": False, "name": "oldNetworkContract", "type": "address"},
          ],
          "name": "KyberNetworkSet",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": False, "name": "token", "type": "address"},
              {"indexed": False, "name": "amount", "type": "uint256"},
              {"indexed": False, "name": "sendTo", "type": "address"},
          ],
          "name": "TokenWithdraw",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": False, "name": "amount", "type": "uint256"},
              {"indexed": False, "name": "sendTo", "type": "address"},
          ],
          "name": "EtherWithdraw",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [{"indexed": False, "name": "pendingAdmin", "type": "address"}],
          "name": "TransferAdminPending",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": False, "name": "newAdmin", "type": "address"},
              {"indexed": False, "name": "previousAdmin", "type": "address"},
          ],
          "name": "AdminClaimed",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": False, "name": "newAlerter", "type": "address"},
              {"indexed": False, "name": "isAdd", "type": "bool"},
          ],
          "name": "AlerterAdded",
          "type": "event",
      },
      {
          "anonymous": False,
          "inputs": [
              {"indexed": False, "name": "newOperator", "type": "address"},
              {"indexed": False, "name": "isAdd", "type": "bool"},
          ],
          "name": "OperatorAdded",
          "type": "event",
      },
  ]
  
  
  def get_network_proxy_address(chain_id: int):
      network_module = NETWORK_ADDRESS_MODULES_BY_CHAIN_ID.get(chain_id)
      proxy = network_module and network_module.ContractAddress.KyberNetworkProxy
  
      return proxy and proxy.value
  
  
  def get_network_contract_proxy(w3: Web3):
      return w3.eth.contract(
          address=get_network_proxy_address(w3.eth.chainId), abi=KYBER_NETWORK_PROXY_ABI
      )
  