Interact with RPC Daemon¶
MoneroDaemonRpc offers a MoneroDaemon implementation that talks to a monerod through its RPC, JSON-RPC and binary interfaces.
Creating a MoneroDaemonRpc instance is very simple:
from monero import MoneroDaemonRpc
# create RPC daemon connected to `monerod-rpc` server
daemon = MoneroDaemonRpc("http://monerod_rpc_uri:18081")
It can also be done using a MoneroRpcConnection instance:
from monero import MoneroRpcConnection, MoneroDaemonRpc
# create rpc connection
connection = MoneroRpcConnection("http://monerod_rpc_uri:18081")
# create daemon using RPC connection
daemon = MoneroDaemonRpc(connection)
Fetch daemon info and status:
# get general daemon rpc info
daemon_info = daemon.get_info()
print(f"Daemon info: {daemon_info.serialize()}")
# get daemon specific sync info
sync_info = daemon.get_sync_info()
print(f"Sync info: {sync_info.serialize()}")
# get peer bans
bans = daemon.get_peer_bans()
for ban in bans:
print(f"Banned peer: {ban.serialize()}")
Query blocks and txs on blockchain:
# get last block header added to blockchain
last_block = daemon.get_last_block_header()
print(f"Last block header: {last_block.serialize()}")
# get specific block by height
block = daemon.get_block_by_height(1520000)
print(f"Block fetched by height: {block.serialize()}")
# get specific block by hash
block = daemon.get_block_by_hash("...block hash...")
print(f"Block fetched by hash: {block.serialize()}")
# get alternative chains seen by the daemon
chains = daemon.get_alt_chains()
print(f"Found {len(chains)} alternative chains")
# get unconfirmed txs in pool
tx_pool = daemon.get_tx_pool()
for tx in tx_pool:
print(f"Found tx in pool: {tx.serialize()}")
# get specific tx added to chain, None if the daemon does not have it
chain_tx = daemon.get_tx("...tx hash...")
if chain_tx is not None:
print(f"Fetched tx: {chain_tx.serialize()}")
Relay mined blocks and signed txs:
# submit a mined block
daemon.submit_block("...block blob...")
# submit a signed tx
result = daemon.submit_tx_hex("...signed tx hex...")
print(f"Tx submitted: {result.is_good}")