# 1. Introduction
iosiro was commissioned by [Synthetix](https://www.synthetix.io) to conduct a smart contract audit of their Schedar Release, which included [SIP-255](https://sips.synthetix.io/sips/sip-255). Two auditors performed the audit between 9 and 10 February, consuming three audit days.
This report is organized into the following sections.
* **[Section 2 - Executive summary:](#section-2)** A high-level description of the findings of the audit.
* **[Section 3 - Audit details:](#section-3)** A description of the scope and methodology of the audit.
* **[Section 4 - Design specification:](#section-4)** An outline of the intended functionality of the smart contracts.
* **[Section 5 - Detailed findings:](#section-5)** Detailed descriptions of the findings of the audit.
The information in this report should be used to understand the smart contracts' risk exposure better and as a guide to improving the security posture of the smart contracts by remediating issues identified. The results of this audit reflect the in-scope source code reviewed at the time of the audit.
The purpose of this audit was to achieve the following:
* Identify potential security flaws.
* Ensure that the smart contracts function according to the documentation provided.
Assessing the off-chain functionality associated with the contracts, for example, backend web application code, was outside of the scope of this audit.
Due to the unregulated nature and ease of transfer of cryptocurrencies, operations that store or interact with these assets are considered high risk from cyber attacks. As such, the highest level of security should be observed when interacting with these assets. This requires a forward-thinking approach, which takes into account the new and experimental nature of blockchain technologies. Strategies that should be used to encourage secure code development include:
* Security should be integrated into the development lifecycle, and the level of perceived security should not be limited to a single code audit.
* Defensive programming should be employed to account for unforeseen circumstances.
* Current best practices should be followed where possible.
# 2. Executive summary
This report presents the findings of a smart contract audit performed by iosiro of Synthetix's Schedar release.
Users previously needed to claim fees generated by the protocol each period. [SIP-255](https://sips.synthetix.io/sips/sip-255) adjusted the fee pool mechanism to burn all fees collected in that period when the period was closed. Doing so reduces the system's debt, effectively directly distributing the fees to the stakers.
Several low risk issues were identified and fixed during the audit.
# 3. Audit details
## 3.1 Scope
The source code considered in-scope for the assessment is described below. Code from all other files was considered to be out-of-scope. Out-of-scope code that interacts with in-scope code was assumed to function as intended and not introduce any functional or security vulnerabilities for the purposes of this audit.
### 3.1.1 Synthetix SIP-255 smart contracts
**Project Name:** Synthetix<br/>
**Files:** FeePool.sol, Issuer.sol
## 3.2 Methodology
A variety of techniques were used in order to perform the audit. These techniques are briefly described below.
### 3.2.1 Code review
The source code was manually inspected to identify potential security flaws. Code review is a useful approach for detecting security flaws, discrepancies between the specification and implementation, design improvements, and high-risk areas of the system.
### 3.2.2 Dynamic analysis
The contracts were compiled, deployed, and tested in a test environment, both manually and through the test suite provided. Manual analysis was used to confirm that the code was functional and to identify security issues that could be exploited.
### 3.2.3 Automated analysis
Tools were used to automatically detect the presence of several types of security vulnerabilities, including reentrancy, timestamp dependency bugs, and transaction-ordering dependency bugs. Static analysis results were reviewed manually and any false positives were removed. Any true positive results are included in this report.
Static analysis tools commonly used include Slither, Securify, and MythX. Tools such as the Remix IDE, compilation output, and linters could also be used to identify potential areas of concern.
## 3.3 Risk ratings
Each issue identified during the audit has been assigned a risk rating. The rating is determined based on the criteria outlined below.
* **High risk** - The issue could result in a loss of funds for the contract owner or system users.
* **Medium risk** - The issue resulted in the code specification being implemented incorrectly.
* **Low risk** - A best practice or design issue that could affect the security of the contract.
* **Informational** - A lapse in best practice or a suboptimal design pattern that has a minimal risk of affecting the security of the contract.
* **Closed** - The issue was identified during the audit and has since been satisfactorily addressed, removing the risk it posed.
# 4. Design specification
The following section outlines the intended functionality of the system at a high level.
## 4.1 SIP-255
The specification of SIP-255 was based on commit hash [910717c](https://github.com/Synthetixio/SIPs/blob/910717c145bab59c3cb5b0fd8903e957a55517cb/content/sips/sip-255.md).
# 5. Detailed findings
The following section includes in-depth descriptions of the findings of the audit.
## 5.1 High risk
No high-risk issues identified during the audit were present at the conclusion of the audit.
## 5.2 Medium risk
No medium-risk issues identified during the audit were present at the conclusion of the audit.
## 5.3 Low risk
No low-risk issues identified during the audit were present at the conclusion of the audit.
## 5.4 Informational
No informational issues identified during the audit were present at the conclusion of the audit.
## 5.5 Closed
### 5.5.1 Incorrect array declaration (low risk)
The `feesByPeriod()` function incorrectly specified its return parameter as `uint[FEE_PERIOD_LENGTH]` instead of `uint[FEE_PERIOD_LENGTH]`. In practice, there was no issue as `FEE_PERIOD_LENGTH` was set to `2`; however, if the value were to be changed, the array would have the wrong dimensions.
`feesByPeriod()` should return `uint[FEE_PERIOD_LENGTH]`. Additionally, all calls to it should be updated to store the returned value correctly.
Recommended changes implemented in [2d73337](https://github.com/Synthetixio/synthetix/pull/1990/commits/2d733375afc09036edae408c1bcbb162c3b3671f#diff-0e3886766356712056c6555d1078630ca0ea691ebf015c964e887085ff31fb74R527).
### 5.5.2 Incorrect array referencing (low risk)
SIP-255 was designed to burn all fees generated during that period when period is closed. As a result, it should not be possible for fees to rollover between periods, as was previously possible. Several references to `FEE_PERIOD_LENGTH - 2` were made in the [_closeSecondary() function](https://github.com/Synthetixio/synthetix/blob/ffac8ecee63b65f1aaddfbee2d83ed247528da8f/contracts/FeePool.sol#L283). As `FEE_PERIOD_LENGTH` was set to `2`, the logic correctly performed calculations on the period being closed; however, if `FEE_PERIOD_LENGTH` were to be changed, the fee period logic would likely not operate as intended.
Adjust fee distribution logic to directly reference fee period `0` instead of using `FEE_PERIOD_LENGTH - 2`.
Recommended changes implemented in [2d73337](https://github.com/Synthetixio/synthetix/pull/1990/commits/2d733375afc09036edae408c1bcbb162c3b3671f).
### 5.5.3 Potentially incorrect number of iterations in `for` loop (low risk)
The `feesBurned()` and `totalFeesBurned()` functions used `for` loops that iterated from `1` to `FEE_PERIOD_LENGTH`, which in effect only performed one iteration of the loop with a value `1`. Referencing period `1` returned correct values for the most recently closed period; however, if the value of `FEE_PERIOD_LENGTH` were to be changed, the functions would return incorrect values, multiple periods would be used in the calculation.
Only return a value for period `1` in `feesBurned()` and `totalFeesBurned()`.
Recommended changes implemented in [11f5523](https://github.com/Synthetixio/synthetix/pull/1990/commits/11f55230e59359548371cf164fa057825d729a53).
### 5.5.4 `feesToBurn()` may report incorrect values (low risk)
In `feesToBurn()`, the value reported could be inaccurate as `_recentFeePeriodsStorage(0).feesToDistribute` does not consider pending fees to be distributed by the EtherWrapper or WrapperFactory . These amounts are only distributed when the `_closeSecondary()` function is called.
1. The function comment should indicate that the value is only an approximation based on the current system state. Changes in the debt shares, debt value, or period fees would change the outcome of the final amount.
2. The `_feesAndRewardsFromPeriod()` function should be used to calculate the debt value instead of repeating the logic to reduce code repetition.
Recommended changes implemented in in [2d73337](https://github.com/Synthetixio/synthetix/pull/1990/commits/2d733375afc09036edae408c1bcbb162c3b3671f#diff-0e3886766356712056c6555d1078630ca0ea691ebf015c964e887085ff31fb74R553).
### 5.5.5 Unnecessary `if` statement (informational)
The `if` statement on [FeePool.sol#L389](https://github.com/Synthetixio/synthetix/blob/ffac8ecee63b65f1aaddfbee2d83ed247528da8f/contracts/FeePool.sol#L389) unnecessarily checked whether the `availableFees` value was greater than `0`, as the value would be set to `0` regardless.
Removed in [11f5523](https://github.com/Synthetixio/synthetix/pull/1990/commits/11f55230e59359548371cf164fa057825d729a53#diff-0e3886766356712056c6555d1078630ca0ea691ebf015c964e887085ff31fb74L389).