Synthetix Menkent Release Smart Contract Audit

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

* [SIP-198](https://sips.synthetix.io/sips/sip-198) from 23 to 29 March 2022 with one auditor, consuming a total of 5 resource days.
* [SIP-222](https://sips.synthetix.io/sips/sip-222) on 4 April 2022 with one auditor, consuming a half a resource day.

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 Menkent release.

[SIP-198](https://sips.synthetix.io/sips/sip-198) was introduced to update the atomic exchange functionality to allow trading at pure Chainlink prices for certain synths, as well as to remove the restriction of requiring the source or destination synth to be sUSD.

Two design comments were made, one of which related to adding a slippage control to protect users in the case of a volatile spot price. Both comments were closed by the end of the audit. Overall, the code was found to be of a high standard.


[SIP-222](https://sips.synthetix.io/sips/sip-222) updated the exchange fees to be charged to be calculated as the sum of fees for source and destination synths. This was done to account for the different levels of volatility between synths, so an appropriate fee can be charged during an exchange.

Two refactoring suggestions were made, both of which were closed by the end of the audit. Overall, the code was found to be of a high standard.


<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-198 smart contracts

**Project Name:** Synthetix<br/>
**Commit:** [65ddcf7](https://github.com/Synthetixio/synthetix/pull/1667/commits/65ddcf75386e79d8a6e658631ce558dbc635b68c) <br/>
**Files:** ExchangeRatesWithDexPricing.sol, ExchangerWithFeeRecAlternatives.sol, MixinSystemSettings.sol, SystemSettings.sol, SystemSettingsLib.sol

### 3.1.2 Synthetix SIP-222 smart contracts

**Project Name:** Synthetix<br/>
**Commit:** [ebcac11](https://github.com/Synthetixio/synthetix/pull/1749/commits/ebcac11d4e20c5d8041c0da983db5cb5455aea3b) <br/>
**Files:** Exchanger.sol, ExchangerWithFeeRecAlternatives.sol

## 3.2  Methodology

The audit was conducted using a variety of techniques 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-198

The specification of SIP-198 was based on commit hash [00bea4d2](https://github.com/Synthetixio/SIPs/blob/00bea4d29369c6a1c6023db30fdd6cbd1acf416a/content/sips/sip-198.md).

## 4.2 SIP-222

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

<a name="section-5"></a>
# 5. Detailed findings

The following section details the findings of the audit.

## 5.1 High risk

No identified high-risk issues were open at the conclusion of the review.

## 5.2 Medium risk

No identified medium-risk issues were open at the conclusion of the review.

## 5.3 Low risk

No identified low-risk issues were open at the conclusion of the review.

## 5.4 Informational

No identified informational issues were open at the conclusion of the review.

## 5.5 Closed

### 5.5.1 Atomic swap slippage control
*SIP-198*

As per SIP-120, the synth rates for atomic exchanges is the worst price between a buffered Chainlink price, the Uniswap V3 TWAP price, and the Uniswap V3 spot price. The spot price is taken as the prior block's observed price rather than the current block price to prevent flashloan style attacks. However, a large legitimate trade may swing the price to the downside, leaving the spot price to be chosen as the worst price between the three available options. For this reason, a slippage control mechanism was suggested to ensure that users receive at least a minimum amount out of their destination synth.

#### Update

Implemented in [3ed5350](https://github.com/Synthetixio/synthetix/pull/1667/commits/3ed5350a25dcf942cc281e7f75d37f077083a6c0).

### 5.5.2 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-198

1. [dexPrice](https://github.com/Synthetixio/synthetix/blob/65ddcf75386e79d8a6e658631ce558dbc635b68c/contracts/ExchangeRatesWithDexPricing.sol#L101) and [chainlinkPriceWithBuffer](https://github.com/Synthetixio/synthetix/blob/65ddcf75386e79d8a6e658631ce558dbc635b68c/contracts/ExchangeRatesWithDexPricing.sol#L107) should be renamed to `dexValue` and `chainlinkValueWithBuffer` respectively to indicate that the value returned from the exchange and not the rate of a synth.

#### Update

1. Fixed in [42726f0](https://github.com/Synthetixio/synthetix/pull/1667/commits/42726f07dc1ce17897fc5b308f54977a66425ab4).

#### SIP-222

1. [Exchanger.sol#L778](https://github.com/Synthetixio/synthetix/blob/ebcac11d4e20c5d8041c0da983db5cb5455aea3b/contracts/Exchanger.sol#L778) and [Exchanger.sol#L798](https://github.com/Synthetixio/synthetix/blob/ebcac11d4e20c5d8041c0da983db5cb5455aea3b/contracts/Exchanger.sol#L798) should be updated to reflect the updated exchange rate calculation.
2. [ExchangerWithFeeRecAlternatives.sol#L272-L273](https://github.com/Synthetixio/synthetix/blob/ebcac11d4e20c5d8041c0da983db5cb5455aea3b/contracts/ExchangerWithFeeRecAlternatives.sol#L272-L273) could be removed since the `sourceCurrencyKey` was no longer unused.


#### Update

1. Fixed in [d5f870e](https://github.com/Synthetixio/synthetix/pull/1749/commits/d5f870ed84366e890a9c6f044c749752a5d3e393).
2. Fixed in [20462e0](https://github.com/Synthetixio/synthetix/pull/1749/commits/20462e01dd6577174edf5df7f1d34de14ef46252).

Secure your system.
Request a service
Start Now