Documentation Contracts
Contracts
Two small contracts carry the gate. The proof registry keeps the latest record for each facility. The loan vault allows a draw only when that record is a fresh pass.
Both are prototypes for the Coston2 testnet. The vault is a mock: its credit limit and its draws are plain numbers, and it holds no token.
The proof registry#
ProofRegistry stores one record per facility: the latest one. One address, the submitter, may post records. In the demo the submitter is the extension's contract, so a record reaches the registry only with a machine's signature behind it.
| Function | Who | What |
|---|---|---|
submit(facilityKey, record) | The submitter | Stores the record as the facility's latest, if every rule below holds |
latest(facilityKey) | Anyone | The stored record, and whether one exists |
status(facilityKey) | Anyone | The effective result: a record past its valid-until date reads as FAIL |
isFresh(facilityKey) | Anyone | True while a record exists and is inside its valid-until date |
setSubmitter(address) | The owner | Changes who may post |
What the registry refuses#
| Rule | Error |
|---|---|
| Only the submitter posts | NotSubmitter |
| A record needs a facility key and a digest | ZeroFacilityKey, ZeroRecordDigest |
| A record needs a result | ResultNone |
| The valid-until date is not before the inspection date | ValidUntilBeforeInspection |
| The inspection date is not more than a day in the future | InspectionDateInFuture |
| No rollback: a record dated before the stored one is refused | OlderThanStoredRecord |
| No replay: each record digest is accepted once | RecordAlreadyPosted |
| Matched units cannot exceed scheduled units | MatchedExceedsScheduled |
Two details matter. A record with the same date as the stored one is accepted, so a correction can be posted the same day as a new record with a new digest. And a FAIL for the record that is stored now may always land, so a later failed check of the same record closes the gate, while a PASS for a used digest stays refused, so nothing old can come back.
The loan vault#
MockLoanVault is bound to one registry, one facility and one borrower when it is deployed.
| Function | Who | What |
|---|---|---|
draw(amount) | The borrower | Draws, or reverts with the reason |
repay(amount) | The borrower | Lowers the drawn amount |
canDraw(amount) | Anyone | Whether a draw would go through, and the reason if not |
setAllowDrawWithExceptions(bool) | The owner | Whether a PASS_WITH_EXCEPTIONS record lets a draw through |
The same flow in words
- The amount must be above zero, or the draw reverts with
ZeroAmount. - The registry must hold a record for the facility, or
ProofMissing. - The record must be inside its valid-until date, or
ProofStale. No transaction is needed for a record to go stale: the clock does it. - The result must not be FAIL, or
ProofFailed. - A PASS_WITH_EXCEPTIONS record lets a draw through only if this vault was set to allow it, or
ProofHasExceptions. - The amount must fit under the credit limit, or
CreditLimitExceeded. - Otherwise the draw goes through and the vault emits
DrawAllowedwith the record’s digest.
Reason from canDraw | Error from draw | Meaning |
|---|---|---|
NO_RECORD | ProofMissing | The registry holds no record for the facility |
STALE | ProofStale | The record is past its valid-until date |
FAILED | ProofFailed | The latest record is a FAIL |
EXCEPTIONS_NOT_ALLOWED | ProofHasExceptions | The record has exceptions and this vault does not lend on them |
OVER_CREDIT_LIMIT | CreditLimitExceeded | The amount does not fit under the credit limit |
Design points#
- No machine address in the vault. A machine's address changes at every restart. The signer is checked through FlareTeeManager each time, by the extension's contract.
- The clock closes the gate. A record goes stale with no transaction. A lender does not depend on anyone to act.
- The vault reads, the registry decides. The vault holds no copy of a record. It asks the registry at every draw.
Tests#
The contracts have 47 unit tests (Hardhat), and 22 end-to-end cases run the real registry and vault behind the extension's contract and a mock FlareTeeManager (Foundry). The runner runs both every cycle.