LDGR/Docs
DocsDevelopersMilestone escrow

Fund the whole job. Release one step at a time.

A milestone escrow holds one asset for a fixed payer and a different beneficiary. The payer approves each step in order, and each approval immediately pays its exact amount.

Fix the terms before funding

milestone_escrow_v1 locks the full opening amount for 2-16 ordered milestones. Every step has a positive exact amount and a nonempty, trimmed public label of at most 80 UTF-8 bytes. Step amounts must sum to the opening amount. Choose one asset, one beneficiary and one future expiry for the entire escrow.

The beneficiary, labels, amounts, order and expiry cannot change. Labels describe the intended work; the ledger does not verify delivery. There are no skipped steps, top-ups, arbitrary programs or external proof checks.

From funded to paid or refunded

  1. Open. The payer locks the full amount. Opening also burns the live native contract-opening fee, bounded by the reviewed maxFeeAtomic. That fee is additional to collateral.
  2. Release the next milestone. Strictly before expiry, only the payer can approve the next zero-based milestoneIndex. Approval and payout form one durable transaction: the beneficiary receives that amount immediately, and the next index advances. There is no separate claim or approved balance.
  3. Complete. The last release marks the escrow completed. All released payments remain final.
  4. Refund the residual. At or after expiry, only the payer can refund all remaining funds. Released payments stay with the beneficiary; a refund never reverses them. No autonomous alarm releases or refunds funds.

For example, fund 1,000 USDL as 300 + 400 + 300. After two approvals, 700 USDL has been paid and 300 USDL remains locked. Once expiry arrives, a refund returns only that 300 USDL. If the payer approves nothing, all 1,000 USDL stays locked until expiry permits a signed refund.

Canonical records expose the immutable milestone list, releasedMilestones, releasedAmount, remainingAmount and refundedAmount. Released plus remaining plus refunded always equals the opening amount. A refund sets remaining to zero and records the returned residual. Status is open, completed or refunded.

Approval belongs to the payer

The beneficiary cannot force approval. There is no arbiter, dispute resolution or proof-of-delivery authority. There is no early cancellation: if approval stops, the residual remains locked until expiry. Agree on these limits before funding.

Review and sign with a local wallet

Utilities provides the creation form. Explorer loads the live contract and offers eligible payer releases or refunds. The extension and Web Wallet review the account and network, beneficiary, all steps, selected step and amount, expiry, already-paid amount, residual and opening native fee cap. Settlement terms are read again immediately before local signing.

Browser integrations use typed openMilestoneEscrow, releaseMilestoneEscrow and refundMilestoneEscrow methods from @ldgr/wallet-sdk. User signing keys stay in the selected local wallet. Link the returned transaction hash as the receipt; the contract ID identifies the ongoing record.

Code example
import { createLdgrWallet } from "@ldgr/wallet-sdk";

const wallet = createLdgrWallet(window.ldgr);
// Connect on a separate user tap when using the popup wallet.
await wallet.connect({ network: "testnet" });
// Open the escrow on a later user tap.
const opened = await wallet.openMilestoneEscrow({
  asset: usdlAssetCode, // exact token code for this network
  to: beneficiaryAddress,
  amount: "1000.00",
  milestones: [
    { label: "Design", amount: "300.00" },
    { label: "Implementation", amount: "400.00" },
    { label: "Handover", amount: "300.00" },
  ],
  expiresAt: String(Date.now() + 30 * 24 * 60 * 60 * 1000),
  // Optional maxFeeAtomic is a native atomic integer string.
  // When omitted, the wallet reviews the live opening fee as the cap.
});
console.log(opened.contractId, opened.transactionHash);
// Each settlement opens a fresh review of the canonical next step or residual.
const released = await wallet.releaseMilestoneEscrow({ contractId: opened.contractId, milestoneIndex: 0 });
console.log(released.transactionHash);

Caller-signed HTTP integration

@ldgr/client supplies getMilestoneEscrow and the same three mutation methods for caller-signed submissions. They preserve the signed envelope and do not sign, rebuild or retry it automatically. After a timeout, reconcile canonical state and retain the exact signed envelope for an identical retry.

Code example
const escrow = await client.getMilestoneEscrow(contractId);
// Sign locally using the original canonical bigint payload, then encode HTTP fields.
const receipt = await client.releaseMilestoneEscrow(signedRelease);
console.log(receipt.transactionHash, escrow.remainingAmount);

All three signed actions use the actual escrow asset and the payer's escrow-asset nonce. Opening needs an active registered asset; release and refund remain available for a known asset after deactivation. Release and refund are fee-free. The opening native fee cap is an atomic integer string; it does not use the collateral asset's decimals.

The HTTP base is /api/v1/contracts/milestone-escrows: POST opens, GET /:contractId reads, and POST /:contractId/release or /:contractId/refund settles. Amounts, including nested milestone amounts, are exact decimal strings in the escrow asset. Nonce, timestamp and expiry are integer strings; milestoneIndex is an integer number.

Actions are milestone_escrow_open, milestone_escrow_release and milestone_escrow_refund. The contract ID is c plus 31 lowercase hex characters, derived from all opening terms except contractId and publicKey. Both settlement actions bind the original full openPayloadHash; release also binds its exact index. Unsupported fields are rejected.