Etherscan

USDC etherscan ABI 缺少小數功能

  • February 20, 2022

我正在從 etherscan 中提取 USDC ABI,然後將其傳遞給 web3

erc20_dict = {
   'USDC': '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
}
url = f'https://api.etherscan.io/api?module=contract&action=getabi&address={contract_address}&apikey={apikey}'
ret = requests.get(url)
ABI = ret.json()['result']
contract = w3.eth.contract(address=w3.toChecksumAddress(erc20_dict['USDC']), abi=ABI)
print(contract.functions.decimals().call())

但我收到以下錯誤:

web3.exceptions.ABIFunctionNotFound: ("The function 'decimals' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?')

etherscan 上的 USDC ABI 不包含小數函式是否有原因?我想通用地使用這個函式來為令牌/合約提取 ABI,所以使用通用的 erc20 ABI 並不是最好的解決方案。有任何想法嗎?謝謝!

這是因為 USDC 使用代理合約。代理合約將用於delegatecall包含所有 ERC-20 功能的實現合約。因此,在您的程式碼中,您不需要從 etherscan 獲取 ABI,您可以使用基本的 ERC-20 合約的 ABI,這應該適用於所有 ERC-20 代幣合約。這也將幫助您避免對每個合約進行外部 API 呼叫。

引用自:https://ethereum.stackexchange.com/questions/122085