Skip to content

Examples

Below are some example snippets to help you get started.

Bridging tokens

uint256 coreAmount = PrecompileLib.convertEvmToCoreAmount(uBTC, evmAmount);
CoreWriterLib.bridgeToCore(uBTC, coreAmount, coreRecipient);

Reading precompiles by EVM address

uint256 spotIdx = TokenRegistry.getSpotIndex(uBTC);
uint256 px = PrecompileLib.spotPxByAddress(uBTC);

See the repository examples/ directory for full working examples.

For Testing Contracts

Bridging

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import {Test} from "@hyper-evm-lib/test/BaseSimulatorTest.sol";
import {CoreWriterLib} from "@hyper-evm-lib/src/CoreWriterLib.sol";
 
import {CoreSimulatorLib} from "@hyper-evm-lib/test/simulation/CoreSimulatorLib.sol";
 
contract ExampleTest is Test {
 
    uint64 constant HYPE = 150;
    ExampleContract example;
 
    function setUp() public {
        vm.createSelectFork("https://rpc.hyperliquid.xyz/evm");
 
        // initialize the HyperCore simulator
        CoreSimulatorLib.init();
 
        // deploy your contract in the test environment
        example = new ExampleContract();
        CoreSimulatorLib.forceAccountActivation(address(example));
    }
 
    function test_bridge() public { 
        // this example contract bridges HYPE to HyperCore
        example.bridgeToCore{value: 1e18}(HYPE, 1e18);
 
        // move to the next block, performing all queued HyperCore interaction(s)
        CoreSimulatorLib.nextBlock();
 
    }
}
 
contract ExampleContract {
    function bridgeToCore(uint64 tokenId, uint256 evmAmount) external payable {
        CoreWriterLib.bridgeToCore(tokenId, evmAmount);
    }
}

Vault deposit

This example inherits from BaseSimulatorTest which performs the basic setup.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import {console} from "forge-std/Test.sol";
 
import {BaseSimulatorTest} from "@hyper-evm-lib/test/BaseSimulatorTest.sol";
import {VaultExample} from "@hyper-evm-lib/src/examples/VaultExample.sol";
import {PrecompileLib} from "@hyper-evm-lib/src/PrecompileLib.sol";
import {CoreWriterLib} from "@hyper-evm-lib/src/CoreWriterLib.sol";
import {CoreSimulatorLib} from "@hyper-evm-lib/test/simulation/CoreSimulatorLib.sol";
 
contract ExampleVaultTest is BaseSimulatorTest {
    function test_exampleVaultDepositAndWithdraw() public {
        VaultExample vaultExampleContract = new VaultExample();
        hyperCore.forceAccountActivation(address(vaultExampleContract));
        hyperCore.forcePerpBalance(address(vaultExampleContract), 1000e6);
 
        address testVault = 0x07Fd993f0fA3A185F7207ADcCD29f7A87404689D;
        uint64 depositAmount = 100e6;
 
        uint64 balanceBefore = PrecompileLib.withdrawable(address(vaultExampleContract));
 
        vaultExampleContract.depositToVault(testVault, depositAmount);
        CoreSimulatorLib.nextBlock();
 
        // Try to withdraw before the lock period expires - should revert
        PrecompileLib.UserVaultEquity memory vaultEquity =
            PrecompileLib.userVaultEquity(address(vaultExampleContract), testVault);
        vm.expectRevert(
            abi.encodeWithSelector(
                CoreWriterLib.CoreWriterLib__StillLockedUntilTimestamp.selector,
                vaultEquity.lockedUntilTimestamp
            )
        );
        vaultExampleContract.withdrawFromVault(testVault, depositAmount);
 
        vm.warp(block.timestamp + 1 days + 1);
 
        vaultExampleContract.withdrawFromVault(testVault, depositAmount);
        CoreSimulatorLib.nextBlock();
 
        uint64 balanceAfter = PrecompileLib.withdrawable(address(vaultExampleContract));
 
        assertEq(balanceBefore, balanceAfter);
    }
}