> For the complete documentation index, see [llms.txt](https://docs.elara.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.elara.fi/protocol-reference/reference/api-reference/staking/contract.elusdstake.md).

# ElUSDStake

[Git Source](https://github.com/TrueFi-Protocol/elara-contracts-v2/blob/9301de6eec32ceafb4021f4e7da883b507799ebc/contracts/staking/ElUSDStake.sol)

**Inherits:** [ElaraBase](https://github.com/TrueFi-Protocol/elara-contracts-v2/blob/docs/latest/contracts/base/ElaraBase.sol/abstract.ElaraBase.md), IElUSDStake

**Title:** ElUSD Stake Contract

Staking contract that converts ElUSD to sElUSD shares

Implements share-based staking with no lock periods or NFTs Invariants:

* Exchange rate between ElUSD and sElUSD is determined by totalElUSD / totalSElUSD
* Users can stake and unstake at any time (permissionless)
* First staker gets 1:1 ratio, subsequent stakes use the current exchange rate
* Treasury can deposit yield to increase share value without minting new shares Upgradeability:
* Uses ElaraBase for UUPS upgradeability
* Initializer must be called exactly once; preserve storage layout

**Note:** security-contact: <security@elara.finance>

## State Variables

### \_elUSD

ElUSD token contract

```solidity
IElUSD internal _elUSD
```

### \_sElUSD

sElUSD token contract

```solidity
IsElUSD internal _sElUSD
```

### \_vault

Vault contract address (only vault can call receiveYield)

```solidity
address internal _vault
```

### \_\_gap

Reserved storage gap for future upgrades to prevent storage collisions

```solidity
uint256[47] private __gap
```

## Functions

### initialize

Security:

* Only callable during initialization via {initializer} modifier
* Validates non-zero addresses for all parameters (vault can be zero and set later)
* Establishes access control via {\_\_ElaraBase\_init}
* Critical for contract initialization; must be called exactly once per proxy

```solidity
function initialize(
    address _accessManager,
    address elUSDAddress,
    address sElUSDAddress,
    address vaultAddress
)
    external
    override
    initializer;
```

**Parameters**

| Name             | Type      | Description                                 |
| ---------------- | --------- | ------------------------------------------- |
| `_accessManager` | `address` | Address of the AccessManager contract       |
| `elUSDAddress`   | `address` | Address of the ElUSD token                  |
| `sElUSDAddress`  | `address` | Address of the sElUSD token                 |
| `vaultAddress`   | `address` | Address of the Vault contract (can be zero) |

### stake

Security:

* Uses {whenNotPaused} from ElaraBase to prevent staking when paused
* Uses {nonReentrant} from ElaraBase to prevent reentrancy attacks
* Follows checks-effects-interactions pattern
* Transfers ElUSD from user before minting sElUSD to prevent reentrancy

```solidity
function stake(uint256 amount) external override nonReentrant whenNotPaused returns (uint256 shares);
```

### unstake

Security:

* Uses {whenNotPaused} from ElaraBase to prevent unstaking when paused
* Uses {nonReentrant} from ElaraBase to prevent reentrancy attacks
* Follows checks-effects-interactions pattern
* Burns sElUSD before transferring ElUSD to prevent reentrancy

```solidity
function unstake(uint256 shares) external override nonReentrant whenNotPaused returns (uint256 amount);
```

### receiveYield

Security:

* Only callable by Vault contract
* Uses {nonReentrant} from ElaraBase to prevent reentrancy attacks
* Does NOT transfer ElUSD (already minted to this contract by Vault)
* Share price increases automatically due to increased ElUSD balance
* Emits {YieldReceived} event with new share price

```solidity
function receiveYield(uint256 amount) external override nonReentrant;
```

### elUSD

```solidity
function elUSD() external view override returns (address);
```

### sElUSD

```solidity
function sElUSD() external view override returns (address);
```

### vault

```solidity
function vault() external view override returns (address);
```

### setVaultAddress

Security:

* Uses {restricted} modifier to allow only authorized callers (admin role)
* Can be called after initialization to resolve circular dependency with Vault

```solidity
function setVaultAddress(address vaultAddress) external override restricted;
```

### totalElUSD

```solidity
function totalElUSD() public view override returns (uint256);
```

### sharePrice

Returns the current price per sElUSD share in ElUSD

* If no shares exist, returns 1e18 (1:1 ratio)
* Otherwise: price = (totalElUSD \* 1e18) / totalSupply

```solidity
function sharePrice() public view override returns (uint256 price);
```

### previewStake

Implements ERC4626-style share calculation

* If no sElUSD exists, mints 1:1
* Otherwise: shares = (amount \* totalSupply) / totalAssets

```solidity
function previewStake(uint256 elUSDAmount) public view override returns (uint256 shares);
```

### previewUnstake

Implements ERC4626-style asset calculation

* amount = (shares \* totalAssets) / totalSupply

```solidity
function previewUnstake(uint256 sElUSDShares) public view override returns (uint256 elUSDAmount);
```
