def test_deploy_contract(raiden_network, deploy_client, tmpdir): """Test deploying contract with different version than the one we have set in Registry.sol. This test makes sense only for geth backend, tester uses mocked Registry class. """ contract_path = get_contract_path('Registry.sol') # Create temporary directory to put all files required to compile the changed contract to. # Why? Solidity uses first 40 characters of the file path as a library symbol. # It would be nice to just do a copy of 'Registry.sol', replace version and include statements # and then by path substitution argument of solc set the path to something like # raiden-contracts=/path/to/your/raiden/source/contracts. But then if the path is too long, # Python solidity compiler will fail because of duplicate library symbol. temp_dir = TempSolidityDir(os.path.dirname(contract_path), tmpdir) replaced_registry_path = os.path.join(temp_dir.name, 'Registry.sol') CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER) replace_contract_version(replaced_registry_path, '0.0.31415') contracts = compile_files_cwd([replaced_registry_path]) contract_proxy = deploy_client.deploy_solidity_contract( 'Registry', contracts, dict(), None, contract_path=replaced_registry_path, ) contract_address = contract_proxy.contract_address app0 = raiden_network[0] with pytest.raises(ContractVersionMismatch): app0.raiden.chain.registry(contract_address)
def create_and_distribute_token( client, receivers, amount_per_receiver=1000, name=None, timeout=120): """Create a new ERC-20 token and distribute it among `receivers`. If `name` is None, the name will be derived from hashing all receivers. """ name = name or hexlify(sha3(''.join(receivers).encode())) contract_path = get_contract_path('HumanStandardToken.sol') with gevent.Timeout(timeout): token_proxy = client.deploy_solidity_contract( 'HumanStandardToken', compile_files_cwd([contract_path]), dict(), ( len(receivers) * amount_per_receiver, name, 2, # decimals name[:4].upper(), # symbol ), contract_path=contract_path, ) for receiver in receivers: token_proxy.transact('transfer', receiver, amount_per_receiver) return hexlify(token_proxy.contract_address)
def deploy_all(client): contracts_expanded = [get_contract_path(x) for x in RAIDEN_CONTRACT_FILES] compiled_contracts = compile_files_cwd(contracts_expanded) deployed = {} for contract in CONTRACTS_TO_DEPLOY: deployed.update(deploy_file(contract, compiled_contracts, client)) return deployed
def create_and_distribute_token(client, receivers, amount_per_receiver=1000, name=None, timeout=120): """Create a new ERC-20 token and distribute it among `receivers`. If `name` is None, the name will be derived from hashing all receivers. """ name = name or encode_hex(sha3(''.join(receivers).encode())) contract_path = get_contract_path('HumanStandardToken.sol') with gevent.Timeout(timeout): token_proxy = client.deploy_solidity_contract( 'HumanStandardToken', compile_files_cwd([contract_path]), dict(), ( len(receivers) * amount_per_receiver, name, 2, # decimals name[:4].upper(), # symbol ), contract_path=contract_path, ) for receiver in receivers: token_proxy.transact('transfer', receiver, amount_per_receiver) return to_checksum_address(token_proxy.contract_address)
def _jsonrpc_services( deploy_key, deploy_client, private_keys, verbose, poll_timeout, registry_address=None ): # we cannot instantiate BlockChainService without a registry, so first # deploy it directly with a JSONRPCClient if registry_address is None: registry_path = get_contract_path('Registry.sol') registry_contracts = compile_files_cwd([registry_path]) log.info('Deploying registry contract') registry_proxy = deploy_client.deploy_solidity_contract( 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, timeout=poll_timeout, ) registry_address = registry_proxy.contract_address # at this point the blockchain must be running, this will overwrite the # method so even if the client is patched twice, it should work fine deploy_blockchain = BlockChainService( deploy_key, deploy_client, GAS_PRICE, ) deploy_registry = deploy_blockchain.registry(registry_address) host = '0.0.0.0' blockchain_services = list() for privkey in private_keys: rpc_client = JSONRPCClient( host, deploy_client.port, privkey, ) blockchain = BlockChainService( privkey, rpc_client, GAS_PRICE, ) blockchain_services.append(blockchain) return BlockchainServices( deploy_registry, deploy_blockchain, blockchain_services, )
def f(initial_amount, decimals, token_name, token_symbol): args = [initial_amount, token_name, decimals, token_symbol] contract_path = get_contract_path('HumanStandardToken.sol') compiled = compile_files_cwd([contract_path]) return deploy_client.deploy_solidity_contract( CONTRACT_HUMAN_STANDARD_TOKEN, compiled, constructor_parameters=args, contract_path=contract_path, )
def registry_address(deploy_client): registry_path = get_contract_path('Registry.sol') registry_contracts = compile_files_cwd([registry_path]) registry_proxy = deploy_client.deploy_solidity_contract( 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, ) return decode_hex(registry_proxy.contract.address)
def deploy_rpc_test_contract(deploy_client): here = os.path.dirname(os.path.relpath(__file__)) contract_path = os.path.join(here, 'RpcTest.sol') contracts = compile_files_cwd([contract_path]) contract_proxy = deploy_client.deploy_solidity_contract( 'RpcTest', contracts, libraries=dict(), constructor_parameters=None, contract_path=contract_path, ) return contract_proxy
def deploy_contract(self, contract_name, contract_path, constructor_parameters=None): contracts = compile_files_cwd([contract_path]) log.info('Deploying contract', path=os.path.basename(contract_path)) proxy = self.client.deploy_solidity_contract( contract_name, contracts, list(), constructor_parameters, contract_path=contract_path, timeout=self.poll_timeout, ) return decode_hex(proxy.contract.address)
def deploy_contracts(client, compile_list=None, deploy_list=None): if compile_list is None: compile_list = RAIDEN_CONTRACT_FILES if deploy_list is None: deploy_list = CONTRACTS_TO_DEPLOY contracts_expanded = [ get_contract_path(x) for x in compile_list ] compiled_contracts = compile_files_cwd(contracts_expanded) deployed = {} for contract in deploy_list: deployed.update(deploy_file(contract, compiled_contracts, client)) return deployed
def create_token( self, registry_address, initial_alloc=10**6, name='raidentester', symbol='RDT', decimals=2, timeout=60, auto_register=True, ): """ Create a proxy for a new HumanStandardToken (ERC20), that is initialized with Args(below). Per default it will be registered with 'raiden'. Args: initial_alloc (int): amount of initial tokens. name (str): human readable token name. symbol (str): token shorthand symbol. decimals (int): decimal places. timeout (int): timeout in seconds for creation. auto_register (boolean): if True(default), automatically register the token with raiden. Returns: token_address_hex: the hex encoded address of the new token/token. """ contract_path = get_contract_path('HumanStandardToken.sol') # Deploy a new ERC20 token with gevent.Timeout(timeout): token_proxy = self._chain.client.deploy_solidity_contract( 'HumanStandardToken', compile_files_cwd([contract_path]), dict(), (initial_alloc, name, decimals, symbol), contract_path=contract_path, ) token_address_hex = hexlify(token_proxy.contract_address) if auto_register: self.register_token(registry_address, token_address_hex) print("Successfully created {}the token '{}'.".format( 'and registered ' if auto_register else ' ', name, )) return token_address_hex
def create_token( self, registry_address, initial_alloc=10 ** 6, name='raidentester', symbol='RDT', decimals=2, timeout=60, auto_register=True, ): """ Create a proxy for a new HumanStandardToken (ERC20), that is initialized with Args(below). Per default it will be registered with 'raiden'. Args: initial_alloc (int): amount of initial tokens. name (str): human readable token name. symbol (str): token shorthand symbol. decimals (int): decimal places. timeout (int): timeout in seconds for creation. auto_register (boolean): if True(default), automatically register the token with raiden. Returns: token_address_hex: the hex encoded address of the new token/token. """ contract_path = get_contract_path('HumanStandardToken.sol') # Deploy a new ERC20 token with gevent.Timeout(timeout): token_proxy = self._chain.client.deploy_solidity_contract( 'HumanStandardToken', compile_files_cwd([contract_path]), dict(), (initial_alloc, name, decimals, symbol), contract_path=contract_path, ) token_address_hex = encode_hex(token_proxy.contract_address) if auto_register: self.register_token(registry_address, token_address_hex) print("Successfully created {}the token '{}'.".format( 'and registered ' if auto_register else ' ', name, )) return token_address_hex
def test_blockchain( init_blockchain, web3, blockchain_rpc_ports, private_keys, poll_timeout): # pylint: disable=too-many-locals addresses = [ privatekey_to_address(priv) for priv in private_keys ] privatekey = private_keys[0] address = privatekey_to_address(privatekey) total_token = 100 host = '127.0.0.1' jsonrpc_client = JSONRPCClient( host, blockchain_rpc_ports[0], privatekey, web3=web3, ) humantoken_path = get_contract_path('HumanStandardToken.sol') humantoken_contracts = compile_files_cwd([humantoken_path]) token_proxy = jsonrpc_client.deploy_solidity_contract( 'HumanStandardToken', humantoken_contracts, list(), (total_token, 'raiden', 2, 'Rd'), contract_path=humantoken_path, timeout=poll_timeout, ) token_proxy = Token(jsonrpc_client, to_canonical_address(token_proxy.contract.address)) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_files_cwd([registry_path]) registry_proxy = jsonrpc_client.deploy_solidity_contract( 'Registry', registry_contracts, list(), tuple(), contract_path=registry_path, timeout=poll_timeout, ) registry_proxy = Registry( jsonrpc_client, to_canonical_address(registry_proxy.contract.address), ) log_list = jsonrpc_client.web3.eth.getLogs( { 'fromBlock': 0, 'toBlock': 'latest', 'topics': [], }, ) assert not log_list assert token_proxy.balance_of(address) == total_token manager_address = registry_proxy.add_token( to_canonical_address(token_proxy.proxy.contract.address), ) assert is_address(manager_address) assert len(registry_proxy.token_addresses()) == 1 log_list = jsonrpc_client.web3.eth.getLogs( { 'fromBlock': 0, 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 1 channel_manager_address_encoded = registry_proxy.manager_address_by_token( token_proxy.proxy.contract.address, ) channel_manager_address = to_canonical_address(channel_manager_address_encoded) log = log_list[0] event = decode_event(CONTRACT_MANAGER.get_contract_abi(CONTRACT_REGISTRY), log) event_args = event['args'] assert channel_manager_address == to_canonical_address(event_args['channel_manager_address']) assert is_same_address(token_proxy.proxy.contract.address, event_args['token_address']) channel_manager_proxy = jsonrpc_client.new_contract_proxy( CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER), channel_manager_address, ) channel_manager_proxy = ChannelManager( jsonrpc_client, to_canonical_address(channel_manager_proxy.contract.address), ) channel_address = channel_manager_proxy.new_netting_channel( addresses[1], 10, ) assert is_address(channel_address) log_list = jsonrpc_client.web3.eth.getLogs( { 'fromBlock': 0, 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 2
def test_blockchain( blockchain_backend, # required to start the geth backend pylint: disable=unused-argument blockchain_rpc_ports, private_keys, poll_timeout): # pylint: disable=too-many-locals addresses = [privatekey_to_address(priv) for priv in private_keys] privatekey = private_keys[0] address = privatekey_to_address(privatekey) total_token = 100 host = '127.0.0.1' jsonrpc_client = JSONRPCClient( host, blockchain_rpc_ports[0], privatekey, ) humantoken_path = get_contract_path('HumanStandardToken.sol') humantoken_contracts = compile_files_cwd([humantoken_path]) token_proxy = jsonrpc_client.deploy_solidity_contract( 'HumanStandardToken', humantoken_contracts, list(), (total_token, 'raiden', 2, 'Rd'), contract_path=humantoken_path, timeout=poll_timeout, ) registry_path = get_contract_path('Registry.sol') registry_contracts = compile_files_cwd([registry_path]) registry_proxy = jsonrpc_client.deploy_solidity_contract( 'Registry', registry_contracts, list(), tuple(), contract_path=registry_path, timeout=poll_timeout, ) log_list = jsonrpc_client.web3.eth.getLogs( { 'fromBlock': 0, 'toBlock': 'latest', 'topics': [], }, ) assert not log_list assert token_proxy.call('balanceOf', address) == total_token transaction_hash = registry_proxy.transact( 'addToken', address, token_proxy.contract_address, ) jsonrpc_client.poll(unhexlify(transaction_hash), timeout=poll_timeout) assert len(registry_proxy.call('tokenAddresses')) == 1 log_list = jsonrpc_client.web3.eth.getLogs( { 'fromBlock': 0, 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 1 channel_manager_address_encoded = registry_proxy.call( 'channelManagerByToken', token_proxy.contract_address, ) channel_manager_address = to_canonical_address( channel_manager_address_encoded) log = log_list[0] event = registry_proxy.decode_event(log) event_args = event['args'] assert channel_manager_address == to_canonical_address( event_args['channel_manager_address']) assert token_proxy.contract_address == to_canonical_address( event_args['token_address']) channel_manager_proxy = jsonrpc_client.new_contract_proxy( CONTRACT_MANAGER.get_contract_abi(CONTRACT_CHANNEL_MANAGER), channel_manager_address, ) transaction_hash = channel_manager_proxy.transact( 'newChannel', addresses[1], 10, ) jsonrpc_client.poll(unhexlify(transaction_hash), timeout=poll_timeout) log_list = jsonrpc_client.web3.eth.getLogs( { 'fromBlock': 0, 'toBlock': 'latest', 'topics': [], }, ) assert len(log_list) == 2
def get_test_contract(name): contract_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "smart_contracts", name)) contracts = compile_files_cwd([contract_path]) return contract_path, contracts
def jsonrpc_services( deploy_key, deploy_client, private_keys, poll_timeout, web3=None, ): deploy_blockchain = BlockChainService( deploy_key, deploy_client, GAS_PRICE, ) secret_registry_address = deploy_contract_web3( CONTRACT_SECRET_REGISTRY, poll_timeout, deploy_client, ) secret_registry = deploy_blockchain.secret_registry( secret_registry_address) # noqa registry_path = get_contract_path('Registry.sol') registry_contracts = compile_files_cwd([registry_path]) log.info('Deploying registry contract') registry_proxy = deploy_client.deploy_solidity_contract( 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, timeout=poll_timeout, ) registry_address = decode_hex(registry_proxy.contract.address) # at this point the blockchain must be running, this will overwrite the # method so even if the client is patched twice, it should work fine deploy_registry = deploy_blockchain.registry(registry_address) host = '0.0.0.0' blockchain_services = list() for privkey in private_keys: rpc_client = JSONRPCClient( host, deploy_client.port, privkey, web3=web3, ) blockchain = BlockChainService( privkey, rpc_client, GAS_PRICE, ) blockchain_services.append(blockchain) return BlockchainServices( deploy_registry, secret_registry, deploy_blockchain, blockchain_services, )
def get_test_contract(name): here = os.path.dirname(os.path.relpath(__file__)) contract_path = os.path.join(here, name) contracts = compile_files_cwd([contract_path]) return contract_path, contracts
def _jsonrpc_services( deploy_key, deploy_client, private_keys, verbose, poll_timeout, deploy_new_contracts, registry_address=None, ): deploy_blockchain = BlockChainService( deploy_key, deploy_client, GAS_PRICE, ) if deploy_new_contracts: # secret registry secret_registry_address = deploy_contract_web3( CONTRACT_SECRET_REGISTRY, poll_timeout, deploy_client, ) secret_registry = deploy_blockchain.secret_registry( secret_registry_address) # noqa network_registry_address = deploy_contract_web3( CONTRACT_TOKEN_NETWORK_REGISTRY, poll_timeout, deploy_client, to_checksum_address(secret_registry_address), deploy_blockchain.network_id, ) network_registry = deploy_blockchain.token_network_registry( network_registry_address) # noqa # we cannot instantiate BlockChainService without a registry, so first # deploy it directly with a JSONRPCClient if registry_address is None: registry_path = get_contract_path('Registry.sol') registry_contracts = compile_files_cwd([registry_path]) log.info('Deploying registry contract') registry_proxy = deploy_client.deploy_solidity_contract( 'Registry', registry_contracts, dict(), tuple(), contract_path=registry_path, timeout=poll_timeout, ) registry_address = decode_hex(registry_proxy.contract.address) # at this point the blockchain must be running, this will overwrite the # method so even if the client is patched twice, it should work fine deploy_registry = deploy_blockchain.registry(registry_address) host = '0.0.0.0' blockchain_services = list() for privkey in private_keys: rpc_client = JSONRPCClient( host, deploy_client.port, privkey, ) blockchain = BlockChainService( privkey, rpc_client, GAS_PRICE, ) blockchain_services.append(blockchain) return BlockchainServices( deploy_registry, deploy_blockchain, blockchain_services, )