Kwenta Token Smart Contract Audit

# 1. Introduction

iosiro was commissioned by Kwenta to conduct a smart contract audit of the Kwenta Token System. The audit was performed over seven days by two auditors between 19 and 22 September. A review of remediations to the findings made was conducted by one auditor on 25 October 2022. Previous iterations of the system were audited included:

* Two auditors between 13 and 28 June 2022, using 28 resource days.
* Two auditors between 7 and 18 February 2022, using 28 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 the 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 an audit performed by iosiro on the Kwenta Token System. This audit builds upon the findings and recommendations of two previous audits conducted on the system by iosiro, which proposed high-level architectural changes that were adopted.

#### Audit findings

iosiro made several design comments relating to best practices and code clarity, as well as suggestions for improving the user-friendliness of escrow staking and trading reward claims. Overall, the code was of high quality and represented a significant improvement over previous iterations.

#### User concerns

System users should note that new Kwenta tokens can be minted by the `SupplySchedule` contract, the address of which can be updated by the `Kwenta` contract owner.

<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 Smart contracts

* **Project name:** Kwenta Token System
* **Audit commit:** [7e2d678](https://github.com/Kwenta/token/tree/7e2d67897cbc5c5fbba7afd47bb86175d05dda96)
* **Review commits:** [4dccd29](https://github.com/Kwenta/token/commit/4dccd298d6330225262cc6b62a502f4e841ed126), [fbcbeae](https://github.com/Kwenta/token/commit/fbcbeae1f29fd7d5c94428ba09c45c3dc49a237a)
* **Files:** ControlL2MerkleDistributor.sol, Kwenta.sol, MerkleDistributor.sol, MultipleMerkleDistributor.sol, RewardEscrow.sol, StakingRewards.sol, SupplySchedule.sol, vKwenta.sol, vKwentaRedeemer.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. The coverage report of the provided tests as on the final day of the audit is given below.

File                              |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------------------------------|----------|----------|----------|----------|----------------|
contracts/                       |    97.03 |    84.38 |     98.8 |    97.12 |                |
 ControlL2MerkleDistributor.sol  |      100 |      100 |      100 |      100 |                |
 Kwenta.sol                      |      100 |       75 |      100 |      100 |                |
 MerkleDistributor.sol           |      100 |    71.43 |      100 |      100 |                |
 MultipleMerkleDistributor.sol   |      100 |      100 |      100 |      100 |                |
 RewardEscrow.sol                |    90.82 |    81.82 |    95.65 |       91 | 133, 138, 169, 180-190 |
 StakingRewards.sol              |      100 |    96.43 |      100 |      100 |                |
 SupplySchedule.sol              |      100 |    80.77 |      100 |      100 |                |
 vKwenta.sol                     |      100 |      100 |      100 |      100 |                |
 vKwentaRedeemer.sol             |      100 |     87.5 |      100 |      100 |                |
contracts/interfaces/            |      100 |      100 |      100 |      100 |                |
 IControlL2MerkleDistributor.sol |      100 |      100 |      100 |      100 |                |
 IERC20.sol                      |      100 |      100 |      100 |      100 |                |
 IERC20Metadata.sol              |      100 |      100 |      100 |      100 |                |
 IKwenta.sol                     |      100 |      100 |      100 |      100 |                |
 IMerkleDistributor.sol          |      100 |      100 |      100 |      100 |                |
 IMultipleMerkleDistributor.sol  |      100 |      100 |      100 |      100 |                |
 IRewardEscrow.sol               |      100 |      100 |      100 |      100 |                |
 IStakingRewards.sol             |      100 |      100 |      100 |      100 |                |
 ISupplySchedule.sol             |      100 |      100 |      100 |      100 |                |
 IvKwentaRedeemer.sol            |      100 |      100 |      100 |      100 |                |
contracts/libraries/             |    52.94 |       50 |     37.5 |    52.94 |                |
 Math.sol                        |      100 |      100 |      100 |      100 |                |
 SafeDecimalMath.sol             |       20 |        0 |    28.57 |       20 | 28, 29, 59-99 |
contracts/utils/                 |    86.44 |    61.54 |       80 |    85.94 |                |
 Context.sol                     |       50 |      100 |       50 |       50 |             21 |
 ERC20.sol                       |    84.78 |       60 |    77.78 |       84 | 86, 87, 120, 121, 177-203 |
 Owned.sol                       |      100 |    66.67 |      100 |      100 |                |
All files                         |     93.4 |    79.75 |    90.52 |     93.4 |                |

### 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 implementation of the system was evaluated according to the specification in the repository's `README.md` at commit hash [7e2d678](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/README.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 audit.

## 5.2 Medium risk

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

## 5.3 Low risk

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

## 5.4 Informational

### 5.4.1 Automatic escrow skating
*StakingRewards.sol, RewardEscrow.sol*

Users are expected to claim their rewards weekly and subsequently restake any escrowed rewards. This requires the user to perform separate transactions to maintain their staking position and achieve the compounding of their rewards. Expanding `RewardEscrow._appendVestingEntry(...)` to accept a boolean argument specifying whether the escrow amount should be staked would allow `StakingRewards` to automatically reinvest any rewards, reducing the number of calls that users are expected to make.

#### Response from Kwenta

No change. Users have a choice after they claim their rewards whether they would like to vest their rewards (at a cost) or restake.

### 5.4.2 Cumulative airdrop
*MultipleMerkleDistributor.sol*

An alternative airdrop pattern to the one used in `MultipleMerkleDrop` is [1inch's cumulative merkle drop](https://github.com/1inch/merkle-distribution). The cumulative pattern removes the need to iterate through different epochs and is thus more gas efficient. Furthermore, it reduces the dependence on admins to correctly calculate each rewards per period, as total rewards are always calculated from the start block until the end block instead of using a shifting window.

#### Response from Kwenta

No change for now. We could implement something like this in the future, replacing the current `MultipleMerkleDistributor.sol`.

### 5.4.3 Centralization risk
*Kwenta.sol*

The owner of `Kwenta` can arbitrarily mint new tokens, as they can change the `SupplySchedule` contract, which is permitted to mint new tokens at any time.

#### Response from Kwenta

No change. This was created in the rare event that we need to upgrade (replace) `SupplySchedule.sol`, `StakingRewards.sol`, or `RewardEscrow.sol`.

### 5.4.4 Defense in depth

The following recommendations are made as additional defense in-depth measures:

1. [StakingRewards#L415](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/StakingRewards.sol#L415): The original Synthetix implementation of `StakingRewards` enforced an [overflow check](https://github.com/Synthetixio/synthetix/blob/26cf098d2c603ef2ddcd7bc3a81a9a3bbff48e90/contracts/StakingRewards.sol#L127) to ensure that the `rewardRate` was within proper bounds. While this overflow is unlikely to occur in the context of the Kwenta system, the check remains a recommended defence-in-depth measure and should be included.
2. [MultipleMerkleDistributor#L87](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/MultipleMerkleDistributor.sol#L87): `claim(...)` should verify that the `epoch` is less than `distributionEpoch` as a sanity check.

#### Response from Kwenta

1. No change. Determined safe because `notifyRewardAmount` is strictly controlled by SupplySchedule. It will need to be considered if SupplySchedule ever changes.
2. No change. There are tests for this to verify that the contracts will revert.

### 5.4.5 Code clarity

The following recommendations are provided to improve the readability and functionality of the codebase:

1. [MerkleDistributor.sol#L31](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/MerkleDistributor.sol#L31): `crossDomainMessengerAddr` is a constant and thus should be renamed to `CROSS_DOMAIN_MESSENGER_ADDR`.
2. [RewardEscrow.sol#L217](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/RewardEscrow.sol#L217): The early vesting fee percentage could be stored in an immutable to allow for setting the value during deployment.


#### Response from Kwenta

1. No change. The contract is no longer being used and will be removed from the repository.
2. No change. The vesting fee isn't expected to change before launch.

##  5.5 Closed

### 5.5.1 Code clarity (informational)

1. [RewardEscrow.sol#L59](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/RewardEscrow.sol#L59): The event parameter `rewardEscrow` should be renamed to `stakingRewards`.
2. [RewardEscrow.sol#L267](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/RewardEscrow.sol#L267): The double-cast `IKwenta(address(kwenta))` could be simplified to `kwenta`.

#### Update

1. Implemented in [4dccd29](https://github.com/Kwenta/token/commit/4dccd298d6330225262cc6b62a502f4e841ed126).
2. Implemented in [4dccd29](https://github.com/Kwenta/token/commit/4dccd298d6330225262cc6b62a502f4e841ed126).

### 5.5.2 Comment clarity (informational)

[StakingRewards.sol#L335](https://github.com/Kwenta/token/blob/7e2d67897cbc5c5fbba7afd47bb86175d05dda96/contracts/StakingRewards.sol#L335): The comment reads, "transfer token from this contract to the caller". This is inaccurate, as tokens are transferred to `rewardEscrow`, and a vesting entry for the caller is created. It could instead read "transfer token from this contract to RewardEscrow for vesting" or something similar.

#### Update

Implemented in [fbcbeae](https://github.com/Kwenta/token/commit/fbcbeae1f29fd7d5c94428ba09c45c3dc49a237a).

Secure your system.
Request a service
Start Now