-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathfunction_helper.py
More file actions
156 lines (137 loc) · 6.18 KB
/
function_helper.py
File metadata and controls
156 lines (137 loc) · 6.18 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# injective_functions/utils/function_mapping.py
from typing import Dict, Tuple, Any, Optional
import json
from pathlib import Path
class InjectiveFunctionMapper:
# Map function names to (client_type, method_name)
FUNCTION_MAP: Dict[str, Tuple[str, str]] = {
# Trader functions
"place_derivative_limit_order": ("trader", "place_derivative_limit_order"),
"place_derivative_market_order": ("trader", "place_derivative_market_order"),
"place_spot_limit_order": ("trader", "place_spot_limit_order"),
"place_spot_market_order": ("trader", "place_spot_market_order"),
"cancel_derivative_limit_order": ("trader", "cancel_derivative_limit_order"),
"cancel_spot_limit_order": ("trader", "cancel_spot_limit_order"),
# Exchange functions
"get_subaccount_deposits": ("exchange", "get_subaccount_deposits"),
"get_aggregate_market_volumes": ("exchange", "get_aggregate_market_volumes"),
"get_aggregate_account_volumes": ("exchange", "get_aggregate_account_volumes"),
"get_subaccount_orders": ("exchange", "get_subaccount_orders"),
"get_historical_orders": ("exchange", "get_historical_orders"),
"get_mid_price_and_tob_derivatives_market": (
"exchange",
"get_mid_price_and_tob_derivatives_market",
),
"get_mid_price_and_tob_spot_market": (
"exchange",
"get_mid_price_and_tob_spot_market",
),
"get_derivatives_orderbook": ("exchange", "get_derivatives_orderbook"),
"get_spot_orderbook": ("exchange", "get_spot_orderbook"),
"trader_derivative_orders": ("exchange", "trader_derivative_orders"),
"trader_derivative_orders_by_hash": (
"exchange",
"trader_derivative_orders_by_hash",
),
"trader_spot_orders": ("exchange", "trader_spot_orders"),
"trader_spot_orders_by_hash": ("exchange", "trader_spot_orders_by_hash"),
# Bank functions
"query_balances": ("bank", "query_balances"),
"transfer_funds": ("bank", "transfer_funds"),
"query_spendable_balances": ("bank", "query_spendable_balances"),
"query_total_supply": ("bank", "query_total_supply"),
# Staking functions
"stake_tokens": ("staking", "stake_tokens"),
"compound_rewards": ("staking", "compound_rewards"),
# Auction functions
"send_bid_auction": ("auction", "send_bid_auction"),
"fetch_auctions": ("auction", "fetch_auctions"),
"fetch_latest_auction": ("auction", "fetch_latest_auction"),
"fetch_auction_bids": ("auction", "fetch_auction_bids"),
# Authz functions
"grant_address_auth": ("authz", "grant_address_auth"),
"revoke_address_auth": ("authz", "revoke_address_auth"),
"fetch_grants": ("authz", "fetch_grants"),
# Token factory functions
"create_denom": ("token_factory", "create_denom"),
"mint": ("token_factory", "mint"),
"burn": ("token_factory", "burn"),
"set_denom_metadata": ("token_factory", "set_denom_metadata"),
}
@classmethod
def get_function_mapping(cls, function_name: str) -> Optional[Tuple[str, str]]:
"""Get the client type and method name for a given function"""
return cls.FUNCTION_MAP.get(function_name)
@classmethod
def validate_function(cls, function_name: str) -> bool:
"""Check if a function name is valid"""
return function_name in cls.FUNCTION_MAP
@classmethod
def get_all_client_types(cls) -> set:
"""Get all unique client types"""
return {client_type for client_type, _ in cls.FUNCTION_MAP.values()}
@classmethod
def get_functions_for_client(cls, client_type: str) -> list:
"""Get all functions for a specific client type"""
return [
func_name
for func_name, (client, _) in cls.FUNCTION_MAP.items()
if client == client_type
]
class FunctionSchemaLoader:
@staticmethod
def load_schemas(schema_paths: list) -> dict:
"""Load and combine function schemas from multiple files"""
combined_schemas = {"functions": []}
for path in schema_paths:
try:
with open(path, "r") as f:
schema = json.load(f)
if "functions" in schema:
combined_schemas["functions"].extend(schema["functions"])
except Exception as e:
print(f"Error loading schema from {path}: {str(e)}")
return combined_schemas["functions"]
@staticmethod
def validate_schema(schema: dict) -> bool:
"""Validate that a schema has the required structure"""
if not isinstance(schema, dict):
return False
if "functions" not in schema:
return False
if not isinstance(schema["functions"], list):
return False
return True
class FunctionExecutor:
@staticmethod
async def execute_function(
clients: Dict[str, Any], function_name: str, arguments: dict
) -> dict:
"""Execute a function with the appropriate client"""
try:
# Get the function mapping
mapping = InjectiveFunctionMapper.get_function_mapping(function_name)
if not mapping:
return {"error": f"Function {function_name} not implemented"}
client_type, method_name = mapping
# Get the client
client = clients.get(client_type)
if not client:
return {"error": f"Client type {client_type} not available"}
# Get and execute the method
method = getattr(client, method_name, None)
if not method:
return {
"error": f"Method {method_name} not found in {client_type} client"
}
return await method(**arguments)
except Exception as e:
return {
"error": str(e),
"success": False,
"details": {
"function": function_name,
"arguments": arguments,
"client_type": client_type if "client_type" in locals() else None,
},
}