How to list your valuable chia coins in python
$ mkdir coins
$ cd coins
$ python3 -m venv venv
$ . ./venv/bin/activate
(venv) $ pip install chia-dev-tools
This will have installed the dev environment.
Now create a file called coins.py :
import asyncio
import sqlite3
from chia.rpc.full_node_rpc_client import FullNodeRpcClient
from chia.util.ints import uint16
from chia.util.default_root import DEFAULT_ROOT_PATH
async def main():
node_ssl_path = f'{DEFAULT_ROOT_PATH}/config/ssl/'
node_rpc_client = await FullNodeRpcClient.create(
'localhost', 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_1575019083.sqlite')
cursor = s.cursor()
cursor.execute('select puzzle_hash from derivation_paths')
phs = {bytes.fromhex(i[0]) for i in cursor.fetchall()}
cursor.close()
s.close()
phslist = list(phs)
for cr in await node_rpc_client.get_coin_records_by_puzzle_hashes(phslist, include_spent_coins=False) :
if cr.coin.amount > 1000000000 :
print( f'Coin {cr.coin.amount}')
node_rpc_client.close()
asyncio.run( main() )
Run the code
(venv) $ python coins.py
Coin 500654465393
Coin 6731204338
Coin 8491004911
Coin 250000000000
(venv) $ deactivate
$
code by the duster
3 Likes
Can you give an example of what sort of coins you’re looking for? Is this looking for Chia altcoins, or am I totally missing the point of what you’re saying here?
There isn’t much point to it. The value of chia that you see in your wallet is made up of individual coins. This is just a code example of how to look at them
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