Skip to content

Commit f9a4018

Browse files
authored
Merge pull request #7 from Moonsong-Labs/feat/withdraw-deposits
feat: ✨ Deposit & Withdraw tokens to core
2 parents edccc20 + b368423 commit f9a4018

18 files changed

+522
-303
lines changed

.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Example environment configuration for HyperLiquid API
2+
3+
# Required: Your wallet private key (without 0x prefix)
4+
PRIVATE_KEY=your_private_key_here
5+
6+
# Required: HyperLiquid Strategy contract address
7+
HYPERLIQUID_STRATEGY=0x_strategy_address_here
8+
9+
# Required: Bridge Strategy contract address
10+
BRIDGE_STRATEGY=0x_bridge_address_here
11+
12+
# Optional: Log level (DEBUG, INFO, WARNING, ERROR)
13+
LOGLEVEL=INFO

examples/08_evm_cctp_roundtrip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from __future__ import annotations
44

5+
import json
56
import logging
67
import os
78
from pathlib import Path
89
from typing import Any
9-
import json
1010

1111
from dotenv import load_dotenv
1212

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Example: Deposit and withdraw tokens between HyperEVM and HyperCore."""
2+
3+
from __future__ import annotations
4+
5+
import logging
6+
import os
7+
import time
8+
9+
from dotenv import load_dotenv
10+
11+
from hl_api import HLProtocolEVM
12+
from hl_api.utils import fetch_token_metadata, get_token_evm_address
13+
14+
load_dotenv()
15+
16+
logging.basicConfig(
17+
level=os.getenv("LOGLEVEL", "INFO").upper(),
18+
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
19+
)
20+
21+
logger = logging.getLogger("core_deposit_withdraw")
22+
23+
24+
def main() -> None:
25+
"""Demonstrate deposit and withdrawal between HyperEVM and HyperCore."""
26+
private_key = os.getenv("PRIVATE_KEY")
27+
if not private_key:
28+
raise ValueError("PRIVATE_KEY not found in environment variables")
29+
30+
hl_rpc_url = os.getenv("HYPER_EVM_RPC", "https://rpc.hyperliquid-testnet.xyz/evm")
31+
mn_rpc_url = os.getenv("HL_EVM_RPC", "https://sepolia.drpc.org")
32+
hl_strategy_address = os.getenv("HYPERLIQUID_STRATEGY")
33+
bridge_address = os.getenv("BRIDGE_STRATEGY")
34+
35+
if not hl_strategy_address or not bridge_address:
36+
raise ValueError("HYPERLIQUID_STRATEGY and BRIDGE_STRATEGY must be set")
37+
38+
testnet = "testnet" in hl_rpc_url
39+
40+
client = HLProtocolEVM(
41+
private_key=private_key,
42+
hl_rpc_url=hl_rpc_url,
43+
mn_rpc_url=mn_rpc_url,
44+
testnet=True,
45+
hl_strategy_address=hl_strategy_address,
46+
bridge_strategy_address=bridge_address,
47+
disable_call_verification=True,
48+
receipt_timeout=300, # 5 minutes for withdrawals
49+
)
50+
51+
logger.info("Connecting to HyperLiquid EVM")
52+
client.connect()
53+
54+
try:
55+
token_metadata = fetch_token_metadata(testnet=testnet)
56+
usdc_index = token_metadata.get("USDC")
57+
if usdc_index is None:
58+
logger.error("USDC not found in token metadata")
59+
return
60+
61+
usdc_address = get_token_evm_address("USDC", testnet=testnet)
62+
if not usdc_address:
63+
logger.error("Could not find USDC contract address")
64+
return
65+
66+
logger.info("USDC token index: %d, address: %s", usdc_index, usdc_address)
67+
68+
deposit_amount = 1.0
69+
logger.info("Depositing %.2f USDC to HyperCore", deposit_amount)
70+
deposit_response = client.deposit_token_to_core(
71+
token_address=usdc_address,
72+
token_index=usdc_index,
73+
amount=deposit_amount,
74+
)
75+
76+
if deposit_response.success:
77+
logger.info("Deposit successful: %s", deposit_response.transaction_hash)
78+
else:
79+
logger.error("Deposit failed: %s", deposit_response.error)
80+
raise RuntimeError("Deposit failed, aborting")
81+
82+
logger.info("Waiting 5 seconds before withdrawal")
83+
time.sleep(5)
84+
85+
withdraw_amount = 1.0
86+
logger.info("Withdrawing %.2f USDC back to HyperEVM", withdraw_amount)
87+
logger.info("Note: Withdrawals can take several minutes to confirm")
88+
89+
withdraw_response = client.withdraw_token_to_evm(
90+
token_index=usdc_index,
91+
amount=withdraw_amount,
92+
)
93+
94+
if withdraw_response.success:
95+
logger.info("Withdrawal initiated: %s", withdraw_response.transaction_hash)
96+
logger.info("Withdrawal will be processed in a few minutes")
97+
else:
98+
logger.error("Withdrawal failed: %s", withdraw_response.error)
99+
raise RuntimeError("Withdrawal failed")
100+
101+
finally:
102+
client.disconnect()
103+
logger.info("Disconnected from HyperLiquid EVM")
104+
105+
106+
if __name__ == "__main__":
107+
main()

0 commit comments

Comments
 (0)