> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lerian.studio/llms.txt
> Use this file to discover all available pages before exploring further.

# How Lerian Consignado — Dataprev works

> The gateway's flows end to end — credential setup, margin fetch, averbação, the 24-hour loan auction, and reconciliation of escrituração and CEF D+2 repasse records — each driven by a command from the credit engine or a scheduled poller.

Lerian Consignado — Dataprev drives its work through five flows. Two are **command-driven** — the credit engine asks for a margin or an averbação — and the rest are **poller-driven**, sweeping Dataprev on a cadence for auctions and settlement. Before any of them can run, a tenant registers its credentials.

## Credential setup

***

A tenant transacts only after registering two credentials through the gateway's control plane:

* an **ICP-Brasil A1 client certificate** (PEM certificate and key), validated before it is stored;
* an **OAuth2 client-credentials secret**.

Both are held in a managed secret store and are **never returned** by any read. A status read reports whether a certificate is present, its fingerprint and expiry, and whether the OAuth secret is set — returning those flags (all false when nothing is stored) rather than an error.

## Margin fetch

***

When the credit engine needs a worker's payroll margin, it issues a **margin-request** command. The gateway queries Dataprev for the worker's available margin and disposable remuneration, and emits a fact carrying that margin, keyed to the originating request.

## Averbação

***

To register a signed contract, the credit engine issues an **averbação-request** command. The gateway registers the contract with Dataprev and emits one of two facts:

* an **averbação-confirmed** fact carrying the rail's own success code — relayed, never hardcoded; or
* an **averbação-rejected** fact carrying the rail's error code and message.

## Loan auction (leilão)

***

Worker loan requests (*solicitações*) each open a **24-hour auction**. A discovery poller lists these requests page by page. For every request it has not yet seen, the gateway scatters the auction concurrently to a configured set of external bid managers, collects their bids, and selects the **lowest annual-CET** bid — ties broken by the lowest installment, and a bid whose CET cannot be parsed can never win. It then submits the winning bid.

A second, win-detection poller watches the submitted proposals. When a proposal reaches an accepted state, the gateway emits a **proposal-accepted** fact so the downstream lending system can originate the loan.

## Reconciliation

***

On a configurable cadence, a reconciliation poller sweeps a sliding window of payroll *competências*. For each competência it reads two independent sources — the per-contract **escrituração** records and the **CEF D+2 repasse** records that settle paid installments — normalizes them, and emits one reconciliation fact per record. The two legs are independent: a failure reading one never blocks the other.
