Checking Token Balance using Soroban Python SDK

darkvallen

darkvallen

Posted on March 24, 2023

Checking Token Balance using Soroban Python SDK

In my Sending Payment using Soroban Python SDK blog post, I wrote how to check token balance using Soroban-CLI, after few hours i'm start to thinking "How to check token balance using Soroban SDK?" so after that i tried to write the script. So in this post, we'll use Soroban Python SDK to check token balance.

Preparation

First, create a Futurenet account, this account will be used to invoke balance function from the token contract using Soroban Python SDK. Create account at Stellar Laboratory and make sure to fund them. The account that i will be using :

Public Key  GATQFKA7BV7LYYE6UJ2LRL56W3BZRRSWTINA6TG2EM5CCDORFIERAKWV
Secret Key  SCHKLGAGY7QGWUKUFDGHEQMSCPFZNMLEHQE6Y6WPGYGHQIM23T3NJE3C
Enter fullscreen mode Exit fullscreen mode

The file that we're gonna use :

check_token_balance.py - The python script to check the token balance.

Checking the Token Balance

To check the token balance, open the check_token_balance.py file and adjust the following parameters:

contract_id = "d93f5c7bb0ebc4a9c8f727c5cebc4e41194d38257e1d0d910356b43bfc528813"
tx_submitter_kp = Keypair.from_secret(
    "SCHKLGAGY7QGWUKUFDGHEQMSCPFZNMLEHQE6Y6WPGYGHQIM23T3NJE3C"
)

Enter fullscreen mode Exit fullscreen mode

tx_submitter_kp: The secret key of the account whose balance you want to check. The contract ID that i'm using is native token contract(XLM Lumens)
contract_id: Token Contract ID of the token that you want to check.

Save the check_token_balance file with your changes.

Before running the script, i want to explain this specific codes :

func_name = "balance"
args = [Address(tx_submitter_kp.public_key)]

invocation = AuthorizedInvocation(
    contract_id=contract_id,
    function_name=func_name,
    args=args,
    sub_invocations=[],
)

contract_auth = ContractAuth(
    address=None,
    nonce=None,
    root_invocation=invocation,
)

source = soroban_server.load_account(tx_submitter_kp.public_key)
tx = (
    TransactionBuilder(source, network_passphrase)
    .add_time_bounds(0, 0)
    .append_invoke_contract_function_op(
        contract_id=contract_id,
        function_name=func_name,
        parameters=args,
        auth=[contract_auth],
    )
    .build()
)
Enter fullscreen mode Exit fullscreen mode

From the script above the function of the token contract that we're going to invoke is balance function, for more info about Token contract functions check here. This script utilizing transaction invoker authorization to invoke the function.

Now we will run the script using this command :

python check_token_balance.py
Enter fullscreen mode Exit fullscreen mode

We will get result:

Your token Balance: 99999999400
Enter fullscreen mode Exit fullscreen mode

To cross-check it, we can use Stellar Laboratory`s explore endpoints tools.

Stellar Laboratory

Closing

In this blog post, we have gone over the steps to Check Token Balance using Soroban Python SDK. From the script you will get an insight how to check token balance using Soroban Python SDK.

💖 💪 🙅 🚩
darkvallen
darkvallen

Posted on March 24, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related