-
Notifications
You must be signed in to change notification settings - Fork 10
/
ConnextRouter.sol
219 lines (192 loc) · 7.84 KB
/
ConnextRouter.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.15;
/**
* @title ConnextRouter.
* @author Fujidao Labs
* @notice A Router implementing Connext specific bridging logic.
*/
import {SafeERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {IConnext, IXReceiver} from "../interfaces/connext/IConnext.sol";
import {BaseRouter} from "../abstracts/BaseRouter.sol";
import {IWETH9} from "../abstracts/WETH9.sol";
import {IVault} from "../interfaces/IVault.sol";
import {IChief} from "../interfaces/IChief.sol";
contract ConnextRouter is BaseRouter, IXReceiver {
/**
* @notice Emitted when a new destination router gets added.
* @param router - The router on another chain.
* @param domain - The destination domain identifier according Connext nomenclature.
*/
event NewRouterAdded(address indexed router, uint256 indexed domain);
/**
* @notice Emitted when Connext `xCall` is invoked.
* @param transferId - The unique identifier of the crosschain transfer.
* @param caller - The account that called the function.
* @param receiver - The router on destDomain.
* @param destDomain - The destination domain identifier according Connext nomenclature.
* @param asset - The asset being transferred.
* @param amount - The amount of transferring asset the recipient address receives.
* @param callData - The calldata sent to destination router that will get decoded and executed.
*/
event XCalled(
bytes32 indexed transferId,
address indexed caller,
address indexed receiver,
uint256 destDomain,
address asset,
uint256 amount,
bytes callData
);
/**
* @notice Emitted when the router receives a cross-chain call.
* @param transferId - The unique identifier of the crosschain transfer.
* @param originDomain - The origin domain identifier according Connext nomenclature.
* @param success - Whether or not the xBundle call succeeds.
* @param asset - The asset being transferred.
* @param amount - The amount of transferring asset the recipient address receives.
* @param callData - The calldata that will get decoded and executed.
*/
event XReceived(
bytes32 indexed transferId,
uint256 indexed originDomain,
bool success,
address asset,
uint256 amount,
bytes callData
);
/// @dev Custom Errors
error ConnextRouter__setRouter_invalidInput();
error ConnextRouter__xReceive_notReceivedAssetBalance();
error ConnextRouter__xReceive_notAllowedCrossCaller();
// The connext contract on the origin domain.
IConnext public immutable connext;
// ref: https://docs.nomad.xyz/developers/environments/domain-chain-ids
mapping(uint256 => address) public routerByDomain;
constructor(IWETH9 weth, IConnext connext_, IChief chief) BaseRouter(weth, chief) {
connext = connext_;
_allowCrossCaller(address(connext_), true);
}
// Connext specific functions
/**
* @notice Called by Connext on the destination chain. It doesn't perform an authentification
* by doing a check on `originSender`. As a result of that, all txns go through the fast path.
* If `xBundle` fails on our side, this contract will keep the custody of the sent funds.
*
* @param transferId - The unique identifier of the crosschain transfer.
* @param amount - The amount of transferring asset the recipient address receives.
* @param asset - The asset being transferred.
* @param originDomain - The origin domain identifier according Connext nomenclature.
* @param callData - The calldata that will get decoded and executed.
*/
function xReceive(
bytes32 transferId,
uint256 amount,
address asset,
address, /* originSender */
uint32 originDomain,
bytes memory callData
)
external
returns (bytes memory)
{
(Action[] memory actions, bytes[] memory args) = abi.decode(callData, (Action[], bytes[]));
// Block callers except allowed cross callers.
if (!_isAllowedCrossCaller[msg.sender]) {
revert ConnextRouter__xReceive_notAllowedCrossCaller();
}
// Ensure that at this entry point expected `asset` `amount` is received.
if (IERC20(asset).balanceOf(address(this)) < amount) {
revert ConnextRouter__xReceive_notReceivedAssetBalance();
} else {
_tokenList.push(asset);
BalanceChecker memory checkedToken =
BalanceChecker(asset, IERC20(asset).balanceOf(address(this)) - amount);
_tokensToCheck.push(checkedToken);
}
try this.xBundle(actions, args) {
emit XReceived(transferId, originDomain, true, asset, amount, callData);
} catch {
// else keep funds in router and let them be handled by admin
emit XReceived(transferId, originDomain, false, asset, amount, callData);
}
return "";
}
function _crossTransfer(bytes memory params) internal override {
(
uint256 destDomain,
uint256 slippage,
address asset,
uint256 amount,
address receiver,
address sender
) = abi.decode(params, (uint256, uint256, address, uint256, address, address));
_checkBeneficiary(receiver);
_safePullTokenFrom(asset, sender, receiver, amount);
_safeApprove(asset, address(connext), amount);
bytes32 transferId = connext.xcall(
// _destination: Domain ID of the destination chain
uint32(destDomain),
// _to: address of the target contract
receiver,
// _asset: address of the token contract
asset,
// _delegate: address that has rights to update the original slippage tolerance
// by calling Connext's forceUpdateSlippage function
msg.sender,
// _amount: amount of tokens to transfer
amount,
// _slippage: can be anything between 0-10000 becaus
// the maximum amount of slippage the user will accept in BPS, 30 == 0.3%
slippage,
// _callData: empty because we're only sending funds
""
);
emit XCalled(transferId, msg.sender, receiver, destDomain, asset, amount, "");
}
function _crossTransferWithCalldata(bytes memory params) internal override {
/// TODO this action requires beneficiary check, though implementation from BaseRouter
/// is not feasible.
(uint256 destDomain, uint256 slippage, address asset, uint256 amount, bytes memory callData) =
abi.decode(params, (uint256, uint256, address, uint256, bytes));
_safePullTokenFrom(asset, msg.sender, msg.sender, amount);
_safeApprove(asset, address(connext), amount);
bytes32 transferId = connext.xcall(
// _destination: Domain ID of the destination chain
uint32(destDomain),
// _to: address of the target contract
routerByDomain[destDomain],
// _asset: address of the token contract
asset,
// _delegate: address that can revert or forceLocal on destination
msg.sender,
// _amount: amount of tokens to transfer
amount,
// _slippage: can be anything between 0-10000 becaus
// the maximum amount of slippage the user will accept in BPS, 30 == 0.3%
slippage,
// _callData: the encoded calldata to send
callData
);
emit XCalled(
transferId, msg.sender, routerByDomain[destDomain], destDomain, asset, amount, callData
);
}
/**
* @notice Anyone can call this function on the origin domain to increase the relayer fee for a transfer.
* @param transferId - The unique identifier of the crosschain transaction
*/
function bumpTransfer(bytes32 transferId) external payable {
connext.bumpTransfer{value: msg.value}(transferId);
}
///////////////////////
/// Admin functions ///
///////////////////////
function setRouter(uint256 domain, address router) external onlyTimelock {
if (router == address(0)) {
revert ConnextRouter__setRouter_invalidInput();
}
routerByDomain[domain] = router;
emit NewRouterAdded(router, domain);
}
}