How To See What chia coins you have

Following on from above how to show your coins we can add some code like the following to send a coin (to ourselves)

            payments = []
            print(f"Sending {int(cr.coin.amount)} coin")
            payments += [{'puzzle_hash': ph, 'amount': uint64(cr.coin.amount)}]

            tx = await wallet_rpc_client.send_transaction_multi(
                1, payments, fee=uint64(fee), coins=[cr.coin],)

But instead lets split up our coin so that we have two

import asyncio
import sys
import sqlite3

from chia.cmds.wallet_funcs import execute_with_wallet
from chia.rpc.full_node_rpc_client import FullNodeRpcClient
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import decode_puzzle_hash
from chia.util.ints import uint16, uint64
from chia.util.default_root import DEFAULT_ROOT_PATH




async def split(params, wallet_rpc_client, fingerprint):
    node_hostname = 'localhost'
    node_ssl_path = f'{DEFAULT_ROOT_PATH}/config/ssl/'
    node_rpc_client = await FullNodeRpcClient.create(
        node_hostname, uint16(int( 8555)), DEFAULT_ROOT_PATH, {
            'private_ssl_ca': {
                'crt': f'{node_ssl_path}/ca/private_ca.crt',
                'key': f'{node_ssl_path}/ca/private_ca.key',
            },
            'daemon_ssl': {
                'private_crt': f'{node_ssl_path}/daemon/private_daemon.crt',
                'private_key': f'{node_ssl_path}/daemon/private_daemon.key',
            },
        }
    )

    s = sqlite3.connect(f'{DEFAULT_ROOT_PATH}/wallet/db/blockchain_wallet_v1_mainnet_{fingerprint}.sqlite')
    cursor = s.cursor()
    cursor.execute('select puzzle_hash from derivation_paths order by derivation_index')
    phs = {bytes.fromhex(i[0]) for i in cursor.fetchall()}
    cursor.close()

    fee = 0

    scan = list(phs)

    for i in range(len(phs) ):
        for cr in await node_rpc_client.get_coin_records_by_puzzle_hashes(scan[i:i+1], include_spent_coins=False):
            #if cr.coin.amount > 1000000:
            #    continue
            payments = []
            if cr.coin.amount > 1:
                amount = cr.coin.amount / 2
                print(f"Splitting {int(cr.coin.amount)} coin")
                addr = await wallet_rpc_client.get_next_address(1, True)
                ph = bytes32(decode_puzzle_hash(addr))
                payments += [{'puzzle_hash': ph, 'amount': uint64(amount)}]
                addr = await wallet_rpc_client.get_next_address(1, True)
                ph = bytes32(decode_puzzle_hash(addr))
                payments += [{'puzzle_hash': ph, 'amount': uint64(cr.coin.amount - amount)}]

                tx = await wallet_rpc_client.send_transaction_multi(
                1, payments, fee=uint64(fee), coins=[cr.coin],)  

    node_rpc_client.close()
    wallet_rpc_client.close()
    await node_rpc_client.await_closed()
    await wallet_rpc_client.await_closed()

if __name__ == '__main__':
    asyncio.run(execute_with_wallet(9256, None, {}, split))

2 Likes