solana-py icon indicating copy to clipboard operation
solana-py copied to clipboard

How to retrieve the balance from a wallet

Open Piufy opened this issue 3 years ago • 9 comments

Here is my code:

`def get_balance(sender_username): resp = solana_client.get_balance(6cAyxMsZ9W2o9x4DPdbSgeQbP8MqK3QVAWSNEuWu41zc) balance = resp['result']['value'] / 1000000000 print(str(balance)) data= { "balance": str(balance), } return data

@bot.command(description='Check account balance') async def balance(ctx): sender_username = ctx.message.author try: data = get_balance(sender_username) if data is not None: public_key = data['publicKey'] balance = data['balance'] message = "{}".format(balance) await ctx.send(message) else: message = "Failed" await ctx.send(message) except Exception as e: print('error:',e) await ctx.send('Failed') return`

I want to try to retrieve the balance from a wallet, but I always just get the error "error: 'bytes' object has no attribute 'to_solders'". How can I solve this?

Piufy avatar Dec 08 '22 20:12 Piufy

Here is an example on how to use the get_balance API: https://michaelhly.github.io/solana-py/rpc/api/#solana.rpc.api.Client.get_balance

michaelhly avatar Dec 08 '22 23:12 michaelhly

Here is an example on how to use the get_balance API: https://michaelhly.github.io/solana-py/rpc/api/#solana.rpc.api.Client.get_balance

Okay, but when I do it like in the example I always get only <function get_balance at 0x0000027B4F45E160>. Am I just too stupid?

Piufy avatar Dec 09 '22 20:12 Piufy

@Piufy would you mind providing a code snippet of what you're trying to do?

michaelhly avatar Dec 09 '22 21:12 michaelhly

@Piufy would you mind providing a code snippet of what you're trying to do?

def get_balance(self, pubkey = key, commitment= None): body = self._get_balance_body(pubkey, commitment) return self._provider.make_request(body)

Here, I don't know, if I do something wrong or if i am just stupid

Piufy avatar Dec 09 '22 22:12 Piufy

can you do this with an actual real world example of an address instead of PubKey + 31 and all that? Most people I'd imagine are going to be using their own addresses formatted like normal

openseauser avatar Jan 14 '23 20:01 openseauser

can you do this with an actual real world example of an address instead of PubKey + 31 and all that? Most people I'd imagine are going to be using their own addresses formatted like normal

By looking at the solana-py source code I found that I needed to use the following code:

program_id = "7sDX4xdZmckRzXMuFAiEVwimH6zph43edCBiHkVa4DeV"  # A staking program I use.

account_info = client.get_account_info(
    Pubkey.from_string(program_id), encoding="base58"
)

But the Pubkey([0] * 31 + [1]) was a bit strange when first reading it. So maybe we should explain that a random byte account is created and users can use their string addresses through the PubKey.from_string method or use a real-world example instead. I think this will help developers to get started more easily with the library.

rickstaa avatar Jan 22 '23 12:01 rickstaa

@openseauser @rickstaa @michaelhly @Piufy @jbn Thx so much in advance!!!!

hi, do you guys know how to get number of SPL token in one account? (not SOL, but SPL tokens, like USDC, etc) Seems solana is so different from evm chain, how should I do?

Mrfranken avatar Mar 08 '24 11:03 Mrfranken