Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/basic_spot_to_perp_with_builder_deployed_dex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import example_utils

from hyperliquid.utils import constants

DUMMY_DEX = "test"
COLLATERAL_TOKEN = "USDC" # nosec


def main():
address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True)

# Transfer 1.23 USDC from perp wallet to spot wallet
transfer_result = exchange.perp_dex_class_transfer(DUMMY_DEX, COLLATERAL_TOKEN, 1.23, False)
print(transfer_result)

# Transfer 1.23 collateral token from spot wallet to perp wallet
transfer_result = exchange.perp_dex_class_transfer(DUMMY_DEX, COLLATERAL_TOKEN, 1.23, True)
print(transfer_result)


if __name__ == "__main__":
main()
22 changes: 22 additions & 0 deletions hyperliquid/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
sign_convert_to_multi_sig_user_action,
sign_l1_action,
sign_multi_sig_action,
sign_perp_dex_class_transfer_action,
sign_spot_transfer_action,
sign_token_delegate_action,
sign_usd_class_transfer_action,
Expand Down Expand Up @@ -455,6 +456,27 @@ def usd_class_transfer(self, amount: float, to_perp: bool) -> Any:
timestamp,
)

def perp_dex_class_transfer(self, dex: str, token: str, amount: float, to_perp: bool) -> Any:
timestamp = get_timestamp_ms()
str_amount = str(amount)
if self.vault_address:
str_amount += f" subaccount:{self.vault_address}"

action = {
"type": "PerpDexClassTransfer",
"dex": dex,
"token": token,
"amount": str_amount,
"toPerp": to_perp,
"nonce": timestamp,
}
signature = sign_perp_dex_class_transfer_action(self.wallet, action, self.base_url == MAINNET_API_URL)
return self._post_action(
action,
signature,
timestamp,
)

def sub_account_transfer(self, sub_account_user: str, is_deposit: bool, usd: int) -> Any:
timestamp = get_timestamp_ms()
sub_account_transfer_action = {
Expand Down
6 changes: 3 additions & 3 deletions hyperliquid/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def query_sub_accounts(self, user: str) -> Any:
def query_user_to_multi_sig_signers(self, multi_sig_user: str) -> Any:
return self.post("/info", {"type": "userToMultiSigSigners", "user": multi_sig_user})

def _validate_subscription(self, subscription: Subscription) -> None:
def _remap_coin_subscription(self, subscription: Subscription) -> None:
if (
subscription["type"] == "l2Book"
or subscription["type"] == "trades"
Expand All @@ -608,14 +608,14 @@ def _validate_subscription(self, subscription: Subscription) -> None:
subscription["coin"] = self.name_to_coin[subscription["coin"]]

def subscribe(self, subscription: Subscription, callback: Callable[[Any], None]) -> int:
self._validate_subscription(subscription)
self._remap_coin_subscription(subscription)
if self.ws_manager is None:
raise RuntimeError("Cannot call subscribe since skip_ws was used")
else:
return self.ws_manager.subscribe(subscription, callback)

def unsubscribe(self, subscription: Subscription, subscription_id: int) -> bool:
self._validate_subscription(subscription)
self._remap_coin_subscription(subscription)
if self.ws_manager is None:
raise RuntimeError("Cannot call unsubscribe since skip_ws was used")
else:
Expand Down
22 changes: 21 additions & 1 deletion hyperliquid/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@
{"name": "nonce", "type": "uint64"},
]

PERP_DEX_CLASS_TRANSFER_SIGN_TYPES = [
{"name": "hyperliquidChain", "type": "string"},
{"name": "dex", "type": "string"},
{"name": "token", "type": "string"},
{"name": "amount", "type": "string"},
{"name": "toPerp", "type": "bool"},
{"name": "nonce", "type": "uint64"},
]

TOKEN_DELEGATE_TYPES = [
{"name": "hyperliquidChain", "type": "string"},
{"name": "validator", "type": "address"},
Expand Down Expand Up @@ -341,6 +350,16 @@ def sign_usd_class_transfer_action(wallet, action, is_mainnet):
)


def sign_perp_dex_class_transfer_action(wallet, action, is_mainnet):
return sign_user_signed_action(
wallet,
action,
PERP_DEX_CLASS_TRANSFER_SIGN_TYPES,
"HyperliquidTransaction:PerpDexClassTransfer",
is_mainnet,
)


def sign_convert_to_multi_sig_user_action(wallet, action, is_mainnet):
return sign_user_signed_action(
wallet,
Expand Down Expand Up @@ -392,7 +411,8 @@ def sign_token_delegate_action(wallet, action, is_mainnet):


def sign_inner(wallet, data):
signed = wallet.sign_typed_data(full_message=data)
structured_data = encode_typed_data(full_message=data)
signed = wallet.sign_message(structured_data)
return {"r": to_hex(signed["r"]), "s": to_hex(signed["s"]), "v": signed["v"]}


Expand Down
4 changes: 2 additions & 2 deletions hyperliquid/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
AllMidsData = TypedDict("AllMidsData", {"mids": Dict[str, str]})
AllMidsMsg = TypedDict("AllMidsMsg", {"channel": Literal["allMids"], "data": AllMidsData})
L2Level = TypedDict("L2Level", {"px": str, "sz": str, "n": int})
BboData = TypedDict("BboData", {"coin": str, "time": int, "bbo": Tuple[Optional[L2Level], Optional[L2Level]]})
BboMsg = TypedDict("BboMsg", {"channel": Literal["bbo"], "data": BboData})
L2BookData = TypedDict("L2BookData", {"coin": str, "levels": Tuple[List[L2Level], List[L2Level]], "time": int})
L2BookMsg = TypedDict("L2BookMsg", {"channel": Literal["l2Book"], "data": L2BookData})
BboData = TypedDict("BboData", {"coin": str, "time": int, "bbo": Tuple[Optional[L2Level], Optional[L2Level]]})
BboMsg = TypedDict("BboMsg", {"channel": Literal["bbo"], "data": BboData})
PongMsg = TypedDict("PongMsg", {"channel": Literal["pong"]})
Trade = TypedDict("Trade", {"coin": str, "side": Side, "px": str, "sz": int, "hash": str, "time": int})
TradesMsg = TypedDict("TradesMsg", {"channel": Literal["trades"], "data": List[Trade]})
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "hyperliquid-python-sdk"
version = "0.14.1"
version = "0.15.0"
description = "SDK for Hyperliquid API trading with Python."
readme = "README.md"
authors = ["Hyperliquid <hello@hyperliquid.xyz>"]
Expand Down