@shift-defi/swap-adapters 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -2
- package/contracts/OneInchAdapter.sol +65 -0
- package/contracts/UniswapV3SwapRouter02Adapter.sol +195 -0
- package/contracts/custom-adapters/monad/WmonUsdt0MonadAdapter.sol +103 -0
- package/contracts/dependencies/1inch/IOneInchV6.sol +15 -0
- package/contracts/dependencies/IWETH9.sol +8 -0
- package/contracts/dependencies/curve/ICurveStableSwapNG.sol +8 -0
- package/contracts/dependencies/uniswap-v3/ISwapRouter02.sol +17 -0
- package/contracts/interfaces/IOneInchAdapter.sol +18 -0
- package/contracts/interfaces/IUniswapV3SwapRouter02Adapter.sol +62 -0
- package/contracts/interfaces/IWmonUsdt0MonadAdapter.sol +11 -0
- package/package.json +23 -4
package/README.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Shift DeFi Swap Adapters
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Swap adapters that route Shift core's `SwapRouter` (`@shift-defi/core`)
|
|
4
|
+
through external venues: Uniswap V3 SwapRouter02, 1inch v6, a generic
|
|
5
|
+
router call (`UniversalAdapter`) and a fixed-price pool (`CustomPool`).
|
|
6
|
+
|
|
7
|
+
## Setup
|
|
8
|
+
|
|
9
|
+
Requires [Foundry](https://getfoundry.sh) v1.8.3 (`foundryup -i v1.8.3`),
|
|
10
|
+
[just](https://github.com/casey/just), Node.js and npm.
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
just setup # npm install + point git at the hooks in .githooks
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Fork tests run against live networks at pinned blocks, so the RPCs must
|
|
17
|
+
serve archive state. Put the RPC URLs in `.env` (gitignored), which forge
|
|
18
|
+
loads on its own: `MONAD_RPC_URL` (and `ETH_RPC_URL` for scripts).
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```shell
|
|
23
|
+
just # list recipes
|
|
24
|
+
just build # compile contracts/ and check sizes
|
|
25
|
+
just fmt # forge fmt
|
|
26
|
+
just test # every test (fork tests pin their blocks)
|
|
27
|
+
just test-unit # unit tests only, no RPC
|
|
28
|
+
just check # fmt check + build + every test
|
|
29
|
+
just analyze # slither + aderyn
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Git hooks: `commit-msg` enforces `<type>(<scope>): <summary>` (see
|
|
33
|
+
`AGENTS.md`); `pre-commit` runs `forge fmt --check`, `just build` and every
|
|
34
|
+
test when Solidity or build config is staged.
|
|
35
|
+
|
|
36
|
+
## Agent harness
|
|
37
|
+
|
|
38
|
+
Agent instructions live in `AGENTS.md`, read natively by Codex and Cursor.
|
|
39
|
+
`CLAUDE.md` is a symlink to it, so Claude Code reads the same file. Edit
|
|
40
|
+
`AGENTS.md` only.
|
|
41
|
+
|
|
42
|
+
- `AGENTS.md`: repo-wide context, conventions and rules
|
|
43
|
+
- `test/AGENTS.md` (with `test/CLAUDE.md` symlinked to it): test
|
|
44
|
+
conventions, loaded when working under `test/`
|
|
45
|
+
- `.claude/settings.json`, `.codex/`, `.cursorignore`: keep `.env` files out
|
|
46
|
+
of agent reach, sandbox shell commands, and run
|
|
47
|
+
`.agents/hooks/sol-change-check.sh` (format, compile `contracts/`, run the
|
|
48
|
+
unit tests) when an agent stops; fork tests run in the pre-commit hook
|
|
49
|
+
|
|
50
|
+
Symlinks do not survive every Windows checkout; on Windows, enable
|
|
51
|
+
`core.symlinks` or replace each `CLAUDE.md` with a one-line pointer to
|
|
52
|
+
`AGENTS.md`.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
|
5
|
+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
6
|
+
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
|
7
|
+
|
|
8
|
+
import {ISwapAdapter} from "@shift-defi/core/interfaces/ISwapAdapter.sol";
|
|
9
|
+
import {IOneInchAdapter} from "./interfaces/IOneInchAdapter.sol";
|
|
10
|
+
import {IOneInchV6} from "./dependencies/1inch/IOneInchV6.sol";
|
|
11
|
+
|
|
12
|
+
contract OneInchAdapter is ReentrancyGuard, ISwapAdapter, IOneInchAdapter {
|
|
13
|
+
using SafeERC20 for IERC20;
|
|
14
|
+
|
|
15
|
+
address public immutable oneInchRouter;
|
|
16
|
+
|
|
17
|
+
constructor(address _oneInchRouter) {
|
|
18
|
+
oneInchRouter = _oneInchRouter;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// @inheritdoc ISwapAdapter
|
|
22
|
+
function previewSwap(
|
|
23
|
+
address tokenIn,
|
|
24
|
+
address tokenOut,
|
|
25
|
+
uint256 amountIn,
|
|
26
|
+
bytes memory data
|
|
27
|
+
) external view override returns (uint256 amountOut) {}
|
|
28
|
+
|
|
29
|
+
/// @inheritdoc ISwapAdapter
|
|
30
|
+
function swap(
|
|
31
|
+
address tokenIn,
|
|
32
|
+
address tokenOut,
|
|
33
|
+
uint256 amountIn,
|
|
34
|
+
uint256 minAmountOut,
|
|
35
|
+
address receiver,
|
|
36
|
+
bytes memory data
|
|
37
|
+
) external payable override nonReentrant {
|
|
38
|
+
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
|
|
39
|
+
IERC20(tokenIn).forceApprove(oneInchRouter, amountIn);
|
|
40
|
+
|
|
41
|
+
uint256 balanceBefore = IERC20(tokenOut).balanceOf(address(this));
|
|
42
|
+
|
|
43
|
+
(bytes4 selector, bytes memory parameters) = abi.decode(data, (bytes4, bytes));
|
|
44
|
+
if (selector == IOneInchV6.swap.selector) {
|
|
45
|
+
(address executor, IOneInchV6.SwapDescription memory descs, bytes memory executorData) = abi.decode(
|
|
46
|
+
parameters,
|
|
47
|
+
(address, IOneInchV6.SwapDescription, bytes)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
require(descs.srcToken == tokenIn, InvalidSourceToken(descs.srcToken));
|
|
51
|
+
require(descs.dstToken == tokenOut, InvalidDestinationToken(descs.dstToken));
|
|
52
|
+
require(descs.amount == amountIn, InvalidAmountIn(descs.amount));
|
|
53
|
+
require(descs.minReturnAmount <= minAmountOut, InvalidMinAmountOut(descs.minReturnAmount));
|
|
54
|
+
require(descs.dstReceiver == address(this), InvalidDstReceiver(descs.dstReceiver));
|
|
55
|
+
IOneInchV6(oneInchRouter).swap(executor, descs, executorData);
|
|
56
|
+
} else {
|
|
57
|
+
revert InvalidSelector(selector);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
uint256 balanceAfter = IERC20(tokenOut).balanceOf(address(this));
|
|
61
|
+
uint256 deltaTokenOut = balanceAfter - balanceBefore;
|
|
62
|
+
require(deltaTokenOut >= minAmountOut, SlippageCheckFailed(tokenOut, deltaTokenOut, minAmountOut));
|
|
63
|
+
IERC20(tokenOut).safeTransfer(receiver, deltaTokenOut);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
|
|
5
|
+
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
|
6
|
+
|
|
7
|
+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
8
|
+
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
|
9
|
+
|
|
10
|
+
import {ISwapAdapter} from "@shift-defi/core/interfaces/ISwapAdapter.sol";
|
|
11
|
+
import {IWETH9} from "./dependencies/IWETH9.sol";
|
|
12
|
+
import {ISwapRouter02} from "./dependencies/uniswap-v3/ISwapRouter02.sol";
|
|
13
|
+
import {IUniswapV3SwapRouter02Adapter} from "./interfaces/IUniswapV3SwapRouter02Adapter.sol";
|
|
14
|
+
|
|
15
|
+
/// @dev Targets Uniswap's SwapRouter02 ABI (`ISwapRouter02`), whose
|
|
16
|
+
/// `ExactInputParams` carries no `deadline` field - the shape actually
|
|
17
|
+
/// deployed on every venue this adapter targets. All swaps go through `swap`;
|
|
18
|
+
/// this adapter does not expose `exactInput`/`exactInputSingle` itself.
|
|
19
|
+
contract UniswapV3SwapRouter02Adapter is AccessControl, ReentrancyGuard, ISwapAdapter, IUniswapV3SwapRouter02Adapter {
|
|
20
|
+
using SafeERC20 for IERC20;
|
|
21
|
+
|
|
22
|
+
bytes32 private constant WHITELIST_MANAGER_ROLE = keccak256("WHITELIST_MANAGER_ROLE");
|
|
23
|
+
|
|
24
|
+
address public immutable swapRouter02;
|
|
25
|
+
address public immutable weth9;
|
|
26
|
+
mapping(bytes => bool) public whitelistedPaths;
|
|
27
|
+
|
|
28
|
+
/// @dev Grants `DEFAULT_ADMIN_ROLE` to `defaultAdmin` and
|
|
29
|
+
/// `WHITELIST_MANAGER_ROLE` to `whitelistManager`.
|
|
30
|
+
/// @param defaultAdmin Account that receives `DEFAULT_ADMIN_ROLE`.
|
|
31
|
+
/// @param whitelistManager Account that receives `WHITELIST_MANAGER_ROLE`.
|
|
32
|
+
/// @param _swapRouter02 Uniswap SwapRouter02 this adapter calls.
|
|
33
|
+
constructor(address defaultAdmin, address whitelistManager, address _swapRouter02) {
|
|
34
|
+
require(defaultAdmin != address(0), ZeroAddress());
|
|
35
|
+
require(whitelistManager != address(0), ZeroAddress());
|
|
36
|
+
require(_swapRouter02 != address(0), ZeroAddress());
|
|
37
|
+
swapRouter02 = _swapRouter02;
|
|
38
|
+
weth9 = ISwapRouter02(_swapRouter02).WETH9();
|
|
39
|
+
_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);
|
|
40
|
+
_grantRole(WHITELIST_MANAGER_ROLE, whitelistManager);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// @dev Only SwapRouter02's `refundETH` sends native tokens here (see `swap`).
|
|
44
|
+
receive() external payable {
|
|
45
|
+
require(msg.sender == swapRouter02, UnexpectedNativeSender(msg.sender));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
modifier onlyWhitelistManager() {
|
|
49
|
+
require(hasRole(WHITELIST_MANAGER_ROLE, msg.sender), NotWhitelistManager(msg.sender));
|
|
50
|
+
_;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// @inheritdoc IUniswapV3SwapRouter02Adapter
|
|
54
|
+
function whitelistPath(
|
|
55
|
+
address[] memory tokens,
|
|
56
|
+
uint24[] memory fees
|
|
57
|
+
) external onlyWhitelistManager returns (bytes memory) {
|
|
58
|
+
require(tokens.length == fees.length + 1, InvalidPathLengths(tokens.length, fees.length));
|
|
59
|
+
require(fees.length > 0, ZeroHopPath());
|
|
60
|
+
|
|
61
|
+
bytes memory path;
|
|
62
|
+
assembly {
|
|
63
|
+
path := mload(0x40)
|
|
64
|
+
let ptr := add(path, 0x20)
|
|
65
|
+
mstore(ptr, shl(96, mload(add(tokens, 0x20))))
|
|
66
|
+
ptr := add(ptr, 20)
|
|
67
|
+
for {
|
|
68
|
+
let i := 0
|
|
69
|
+
} lt(i, mload(fees)) {
|
|
70
|
+
i := add(i, 1)
|
|
71
|
+
} {
|
|
72
|
+
let fee := mload(add(fees, mul(add(i, 1), 0x20)))
|
|
73
|
+
mstore8(ptr, shr(16, and(fee, 0xffffff)))
|
|
74
|
+
mstore8(add(ptr, 1), shr(8, and(fee, 0xffffff)))
|
|
75
|
+
mstore8(add(ptr, 2), and(fee, 0xff))
|
|
76
|
+
ptr := add(ptr, 3)
|
|
77
|
+
mstore(ptr, shl(96, mload(add(tokens, mul(add(i, 2), 0x20)))))
|
|
78
|
+
ptr := add(ptr, 20)
|
|
79
|
+
}
|
|
80
|
+
mstore(path, sub(ptr, add(path, 0x20)))
|
|
81
|
+
mstore(0x40, ptr)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
whitelistedPaths[path] = true;
|
|
85
|
+
emit PathWhitelisted(tokens[0], tokens[tokens.length - 1], path);
|
|
86
|
+
|
|
87
|
+
return path;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// @inheritdoc IUniswapV3SwapRouter02Adapter
|
|
91
|
+
function decodePath(bytes memory path) public pure returns (address[] memory tokens, uint24[] memory fees) {
|
|
92
|
+
assembly {
|
|
93
|
+
let len := mload(path)
|
|
94
|
+
let n := div(sub(len, 20), 23)
|
|
95
|
+
|
|
96
|
+
tokens := mload(0x40)
|
|
97
|
+
mstore(tokens, add(n, 1))
|
|
98
|
+
let feesPtr := add(tokens, add(0x20, mul(add(n, 1), 0x20)))
|
|
99
|
+
fees := feesPtr
|
|
100
|
+
mstore(fees, n)
|
|
101
|
+
|
|
102
|
+
let ptr := add(path, 0x20)
|
|
103
|
+
let tokensPtr := add(tokens, 0x20)
|
|
104
|
+
|
|
105
|
+
mstore(tokensPtr, shr(96, mload(ptr)))
|
|
106
|
+
ptr := add(ptr, 20)
|
|
107
|
+
|
|
108
|
+
for {
|
|
109
|
+
let i := 0
|
|
110
|
+
} lt(i, n) {
|
|
111
|
+
i := add(i, 1)
|
|
112
|
+
} {
|
|
113
|
+
let feeVal := and(shr(232, mload(ptr)), 0xffffff)
|
|
114
|
+
mstore(add(fees, mul(add(i, 1), 0x20)), feeVal)
|
|
115
|
+
|
|
116
|
+
ptr := add(ptr, 3)
|
|
117
|
+
|
|
118
|
+
tokensPtr := add(tokensPtr, 0x20)
|
|
119
|
+
mstore(tokensPtr, shr(96, mload(ptr)))
|
|
120
|
+
ptr := add(ptr, 20)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mstore(0x40, and(add(feesPtr, add(0x20, mul(n, 0x20))), not(31)))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// @inheritdoc IUniswapV3SwapRouter02Adapter
|
|
128
|
+
function blacklistPath(bytes calldata path) external onlyWhitelistManager {
|
|
129
|
+
require(whitelistedPaths[path], PathNotWhitelisted(path));
|
|
130
|
+
whitelistedPaths[path] = false;
|
|
131
|
+
|
|
132
|
+
(address[] memory tokens, ) = decodePath(path);
|
|
133
|
+
emit PathBlacklisted(tokens[0], tokens[tokens.length - 1], path);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// @inheritdoc ISwapAdapter
|
|
137
|
+
function previewSwap(
|
|
138
|
+
address tokenIn,
|
|
139
|
+
address tokenOut,
|
|
140
|
+
uint256 amountIn,
|
|
141
|
+
bytes memory data
|
|
142
|
+
) external view override returns (uint256 amountOut) {}
|
|
143
|
+
|
|
144
|
+
/// @inheritdoc ISwapAdapter
|
|
145
|
+
function swap(
|
|
146
|
+
address tokenIn,
|
|
147
|
+
address tokenOut,
|
|
148
|
+
uint256 amountIn,
|
|
149
|
+
uint256 minAmountOut,
|
|
150
|
+
address receiver,
|
|
151
|
+
bytes calldata data
|
|
152
|
+
) external payable override nonReentrant {
|
|
153
|
+
require(whitelistedPaths[data], PathNotWhitelisted(data));
|
|
154
|
+
// Whitelisted paths are always >= 43 bytes, so both slices are in bounds.
|
|
155
|
+
address pathTokenIn = address(bytes20(data[:20]));
|
|
156
|
+
address pathTokenOut = address(bytes20(data[data.length - 20:]));
|
|
157
|
+
require(tokenIn == pathTokenIn, TokenInPathMismatch(tokenIn, pathTokenIn));
|
|
158
|
+
require(tokenOut == pathTokenOut, TokenOutPathMismatch(tokenOut, pathTokenOut));
|
|
159
|
+
|
|
160
|
+
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
|
|
161
|
+
|
|
162
|
+
address weth9Cached = weth9;
|
|
163
|
+
if (tokenIn == weth9Cached) {
|
|
164
|
+
uint256 nativeBalanceBefore = address(this).balance;
|
|
165
|
+
ISwapRouter02(swapRouter02).refundETH();
|
|
166
|
+
uint256 refunded = address(this).balance - nativeBalanceBefore;
|
|
167
|
+
if (refunded > 0) {
|
|
168
|
+
IWETH9(weth9Cached).deposit{value: refunded}();
|
|
169
|
+
amountIn += refunded;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
IERC20(tokenIn).forceApprove(swapRouter02, amountIn);
|
|
174
|
+
|
|
175
|
+
uint256 tokenInBalanceBefore = IERC20(tokenIn).balanceOf(address(this));
|
|
176
|
+
uint256 balanceBefore = IERC20(tokenOut).balanceOf(address(this));
|
|
177
|
+
ISwapRouter02(swapRouter02).exactInput(
|
|
178
|
+
ISwapRouter02.ExactInputParams({
|
|
179
|
+
amountIn: amountIn,
|
|
180
|
+
amountOutMinimum: minAmountOut,
|
|
181
|
+
path: data,
|
|
182
|
+
recipient: address(this)
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
IERC20(tokenIn).forceApprove(swapRouter02, 0);
|
|
186
|
+
|
|
187
|
+
uint256 amountInSpent = tokenInBalanceBefore - IERC20(tokenIn).balanceOf(address(this));
|
|
188
|
+
require(amountInSpent == amountIn, PartialFill(amountIn, amountInSpent));
|
|
189
|
+
|
|
190
|
+
uint256 balanceAfter = IERC20(tokenOut).balanceOf(address(this));
|
|
191
|
+
uint256 deltaTokenOut = balanceAfter - balanceBefore;
|
|
192
|
+
require(deltaTokenOut >= minAmountOut, SlippageCheckFailed(tokenOut, deltaTokenOut, minAmountOut));
|
|
193
|
+
IERC20(tokenOut).safeTransfer(receiver, deltaTokenOut);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
|
5
|
+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
6
|
+
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
|
7
|
+
|
|
8
|
+
import {ISwapAdapter} from "@shift-defi/core/interfaces/ISwapAdapter.sol";
|
|
9
|
+
|
|
10
|
+
import {IWmonUsdt0MonadAdapter} from "../../interfaces/IWmonUsdt0MonadAdapter.sol";
|
|
11
|
+
import {IWETH9} from "../../dependencies/IWETH9.sol";
|
|
12
|
+
import {ISwapRouter02} from "../../dependencies/uniswap-v3/ISwapRouter02.sol";
|
|
13
|
+
import {ICurveStableSwapNG} from "../../dependencies/curve/ICurveStableSwapNG.sol";
|
|
14
|
+
|
|
15
|
+
contract WmonUsdt0MonadAdapter is ReentrancyGuard, IWmonUsdt0MonadAdapter {
|
|
16
|
+
using SafeERC20 for IERC20;
|
|
17
|
+
|
|
18
|
+
address private constant WMON = 0x3bd359C1119dA7Da1D913D1C4D2B7c461115433A;
|
|
19
|
+
address private constant USDC = 0x754704Bc059F8C67012fEd69BC8A327a5aafb603;
|
|
20
|
+
address private constant USDT0 = 0xe7cd86e13AC4309349F30B3435a9d337750fC82D;
|
|
21
|
+
|
|
22
|
+
ISwapRouter02 private constant SWAP_ROUTER_02 = ISwapRouter02(0xfE31F71C1b106EAc32F1A19239c9a9A72ddfb900);
|
|
23
|
+
ICurveStableSwapNG private constant CURVE_3POOL = ICurveStableSwapNG(0x942644106B073E30D72c2C5D7529D5C296ea91ab);
|
|
24
|
+
|
|
25
|
+
uint24 private constant UNISWAP_FEE = 3000;
|
|
26
|
+
int128 private constant USDC_INDEX = 1;
|
|
27
|
+
int128 private constant USDT0_INDEX = 2;
|
|
28
|
+
|
|
29
|
+
/// @dev Only SwapRouter02's `refundETH` sends native tokens here (see `swap`).
|
|
30
|
+
receive() external payable {
|
|
31
|
+
require(msg.sender == address(SWAP_ROUTER_02), UnexpectedNativeSender(msg.sender));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// @inheritdoc ISwapAdapter
|
|
35
|
+
function previewSwap(
|
|
36
|
+
address tokenIn,
|
|
37
|
+
address tokenOut,
|
|
38
|
+
uint256 amountIn,
|
|
39
|
+
bytes memory data
|
|
40
|
+
) external view override returns (uint256 amountOut) {}
|
|
41
|
+
|
|
42
|
+
/// @inheritdoc ISwapAdapter
|
|
43
|
+
function swap(
|
|
44
|
+
address tokenIn,
|
|
45
|
+
address tokenOut,
|
|
46
|
+
uint256 amountIn,
|
|
47
|
+
uint256 minAmountOut,
|
|
48
|
+
address receiver,
|
|
49
|
+
bytes memory
|
|
50
|
+
) external payable override nonReentrant {
|
|
51
|
+
require(tokenIn == WMON, InvalidTokenIn(tokenIn, WMON));
|
|
52
|
+
require(tokenOut == USDT0, InvalidTokenOut(tokenOut, USDT0));
|
|
53
|
+
|
|
54
|
+
IERC20(WMON).safeTransferFrom(msg.sender, address(this), amountIn);
|
|
55
|
+
amountIn += _wrapRouterNative();
|
|
56
|
+
|
|
57
|
+
uint256 amountUsdc = _swapWmonToUsdc(amountIn);
|
|
58
|
+
uint256 amountUsdt0 = _swapUsdcToUsdt0(amountUsdc, minAmountOut);
|
|
59
|
+
|
|
60
|
+
require(amountUsdt0 >= minAmountOut, SlippageCheckFailed(USDT0, amountUsdt0, minAmountOut));
|
|
61
|
+
IERC20(USDT0).safeTransfer(receiver, amountUsdt0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function _wrapRouterNative() private returns (uint256 refunded) {
|
|
65
|
+
uint256 nativeBalanceBefore = address(this).balance;
|
|
66
|
+
SWAP_ROUTER_02.refundETH();
|
|
67
|
+
refunded = address(this).balance - nativeBalanceBefore;
|
|
68
|
+
if (refunded > 0) {
|
|
69
|
+
IWETH9(WMON).deposit{value: refunded}();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function _swapWmonToUsdc(uint256 amountIn) private returns (uint256) {
|
|
74
|
+
uint256 wmonBalanceBefore = IERC20(WMON).balanceOf(address(this));
|
|
75
|
+
uint256 usdcBalanceBefore = IERC20(USDC).balanceOf(address(this));
|
|
76
|
+
|
|
77
|
+
IERC20(WMON).forceApprove(address(SWAP_ROUTER_02), amountIn);
|
|
78
|
+
SWAP_ROUTER_02.exactInput(
|
|
79
|
+
ISwapRouter02.ExactInputParams({
|
|
80
|
+
path: abi.encodePacked(WMON, UNISWAP_FEE, USDC),
|
|
81
|
+
recipient: address(this),
|
|
82
|
+
amountIn: amountIn,
|
|
83
|
+
amountOutMinimum: 0
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
IERC20(WMON).forceApprove(address(SWAP_ROUTER_02), 0);
|
|
87
|
+
|
|
88
|
+
uint256 amountInSpent = wmonBalanceBefore - IERC20(WMON).balanceOf(address(this));
|
|
89
|
+
require(amountInSpent == amountIn, PartialFill(amountIn, amountInSpent));
|
|
90
|
+
|
|
91
|
+
return IERC20(USDC).balanceOf(address(this)) - usdcBalanceBefore;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function _swapUsdcToUsdt0(uint256 amountUsdc, uint256 minAmountOut) private returns (uint256) {
|
|
95
|
+
uint256 usdt0BalanceBefore = IERC20(USDT0).balanceOf(address(this));
|
|
96
|
+
|
|
97
|
+
IERC20(USDC).forceApprove(address(CURVE_3POOL), amountUsdc);
|
|
98
|
+
CURVE_3POOL.exchange(USDC_INDEX, USDT0_INDEX, amountUsdc, minAmountOut);
|
|
99
|
+
IERC20(USDC).forceApprove(address(CURVE_3POOL), 0);
|
|
100
|
+
|
|
101
|
+
return IERC20(USDT0).balanceOf(address(this)) - usdt0BalanceBefore;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
interface IOneInchV6 {
|
|
5
|
+
struct SwapDescription {
|
|
6
|
+
address srcToken;
|
|
7
|
+
address dstToken;
|
|
8
|
+
address srcReceiver;
|
|
9
|
+
address dstReceiver;
|
|
10
|
+
uint256 amount;
|
|
11
|
+
uint256 minReturnAmount;
|
|
12
|
+
uint256 flags;
|
|
13
|
+
}
|
|
14
|
+
function swap(address executor, SwapDescription memory descs, bytes memory data) external payable;
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
interface ICurveStableSwapNG {
|
|
5
|
+
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy) external returns (uint256);
|
|
6
|
+
|
|
7
|
+
function get_dy(int128 i, int128 j, uint256 dx) external view returns (uint256);
|
|
8
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
interface ISwapRouter02 {
|
|
5
|
+
struct ExactInputParams {
|
|
6
|
+
bytes path;
|
|
7
|
+
address recipient;
|
|
8
|
+
uint256 amountIn;
|
|
9
|
+
uint256 amountOutMinimum;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function exactInput(ExactInputParams memory params) external payable returns (uint256 amountOut);
|
|
13
|
+
|
|
14
|
+
function refundETH() external payable;
|
|
15
|
+
|
|
16
|
+
function WETH9() external view returns (address);
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
interface IOneInchAdapter {
|
|
5
|
+
error NotRouter(address sender);
|
|
6
|
+
error InvalidSelector(bytes4 selector);
|
|
7
|
+
error InvalidSourceToken(address token);
|
|
8
|
+
error InvalidDestinationToken(address token);
|
|
9
|
+
error InvalidAmountIn(uint256 amount);
|
|
10
|
+
error InvalidMinAmountOut(uint256 amount);
|
|
11
|
+
error InvalidDstReceiver(address receiver);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @notice Returns the address of the 1inch router contract
|
|
15
|
+
* @return The immutable address of the 1inch router
|
|
16
|
+
*/
|
|
17
|
+
function oneInchRouter() external view returns (address);
|
|
18
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
interface IUniswapV3SwapRouter02Adapter {
|
|
5
|
+
event PathWhitelisted(address indexed tokenIn, address indexed tokenOut, bytes path);
|
|
6
|
+
event PathBlacklisted(address indexed tokenIn, address indexed tokenOut, bytes path);
|
|
7
|
+
|
|
8
|
+
error ZeroAddress();
|
|
9
|
+
error PathNotWhitelisted(bytes path);
|
|
10
|
+
error NotWhitelistManager(address sender);
|
|
11
|
+
error InvalidPathLengths(uint256 expectedLength, uint256 actualLength);
|
|
12
|
+
error ZeroHopPath();
|
|
13
|
+
error TokenInPathMismatch(address tokenIn, address pathTokenIn);
|
|
14
|
+
error TokenOutPathMismatch(address tokenOut, address pathTokenOut);
|
|
15
|
+
error PartialFill(uint256 amountIn, uint256 amountInSpent);
|
|
16
|
+
error UnexpectedNativeSender(address sender);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @notice Returns the address of the Uniswap V3 SwapRouter02 contract
|
|
20
|
+
* @return The immutable address of the Uniswap V3 SwapRouter02
|
|
21
|
+
*/
|
|
22
|
+
function swapRouter02() external view returns (address);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @notice Returns the WETH9 token used by SwapRouter02
|
|
26
|
+
* @return The immutable WETH9 address, read from SwapRouter02 at construction
|
|
27
|
+
*/
|
|
28
|
+
function weth9() external view returns (address);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @notice Returns whether a swap path is whitelisted
|
|
32
|
+
* @param path The encoded swap path (tokenIn, fee, tokenOut)
|
|
33
|
+
* @return Whether the path is whitelisted
|
|
34
|
+
*/
|
|
35
|
+
function whitelistedPaths(bytes calldata path) external view returns (bool);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @notice Whitelists a swap path for a token pair with a specific fee tier
|
|
39
|
+
* @dev Only callable by accounts with WHITELIST_MANAGER_ROLE
|
|
40
|
+
* @dev Emits PathWhitelisted event
|
|
41
|
+
* @param tokens The token addresses in the path
|
|
42
|
+
* @param fees The fee tiers for each pool in the path
|
|
43
|
+
* @return path The encoded swap path (tokenIn, fee, tokenOut)
|
|
44
|
+
*/
|
|
45
|
+
function whitelistPath(address[] memory tokens, uint24[] memory fees) external returns (bytes memory path);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @notice Decodes a swap path into tokens and fees
|
|
49
|
+
* @param path The encoded swap path (tokenIn, fee, tokenOut)
|
|
50
|
+
* @return tokens The token addresses in the path
|
|
51
|
+
* @return fees The fee tiers for each pool in the path
|
|
52
|
+
*/
|
|
53
|
+
function decodePath(bytes memory path) external pure returns (address[] memory tokens, uint24[] memory fees);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @notice Blacklists a swap path for a token pair with a specific fee tier
|
|
57
|
+
* @param path The encoded swap path (tokenIn, fee, tokenOut)
|
|
58
|
+
* @dev Only callable by accounts with WHITELIST_MANAGER_ROLE
|
|
59
|
+
* @dev Emits PathBlacklisted event
|
|
60
|
+
*/
|
|
61
|
+
function blacklistPath(bytes calldata path) external;
|
|
62
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import {ISwapAdapter} from "@shift-defi/core/interfaces/ISwapAdapter.sol";
|
|
5
|
+
|
|
6
|
+
interface IWmonUsdt0MonadAdapter is ISwapAdapter {
|
|
7
|
+
error InvalidTokenIn(address tokenIn, address expectedTokenIn);
|
|
8
|
+
error InvalidTokenOut(address tokenOut, address expectedTokenOut);
|
|
9
|
+
error PartialFill(uint256 amountIn, uint256 amountInSpent);
|
|
10
|
+
error UnexpectedNativeSender(address sender);
|
|
11
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shift-defi/swap-adapters",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Swap adapters for Shift DeFi Platform",
|
|
5
|
+
"author": "ShiftDeFi",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ShiftDeFi/shift-defi-swap-adapters"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://shiftdefi.com/",
|
|
12
|
+
"files": [
|
|
13
|
+
"contracts/**/*.sol",
|
|
14
|
+
"!contracts/UniversalAdapter.sol",
|
|
15
|
+
"!contracts/interfaces/IUniversalAdapter.sol",
|
|
16
|
+
"!contracts/CustomPool.sol",
|
|
17
|
+
"!contracts/interfaces/ICustomPool.sol",
|
|
18
|
+
"!contracts/1inch",
|
|
19
|
+
"!contracts/uniswap-v3",
|
|
20
|
+
"!contracts/dependencies/uniswap-v3/IUniswapV3Factory.sol"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@shift-defi/core": "0.7.1"
|
|
24
|
+
}
|
|
25
|
+
}
|