Skip to main content
Pledge a tokenised Treasury bill, borrow a stablecoin against it for a fixed term, then repay and get the pledge released — or miss the term and watch the lender take the collateral. The pledged bills never move: an Asset Tokenization Studio hold encumbers them in the borrower’s own account, and the loan contract is only the escrow that can release or execute it.

Get the code

npx scaffold-hbar@latest --template hedera-dev/hedera-finance-templates#templates/collateral-mobility

Overview

A repo (sale and repurchase agreement) is how institutions borrow cash short-term against securities. The borrower pledges a bond, receives cash, and pays it back with interest at the end of the term. If they do not, the lender keeps the bond. Collateral mobility is the problem of moving that pledge around without moving the asset. The usual answer is a custodian: the security is transferred to them and they hold it for the lender. This template uses an ATS hold instead:
  • The bills stay in the borrower’s account the whole time. balanceOf excludes them, so they are visibly encumbered but not gone.
  • The hold names the loan contract as escrow and the lender as destination, both fixed when the pledge is created. The escrow cannot redirect them.
  • The loan contract holds no role on the bill and is not on its control list. It can only release the hold back to the borrower, or execute it to the one destination the borrower already chose.
The loan contract reads the hold back from the bill before it lends anything, so the pledge is verified on-chain rather than taken on trust from the page. The desk has two actors:
  • Lender — the operator account the app runs with. Funds the loan and is the hold’s destination
  • Borrower — a demo account the app creates, holding the bills and the loan’s cash. Your own wallet can be added as a second borrower

The bill and the loan

The worked example is one bill and one loan, and every number on the page comes from it: The price is not typed in. The feed computes it from the published discount rate and the days remaining, so the bill accretes toward par on its own.

The demo clock

One compression, applied in one place: ten seconds of real time is one day of the bill’s life. The 30-day term runs in five minutes and the 91-day bill matures in about fifteen. Both contracts take the same secondsPerDay, which is 10 here and 86400 in production. Nothing else in the app has a clock of its own. The bill’s own getMaturityDate() is still set 91 real days out, because that is what the instrument is, and the page shows both dates side by side. Without the compression the interest on a five-minute loan at 4.8% is less than half a cent and every figure on the page would read $0.00.
The staleness window, the grace period and the hold’s expiry margin are in real seconds and do not scale with secondsPerDay. If you change the clock, change them too, or they will dwarf the loan they govern.

Prerequisites

  • Node.js 22.23.4 or later on the 22.x line, and Yarn
  • A Hedera testnet account from the Hedera Portal with an ECDSA key, which becomes the lender. JSON-RPC transactions are ECDSA-signed, so an ED25519 key cannot be used; the app says so at start
  • Testnet HBAR on that account. A desk costs about 50 HBAR to set up, and setup refuses to start below 100. The stablecoin’s HTS creation fee is priced in dollars, so the HBAR cost rises as HBAR falls
  • Optional: MetaMask on Hedera testnet (chain id 296) to borrow from your own account

Project structure

Configuration

Create packages/nextjs/.env.local:
The page opens on a guided tour: Set up, Pledge, Borrow, Break it, Stress it, Resolve. The Sandbox tab has every control on one screen.

Set up the desk

Press Set up desk. Seven steps run, each reporting as it goes:
  1. Resolve the latest version of the ATS bond configuration and the Stablecoin Studio stablecoin configuration
  2. Deploy the bill through the ATS factory, with an allowlist as its control list
  3. Deploy the stablecoin, its HTS token and its reserve through the Stablecoin Studio factory
  4. Deploy the price feed, anchored to a maturity 91 demo-days out
  5. Deploy the loan contract, which reads the cash and feed decimals from the chain rather than assuming them
  6. Allowlist the borrower and the lender, grant the cash-in role, associate the stablecoin
  7. Issue 10 bills to the borrower and mint the lender’s stablecoin
Both studios are already on testnet, so steps 2 and 3 deploy nothing of your own: Running setup again starts a fresh desk and leaves the old one on the network.

Pledge and borrow

Pledge creates the hold on the bill. The amount is in whole bills, and the hold’s destination is fixed to the lender at this moment — not at liquidation.
Borrow calls openLoan, which reads the hold back before it moves any cash:
Only then does it pull the lender’s stablecoin and push it to the borrower. The borrowing limit is the pledge’s value less the haircut, and the page previews the LTV before you commit.
getHoldForByPartition returns seven values, not the five the struct suggests — the last is a ThirdPartyType. Decoding it wrongly is the easiest way to get this integration subtly wrong.

Repay, or default

Repay pulls principal plus accrued interest from the borrower, sends it to the lender, and calls releaseHoldByPartition. The bills were never anywhere else, so releasing the hold is the whole of giving them back. Pay back part of the loan (topUp) repays part of the principal, crystallising the interest earned so far, and lowers the LTV without closing the loan. Interest accrues over whole demo days and stops at the end of the term, so a defaulted loan does not keep growing.

Two ways a loan is liquidated

By term. Once the term has elapsed, anyone may call liquidate. This path reads no price at all, which is the point: it still works when the feed is stale, broken or gone. It is the primary path. By price. While the loan is live, a bill price low enough to push the LTV past the 99% threshold also permits liquidation — but only on a positive, non-stale price. Either way the contract calls executeHoldByPartition, which delivers the bills to the destination fixed at pledge time. The lender cannot be changed at the moment of seizure, because there is nothing to change. The asymmetry is deliberate: a stale feed blocks borrowing, partial repayment and price liquidation, but never term liquidation. Freeze the feed on the page and try both to see it.
Liquidating on price alone takes a rise of about 700 basis points in the discount rate when the loan opens, and more as the bill accretes. No three-month bill has moved like that. The shock is there to show the mechanism, not as a realistic scenario.

What the contract refuses

Break it runs four attacks against the live contracts. Three are sent as real transactions, so each refusal is a reverted transaction on HashScan. Only the escrow could sign the fourth, and the loan contract has no function for it, so it is asked with an eth_call: The fake pledge is the only evidence from outside the code that the contract reads the hold. The reclaim attack is why openLoan refuses a hold that does not outlast the term plus a grace period: otherwise a borrower could wait for expiry, take the bills back and keep the cash. Borrowing past capacity, repaying more than the principal and liquidating a healthy loan are also refused, with ExceedsCapacity, AmountTooLarge and NotLiquidatable.

Going to production

  • The clock. secondsPerDay is 10 here. In production it is 86400, and the real-second constants beside it have to be re-scaled to match.
  • A real price. The feed computes a bank-discount price from one published rate. A real desk marks against observed quotes, and AggregatorV3Interface means a Chainlink feed drops straight in.
  • Who may liquidate. liquidate is permissionless on purpose, so a default is not at the lender’s discretion. That is a design choice, not the only one.
  • The haircut is static. 2% here, fixed. Real haircuts move with volatility, and moving them is procyclical: they rise exactly when borrowers can least afford them.
  • One lender, one bill. The contract is scoped to a single lender and a single collateral asset, set at deployment. A real tri-party arrangement is neither.
  • Custody. The borrower’s key lives on the server so the demo can drive both sides. Nothing about the hold mechanism requires that, and in production it must not be true.

Next steps

ATS Bond Issuance

Issuing the security this template pledges, with its allowlist and roles.

DvP Settlement

Atomic delivery versus payment, where both legs actually move.

Asset Tokenization Studio

The factory, the resolver and the ERC-1400 securities they deploy.

Stablecoin Studio

The cash leg: proxies, reserves and the roles that govern them.