hyperliquid.core_writer
Documentation for eth_defi.hyperliquid.core_writer Python module.
CoreWriter transaction encoding for Hypercore native vaults.
Encodes raw action bytes for the CoreWriter system contract at
0x3333333333333333333333333333333333333333 on HyperEVM.
The raw action format is:
byte 0: version (always
1)bytes 1-3: action ID (big-endian uint24)
bytes 4+:
abi.encode(action-specific parameters)
See /README-Hypercore-guard for the full deposit/withdrawal flow.
Example:
from eth_defi.hyperliquid.core_writer import (
encode_vault_deposit,
encode_transfer_usd_class,
CORE_WRITER_ADDRESS,
)
# Build the raw action bytes for a vault deposit
raw_action = encode_vault_deposit(vault_address, usdc_amount_wei)
# Call CoreWriter.sendRawAction(raw_action) via the guard
core_writer = web3.eth.contract(
address=CORE_WRITER_ADDRESS,
abi=core_writer_abi,
)
fn_call = core_writer.functions.sendRawAction(raw_action)
Module Attributes
CoreWriter system contract address on HyperEVM |
|
CoreDepositWallet addresses by chain ID. |
|
USDC token index on HyperCore |
|
Spot dex constant (type(uint32).max) |
|
Minimum USDC deposit into a Hypercore vault (raw, 6 decimals). |
Functions
|
Build a multicall to activate a Safe's HyperCore account. |
|
Build a single multicall transaction for the full Hypercore deposit flow. |
|
Build phase 1 of a two-phase Hypercore deposit: bridge USDC to HyperCore spot. |
|
Build phase 2 of a two-phase Hypercore deposit: spot to perp to vault. |
Build a single multicall transaction for the full Hypercore withdrawal flow. |
|
|
Encode a CoreWriter spotSend action (action ID 6). |
|
Encode a CoreWriter transferUsdClass action (action ID 7). |
|
Encode a CoreWriter vaultTransfer deposit action (action ID 2). |
|
Encode a CoreWriter vaultTransfer withdraw action (action ID 2). |
|
Get a Contract instance for the CoreDepositWallet. |
|
Get a Contract instance for the CoreWriter system contract. |
- CORE_WRITER_ADDRESS: eth_typing.evm.HexAddress = '0x3333333333333333333333333333333333333333'
CoreWriter system contract address on HyperEVM
- CORE_DEPOSIT_WALLET: dict[int, eth_typing.evm.HexAddress] = {998: '0x0B80659a4076E9E93C7DbE0f10675A16a3e5C206', 999: '0x6B9E773128f453f5c2C60935Ee2DE2CBc5390A24'}
CoreDepositWallet addresses by chain ID. Chain 999 = HyperEVM mainnet, chain 998 = HyperEVM testnet.
- USDC_TOKEN_INDEX = 0
USDC token index on HyperCore
- SPOT_DEX = 4294967295
Spot dex constant (type(uint32).max)
- MINIMUM_VAULT_DEPOSIT = 5000000
Minimum USDC deposit into a Hypercore vault (raw, 6 decimals). Hyperliquid silently rejects vaultTransfer deposits below this amount. Determined by reverse-engineering the Hyperliquid web UI.
- encode_vault_deposit(vault, usdc_amount_wei)
Encode a CoreWriter vaultTransfer deposit action (action ID 2).
- Parameters
vault (Union[eth_typing.evm.HexAddress, str]) – Hypercore native vault address.
usdc_amount_wei (int) – USDC amount in HyperCore wei (uint64). Note: HyperCore uses different decimal representations than EVM.
- Returns
Raw action bytes for
CoreWriter.sendRawAction().- Raises
AssertionError – If the deposit amount is below
MINIMUM_VAULT_DEPOSIT.- Return type
- encode_vault_withdraw(vault, usdc_amount_wei)
Encode a CoreWriter vaultTransfer withdraw action (action ID 2).
- Parameters
vault (Union[eth_typing.evm.HexAddress, str]) – Hypercore native vault address.
usdc_amount_wei (int) – USDC amount in HyperCore wei (uint64).
- Returns
Raw action bytes for
CoreWriter.sendRawAction().- Return type
- encode_transfer_usd_class(amount_wei, to_perp)
Encode a CoreWriter transferUsdClass action (action ID 7).
Moves USDC between spot and perp accounts on HyperCore.
- encode_spot_send(destination, token_id, amount_wei)
Encode a CoreWriter spotSend action (action ID 6).
Sends tokens from HyperCore spot to an address. Used to bridge tokens from Core back to EVM (destination = EVM address).
- Parameters
destination (Union[eth_typing.evm.HexAddress, str]) – Recipient address (typically the Safe address for bridging back).
token_id (int) – HyperCore token index (0 = USDC).
amount_wei (int) – Amount in HyperCore wei (uint64).
- Returns
Raw action bytes for
CoreWriter.sendRawAction().- Return type
- get_core_deposit_wallet_contract(web3, address)
Get a Contract instance for the CoreDepositWallet.
Uses the MockCoreDepositWallet ABI which has the same
deposit(uint256,uint32)signature as the real CoreDepositWallet.- Parameters
web3 (web3.main.Web3) – Web3 connection.
address (Union[eth_typing.evm.HexAddress, str]) – CoreDepositWallet address (use
CORE_DEPOSIT_WALLETwith chain ID).
- Returns
Contract instance with the CoreDepositWallet ABI.
- Return type
web3.contract.contract.Contract
- get_core_writer_contract(web3)
Get a Contract instance for the CoreWriter system contract.
Uses the MockCoreWriter ABI which exposes the same
sendRawAction(bytes)interface as the real CoreWriter precompile.- Parameters
web3 (web3.main.Web3) – Web3 connection.
- Returns
Contract instance at
CORE_WRITER_ADDRESS.- Return type
web3.contract.contract.Contract
- build_hypercore_deposit_multicall(lagoon_vault, evm_usdc_amount, hypercore_usdc_amount, vault_address, check_activation=False, chain_id=None, asset_address=None)
Build a single multicall transaction for the full Hypercore deposit flow.
Warning
The Safe must be activated on HyperCore before using the batched deposit. Pass
check_activation=Trueto automatically verify, or useactivate_account()beforehand. Without activation, deposited USDC gets permanently stuck in EVM escrow.Batches the 4-step deposit into one EVM transaction:
approve(CoreDepositWallet, amount)— approve USDC transferCoreDepositWallet.deposit(amount, SPOT_DEX)— bridge USDC to HyperCore spotCoreWriter.sendRawAction(transferUsdClass)— move USDC from spot to perpCoreWriter.sendRawAction(vaultTransfer)— deposit into vault
When the EVM block finishes execution, all queued CoreWriter actions are processed sequentially on HyperCore (~47k gas per action).
For extra safety under heavy HyperCore load, use the two-phase approach with
build_hypercore_deposit_phase1()andbuild_hypercore_deposit_phase2()withwait_for_evm_escrow_clear()between them.Derives all contract instances internally from the
LagoonVault:modulefromLagoonVault.trading_strategy_moduleusdc_contractfrom the vault’s underlying asset addresscore_deposit_walletfrom the chain ID (mainnet vs testnet)core_writerat the system addressCORE_WRITER_ADDRESS
Example:
from eth_defi.hyperliquid.core_writer import build_hypercore_deposit_multicall fn = build_hypercore_deposit_multicall( lagoon_vault=lagoon_vault, evm_usdc_amount=10_000 * 10**6, hypercore_usdc_amount=10_000 * 10**6, vault_address="0x...", check_activation=True, ) tx_hash = fn.transact({"from": asset_manager})- Parameters
lagoon_vault (LagoonVault) – Lagoon vault instance with
trading_strategy_module_addressconfigured.evm_usdc_amount (int) – USDC amount in EVM wei (uint256) for approve and CDW deposit.
hypercore_usdc_amount (int) – USDC amount in HyperCore wei (uint64) for CoreWriter actions.
vault_address (HexAddress | str) – Hypercore native vault address (not the Lagoon vault address).
check_activation (bool) – If
True, verifies the Safe is activated on HyperCore using thecoreUserExistsprecompile before building the multicall. Set toFalse(default) in simulate/Anvil mode where the precompile is not available.chain_id (int | None) – Override the chain ID used to look up the
CoreDepositWalletaddress. WhenNone(default), derived fromlagoon_vault.spec.chain_id. Pass explicitly when using aLagoonSatelliteVaultwhich has no.specattribute.asset_address (HexAddress | str | None) – Override the USDC token address used for the
approvecall. WhenNone(default), derived from the vault’s underlying asset (lagoon_vault.vault_contract.functions.asset()). Pass explicitly when using a satellite vault which has no.vault_contractattribute.
- Returns
Bound
module.functions.multicall(data)ready to.transact().- Raises
RuntimeError – If
check_activationis True and the Safe is not activated on HyperCore.- Return type
ContractFunction
- build_activate_account_multicall(lagoon_vault, activation_amount=None)
Build a multicall to activate a Safe’s HyperCore account.
Smart contracts (like Safe multisigs) must be activated on HyperCore before
CoreDepositWallet.deposit()bridge actions will clear the EVM escrow. This multicall performs the activation in a single transaction via the Safe’s trading strategy module:approve(CoreDepositWallet, activation_amount)CoreDepositWallet.depositFor(safe, activation_amount, SPOT_DEX)
Note
New HyperCore accounts incur a 1 USDC account creation fee. The default
activation_amountof 2 USDC exceeds the fee.- Parameters
lagoon_vault (LagoonVault) – Lagoon vault instance with
trading_strategy_module_addressconfigured.activation_amount (int | None) – USDC amount in raw units (6 decimals) for activation. Defaults to
DEFAULT_ACTIVATION_AMOUNT(2 USDC).
- Returns
Bound
module.functions.multicall(data)ready to.transact().- Return type
ContractFunction
- build_hypercore_deposit_phase1(lagoon_vault, evm_usdc_amount)
Build phase 1 of a two-phase Hypercore deposit: bridge USDC to HyperCore spot.
This multicall performs:
approve(CoreDepositWallet, amount)– approve USDC transferCoreDepositWallet.deposit(amount, SPOT_DEX)– bridge USDC to HyperCore spot
After this transaction lands, the USDC enters EVM escrow. Use
wait_for_evm_escrow_clear()to wait for the funds to arrive in the spot account, then callbuild_hypercore_deposit_phase2()for the remaining steps.Example:
from eth_defi.hyperliquid.core_writer import ( build_hypercore_deposit_phase1, build_hypercore_deposit_phase2, ) from eth_defi.hyperliquid.evm_escrow import wait_for_evm_escrow_clear # Phase 1: bridge USDC to HyperCore fn1 = build_hypercore_deposit_phase1(lagoon_vault, evm_usdc_amount=1_000_000) tx_hash = fn1.transact({"from": asset_manager}) # Wait for escrow to clear wait_for_evm_escrow_clear(session, user=safe_address) # Phase 2: move to perp and deposit into vault fn2 = build_hypercore_deposit_phase2( lagoon_vault, hypercore_usdc_amount=1_000_000, vault_address="0x...", ) tx_hash = fn2.transact({"from": asset_manager})- Parameters
lagoon_vault (LagoonVault) – Lagoon vault instance with
trading_strategy_module_addressconfigured.evm_usdc_amount (int) – USDC amount in EVM wei (uint256) for approve and CDW deposit.
- Returns
Bound
module.functions.multicall(data)ready to.transact().- Return type
ContractFunction
- build_hypercore_deposit_phase2(lagoon_vault, hypercore_usdc_amount, vault_address)
Build phase 2 of a two-phase Hypercore deposit: spot to perp to vault.
Batches two CoreWriter actions into a single multicall:
transferUsdClass— move USDC from spot to perpvaultTransfer— deposit USDC from perp into vault
When the EVM block finishes execution, HyperCore processes all queued CoreWriter actions from that block sequentially, so the
transferUsdClasscompletes before thevaultTransferruns.Must only be called after phase 1 USDC has cleared the EVM escrow and is available in the user’s HyperCore spot account. Use
wait_for_evm_escrow_clear()between phase 1 and phase 2.- Parameters
lagoon_vault (LagoonVault) – Lagoon vault instance with
trading_strategy_module_addressconfigured.hypercore_usdc_amount (int) – USDC amount in HyperCore wei (uint64) for both CoreWriter actions.
vault_address (HexAddress | str) – Hypercore native vault address (not the Lagoon vault address).
- Returns
Bound
module.functions.multicall(data)ready to.transact().- Return type
ContractFunction
- build_hypercore_withdraw_multicall(lagoon_vault, hypercore_usdc_amount, vault_address)
Build a single multicall transaction for the full Hypercore withdrawal flow.
Batches the 3-step withdrawal into one EVM transaction:
CoreWriter.sendRawAction(vaultTransfer)— withdraw from vaultCoreWriter.sendRawAction(transferUsdClass)— move USDC from perp to spotCoreWriter.sendRawAction(spotSend)— bridge USDC back to EVM Safe
When the EVM block finishes execution, all queued CoreWriter actions are processed sequentially on HyperCore (~47k gas per action).
Derives all contract instances internally from the
LagoonVault:modulefromLagoonVault.trading_strategy_modulecore_writerat the system addressCORE_WRITER_ADDRESSsafe_addressfromLagoonVault.safe_address
- Parameters
lagoon_vault (LagoonVault) – Lagoon vault instance with
trading_strategy_module_addressconfigured.hypercore_usdc_amount (int) – USDC amount in HyperCore wei (uint64) for all CoreWriter actions.
vault_address (HexAddress | str) – Hypercore native vault address (not the Lagoon vault address).
- Returns
Bound
module.functions.multicall(data)ready to.transact().- Return type
ContractFunction