0

I'm trying to find out which tz addresses have a minimum of 50 tez. I got chatgpt to help me but I'm getting this error:

TypeError: PyTezosClient.balance() takes 1 positional argument but 2 were given

Here's my code:

import csv
from pytezos import pytezos

# Connect to a Tezos node 
pytezos = pytezos.using(shell='https://rpc.tzkt.io') 

# Open input CSV file and create output CSV file
with open('addresses.csv', 'r') as infile, open('over50.csv', 'w') as outfile:

    # Create CSV reader and writer
    reader = csv.reader(infile)
    writer = csv.writer(outfile)
    
    # Write header row
    writer.writerow(['Address', 'Balance'])
    
    # Loop through rows
    for row in reader:
        address = row[0]
        
        # Get balance for address
        balance = pytezos.balance(address)
        
        # Check if balance is >= 50 XTZ
        if balance >= 50_000_000:
            
            # Write row to output
            writer.writerow([address, balance])

1 Answer 1

3

The .balance() function only takes one argument as described in the PyTezos documentation. It is used to get the balance of the wallet that was initialized by using its private key.

For your use case, I'd suggest using a Simple TzKt API query to get the balance of any address on the mainnet and other testnets. Using balance_mutez = get(f'https://api.tzkt.io/v1/accounts/{address}/balance').json()

This is how you can use TzKt API's Get Balance endpoint.

So the entire code will be:

import csv
from requests import get

# Open input CSV file and create output CSV file
with open('addresses.csv', 'r') as infile, open('over50.csv', 'w') as outfile:

    # Create CSV reader and writer
    reader = csv.reader(infile)
    writer = csv.writer(outfile)
    
    # Write header row
    writer.writerow(['Address', 'Balance'])
    
    # Loop through rows
    for row in reader:
        address = row[0]
        
        # Get balance for address
        balance = get(f'https://api.tzkt.io/v1/accounts/{address}/balance').json()
        
        # Check if balance is >= 50 XTZ
        if balance >= 50_000_000:
            
            # Write row to output
            writer.writerow([address, balance])
4
  • Thanks very much! I have a large list of addresses so it worked for the first 22 but then I got: requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')) I will try to break it up in to chunks and space it out to go easy on the API
    – leo
    Commented Jul 28, 2023 at 6:16
  • Great! If there are a lot of addresses, you can use another way to get balances of multiple addresses in one API call. I'm unsure how many addresses it can support, but let me know if you need it. Commented Jul 28, 2023 at 8:32
  • That would be very much appreciated! I'm a baker and would like to occasionally send NFTs to some of my delegates. The list I used had just over 2000 addresses.
    – leo
    Commented Jul 28, 2023 at 13:47
  • 1
    @leo use this endpoint api.tzkt.io/#operation/Accounts_Get to get your delegators with specific balance (filter by delegate and balance). Or better ask a separate question and I will answer, so other people can learn from it too ;)
    – Groxan
    Commented Jul 28, 2023 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.