michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
1.06k stars 272 forks source link

How to retrieve the balance from a wallet #324

Open Piufy opened 1 year ago

Piufy commented 1 year ago

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?

michaelhly commented 1 year ago

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

Piufy commented 1 year ago

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?

michaelhly commented 1 year ago

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

Piufy commented 1 year ago

@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

openseauser commented 1 year ago

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

rickstaa commented 1 year ago

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.

Mrfranken commented 8 months ago

@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?

btcoin23 commented 1 month ago

@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?

It can be done easily.

const getTokenBalance = async (mint: string): Promise<number|null> => {
  try {
    const ata = getAssociatedTokenAddressSync(
      new PublicKey(mint),
      owner.publicKey,
      true,
      TOKEN_PROGRAM_ID,
      ASSOCIATED_TOKEN_PROGRAM_ID
    );

    const balance = await connection.getTokenAccountBalance(ata);
    // console.log(balance.value.uiAmount)
    return Number(balance.value.uiAmount);
  } catch (e) {
    return null;
  }
};
btcoin23 commented 1 month ago

@Mrfranken Hope this help you!