Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiSwap Gas Optimisation & Improvements #45

Merged
merged 11 commits into from Apr 4, 2024

Conversation

salmanferrum
Copy link
Contributor

@salmanferrum salmanferrum commented Mar 4, 2024

Pull Request Description

Changes Proposed

This PR introduces a significant improvement to the gas efficiency of our system. The 1inchDecoder smart contract has been moved to the backend, and its functionality to decode the name of the function to be called on 1inch is now integrated into the swapHelperForOneInch function. The swapHelperForOneInch function now takes an enum parameter, representing the function to be called on 1inch.

1inch Decoder Context

The primary motivation behind this change is to enhance gas efficiency by eliminating the need for a separate smart contract (1inchDecoder) on the blockchain. By moving the decoding logic to the backend, we can reduce gas costs and improve overall system performance.

1inch Changes Overview

  • 1inchDecoder Removal: The 1inchDecoder smart contract has been removed from FiberRouter.
  • Function Name Decoding: The decoding logic for 1inch function names is now handled in the backend and a parameter funcSelector is passed to the swap or withdrawal functions.
  • Enum Parameter: The swapHelperForOneInch function now takes an enum parameter to identify the specific function to be called on 1inch.

Change Regarding LiquidityManagerRole / TokenReceivable

Background:

The changes were prompted by an issue identified in the TokenReceivable contract. Specifically, when attempting to remove liquidity using removeLiquidityByManager, the existing implementation with safeTransfer caused discrepancies in the token inventory and causing the transaction to fail.

Root Cause:

The discrepancy arose from a mismatch between how tokens were removed (using safeTransfer) in the LiquidityManagerRole contract and how the inventory was updated in the TokenReceivable contract. The absence of proper synchronization led to negative balances during subsequent calls to TokenReceivable.sync.

Resolution:

To address this, we introduced the addLiquidityByManager function, which utilizes the TokenReceivable.sendToken method for secure token transfers. This ensures consistency in token handling and updates the inventory using the TokenReceivable.sync method.

Similarly, in removeLiquidityByManager, the direct use of TokenReceivable.sendToken was introduced, providing a unified approach for managing token transfers and updating the inventory.

Additional Context:

  • The adjustment from safeTransfer to sendToken in removeLiquidityByManager improves consistency in token handling and leverages the functionality provided by the TokenReceivable library.
  • The addition of the addLiquidityByManager function enriches the contract's feature set, allowing liquidity managers to contribute assets in a controlled manner.

Changes Made:

  1. Introduction of addLiquidityByManager Function:

    • Implemented a new function, addLiquidityByManager, empowering designated liquidity managers to add a specified amount of liquidity for a given token.
    • This addition enhances the contract's flexibility by allowing controlled liquidity addition while ensuring secure token transfers to the FundManager contract. The inventory is also updated using the TokenReceivable.sync method.
  2. Enhancements in removeLiquidityByManager:

    • Modified the removeLiquidityByManager function to utilize the TokenReceivable.sendToken method directly, replacing the previous use of safeTransfer.
    • Ensured the function continues to validate a positive amount and a valid token address before proceeding with liquidity removal.

Same Network Swap Feature

This PR introduces new functionalities to the fiberRouter contract to enable same network token swaps on MultiSwap. The following key functions have been added:

  1. swapOnSameNetwork: This external function allows users to perform token swaps between two tokens on the same network using 1inch exchange. It takes parameters such as input amount, output amount, source token, target token, target address, and 1inch swap data.

  2. swapOnSameNetworkETH: Another external function introduced to enable swaps between native currency and a desired token on the same network. Users can swap native currency (ETH) to a target token using 1inch exchange. Parameters include output amount, target token, target address, and 1inch swap data.

  3. _swapOnSameNetwork: An internal function called within the above two functions to execute the token swaps using 1inch helper functions in fiberRouter.

These functions enhance the usability and flexibility of the fiberRouter contract by providing seamless token swapping capabilities within the same network using the powerful 1inch exchange.

@taha-abbasi taha-abbasi removed their request for review April 4, 2024 18:29
@taha-abbasi taha-abbasi merged commit 3c05db2 into ferrumnet:develop Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants