2020-08-25 16:22:59 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2019-02-15 21:14:08 +01:00
|
|
|
import os
|
2020-08-25 16:22:59 +02:00
|
|
|
import asyncio
|
2019-02-15 21:14:08 +01:00
|
|
|
|
|
|
|
|
from electrum.simple_config import SimpleConfig
|
|
|
|
|
from electrum import constants
|
|
|
|
|
from electrum.daemon import Daemon
|
|
|
|
|
from electrum.storage import WalletStorage
|
2019-02-28 20:22:23 +01:00
|
|
|
from electrum.wallet import Wallet, create_new_wallet
|
2020-08-25 16:22:59 +02:00
|
|
|
from electrum.wallet_db import WalletDB
|
2019-02-15 21:14:08 +01:00
|
|
|
from electrum.commands import Commands
|
2020-08-25 16:22:59 +02:00
|
|
|
from electrum.util import create_and_start_event_loop, log_exceptions
|
|
|
|
|
|
2019-02-15 21:14:08 +01:00
|
|
|
|
2020-08-25 16:22:59 +02:00
|
|
|
loop, stopping_fut, loop_thread = create_and_start_event_loop()
|
2019-02-15 21:14:08 +01:00
|
|
|
|
|
|
|
|
config = SimpleConfig({"testnet": True}) # to use ~/.electrum/testnet as datadir
|
2024-09-16 15:27:45 +00:00
|
|
|
constants.BitcoinTestnet.set_as_network() # to set testnet magic bytes
|
2019-02-15 21:14:08 +01:00
|
|
|
daemon = Daemon(config, listen_jsonrpc=False)
|
|
|
|
|
network = daemon.network
|
|
|
|
|
assert network.asyncio_loop.is_running()
|
|
|
|
|
|
|
|
|
|
# get wallet on disk
|
|
|
|
|
wallet_dir = os.path.dirname(config.get_wallet_path())
|
|
|
|
|
wallet_path = os.path.join(wallet_dir, "test_wallet")
|
|
|
|
|
if not os.path.exists(wallet_path):
|
2019-09-22 20:46:01 +02:00
|
|
|
create_new_wallet(path=wallet_path, config=config)
|
2019-02-15 21:14:08 +01:00
|
|
|
|
|
|
|
|
# open wallet
|
2023-10-10 16:57:44 +02:00
|
|
|
wallet = daemon.load_wallet(wallet_path, password=None, upgrade=True)
|
2019-02-15 21:14:08 +01:00
|
|
|
|
|
|
|
|
# you can use ~CLI commands by accessing command_runner
|
2019-09-22 20:46:01 +02:00
|
|
|
command_runner = Commands(config=config, daemon=daemon, network=network)
|
2020-08-25 16:22:59 +02:00
|
|
|
print("balance", network.run_from_another_thread(command_runner.getbalance(wallet=wallet)))
|
|
|
|
|
print("addr", network.run_from_another_thread(command_runner.getunusedaddress(wallet=wallet)))
|
|
|
|
|
print("gettx", network.run_from_another_thread(
|
|
|
|
|
command_runner.gettransaction("bd3a700b2822e10a034d110c11a596ee7481732533eb6aca7f9ca02911c70a4f")))
|
|
|
|
|
|
2019-02-15 21:14:08 +01:00
|
|
|
|
|
|
|
|
# but you might as well interact with the underlying methods directly
|
|
|
|
|
print("balance", wallet.get_balance())
|
|
|
|
|
print("addr", wallet.get_unused_address())
|
|
|
|
|
print("gettx", network.run_from_another_thread(network.get_transaction("bd3a700b2822e10a034d110c11a596ee7481732533eb6aca7f9ca02911c70a4f")))
|
2020-08-25 16:22:59 +02:00
|
|
|
|
|
|
|
|
stopping_fut.set_result(1) # to stop event loop
|