Skip to content

Commit

Permalink
Add linea stargate (#124)
Browse files Browse the repository at this point in the history
* Add linea stargate config

* Export linea stargate info

* Add linea endpoints

* Add lynex swap for linea

* Fix correct lynex swap

* Deploy swap for linea

---------

Co-authored-by: AAweidai <[email protected]>
  • Loading branch information
hacpy and AAweidai authored Apr 15, 2024
1 parent cdbf4ce commit 363b6d4
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 188 deletions.
25 changes: 25 additions & 0 deletions ethereum/brownie-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2353,8 +2353,33 @@ networks:
router: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64"
token_list: ""
linea-main:
omnibtc_chainid: 59144
chainid: 59144
SoDiamond: ""
basic_beneficiary: "0xD4c56833A6D4C83A81972dA7e0eDA924F0729989"
StargateHelper: ""
endpoints:
[
"https://linea.blockpi.network/v1/rpc/public",
"https://1rpc.io/linea",
"https://linea.drpc.org",
"https://linea.decubate.com",
"https://rpc.linea.build",
]
stargate:
router: "0x2F6F07CDcf3588944Bf4C42aC74ff24bF56e7590"
chainid: 183
poolid:
weth: 13
tokens:
sgeth:
address: "0x224D8Fd7aB6AD4c6eb4611Ce56EF35Dec2277F03"
decimal: 18
swap:
- ILynexRouter:
name: Lynex
router: "0x610D2f07b7EdC67565160F587F37636194C34E74"
token_list: ""
- IMetaAggregationRouterV2:
name: KyberSwap
router: "0x6131B5fae19EA4f9D964eAc0408E4408b66337b5"
Expand Down
38 changes: 38 additions & 0 deletions ethereum/contracts/Interfaces/Lynex/ILynexRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

interface ILynexRouter {
struct route {
address from;
address to;
bool stable;
}

function getAmountsOut(
uint amountIn,
route[] memory routes
) external view returns (uint[] memory amounts);

function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
route[] calldata routes,
address to,
uint deadline
) external returns (uint[] memory amounts);

function swapExactETHForTokens(
uint amountOutMin,
route[] calldata routes,
address to,
uint deadline
) external payable returns (uint[] memory amounts);

function swapExactTokensForETH(
uint amountIn,
uint amountOutMin,
route[] calldata routes,
address to,
uint deadline
) external returns (uint[] memory amounts);
}
126 changes: 126 additions & 0 deletions ethereum/contracts/Libraries/LibCorrectSwapV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {ICamelotRouter} from "../Interfaces/Camelot/ICamelotRouter.sol";
import {IMetaAggregationRouterV2} from "../Interfaces/Kyberswap/IMetaAggregationRouterV2.sol";
import {IOneInchGenericRouter, IOneInchClipperRouter, IOneInchUnoswapRouter, IOneInchUnoswapV3Router} from "../Interfaces/OneInch/IAggregationRouterV5.sol";
import {IOpenOceanExchange, IOpenOceanCaller, IUniswapV2Exchange} from "../Interfaces/OpenOcean/IOpenOceanExchange.sol";
import {ILynexRouter} from "../Interfaces/Lynex/ILynexRouter.sol";

contract LibCorrectSwapV2 is ICorrectSwap {
address public owner;
Expand Down Expand Up @@ -239,6 +240,14 @@ library LibSwapFuncSigs {
IMoeRouter.swapExactNativeForTokens.selector;
bytes4 internal constant _FUNC56 =
IMoeRouter.swapExactTokensForNative.selector;

// Lynex
bytes4 internal constant _FUNC57 =
ILynexRouter.swapExactETHForTokens.selector;
bytes4 internal constant _FUNC58 =
ILynexRouter.swapExactTokensForETH.selector;
bytes4 internal constant _FUNC59 =
ILynexRouter.swapExactTokensForTokens.selector;
}

contract CorrectUniswapV2Factory {
Expand Down Expand Up @@ -2722,3 +2731,120 @@ contract CorrectOpenOcean is ICorrectSwap {
);
}
}

contract CorrectLynexFactory {
constructor(LibCorrectSwapV2 libCorrectSwapV2) {
// Lynex
bytes4[] memory sigs = new bytes4[](3);
sigs[0] = LibSwapFuncSigs._FUNC57;
sigs[1] = LibSwapFuncSigs._FUNC58;
sigs[2] = LibSwapFuncSigs._FUNC59;
address correctLynex = address(new CorrectLynex());
libCorrectSwapV2.setCorrectSwap(sigs, correctLynex);
}
}

contract CorrectLynex is ICorrectSwap {
// Lynex
bytes4 internal constant _FUNC57 =
ILynexRouter.swapExactETHForTokens.selector;
bytes4 internal constant _FUNC58 =
ILynexRouter.swapExactTokensForETH.selector;
bytes4 internal constant _FUNC59 =
ILynexRouter.swapExactTokensForTokens.selector;

// @dev Correct input of destination chain swapData
function correctSwap(
bytes calldata _data,
uint256 _amount
) external returns (bytes memory) {
bytes4 sig = bytes4(_data[:4]);

if (sig == _FUNC57) {
return _data;
} else if (sig == _FUNC58 || sig == _FUNC59) {
return basicCorrectSwap(_data, _amount);
} else {
revert("correctLynex error");
}
}

// @dev Fix min amount
function fixMinAmount(
bytes calldata _data,
uint256 _deltaMinAmount
) external view returns (uint256, bytes memory) {
bytes4 sig = bytes4(_data[:4]);
if (sig == _FUNC57) {
(
uint256 _amountOutMin,
ILynexRouter.route[] memory _path,
address _to,
uint256 _deadline
) = abi.decode(
_data[4:],
(uint256, ILynexRouter.route[], address, uint256)
);
return (
_amountOutMin,
abi.encodeWithSelector(
sig,
_amountOutMin + _deltaMinAmount,
_path,
_to,
_deadline
)
);
} else if (sig == _FUNC58 || sig == _FUNC59) {
(
uint256 _amount,
uint256 _amountOutMin,
ILynexRouter.route[] memory _path,
address _to,
uint256 _deadline
) = abi.decode(
_data[4:],
(uint256, uint256, ILynexRouter.route[], address, uint256)
);
return (
_amountOutMin,
abi.encodeWithSelector(
sig,
_amount,
_amountOutMin + _deltaMinAmount,
_path,
_to,
_deadline
)
);
}

revert("fix lynex swap amount fail!");
}

function basicCorrectSwap(
bytes calldata _data,
uint256 _amount
) internal pure returns (bytes memory) {
(
,
uint256 _amountOutMin,
ILynexRouter.route[] memory _path,
address _to,
uint256 _deadline
) = abi.decode(
_data[4:],
(uint256, uint256, ILynexRouter.route[], address, uint256)
);

return
abi.encodeWithSelector(
bytes4(_data[:4]),
_amount,
_amountOutMin,
_path,
_to,
_deadline
);
}
}
2 changes: 2 additions & 0 deletions ethereum/export/mainnet/ContractDeployed.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@
"BulkTransfer": "0xbb9220B732f5028607838447a67d937C6a6a099f"
},
"linea-main": {
"LibSoFeeStargateV2": "0x933Ce99df42035f25511aaeecCfE8B03175A6b0e",
"StargateFacet": "0x6205053c3a7Fc339338a63FeB3f0A801eC180FBE",
"CorrectKyberswapFactory": "0x62435173203A235c7336FA640DcDE6c20D857630",
"DiamondCutFacet": "0x1f0d7E0C10Adf20ae9621431a53151ed6F4361E3",
"DiamondLoupeFacet": "0x7857E0Ca2b63Ffcaba9c3c92c1553B8DA3492096",
Expand Down
Loading

0 comments on commit 363b6d4

Please sign in to comment.