def test_with_link(self, artifact, links): unlinked_bytecode = artifact['bin'] get_link_address = self.get_links_address(links) linked_bytecode = link_code(unlinked_bytecode, get_link_address) try: contract_factory = self.web3.eth.contract(abi=artifact['abi'], bytecode=linked_bytecode) except ValueError as valueError: value_error = str(valueError.args.__getitem__(0)) if "'" in value_error and not self.more: error = str(value_error).split("'") console_log(str(error[0]), "error", "ValueError") elif "'" in value_error and self.more: console_log( str(value_error), "error", "ValueError") elif not self.more: console_log( str(value_error).strip('\n')[0], "error", "ValueError") elif self.more: console_log( str(value_error), "error", "ValueError") sys.exit() # Get transaction hash tx_hash = contract_factory.constructor().transact() address = self.web3.eth.getTransactionReceipt(tx_hash)['contractAddress'] contract = {"abi": artifact['abi'], "bytecode": linked_bytecode} contract_name_and_address = artifact['contractName'] + ":" + str(address) self.contracts.setdefault(contract_name_and_address, contract)
def deploy(w3, source, contractName="", mode='file'): contract = None subaddrs = {} contractInfo = {} try: if contractName is None or contractName == "": contractName = os.path.splitext(os.path.os.path.basename(source))[0] contracts = compiler(source, mode) for cont in contracts: if '__' not in cont['bytecode']: # 生成合约对象 Contract = w3.eth.contract(abi=cont['abi'], bytecode=cont['bytecode']) # 部署合约 txhash = Contract.constructor().transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) # print(txreceipt) if txreceipt['status'] == 1: subaddrs[cont['name']] = txreceipt.contractAddress contractInfo[cont['name']] = { 'name': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi'], 'bytecode': cont['bytecode'] } # print(tx_receipt) for cont in contracts: if '__' in cont['bytecode']: abi = cont['abi'] bytecode = link_code(cont['bytecode'], subaddrs) # 生成合约对象 Contract = w3.eth.contract(abi=abi, bytecode=bytecode) # 部署合约 txhash = Contract.constructor().transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) # print(txreceipt) if txreceipt['status'] == 1: subaddrs[cont['name']] = txreceipt.contractAddress # 将合约地址和abi进行json化保存到本地文件 # print(bytecode) contractInfo[cont['name']] = { 'name': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi'], 'bytecode': bytecode } with open('./' + contractName + '.json', 'w', encoding='utf-8') as wf: wf.write(json.dumps(contractInfo)) if os.path.exists('./' + contractName + '.json'): contract = getContract('./' + contractName + '.json') except Exception as ex: print('deploy error: ' + str(ex)) return contract
def deploy(w3, contracts, params, output, abigen, consarg="1.0"): subaddrs = {} contractInfo = {} for cont in contracts: if '__' not in cont['bytecode']: # 生成合约对象 Contract = w3.eth.contract(abi=cont['abi'], bytecode=cont['bytecode']) # 部署合约 if consarg is None: txhash = Contract.constructor().transact(params) else: txhash = Contract.constructor(consarg).transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) # print(txreceipt) if txreceipt['status'] == 1: subaddrs[cont['name']] = txreceipt.contractAddress contractInfo[cont['name']] = { 'name': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi'], 'bytecode': cont['bytecode'] } # print(tx_receipt) for cont in contracts: if '__' in cont['bytecode']: abi = cont['abi'] bytecode = link_code(cont['bytecode'], subaddrs) # 生成合约对象 Contract = w3.eth.contract(abi=abi, bytecode=bytecode) # 部署合约 if consarg is None: txhash = Contract.constructor().transact(params) else: txhash = Contract.constructor(consarg).transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) # print(txreceipt) if txreceipt['status'] == 1: subaddrs[cont['name']] = txreceipt.contractAddress # 将合约地址和abi进行json化保存到本地文件 # print(bytecode) contractInfo[cont['name']] = { 'name': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi'], 'bytecode': bytecode } if abigen != '': codegen(tuple(contractInfo.values())[-1], output, abigen) if output != '': with open(output, 'w', encoding='utf-8') as wf: wf.write(json.dumps(contractInfo)) return contractInfo
def test_with_link(self, artifact, links): unlinked_bytecode = artifact['bin'] get_link_address = self.get_links_address(links) linked_bytecode = link_code(unlinked_bytecode, get_link_address) contract_factory = self.web3.eth.contract(abi=artifact['abi'], bytecode=linked_bytecode) tx_hash = contract_factory.constructor().transact() address = self.web3.eth.getTransactionReceipt(tx_hash)['contractAddress'] contract = {"abi": artifact['abi'], "bytecode": linked_bytecode} contract_name_and_address = artifact['contractName'] + ":" + str(address) self.contracts.setdefault(contract_name_and_address, contract)
def deploy_n_transact(file_path, mappings=[]): # compile all files contracts = compile_files(file_path, import_remappings=mappings) link_add = {} contract_interface, links = separate_main_n_link(file_path, contracts) # first deploy all link libraries for link in links: link_add[link] = deploy_contract(links[link]) # now link dependent library code to main contract binary # https://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html?highlight=library if link_add: contract_interface['bin'] = link_code(contract_interface['bin'], link_add) # return contract receipt and abi(application binary interface) return deploy_contract(contract_interface), contract_interface['abi']
def test_full_code_linking(): output = link_code( CODE, { 'TestA': TEST_A_ADDRESS, 'TestB': TEST_B_ADDRESS, 'TestC': TEST_C_ADDRESS, }) assert '__TestA__' not in output assert '__TestB__' not in output assert '__TestC__' not in output assert TEST_A_ADDRESS[2:] in output assert TEST_B_ADDRESS[2:] in output assert TEST_C_ADDRESS[2:] in output assert output == LINKED_CODE
def deploy(csc, w3): # 编译合约源码 compiledSol = compile_source(csc) # contractId, contractInterface = compiledSol.popitem() contracts = [] subaddrs = {} contractInfo = {} for contractId, contractInterface in compiledSol.items(): ctt = {} ast = contractInterface['ast']['children'] for item in ast: if len(item['attributes'].keys()) > 2: if str(contractId).split(':')[-1] == str(item['attributes']['name']): ctt['name'] = contractId ctt['type'] = item['attributes']['contractKind'] ctt['abi'] = contractInterface['abi'] ctt['bytecode'] = contractInterface['bin'] contracts.append(ctt) # contractInterface = compiledSol['<stdin>:' + cname] for cont in contracts: if '__' not in cont['bytecode']: # 生成合约对象 Contract = w3.eth.contract(abi=cont['abi'], bytecode=cont['bytecode']) # 部署合约 txhash = Contract.constructor().transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) subaddrs[cont['name']] = txreceipt.contractAddress contractInfo[cont['name']] = {'id': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi']} # print(tx_receipt) for cont in contracts: if '__' in cont['bytecode']: abi = cont['abi'] bytecode = link_code(cont['bytecode'], subaddrs) # 生成合约对象 Contract = w3.eth.contract(abi=abi, bytecode=bytecode) # 部署合约 txhash = Contract.constructor().transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) subaddrs[cont['name']] = txreceipt.contractAddress # 将合约地址和abi进行json化保存到本地文件 print(bytecode) contractInfo[cont['name']] = {'id': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi']} with open('./test.json', 'w') as wf: wf.write(json.dumps(contractInfo)) return contractInfo
def create_contract(path, args=(), sender=ethtester.k0, libraries=dict()): abi, hexcode = deployer.builder.get_contract_data(path) encoded_args = ( ContractTranslator(abi).encode_constructor_arguments(args) if args else b'') libraries = _encode_libs(libraries) linked_hexcode = link_code(hexcode, libraries) code = ethutils.decode_hex(linked_hexcode) + encoded_args address = ethtester.chain.tx(sender=sender, to=b'', startgas=START_GAS, data=code) return ethtester.ABIContract(ethtester.chain, abi, address)
def test_full_code_linking(): raw_output = link_code(CODE, { 'TestA': TEST_A_ADDRESS, 'TestB': TEST_B_ADDRESS, 'TestC': TEST_C_ADDRESS, }) output, _message = raw_output.splitlines() assert '__TestA__' not in output assert '__TestB__' not in output assert '__TestC__' not in output assert TEST_A_ADDRESS[2:] in output assert TEST_B_ADDRESS[2:] in output assert TEST_C_ADDRESS[2:] in output assert output == LINKED_CODE
def deploy(w3, contracts, params, output, java): subaddrs = {} contractInfo = {} for cont in contracts: if '__' not in cont['bytecode']: # 生成合约对象 Contract = w3.eth.contract(abi=cont['abi'], bytecode=cont['bytecode']) # 部署合约 txhash = Contract.constructor("0x881c69043cD24c153782010DdDF9f0eCd4f0dde5").transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) # print(txreceipt) if txreceipt['status'] == 1: subaddrs[cont['name']] = txreceipt.contractAddress contractInfo[cont['name']] = { 'name': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi'], 'bytecode': cont['bytecode'] } # print(tx_receipt) for cont in contracts: if '__' in cont['bytecode']: abi = cont['abi'] bytecode = link_code(cont['bytecode'], subaddrs) # 生成合约对象 Contract = w3.eth.contract(abi=abi, bytecode=bytecode) # 部署合约 txhash = Contract.constructor().transact(params) # 等待合约部署交易完成 txreceipt = w3.eth.waitForTransactionReceipt(txhash) # print(txreceipt) if txreceipt['status'] == 1: subaddrs[cont['name']] = txreceipt.contractAddress # 将合约地址和abi进行json化保存到本地文件 # print(bytecode) contractInfo[cont['name']] = { 'name': cont['name'], 'address': txreceipt.contractAddress, 'abi': cont['abi'], 'bytecode': bytecode } if java != '': javait(tuple(contractInfo.values())[-1], output, java) if output != '': with open(output, 'w', encoding='utf-8') as wf: wf.write(json.dumps(contractInfo)) return contractInfo
def deploy_contract(contract_interface): # Instantiate and deploy contract contract = w3.eth.contract( abi=contract_interface['abi'], bytecode=contract_interface['bin'] ) # Get transaction hash from deployed contract tx_hash =contract.deploy(transaction{'from':w3.eth.accounts[1]}) # Get tx receipt to get contract address tx_receipt = w3.eth.getTransactionReceipt(tx_hash) return tx_receipt['contractAddress'] # compile all contract files contracts = compile_files(['user.sol', 'stringUtils.sol']) # separate main file and link file main_contract = contracts.pop("user.sol:userRecords") library_link = contracts.pop("stringUtils.sol:StringUtils") # print bin part in console you will see 'stringUtils' in that we need to link library address in that bin code. # to that we have to deploy library code first then link it library_address = { "stringUtils.sol:StringUtils": deploy_contract(library_link) } main_contract['bin'] = link_code( main_contract['bin'], library_address) # add abi(application binary interface) and transaction reciept in json file with open('data.json', 'w') as outfile: data = { "abi": main_contract['abi'], "contract_address": deploy_contract(main_contract) } json.dump(data, outfile, indent=4, sort_keys=True)
) tx_hash =contract.constructor().transact(transaction={'from':w3.eth.accounts[0]}) tx_receipt = w3.eth.getTransactionReceipt(tx_hash) return tx_receipt['contractAddress'] contracts = compile_files(['../contracts/Deployment.sol','../contracts/TicTacToe.sol']) for i in contracts: print(i) game_contract = contracts.pop("../contracts/TicTacToe.sol:Tic") deployment_link = contracts.pop("../contracts/Deployment.sol:DeployTic") deployment_address = { "Deployment.sol:DeployTic": deploy_contract(deployment_link) } game_contract['bin'] = link_code(game_contract['bin'],deployment_address) with open('data.json', 'w') as outfile: data = { "abi": game_contract['abi'], "contract_address": deploy_contract(game_contract) } json.dump(data, outfile, indent=4, sort_keys=True) ########################### # ROUTES ########################### @app.before_first_request def initialize():
def test_partial_code_linking(): output = link_code(CODE, {'TestA': TEST_A_ADDRESS}) assert '__TestA__' not in output assert '__TestB__' in output assert '__TestC__' in output assert TEST_A_ADDRESS[2:] in output