-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
95 lines (73 loc) · 2.51 KB
/
Copy pathutils.py
File metadata and controls
95 lines (73 loc) · 2.51 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Utility functions for CDP Wallet operations
"""
import os
import asyncio
from dotenv import load_dotenv
from cdp import CdpClient
# Load environment variables
load_dotenv()
# Constants
NETWORK = "base-sepolia"
# Singleton CDP client instance
_cdp_instance = None
async def get_cdp_client() -> CdpClient:
"""
Get or create a singleton CDP client instance
"""
global _cdp_instance
if _cdp_instance is None:
# Verify environment variables
api_key_id = os.getenv('CDP_API_KEY_ID')
api_key_secret = os.getenv('CDP_API_KEY_SECRET')
wallet_secret = os.getenv('CDP_WALLET_SECRET')
if not all([api_key_id, api_key_secret, wallet_secret]):
raise ValueError("Missing required environment variables. Please check your .env file.")
_cdp_instance = CdpClient()
return _cdp_instance
async def fund_account_from_faucet(address: str):
"""
Request testnet funds from the CDP faucet
"""
cdp = await get_cdp_client()
try:
await cdp.evm.request_faucet(
address=address,
network=NETWORK,
token="eth"
)
await asyncio.sleep(10) # Wait for funds to confirm
except Exception as e:
print(f"Faucet failed: {str(e)}")
async def fetch_balance(address: str):
"""
Fetch and display the balance of an account
"""
cdp = await get_cdp_client()
try:
balances = await cdp.evm.list_token_balances(
address=address,
network=NETWORK
)
if balances:
balance_list = balances.balances if hasattr(balances, 'balances') else balances
print(f"{address}:")
for balance in balance_list:
symbol = balance.token.symbol if hasattr(balance, 'token') else 'ETH'
if hasattr(balance, 'amount') and hasattr(balance.amount, 'amount'):
amount = balance.amount.amount
decimals = balance.amount.decimals if hasattr(balance.amount, 'decimals') else 18
readable_amount = amount / (10 ** decimals)
print(f" {symbol}: {readable_amount}")
return balances
except Exception as e:
print(f"⚠ Balance fetch failed: {str(e)}")
return None
async def close_cdp_client():
"""
Close the CDP client connection
"""
global _cdp_instance
if _cdp_instance:
await _cdp_instance.close()
_cdp_instance = None