Get correct Token balance - Algorand

Get correct Token balance

post by nikitagugale on Jun 22, 2022

Following is my ASA balance function developed using python SDK

gettokenbalance(account_address,token_ID):
    account_info = algod_client.account_info(account_address)
    for a in account_info["assets"] :
        if (a['asset-id'] == token_ID):
            return (a['amount'])
            break

ASA has decimal: 4

In return I get Token Balance as 79

Where as expected is 0.0079

If anyone can help what has to be done, please do.

post by fabrice on Jun 22, 2022

Since the number of decimals is 4, you need to divide the number you got (79) by 10,000 (=10^4). This way you get 0.0079.

post by nikitagugale on Jun 23, 2022

Great thanks it worked.

Just another query regarding ASA

Following is transfer of ASA

def transfertoken(sender,sender_private_key,purchaser_address,amt,asset_id):
    params = algod_client.suggested_params()
    # comment these two lines if you want to use suggested params
    params.fee = 1000
    params.flat_fee = True
    txn = AssetTransferTxn(
        sender=sender,
        sp=params,
        receiver=purchaser_address,
        amt=amt,
        index=asset_id)
    stxn = txn.sign(sender_private_key)
    txid = algod_client.send_transaction(stxn)
    # print(txid)
    # Wait for the transaction to be confirmed
    stxd = wait_for_confirmation(algod_client, txid)
    # The balance should now be 10.
    print_asset_holding(algod_client, purchaser_address, asset_id)
    return txid,stxd

Transfer for whole number works fine but for float it gives error

File "/home/ubuntu/.local/lib/python3.6/site-packages/algosdk/future/transaction.py", line 1323, in __init__
    raise error.WrongAmountType
algosdk.error.WrongAmountType

post by humblefool on Jun 23, 2022

You must multiply the amount by the 10 power decimal before preparing the transaction.

EX: if you want to transfer 1.2, and the decimal of the asset is 1, then you need to do:

1.2 * 10 power 1, which makes it 12.