MultiSwap Gas Optimisation & Improvements #45
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theswapHelperForOneInch
function. TheswapHelperForOneInch
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
smart contract has been removed fromFiberRouter
.funcSelector
is passed to the swap or withdrawal functions.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 usingremoveLiquidityByManager
, the existing implementation withsafeTransfer
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 theLiquidityManagerRole
contract and how the inventory was updated in theTokenReceivable
contract. The absence of proper synchronization led to negative balances during subsequent calls toTokenReceivable.sync
.Resolution:
To address this, we introduced the
addLiquidityByManager
function, which utilizes theTokenReceivable.sendToken
method for secure token transfers. This ensures consistency in token handling and updates the inventory using theTokenReceivable.sync
method.Similarly, in
removeLiquidityByManager
, the direct use ofTokenReceivable.sendToken
was introduced, providing a unified approach for managing token transfers and updating the inventory.Additional Context:
safeTransfer
tosendToken
inremoveLiquidityByManager
improves consistency in token handling and leverages the functionality provided by theTokenReceivable
library.addLiquidityByManager
function enriches the contract's feature set, allowing liquidity managers to contribute assets in a controlled manner.Changes Made:
Introduction of
addLiquidityByManager
Function:addLiquidityByManager
, empowering designated liquidity managers to add a specified amount of liquidity for a given token.TokenReceivable.sync
method.Enhancements in
removeLiquidityByManager
:removeLiquidityByManager
function to utilize theTokenReceivable.sendToken
method directly, replacing the previous use ofsafeTransfer
.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: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.
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.
_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.