Skip to content

Commit

Permalink
Merge branch 'release/1.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
taha-abbasi committed Feb 28, 2024
2 parents 35685ab + 7462375 commit 1d5cb04
Show file tree
Hide file tree
Showing 14 changed files with 719 additions and 183 deletions.
20 changes: 19 additions & 1 deletion contracts/common/oneInch/IOneInchSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ interface IOneInchSwap {
uint256 flags;
}

struct OrderRFQ {
uint256 info; // lowest 64 bits is the order id, next 64 bits is the expiration timestamp
address makerAsset; // targetToken
address takerAsset; // foundryToken
address maker;
address allowedSender; // equals to Zero address on public orders
uint256 makingAmount;
uint256 takingAmount; // destinationAmountIn / foundryTokenAmountIn
}

struct Order {
uint256 salt;
address makerAsset; // targetToken
Expand All @@ -27,7 +37,15 @@ interface IOneInchSwap {
}

// Define external functions that will be available for interaction


// fillOrderRFQTo
function fillOrderRFQTo(
OrderRFQ calldata order,
bytes calldata signature,
uint256 flagsAndAmount,
address target // receiverAddress
) external payable returns (uint256 filledMakingAmount, uint256 filledTakingAmount, bytes32 orderHash);

// fillOrderTo function
function fillOrderTo(
Order calldata order_,
Expand Down
38 changes: 37 additions & 1 deletion contracts/common/oneInch/OneInchDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ library OneInchDecoder {
uint256 offsets;
bytes interactions; // concat(makerAssetData, takerAssetData, getMakingAmount, getTakingAmount, predicate, permit, preIntercation, postInteraction)
}
struct OrderRFQ {
uint256 info; // lowest 64 bits is the order id, next 64 bits is the expiration timestamp
address makerAsset; // targetToken
address takerAsset; // foundryToken
address maker;
address allowedSender; // equals to Zero address on public orders
uint256 makingAmount;
uint256 takingAmount;
}

// Define the function signatures
bytes4 public constant selectorUnoswap =
Expand All @@ -43,7 +52,12 @@ library OneInchDecoder {
"fillOrderTo((uint256,address,address,address,address,address,uint256,uint256,uint256,bytes),bytes,bytes,uint256,uint256,uint256,address)"
)
);

bytes4 public constant selectorFillOrderRFQTo =
bytes4(
keccak256(
"fillOrderRFQTo((uint256,address,address,address,address,uint256,uint256),bytes,uint256,address)"
)
);

function decodeUnoswap(bytes memory data)
public
Expand Down Expand Up @@ -155,6 +169,28 @@ library OneInchDecoder {
);
}

function decodeFillOrderRFQTo(bytes memory data)
public
pure
returns (
OrderRFQ memory order,
bytes memory signature,
uint256 flagsAndAmount,
address target // receiverAddress
)
{
require(data.length >= 4, "Data too short");

// Skip the first 4 bytes (function signature)
bytes memory params = slice(data, 4, data.length - 4);

// Decode the parameters
(order, signature, flagsAndAmount, target) = abi.decode(
params,
(OrderRFQ, bytes, uint256, address)
);
}

// Helper function to slice bytes array
function slice(
bytes memory data,
Expand Down
100 changes: 80 additions & 20 deletions contracts/multiswap-contracts/FiberRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
@notice Sets the fund manager contract.
@dev Sets the fund manager contract.
@param _pool The fund manager
*/
function setPool(address _pool) external onlyOwner {
Expand All @@ -115,7 +115,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
@notice Sets the gas wallet address.
@dev Sets the gas wallet address.
@param _gasWallet The wallet which pays for the funds on withdrawal
*/
function setGasWallet(address payable _gasWallet) external onlyOwner {
Expand All @@ -127,7 +127,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
@notice Sets the 1inch Aggregator Router address
@dev Sets the 1inch Aggregator Router address
@param _newRouterAddress The new Router Address of oneInch
*/
function setOneInchAggregatorRouter(address _newRouterAddress)
Expand All @@ -142,7 +142,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Initiate an x-chain swap.
* @dev Initiate an x-chain swap.
* @param token The token to be swapped
* @param amount The amount to be swapped
* @param targetNetwork The target network for the swap
Expand Down Expand Up @@ -195,7 +195,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
*@notice Initiate an x-chain swap.
*@dev Initiate an x-chain swap.
*@param token The source token to be swaped
*@param amount The source amount
*@param targetNetwork The chain ID for the target network
Expand Down Expand Up @@ -252,7 +252,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Do a local swap and generate a cross-chain swap
* @dev Do a local swap and generate a cross-chain swap
* @param amountIn The input amount
* @param amountOut Equivalent to amountOut on oneInch
* @param crossTargetNetwork The target network for the swap
Expand Down Expand Up @@ -331,7 +331,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Swap and cross to oneInch in native currency
* @dev Swap and cross to oneInch in native currency
* @param amountOut Equivalent to amountOut on oneInch
* @param crossTargetNetwork The target network for the swap
* @param crossTargetToken The target token for the cross-chain swap
Expand Down Expand Up @@ -393,12 +393,14 @@ contract FiberRouter is Ownable, TokenReceivable {


/**
*@notice Withdraws funds based on a multisig
*@param token The token to withdraw
*@param payee Address for where to send the tokens to
*@param amount The amount
*@param salt The salt for unique tx
*@param multiSignature The multisig validator signature
* @dev Initiates a signed token withdrawal, exclusive to the router.
* @notice Ensure valid parameters and router setup.
* @param token The token to withdraw
* @param payee Address for where to send the tokens to
* @param amount The amount
* @param salt The salt for unique tx
* @param expiry The expiration time for the signature
* @param multiSignature The multisig validator signature
*/
function withdrawSigned(
address token,
Expand Down Expand Up @@ -426,7 +428,8 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Withdraws funds and swaps to a new token
* @dev Initiates a signed OneInch token withdrawal, exclusive to the router.
* @notice Ensure valid parameters and router setup.
* @param to The address to withdraw to
* @param amountIn The amount to be swapped in
* @param amountOut The expected amount out in the OneInch swap
Expand Down Expand Up @@ -496,7 +499,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Helper function for executing token swaps using OneInch aggregator
* @dev Helper function for executing token swaps using OneInch aggregator
* @param to The recipient address to receive the swapped tokens
* @param srcToken The source token to be swapped (input token)
* @param amountIn The amount of input tokens to be swapped
Expand Down Expand Up @@ -527,13 +530,15 @@ contract FiberRouter is Ownable, TokenReceivable {
returnAmount = handleSwap(to, srcToken, amountIn, amountOut, oneInchData);
} else if (receivedSelector == OneInchDecoder.selectorFillOrderTo) {
returnAmount = handleFillOrderTo(to, srcToken, amountIn, oneInchData);
} else if (receivedSelector == OneInchDecoder.selectorFillOrderRFQTo) {
returnAmount = handleFillOrderRFQTo(to, srcToken, amountIn, oneInchData);
} else {
revert("FR: incorrect oneInchData");
}
}

/**
* @notice Handles the execution of a token swap operation using UnoSwap
* @dev Handles the execution of a token swap operation using UnoSwap
* @param to The recipient address to receive the swapped tokens
* @param fromToken The token to be swapped from (input token)
* @param amountIn The amount of input tokens to be swapped
Expand Down Expand Up @@ -577,7 +582,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Handles the execution of a token swap operation involving 1inch aggregator
* @dev Handles the execution of a token swap operation involving 1inch aggregator
* @param to The recipient address to receive the swapped tokens
* @param amountIn The amount of input tokens to be swapped
* @param amountOut The expected amount of output tokens after the swap
Expand Down Expand Up @@ -615,7 +620,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Handles the execution of a token swap operation, potentially involving 1inch aggregator
* @dev Handles the execution of a token swap operation, potentially involving 1inch aggregator
* @param to The recipient address to receive the swapped tokens
* @param fromToken The address of the input token for the swap
* @param amountIn The amount of input tokens to be swapped
Expand Down Expand Up @@ -681,7 +686,7 @@ contract FiberRouter is Ownable, TokenReceivable {
}

/**
* @notice Handles the execution of the `fillOrderTo` operation, involving 1inch aggregator
* @dev Handles the execution of the `fillOrderTo` operation, involving 1inch aggregator
* @param to The recipient address to receive the swapped tokens
* @param fromToken The address of the input token for the swap (foundryToken or takerAsset)
* @param amountIn The amount of input tokens to be swapped
Expand Down Expand Up @@ -746,9 +751,64 @@ contract FiberRouter is Ownable, TokenReceivable {
);
}

/**
* @dev Handles the execution of the `fillOrderRFQTo` operation, involving 1inch aggregator
* @param to The recipient address to receive the swapped tokens
* @param fromToken The address of the input token for the swap (foundryToken or takerAsset)
* @param amountIn The amount of input tokens to be swapped
* @param oneInchData The data containing information for the 1inch swap
* @return returnAmount The amount of tokens received after the swap and transaction execution
*/
function handleFillOrderRFQTo(
address payable to,
address fromToken, // foundryToken // takerAsset
uint256 amountIn,
bytes memory oneInchData
) internal returns (uint256 returnAmount) {
// Decoding oneInchData to get the required parameters
(
OneInchDecoder.OrderRFQ memory order,
bytes memory signature,
uint256 flagsAndAmount,
address target // receiverAddress
) = OneInchDecoder.decodeFillOrderRFQTo(oneInchData);

// Manually create a new OrderRFQ for IOneInchSwap
IOneInchSwap.OrderRFQ memory oneInchOrderRFQ = IOneInchSwap.OrderRFQ({
info: order.info,
makerAsset: order.makerAsset,
takerAsset: order.takerAsset,
maker: order.maker,
allowedSender: order.allowedSender,
makingAmount: order.makingAmount,
takingAmount: order.takingAmount
});

// Perform additional checks and validations if needed
require(to == target, "FR: recipient address bad oneInch Data");
require(fromToken == order.takerAsset, "FR: takerAsset bad oneInch Data");
require(amountIn == order.takingAmount, "FR: inputAmount bad oneInch Data ");
require(oneInchData.length >= 4, "Data too short for valid call");

// Performing the swap
( returnAmount, , ) = IOneInchSwap(oneInchAggregatorRouter).fillOrderRFQTo(
oneInchOrderRFQ,
signature,
flagsAndAmount,
target
);

emit SwapHandled(
oneInchAggregatorRouter,
to,
fromToken,
amountIn,
returnAmount // should be returned
);
}

/**
* @notice Performs a token swap and cross-network transaction using the 1inch Aggregator
* @dev Performs a token swap and cross-network transaction using the 1inch Aggregator
* @param amountIn The amount of input tokens to be swapped
* @param amountOut The expected amount of output tokens after the swap on 1inch
* @param crossTargetNetwork The network identifier for the cross-network transaction
Expand Down
Loading

0 comments on commit 1d5cb04

Please sign in to comment.