This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CurveV1StableSwap.sol
87 lines (71 loc) · 3.44 KB
/
CurveV1StableSwap.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2023 Tokemak Foundation. All rights reserved.
pragma solidity 0.8.17;
import { IERC20, SafeERC20 } from "openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import { ICurveV1StableSwap } from "src/interfaces/external/curve/ICurveV1StableSwap.sol";
import { ISwapRouter } from "src/interfaces/swapper/ISwapRouter.sol";
import { BaseAdapter, ISyncSwapper } from "src/swapper/adapters/BaseAdapter.sol";
import { IWETH9 } from "src/interfaces/utils/IWETH9.sol";
import { LibAdapter } from "src/libs/LibAdapter.sol";
contract CurveV1StableSwap is BaseAdapter {
using SafeERC20 for IERC20;
IWETH9 public immutable weth;
constructor(address _router, address _weth) BaseAdapter(_router) {
weth = IWETH9(_weth);
}
/// @inheritdoc ISyncSwapper
function validate(address fromAddress, ISwapRouter.SwapData memory swapData) external view override {
(int128 sellIndex, int128 buyIndex,) = abi.decode(swapData.data, (int128, int128, bool));
ICurveV1StableSwap pool = ICurveV1StableSwap(swapData.pool);
address sellAddress = pool.coins(_int128ToUint256(sellIndex));
address buyAddress = pool.coins(_int128ToUint256(buyIndex));
// verify that the fromAddress and toAddress are in the pool
if (!isTokenMatch(fromAddress, sellAddress)) revert DataMismatch("fromAddress");
if (!isTokenMatch(swapData.token, buyAddress)) revert DataMismatch("toAddress");
}
/// @inheritdoc ISyncSwapper
function swap(
address poolAddress,
address sellTokenAddress,
uint256 sellAmount,
address buyTokenAddress,
uint256 minBuyAmount,
bytes memory data
) external override onlyRouter returns (uint256 amount) {
(int128 sellIndex, int128 buyIndex, bool isEth) = abi.decode(data, (int128, int128, bool));
ICurveV1StableSwap pool = ICurveV1StableSwap(poolAddress);
IERC20(sellTokenAddress).safeApprove(poolAddress, sellAmount);
amount = pool.exchange(sellIndex, buyIndex, sellAmount, minBuyAmount);
// The rest of the system only deals in WETH
if (isEth && buyTokenAddress == address(weth)) {
// slither-disable-next-line arbitrary-send-eth
weth.deposit{ value: amount }();
}
}
function _int128ToUint256(int128 value) internal pure returns (uint256 result) {
if (value < 0) {
revert InvalidIndex();
}
// slither-disable-start assembly
// solhint-disable-next-line no-inline-assembly
assembly {
result := value
}
//slither-disable-end assembly
}
/// @notice Determine if the supplied and queried tokens match
/// @dev Accounts of Curve 0xEeeEe... tokens and accepts WETH as its already wrapped
/// @param fromAddress Token supplied in our config
/// @param queriedAddress Token queried based on the supplied index
/// @return true for "matches"
function isTokenMatch(address fromAddress, address queriedAddress) internal view returns (bool) {
if (queriedAddress == LibAdapter.CURVE_REGISTRY_ETH_ADDRESS_POINTER) {
if (fromAddress == LibAdapter.CURVE_REGISTRY_ETH_ADDRESS_POINTER || fromAddress == address(weth)) {
return true;
}
}
// Only special case is the Curve 0xEeeE representation
// All others must match exact
return fromAddress == queriedAddress;
}
}