Search code examples
apiautomationcryptographycoinbase-api

getting specific wallet balance from coinbase using python api


so I am working on a project attempting to make a script that will activate once a day check the BTC balance in my coinbase account and if there is an amount worth selling it will sell. So at the end of every 24 hour period of mining the script will cash out whatever i have mined that day. I have found function to print all the balances of my coinbase account but i cant seem to find an example of isolating the balance in one coinbase wallet so i can pass it into a variable. Any help would be appreciated. ill attach the code I have been using to view all my account balances

'''

from coinbase.wallet.client import Client 
import json 
import urllib

api_key = '*********'
api_secret = '***************'
client = Client(api_key,api_secret)

total = 0
message = []
accounts = client.get_accounts()
for wallet in accounts.data:
    message.append(str(wallet['name'])+''+str(wallet['native_balance']))
    value = str(wallet['native_balance']).replace('USD','')
    total += float(value)

message.append('Total Balance: '+'USD'+str(total))

print ('\n'.join(message))

'''


Solution

  • I would recommend you do it directly in the BTC account:

    import json
    BTCaccount = client.get_account('BTC')
    
    #convert to dict. dunno if needed.worked for me. you need to import json
    BTCaccountdict = json.loads(json.dumps(BTCaccount))
    
    #using dict to get the current BTC balance
    BTCbalance = BTCaccountdict['balance']['amount']
    

    of course you would then need to run this over your time period and maybe use time library or something to see how much you mined over 24 hours.