refactored code

This commit is contained in:
pradyuman-verma 2022-04-04 00:41:30 +05:30
parent 9efdf4239c
commit ba6edcd1d4
No known key found for this signature in database
GPG Key ID: E36FD6BC8923221F
3 changed files with 71 additions and 164 deletions

View File

@ -2,20 +2,7 @@
pragma solidity ^0.7.0;
contract Events {
event LogSendToL1(
address token,
uint256 chainId,
address recipient,
uint256 amount,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
uint256 destinationAmountOutMin,
uint256 destinationDeadline,
uint256 getId
);
event LogSendToL2(
event LogBridge(
address token,
uint256 chainId,
address recipient,

View File

@ -7,31 +7,44 @@ import { Basic } from "../../common/basic.sol";
import "./interface.sol";
contract Helpers is DSMath, Basic {
function _swapAndSend(
address token,
uint256 chainId,
address recipient,
uint256 amount,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
uint256 destinationAmountOutMin,
uint256 destinationDeadline
) internal {
IHopRouter router = _getRouter(token);
/**
* @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)
* @param chainId The Id of the destination chain.(For MAINNET : 1)
* @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.
* @param amountOutMin minimum amount of token out for swap
* @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week))
* @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging
* @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging
*/
struct BridgeParams {
address token;
uint256 chainId;
address recipient;
uint256 amount;
uint256 bonderFee;
uint256 amountOutMin;
uint256 deadline;
uint256 destinationAmountOutMin;
uint256 destinationDeadline;
}
TokenInterface tokenContract = TokenInterface(token);
approve(tokenContract, address(router), amount);
function _swapAndSend(BridgeParams memory params) internal {
IHopRouter router = _getRouter(params.token);
TokenInterface tokenContract = TokenInterface(params.token);
approve(tokenContract, address(router), params.amount);
router.swapAndSend(
chainId,
recipient,
amount,
bonderFee,
amountOutMin,
deadline,
destinationAmountOutMin,
destinationDeadline
params.chainId,
params.recipient,
params.amount,
params.bonderFee,
params.amountOutMin,
params.deadline,
params.destinationAmountOutMin,
params.destinationDeadline
);
}

View File

@ -17,154 +17,61 @@ abstract contract Resolver is Helpers {
/**
* @dev Bridge Token.
* @notice Bridge Token on HOP.
* @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)
* @param chainId The Id of the destination chain.(For MAINNET : 1)
* @param recipientOnL1 The address to recieve the token on destination chain (Layer 1).
* @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.
* @param amountOutMin minimum amount of token out for swap
* @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week))
* @param destinationAmountOutMin minimum amount of token out for bridge
* @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week))
* @param getId ID to retrieve amtA.
* @param params BridgeParams struct for bridging
* @param bridgeToL1 bool to check which layer to migrate to
* @param getId ID to retrieve amount from last spell.
*/
function sendToL1(
address token,
uint256 chainId,
address recipientOnL1,
uint256 amount,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
uint256 destinationAmountOutMin,
uint256 destinationDeadline,
BridgeParams memory params,
bool bridgeToL1,
uint256 getId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amount);
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
if (isMatic) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
convertMaticToWmatic(isMatic, tokenContract, _amt);
} else {
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
if (bridgeToL1) {
require(
params.destinationAmountOutMin == 0,
"destinationAmountOutMin != 0, sending to L1"
);
require(
params.destinationDeadline == 0,
"destinationDeadline != 0, sending to L1"
);
}
require(
destinationAmountOutMin == 0,
"destinationAmountOutMin != 0, sending to L1"
);
require(
destinationDeadline == 0,
"destinationDeadline != 0, sending to L1"
);
params.amount = getUint(getId, params.amount);
_swapAndSend(
_token,
chainId,
recipientOnL1,
_amt,
bonderFee,
amountOutMin,
deadline,
destinationAmountOutMin,
destinationDeadline
);
bool isMatic = params.token == maticAddr;
params.token = params.token == maticAddr ? wmaticAddr : params.token;
_eventName = "LogSendToL1(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
_token,
chainId,
recipientOnL1,
_amt,
bonderFee,
amountOutMin,
deadline,
destinationAmountOutMin,
destinationDeadline,
getId
);
}
/**
* @dev Send to L2 .
* @notice Bridge Token on HOP.
* @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)
* @param chainId The Id of the destination chain.(For MAINNET : 1)
* @param recipientOnL2 The address to recieve the token on destination chain (Layer 2).
* @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.
* @param amountOutMin minimum amount of token out for swap
* @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week))
* @param destinationAmountOutMin minimum amount of token out for bridge
* @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week))
* @param getId ID to retrieve amtA.
*/
function sendToL2(
address token,
uint256 chainId,
address recipientOnL2,
uint256 amount,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
uint256 destinationAmountOutMin,
uint256 destinationDeadline,
uint256 getId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amount);
bool isMatic = token == maticAddr;
address _token = isMatic ? wmaticAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
TokenInterface tokenContract = TokenInterface(params.token);
if (isMatic) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
convertMaticToWmatic(isMatic, tokenContract, _amt);
params.amount = params.amount == uint256(-1)
? address(this).balance
: params.amount;
convertMaticToWmatic(isMatic, tokenContract, params.amount);
} else {
_amt = _amt == uint256(-1)
params.amount = params.amount == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
: params.amount;
}
_swapAndSend(
_token,
chainId,
recipientOnL2,
_amt,
bonderFee,
amountOutMin,
deadline,
destinationAmountOutMin,
destinationDeadline
);
_swapAndSend(params);
_eventName = "LogSendToL2(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)";
_eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
_token,
chainId,
recipientOnL2,
_amt,
bonderFee,
amountOutMin,
deadline,
destinationAmountOutMin,
destinationDeadline,
params.token,
params.chainId,
params.recipient,
params.amount,
params.bonderFee,
params.amountOutMin,
params.deadline,
params.destinationAmountOutMin,
params.destinationDeadline,
getId
);
}