- After the loan reaches
maturity. - Before maturity, if the USD value of the locked collateral drops below
marginCall.
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 packet34, whileDiaBtcUsdOracle.verifychecks the DIA signature andintentExpiry. - Resolution triggers: succeeds when
quote.time >= maturity, or whenquote.time >= startAtand collateral value falls belowmarginCall(preventing pre-setup prices from triggering a margin call). - Payout: pays
principal * 100000000 / quote.pricesatoshis (truncated) tolenderScripton output 1, and any remaining collateral above Bitcoin’s 330-sat dust limit toborrowerScripton output 2. - Edge cases: requires exactly two inputs, so two loans cannot share one lender output. It caps
owedatlockedand 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 whereoweditself could fall at or below 330 sats, fold that leg intoborrowerScriptas 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.valuetoborrowerScripton output 0, so the lender cannot redirect funds.
unilateral
Provides the onchain exit if the operator stops cosigning (Exit).
- Waits for
operatorExitDelayafter unrolling onchain. - Requires both
lenderPkandborrowerPkto sign, letting both parties choose the destination together.
Borrower Protection
While the loan is healthy beforematurity, 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.