Synthetix Altair Release Smart Contract Audit

# 1. Introduction
iosiro was commissioned by [Synthetix](https://www.synthetix.io) to conduct a smart contract audit on the Altair release, including [SIP-15](https://sips.synthetix.io/sips/sip-15), [SIP-46](https://sips.synthetix.io/sips/sip-46), and [SIP-47](https://sips.synthetix.io/sips/sip-47). The audit was performed between 27 and 29 May, and 02 and 04 June.  

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 risk exposure of the smart contracts, and as a guide to improving the security posture of the smart contracts by remediating the issues that were identified. The results of this audit are only a reflection of the source code reviewed at the time of the audit and of the source code that was determined to be in-scope.

The purpose of this audit was to achieve the following:

* Ensure that the smart contracts functioned as intended.  
* Identify potential security flaws.

Assessing the market effect, economics, game theory, or underlying business model of the platform were strictly beyond 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 very high risk with regards to 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. There are a number of techniques that can help to achieve this, some of which are described below.

* Security should be integrated into the development lifecycle.
* Defensive programming should be employed to account for unforeseen circumstances.
* Current best practices should be followed when possible.

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

This report presents the findings of the audit performed by iosiro on the smart contract implementation of the [Altair release](https://blog.synthetix.io/the-altair-release/).  

#### SIP-15

The purpose of SIP-15 was to introduce a liquidation mechanism for SNX collateral. Synths can be redeemed for staked SNX at a discount if the collateral ratio of a staker falls below a specified liquidation ratio for a configurable period of time.

Two high risks issues and several informational issues were identified during the audit. By the conclusion of the audit, all of the issues had been addressed. Overall, the implementation was of a high standard and accorded with the specification provided.

#### SIP-56

SIP-56 introduced per-synth exchange rate fees. Prior to this SIP's implementation, a single universal fee was used for exchanging all synths. With the implementation of the SIP, different exchange fee rates can be set for each synth by the DAO. When exchanging synths, the fee rate of the synth being exchanged into will be charged.

Two informational issues were raised during the audit, one of which was already noted by a comment in the contract code. The implementation accorded with its specification and was of a high standard.

#### SIP-57

SIP-57 introduced a permanent read-only proxy that would point to the address of the latest AddressResolver contract. This was done to provide third-party contracts with a stable method for calling non-mutative functions in the AddressResolver contract. This would reduce the burden on third-parties to upgrade their contracts as the Synthetix protocol is upgraded.

Three design comments were raised during the audit, all of which were addressed. The implementation accorded with its specification and was 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 is considered to be out-of-scope. Out-of-scope code that interacts with in-scope code is assumed to function as intended and introduce no functional or security vulnerabilities for the purposes of this audit.

### 3.1.1 Synthetix SIP-15 Smart Contracts

**Project Name:** Synthetix<br/>
**Commits:** [7bcd24d](https://github.com/Synthetixio/synthetix/tree/7bcd24db1fdbde57e62e0f07ad5666439034c358/contracts)<br/>
**Files:** Exchanger.sol, Issuer.sol, Liquidations.sol, Synthetix.sol

### 3.1.2 Synthetix SIP-56 Smart Contracts

**Project Name:** Synthetix<br/>
**Commits:** [048165f](https://github.com/Synthetixio/synthetix/pull/526/commits/048165ff1f57e8fc9ee7dbd350d6390f38f92f0c)<br/>
**Files:** Exchanger.sol, FeePool.sol

### 3.1.3 Synthetix SIP-57 Smart Contracts

**Project Name:** Synthetix<br/>
**Commits:** [d4c2e5c](https://github.com/Synthetixio/synthetix/pull/512/commits/d4c2e5c69449675313d36655b2fe0fac2aaa3b35)<br/>
**Files:** AddressResolver.sol, ReadyProxy.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 Ganache test environment, both manually and through the test suite provided. Manual analysis was used to confirm that the code operated at a functional level, and to verify the exploitability of any potential security issues identified.

### 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. The static analysis results were manually analyzed to remove false-positive results. True positive results would be indicated in this report. Static analysis tools commonly used include Slither, Securify, and MythX. Tools such as the Remix IDE, compilation output, and linters are also used to identify potential issues.

## 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 addressed to a satisfactory level to remove the risk that 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-15

The specification of SIP-15 was based on commit hash [d93eccf](https://github.com/Synthetixio/SIPs/blob/d93eccf8ad5b3c8cbf9658a6313e92bf2f887fe4/SIPS/sip-15.md).

## 4.2 SIP-56

The specification of SIP-56 was based on commit hash [a135dcb](https://github.com/Synthetixio/SIPs/blob/a135dcbad1ca0e97ba61718a649c91b2eeaa0156/SIPS/sip-56.md).

## 4.3 SIP-57

The specification of SIP-57 was based on commit hash [169358f](https://github.com/Synthetixio/SIPs/blob/169358f9e8062e15ba80608d6280b50e086666dd/SIPS/sip-57.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 were present at the conclusion of the audit.

## 5.2 Medium Risk

No medium risk issues were present at the conclusion of the audit.

## 5.3 Low Risk

No low risk issues were present at the conclusion of the audit.

## 5.4 Informational  

### 5.4.1 Design Comments

#### `msg.sender` in Proxied Functions
*SIP-57: [ReadProxy.sol#L21](https://github.com/Synthetixio/synthetix/blob/d4c2e5c69449675313d36655b2fe0fac2aaa3b35/contracts/ReadProxy.sol#L21)*

As noted in the comments for `ReadProxy`'s fallback function, all proxied functions will see `msg.sender` as the address of the `ReadProxy`, rather than as the address calling functions on the `ReadProxy`. `ReadProxy` is only used to proxy `AddressResolver`, and does not proxy mutative functions. In the current implementation, none of the functions able to be proxied by `ReadProxy` use `msg.sender`, so this has no negative implications.

This issue should be kept in mind for future upgrades and integrations, as it has the potential to cause unexpected behaviour.

#### Comment Clarity
*SIP-57: [ReadProxy.sol#L21](https://github.com/Synthetixio/synthetix/blob/d4c2e5c69449675313d36655b2fe0fac2aaa3b35/contracts/ReadProxy.sol#L21)*

This comment was missing a word and should read "msg.sender in the underlying *call*". Alternatively, it could be rephrased as "msg.sender in the proxied call" for enhanced clarity.

## 5.5 Closed

### 5.5.1 Deadline Unenforced (High Risk)
*SIP-15: [Liquidations.sol#L117](https://github.com/Synthetixio/synthetix/blob/4e1203b2e6f84942d0019ad9166999e3742bd85d/contracts/Liquidations.sol#L117)*

#### Description
Liquidations on an account could only be performed after a set period of time had elapsed since the account was flagged for liquidation. When verifying whether this deadline had passed, the deadline duration was added to the value of `now`. This resulted in the `isOpenForLiquidation(...)` returning true immediately after an account was flagged for liquidation, negating the deadline.

#### Remedial Action
To correctly enforce the deadline, the affected line of code should be changed to the below:

```
now > liquidation.deadline
```

##### Update
Implemented in [825e0de](https://github.com/Synthetixio/synthetix/pull/536/commits/825e0de62a079ebebee1b41202e5c8c397865ce1).

### 5.5.2 Partial Liquidation Prevents Full Liquidation (High Risk)
*SIP-15: [Liquidations.sol#L115](https://github.com/Synthetixio/synthetix/blob/4e1203b2e6f84942d0019ad9166999e3742bd85d/contracts/Liquidations.sol#L115)*

#### Description
When liquidating an account, validation was performed to ensure that the account's collateralisation ratio was above the liquidation ratio. If a partial liquidation occurred, its collateralisation ratio would be decreased. A partial liquidation could thus lead to a case where the account was liquidated under the liquidation ratio, but above the issuance ratio. Under these conditions, it would no longer be possible to liquidate the account, despite the account still being under-collateralised.  

#### Remedial Action
It is recommended that the liquidation ratio validation be removed, resulting in only checking whether the collateralisation ratio is above the issuance ratio.

##### Update
Implemented in [2f7b0d4](https://github.com/Synthetixio/synthetix/pull/536/commits/2f7b0d45c2a14fa607fcc2a8d5b020a2fe36e2d0).

### 5.5.3 Burn Does Not Reset Liquidation Flag (Informational)
*SIP-15: [Issuer.sol#L281](https://github.com/Synthetixio/synthetix/blob/4e1203b2e6f84942d0019ad9166999e3742bd85d/contracts/Issuer.sol#L281)*

#### Description
The `burnSynthsToTarget()` function correctly reset the liquidation flag when being called; however, if synths were burned through the `burnSynths(...)` or `burnSynthsOnBehalf(...)` the flag would only be reset if the total debt was cleared.  

#### Remedial Action
It is recommended that the logic be changed to reset the liquidation flag as described in the specification.

##### Update

The burn functionality was modified to correctly remove the flag in [225de7c](https://github.com/Synthetixio/synthetix/pull/536/commits/225de7c4005b03189b500ecd9b170dab4342f3f1), resulting in the desired behaviour.

### 5.5.4 Design Comments (Informational)

#### Fix Spelling and Grammatical Errors

Language mistakes were identified in the comments and revert messages in the codebase. Fixing these mistakes can help improve the end-user experience by providing clear information on errors encountered, and improve the maintainability and auditability of the codebase.

1. SIP-15: [Liquidations.sol#L243](https://github.com/Synthetixio/synthetix/blob/4e1203b2e6f84942d0019ad9166999e3742bd85d/contracts/Liquidations.sol#L243), [Liquidations.sol#L268](https://github.com/Synthetixio/synthetix/blob/4e1203b2e6f84942d0019ad9166999e3742bd85d/contracts/Liquidations.sol#L268) `Liqudation` → `Liquidation`

##### Update

1. Implemented in [6a266c2](https://github.com/Synthetixio/synthetix/pull/536/commits/6a266c21e0a9527c6b964f01f5b5cc91a9a403d8).

#### Refactoring Suggestions
It is recommended that certain portions of the code be refactored to improve readability and consistency, as indicated below.  

1. SIP-15: `Liquidations.burnSynthsForLiquidation(...)` should be renamed to `Liquidations._burnSynthsForLiquidation(...)` to follow the underscore notation of internal functions.
2. SIP-56: `setExchangeFeeRateForSynths` uses `onlyOwner` rather than `optionalProxy_onlyOwner`, which was used by the previous version of the function. The reasons for this change are unclear.
3. SIP-56: `Exchanger._getAmoutsForExchange` would be better named `_getAmountsForExchangeMinusFees`, per the review comments.
4. SIP-56: The logic for exchange fees should be moved to `Exchanger` and `SynthetixEternalStorage` once this is viable, per the review comments.

##### Update
1. Implemented in [6a266c2](https://github.com/Synthetixio/synthetix/pull/536/commits/6a266c21e0a9527c6b964f01f5b5cc91a9a403d8).
2. `onlyOwner` changed back to `onlyOwner_optionalProxy` in [3ae5e29](https://github.com/Synthetixio/synthetix/pull/526/commits/3ae5e29dd6b839445986376c880bd359e0accc37).
3. Implemented in [3ae5e29](https://github.com/Synthetixio/synthetix/pull/526/commits/3ae5e29dd6b839445986376c880bd359e0accc37)
4. Synthetix responded that they would make this change in SIP-148 for `SynthetixEternalStorage` in a later release.

#### Unused Constant
*SIP-15: [Liquidations.sol#L29](https://github.com/Synthetixio/synthetix/blob/4e1203b2e6f84942d0019ad9166999e3742bd85d/contracts/Liquidations.sol#L29)*

The `sUSD` constant variable was defined, but was unused. It is recommended that it be removed.

##### Update
Implemented in [79b9862](https://github.com/Synthetixio/synthetix/pull/536/commits/79b9862e282a197720fa964ce6a12d7092f2bd17).

#### Implement `MIN_LIQUIDATION_RATIO`

A `MIN_LIQUIDATION_RATIO` should be implemented, i.e. the maximum amount of collateral that could be required to avoid liquidation, both from a usability and validation perspective. For example, it would be excessive to require a user to maintain a collateralisation ratio above the issuance ratio.

##### Update
Implemented in [3b3c85e](https://github.com/Synthetixio/synthetix/pull/536/commits/3b3c85e63742920fcdf37bb80ea7395517c8a05f).

Secure your system.
Request a service
Start Now