def deploy_standard_token(deploy_key, tester_chain, token_amount): standard_token_path = get_contract_path('StandardToken.sol') human_token_path = get_contract_path('HumanStandardToken.sol') standard_token_compiled = _solidity.compile_contract( standard_token_path, "StandardToken" ) standard_token_address = tester_chain.contract( standard_token_compiled['bin'], language='evm' ) tester_chain.mine(number_of_blocks=1) contract_libraries = { 'StandardToken': hexlify(standard_token_address), } human_token_compiled = _solidity.compile_contract( human_token_path, 'HumanStandardToken', contract_libraries ) ct = tester.ContractTranslator(human_token_compiled['abi']) human_token_args = ct.encode_constructor_arguments([token_amount, 'raiden', 0, 'rd']) human_token_address = tester_chain.contract( human_token_compiled['bin'] + human_token_args, language='evm', sender=deploy_key ) tester_chain.mine(number_of_blocks=1) return human_token_address
def deploy_standard_token(deploy_key, tester_chain, token_amount): standard_token_path = get_contract_path('StandardToken.sol') human_token_path = get_contract_path('HumanStandardToken.sol') standard_token_compiled = _solidity.compile_contract( standard_token_path, "StandardToken" ) standard_token_address = tester_chain.contract( standard_token_compiled['bin'], language='evm' ) tester_chain.mine(number_of_blocks=1) contract_libraries = { 'StandardToken': hexlify(standard_token_address), } human_token_compiled = _solidity.compile_contract( human_token_path, 'HumanStandardToken', contract_libraries ) ct = tester.ContractTranslator(human_token_compiled['abi']) human_token_args = ct.encode_constructor_arguments([token_amount, 'raiden', 0, 'rd']) human_token_address = tester_chain.contract( human_token_compiled['bin'] + human_token_args, language='evm', sender=deploy_key ) tester_chain.mine(number_of_blocks=1) return human_token_address
def deploy_auxiliary_tester(tester_chain, tester_nettingchannel_library_address): contracts_path = os.path.join(get_project_root(), 'smart_contracts') raiden_remap = 'raiden={}'.format(contracts_path) contract_libraries = { 'NettingChannelLibrary': hexlify(tester_nettingchannel_library_address), } auxiliary_tester_compiled = _solidity.compile_contract( get_relative_contract(__file__, 'AuxiliaryTester.sol'), 'AuxiliaryTester', contract_libraries, extra_args=raiden_remap ) auxiliary_tester_address = tester_chain.contract( auxiliary_tester_compiled['bin'], language='evm', sender=tester.k0 ) auxiliary = tester.ABIContract( tester_chain, auxiliary_tester_compiled['abi'], auxiliary_tester_address ) tester_chain.mine(number_of_blocks=1) return auxiliary
def test_endpointregistry(tester_chain, tester_events): account0 = tester.a0 sender = address_encoder(account0) endpointregistry_path = get_contract_path('EndpointRegistry.sol') endpointregistry_compiled = _solidity.compile_contract( endpointregistry_path, "EndpointRegistry" ) tester_chain.head_state.log_listeners.append(tester_events.append) endpointregistry_address = tester_chain.contract( endpointregistry_compiled['bin'], language='evm' ) endpoint_registry = tester.ABIContract( tester_chain, endpointregistry_compiled['abi'], endpointregistry_address ) endpoint_registry.registerEndpoint('127.0.0.1:4001') assert endpoint_registry.findAddressByEndpoint('127.0.0.1:4001') == sender assert endpoint_registry.findEndpointByAddress(sender) == b'127.0.0.1:4001' endpoint_registry.registerEndpoint('192.168.0.1:4002') assert endpoint_registry.findAddressByEndpoint('192.168.0.1:4002') == sender assert endpoint_registry.findEndpointByAddress(sender) == b'192.168.0.1:4002' assert len(tester_events) == 2 event0 = event_decoder(tester_events[0], endpoint_registry.translator) event1 = event_decoder(tester_events[1], endpoint_registry.translator) assert event0['_event_type'] == b'AddressRegistered' assert event1['_event_type'] == b'AddressRegistered'
def test_endpointregistry(tester_chain, tester_events): account0 = tester.a0 sender = address_encoder(account0) endpointregistry_path = get_contract_path('EndpointRegistry.sol') endpointregistry_compiled = _solidity.compile_contract( endpointregistry_path, "EndpointRegistry") tester_chain.head_state.log_listeners.append(tester_events.append) endpointregistry_address = tester_chain.contract( endpointregistry_compiled['bin'], language='evm') endpoint_registry = tester.ABIContract(tester_chain, endpointregistry_compiled['abi'], endpointregistry_address) endpoint_registry.registerEndpoint('127.0.0.1:4001') assert endpoint_registry.findAddressByEndpoint('127.0.0.1:4001') == sender assert endpoint_registry.findEndpointByAddress(sender) == b'127.0.0.1:4001' endpoint_registry.registerEndpoint('192.168.0.1:4002') assert endpoint_registry.findAddressByEndpoint( '192.168.0.1:4002') == sender assert endpoint_registry.findEndpointByAddress( sender) == b'192.168.0.1:4002' assert len(tester_events) == 2 event0 = event_decoder(tester_events[0], endpoint_registry.translator) event1 = event_decoder(tester_events[1], endpoint_registry.translator) assert event0['_event_type'] == b'AddressRegistered' assert event1['_event_type'] == b'AddressRegistered'
def allcontracts(contract_files): return { "{}:{}".format(c, name_from_file(c)): compile_contract(get_contract_path(c), name_from_file(c), optimize=False) for c in contract_files }
def test_token(): standard_token_path = get_contract_path('StandardToken.sol') human_token_path = get_contract_path('HumanStandardToken.sol') test_chain = tester.Chain() address0 = tester.a0 address1 = tester.a1 standard_token_compiled = _solidity.compile_contract( standard_token_path, "StandardToken" ) standard_token_address = test_chain.contract( standard_token_compiled['bin'], language='evm', sender=tester.k0 ) contract_libraries = { 'StandardToken': hexlify(standard_token_address), } human_token_compiled = _solidity.compile_contract( human_token_path, 'HumanStandardToken', contract_libraries ) ct = ethereum.abi.ContractTranslator(human_token_compiled['abi']) human_token_args = ct.encode_constructor_arguments([10000, 'raiden', 0, 'rd']) human_token_address = test_chain.contract( human_token_compiled['bin'] + human_token_args, language='evm', sender=tester.k0 ) human_token = tester.ABIContract(test_chain, human_token_compiled['abi'], human_token_address) test_chain.mine() # pylint: disable=no-member assert human_token.balanceOf(address0) == 10000 assert human_token.balanceOf(address1) == 0 assert human_token.transfer(address1, 5000) is True assert human_token.balanceOf(address0) == 5000 assert human_token.balanceOf(address1) == 5000
def validate_solc(): if _solidity.get_solidity() is None: raise RuntimeError( "Couldn't find the solc in the current $PATH.\n" "Make sure the solidity compiler is installed and available on your $PATH." ) try: _solidity.compile_contract( get_contract_path('HumanStandardToken.sol'), 'HumanStandardToken', combined='abi', optimize=False, ) except subprocess.CalledProcessError as e: msg = ( 'The solidity compiler failed to execute. Please make sure that you\n' 'are using the binary version of the compiler (solc-js is not compatible)\n' 'and that the version is >= {}'.format(MIN_REQUIRED_SOLC) ) if e.output: msg += ( '\n' 'Output: ' + e.output ) raise RuntimeError(msg) except OSError as e: msg = ( 'The solidity compiler failed to execute. Please make sure the\n' 'binary is compatible with your architecture and you can execute it.' ) child_traceback = getattr(e, 'child_traceback', None) if child_traceback: msg += ( '\n' 'Traceback: ' + child_traceback ) raise RuntimeError(msg)
def deploy_nettingchannel_library(deploy_key, tester_chain): netting_library_path = get_contract_path('NettingChannelLibrary.sol') netting_library_compiled = _solidity.compile_contract( netting_library_path, "NettingChannelLibrary") netting_channel_library_address = tester_chain.contract( netting_library_compiled['bin'], language='evm', sender=deploy_key) tester_chain.mine(number_of_blocks=1) return netting_channel_library_address
def validate_solc(): if _solidity.get_solidity() is None: raise RuntimeError( "Couldn't find the solc in the current $PATH.\n" "Make sure the solidity compiler is installed and available on your $PATH." ) try: _solidity.compile_contract( get_contract_path('HumanStandardToken.sol'), 'HumanStandardToken', combined='abi', optimize=False, ) except subprocess.CalledProcessError as e: msg = ( 'The solidity compiler failed to execute. Please make sure that you\n' 'are using the binary version of the compiler (solc-js is not compatible)\n' ) if e.output: msg += ( '\n' 'Output: ' + e.output ) raise RuntimeError(msg) except OSError as e: msg = ( 'The solidity compiler failed to execute. Please make sure the\n' 'binary is compatible with your architecture and you can execute it.' ) child_traceback = getattr(e, 'child_traceback', None) if child_traceback: msg += ( '\n' 'Traceback: ' + child_traceback ) raise RuntimeError(msg)
def deploy_registry(deploy_key, tester_chain, channel_manager_library_address): registry_path = get_contract_path('Registry.sol') contract_libraries = { 'ChannelManagerLibrary': hexlify(channel_manager_library_address), } registry_compiled = _solidity.compile_contract(registry_path, 'Registry', contract_libraries) registry_address = tester_chain.contract(registry_compiled['bin'], language='evm', sender=deploy_key) tester_chain.mine(number_of_blocks=1) return registry_address
def deploy_nettingchannel_library(deploy_key, tester_chain): netting_library_path = get_contract_path('NettingChannelLibrary.sol') netting_library_compiled = _solidity.compile_contract( netting_library_path, "NettingChannelLibrary" ) netting_channel_library_address = tester_chain.contract( netting_library_compiled['bin'], language='evm', sender=deploy_key ) tester_chain.mine(number_of_blocks=1) return netting_channel_library_address
def get_static_or_compile( contract_path: str, contract_name: str, **compiler_flags): """Search the path of `contract_path` for a file with the same name and the extension `.static-abi.json`. If the file exists, and the recorded checksum matches, this will return the precompiled contract, otherwise it will compile it. Writing compiled contracts to the desired file and path happens only when the environment variable `STORE_PRECOMPILED` is set (to whatever value). Users are not expected to ever set this value, the functionality is exposed through the `setup.py compile_contracts` command. Args: contract_path: the path of the contract file contract_name: the contract name **compiler_flags: flags that will be passed to the compiler """ # this will be set by `setup.py compile_contracts` store_updated = os.environ.get('STORE_PRECOMPILED', False) precompiled = None precompiled_path = '{}.static-abi.json'.format(contract_path) try: with open(precompiled_path) as f: precompiled = json.load(f) except IOError: pass if precompiled or store_updated: checksum = contract_checksum(contract_path) if precompiled and precompiled['checksum'] == checksum: return precompiled validate_solc() compiled = _solidity.compile_contract( contract_path, contract_name, **compiler_flags ) if store_updated: compiled['checksum'] = checksum with open(precompiled_path, 'w') as f: json.dump(compiled, f) print("'{}' written".format(precompiled_path)) return compiled
def deploy_channelmanager_library(deploy_key, tester_chain, tester_nettingchannel_library_address): channelmanager_library_path = get_contract_path( 'ChannelManagerLibrary.sol') contract_libraries = { 'NettingChannelLibrary': hexlify(tester_nettingchannel_library_address), } channelmanager_library_compiled = _solidity.compile_contract( channelmanager_library_path, 'ChannelManagerLibrary', contract_libraries) channelmanager_library_address = tester_chain.contract( channelmanager_library_compiled['bin'], language='evm') tester_chain.mine(number_of_blocks=1) return channelmanager_library_address
def deploy_channelmanager_library(deploy_key, tester_chain, tester_nettingchannel_library_address): channelmanager_library_path = get_contract_path('ChannelManagerLibrary.sol') contract_libraries = { 'NettingChannelLibrary': hexlify(tester_nettingchannel_library_address), } channelmanager_library_compiled = _solidity.compile_contract( channelmanager_library_path, 'ChannelManagerLibrary', contract_libraries ) channelmanager_library_address = tester_chain.contract( channelmanager_library_compiled['bin'], language='evm' ) tester_chain.mine(number_of_blocks=1) return channelmanager_library_address
def deploy_registry(deploy_key, tester_chain, channel_manager_library_address): registry_path = get_contract_path('Registry.sol') contract_libraries = { 'ChannelManagerLibrary': hexlify(channel_manager_library_address), } registry_compiled = _solidity.compile_contract( registry_path, 'Registry', contract_libraries ) registry_address = tester_chain.contract( registry_compiled['bin'], language='evm', sender=deploy_key ) tester_chain.mine(number_of_blocks=1) return registry_address
def test_token_approve(): test_path = os.path.join(os.path.dirname(__file__), 'SimpleApproveTransfer.sol') standard_token_path = get_contract_path('StandardToken.sol') human_token_path = get_contract_path('HumanStandardToken.sol') test_chain = tester.Chain() address0 = tester.a0 address1 = tester.a1 standard_token_compiled = _solidity.compile_contract( standard_token_path, "StandardToken" ) standard_token_address = test_chain.contract( standard_token_compiled['bin'], language='evm', sender=tester.k0 ) contract_libraries = { 'StandardToken': hexlify(standard_token_address), } human_token_compiled = _solidity.compile_contract( human_token_path, 'HumanStandardToken', contract_libraries ) ct = ethereum.abi.ContractTranslator(human_token_compiled['abi']) human_token_args = ct.encode_constructor_arguments([10000, 'raiden', 0, 'rd']) human_token_address = test_chain.contract( human_token_compiled['bin'] + human_token_args, language='evm', sender=tester.k0 ) human_token = tester.ABIContract(test_chain, human_token_compiled['abi'], human_token_address) test_token_compiled = _solidity.compile_contract( test_path, 'SimpleApproveTransfer', contract_libraries ) ct = ethereum.abi.ContractTranslator(test_token_compiled['abi']) test_token_args = ct.encode_constructor_arguments([human_token_address]) test_token_address = test_chain.contract( test_token_compiled['bin'] + test_token_args, language='evm', sender=tester.k0 ) test = tester.ABIContract(test_chain, test_token_compiled['abi'], test_token_address) # pylint: disable=no-member assert human_token.balanceOf(address0) == 10000 assert human_token.balanceOf(address1) == 0 assert human_token.balanceOf(test.address) == 0 assert human_token.allowance(address0, address0) == 0 assert human_token.allowance(address0, address1) == 0 assert human_token.allowance(address0, test.address) == 0 assert human_token.approve(test.address, 5000) is True assert human_token.balanceOf(address0) == 10000 assert human_token.balanceOf(address1) == 0 assert human_token.balanceOf(test.address) == 0 assert human_token.allowance(address0, address0) == 0 assert human_token.allowance(address0, address1) == 0 assert human_token.allowance(address0, test.address) == 5000 assert test.transfer(address1, 2000) is True assert human_token.balanceOf(address0) == 10000 - 2000 assert human_token.balanceOf(address1) == 0 + 2000 assert human_token.balanceOf(test.address) == 0 assert human_token.allowance(address0, address0) == 0 assert human_token.allowance(address0, address1) == 0 assert human_token.allowance(address0, test.address) == 5000 - 2000