Using the Ethereum RPC client in Python
Ethereum - RPC client¶
In [1]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
In this notebook I will try out the RPC client of Ethereum using Python. See JSON RPC for more information on JSON RPC .
In [2]:
import sys
sys.version
Out[2]:
Prepare environment¶
Install the Python RPC client for Ethereum. Github
jitsejan@jjvps:~$ pip install ethereum-rpc-client
Start the blockchain making sure RPC is enabled.
jitsejan@jjvps:~$ geth --networkid 23 --nodiscover --maxpeers 0 --port 30333 --rpc
Verify that geth is running and the account is listed.
In [3]:
!geth account list
Connect to the RPC client¶
In [4]:
from eth_rpc_client import Client
client = Client(host="127.0.0.1", port="8545")
Inspect the client¶
In [5]:
import pdir
pdir(client)
Out[5]:
Get the coinbase for the blockchain¶
In [6]:
address = client.get_coinbase()
address
Out[6]:
Retrieve the balance of the main address¶
In [7]:
client.get_balance(address)
Out[7]:
Set the addresses of the two other machines:
In [8]:
address_vps_one = "0xc257beaea430afb3a09640ce7f020c906331f805"
address_vps_two = "0xe86ee31b7d32b743907fa7438c422a1803717deb"
In [9]:
client.get_balance(address_vps_one)
client.get_balance(address_vps_two)
Out[9]:
Out[9]:
Transaction¶
Lets send 12 Ether from the main machine to VPS one.
In [10]:
amount = 12 # Ether
sending_address = address
receiving_address = address_vps_one
1 GWEI = 0.000000001 Ether
Get the password to unlock the sending account¶
In [11]:
from getpass import getpass
pw = getpass(prompt='Enter the password for the sender: ')
Unlock the account via the command line¶
(By lack of a better way)
In [12]:
command = r'geth --exec "personal.unlockAccount(\"%s\", \"%s\");" attach ' % (sending_address, pw)
result = !$command
if result[0] != 'true':
print('Fail: %s' % result[0])
else:
print('Account is unlocked!')
Send the transaction¶
In [13]:
tx_hash = client.send_transaction(to=receiving_address, _from=sending_address, value=amount * 10**9)
Check the transaction details¶
In [14]:
client.get_transaction_by_hash(tx_hash)
Out[14]:
Perform one mining step¶
Execute the miner to validate the transaction.
In [15]:
prev_balance_sen = client.get_balance(sending_address)
prev_balance_rec = client.get_balance(receiving_address)
In [16]:
result = !geth --exec "miner.start();admin.sleepBlocks(1);miner.stop();" attach
if result[0] != 'true':
print('Fail: %s' % result[0])
else:
print("Mining finished!")
Check if the Ether has been received¶
In [17]:
print("Received %d"% (client.get_balance(receiving_address)-prev_balance_rec))
Check if the Ether was sent¶
First check the difference in the balance.
In [18]:
print("Difference of the sender %d"% (client.get_balance(sending_address)-prev_balance_sen))
For mining, the miner will get a mining bonus.
In [19]:
mining_bonus = 5000000000000000000
To get the amount of Ether sent we need to substract the mining bonus.
In [20]:
print("Amount difference: %d" % int(client.get_balance(sending_address)-prev_balance_sen - mining_bonus))