> For the complete documentation index, see [llms.txt](https://lendxyz.gitbook.io/lend/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lendxyz.gitbook.io/lend/reference/smart-contracts.md). Agents can also search and read this documentation through its [MCP server](https://lendxyz.gitbook.io/lend/~gitbook/mcp).

# Smart contracts

Lend runs the same protocol on two very different chains: **EVM** (Solidity) and **Stellar Soroban** (Rust). The contracts are separate codebases, but they implement the same model, use the same method names in the local naming convention of each chain, and emit events that carry the same fields.

{% content-ref url="/pages/j8mpRBjaljPmp5cjSDbd" %}
[EVM contracts](https://lendxyz.gitbook.io/lend/reference/smart-contracts/evm.md)
{% endcontent-ref %}

{% content-ref url="/pages/RwvTMcazpJCejXAzPaVx" %}
[Soroban contracts](https://lendxyz.gitbook.io/lend/reference/smart-contracts/soroban.md)
{% endcontent-ref %}

***

## The four contracts

| Contract        | Role                                                                                                            |
| --------------- | --------------------------------------------------------------------------------------------------------------- |
| **Factory**     | Creates operations, deploys their opLend token, and owns every funding path (invest, predeposit, gift, refund). |
| **opLend**      | One token per operation. Represents a share of that operation. Transfer-restricted to whitelisted addresses.    |
| **Rewards**     | Distributes revenue per epoch through merkle roots. Users claim their own leaf.                                 |
| **Restitution** | Redeems opLend against USDC at the end of an operation. EVM only.                                               |
| **PriceOracle** | Publishes the EUR price of an operation token, used for secondary-market and portfolio valuation. EVM only.     |

***

## Concepts shared by both chains

### Operations and shares

An operation is created with a fixed `totalShares` and a price `eurPerShares`. Shares are the unit users buy; one share equals one opLend token. Funding is complete when `fundingProgress` reaches `totalShares`, at which point the operation emits a *finished* event and the raised USDC becomes withdrawable by the treasury.

The USDC cost of a purchase is derived at call time, not stored:

```
sharesPriceEur = eurPerShares × sharesAmount / 1e6
usdcCost       = sharesPriceEur × eurUsdPrice / 1e6
```

`eurUsdPrice` comes from a Chainlink EUR/USD feed on EVM, and from a Reflector feed on Soroban. Both reject stale rounds.

### Decimals

| Value           | Decimals | Note                                                                                                                                                                 |
| --------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| opLend / shares | 6        | Fixed, on both chains                                                                                                                                                |
| `eurPerShares`  | 6        | e.g. `100_000000` = 100 € per share                                                                                                                                  |
| USDC            | 6 on EVM | On Soroban it is read from the token and cached — Stellar USDC is 7 decimals, so only the leg that actually moves tokens is denominated in the token's own precision |

All internal pricing arithmetic is 6-decimal on both chains.

### The backend signature gate

Every user-initiated funding call (`invest`, `predeposit`, `fiatInvest`) and the self-service whitelist call carry a `nonce` and a `signature`. The contract rebuilds the message, recovers the signer, and requires it to equal the configured backend signer. The nonce is then burned, so a signature is single-use.

This is what enforces compliance off-chain: the backend only signs for a user whose identity has been declared and whose contract has been signed. The signature is obtained from the API — see [`/users/signatures/mint-proof`](https://lendxyz.gitbook.io/lend/reference/api-reference.md#signatures).

The message layouts differ per path:

| Path             | Message preimage                                                                  |
| ---------------- | --------------------------------------------------------------------------------- |
| On-chain invest  | `"ONCHAIN_INVEST" ‖ factory ‖ chainId ‖ opId ‖ user ‖ amount ‖ nonce`             |
| Fiat invest      | `"FIAT_INVEST" ‖ factory ‖ chainId ‖ opId ‖ user ‖ opLendHolder ‖ amount ‖ nonce` |
| opLend whitelist | `opLendAddress ‖ chainId ‖ user ‖ nonce`                                          |
| Oracle round     | `opId ‖ newPrice ‖ nonce`                                                         |

Binding the factory address and the chain id into the preimage is what prevents a signature obtained on one chain from being replayed on another.

### The whitelist

opLend tokens can only move between addresses that are both whitelisted. Minting whitelists the recipient automatically; a user who receives tokens through a bridge or wants to trade on the DEX calls `whitelistUser` with a backend signature, or is whitelisted by an admin.

This is the on-chain half of the [compliance](https://lendxyz.gitbook.io/lend/fundamentals/compliance.md) rules — an opLend token can never end up in the hands of an address Lend has not seen.

### Reward epochs

Revenue is not pushed to holders. Each period, the backend computes every holder's share, builds a merkle tree of `(user, balance)` leaves, and the admin publishes only the root together with the total allocation, which is transferred into the Rewards contract in the same call. Users then claim their leaf with a proof.

A root can only be written once per `(opId, epoch)` — an epoch is immutable after distribution. Referral rewards use the same mechanism with a root keyed by epoch only.

***

## Access control

| Level       | EVM                                                 | Soroban                                  |
| ----------- | --------------------------------------------------- | ---------------------------------------- |
| Owner/admin | `LibDiamond.enforceIsContractOwner()`, `onlyOwner`  | `require_admin` → `admin.require_auth()` |
| Backend     | ECDSA signature recovered against the stored signer | Ed25519 signature against the stored key |
| User        | `msg.sender`                                        | explicit `Address` + `require_auth()`    |

{% hint style="warning" %}
Admin-only methods are listed for completeness. They are callable only by the protocol owner and are not part of the surface an integrator or a user interacts with.
{% endhint %}

## Upgradeability

| Contract                              | Mechanism                                                           |
| ------------------------------------- | ------------------------------------------------------------------- |
| EVM Factory                           | EIP-2535 Diamond — facets are added, replaced or removed            |
| EVM Rewards, Restitution, PriceOracle | UUPS proxies, `_authorizeUpgrade` restricted to the owner           |
| Soroban Factory, Rewards              | `upgrade(new_wasm_hash)`, admin-only; contract id and state survive |
| opLend (both chains)                  | Not upgradeable                                                     |

See [Audits](https://lendxyz.gitbook.io/lend/technicals/audits.md) for the audit scope covering these contracts.
