Hyperliquid does not currently have perps for spot assets. With the introduction of the testnet evm that has access to spot prices, we can create GMX style perps. This implementation was done in 2 hours from start to finish and is very primitive.
First claim some gas token from the faucet:
curl -X POST --header "Content-Type: application/json" --data '{"type":"ethFaucet", "user": "0x7735cE49c065d175D5fC39CF030586575b5194c5"}' https://api.hyperliquid-testnet.xyz/info
To deploy:
forge script script/Main.s.sol --rpc-url $rpc --broadcast
This will deploy the Perpetual
contract which will deploy a USDC token within its constructor.
- Ensure you've claimed gas from the faucet as mentioned above
Perpetual.faucet()
can be queried once per address and will cause the user to receive 10k mock USDC.- Use
Perpetual.openPosition(uint256 margin, uint256 usdNotional, bool isLong, uint256 assetIndex)
to ape into something.margin
is the amount of USDC that will be transferred out of your account (no approval needed,Perpetual
has god approval).usdNotional
is the amount of the token perp that will be created (i.eusdNotional / margin
is your leverage, which cannot exceed 1000).isLong = false
for shortisLong = true
for long.assetIndex
is the index of the asset that you would like to purchase. It is unclear how that aligns with the asset name on testnet since there are much more assets on testnet then prices returned by the oracle. UsePerpetual.getMarkOraclePxs()
which will return the prices (just a pass through to the oracle for simplicity, appears to be 10**3 precision). - Use
Perpetual.closePosition(uint256 tokenNotional, uint256 assetIndex)
to close a position. This will transfer the user back their remaining margin + profit if any (position is checked for liquidation prior to closing).tokenNotional
is demoninated in the token count - you can usePerpetual.balanceOfNotional(address user, uint256 assetIndex)
to find out how much notional someone has in their position. Perpetual.liquidateAllUsers()
should be called periodically to ensure there isn't any bad-debt (there probably will be since leverage is super high and LTV is 1 but we don't really care here).Perpetual.getUserPositions(address user)
to return information for a users given positions such as pnl, margin, initial debt, etc.Perpetual.getPositionValue(address user, uint256 assetIndex)
gives the USDC value of a given perp position.Perpetual.getPositionPnl(address user, uint256 assetIndex)
gives the USDC PnL of a given perp position (can be negative obviously).
Perpetual = 0x213f84F9eb781046108996D11B665B5dE9137CA2
USDC = 0x086C49A2b42bD3eF3F9857C64630f76395F88868
- Discord post #1
- Discord post #2
- Tweet
- JSON-RPC endpoint:
https://api.hyperliquid-testnet.xyz/evm
.chainId = 998
.
Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
Foundry consists of:
- Forge: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- Cast: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- Anvil: Local Ethereum node, akin to Ganache, Hardhat Network.
- Chisel: Fast, utilitarian, and verbose solidity REPL.
$ forge build
$ forge test
$ forge fmt
$ forge snapshot
$ anvil
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
$ cast <subcommand>
$ forge --help
$ anvil --help
$ cast --help