def format_data_for_call(sender='', to='', value=0, data='', startgas=GAS_PRICE, gasprice=GAS_PRICE): """ Helper to format the transaction data. """ json_data = {} if sender is not None: json_data['from'] = address_encoder(sender) if to is not None: json_data['to'] = data_encoder(to) if value is not None: json_data['value'] = quantity_encoder(value) if gasprice is not None: json_data['gasPrice'] = quantity_encoder(gasprice) if startgas is not None: json_data['gas'] = quantity_encoder(startgas) if data is not None: json_data['data'] = data_encoder(data) return json_data
def eth_sendTransaction( self, nonce=None, sender='', to='', value=0, data='', gasPrice=GAS_PRICE, gas=GAS_PRICE): """ Creates new message call transaction or a contract creation, if the data field contains code. Args: sender (address): The 20 bytes address the transaction is sent from. to (address): DATA, 20 Bytes - (optional when creating new contract) The address the transaction is directed to. gas (int): Gas provided for the transaction execution. It will return unused gas. gasPrice (int): gasPrice used for each unit of gas paid. value (int): Value sent with this transaction. data (bin): The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. nonce (int): This allows to overwrite your own pending transactions that use the same nonce. """ if to == '' and data.isalnum(): warnings.warn( 'Verify that the data parameter is _not_ hex encoded, if this is the case ' 'the data will be double encoded and result in unexpected ' 'behavior.' ) if to == '0' * 40: warnings.warn('For contract creation the empty string must be used.') if sender is None: raise ValueError('sender needs to be provided.') json_data = { 'to': data_encoder(normalize_address(to, allow_blank=True)), 'value': quantity_encoder(value), 'gasPrice': quantity_encoder(gasPrice), 'gas': quantity_encoder(gas), 'data': data_encoder(data), 'from': address_encoder(sender), } if nonce is not None: json_data['nonce'] = quantity_encoder(nonce) res = self.call('eth_sendTransaction', json_data) return data_decoder(res)
def format_data_for_call(sender: address = b'', to: address = b'', value: int = 0, data: bytes = b'', startgas: int = GAS_LIMIT, gasprice: int = GAS_PRICE): """ Helper to format the transaction data. """ return { 'from': address_encoder(sender), 'to': data_encoder(to), 'value': quantity_encoder(value), 'gasPrice': quantity_encoder(gasprice), 'gas': quantity_encoder(startgas), 'data': data_encoder(data) }
def format_data_for_call( sender: address = b'', to: address = b'', value: int = 0, data: bytes = b'', startgas: int = GAS_PRICE, gasprice: int = GAS_PRICE): """ Helper to format the transaction data. """ return { 'from': address_encoder(sender), 'to': data_encoder(to), 'value': quantity_encoder(value), 'gasPrice': quantity_encoder(gasprice), 'gas': quantity_encoder(startgas), 'data': data_encoder(data) }
def eth_sendTransaction( self, sender: address = b'', to: address = b'', value: int = 0, data: bytes = b'', gasPrice: int = GAS_PRICE, gas: int = GAS_PRICE, nonce: Optional[int] = None): """ Creates new message call transaction or a contract creation, if the data field contains code. Args: sender: The address the transaction is sent from. to: The address the transaction is directed to. (optional when creating new contract) gas: Gas provided for the transaction execution. It will return unused gas. gasPrice: gasPrice used for each unit of gas paid. value: Value sent with this transaction. data: The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. nonce: This allows to overwrite your own pending transactions that use the same nonce. """ if to == b'' and data.isalnum(): warnings.warn( 'Verify that the data parameter is _not_ hex encoded, if this is the case ' 'the data will be double encoded and result in unexpected ' 'behavior.' ) if to == b'0' * 40: warnings.warn('For contract creation the empty string must be used.') if sender is None: raise ValueError('sender needs to be provided.') json_data = format_data_for_call( sender, to, value, data, gas, gasPrice ) if nonce is not None: json_data['nonce'] = quantity_encoder(nonce) res = self.call('eth_sendTransaction', json_data) return data_decoder(res)
def eth_sendTransaction(self, sender: address = b'', to: address = b'', value: int = 0, data: bytes = b'', gasPrice: int = GAS_PRICE, gas: int = GAS_PRICE, nonce: Optional[int] = None): """ Creates new message call transaction or a contract creation, if the data field contains code. Args: sender: The address the transaction is sent from. to: The address the transaction is directed to. (optional when creating new contract) gas: Gas provided for the transaction execution. It will return unused gas. gasPrice: gasPrice used for each unit of gas paid. value: Value sent with this transaction. data: The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. nonce: This allows to overwrite your own pending transactions that use the same nonce. """ if to == b'' and data.isalnum(): warnings.warn( 'Verify that the data parameter is _not_ hex encoded, if this is the case ' 'the data will be double encoded and result in unexpected ' 'behavior.') if to == b'0' * 40: warnings.warn( 'For contract creation the empty string must be used.') if sender is None: raise ValueError('sender needs to be provided.') json_data = format_data_for_call(sender, to, value, data, gas, gasPrice) if nonce is not None: json_data['nonce'] = quantity_encoder(nonce) res = self.call('eth_sendTransaction', json_data) return data_decoder(res)
def filter_changes(self, fid: int) -> List: changes = self.call('eth_getFilterChanges', quantity_encoder(fid)) if not changes: return list() assert isinstance(changes, list) decoders = { 'blockHash': data_decoder, 'transactionHash': data_decoder, 'data': data_decoder, 'address': address_decoder, 'topics': lambda x: [topic_decoder(t) for t in x], 'blockNumber': quantity_decoder, 'logIndex': quantity_decoder, 'transactionIndex': quantity_decoder } return [{k: decoders[k](v) for k, v in c.items() if v is not None} for c in changes]
def filter_changes(self, fid: int) -> List: changes = self.call('eth_getFilterChanges', quantity_encoder(fid)) if not changes: return list() assert isinstance(changes, list) decoders = { 'blockHash': data_decoder, 'transactionHash': data_decoder, 'data': data_decoder, 'address': address_decoder, 'topics': lambda x: [topic_decoder(t) for t in x], 'blockNumber': quantity_decoder, 'logIndex': quantity_decoder, 'transactionIndex': quantity_decoder } return [ {k: decoders[k](v) for k, v in c.items() if v is not None} for c in changes ]