File size: 2,183 Bytes
80fa9cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from mcp.server.fastmcp import FastMCP
from hype_accounts import setup, long_short_shares_mkt_price

mcp = FastMCP("hype_accounts_server")

@mcp.tool()
async def get_account_details() -> dict:
    """Get cash balance, cryptocurrency holdings and PNL of hyperliquid account.
    """
    account_details_dict = {
        "holdings": None,
        "cash_balance": None,
        "profit_and_loss": None
    }
    address, info, exchange = setup()
    user_state = info.user_state(address)
    account_details_dict["cash_balance"] = user_state['marginSummary']['accountValue']
    if user_state['assetPositions'] == []:
        account_details_dict["holdings"] = "No holdings"
    else:
        account_details_dict["holdings"] = user_state['assetPositions']
    account_details_dict["profit_and_loss"] = exchange.info.portfolio(address)[3][1]['pnlHistory'][-1][1]

    return account_details_dict

@mcp.tool()
async def long_perps_mkt_price(symbol: str, value:float, leverage:int) -> str:
    """Create a long position for a cryptocurrency perpetual contract at market price.

    Args:
        symbol: The symbol of the cryptocurrency (e.g., "HYPE","ETC","BTC","XRP").
        value: The USD value to long.
        leverage: The leverage to use for the position.
    """
    return str(long_short_shares_mkt_price(symbol, value, leverage, is_buy=True))

@mcp.tool()
async def short_perps_mkt_price(symbol: str, value:float, leverage:int) -> str:
    """Create a short position for a cryptocurrency perpetual contract at market price.

    Args:
        symbol: The symbol of the cryptocurrency (e.g., "HYPE","ETC","BTC").
        value: The USD value to short.
        leverage: The leverage to use for the position.
    """
    return str(long_short_shares_mkt_price(symbol, value, leverage, is_buy=False))

@mcp.tool()
async def close_perps_mkt_price(symbol: str) -> str:
    """Close the position for a cryptocurrency perpetual contract at market price.

    Args:
        symbol: The symbol of the cryptocurrency (e.g., "HYPE","ETC","BTC").
    """
    _, _, exchange = setup()
    return str(exchange.market_close(symbol))

if __name__ == "__main__":
    mcp.run(transport='stdio')