Skip to content

Instantly share code, notes, and snippets.

@killerstorm
Last active January 22, 2025 00:16
Show Gist options
  • Save killerstorm/095faa9cb5183839d54b68cfef92ddc5 to your computer and use it in GitHub Desktop.
Save killerstorm/095faa9cb5183839d54b68cfef92ddc5 to your computer and use it in GitHub Desktop.
ChromiaPyClientExample.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyM87uT7Y5PvIcvV8YIrcfrU",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/killerstorm/095faa9cb5183839d54b68cfef92ddc5/chromiapyclientexample.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HnRgGTGB-nJL",
"outputId": "121e869c-def6-4472-a3c3-ec446840a3d6",
"collapsed": true
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: postchain-client-py in /usr/local/lib/python3.11/dist-packages (0.1.4)\n"
]
}
],
"source": [
"!pip install -q postchain-client-py"
]
},
{
"cell_type": "code",
"source": [
"from postchain_client_py import BlockchainClient\n",
"from postchain_client_py.blockchain_client.types import NetworkSettings, Operation, Transaction\n",
"from postchain_client_py.blockchain_client.enums import FailoverStrategy\n",
"\n",
"settings = NetworkSettings(\n",
" # nodes from cluster your blockchain is from; current version of client does not auto-discover nodes from\n",
" # the directory chain, you can find them on https://explorer.chromia.com\n",
"\n",
" # this is system cluster\n",
" node_url_pool=[\"https://system.chromaway.com:7740\"],\n",
"\n",
" # id of a specific blockchain we connect to\n",
" # this is economy chain running on system cluster\n",
" blockchain_rid=\"15C0CA99BEE60A3B23829968771C50E491BD00D2E3AE448580CD48A8D71E7BBA\",\n",
")\n",
"\n",
"client = await BlockchainClient.create(settings)\n"
],
"metadata": {
"id": "HPBsXq4a-x3L"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import json\n",
"\n",
"# we can get application structure from the chain itself,\n",
"# this is equivalent of ABI in Ethereum\n",
"\n",
"app_structure = await client.query(\"rell.get_app_structure\")\n",
"\n",
"print(\"Queries by module\")\n",
"\n",
"queries = {}\n",
"for module in app_structure[\"modules\"]:\n",
" print(module)\n",
" if \"queries\" not in app_structure[\"modules\"][module]:\n",
" continue\n",
" for query in app_structure[\"modules\"][module][\"queries\"]:\n",
" query_data = app_structure[\"modules\"][module][\"queries\"][query]\n",
" queries[query_data[\"mount\"]] = query_data\n",
" print(\" \", query_data[\"mount\"])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "66leBNOaCCEQ",
"outputId": "b1955a04-9ec1-42fb-a0be-77156d49e1b0"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Queries by module\n",
"chain_version\n",
" release_commit\n",
" release_version\n",
"common_consensus\n",
"common_proposal\n",
" get_common_proposal\n",
" get_common_proposal_voter_info\n",
" get_common_proposal_voting_results\n",
" get_common_proposals_range\n",
" get_common_pubkey_votes\n",
" get_common_voter_set_members\n",
" get_common_voter_sets\n",
" get_relevant_common_proposals\n",
"constants\n",
"economy_chain\n",
" api_version\n",
" does_account_require_memo\n",
" find_dapp_details\n",
" get_all_bridge_leases_for_blockchain\n",
" get_all_bridge_leases_for_container\n",
" get_assign_subnode_image_to_container_ticket_by_id\n",
" get_assign_subnode_image_to_container_ticket_by_transaction\n",
" get_available_subnode_images_for_all_clusters\n",
" get_available_subnode_images_for_cluster\n",
" get_balance\n",
" get_blockchains_with_bridge_and_anomaly_detection\n",
" get_bridge_lease_for_blockchain\n",
" get_chr_asset\n",
" get_chromia_foundation_voter_set_info\n",
" get_cluster_by_name\n",
" get_cluster_change_tag_proposal\n",
" get_cluster_create_proposal\n",
" get_cluster_creation_status\n",
" get_clusters\n",
" get_create_container_ticket_by_id\n",
" get_create_container_ticket_by_transaction\n",
" get_dapp_provider_creation_cost\n",
" get_ec_voter_set_update_proposal\n",
" get_economy_constants\n",
" get_economy_metrics\n",
" get_economy_metrics_per_node\n",
" get_economy_metrics_per_provider_nodes\n",
" get_econony_constants_proposal\n",
" get_ft4_account_ids\n",
" get_lease_by_container_name\n",
" get_lease_purchases\n",
" get_lease_purchases_by_account\n",
" get_lease_refunds\n",
" get_lease_refunds_by_account\n",
" get_leases_by_account\n",
" get_max_lease_duration\n",
" get_min_lease_duration\n",
" get_minting_proposal\n",
" get_pool_account_id_query\n",
" get_pool_balance\n",
" get_price_oracle_rate_proposal\n",
" get_price_oracle_rates\n",
" get_propose_transfer_chromia_foundation_funds_proposal\n",
" get_provider_account_id\n",
" get_provider_performance_metrics\n",
" get_provider_performance_metrics_last_30_days\n",
" get_provider_performance_metrics_last_365_days\n",
" get_provider_performance_metrics_last_7_days\n",
" get_provider_staking_status\n",
" get_provisional_stake_balance\n",
" get_staking_requirement_constants_proposal\n",
" get_system_provider_economy_constants_proposal\n",
" get_tag_by_name\n",
" get_tag_proposal\n",
" get_tags\n",
" get_total_provisional_stake\n",
" get_total_provisional_stake_accounts\n",
" get_upgrade_container_ticket_by_id\n",
" get_upgrade_container_ticket_by_transaction\n",
" staking_get_all_shared_rewards_paid\n",
" staking_get_balance\n",
" staking_get_cannot_change_before\n",
" staking_get_current_delegate\n",
" staking_get_current_delegate_on_network\n",
" staking_get_current_rate\n",
" staking_get_native_balance\n",
" staking_get_next_delegation_change\n",
" staking_get_number_of_delegates_to\n",
" staking_get_provider_stake\n",
" staking_get_rewards_paid\n",
" staking_get_rewards_start\n",
" staking_get_shared_rewards_paid\n",
" staking_get_total_delegation_to\n",
" staking_get_withdrawal_time\n",
" staking_rewards_claimable_for\n",
" staking_rewards_for\n",
" staking_total_accounts\n",
" staking_total_accounts_on_network\n",
" staking_total_delegated\n",
" staking_total_delegation_accounts\n",
" staking_total_stake\n",
" staking_total_stake_on_network\n",
" total_lease_investment\n",
"economy_chain_prod\n",
"lib.eif.common\n",
"lib.eif.messaging\n",
"lib.eif.utils\n",
"lib.eif.version\n",
" eif.api_version\n",
"lib.ft4\n",
"lib.ft4.accounts\n",
"lib.ft4.accounts.strategies\n",
"lib.ft4.accounts.strategies.transfer\n",
"lib.ft4.accounts.strategies.transfer.fee\n",
"lib.ft4.accounts.strategies.transfer.open\n",
"lib.ft4.assets\n",
"lib.ft4.auth\n",
"lib.ft4.core.accounts\n",
"lib.ft4.core.accounts.strategies\n",
"lib.ft4.core.accounts.strategies.transfer\n",
" ft4.get_allowed_assets\n",
" ft4.get_pending_transfer_strategies\n",
" ft4.get_transfer_rules\n",
" ft4.has_pending_create_account_transfer_for_strategy\n",
"lib.ft4.core.accounts.strategies.transfer.fee\n",
" ft4.get_fee_assets\n",
"lib.ft4.core.accounts.strategies.transfer.open\n",
"lib.ft4.core.assets\n",
"lib.ft4.core.auth\n",
"lib.ft4.core.crosschain\n",
"lib.ft4.crosschain\n",
"lib.ft4.external.accounts\n",
" ft4.get_account_auth_descriptor_by_id\n",
" ft4.get_account_auth_descriptors\n",
" ft4.get_account_auth_descriptors_by_signer\n",
" ft4.get_account_by_id\n",
" ft4.get_account_main_auth_descriptor\n",
" ft4.get_account_rate_limit_last_update\n",
" ft4.get_accounts_by_auth_descriptor_id\n",
" ft4.get_accounts_by_signer\n",
" ft4.get_accounts_by_type\n",
" ft4.get_auth_descriptor_counter\n",
" ft4.get_config\n",
" ft4.is_auth_descriptor_valid\n",
"lib.ft4.external.accounts.strategies\n",
" ft4.get_enabled_registration_strategies\n",
" ft4.get_register_account_message\n",
"lib.ft4.external.assets\n",
" ft4.get_all_assets\n",
" ft4.get_asset_balance\n",
" ft4.get_asset_balances\n",
" ft4.get_asset_by_id\n",
" ft4.get_asset_details_for_crosschain_registration\n",
" ft4.get_assets_by_name\n",
" ft4.get_assets_by_symbol\n",
" ft4.get_assets_by_type\n",
" ft4.get_transfer_details\n",
" ft4.get_transfer_details_by_asset\n",
" ft4.get_transfer_history\n",
" ft4.get_transfer_history_entry\n",
" ft4.get_transfer_history_from_height\n",
"lib.ft4.external.auth\n",
" ft4.get_all_auth_handlers\n",
" ft4.get_auth_flags\n",
" ft4.get_auth_handler_for_operation\n",
" ft4.get_auth_message_template\n",
" ft4.get_first_allowed_auth_descriptor\n",
" ft4.get_first_allowed_auth_descriptor_by_signers\n",
" ft4.get_login_config\n",
"lib.ft4.external.crosschain\n",
" ft4.crosschain.get_apply_transfer_tx\n",
" ft4.crosschain.get_asset_origin_by_id\n",
" ft4.crosschain.get_last_pending_transfer_for_account\n",
" ft4.crosschain.get_pending_transfers_for_account\n",
" ft4.crosschain.is_transfer_applied\n",
"lib.ft4.utils\n",
"lib.ft4.version\n",
" ft4.get_version\n",
"lib.hbridge\n",
" eif.hbridge.are_snapshots_enabled_for_asset\n",
" eif.hbridge.get_account_for_eoa_address\n",
" eif.hbridge.get_account_for_sc_address\n",
" eif.hbridge.get_bridge_contracts\n",
" eif.hbridge.get_eoa_addresses_for_account\n",
" eif.hbridge.get_erc20_deposit_history\n",
" eif.hbridge.get_erc20_deposits\n",
" eif.hbridge.get_erc20_for_asset\n",
" eif.hbridge.get_erc20_withdrawal\n",
" eif.hbridge.get_erc20_withdrawal_by_event_hash\n",
" eif.hbridge.get_erc20_withdrawal_by_id\n",
" eif.hbridge.get_erc20_withdrawal_by_tx\n",
" eif.hbridge.get_erc20_withdrawal_history\n",
" eif.hbridge.get_erc20_withdrawals\n",
" eif.hbridge.get_registered_erc20_assets\n",
" eif.hbridge.get_sc_addresses_for_account\n",
" eif.hbridge.get_state_slot_ids_for_account\n",
" eif.hbridge.get_state_slot_ids_for_address\n",
" eif.hbridge.get_withdrawal_state_slot_ids_for_address\n",
"lib.iccf\n",
"lib.icmf\n",
"lib.icmf.constants\n",
"lib.icmf.receiver\n",
"lib.price_oracle_messages\n",
"lib.transaction_submitter_messaging\n",
"messaging.blockchain_rid\n",
"messaging.bridge_mapping\n",
"messaging.economy_chain\n",
"messaging.economy_chain_staking\n",
"messaging.node_availability_reporting\n",
"provider_auth\n",
"provider_auth.model\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"print(queries[\"ft4.get_assets_by_symbol\"])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MojTtJImA6s9",
"outputId": "8d45faae-1ff8-4c47-fa12-b3c498fd0c1a"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'mount': 'ft4.get_assets_by_symbol', 'parameters': [{'name': 'symbol', 'type': 'text'}, {'name': 'page_size', 'type': {'type': 'nullable', 'value': 'integer'}}, {'name': 'page_cursor', 'type': {'type': 'nullable', 'value': 'text'}}], 'type': 'lib.ft4.utils:paged_result'}\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def describe_query(query_name):\n",
" \"\"\"\n",
" Describes the query parameters and output type based on the global `queries` dictionary.\n",
"\n",
" Args:\n",
" query_name: The name of the query to describe.\n",
" \"\"\"\n",
"\n",
" if query_name not in queries:\n",
" print(f\"Query '{query_name}' not found.\")\n",
" return\n",
"\n",
" query_data = queries[query_name]\n",
" print(f\"Query: {query_name}\")\n",
" print(\"Parameters:\")\n",
" for param in query_data.get(\"parameters\", []):\n",
" param_name = param[\"name\"]\n",
" param_type = param[\"type\"]\n",
"\n",
" if isinstance(param_type, dict):\n",
" param_type_str = f\"{param_type['type']}({param_type['value']})\"\n",
" else:\n",
" param_type_str = param_type\n",
"\n",
" print(f\" - {param_name}: {param_type_str}\")\n",
" print(f\"Output Type: {query_data.get('type')}\")\n"
],
"metadata": {
"id": "ojhpz1YMKViX"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"print(describe_query(\"ft4.get_assets_by_symbol\"))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "J48c5aWVVX12",
"outputId": "d8dbb15c-89f9-4411-97af-05180fd634e1"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Query: ft4.get_assets_by_symbol\n",
"Parameters:\n",
" - symbol: text\n",
" - page_size: nullable(integer)\n",
" - page_cursor: nullable(text)\n",
"Output Type: lib.ft4.utils:paged_result\n",
"None\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"chr_asset = await client.query(\"ft4.get_assets_by_symbol\", {\"symbol\":\"CHR\", \"page_size\":None, \"page_cursor\":None})\n",
"chr_asset"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TOf5J1gnEu4t",
"outputId": "32744c47-d5b8-4efa-dcc6-c40e38286a44"
},
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'data': [{'blockchain_rid': '15C0CA99BEE60A3B23829968771C50E491BD00D2E3AE448580CD48A8D71E7BBA',\n",
" 'decimals': 6,\n",
" 'icon_url': 'https://assets.chromia.com/chr.png',\n",
" 'id': '5F16D1545A0881F971B164F1601CBBF51C29EFD0633B2730DA18C403C3B428B5',\n",
" 'name': 'Chromia',\n",
" 'supply': '85929786728814',\n",
" 'symbol': 'CHR',\n",
" 'type': 'ft4'}],\n",
" 'next_cursor': None}"
]
},
"metadata": {},
"execution_count": 7
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment