def build_and_send_tx(self, func: ContractFunction, tx_params: Optional[TxParams] = None) -> HexBytes: if not tx_params: tx_params = self.get_tx_params() transaction = func.buildTransaction(tx_params) signed_tx = self.w3.eth.account.sign_transaction( transaction, private_key=self.secrets._pk) try: return self.w3.eth.send_raw_transaction(signed_tx.rawTransaction) finally: self.last_nonce = Nonce(tx_params["nonce"] + 1)
def _build_and_send_tx(self, function: ContractFunction, tx_params: Optional[TxParams] = None) -> HexBytes: """Build and send a transaction.""" if not tx_params: tx_params = self._get_tx_params() transaction = function.buildTransaction(tx_params) signed_txn = self.w3.eth.account.sign_transaction( transaction, private_key=self.private_key) # TODO: This needs to get more complicated if we want to support replacing a transaction # FIXME: This does not play nice if transactions are sent from other places using the same wallet. try: return self.w3.eth.sendRawTransaction(signed_txn.rawTransaction) finally: logger.debug(f"nonce: {tx_params['nonce']}") self.last_nonce = Nonce(tx_params["nonce"] + 1)
def test_eth_replaceTransaction_incorrect_nonce( self, web3: "Web3", unlocked_account: ChecksumAddress) -> None: txn_params: TxParams = { 'from': unlocked_account, 'to': unlocked_account, 'value': Wei(1), 'gas': Wei(21000), 'gasPrice': web3.eth.gasPrice, } txn_hash = web3.eth.sendTransaction(txn_params) txn = web3.eth.getTransaction(txn_hash) txn_params['gasPrice'] = Wei(web3.eth.gasPrice * 2) txn_params['nonce'] = Nonce(txn['nonce'] + 1) with pytest.raises(ValueError): web3.eth.replaceTransaction(txn_hash, txn_params)
def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddress) -> None: txn_params: TxParams = { 'from': unlocked_account, 'to': unlocked_account, 'value': Wei(1), 'gas': Wei(21000), 'gasPrice': web3.eth.gasPrice, 'nonce': Nonce(0), } result = web3.eth.signTransaction(txn_params) signatory_account = web3.eth.account.recover_transaction(result['raw']) assert unlocked_account == signatory_account assert result['tx']['to'] == txn_params['to'] assert result['tx']['value'] == txn_params['value'] assert result['tx']['gas'] == txn_params['gas'] assert result['tx']['gasPrice'] == txn_params['gasPrice'] assert result['tx']['nonce'] == txn_params['nonce']
def _build_and_send_tx(self, function: ContractFunction, tx_params: Optional[TxParams] = None) -> HexBytes: """Build and send a transaction.""" if not tx_params: tx_params = self._get_tx_params() transaction = function.buildTransaction(tx_params) signed_txn = self.w3.eth.account.sign_transaction( transaction, private_key=self.private_key) try: return self.w3.eth.sendRawTransaction(signed_txn.rawTransaction) finally: print("Print TX = ", self.w3.toHex(self.w3.keccak(signed_txn.rawTransaction))) logger.debug(f"nonce: {tx_params['nonce']}") self.last_nonce = Nonce(tx_params["nonce"] + 1) #LOGG TX TO JSON with open('transactions.json', 'r') as fp: data = json.load(fp) tx_input = tx_params data.append(tx_input) with open('transactions.json', 'w') as fp: json.dump(data, fp, indent=2) fp.close() last_tx = self.w3.toHex(self.w3.keccak(signed_txn.rawTransaction)) hash = [{'hash': last_tx}] with open('hash.json', 'w') as fp: json.dump(hash, fp, indent=2) fp.close() print("Waiting for TX to Confirm") while True: try: if self.w3.eth.getTransactionReceipt(last_tx) != None: print("Transaction Confirmed!") break else: print("waiting for tx to confirm") except Exception: pass time.sleep(5)
def get( participant: HexAddress, channel_identifier: ChannelID, v: int = 27, other_token_network: Optional[Contract] = None, ) -> bytes: _token_network = other_token_network or token_network private_key = get_private_key(participant) non_closing_signature = sign_balance_proof_message( private_key, _token_network.address, _token_network.functions.chain_id().call(), channel_identifier, MessageTypeId.BALANCE_PROOF, EMPTY_BALANCE_HASH, Nonce(0), EMPTY_ADDITIONAL_HASH, EMPTY_SIGNATURE, v, ) return non_closing_signature
def get( channel_identifier: ChannelID, participant: HexAddress, transferred_amount: TokenAmount = TokenAmount(0), # noqa locked_amount: TokenAmount = TokenAmount(0), # noqa nonce: Nonce = Nonce(0), # noqa locksroot: Optional[Locksroot] = None, additional_hash: Optional[AdditionalHash] = None, v: int = 27, signer: Optional[HexAddress] = None, other_token_network: Optional[Contract] = None, ) -> OnchainBalanceProof: _token_network = other_token_network or token_network private_key = get_private_key(signer or participant) locksroot = locksroot or LOCKSROOT_OF_NO_LOCKS additional_hash = additional_hash or AdditionalHash(b"\x00" * 32) balance_hash = hash_balance_data(transferred_amount, locked_amount, locksroot) signature = sign_balance_proof( private_key, _token_network.address, _token_network.functions.chain_id().call(), channel_identifier, MessageTypeId.BALANCE_PROOF, balance_hash, nonce, additional_hash, v, ) # The keys of the dictionary correspond to the parameters of # create_balance_proof_countersignature. return OnchainBalanceProof( balance_hash=balance_hash, nonce=nonce, additional_hash=additional_hash, original_signature=signature, )
def sign_bundle( self, bundled_transactions: List[Union[FlashbotsBundleTx, FlashbotsBundleRawTx]], ) -> List[HexBytes]: """ Given a bundle of signed and unsigned transactions, it signs them all""" nonces = {} signed_transactions = [] for tx in bundled_transactions: # if it's not given a signer, we assume it's a signed RLP encoded tx, if "signer" not in tx: # TODO: Figure out how to decode a raw transaction with RLP # decoded_tx = rlp.decode(tx["signed_transaction"], sedes=BaseTransactionFields) # nonces[decoded_tx["from"]] = decoded_tx["nonce"] + 1 signed_transactions.append(tx["signed_transaction"]) else: # set all the fields signer = tx["signer"] tx = tx["transaction"] if tx["nonce"] is None: nonce = nonces.get(signer.address) or Nonce(0) tx["nonce"] = nonce else: nonce = tx["nonce"] # store the new nonce nonces[signer.address] = nonce + 1 # and update the tx details tx["from"] = signer.address tx["gasPrice"] = 0 if (tx["gas"] == 0): tx["gas"] = self.web3.eth.estimateGas(tx) # sign the tx signed_tx = signer.sign_transaction(tx) signed_transactions.append(signed_tx.rawTransaction) return signed_transactions