Margin kept by the network.

Open a leveraged HBAR position. If it falls under its margin, Hedera liquidates it on schedule. No keeper bots, no servers.

A Scaffold-HBAR template: contracts, app and docs for margin trading, in one command.

Verified on Hedera testnet

Read the testnet notes
  • PerpMarket: the HBAR-USD market and its keeper0.0.10833087
  • LiquidityPool: takes USDC, mints and burns the LP share token0.0.10833082
  • cUSDC-LP: the LP share, a native HTS token the pool created0.0.10833090
  • PriceGuard: Chainlink checked against Supra0.0.10833074
  • The first check, booked on the Schedule Service by the contract itself0.0.10833097
  • Keeper started: revive() books the first check0xa1b7d2…fa1b71
  • The pool creates its HTS share token0xf405ea…340a5a
  • Deposit 361.79 USDC: the pool mints cUSDC-LP0x55d081…54d722
  • Open a 50x long with HBAR only: SaucerSwap swaps it to USDC collateral0x62868b…3986e7
  • Deposit with HBAR: HbarDepositZap swaps 0.5 HBAR to 1.12 USDC on SaucerSwap, the pool mints the caller 1.11 cUSDC-LP0x3c23d3…6ca098
  • Open a 50x short with 1.2 USDC0xfac6b7…b7bdba
  • Open a 2x long with 1 USDC0xd3f325…443de8
  • Redeem 10 cUSDC-LP: the pool burns the shares and pays USDC0x904872…2d7422
  • First check, run by the network at the second it was booked: no one signed it0x5bebf1…0038f6
  • Tick 4, run by the network: liquidates the 50x short, Liquidated(byNetwork = true, reward = 0), nobody signed it0x7c37d2…de59a9
  • Tick 18, run by the network: liquidates the 50x long, Liquidated(byNetwork = true, reward = 0), nobody signed it0x963bf6…ec8303
  • Checks the network has run so farReading the keeper…

Two prices, checked against each other

These are the live feeds on Hedera testnet, read straight from their contracts. Custos never liquidates on a price it can't confirm.

ChainlinkHBAR / USD
—
Waiting for the feed. The price every trade and liquidation uses.
SupraHBAR / USDT
—
Waiting for the feed. The independent check.

Reading both feeds on Hedera testnet…

What the network does every thirty minutes

Other chains rent this job to Gelato, Chainlink Automation or a team's own bot. Hedera can book a contract call for later, so the market keeps itself.

  1. 1

    Hedera calls tick()

    The call was booked on the previous run through the Schedule Service (HIP-1215). No person or server sends it.

  2. 2

    Prices are checked

    PriceGuard reads Chainlink and Supra. If either is stale or they disagree, the run stops here and liquidates no one.

  3. 3

    Funding is charged

    The side with more open interest pays the other, so the market leans back toward balance.

  4. 4

    Accounts are swept

    A page of traders is checked. Anyone under the maintenance margin is closed and their collateral goes to the pool.

  5. 5

    The next run is booked

    Before it ends, the contract books its own next call, trying the following seconds if one is full.

Under the hood

What Custos uses on Hedera, and what we measured running it on testnet. Read the testnet notes.

Schedule Service (HIP-1215)
Each run books the next with scheduleCall, so the market never waits on a bot. Booking costs about 1.41M gas. On testnet a whole run (funding, sweep and booking) used 1.62M gas and cost 1.36 HBAR, paid from the contract's own reserve.
Token Service
Collateral is SaucerSwap's testnet USDC (0.0.5449), an HTS token. The market, the pool and the deposit zap associate with it when they start, so traders and LPs can use it straight away.
Chainlink
The HBAR/USD push feed prices every trade, every liquidation and every funding charge. It needs no API key and no update fee.
Supra
The HBAR/USDT push feed is the independent check. It quotes USDT and updates about hourly on testnet, so the two feeds may sit up to 5% apart. The live readout shows both limits as PriceGuard holds them.
SaucerSwap
HBAR goes in and out of both sides through its V1 router: open with HBAR, close to HBAR, and deposit into the pool with HBAR. Every swap the contracts make for someone else must fill within 3% of Chainlink.
Smart contracts
Solidity contracts on Hedera's EVM, with unit tests and fuzzed solvency invariants: the pool always covers what it owes.
Hedera timing
block.timestamp is the start of a ~2 second block, so a check that fires on time can look a second early. The keeper allows five seconds.

Parts you can take for your own project

The perpetual market is the demo. These three contracts are the template.

Runs any job on a timer with no bot. Books itself, pays from its own HBAR reserve, and anyone can restart it if the chain of calls breaks.

Collateral, profit and loss, health and liquidation rules. The same book works for lending or repo.

One place prices come from: Chainlink first, Supra as the check, with age and disagreement limits.

Start from one command

npm create scaffold-hbar@latest -- --template victorjayeoba/scaffold-hbar-custos
  1. Create the project with the command above and pick a deployer account.
  2. Get testnet HBAR from the Hedera Portal faucet and testnet USDC from Circle's faucet.
  3. Deploy to Hedera testnet. The deploy script books the first network check.
  4. Start the app and open the Keeper page to watch the schedule run.