This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
RESDLTokenBridge.sol
237 lines (211 loc) · 8.3 KB
/
RESDLTokenBridge.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol";
import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../interfaces/ISDLPool.sol";
import "../interfaces/ISDLPoolCCIPController.sol";
/**
* @title reSDL Token Bridge
* @notice Handles CCIP transfers of reSDL NFTs
*/
contract RESDLTokenBridge is Ownable {
using SafeERC20 for IERC20;
IERC20 public linkToken;
IERC20 public sdlToken;
ISDLPool public sdlPool;
ISDLPoolCCIPController public sdlPoolCCIPController;
mapping(uint64 => bytes) public extraArgsByChain;
event TokenTransferred(
bytes32 indexed messageId,
uint64 indexed destinationChainSelector,
address indexed sender,
address receiver,
uint256 tokenId,
address feeToken,
uint256 fees
);
event TokenReceived(
bytes32 indexed messageId,
uint64 indexed sourceChainSelector,
address indexed sender,
address receiver,
uint256 tokenId
);
event SetExtraArgs(uint64 indexed chainSelector, bytes extraArgs);
error InsufficientFee();
error TransferFailed();
error FeeExceedsLimit();
error SenderNotAuthorized();
error InvalidReceiver();
error InvalidMsgValue();
/**
* @notice Initializes the contract
* @param _linkToken address of the LINK token
* @param _sdlToken address of the SDL token
* @param _sdlPool address of the SDL Pool
* @param _sdlPoolCCIPController address of the SDL Pool CCIP controller
**/
constructor(
address _linkToken,
address _sdlToken,
address _sdlPool,
address _sdlPoolCCIPController
) {
linkToken = IERC20(_linkToken);
sdlToken = IERC20(_sdlToken);
sdlPool = ISDLPool(_sdlPool);
sdlPoolCCIPController = ISDLPoolCCIPController(_sdlPoolCCIPController);
}
modifier onlySDLPoolCCIPController() {
if (msg.sender != address(sdlPoolCCIPController)) revert SenderNotAuthorized();
_;
}
/**
* @notice Transfers an reSDL token to a destination chain
* @param _destinationChainSelector id of destination chain
* @param _receiver address to receive reSDL on destination chain
* @param _tokenId id of reSDL token
* @param _payNative whether fee should be paid natively or with LINK
* @param _maxLINKFee call will revert if LINK fee exceeds this value
**/
function transferRESDL(
uint64 _destinationChainSelector,
address _receiver,
uint256 _tokenId,
bool _payNative,
uint256 _maxLINKFee
) external payable returns (bytes32 messageId) {
if (msg.sender != sdlPool.ownerOf(_tokenId)) revert SenderNotAuthorized();
if (_receiver == address(0)) revert InvalidReceiver();
if (_payNative == false && msg.value != 0) revert InvalidMsgValue();
(address destination, ISDLPool.RESDLToken memory reSDLToken) = sdlPoolCCIPController.handleOutgoingRESDL(
_destinationChainSelector,
msg.sender,
_tokenId
);
bytes memory extraArgs = extraArgsByChain[_destinationChainSelector];
Client.EVM2AnyMessage memory evm2AnyMessage = _buildCCIPMessage(
_receiver,
_tokenId,
reSDLToken,
destination,
_payNative ? address(0) : address(linkToken),
extraArgs
);
uint256 fees = IRouterClient(sdlPoolCCIPController.getRouter()).getFee(_destinationChainSelector, evm2AnyMessage);
if (_payNative) {
if (fees > msg.value) revert InsufficientFee();
messageId = sdlPoolCCIPController.ccipSend{value: fees}(_destinationChainSelector, evm2AnyMessage);
if (fees < msg.value) {
(bool success, ) = msg.sender.call{value: msg.value - fees}("");
if (!success) revert TransferFailed();
}
} else {
if (fees > _maxLINKFee) revert FeeExceedsLimit();
linkToken.safeTransferFrom(msg.sender, address(sdlPoolCCIPController), fees);
messageId = sdlPoolCCIPController.ccipSend(_destinationChainSelector, evm2AnyMessage);
}
emit TokenTransferred(
messageId,
_destinationChainSelector,
msg.sender,
_receiver,
_tokenId,
_payNative ? address(0) : address(linkToken),
fees
);
}
/**
* @notice Returns the current fee for an reSDL transfer
* @param _destinationChainSelector id of destination chain
* @param _payNative whether fee should be paid natively or with LINK
* @return fee current fee
**/
function getFee(uint64 _destinationChainSelector, bool _payNative) external view returns (uint256) {
Client.EVM2AnyMessage memory evm2AnyMessage = _buildCCIPMessage(
address(this),
0,
ISDLPool.RESDLToken(0, 0, 0, 0, 0),
address(this),
_payNative ? address(0) : address(linkToken),
extraArgsByChain[_destinationChainSelector]
);
return IRouterClient(sdlPoolCCIPController.getRouter()).getFee(_destinationChainSelector, evm2AnyMessage);
}
/**
* @notice Sets the extra args used for sending reSDL to a chain
* @param _chainSelector id of chain
* @param _extraArgs extra args as defined in CCIP API
**/
function setExtraArgs(uint64 _chainSelector, bytes calldata _extraArgs) external onlyOwner {
extraArgsByChain[_chainSelector] = _extraArgs;
emit SetExtraArgs(_chainSelector, _extraArgs);
}
/**
* @notice Processes a received message
* @dev handles incoming reSDL transfers
* @param _message CCIP message
**/
function ccipReceive(Client.Any2EVMMessage memory _message) external onlySDLPoolCCIPController {
address sender = abi.decode(_message.sender, (address));
(
address receiver,
uint256 tokenId,
uint256 amount,
uint256 boostAmount,
uint64 startTime,
uint64 duration,
uint64 expiry
) = abi.decode(_message.data, (address, uint256, uint256, uint256, uint64, uint64, uint64));
sdlPoolCCIPController.handleIncomingRESDL(
_message.sourceChainSelector,
receiver,
tokenId,
ISDLPool.RESDLToken(amount, boostAmount, startTime, duration, expiry)
);
emit TokenReceived(_message.messageId, _message.sourceChainSelector, sender, receiver, tokenId);
}
/**
* @notice Builds a CCIP message
* @dev builds the message for outgoing reSDL transfers
* @param _receiver address to receive reSDL token on destination chain
* @param _tokenId id of reSDL token
* @param _reSDLToken reSDL token
* @param _destination address of destination contract
* @param _feeTokenAddress address of token that fees will be paid in
* @param _extraArgs encoded args as defined in CCIP API
**/
function _buildCCIPMessage(
address _receiver,
uint256 _tokenId,
ISDLPool.RESDLToken memory _reSDLToken,
address _destination,
address _feeTokenAddress,
bytes memory _extraArgs
) internal view returns (Client.EVM2AnyMessage memory) {
Client.EVMTokenAmount[] memory tokenAmounts = new Client.EVMTokenAmount[](1);
Client.EVMTokenAmount memory tokenAmount = Client.EVMTokenAmount({
token: address(sdlToken),
amount: _reSDLToken.amount
});
tokenAmounts[0] = tokenAmount;
Client.EVM2AnyMessage memory evm2AnyMessage = Client.EVM2AnyMessage({
receiver: abi.encode(_destination),
data: abi.encode(
_receiver,
_tokenId,
_reSDLToken.amount,
_reSDLToken.boostAmount,
_reSDLToken.startTime,
_reSDLToken.duration,
_reSDLToken.expiry
),
tokenAmounts: tokenAmounts,
extraArgs: _extraArgs,
feeToken: _feeTokenAddress
});
return evm2AnyMessage;
}
}