Contracts

Solvency Model

The vault always holds at least enough to return every staker's principal and pay every reward it has already committed.

Invariant
1
Mechanisms
3
Checked by
Fuzz tests + cron

The problem

LayerSub stakes and pays rewards in the same ERC-20. A vault holding one asset for two purposes has no structural separation between principal and yield — the balance is a single number, and nothing in the token prevents paying one out of the other.

The failure mode this invites is a vault that quietly funds yield from deposits. It looks solvent while stake is growing, because incoming principal covers outgoing rewards, and it fails at the moment withdrawals outpace deposits. The problem is not that the vault runs out of tokens; it is that the shortfall is invisible until someone tries to leave.

The invariant

One inequality, asserted over every reachable state of the vault:

Solvency invariant

vaultBalance >= totalStaked + unclaimedRewards

It is an inequality rather than an equality on purpose — see rounding.

Three mechanisms

The invariant is not enforced by a runtime check. It holds because three separate properties of the vault each remove a way to break it.

01

Rewards come only from an explicit budget

fundRewards is the only path by which reward tokens enter rewardsAvailable, and it is owner-gated. A plain ERC-20 transfer to the vault does not increase the budget — it becomes unaccounted surplus the accounting never spends.

Therefore

Yield can never originate from a stake() call, because stake() does not touch rewardsAvailable.

02

Emission stops at the budget, not at a date

periodFinish is re-derived from rewardsAvailable / rewardRate on every funding and every rate change, and lastTimeRewardApplicable() caps accrual at it. Raising the rate shortens the window rather than over-committing.

Therefore

The vault cannot promise more than it holds. There is no configuration of rate and budget that commits tokens the vault has not been funded with.

03

Recovery cannot reach committed value

recoverUnallocatedRewards draws only from rewardsAvailable, and settles accounting before it checks the balance — so pending accrual is already moved out of the budget by the time the withdrawal is authorised.

Therefore

The owner cannot front-run accrual, and cannot touch staked principal at all. The uncommitted budget is the entire owner-recoverable surface.

Rounding

Rounding favours the vault

rewardsAvailable is debited by the full elapsed × rewardRate, while each staker's share passes through two floor divisions — once in rewardPerToken(), once in earned(). Slightly less is therefore paid out than was committed.

The difference accumulates as surplus the vault holds beyond what it owes. The invariant is an inequality for this reason: the last staker out is over-covered, never short.

Reward per tokenrewardPerToken() = rewardPerTokenStored + (elapsed × rewardRate × 1e18) / totalStaked

elapsed = lastTimeRewardApplicable() − lastUpdateTime. Returns the stored value unchanged while totalStaked is zero.

Earned by a stakerearned(a) = balanceOf(a) × (rewardPerToken() − userRewardPerTokenPaid[a]) / 1e18 + rewards[a]

Two floor divisions sit between the budget and the payout, which is where the vault's rounding surplus comes from.

Edge cases

States that look alarming and are not, plus the one that is merely lossy.

Nobody is staked

Accrual pauses — with totalStaked at zero there is no per-token rate, so nothing is committed and the budget is not debited. rewardPerToken() returns its stored value unchanged.

Verdict

Safe, but lossy: periodFinish is not pushed back to compensate, so wall-clock time keeps advancing and budget can be stranded past the end of the window. The owner recovers it or funds again to extend.

Rate raised mid-window

Accrued rewards are settled first, then periodFinish is recomputed from the remaining budget at the new rate.

Verdict

Safe. A rate change never retroactively alters what stakers have already earned, and never extends a commitment past the budget.

Rate set to zero

Emission halts immediately and periodFinish is set to the current block timestamp.

Verdict

Safe. Already-accrued rewards remain claimable; only future accrual stops.

Transfer-taxing or rebasing staking token

stake() and fundRewards() credit the measured balance delta rather than the requested amount, and revert with ZeroAmount if the delta is zero.

Verdict

Accounting stays synchronised with the balance. Not exercised in this deployment, where the token is a standard ERC-20.

Reentrancy during a claim

Every state-changing entry point carries nonReentrant, and accounting is settled before tokens move.

Verdict

Covered by an ERC-777-style reentrancy test in the Foundry suite alongside the fuzzed invariant.

Verification

The argument above is checked in three places rather than taken on faith.

  1. 01Foundry fuzz tests

    The invariant is asserted over randomised sequences of stakes, withdrawals, claims, funding and rate changes. It is a property test, not a scenario test — the sequence is generated, not written.

  2. 02Backend cron

    The indexer re-checks the invariant against live chain state on a schedule and stores the verdict, including the surplus, in each snapshot.

  3. 03Public API

    The latest verdict is served on /stats as solvency.solvent, solvency.consistent and solvency.surplus, and the historical series on /stats/history. Anyone can audit it without running the indexer.

No third-party audit has been performed. See the FAQ.

Reconstructing liability

If you build your own indexer

  • Total reward liability at a given moment is not readable in a single call. earned() is per-account, and the vault stores no aggregate of unsettled accrual — rewardPerTokenStored plus the per-account snapshots is the whole picture, and summing it requires knowing every staker.
  • An independent indexer therefore reconstructs it from events: track every address that has ever staked, replay rewardPerToken() at the block of interest, and apply the same two floor divisions the contract uses. Skipping the flooring overstates the liability and will report a solvent vault as insolvent.