// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; import {AsyncClient} from "./AsyncClient.sol"; import {AsyncTypes, IAsyncRouter} from "./IAsync.sol"; /// @title QuickVault /// @notice The sample consumer from the quickstart: it buys or sells on HyperCore through an intent /// and only updates its books when the receipt arrives. Its state is what the contract believes. contract QuickVault is AsyncClient { uint32 public immutable asset; int256 public position; // E8, from receipts only uint256 public lastIntent; AsyncTypes.Status public lastStatus; uint64 public lastFilled; uint64 public lastPx; uint256 public receiptsSeen; event Sent(uint256 indexed intentId, bool isBuy, uint64 sz, uint64 limitPx); event Heard(uint256 indexed intentId, AsyncTypes.Status status, uint64 filledSz, uint64 avgPx, int256 position); constructor(IAsyncRouter router_, address owner_, uint32 asset_) AsyncClient(router_, owner_) { asset = asset_; } function trade(bool isBuy, uint64 sz, uint64 limitPx, AsyncTypes.Tif tif, uint64 ttl) external onlyAsyncOwner returns (uint256 id) { id = _request( AsyncTypes.Intent({ asset: asset, isBuy: isBuy, reduceOnly: false, tif: tif, sz: sz, limitPx: limitPx, deadline: uint64(block.timestamp) + ttl, callbackGas: 150_000 }) ); lastIntent = id; lastStatus = AsyncTypes.Status.Open; emit Sent(id, isBuy, sz, limitPx); } function _onReceipt(uint256 id, AsyncTypes.Receipt calldata r) internal override { AsyncTypes.Intent memory it = _intentOf(id); int256 f = int256(uint256(r.filledSz)); position += it.isBuy ? f : -f; receiptsSeen++; if (id == lastIntent) { lastStatus = r.status; lastFilled = r.filledSz; lastPx = r.avgPx; } emit Heard(id, r.status, r.filledSz, r.avgPx, position); } function _intentOf(uint256 id) internal view returns (AsyncTypes.Intent memory it) { (bool ok, bytes memory data) = address(asyncRouter).staticcall(abi.encodeWithSignature("intentOf(uint256)", id)); if (ok) { (,,,,,,,, it) = abi.decode(data, (address, address, uint64, uint8, bool, bool, uint128, uint128, AsyncTypes.Intent)); } else { it.isBuy = true; } } }