What is the py-algorand-sdk equivalent of "./sandbox goal account list"

269 views Asked by At

TLDR; I am running an Algorand PrivateNet in a sandbox. I would like to know how I can use the Python SDK for Algorand to return the address and balance of the accounts on this network.

Longer version; Until now I have been using "./sandbox goal account list" to find the account addresses and account balances in Bash, this returns:

[offline] I3CMDHG236HVBDMCKEM22DLY5L2OJ3CBRUDHG4PVVFGEZ4QQR3X3KNHRMU I3CMDHG236HVBDMCKEM22DLY5L2OJ3CBRUDHG4PVVFGEZ4QQR3X3KNHRMU 8012889074131520 microAlgos
[offline] 3NSWKJTYMW3PSZZDIE6NCHMRTOZ24VY5G3OJNDSTYRVRXMZBZVPCGQHJJI 3NSWKJTYMW3PSZZDIE6NCHMRTOZ24VY5G3OJNDSTYRVRXMZBZVPCGQHJJI 1001611000000000 microAlgos
[online] 5KLSI3AHMBBDALXBEO2HEA3PBBCBAYT4PIHCD3B25557WGWUZGRTQETPHQ 5KLSI3AHMBBDALXBEO2HEA3PBBCBAYT4PIHCD3B25557WGWUZGRTQETPHQ 100000 microAlgos

I have tried to utilise the algod and v2client.indexer modules as outlined below but instead of the 3 accounts listed above 4 are being listed by my code (the original 3 +1). The code:

    #imports
    from algosdk import algod, v2client
    
    # instatiate AlgodClient
    def connect_to_network():
        algod_address = "http://localhost:4001"
        algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        algod_client = algod.AlgodClient(algod_token, algod_address)
        return algod_client 
    
    # instantiate IndexClient
    def indexer():
        indexer_token = ""
        indexer_address = "http://localhost:8980"
        indexer_client = v2client.indexer.IndexerClient(indexer_token, indexer_address)
        return indexer_client
    
    # returns all account details from indexer
    def account_details():
        raw_details = indexer().accounts() # <-- I think the error is here because this returns 4 addresses.
        account_details = []
        for details in raw_details["accounts"]:
            account_details.append({"address":details["address"], "amount": details["amount"]})
        return account_details
    
    # returns the amount in each account
    def account_amounts(account_address):
        return connect_to_network().account_info(account_address)
    
    # returns account address and amount for each account
    for detail in account_details():
        print(account_amounts(detail["address"])["address"])
        print(account_amounts(detail["address"])["amount"])

This returns:

I3CMDHG236HVBDMCKEM22DLY5L2OJ3CBRUDHG4PVVFGEZ4QQR3X3KNHRMU 8013089091731545
NTMTJYBQHWUXEGVG3XRRX5CH6FCYJ3HKCSIOYW4DLAOF6V2WHSIIFMWMPY 1001636000000000
3NSWKJTYMW3PSZZDIE6NCHMRTOZ24VY5G3OJNDSTYRVRXMZBZVPCGQHJJI 1001636000000000
5KLSI3AHMBBDALXBEO2HEA3PBBCBAYT4PIHCD3B25557WGWUZGRTQETPHQ 100000
  1. How can I replicate "./sandbox goal account list" with py-algorand-sdk?
  2. (Bonus question) what is this extra mystery "account" that appears when I use the SDK compared to Bash?
1

There are 1 answers

0
Uppers On

The issue above arose because like with ./sandbox goal accounts listI was trying to return the accounts I had keys for locally in the KMD but my call to the indexer raw_details = indexer().accounts() was returning all accounts on the network.

To remedy this, instead of importing and using the v2client.indexer module import the wallet module from the algosdk library. These are the necessary imports:

from algosdk import algod, kmd, wallet

Then set up the connections to algod, kmd, and your wallet:

# instatiate AlgodClient
def connect_to_network():
    algod_address = "http://localhost:4001"
    algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    algod_client = algod.AlgodClient(algod_token, algod_address)
    return algod_client 

# instantiate KMDClient <--- this is used as a parameter when instantiating the Wallet Class.
def key_management():
    kmd_address = "http://localhost:4002"
    kmd_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    kmd_client = kmd.KMDClient(kmd_token, kmd_address)
    return kmd_client

#get wallet details
def wallet_details():
    wallet_name = 'unencrypted-default-wallet'
    password = ""
    kmd_client = key_management()
    return wallet.Wallet(wallet_name, password, kmd_client)

You can now return the account addresses held locally in the KMD:

keys = wallet_details().list_keys()

Now that you have the addresses you can query use the algod client to return the amount of (Micro) Algo held at each of these addresses.