1inch Network Fixed Rate Swap Smart Contract Audit

# 1. Introduction
iosiro was commissioned by [1inch Network](https://1inch.io/) to conduct an audit of their Fixed Rate Swap smart contracts. The audit was performed by 1 auditor between 3 and 5 August 2021, consuming a total of 3 person days. A review of changes made to address findings was conducted on 23 August 2021.

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 better understand the risk exposure of the smart contracts, and as a guide to improving the security posture of the smart contracts by remediating issues 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:

* Identify potential security flaws.
* Ensure that the smart contracts functioned according to the documentation provided.

Assessing the 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. 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 an audit performed by iosiro on 1inch Network's Fixed Rate Swap smart contracts.

Several informational issues were identified during the audit, all of which were noted as acceptable risks by the development team. The code was found to be of a high standard and made use of best practices.

<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 1inch smart contracts
**Etherscan verified source code:** [0x40bbdE0eC6F177C4A67360d0f0969Cfc464b0bB4](https://etherscan.io/address/0x40bbdE0eC6F177C4A67360d0f0969Cfc464b0bB4#code)<br/>
**Final review commit:** [65b144a](https://github.com/1inch/fixed-rate-swap/commit/65b144ab9c5c55dc451a6cf964c6a90c96038eae)

## 3.2  Methodology

A variety of techniques were used while conducting 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. 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 analysed to remove false positive results. True positive results would be indicated in this report. Static analysis tools commonly used include Slither, MythX, as well as Securify. Furthermore, the Remix IDE, compilation output, and linters are also 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 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 Fixed rate swap contract
The fixed rate swap contract is an automated market maker that allows swapping tokens with a 1:1 rate and a variable fee. The system is specifically designed to allow for same asset type swaps, e.g. USDC and USDT.    

### Deposits
Only the owner of the contract is able to deposit liquidity into the system. When depositing liquidity, a proportionate amount of liquidity provider (LP) tokens are issued based on the relative change in liquidity.

### Withdrawal
LP tokens can be burned in order to retrieve a proportionate amount of the underlying assets held by the system.

### Fee calculation
Fees are taken on each swap and distributed between LPs. The fee amount is calculated as follows:

`getReturn(x0, x1) = (0.9999 * (x1 - x0) + 3.3827123349983306 * ((x0 - 0.4568073509746632) ** 18 - (x1 - 0.4568073509746632) ** 18)) / (x1 - x0)` where `x0` and `x1` are the proportion of the `from` asset of the total balance before and after the swap, respectively.

### Swap types
* `swap0To1(uint256 inputAmount)` - swaps from asset 0 to asset 1 and sends the swapped assets to `msg.sender`
* `swap0To1For(uint256 inputAmount, address to)` -  swaps from asset 0 to asset 1 and sends the swapped assets to `to`
* `swap1To0(uint256 inputAmount)` - swaps from asset 1 to asset 0 and sends the swapped assets to `msg.sender`
* `swap1To0For(uint256 inputAmount, address to)` -  swaps from asset 1 to asset 0 and sends the swapped assets to `to`

### Helper functions
A `getReturn(IERC20 tokenFrom, IERC20 tokenTo, uint256 inputAmount)` function is exposed to allow users to determine the amount of tokens they will receive for a given swap.

<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 Token pairs must have the same number of decimals

No logic was included in the contract to account for token pairs with a differing number of decimals. As a result, if the contract were to be re-deployed with different tokens, special care should be taken to ensure that the tokens used in the pair have the same number of decimals.

### 5.4.2 Token transfer fees could lead to incorrect system states

The mainnet contract in scope for the audit used the USDC-USDT pair. USDT has an optional transfer fee that can optionally be enabled by Tether. If this fee were to be enabled, it would lead to inconsistencies, specifically when exchanging from USDT to USDC, as the transfer fee would not be accounted for in the exchanged amount.

The likelihood of the fee being enabled is fairly low given the far reaching effect it would have on the ecosystem. Furthermore, the assets could be migrated across to a new pool in the event that this were to happen.

### 5.4.3 Potential overflow

The `_powerHelper(uint256 x)` function made use of the `unchecked` keyword to reduce the gas expenditure for the calculation performed. As a result, it would theoretically be possible for the function to overflow if a sufficiently large value for `x` was used. Given the unlikely nature of this occurring, as well as the validation used to ensure that the maximum return ratio was 1-to-1, the risk is fairly negligible.

## 5.5 Closed

### 5.5.1 Missing validation for `to` value

Additional validation can be added to the `withdrawFor(uint256 amount, address to)` function to ensure that the `to` parameter is non-zero. This would reduce the likelihood of erroneously sending liquidity to the zero address as a result of a faulty front-end.

### Update
Implemented in [65b144a](https://github.com/1inch/fixed-rate-swap/commit/65b144ab9c5c55dc451a6cf964c6a90c96038eae).

Secure your system.
Request a service
Start Now