Here is the code to transfer ETH from addr1 to addr2 in web3.py
from web3 import Web3, HTTPProvider
api = 'https://rinkeby.infura.io/v3/...'
w3 = Web3(HTTPProvider(api))
addr1 = '...'
key1 = '...'
addr2 = '...'
key2 = '...'
fromAddress = Web3.toChecksumAddress(addr1)
toAddress = Web3.toChecksumAddress(addr2)
gas = 21000
nonce = w3.eth.getTransactionCount(fromAddress)
gasPrice = w3.eth.gasPrice
transaction = {'from': fromAddress, 'to': toAddress, 'nonce': nonce, 'gasPrice': gasPrice, 'gas': gas,
'value': 0, 'data': ''}
signed_tx = w3.eth.account.signTransaction(transaction, key1)
txn_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(txn_hash.hex())
Consider the situation: Addr1 have some ETH and addr2 have no ETH. I need to transfer ETH from addr1 to addr2 and reverse simultaneously. After send transaction from addr1 to addr2, Addr2 have to wait for block confirmation to ensure enough ETH for gas fee and then send back. That means the two transactions are in two different block. Now I want to do these in one block like this: How?