def multiply_13(testrpc_chain, library_13): chain = testrpc_chain web3 = chain.web3 Multiply13 = chain.contract_factories['Multiply13'] code = link_bytecode(Multiply13.code, Library13=library_13.address) code_runtime = link_bytecode(Multiply13.code_runtime, Library13=library_13.address) LinkedMultiply13 = chain.web3.eth.contract( abi=Multiply13.abi, code=code, code_runtime=code_runtime, source=Multiply13.source, ) multiply_13_deploy_txn_hash = LinkedMultiply13.deploy() multiply_13_deploy_txn = web3.eth.getTransaction( multiply_13_deploy_txn_hash) multiply_13_address = chain.wait.for_contract_address( multiply_13_deploy_txn_hash, timeout=30) assert multiply_13_deploy_txn['input'] == LinkedMultiply13.code assert web3.eth.getCode( multiply_13_address) == LinkedMultiply13.code_runtime return LinkedMultiply13(address=multiply_13_address)
def do_verify(deploy_operation): assert '__Library13__' in MULTIPLY_13['code'] assert '__Library13__' in MULTIPLY_13['code_runtime'] operation_receipt = deploy_operation.execute( chain, compiled_contracts={ 'Multiply13': MULTIPLY_13, 'Library13': LIBRARY_13, }, ) deploy_txn_hash = operation_receipt['deploy-transaction-hash'] deploy_txn = web3.eth.getTransaction(deploy_txn_hash) contract_address = chain.wait.for_contract_address(deploy_txn_hash, timeout=30) expected_code = link_bytecode( MULTIPLY_13['code'], Library13=library_13.address, ) expected_runtime = link_bytecode( MULTIPLY_13['code_runtime'], Library13=library_13.address, ) assert '__Library13__' not in expected_code assert '__Library13__' not in expected_runtime assert deploy_txn['input'] == expected_code assert web3.eth.getCode(contract_address) == expected_runtime contract = Multiply13(address=contract_address) should_be_39 = contract.call().multiply13(3) assert should_be_39 == 39
def deploy_contract(chain, contract_name, contract_factory=None, deploy_transaction=None, deploy_arguments=None, link_dependencies=None): if contract_factory is None: contract_factory = chain.contract_factories[contract_name] web3 = chain.web3 code = link_bytecode(contract_factory.code, **link_dependencies) code_runtime = link_bytecode(contract_factory.code_runtime, **link_dependencies) ContractFactory = web3.eth.contract( abi=contract_factory.abi, code=code, code_runtime=code_runtime, source=contract_factory.source, ) deploy_transaction_hash = ContractFactory.deploy( deploy_transaction, deploy_arguments, ) return deploy_transaction_hash, ContractFactory
def get_contract_from_registrar(chain, contract_name, contract_factory, link_dependencies=None): if link_dependencies is None: link_dependencies = {} web3 = chain.web3 registrar = chain.registrar registrar_key = 'contract/{name}'.format(name=contract_name) if not registrar.call().exists(registrar_key): return None contract_address = registrar.call().getAddress(registrar_key) expected_runtime = link_bytecode( contract_factory.code_runtime, **link_dependencies ) actual_runtime = web3.eth.getCode(contract_address) # If the runtime doesn't match then don't choose it. if actual_runtime != expected_runtime: return None return contract_factory(address=contract_address)
def get_contract_from_registrar(chain, contract_name, contract_factory, link_dependencies=None): if link_dependencies is None: link_dependencies = {} web3 = chain.web3 registrar = chain.registrar registrar_key = 'contract/{name}'.format(name=contract_name) if not registrar.call().exists(registrar_key): return None contract_address = registrar.call().getAddress(registrar_key) expected_runtime = link_bytecode(contract_factory.code_runtime, **link_dependencies) actual_runtime = web3.eth.getCode(contract_address) # If the runtime doesn't match then don't choose it. if actual_runtime != expected_runtime: return None return contract_factory(address=contract_address)
def test_get_contract_factory_with_declared_dependency(testrpc_chain): chain = testrpc_chain MULTIPLY_13 = chain.project.compiled_contracts['Multiply13'] Multiply13 = chain.get_contract_factory( 'Multiply13', link_dependencies={ 'Library13': '0xd3cda913deb6f67967b99d67acdfa1712c293601', }, ) expected_code = link_bytecode(MULTIPLY_13['code'], Library13='0xd3cda913deb6f67967b99d67acdfa1712c293601') expected_runtime = link_bytecode(MULTIPLY_13['code_runtime'], Library13='0xd3cda913deb6f67967b99d67acdfa1712c293601') assert Multiply13.code == expected_code assert Multiply13.code_runtime == expected_runtime
def _link_code(self, bytecodes, link_dependencies=None, validate_bytecode=True, raise_on_error=False): if link_dependencies is None: link_dependencies = {} library_dependencies = set( itertools.chain.from_iterable( self._extract_library_dependencies(bytecode, link_dependencies) for bytecode in bytecodes)) # Determine which dependencies would need to be present in the # registrar since they were not explicitely declared via the # `link_dependencies`. registrar_dependencies = set(library_dependencies).difference( link_dependencies.keys()) if registrar_dependencies and not self.has_registrar: raise NoKnownAddress( "Unable to link bytecode. Addresses for the following link " "dependencies were not provided and this chain does not have a " "registrar.\n\n{0}".format( ', '.join(registrar_dependencies), )) # Loop over all of the dependencies and ensure that they are available, # raising an exception if they are not. for library_name in registrar_dependencies: self.is_contract_available( library_name, link_dependencies=link_dependencies, validate_bytecode=validate_bytecode, raise_on_error=True, ) registrar_link_dependencies = { library_name: self.get_contract( library_name, link_dependencies=link_dependencies, validate_bytecode=validate_bytecode, ).address for library_name in registrar_dependencies } all_link_dependencies = { library_name: library_address for library_name, library_address in itertools.chain( link_dependencies.items(), registrar_link_dependencies.items(), ) } linked_bytecodes = [ link_bytecode(bytecode, **all_link_dependencies) for bytecode in bytecodes ] return linked_bytecodes
def test_get_contract_factory_with_registrar_dependency(testrpc_chain, library_13): chain = testrpc_chain registrar = chain.registrar register_txn_hash = registrar.transact().setAddress( 'contract/Library13', library_13.address, ) chain.wait.for_receipt(register_txn_hash) MULTIPLY_13 = chain.project.compiled_contracts['Multiply13'] Multiply13 = chain.get_contract_factory('Multiply13') expected_code = link_bytecode(MULTIPLY_13['code'], Library13=library_13.address) expected_runtime = link_bytecode(MULTIPLY_13['code_runtime'], Library13=library_13.address) assert Multiply13.code == expected_code assert Multiply13.code_runtime == expected_runtime
def multiply_13(testrpc_chain, library_13): chain = testrpc_chain web3 = chain.web3 Multiply13 = chain.contract_factories['Multiply13'] code = link_bytecode(Multiply13.code, Library13=library_13.address) code_runtime = link_bytecode(Multiply13.code_runtime, Library13=library_13.address) LinkedMultiply13 = chain.web3.eth.contract( abi=Multiply13.abi, code=code, code_runtime=code_runtime, source=Multiply13.source, ) multiply_13_deploy_txn_hash = LinkedMultiply13.deploy() multiply_13_deploy_txn = web3.eth.getTransaction(multiply_13_deploy_txn_hash) multiply_13_address = chain.wait.for_contract_address(multiply_13_deploy_txn_hash, timeout=30) assert multiply_13_deploy_txn['input'] == LinkedMultiply13.code assert web3.eth.getCode(multiply_13_address) == LinkedMultiply13.code_runtime return LinkedMultiply13(address=multiply_13_address)