Skip to content

Commit

Permalink
SwapOnSameNetwork & SourceNativeSwap (ferrumnet#13)
Browse files Browse the repository at this point in the history
* QP-Stargate-Fee

* few changes

* Config Script Added

* qp-ms integration

* tokenABI

* "minor change"

* SameNetworkSwap/SrcEthSwap
  • Loading branch information
raviansalman authored Sep 3, 2024
1 parent 0ec8494 commit 9a5d94d
Show file tree
Hide file tree
Showing 5 changed files with 569 additions and 10 deletions.
153 changes: 150 additions & 3 deletions contracts/FiberRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,157 @@ import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { BaseRouter } from "./BaseRouter.sol";
import { FeeDistributor } from "./FeeDistributor.sol";
import { CCIPApp } from "./CCIPApp.sol";
import { IWETH } from "./common/IWETH.sol";
import { StargateComposer } from "./StargateComposer.sol";
import { QuantumPortalApp } from "./QuantumPortalApp.sol";


contract FiberRouter is FeeDistributor, StargateComposer, QuantumPortalApp, CCIPApp {
address private constant NATIVE_CURRENCY = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address public weth;

event SwapSameNetwork(
address sourceToken,
address targetToken,
uint256 sourceAmount,
uint256 settledAmount,
address sourceAddress,
address targetAddress
);

constructor(
address pool,
address payable gasWallet,
address portal,
address ccipRouter
) QuantumPortalApp(portal) Ownable(tx.origin) CCIPApp(ccipRouter) BaseRouter(pool, gasWallet) {}
address ccipRouter,
address _weth
) QuantumPortalApp(portal) Ownable(tx.origin) CCIPApp(ccipRouter) BaseRouter(pool, gasWallet) {
require(_weth != address(0), "FR: Weth address cannot be zero");
weth = _weth;
}

//#############################################################
//###################### USER FUNCTIONS #######################
//#############################################################


function swapOnSameNetwork(
uint256 amountIn,
uint256 minAmountOut,
address fromToken,
address toToken,
address targetAddress,
address router,
bytes memory routerCalldata
) external nonReentrant {
_swapOnSameNetwork(
amountIn,
minAmountOut,
fromToken,
toToken,
targetAddress,
router,
routerCalldata
);
}

function _swapOnSameNetwork(
uint256 amountIn,
uint256 minAmountOut,
address fromToken,
address toToken,
address targetAddress,
address router,
bytes memory routerCalldata
) internal {
// Validation checks
require(fromToken != address(0), "FR: From token address cannot be zero");
require(toToken != address(0), "FR: To token address cannot be zero");
require(amountIn != 0, "FR: Amount in must be greater than zero");
require(minAmountOut != 0, "FR: Amount out must be greater than zero");
require(targetAddress != address(0), "FR: Target address cannot be zero");

// Move tokens from the user to the contract
amountIn = _moveTokens(fromToken, msg.sender, address(this), amountIn);

// Perform the token swap
uint256 amountOut = _swap(
targetAddress,
fromToken,
toToken,
amountIn,
minAmountOut,
router,
routerCalldata
);

// Emit Swap event
emit SwapSameNetwork(
fromToken,
toToken,
amountIn,
amountOut,
msg.sender,
targetAddress
);
}

function swapOnSameNetworkETH(
uint256 minAmountOut,
address toToken,
address targetAddress,
address router,
bytes memory routerCalldata
) external payable {
_swapOnSameNetworkETH(
minAmountOut,
toToken,
targetAddress,
router,
routerCalldata
);
}

function _swapOnSameNetworkETH(
uint256 minAmountOut,
address toToken,
address targetAddress,
address router,
bytes memory routerCalldata
) internal {
uint256 amountIn = msg.value;

// Validation checks
require(toToken != address(0), "FR: To token address cannot be zero");
require(amountIn != 0, "FR: Amount in must be greater than zero");
require(minAmountOut != 0, "FR: Amount out must be greater than zero");
require(targetAddress != address(0), "FR: Target address cannot be zero");
require(bytes(routerCalldata).length != 0, "FR: Calldata cannot be empty");

// Deposit ETH and get WETH
IWETH(weth).deposit{value: amountIn}();

uint256 amountOut = _swap(
targetAddress,
weth,
toToken,
amountIn,
minAmountOut,
router,
routerCalldata
);

// Emit Swap event
emit SwapSameNetwork(
NATIVE_CURRENCY,
toToken,
amountIn,
amountOut,
msg.sender,
targetAddress
);
}

function cross(
address sourceFoundryToken,
uint256 amountIn,
Expand Down Expand Up @@ -169,7 +304,19 @@ contract FiberRouter is FeeDistributor, StargateComposer, QuantumPortalApp, CCIP
bytes calldata srcRouterCalldata,
bytes memory dstData
) internal {
amountIn = _moveTokens(fromToken, msg.sender, address(this), amountIn);

// Check if the fromToken is the NATIVE Token
if (fromToken == NATIVE_CURRENCY) {
amountIn = msg.value;
// Convert ETH to WETH
IWETH(weth).deposit{value: amountIn}();
fromToken = weth; // Update fromToken to WETH address
} else {
// If not NATIVE Token, transfer ERC20 token to contract
amountIn = _moveTokens(fromToken, msg.sender, address(this), amountIn);
}

// amountIn = _moveTokens(fromToken, msg.sender, address(this), amountIn);

uint256 amountOut = _swap(
address(this),
Expand Down
Loading

0 comments on commit 9a5d94d

Please sign in to comment.