From 3966e1628d37bd74f1f4611867f7a7a4ca3308b6 Mon Sep 17 00:00:00 2001 From: Jim Berry Date: Tue, 15 Sep 2015 14:01:26 -0400 Subject: [PATCH 1/2] Changes from the etherpoker project --- ethjsonrpc/client.py | 67 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/ethjsonrpc/client.py b/ethjsonrpc/client.py index ff96ed5..ec0e07e 100644 --- a/ethjsonrpc/client.py +++ b/ethjsonrpc/client.py @@ -85,9 +85,28 @@ def _encode_function(self, signature, param_values): encoded_params = encode_abi(types, param_values) return utils.zpad(utils.encode_int(prefix), 4) + encoded_params - def _install_contract(self, language, contract_code, value=0, from_address=None, gas=None, gas_price=None): - byte_code = self.compilers[language](contract_code) - return self.eth_sendTransaction(data=byte_code, value=value, from_address=from_address, gas=gas, gas_price=gas_price) +# def _install_contract(self, language, contract_code, value=0, from_address=None, gas=None, gas_price=None): +# byte_code = self.compilers[language](contract_code) +# return self.eth_sendTransaction(data=byte_code, value=value, from_address=from_address, gas=gas, gas_price=gas_price) + + def _install_contract(self, language, contract_code, value=0, from_address=None, + gas=None, gas_price=None, ctor_sig=None, ctor_params=None, byte_data=None): + ''' + Compiles a contract and then sends a transaction to create it - maybe passing parameters + to the constructor if ctor_sig and ctor_params are provided. + If byte_data is provided it is sent as-is rather than performing any compilation + ''' + if not byte_data: + byte_data = self.compilers[language](contract_code) + + # constructor parameters> + if ctor_sig: + types = ctor_sig[ctor_sig.find('(') + 1: ctor_sig.find(')')].split(',') + encoded_params = encode_abi(types, ctor_params) + byte_data = byte_data + encoded_params + + return self.eth_sendTransaction(data=byte_data, value=value, from_address=from_address, gas=gas, gas_price=gas_price) + def install_solidity_contract(self, contract_code, value=0, from_address=None, gas=None, gas_price=None): ''' @@ -107,8 +126,29 @@ def install_lll_contract(self, contract_code, value=0, from_address=None, gas=No ''' return self._install_contract('lll', contract_code, value, from_address, gas, gas_price) - def contract_instant_call(self, to_address, function_signature, function_parameters=None, result_types=None, default_block=BLOCK_TAG_LATEST): - ''' +# def contract_instant_call(self, to_address, function_signature, function_parameters=None, result_types=None, default_block=BLOCK_TAG_LATEST): +# ''' +# This method makes a instant call on a contract function without the need to have the contract source code. +# Examples of function_signature in solidity: +# mult(uint x, uint y) => sig: mult(uint256,uint256) (all uint should be transformed to uint256) +# setAddress(address entity_address) => sig:setAddress(address) +# doSomething() => sig: doSomething() (functions with no parameters must end with the '()') +# In serpent, all functions parameter signatures are int256. Example: +# setXYZ(x, y, z) => sig: setXYZ(int256,int256,int256) +# ''' +# data = self._encode_function(function_signature, function_parameters) +# params = [ +# { +# 'to': to_address, +# 'data': '0x{0}'.format(data.encode('hex')) +# }, +# default_block +# ] +# response = self._call('eth_call', params) +# return decode_abi(result_types, response[2:].decode('hex')) + + def contract_instant_call(self, to_address, function_signature, function_parameters=None, result_types=None, default_block=BLOCK_TAG_LATEST, from_address=None): + """ This method makes a instant call on a contract function without the need to have the contract source code. Examples of function_signature in solidity: mult(uint x, uint y) => sig: mult(uint256,uint256) (all uint should be transformed to uint256) @@ -116,7 +156,7 @@ def contract_instant_call(self, to_address, function_signature, function_paramet doSomething() => sig: doSomething() (functions with no parameters must end with the '()') In serpent, all functions parameter signatures are int256. Example: setXYZ(x, y, z) => sig: setXYZ(int256,int256,int256) - ''' + """ data = self._encode_function(function_signature, function_parameters) params = [ { @@ -125,9 +165,15 @@ def contract_instant_call(self, to_address, function_signature, function_paramet }, default_block ] - response = self._call('eth_call', params) + + if from_address: + params[0]['from'] = from_address + + response = self._call('eth_call', params) return decode_abi(result_types, response[2:].decode('hex')) + + def contract_transaction_call(self, to_address, function_signature, function_parameters=None, from_address=None, gas=None, gas_price=None, default_block=BLOCK_TAG_LATEST): ''' This method makes a call on a contract function through a transaction. Returns the transaction_id. @@ -141,12 +187,12 @@ def contract_transaction_call(self, to_address, function_signature, function_par # Default values for gas and gas_price gas = gas or self.DEFAULT_GAS_FOR_TRANSACTIONS gas_price = gas_price or self.DEFAULT_GAS_PRICE - + # Default value for from_address from_address = from_address or self.eth_accounts()[0] - + data = self._encode_function(function_signature, function_parameters) - + params = { 'from': from_address, 'to': to_address, @@ -158,6 +204,7 @@ def contract_transaction_call(self, to_address, function_signature, function_par response = self._call('eth_sendTransaction', [params]) return response + def create_contract(self, contract_code, value=0, from_address=None, gas=None, gas_price=None): self.update_code(contract_code) byte_code = serpent.compile(contract_code) From 5b52cbd6011b479af168df92819f705ca24ada28 Mon Sep 17 00:00:00 2001 From: Jim Berry Date: Tue, 15 Sep 2015 14:11:24 -0400 Subject: [PATCH 2/2] Formatting changes to match repo style --- ethjsonrpc/client.py | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/ethjsonrpc/client.py b/ethjsonrpc/client.py index ec0e07e..4ffbfa6 100644 --- a/ethjsonrpc/client.py +++ b/ethjsonrpc/client.py @@ -84,11 +84,7 @@ def _encode_function(self, signature, param_values): types = signature[signature.find('(') + 1: signature.find(')')].split(',') encoded_params = encode_abi(types, param_values) return utils.zpad(utils.encode_int(prefix), 4) + encoded_params - -# def _install_contract(self, language, contract_code, value=0, from_address=None, gas=None, gas_price=None): -# byte_code = self.compilers[language](contract_code) -# return self.eth_sendTransaction(data=byte_code, value=value, from_address=from_address, gas=gas, gas_price=gas_price) - + def _install_contract(self, language, contract_code, value=0, from_address=None, gas=None, gas_price=None, ctor_sig=None, ctor_params=None, byte_data=None): ''' @@ -106,8 +102,7 @@ def _install_contract(self, language, contract_code, value=0, from_address=None, byte_data = byte_data + encoded_params return self.eth_sendTransaction(data=byte_data, value=value, from_address=from_address, gas=gas, gas_price=gas_price) - - + def install_solidity_contract(self, contract_code, value=0, from_address=None, gas=None, gas_price=None): ''' Installs a solidity contract into ethereum node @@ -126,29 +121,8 @@ def install_lll_contract(self, contract_code, value=0, from_address=None, gas=No ''' return self._install_contract('lll', contract_code, value, from_address, gas, gas_price) -# def contract_instant_call(self, to_address, function_signature, function_parameters=None, result_types=None, default_block=BLOCK_TAG_LATEST): -# ''' -# This method makes a instant call on a contract function without the need to have the contract source code. -# Examples of function_signature in solidity: -# mult(uint x, uint y) => sig: mult(uint256,uint256) (all uint should be transformed to uint256) -# setAddress(address entity_address) => sig:setAddress(address) -# doSomething() => sig: doSomething() (functions with no parameters must end with the '()') -# In serpent, all functions parameter signatures are int256. Example: -# setXYZ(x, y, z) => sig: setXYZ(int256,int256,int256) -# ''' -# data = self._encode_function(function_signature, function_parameters) -# params = [ -# { -# 'to': to_address, -# 'data': '0x{0}'.format(data.encode('hex')) -# }, -# default_block -# ] -# response = self._call('eth_call', params) -# return decode_abi(result_types, response[2:].decode('hex')) - def contract_instant_call(self, to_address, function_signature, function_parameters=None, result_types=None, default_block=BLOCK_TAG_LATEST, from_address=None): - """ + ''' This method makes a instant call on a contract function without the need to have the contract source code. Examples of function_signature in solidity: mult(uint x, uint y) => sig: mult(uint256,uint256) (all uint should be transformed to uint256) @@ -156,7 +130,7 @@ def contract_instant_call(self, to_address, function_signature, function_paramet doSomething() => sig: doSomething() (functions with no parameters must end with the '()') In serpent, all functions parameter signatures are int256. Example: setXYZ(x, y, z) => sig: setXYZ(int256,int256,int256) - """ + ''' data = self._encode_function(function_signature, function_parameters) params = [ {