2022-04-11 12:02:58 +00:00
|
|
|
//SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.7.0;
|
|
|
|
|
|
|
|
import { TokenInterface } from "../../common/interfaces.sol";
|
|
|
|
import { DSMath } from "../../common/math.sol";
|
|
|
|
import { Basic } from "../../common/basic.sol";
|
|
|
|
import "./interface.sol";
|
|
|
|
|
|
|
|
contract Helpers is DSMath, Basic {
|
|
|
|
/**
|
2022-04-16 17:24:24 +00:00
|
|
|
* @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)
|
|
|
|
* @param targetChainId The Id of the destination chain.(For MAINNET : 1)
|
|
|
|
* @param router The address of hop router.
|
2022-04-11 12:02:58 +00:00
|
|
|
* @param recipient The address to recieve the token on destination chain.
|
|
|
|
* @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost).
|
|
|
|
* @param bonderFee The fee to be recieved by bonder at destination chain.
|
2022-04-16 17:24:24 +00:00
|
|
|
* @param sourceAmountOutMin minimum amount of token out for swap on source chain.
|
|
|
|
* @param sourceDeadline The deadline for the source chain transaction (Recommended - Date.now() + 604800 (1 week))
|
|
|
|
* @param destinationAmountOutMin minimum amount of token out for bridge on target chain, zero for L1 bridging
|
|
|
|
* @param destinationDeadline The deadline for the target chain transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging
|
2022-04-11 12:02:58 +00:00
|
|
|
*/
|
|
|
|
struct BridgeParams {
|
|
|
|
address token;
|
2022-04-16 17:24:24 +00:00
|
|
|
address router;
|
2022-04-11 12:02:58 +00:00
|
|
|
address recipient;
|
2022-04-16 17:24:24 +00:00
|
|
|
uint256 targetChainId;
|
2022-04-11 12:02:58 +00:00
|
|
|
uint256 amount;
|
|
|
|
uint256 bonderFee;
|
2022-04-16 17:24:24 +00:00
|
|
|
uint256 sourceAmountOutMin;
|
|
|
|
uint256 sourceDeadline;
|
2022-04-11 12:02:58 +00:00
|
|
|
uint256 destinationAmountOutMin;
|
|
|
|
uint256 destinationDeadline;
|
|
|
|
}
|
|
|
|
|
2022-05-11 19:33:14 +00:00
|
|
|
function _swapAndSend(BridgeParams memory params, bool isEth) internal {
|
2022-04-16 17:24:24 +00:00
|
|
|
IHopRouter router = IHopRouter(params.router);
|
2022-04-11 12:02:58 +00:00
|
|
|
|
2022-05-11 19:33:14 +00:00
|
|
|
uint256 nativeTokenAmt = isEth ? params.amount : 0;
|
|
|
|
if (!isEth) {
|
|
|
|
TokenInterface tokenContract = TokenInterface(params.token);
|
|
|
|
approve(tokenContract, params.router, params.amount);
|
|
|
|
}
|
2022-04-11 12:02:58 +00:00
|
|
|
|
2022-05-11 19:33:14 +00:00
|
|
|
router.swapAndSend{ value: nativeTokenAmt }(
|
2022-04-16 17:24:24 +00:00
|
|
|
params.targetChainId,
|
2022-04-11 12:02:58 +00:00
|
|
|
params.recipient,
|
|
|
|
params.amount,
|
|
|
|
params.bonderFee,
|
2022-04-16 17:24:24 +00:00
|
|
|
params.sourceAmountOutMin,
|
|
|
|
params.sourceDeadline,
|
2022-04-11 12:02:58 +00:00
|
|
|
params.destinationAmountOutMin,
|
|
|
|
params.destinationDeadline
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|