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
24 changes: 24 additions & 0 deletions examples/basic_send_asset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import example_utils

from hyperliquid.utils import constants

SOURCE_DEX = ""
DESTINATION_DEX = "test"


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

if exchange.account_address != exchange.wallet.address:
raise Exception("Agents do not have permission to perform internal transfers")

# Transfer 1.23 USDC from SOURCE_DEX to the zero address on DESTINATION_DEX for demonstration purposes
# Note that the collateral token for SOURCE_DEX and DESTINATION_DEX must match
transfer_result = exchange.send_asset(
"0x0000000000000000000000000000000000000000", SOURCE_DEX, DESTINATION_DEX, "USDC", 1.23
)
print(transfer_result)


if __name__ == "__main__":
main()
22 changes: 22 additions & 0 deletions examples/basic_spot_to_builder_deployed_perp_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 spot wallet to perp wallet
transfer_result = exchange.send_asset(address, "spot", DUMMY_DEX, COLLATERAL_TOKEN, 1.23)
print(transfer_result)

# Transfer 1.23 collateral token from perp wallet back to spot wallet
transfer_result = exchange.send_asset(address, DUMMY_DEX, "spot", COLLATERAL_TOKEN, 1.23)
print(transfer_result)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion examples/multi_sig_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def main():
exchange.base_url == constants.MAINNET_API_URL,
None,
timestamp,
exchange.expires_after,
multi_sig_user,
address,
exchange.expires_after,
)
signatures.append(signature)

Expand Down
2 changes: 1 addition & 1 deletion examples/multi_sig_register_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def main():
exchange.base_url == constants.MAINNET_API_URL,
None,
timestamp,
exchange.expires_after,
multi_sig_user,
address,
exchange.expires_after,
)
signatures.append(signature)

Expand Down
14 changes: 10 additions & 4 deletions examples/perp_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
def main():
address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True)

# get perp deploy auction status which includes auction start and gas information
perp_deploy_auction_status = info.query_perp_deploy_auction_status()
print("perp deploy auction status:", perp_deploy_auction_status)

# Step 1: Registering a Perp Dex and Assets
#
# Takes part in the perp deploy auction and if successful, registers asset "TEST0".
Expand Down Expand Up @@ -50,10 +54,12 @@ def main():
f"{DUMMY_DEX}:TEST0": "12.0",
f"{DUMMY_DEX}:TEST1": "1.0",
},
{
f"{DUMMY_DEX}:TEST1": "3.0",
f"{DUMMY_DEX}:TEST0": "14.0",
},
[
{
f"{DUMMY_DEX}:TEST1": "3.0",
f"{DUMMY_DEX}:TEST0": "14.0",
}
],
)
print("set oracle result:", set_oracle_result)

Expand Down
26 changes: 14 additions & 12 deletions hyperliquid/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
sign_convert_to_multi_sig_user_action,
sign_l1_action,
sign_multi_sig_action,
sign_perp_dex_class_transfer_action,
sign_send_asset_action,
sign_spot_transfer_action,
sign_token_delegate_action,
sign_usd_class_transfer_action,
Expand Down Expand Up @@ -456,21 +456,25 @@ 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:
def send_asset(self, destination: str, source_dex: str, destination_dex: str, token: str, amount: float) -> Any:
"""
For the default perp dex use the empty string "" as name. For spot use "spot".
Token must match the collateral token if transferring to or from a perp dex.
"""
timestamp = get_timestamp_ms()
str_amount = str(amount)
if self.vault_address:
str_amount += f" subaccount:{self.vault_address}"

action = {
"type": "PerpDexClassTransfer",
"dex": dex,
"type": "sendAsset",
"destination": destination,
"sourceDex": source_dex,
"destinationDex": destination_dex,
"token": token,
"amount": str_amount,
"toPerp": to_perp,
"fromSubAccount": self.vault_address if self.vault_address else "",
"nonce": timestamp,
}
signature = sign_perp_dex_class_transfer_action(self.wallet, action, self.base_url == MAINNET_API_URL)
signature = sign_send_asset_action(self.wallet, action, self.base_url == MAINNET_API_URL)
return self._post_action(
action,
signature,
Expand Down Expand Up @@ -917,13 +921,11 @@ def perp_deploy_set_oracle(
self,
dex: str,
oracle_pxs: Dict[str, str],
mark_pxs: Optional[Dict[str, str]],
all_mark_pxs: List[Dict[str, str]],
) -> Any:
timestamp = get_timestamp_ms()
oracle_pxs_wire = sorted(list(oracle_pxs.items()))
mark_pxs_wire = None
if mark_pxs is not None:
mark_pxs_wire = sorted(list(mark_pxs.items()))
mark_pxs_wire = [sorted(list(mark_pxs.items())) for mark_pxs in all_mark_pxs]
action = {
"type": "perpDeploy",
"setOracle": {
Expand Down
3 changes: 3 additions & 0 deletions hyperliquid/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,9 @@ 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 query_perp_deploy_auction_status(self) -> Any:
return self.post("/info", {"type": "perpDeployAuctionStatus"})

def _remap_coin_subscription(self, subscription: Subscription) -> None:
if (
subscription["type"] == "l2Book"
Expand Down
14 changes: 8 additions & 6 deletions hyperliquid/utils/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@
{"name": "nonce", "type": "uint64"},
]

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

Expand Down Expand Up @@ -350,12 +352,12 @@ def sign_usd_class_transfer_action(wallet, action, is_mainnet):
)


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

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.15.0"
version = "0.16.0"
description = "SDK for Hyperliquid API trading with Python."
readme = "README.md"
authors = ["Hyperliquid <[email protected]>"]
Expand Down