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; pragma solidity ^0.7.0;
contract Events { contract Events {
event LogSendToL1( event LogBridge(
address token,
uint256 chainId,
address recipient,
uint256 amount,
uint256 bonderFee,
uint256 amountOutMin,
uint256 deadline,
uint256 destinationAmountOutMin,
uint256 destinationDeadline,
uint256 getId
);
event LogSendToL2(
address token, address token,
uint256 chainId, uint256 chainId,
address recipient, address recipient,

View File

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

View File

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