# On-Chain Slippage Calculations Vulnerability

{% hint style="info" %}
[**Book an audit with Zokyo**](https://www.zokyo.io/)
{% endhint %}

#### **Introduction**

In the rapidly evolving world of Decentralized Finance (DeFi), both security and efficiency are of paramount importance. Slippage checks, integral to ensuring trade price accuracy, have become a focal point for developers. While we've previously examined the challenges of both overly relaxed and overly strict slippage checks, a third dimension arises: where these checks are computed - on-chain or off-chain. As highlighted by the warden, calculating slippage checks off-chain is essential to sidestep vulnerabilities.

#### **Why Off-Chain?**

1. **Avoiding Front-running Attacks in the Mempool:**

   The mempool is a holding area for transactions awaiting confirmation. When a user sends a transaction with a slippage check calculated on-chain, it first lands in the mempool before being added to the blockchain. In this interim period, malicious actors can view these transactions and can act on this information (e.g., submitting a competing transaction with a higher gas fee) to their advantage. This behavior is called front-running, and it can lead to significant financial losses for the original transaction sender.

   Calculating slippage checks off-chain mitigates this risk, as the specifics of the slippage range aren't immediately visible or exploitable in the mempool.
2. **Efficiency and Cost Saving:**

   Off-chain calculations can be more efficient and cost-effective. On-chain computations require gas (transaction fees), and the more complex the computation, the higher the gas cost. By shifting the slippage calculation off-chain, you can reduce the computational burden and associated costs on the main chain.

#### **Hardcoded Slippage Checks and Griefing Attacks**

Hardcoded slippage checks, where specific limits are pre-defined in the contract, can lead to vulnerabilities.

**The Griefing Attack Vector:**

Malicious actors can exploit these hardcoded checks to cause disruptions. For instance, by deliberately sending tokens to a contract in such a way as to disrupt balance ratios, they can force a transaction to revert due to the overly strict slippage check. While these attackers might not derive a direct financial gain from such actions, they can cause considerable disruptions, effectively "griefing" the system and its users.

**Mitigation:**

Dynamic slippage checks, ideally determined off-chain, offer a more robust approach. They can adapt to market conditions and be set by users based on their risk tolerance. By avoiding hardcoding specific slippage parameters, DeFi protocols can ensure both flexibility and security.

#### **Conclusion**

DeFi's promise hinges on the interplay of security, efficiency, and user-friendliness. As the space matures, best practices like off-chain slippage calculations will be crucial in ensuring that DeFi platforms protect their users from both financial losses and malicious attacks. By understanding and integrating these principles, DeFi can continue its trajectory towards redefining the financial landscape.

Example Issue:&#x20;

{% embed url="<https://github.com/code-423n4/2021-11-malt-findings/issues/257>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zokyo-auditing-tutorials.gitbook.io/zokyo-tutorials/tutorial-17-dexs-decentralized-exchanges/common-vulnerabilities-in-dex-code/on-chain-slippage-calculations-vulnerability.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
