<!-- canonical: https://0xsimao.com/findings/m-0-started-earning-emitted-already -->

# StartedEarning event is emitted even if account is already earning

Low/Info · Three Sigma · Stablecoin framework · 8th January, 2024

Finding 3S-M^0-N07 of the M^0 Minter Gateway security review.

- Protocol: https://www.m0.org/
- Report: /reports/m-0
- Codebase: https://github.com/0xsimao/ttg/tree/a8127901fa1f24a2e821cf4d9854a1aa6ac8088c
- Source: https://cdn.sanity.io/files/qoqld077/production/1cdafafad874aba76e062ad8c216c98338c096db.pdf

---

### Description

In [MToken._startEarning](https://github.com/MZero-Labs/protocol/blob/3499f50ff3382729f3e59565b19386ba61ef8e36/src/MToken.sol#L239C5-L239C56) the StartedEarning event will be emitted even if the calling
account is already earning:

```solidity
function _startEarning(address account_) internal {
    emit StartedEarning(account_);
    MBalance storage mBalance_ = _balances[account_];
    if (mBalance_.isEarning) return;
    // ... }
```

The same happens in the [MToken._stopEarning](https://github.com/MZero-Labs/protocol/blob/3499f50ff3382729f3e59565b19386ba61ef8e36/src/MToken.sol#L271). The StoppedEarning event will be emitted
even if the user isn't an earner.

### Recommendation

Move the line that emits the event to the end of the function so that it will only fire when a
new user starts/stops being an earner.

### Status

Addressed in [#5b68e4e](https://github.com/MZero-Labs/protocol/commit/5b68e4e70a09b13e49b82267442b30555a083ddf).

---

Related findings:

- [Incorrect `RefundClaim` event due to deleting `idoConfig.accountPositions[msg.sender]`](https://0xsimao.com/findings/blast-ido-pools-refund-deleting-config-positions): Blast IDO Pools
