简体   繁体   中英

how do I check balance of USDT(Tether) in Python?

I was working on my python crypto project recently, but I had to make a method that checks balance of Tether wallet. Is there a way to check balance of tether with python? this is my code:

class wallet_Tether:
#Tether shares same blockchain with bitcoin

def __init__(self,wallet_address,wallet_privatekey,wallet_balance,wallet_history,wallet_public_key,wallet_name,wallet):

    self.wallet_address = wallet_address
    self.wallet_privatekey = wallet_privatekey
    self.wallet_balance = wallet_balance
    self.wallet_history = wallet_history
    self.wallet_public_key = wallet_public_key
    self.wallet_name = wallet_name
    self.wallet = wallet

def create_address(self):

    self.wallet = Wallet.create(self.wallet_name)
    key_usdt = self.wallet.get_key()
    self.wallet_address = key_usdt.get_address

    return self.wallet_address

def check_balance(self):

    return self.wallet_balance

You can use tether explorer api to get balance information.

For example, etherscan.io api ->

It allows you to send 100,000 requests per day and 5 requests per second for free.

https://docs.etherscan.io/getting-started/creating-an-account -> Some api docs

import requests

url = "https://api.etherscan.io/api"
address = "Your address"
apikey = "Your apikey"
params = {"module": "account", "action": "balance", "address": address, "tag": "latest", "apikey": apikey}
response = requests.get(url, params=params).json()

Variable response contains a dictionary with balance data. Example:

{
   "status":"1",
   "message":"OK",
   "result":"40891626854930000000000" 
}

The result is returned in wei. So, we need to divide result by 10^18

total_balance = int(response["result"]) / (10**18)

Where total_balance - your tether wallet balance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM