-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossChainVaultBridge.sol
More file actions
442 lines (369 loc) · 16.2 KB
/
CrossChainVaultBridge.sol
File metadata and controls
442 lines (369 loc) · 16.2 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {Client, IRouterClient, CCIPReceiver} from "./interfaces/ICCIPRouter.sol";
/**
* @title CrossChainVaultBridge
* @author Yault
* @notice Chainlink CCIP bridge for cross-chain vault operations.
*
* @dev Enables:
* 1. Cross-chain attestation relay — relay release attestations from one chain to another
* 2. Cross-chain portfolio sync — sync vault position data across chains
* 3. Cross-chain deposit — deposit on chain A, receive vault shares on chain B
*
* Message types:
* - ATTESTATION_RELAY: Forward a release attestation to a remote chain
* - POSITION_SYNC: Broadcast portfolio position data to remote trackers
* - DEPOSIT_INTENT: Signal intent to deposit on remote chain
*
* Security:
* - Only whitelisted source chains and senders
* - Replay protection via nonces
* - Rate limiting on message sends
*/
contract CrossChainVaultBridge is Ownable, ReentrancyGuard, CCIPReceiver {
using SafeERC20 for IERC20;
// -----------------------------------------------------------------------
// Message Types
// -----------------------------------------------------------------------
uint8 public constant MSG_ATTESTATION_RELAY = 1;
uint8 public constant MSG_POSITION_SYNC = 2;
uint8 public constant MSG_DEPOSIT_INTENT = 3;
// -----------------------------------------------------------------------
// Types
// -----------------------------------------------------------------------
struct CrossChainMessage {
uint8 msgType;
bytes32 nonce;
bytes payload;
}
struct AttestationPayload {
bytes32 walletIdHash;
uint256 recipientIndex;
uint8 decision;
bytes32 evidenceHash;
}
struct PositionPayload {
address user;
address vault;
uint256 shares;
uint256 assetsUnderlying;
uint256 valueUSD;
uint256 timestamp;
}
struct RemoteChainConfig {
address remoteBridge; // Bridge contract on the remote chain
bool allowed;
uint256 lastMessageTime;
}
// -----------------------------------------------------------------------
// State
// -----------------------------------------------------------------------
/// @notice Chainlink CCIP Router.
IRouterClient public immutable ccipRouter;
/// @notice LINK token for paying CCIP fees (address(0) = pay in native).
address public linkToken;
/// @notice Remote chain configurations (chainSelector => config).
mapping(uint64 => RemoteChainConfig) public remoteChains;
/// @notice Supported remote chain selectors (for iteration).
uint64[] public supportedChains;
/// @notice Processed message nonces (replay protection).
mapping(bytes32 => bool) public processedNonces;
/// @notice Monotonic nonce for outgoing messages.
uint256 public outgoingNonce;
/// @notice Deployment timestamp for nonce uniqueness across redeployments.
uint256 public immutable deployedAt;
/// @notice Minimum interval between messages to the same chain (rate limiting).
uint256 public minMessageInterval = 60; // 1 minute
/// @notice Gas limit for CCIP messages.
uint256 public ccipGasLimit = 200_000;
// -----------------------------------------------------------------------
// Events
// -----------------------------------------------------------------------
event MessageSent(
bytes32 indexed messageId,
uint64 indexed destChainSelector,
uint8 msgType,
bytes32 nonce
);
event MessageReceived(
bytes32 indexed messageId,
uint64 indexed sourceChainSelector,
uint8 msgType,
bytes32 nonce
);
event AttestationRelayed(
bytes32 indexed walletIdHash,
uint256 recipientIndex,
uint8 decision,
uint64 sourceChain
);
event PositionSynced(
address indexed user,
address indexed vault,
uint256 valueUSD,
uint64 sourceChain
);
event RemoteChainConfigured(uint64 indexed chainSelector, address remoteBridge, bool allowed);
// -----------------------------------------------------------------------
// Errors
// -----------------------------------------------------------------------
error ZeroAddress();
error ChainNotSupported(uint64 chainSelector);
error UnauthorizedSender(uint64 sourceChain, address sender);
error MessageAlreadyProcessed(bytes32 nonce);
error RateLimitExceeded(uint64 chainSelector);
error InvalidMessageType(uint8 msgType);
error InsufficientFee(uint256 required, uint256 provided);
error OnlyRouter();
error ETHTransferFailed();
error MinIntervalTooLow();
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
constructor(
address initialOwner,
address _ccipRouter,
address _linkToken
) Ownable(initialOwner) {
if (_ccipRouter == address(0)) revert ZeroAddress();
ccipRouter = IRouterClient(_ccipRouter);
linkToken = _linkToken;
deployedAt = block.timestamp;
}
// -----------------------------------------------------------------------
// Admin: Chain Configuration
// -----------------------------------------------------------------------
/// @notice Configure a remote chain for cross-chain messaging.
function configureRemoteChain(
uint64 chainSelector,
address remoteBridge,
bool allowed
) external onlyOwner {
if (remoteBridge == address(0)) revert ZeroAddress();
bool wasAllowed = remoteChains[chainSelector].allowed
&& remoteChains[chainSelector].remoteBridge != address(0);
remoteChains[chainSelector] = RemoteChainConfig({
remoteBridge: remoteBridge,
allowed: allowed,
lastMessageTime: 0
});
if (allowed && !wasAllowed) {
// Add to supportedChains on first enable or re-enable after disable
supportedChains.push(chainSelector);
} else if (!allowed && wasAllowed) {
// SC-M-08 FIX: Remove disabled chain from supportedChains (swap-and-pop)
for (uint256 i; i < supportedChains.length;) {
if (supportedChains[i] == chainSelector) {
supportedChains[i] = supportedChains[supportedChains.length - 1];
supportedChains.pop();
break;
}
unchecked { ++i; }
}
}
emit RemoteChainConfigured(chainSelector, remoteBridge, allowed);
}
/// @notice Update the CCIP gas limit.
function setCcipGasLimit(uint256 gasLimit) external onlyOwner {
ccipGasLimit = gasLimit;
}
/// @notice Update the minimum message interval (minimum 10 seconds).
function setMinMessageInterval(uint256 interval) external onlyOwner {
if (interval < 10) revert MinIntervalTooLow();
minMessageInterval = interval;
}
// -----------------------------------------------------------------------
// Send: Attestation Relay
// -----------------------------------------------------------------------
/// @notice Relay a release attestation to a remote chain via CCIP.
function relayAttestation(
uint64 destChainSelector,
bytes32 walletIdHash,
uint256 recipientIndex,
uint8 decision,
bytes32 evidenceHash
) external onlyOwner nonReentrant returns (bytes32 messageId) {
_validateDestination(destChainSelector);
AttestationPayload memory payload = AttestationPayload({
walletIdHash: walletIdHash,
recipientIndex: recipientIndex,
decision: decision,
evidenceHash: evidenceHash
});
bytes32 nonce = _nextNonce();
CrossChainMessage memory ccMsg = CrossChainMessage({
msgType: MSG_ATTESTATION_RELAY,
nonce: nonce,
payload: abi.encode(payload)
});
messageId = _sendCCIPMessage(destChainSelector, abi.encode(ccMsg));
emit MessageSent(messageId, destChainSelector, MSG_ATTESTATION_RELAY, nonce);
}
// -----------------------------------------------------------------------
// Send: Position Sync
// -----------------------------------------------------------------------
/// @notice Sync a user's vault position to remote chains.
function syncPosition(
uint64 destChainSelector,
address user,
address vault,
uint256 valueUSD
) external onlyOwner nonReentrant returns (bytes32 messageId) {
_validateDestination(destChainSelector);
IERC4626 v = IERC4626(vault);
uint256 shares = v.balanceOf(user);
uint256 assets = shares > 0 ? v.convertToAssets(shares) : 0;
PositionPayload memory payload = PositionPayload({
user: user,
vault: vault,
shares: shares,
assetsUnderlying: assets,
valueUSD: valueUSD,
timestamp: block.timestamp
});
bytes32 nonce = _nextNonce();
CrossChainMessage memory ccMsg = CrossChainMessage({
msgType: MSG_POSITION_SYNC,
nonce: nonce,
payload: abi.encode(payload)
});
messageId = _sendCCIPMessage(destChainSelector, abi.encode(ccMsg));
emit MessageSent(messageId, destChainSelector, MSG_POSITION_SYNC, nonce);
}
// -----------------------------------------------------------------------
// Receive: CCIP Message Handler
// -----------------------------------------------------------------------
/// @notice Called by the CCIP router when a message is received.
function ccipReceive(Client.Any2EVMMessage calldata message) external override {
if (msg.sender != address(ccipRouter)) revert OnlyRouter();
uint64 sourceChain = message.sourceChainSelector;
address sender = abi.decode(message.sender, (address));
// Verify the sender is an authorized remote bridge
RemoteChainConfig storage config = remoteChains[sourceChain];
if (!config.allowed || config.remoteBridge != sender) {
revert UnauthorizedSender(sourceChain, sender);
}
CrossChainMessage memory ccMsg = abi.decode(message.data, (CrossChainMessage));
// Replay protection
if (processedNonces[ccMsg.nonce]) {
revert MessageAlreadyProcessed(ccMsg.nonce);
}
processedNonces[ccMsg.nonce] = true;
if (ccMsg.msgType == MSG_ATTESTATION_RELAY) {
_handleAttestationRelay(ccMsg.payload, sourceChain);
} else if (ccMsg.msgType == MSG_POSITION_SYNC) {
_handlePositionSync(ccMsg.payload, sourceChain);
} else {
revert InvalidMessageType(ccMsg.msgType);
}
emit MessageReceived(message.messageId, sourceChain, ccMsg.msgType, ccMsg.nonce);
}
// -----------------------------------------------------------------------
// View
// -----------------------------------------------------------------------
/// @notice Get the fee for sending a CCIP message to a destination chain.
function getMessageFee(uint64 destChainSelector, bytes calldata data)
external
view
returns (uint256)
{
Client.EVM2AnyMessage memory ccipMessage = _buildCCIPMessage(
destChainSelector, data
);
return ccipRouter.getFee(destChainSelector, ccipMessage);
}
/// @notice Get all supported chain selectors.
function getSupportedChains() external view returns (uint64[] memory) {
return supportedChains;
}
// -----------------------------------------------------------------------
// Internal
// -----------------------------------------------------------------------
function _validateDestination(uint64 destChainSelector) internal {
RemoteChainConfig storage config = remoteChains[destChainSelector];
if (!config.allowed) revert ChainNotSupported(destChainSelector);
// Rate limiting
if (block.timestamp < config.lastMessageTime + minMessageInterval) {
revert RateLimitExceeded(destChainSelector);
}
config.lastMessageTime = block.timestamp;
}
/// @dev SC-M-03 FIX: Include deployedAt to prevent nonce collision across redeployments.
function _nextNonce() internal returns (bytes32) {
outgoingNonce++;
return keccak256(abi.encodePacked(block.chainid, address(this), deployedAt, outgoingNonce));
}
function _buildCCIPMessage(uint64 destChainSelector, bytes memory data)
internal
view
returns (Client.EVM2AnyMessage memory)
{
RemoteChainConfig storage config = remoteChains[destChainSelector];
Client.EVMTokenAmount[] memory tokenAmounts = new Client.EVMTokenAmount[](0);
return Client.EVM2AnyMessage({
receiver: abi.encode(config.remoteBridge),
data: data,
tokenAmounts: tokenAmounts,
/// @dev SC-L-08 FIX: Use CCIP-standard extraArgs encoding.
extraArgs: Client._argsToBytes(Client.EVMExtraArgsV1({gasLimit: ccipGasLimit})),
feeToken: linkToken
});
}
function _sendCCIPMessage(uint64 destChainSelector, bytes memory data)
internal
returns (bytes32)
{
Client.EVM2AnyMessage memory ccipMessage = _buildCCIPMessage(
destChainSelector, data
);
uint256 fee = ccipRouter.getFee(destChainSelector, ccipMessage);
if (linkToken != address(0)) {
// SC-C-02 FIX: Use forceApprove for safe approval handling
IERC20(linkToken).forceApprove(address(ccipRouter), fee);
return ccipRouter.ccipSend(destChainSelector, ccipMessage);
} else {
return ccipRouter.ccipSend{value: fee}(destChainSelector, ccipMessage);
}
}
/// @dev SC-H-03 FIX: Store relayed attestations on-chain for downstream consumers.
mapping(bytes32 => mapping(uint256 => AttestationPayload)) public relayedAttestations;
function _handleAttestationRelay(bytes memory payload, uint64 sourceChain) internal {
AttestationPayload memory att = abi.decode(payload, (AttestationPayload));
// Store the relayed attestation for downstream contracts to query
relayedAttestations[att.walletIdHash][att.recipientIndex] = att;
emit AttestationRelayed(
att.walletIdHash,
att.recipientIndex,
att.decision,
sourceChain
);
}
/// @dev SC-I-06 FIX: Store synced positions on-chain.
mapping(address => mapping(address => PositionPayload)) public syncedPositions;
function _handlePositionSync(bytes memory payload, uint64 sourceChain) internal {
PositionPayload memory pos = abi.decode(payload, (PositionPayload));
syncedPositions[pos.user][pos.vault] = pos;
emit PositionSynced(pos.user, pos.vault, pos.valueUSD, sourceChain);
}
/// @notice Withdraw stuck ETH from the contract.
function withdrawETH(address payable to, uint256 amount) external onlyOwner {
if (to == address(0)) revert ZeroAddress();
(bool ok,) = to.call{value: amount}("");
// SC-L-02 FIX: Use custom error instead of require string
if (!ok) revert ETHTransferFailed();
}
/// @notice Withdraw stuck LINK (or any ERC-20) from the contract.
/// @dev SC-C-01 FIX: Use safeTransfer for non-standard ERC-20 compatibility.
function withdrawToken(address token, address to, uint256 amount) external onlyOwner {
if (to == address(0)) revert ZeroAddress();
IERC20(token).safeTransfer(to, amount);
}
/// @notice Accept native token for CCIP fees.
receive() external payable {}
}