fix: use raw eth_call for USDC balance (web3 7.x ABI bug)
This commit is contained in:
@@ -366,34 +366,23 @@ def wait_for_receipt(w3, tx_hash: str, timeout=120):
|
|||||||
# =============================================================================
|
# =============================================================================
|
||||||
# BALANCE CHECK
|
# BALANCE CHECK
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
def get_usdc_balance(wallet: str) -> float:
|
def get_usdc_balance(wallet: str) -> float:
|
||||||
"""Get USDC.e balance on Polygon."""
|
"""Get USDC.e balance on Polygon via raw eth_call (avoids web3 contract ABI issues)."""
|
||||||
w3 = get_w3()
|
w3 = get_w3()
|
||||||
usdc_abi = [
|
wallet_checksum = Web3.to_checksum_address(wallet)
|
||||||
{
|
usdc_checksum = Web3.to_checksum_address(USDC_ADDRESS)
|
||||||
"name": "balanceOf",
|
|
||||||
"inputs": [{"name": "account", "type": "address"}],
|
#balanceOf(address) — the "data" is the function selector hash + padded address
|
||||||
"outputs": [{"name": "", "type": "uint256"}],
|
selector = "0x70a08231" # balanceOf(address)
|
||||||
"stateMutability": "view",
|
data = selector + wallet_checksum[2:].lower().rjust(64, '0')
|
||||||
"type": "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "decimals",
|
|
||||||
"inputs": [],
|
|
||||||
"outputs": [{"name": "", "type": "uint8"}],
|
|
||||||
"stateMutability": "view",
|
|
||||||
"type": "function"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
usdc = w3.eth.contract(
|
|
||||||
address=Web3.to_checksum_address(USDC_ADDRESS),
|
|
||||||
abi=usdc_abi
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
decimals = usdc.functions.decimals().call()
|
result = w3.eth.call({
|
||||||
bal = usdc.functions.balanceOf(Web3.to_checksum_address(wallet)).call()
|
"to": usdc_checksum,
|
||||||
return bal / (10 ** decimals)
|
"data": data,
|
||||||
|
})
|
||||||
|
bal = int.from_bytes(result, "big")
|
||||||
|
return bal / 1e6 # USDC.e = 6 decimals
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
warn(f"Balance check failed: {e}")
|
warn(f"Balance check failed: {e}")
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|||||||
Reference in New Issue
Block a user