def call_const_function( self, priv_key, value, contract_hash, contract_abi, function_name, eth_args ): # src_address = b2h(utils.privtoaddr(priv_key)) translator = ContractTranslator(json.loads(contract_abi)) call = translator.encode_function_call(function_name, eth_args) # nonce = get_num_transactions(src_address) # gas_price = get_gas_price_in_wei() # start_gas = eval_startgas( # src_address, contract_hash, value, b2h(call), gas_price) # nonce = int(nonce, base=16) # gas_price = int(gas_price, base=16) # start_gas = int(start_gas, base=16) + 100000 # start_gas = 7612288 params = { # "from": "0x" + src_address, "to": "0x" + contract_hash, # "gas": "0x" + "%x" % start_gas, # "gasPrice": "0x" + "%x" % gas_price, # "value": "0x" + str(value), "data": "0x" + b2h(call), } return_value = self._json_call("eth_call", [params, "latest"]) # print return_value return_value = h2b(return_value[2:]) # remove 0x return translator.decode_function_result(function_name, return_value)
async def test_deploy_contract(self, *, node): client = JsonRPCClient(node.dsn()['url']) sourcecode = b"contract greeter{string greeting;function greeter(string _greeting) public{greeting=_greeting;}function greet() constant returns (string){return greeting;}}" #source_fn = os.path.join(node.get_data_directory(), 'greeting.sol') #with open(source_fn, 'wb') as wf: # wf.write(sourcecode) source_fn = '<stdin>' contract_name = 'greeter' constructor_args = [b'hello world!'] args = ['solc', '--combined-json', 'bin,abi', '--add-std'] # , source_fn] #output = subprocess.check_output(args, stderr=subprocess.PIPE) process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, stderrdata = process.communicate(input=sourcecode) output = json_decode(output) contract = output['contracts']['{}:{}'.format(source_fn, contract_name)] bytecode = data_decoder(contract['bin']) contract_interface = json_decode(contract['abi']) translator = ContractTranslator(contract_interface) constructor_call = translator.encode_constructor_arguments( constructor_args) bytecode += constructor_call tx_hash, contract_address = await self.deploy_contract(bytecode) tx_receipt = await client.eth_getTransactionReceipt(tx_hash) self.assertIsNotNone(tx_receipt) code = await client.eth_getCode(contract_address) self.assertIsNotNone(code) self.assertNotEqual(data_decoder(code), b'') # call the contract and check the result res = await client.eth_call( from_address='0x39bf9e501e61440b4b268d7b2e9aa2458dd201bb', to_address=contract_address, data=sha3('greet()')) result = translator.decode_function_result('greet', data_decoder(res)) self.assertEqual(result[0], constructor_args[0])
class Repo(object): def __init__(self, account, infura, bin_path, abi_path): self.account = account self.infura = infura with open(bin_path, 'rb') as contract_file: contract_bin = contract_file.read().replace(b"\n", b"") self.contract_bin = binascii.unhexlify(contract_bin) with open(abi_path, 'r') as abi_file: self.contract = ContractTranslator(json.load(abi_file)) def create(self, ref_head, gasprice, startgas): constructor_args = self.contract.encode_constructor_arguments([ref_head]) tx, response = self.account.send_transaction(gasprice, startgas, self.contract_bin + constructor_args) contract_address = '0x' + encode_hex(tx.creates) return contract_address, response.json() def transact(self, contract_address, function, args, value=0, gasprice=15000000000, gaslimit=140000): txdata = self.contract.encode_function_call(function, args) tx, response = self.account.send_transaction(gasprice, gaslimit, txdata, to=contract_address, value=value) return response.json() def call(self, contract_address, function, args): txdata = self.contract.encode_function_call(function, args) params = { "params": json.dumps([{ "to": contract_address, "data": "0x" + encode_hex(txdata) }, 'latest']) } response = self.infura.get('eth_call', params).json() if 'result' in response and response['result'] == '0x': response['result'] = [] elif 'result' in response: response['result'] = self.contract.decode_function_result(function, binascii.unhexlify(response['result'][2:])) return response