DEV Community

drake
drake

Posted on

1

Python查询钱包地址余额

需要用到 三方库

  • 1、查询 BSC 链 BNB 余额
from web3 import Web3
from web3.middleware import geth_poa_middleware
# r('https://bsc-dataseed3.binance.org/'))
bsc_url = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc_url))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
wallet_address = '你的钱包地址'
# Web3.toChecksumAddress(wallet_address)
checksum_address = Web3.to_checksum_address(wallet_address)
balance = web3.eth.get_balance(checksum_address)

# Convert the balance from wei to BNB
balance_bnb = web3.from_wei(balance, 'ether')

# Print the balance
print(f"Wallet balance: {balance_bnb} BNB")
Enter fullscreen mode Exit fullscreen mode
  • 2、查询 BSC 链 USDT 余额
from web3 import Web3
from web3.middleware import geth_poa_middleware

bsc_url = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc_url))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

wallet_address = '你的钱包地址'
checksum_address = Web3.to_checksum_address(wallet_address)

# USDT contract address on BSC
usdt_contract_address = '0x55d398326f99059ff775485246999027b3197955'

# ABI for the balanceOf function
usdt_abi = [{"constant":True,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"}]
usdt_contract_address_checksum = Web3.to_checksum_address(usdt_contract_address)
usdt_contract = web3.eth.contract(address=usdt_contract_address_checksum, abi=usdt_abi)

# Get the balance of the wallet address
balance = usdt_contract.functions.balanceOf(checksum_address).call()

# Convert the balance from wei to USDT
balance_usdt = web3.from_wei(balance, 'ether')

# Print the balance
print(f"Wallet balance: {balance_usdt} USDT")

Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs