# EHIVE: reward miscalculation in stake() drains staking pool

EHIVE stake() updated staked balance before computing earned rewards, letting an attacker stake 0, wait, then stake to mint large EHIVE rewards and sell.

- Payout: Real-world hack
- Severity: high
- Program: EHIVE
- Analysis by: DeFiHackLabs
- Disclosed: 2023-08-21
- Write-up quality: 7/10
- Original: https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2023-08/EHIVE_exp.sol
- Library page: https://bugbountylibrary.com/r/dhl%3A20230821-ehive
- Tags: area:web3 domain:web3 impact:funds-loss kind:incident platform:defihacklabs sev:high tech:evm tech:solidity technique:chained vuln:accounting-error vuln:business-logic vuln:reward-distribution

**Target:** EHIVE staking contract on Ethereum mainnet
**Root cause:** In stake(), the contract increments _stakers[].staked before calling _userEarned(), so the earned calculation uses the already-inflated staked amount, over-crediting rewards.
**Steps:**
1. Deploy 28 helper contracts; each calls `EHIVE.stake(0, 0)` to register as a staker with zero stake.
2. Wait ~38 days so the zero-stake position accrues time-based reward.
3. Flash loan 18 WETH from Aave, swap WETH->EHIVE on the EHIVE/WETH Uniswap pair.
4. Transfer EHIVE to contractList[0].
5. Chain `unstake(next)` across the 28 contracts: each calls `EHIVE.stake(EHIVE.balanceOf(this), 0)` then `EHIVE.unstake(0)` and forwards EHIVE, minting inflated rewards at each hop.
6. Final contract unstakes to attacker, swap EHIVE->WETH, repay flash loan.
**How found:** Researcher read stake() and noticed staked is incremented before _userEarned() is called, meaning earned is computed against the post-increment balance rather than the prior one.
**Exploitation:** Attacker pre-registered 28 zero-stake positions, waited 38 days, then used an 18 WETH Aave flash loan to buy EHIVE and chain stake/unstake calls through the helper contracts. Each stake() call credited earned rewards based on the inflated staked balance, compounding EHIVE across hops; attacker sold the minted EHIVE for WETH and repaid the loan, netting ~$15K.
**Escalation:** Chained 28 helper contracts so each hop's inflated reward became the next hop's stake, compounding the over-credit before selling.
**Insight:** In staking/reward contracts, check the ordering of state mutations vs. reward accrual: if earned is computed after staked is increased, a zero-stake registration plus a later stake mints rewards for the whole elapsed period. Registering many zero-stake positions early and chaining stake/unstake compounds the over-credit.

