Synthetix Alsephina Release Smart Contract Audit

# 1. Introduction
iosiro was commissioned by [Synthetix](https://www.synthetix.io) to conduct a smart contract audit of their Alsephina Release, which included the following components:

* [SIP-184](https://sips.synthetix.io/sips/sip-184) on 20 to 21 January 2022 with one auditor, consuming a total of 2 resource days.
* [SIP-193](https://sips.synthetix.io/sips/sip-193) on 21 January 2022 with one auditor, consuming a total of 1 resource day.
* [SIP-196](https://sips.synthetix.io/sips/sip-196) on 17, 18, and 25 January 2022 with one auditor, consuming a total of 3 resource 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.

<a name="section-2"></a>
# 2. Executive summary

This report presents the findings of a smart contract audit performed by iosiro of Synthetix's Alsephina release.

[SIP-184](https://sips.synthetix.io/sips/sip-184) introduced a dynamic fee dependent on synth price volatility to limit front-running opportunities. While no vulnerabilities were found in the implementation, several design remarks were raised.

[SIP-193](https://sips.synthetix.io/sips/sip-193) refactored the `SystemSettings` contract to use a library for the majority of its functionality to reduce its contract size. No vulnerabilities were found in its implementation; a single design comment was raised.

[SIP-196](https://sips.synthetix.io/sips/sip-196) removed all code that interacted with the internal price oracle from the `ExchangeRates` contract. No vulnerabilities were found by this code removal; one design comment was raised.

<a name="section-3"></a>
# 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-200 smart contracts

#### SIP-184

**Commit:** [6edd50f](https://github.com/Synthetixio/synthetix/pull/1649/commits/6edd50f259762dbb238bc279bd9f3a0579da86be) <br/>
**Files:** ExchangeRates.sol, Exchanger.sol, ExchangerWithFeeRecAlternatives.sol, MixinSystemSettings.sol, SystemSettings.sol, SystemSettingsLib.sol

#### SIP-193

**Commit:** [7db579f](https://github.com/Synthetixio/synthetix/pull/1627/commits/7db579f03201189e148eacdb3d581b71bf84a75b) </br>
**Files:** MixinSystemSettings.sol, SystemSettings.sol, SystemSettingsLib.sol

#### SIP-196

**Commit:** [bec2040](https://github.com/Synthetixio/synthetix/pull/1636/commits/bec2040fdb8a79091233afd9687733dc2ca555ec) <br/>
**Files:** ExchangeRates.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 discover 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.

<a name="section-4"></a>
# 4. Design specification

The following section outlines the intended functionality of the system at a high level.

## 4.1 SIP-184

The specification of SIP-184 was based on commit hash [8d2bcaa](https://github.com/Synthetixio/SIPs/blob/8d2bcaaa800df10cb42f7059b1e79d5f760c46b6/content/sips/sip-184.md).

## 4.1 SIP-193

The specification of SIP-193 was based on commit hash [8d2bcaa](https://github.com/Synthetixio/SIPs/blob/8d2bcaaa800df10cb42f7059b1e79d5f760c46b6/content/sips/sip-193.md).

## 4.1 SIP-196

The specification of SIP-196 was based on commit hash [a9d7c5b](https://github.com/Synthetixio/SIPs/blob/a9d7c5b1bae78be1831eac56d49b37fe0f235e55/content/sips/sip-196.md).

<a name="section-5"></a>
# 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  

### 5.4.1 Design comments

Actions to improve the functionality and readability of the codebase are outlined below.

#### Refactoring suggestions

Portions of the code can be refactored to improve readability and consistency, as indicated below.

##### SIP-193

1. The following functions implemented in SIP-184 were added directly to `SystemSettings` and could be rewritten to use the library for consistency:
   * [`setExchangeDynamicFeeThreshold(...)`](https://github.com/Synthetixio/synthetix/blob/ccda4a74dd0e31f492d25a8e4015c39915617149/contracts/SystemSettings.sol#L310)
   * [`setExchangeDynamicFeeWeightDecay(...)`](https://github.com/Synthetixio/synthetix/blob/ccda4a74dd0e31f492d25a8e4015c39915617149/contracts/SystemSettings.sol#L321)
   * [`setExchangeDynamicFeeRounds(...)`](https://github.com/Synthetixio/synthetix/blob/ccda4a74dd0e31f492d25a8e4015c39915617149/contracts/SystemSettings.sol#L332)
   * [`setExchangeMaxDynamicFee(...)`](https://github.com/Synthetixio/synthetix/blob/ccda4a74dd0e31f492d25a8e4015c39915617149/contracts/SystemSettings.sol#L341)

##### SIP-196

The function `effectiveValueAtRound` returned a value of `sourceAmount` when `sourceCurrencyKey == destinationCurrencyKey` and the currency was non-existent. Returning a non-zero value for this case may give the impression that an exchange can be performed.

Given the current behavior of the function and the likelihood that third party systems will interact with it, documentation should clearly state that this is a possible edge case for integrations to consider.

## 5.5 Closed

### 5.5.1 Design comments (informational)

#### Default return for  `effectiveValueAndRatesAtRound`
*SIP-184 [ExchangeRates.sol#L120](https://github.com/Synthetixio/synthetix/blob/6edd50f259762dbb238bc279bd9f3a0579da86be/contracts/ExchangeRates.sol#L120)*

The `if`-statement in the `effectiveValueAndRatesAtRound()` function did not set the `destinationRate`, leaving the return value at the default of 0. While no impact was found as a result of this, it was recommended to set the `destinationRate` to the `sourceRate` for consistency.  

##### Update

Fixed in [c357a21](https://github.com/Synthetixio/synthetix/blob/c357a214de2b5a26b32eb9cfa941f83aceca26a2/contracts/ExchangeRates.sol#L138).

#### Fee rates are not set through a configurable state variable
*SIP-184 [Exchanger.sol#L806](https://github.com/Synthetixio/synthetix/blob/6edd50f259762dbb238bc279bd9f3a0579da86be/contracts/Exchanger.sol#L806), [Exchanger.sol#L828](https://github.com/Synthetixio/synthetix/blob/6edd50f259762dbb238bc279bd9f3a0579da86be/contracts/Exchanger.sol#L828)*

The fee rate for the `_feeRateForExchange()` and `_feeRateForExchangeAtRounds()` functions were capped to 100%. Although it was unlikely that this cap would be reached under normal circumstances, setting this fee cap as a configurable global state variable would allow it to be reduced as needed.

##### Update

Fixed in [f9e8649](https://github.com/Synthetixio/synthetix/commit/f9e8649523f7345b148cbe5fd832751d7d214efc). As a result of the fix, trades will no-op if the dynamic fee cap is hit, as implemented in [3924d75](https://github.com/Synthetixio/synthetix/pull/1661/commits/3924d75f6a76740f2fb13a7488978efb755218d4).

#### Incorrect function visibility
*SIP-184 [Exchanger.sol#L923](https://github.com/Synthetixio/synthetix/blob/6edd50f259762dbb238bc279bd9f3a0579da86be/contracts/Exchanger.sol#L923)*

The `_thresholdedAbsDeviationRatio()` function had `public` visibility when it should have been marked as `internal`.

##### Update

Fixed in [c357a21](https://github.com/Synthetixio/synthetix/blob/c357a214de2b5a26b32eb9cfa941f83aceca26a2/contracts/Exchanger.sol#L923).

#### Unclear function description
*SIP-184 [ExchangeRates.sol#L315](https://github.com/Synthetixio/synthetix/blob/6edd50f259762dbb238bc279bd9f3a0579da86be/contracts/ExchangeRates.sol#L315)*

The function name `anyRateIsInvalidAtRound()` could be misunderstood since it checks if a rate at a round is invalid relative to the current timestamp `now`, but could be interpreted as "checks if the rate was invalid at the time of the round". In-line comments to explain the use of this function would aid users reading the codebase.

##### Update

Clarified in [c357a21](https://github.com/Synthetixio/synthetix/blob/c357a214de2b5a26b32eb9cfa941f83aceca26a2/contracts/ExchangeRates.sol#L316-L318).

Secure your system.
Request a service
Start Now