-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtrade_tools.py
More file actions
111 lines (91 loc) · 3.36 KB
/
trade_tools.py
File metadata and controls
111 lines (91 loc) · 3.36 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from models import Balances, OrderResponse, TraderState
from py_clob_client.client import ClobClient
from py_clob_client.clob_types import OrderType, OrderArgs
import os
import logging
from py_clob_client.constants import POLYGON, AMOY
from dotenv import load_dotenv
from web3 import Web3
logger = logging.getLogger(__name__)
load_dotenv()
class PolymarketTrader:
def __init__(self):
# Initialize CLOB client
# For Mumbai testnet
is_production = (
True # os.getenv("ENVIRONMENT", "development").lower() == "production"
)
key = os.getenv("POLYMARKET_PRIVATE_KEY")
if is_production:
host = "https://clob.polymarket.com"
chain_id = POLYGON
self.client = ClobClient(
host=host,
key=key,
chain_id=chain_id,
signature_type=1,
funder=os.getenv("POLYMARKET_PROXY_ADDRESS"),
)
self.client.set_api_creds(self.client.create_or_derive_api_creds())
else:
# Mumbai testnet setup
host = "https://clob.polymarket.com"
chain_id = AMOY
self.client = ClobClient(
host=host,
key=key,
chain_id=chain_id,
signature_type=1,
funder=os.getenv("POLYMARKET_PROXY_ADDRESS"),
)
self.client.set_api_creds(self.client.create_or_derive_api_creds())
def _trade_execute(order_args: OrderArgs):
trader = PolymarketTrader()
signed_order = trader.client.create_order(order_args)
resp = trader.client.post_order(signed_order, OrderType.GTC)
return resp
def trade_execution(state: TraderState):
"""Execute trades based on market analysis recommendation."""
try:
# Create order arguments
order_args: OrderArgs = state.order_details.order_args
resp = _trade_execute(order_args)
return {
"order_response": OrderResponse(
status="success",
response=resp,
)
}
except Exception as e:
logger.error(f"Trade execution failed: {str(e)}")
return {
"order_response": OrderResponse(
status="failure", response={"failure": str(e)}
)
}
def get_balances(state: Balances):
"""Get the current USDC balance of the trader"""
# Get RPC URL from environment variable
rpc_url = os.getenv("POLYGON_RPC_URL", "https://polygon-rpc.com")
w3 = Web3(Web3.HTTPProvider(rpc_url))
# USDC contract address on Polygon
USDC_ADDRESS = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"
# USDC ABI - we only need the balanceOf function
USDC_ABI = [
{
"inputs": [{"name": "account", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function",
}
]
# wallet_address = trader.client.get_address()
wallet_address = os.getenv("POLYMARKET_PROXY_ADDRESS")
# Create contract instance
usdc_contract = w3.eth.contract(address=USDC_ADDRESS, abi=USDC_ABI)
# Get balance
balance = usdc_contract.functions.balanceOf(wallet_address).call()
# USDC has 6 decimals
balance_formatted = balance / 1e6
return {"balances": {"USDC": balance_formatted}}