-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetdecimal.py
More file actions
55 lines (45 loc) · 1.64 KB
/
getdecimal.py
File metadata and controls
55 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from web3 import Web3
from web3.contract import Contract
from web3.exceptions import BadFunctionCallOutput
ERC20_ABI = [
{
"constant": True,
"inputs": [],
"name": "decimals",
"outputs": [{"name": "", "type": "uint8"}],
"payable": False,
"stateMutability": "view",
"type": "function"
}
]
def getDecimals(tokenAddress: str, web3: Web3) -> int:
"""
Get the decimals of an ERC20 token
Args:
tokenAddress: The contract address of the token
web3: Web3 instance connected to a provider
Returns:
int: Number of decimals (typically 18 for ETH, 6 for USDC, etc.)
Raises:
ValueError: If the address is invalid or contract doesn't support decimals
"""
try:
# Validate address
if not web3.is_address(tokenAddress):
raise ValueError(f"Invalid token address: {tokenAddress}")
if tokenAddress.lower() == "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee":
return 18
# Create contract instance
contract: Contract = web3.eth.contract(
address=web3.to_checksum_address(tokenAddress),
abi=ERC20_ABI
)
# Call decimals function
decimals: int = contract.functions.decimals().call()
return decimals
except BadFunctionCallOutput:
raise ValueError("Contract doesn't support decimals function")
except Exception as e:
raise ValueError(f"Error getting decimals: {str(e)}")
if __name__ == "__main__":
print(getDecimals('0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', Web3))