Skip to main content
A simple bilateral loan is a contract between a borrower who locks bitcoin collateral and a lender who pays the principal out of band in stablecoins or fiat. When the loan resolves, the contract converts the principal to satoshis at the resolution price and pays the lender. Whatever collateral remains returns to the borrower. Resolution can happen in two cases:
  • After the loan reaches maturity.
  • Before maturity, if the USD value of the locked collateral drops below marginCall.
Before sending the principal out of band, the lender checks that the borrower funded enough collateral. When the borrower repays out of band, the lender signs close to return the collateral to borrowerScript without requiring the borrower online. Instead of coupling to an oracle key, Loan stores the oracleId of a DIA BTC/USD Live Oracle output (Asset as Contract ID). Save loan.ark next to dia_btc_usd_cents.ark from DIA BTC/USD Live Oracle and compile loan.ark. Both parties must compile the exact same source, because any edit changes the contract address.
loan.ark

Constructor

operatorExitDelay is not a constructor parameter, because the SDK binds it to the operator’s exit delay (CSV delays). Both parties check these values before the borrower funds the loan, because the contract does not re-check them at resolve.

Functions

resolve

Anyone can submit resolve once maturity is reached or collateral falls below marginCall.
  • Transaction layout: spends the loan as input 0 and the DIA BTC/USD Live Oracle output as input 1. Output 0 continues the oracle unchanged, and payouts start at output 1.
  • Oracle price: DiaBtcUsdCents.readPrice(oracleId) verifies input 1 holds the oracle’s identity asset and reads (price, time) from packet 34, while DiaBtcUsdOracle.verify checks the DIA signature and intentExpiry.
  • Resolution triggers: succeeds when quote.time >= maturity, or when quote.time >= startAt and collateral value falls below marginCall (preventing pre-setup prices from triggering a margin call).
  • Payout: pays principal * 100000000 / quote.price satoshis (truncated) to lenderScript on output 1, and any remaining collateral above Bitcoin’s 330-sat dust limit to borrowerScript on output 2.
  • Edge cases: requires exactly two inputs, so two loans cannot share one lender output. It caps owed at locked and omits a sub-dust borrower output (remainder <= 330), so those leftover sats fold into the lender output instead of blocking relay. If your product allows tiny loans where owed itself could fall at or below 330 sats, fold that leg into borrowerScript as well.

close

Returns the full collateral early when the borrower repays out of band, without requiring the borrower online.
  • Takes a single input signed only by lenderPk.
  • Enforces paying tx.input.current.value to borrowerScript on output 0, so the lender cannot redirect funds.

unilateral

Provides the onchain exit if the operator stops cosigning (Exit).
  • Waits for operatorExitDelay after unrolling onchain.
  • Requires both lenderPk and borrowerPk to sign, letting both parties choose the destination together.

Borrower Protection

While the loan is healthy before maturity, the lender cannot spend the collateral except through close, which pays borrowerScript. Once maturity arrives, resolve requires no lender signature, so the borrower can submit it alone to recover the excess collateral (locked - owed). Out-of-band repayment still carries counterparty risk, because the contract cannot verify a bank transfer or an external stablecoin payment. If the borrower repays out of band and a dishonest lender refuses to sign close, that lender can still call resolve at maturity or on a margin call. Adding a borrower-only timeout after maturity does not prevent that attack, because an online lender can call resolve first. Such a timeout would also let a borrower take the collateral if the oracle went offline during the claim window. To let the borrower repay early without relying on the lender, denominate the principal in an asset issued within Arkade (Arkade Assets). close can then verify the asset repayment to lenderScript atomically in the same transaction. For off-network fiat loans, pair the contract with a legal agreement or an arbiter key on close.