-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRootBridgeAgent.sol
1335 lines (1100 loc) · 51.8 KB
/
RootBridgeAgent.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Ownable} from "solady/auth/Ownable.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {SafeCastLib} from "solady/utils/SafeCastLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {WETH9} from "./interfaces/IWETH9.sol";
import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import {AnycallFlags} from "./lib/AnycallFlags.sol";
import {IAnycallProxy} from "./interfaces/IAnycallProxy.sol";
import {IAnycallConfig} from "./interfaces/IAnycallConfig.sol";
import {IAnycallExecutor} from "./interfaces/IAnycallExecutor.sol";
import {IApp, IRootBridgeAgent} from "./interfaces/IRootBridgeAgent.sol";
import {IBranchBridgeAgent} from "./interfaces/IBranchBridgeAgent.sol";
import {IERC20hTokenRoot} from "./interfaces/IERC20hTokenRoot.sol";
import {IRootPort as IPort} from "./interfaces/IRootPort.sol";
import {IRootRouter as IRouter} from "./interfaces/IRootRouter.sol";
import {VirtualAccount} from "./VirtualAccount.sol";
import {
IRootBridgeAgent,
DepositParams,
DepositMultipleParams,
Settlement,
SettlementStatus,
SettlementParams,
SettlementMultipleParams,
UserFeeInfo,
SwapCallbackData
} from "./interfaces/IRootBridgeAgent.sol";
import {DeployRootBridgeAgentExecutor, RootBridgeAgentExecutor} from "./RootBridgeAgentExecutor.sol";
/// @title Library for Cross Chain Deposit Parameters Validation.
library CheckParamsLib {
/**
* @notice Function to check cross-chain deposit parameters and verify deposits made on branch chain are valid.
* @param _localPortAddress Address of local Port.
* @param _dParams Cross Chain swap parameters.
* @param _fromChain Chain ID of the chain where the deposit was made.
* @dev Local hToken must be recognized and address must match underlying if exists otherwise only local hToken is checked.
*
*/
function checkParams(address _localPortAddress, DepositParams memory _dParams, uint24 _fromChain)
internal
view
returns (bool)
{
if (
(_dParams.amount < _dParams.deposit) //Deposit can't be greater than amount.
|| (_dParams.amount > 0 && !IPort(_localPortAddress).isLocalToken(_dParams.hToken, _fromChain)) //Check local exists.
|| (_dParams.deposit > 0 && !IPort(_localPortAddress).isUnderlyingToken(_dParams.token, _fromChain)) //Check underlying exists.
) {
return false;
}
return true;
}
}
/// @title Library for Root Bridge Agent Deployment.
library DeployRootBridgeAgent {
function deploy(
WETH9 _wrappedNativeToken,
uint24 _localChainId,
address _daoAddress,
address _localAnyCallAddress,
address _localAnyCallExecutorAddress,
address _localPortAddress,
address _localRouterAddress
) external returns (RootBridgeAgent) {
return new RootBridgeAgent(
_wrappedNativeToken,
_localChainId,
_daoAddress,
_localAnyCallAddress,
_localAnyCallExecutorAddress,
_localPortAddress,
_localRouterAddress
);
}
}
/// @title Root Bridge Agent Contract
contract RootBridgeAgent is IRootBridgeAgent {
using SafeTransferLib for address;
using SafeCastLib for uint256;
/*///////////////////////////////////////////////////////////////
ENCODING CONSTS
//////////////////////////////////////////////////////////////*/
/// AnyExec Consts
uint8 internal constant PARAMS_START = 1;
uint8 internal constant PARAMS_START_SIGNED = 21;
uint8 internal constant PARAMS_ADDRESS_SIZE = 20;
uint8 internal constant PARAMS_GAS_IN = 32;
uint8 internal constant PARAMS_GAS_OUT = 16;
/// BridgeIn Consts
uint8 internal constant PARAMS_TKN_START = 5;
uint8 internal constant PARAMS_AMT_OFFSET = 64;
uint8 internal constant PARAMS_DEPOSIT_OFFSET = 96;
/*///////////////////////////////////////////////////////////////
ROOT BRIDGE AGENT STATE
//////////////////////////////////////////////////////////////*/
/// @notice Local Chain Id
uint24 public immutable localChainId;
/// @notice Local Wrapped Native Token
WETH9 public immutable wrappedNativeToken;
/// @notice Bridge Agent Factory Address.
address public immutable factoryAddress;
/// @notice Address of DAO.
address public immutable daoAddress;
/// @notice Local Core Root Router Address
address public immutable localRouterAddress;
/// @notice Address for Local Port Address where funds deposited from this chain are stored.
address public immutable localPortAddress;
/// @notice Local Anycall Address
address public immutable localAnyCallAddress;
/// @notice Local Anyexec Address
address public immutable localAnyCallExecutorAddress;
/// @notice Address of Root Bridge Agent Executor.
address public immutable bridgeAgentExecutorAddress;
/*///////////////////////////////////////////////////////////////
BRANCH BRIDGE AGENTS STATE
//////////////////////////////////////////////////////////////*/
/// @notice Chain -> Branch Bridge Agent Address. For N chains, each Root Bridge Agent Address has M =< N Branch Bridge Agent Address.
mapping(uint256 => address) public getBranchBridgeAgent;
/// @notice If true, bridge agent manager has allowed for a new given branch bridge agent to be synced/added.
mapping(uint256 => bool) public isBranchBridgeAgentAllowed;
/*///////////////////////////////////////////////////////////////
SETTLEMENTS STATE
//////////////////////////////////////////////////////////////*/
/// @notice Deposit nonce used for identifying transaction.
uint32 public settlementNonce;
/// @notice Mapping from Settlement nonce to Deposit Struct.
mapping(uint32 => Settlement) public getSettlement;
/*///////////////////////////////////////////////////////////////
EXECUTOR STATE
//////////////////////////////////////////////////////////////*/
/// @notice If true, bridge agent has already served a request with this nonce from a given chain. Chain -> Nonce -> Bool
mapping(uint256 => mapping(uint32 => bool)) public executionHistory;
/*///////////////////////////////////////////////////////////////
GAS MANAGEMENT STATE
//////////////////////////////////////////////////////////////*/
uint256 internal constant MIN_FALLBACK_RESERVE = 155_000; // 100_000 for anycall + 55_000 for fallback
uint256 internal constant MIN_EXECUTION_OVERHEAD = 155_000; // 100_000 for anycall + 30_000 Pre 1st Gas Checkpoint Execution + 25_000 Post last Gas Checkpoint Execution
uint256 public initialGas;
UserFeeInfo public userFeeInfo;
/*///////////////////////////////////////////////////////////////
DAO STATE
//////////////////////////////////////////////////////////////*/
uint256 public accumulatedFees;
/**
* @notice Constructor for Bridge Agent.
* @param _wrappedNativeToken Local Wrapped Native Token.
* @param _daoAddress Address of DAO.
* @param _localChainId Local Chain Id.
* @param _localAnyCallAddress Local Anycall Address.
* @param _localPortAddress Local Port Address.
* @param _localRouterAddress Local Port Address.
*/
constructor(
WETH9 _wrappedNativeToken,
uint24 _localChainId,
address _daoAddress,
address _localAnyCallAddress,
address _localAnyCallExecutorAddress,
address _localPortAddress,
address _localRouterAddress
) {
require(address(_wrappedNativeToken) != address(0), "Wrapped native token cannot be zero address");
require(_daoAddress != address(0), "DAO cannot be zero address");
require(_localAnyCallAddress != address(0), "Anycall Address cannot be zero address");
require(_localAnyCallExecutorAddress != address(0), "Anycall Executor Address cannot be zero address");
require(_localPortAddress != address(0), "Port Address cannot be zero address");
require(_localRouterAddress != address(0), "Router Address cannot be zero address");
wrappedNativeToken = _wrappedNativeToken;
factoryAddress = msg.sender;
daoAddress = _daoAddress;
localChainId = _localChainId;
localAnyCallAddress = _localAnyCallAddress;
localPortAddress = _localPortAddress;
localRouterAddress = _localRouterAddress;
bridgeAgentExecutorAddress = DeployRootBridgeAgentExecutor.deploy(address(this));
localAnyCallExecutorAddress = _localAnyCallExecutorAddress;
settlementNonce = 1;
accumulatedFees = 1; //Avoid paying 20k gas in first `payExecutionGas` making MIN_EXECUTION_OVERHEAD constant.
}
/*///////////////////////////////////////////////////////////////
VIEW EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IRootBridgeAgent
function getSettlementEntry(uint32 _settlementNonce) external view returns (Settlement memory) {
return getSettlement[_settlementNonce];
}
/*///////////////////////////////////////////////////////////////
USER EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IRootBridgeAgent
function retrySettlement(uint32 _settlementNonce, uint128 _remoteExecutionGas) external payable {
//Update User Gas available.
if (initialGas == 0) {
userFeeInfo.depositedGas = uint128(msg.value);
userFeeInfo.gasToBridgeOut = _remoteExecutionGas;
}
//Clear Settlement with updated gas.
_retrySettlement(_settlementNonce);
}
/// @inheritdoc IRootBridgeAgent
function redeemSettlement(uint32 _depositNonce) external lock {
//Get deposit owner.
address depositOwner = getSettlement[_depositNonce].owner;
//Update Deposit
if (getSettlement[_depositNonce].status != SettlementStatus.Failed || depositOwner == address(0)) {
revert SettlementRedeemUnavailable();
} else if (
msg.sender != depositOwner && msg.sender != address(IPort(localPortAddress).getUserAccount(depositOwner))
) {
revert NotSettlementOwner();
}
_redeemSettlement(_depositNonce);
}
/*///////////////////////////////////////////////////////////////
ROOT ROUTER EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IRootBridgeAgent
function callOut(address _recipient, bytes memory _data, uint24 _toChain) external payable lock requiresRouter {
//Encode Data for call.
bytes memory data =
abi.encodePacked(bytes1(0x00), _recipient, settlementNonce++, _data, _manageGasOut(_toChain));
//Perform Call to clear hToken balance on destination branch chain.
_performCall(data, _toChain);
}
/// @inheritdoc IRootBridgeAgent
function callOutAndBridge(
address _owner,
address _recipient,
bytes memory _data,
address _globalAddress,
uint256 _amount,
uint256 _deposit,
uint24 _toChain
) external payable lock requiresRouter {
//Get destination Local Address from Global Address.
address localAddress = IPort(localPortAddress).getLocalTokenFromGlobal(_globalAddress, _toChain);
//Get destination Underlying Address from Local Address.
address underlyingAddress = IPort(localPortAddress).getUnderlyingTokenFromLocal(localAddress, _toChain);
//Check if valid assets
if (localAddress == address(0) || (underlyingAddress == address(0) && _deposit > 0)) {
revert InvalidInputParams();
}
//Prepare data for call
bytes memory data = abi.encodePacked(
bytes1(0x01),
_recipient,
settlementNonce,
localAddress,
underlyingAddress,
_amount,
_deposit,
_data,
_manageGasOut(_toChain)
);
//Update State to reflect bridgeOut
_updateStateOnBridgeOut(
msg.sender, _globalAddress, localAddress, underlyingAddress, _amount, _deposit, _toChain
);
//Create Settlement
_createSettlement(_owner, _recipient, localAddress, underlyingAddress, _amount, _deposit, data, _toChain);
//Perform Call to clear hToken balance on destination branch chain and perform call.
_performCall(data, _toChain);
}
/// @inheritdoc IRootBridgeAgent
function callOutAndBridgeMultiple(
address _owner,
address _recipient,
bytes memory _data,
address[] memory _globalAddresses,
uint256[] memory _amounts,
uint256[] memory _deposits,
uint24 _toChain
) external payable lock requiresRouter {
address[] memory hTokens = new address[](_globalAddresses.length);
address[] memory tokens = new address[](_globalAddresses.length);
for (uint256 i = 0; i < _globalAddresses.length;) {
//Populate Addresses for Settlement
hTokens[i] = IPort(localPortAddress).getLocalTokenFromGlobal(_globalAddresses[i], _toChain);
tokens[i] = IPort(localPortAddress).getUnderlyingTokenFromLocal(hTokens[i], _toChain);
if (hTokens[i] == address(0) || (tokens[i] == address(0) && _deposits[i] > 0)) revert InvalidInputParams();
_updateStateOnBridgeOut(
msg.sender, _globalAddresses[i], hTokens[i], tokens[i], _amounts[i], _deposits[i], _toChain
);
unchecked {
++i;
}
}
//Prepare data for call with settlement of multiple assets
bytes memory data = abi.encodePacked(
bytes1(0x02),
_recipient,
uint8(hTokens.length),
settlementNonce,
hTokens,
tokens,
_amounts,
_deposits,
_data,
_manageGasOut(_toChain)
);
//Create Settlement Balance
_createMultipleSettlement(_owner, _recipient, hTokens, tokens, _amounts, _deposits, data, _toChain);
//Perform Call to destination Branch Chain.
_performCall(data, _toChain);
}
/*///////////////////////////////////////////////////////////////
TOKEN MANAGEMENT EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IRootBridgeAgent
function bridgeIn(address _recipient, DepositParams memory _dParams, uint24 _fromChain)
public
requiresAgentExecutor
{
//Check Deposit info from Cross Chain Parameters.
if (!CheckParamsLib.checkParams(localPortAddress, _dParams, _fromChain)) {
revert InvalidInputParams();
}
//Get global address
address globalAddress = IPort(localPortAddress).getGlobalTokenFromLocal(_dParams.hToken, _fromChain);
//Check if valid asset
if (globalAddress == address(0)) revert InvalidInputParams();
//Move hTokens from Branch to Root + Mint Sufficient hTokens to match new port deposit
IPort(localPortAddress).bridgeToRoot(_recipient, globalAddress, _dParams.amount, _dParams.deposit, _fromChain);
}
/// @inheritdoc IRootBridgeAgent
function bridgeInMultiple(address _recipient, DepositMultipleParams memory _dParams, uint24 _fromChain)
external
requiresAgentExecutor
{
for (uint256 i = 0; i < _dParams.hTokens.length;) {
bridgeIn(
_recipient,
DepositParams({
hToken: _dParams.hTokens[i],
token: _dParams.tokens[i],
amount: _dParams.amounts[i],
deposit: _dParams.deposits[i],
toChain: _dParams.toChain,
depositNonce: 0
}),
_fromChain
);
unchecked {
++i;
}
}
}
/*///////////////////////////////////////////////////////////////
TOKEN MANAGEMENT INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Updates the token balance state by moving assets from root omnichain environment to branch chain, when a user wants to bridge out tokens from the root bridge agent chain.
* @param _sender address of the sender.
* @param _globalAddress address of the global token.
* @param _localAddress address of the local token.
* @param _underlyingAddress address of the underlying token.
* @param _amount amount of hTokens to be bridged out.
* @param _deposit amount of underlying tokens to be bridged out.
* @param _toChain chain to bridge to.
*/
function _updateStateOnBridgeOut(
address _sender,
address _globalAddress,
address _localAddress,
address _underlyingAddress,
uint256 _amount,
uint256 _deposit,
uint24 _toChain
) internal {
if (_amount - _deposit > 0) {
//Move output hTokens from Root to Branch
if (_localAddress == address(0)) revert UnrecognizedLocalAddress();
_globalAddress.safeTransferFrom(_sender, localPortAddress, _amount - _deposit);
}
if (_deposit > 0) {
//Verify there is enough balance to clear native tokens if needed
if (_underlyingAddress == address(0)) revert UnrecognizedUnderlyingAddress();
if (IERC20hTokenRoot(_globalAddress).getTokenBalance(_toChain) < _deposit) {
revert InsufficientBalanceForSettlement();
}
IPort(localPortAddress).burn(_sender, _globalAddress, _deposit, _toChain);
}
}
/*///////////////////////////////////////////////////////////////
SETTLEMENT INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Function to store a Settlement instance. Settlement should be reopened if fallback occurs.
* @param _owner settlement owner address.
* @param _recipient destination chain reciever address.
* @param _hToken deposited global token address.
* @param _token deposited global token address.
* @param _amount amounts of total hTokens + Tokens output.
* @param _deposit amount of underlying / native token to output.
* @param _callData calldata to execute on destination Router.
* @param _toChain Destination chain identificator.
*
*/
function _createSettlement(
address _owner,
address _recipient,
address _hToken,
address _token,
uint256 _amount,
uint256 _deposit,
bytes memory _callData,
uint24 _toChain
) internal {
//Cast to Dynamic
address[] memory hTokens = new address[](1);
hTokens[0] = _hToken;
address[] memory tokens = new address[](1);
tokens[0] = _token;
uint256[] memory amounts = new uint256[](1);
amounts[0] = _amount;
uint256[] memory deposits = new uint256[](1);
deposits[0] = _deposit;
//Call createSettlement
_createMultipleSettlement(_owner, _recipient, hTokens, tokens, amounts, deposits, _callData, _toChain);
}
/**
* @notice Function to create a settlemment. Settlement should be reopened if fallback occurs.
* @param _owner settlement owner address.
* @param _recipient destination chain reciever address.
* @param _hTokens deposited global token addresses.
* @param _tokens deposited global token addresses.
* @param _amounts amounts of total hTokens + Tokens output.
* @param _deposits amount of underlying / native tokens to output.
* @param _callData calldata to execute on destination Router.
* @param _toChain Destination chain identificator.
*
*
*/
function _createMultipleSettlement(
address _owner,
address _recipient,
address[] memory _hTokens,
address[] memory _tokens,
uint256[] memory _amounts,
uint256[] memory _deposits,
bytes memory _callData,
uint24 _toChain
) internal {
// Update State
getSettlement[_getAndIncrementSettlementNonce()] = Settlement({
owner: _owner,
recipient: _recipient,
hTokens: _hTokens,
tokens: _tokens,
amounts: _amounts,
deposits: _deposits,
callData: _callData,
toChain: _toChain,
status: SettlementStatus.Success,
gasToBridgeOut: userFeeInfo.gasToBridgeOut
});
}
/**
* @notice Function to retry a user's Settlement balance with a new amount of gas to bridge out of Root Bridge Agent's Omnichain Environment.
* @param _settlementNonce Identifier for token settlement.
*
*/
function _retrySettlement(uint32 _settlementNonce) internal returns (bool) {
//Get Settlement
Settlement memory settlement = getSettlement[_settlementNonce];
//Check if Settlement hasn't been redeemed.
if (settlement.owner == address(0)) return false;
//abi encodePacked
bytes memory newGas = abi.encodePacked(_manageGasOut(settlement.toChain));
//overwrite last 16bytes of callData
for (uint256 i = 0; i < newGas.length;) {
settlement.callData[settlement.callData.length - 16 + i] = newGas[i];
unchecked {
++i;
}
}
Settlement storage settlementReference = getSettlement[_settlementNonce];
//Update Gas To Bridge Out
settlementReference.gasToBridgeOut = userFeeInfo.gasToBridgeOut;
//Set Settlement Calldata to send to Branch Chain
settlementReference.callData = settlement.callData;
//Update Settlement Staus
settlementReference.status = SettlementStatus.Success;
//Retry call with additional gas
_performCall(settlement.callData, settlement.toChain);
//Retry Success
return true;
}
/**
* @notice Function to retry a user's Settlement balance.
* @param _settlementNonce Identifier for token settlement.
*
*/
function _redeemSettlement(uint32 _settlementNonce) internal {
// Get storage reference
Settlement storage settlement = getSettlement[_settlementNonce];
//Clear Global hTokens To Recipient on Root Chain cancelling Settlement to Branch
for (uint256 i = 0; i < settlement.hTokens.length;) {
//Check if asset
if (settlement.hTokens[i] != address(0)) {
//Move hTokens from Branch to Root + Mint Sufficient hTokens to match new port deposit
IPort(localPortAddress).bridgeToRoot(
msg.sender,
IPort(localPortAddress).getGlobalTokenFromLocal(settlement.hTokens[i], settlement.toChain),
settlement.amounts[i],
settlement.deposits[i],
settlement.toChain
);
}
unchecked {
++i;
}
}
// Delete Settlement
delete getSettlement[_settlementNonce];
}
/**
* @notice Function to reopen a user's Settlement balance as pending and thus retryable by users. Called upon anyFallback of triggered by Branch Bridge Agent.
* @param _settlementNonce Identifier for token settlement.
*
*/
function _reopenSettlemment(uint32 _settlementNonce) internal {
//Update Deposit
getSettlement[_settlementNonce].status = SettlementStatus.Failed;
}
/**
* @notice Function that returns Deposit nonce and increments nonce counter.
*
*/
function _getAndIncrementSettlementNonce() internal returns (uint32) {
return settlementNonce++;
}
/*///////////////////////////////////////////////////////////////
GAS SWAP INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
uint24 private constant GLOBAL_DIVISIONER = 1e6; // for basis point (0.0001%)
//Local mapping of valid gas pools
mapping(address => bool) private approvedGasPool;
/// @inheritdoc IRootBridgeAgent
function uniswapV3SwapCallback(int256 amount0, int256 amount1, bytes calldata _data) external {
if (!approvedGasPool[msg.sender]) revert CallerIsNotPool();
if (amount0 == 0 && amount1 == 0) revert AmountsAreZero();
SwapCallbackData memory data = abi.decode(_data, (SwapCallbackData));
address(data.tokenIn).safeTransfer(msg.sender, uint256(amount0 > 0 ? amount0 : amount1));
}
/**
* @notice Swaps gas tokens from the given branch chain to the root chain
* @param _amount amount of gas token to swap
* @param _fromChain chain to swap from
*/
function _gasSwapIn(uint256 _amount, uint24 _fromChain) internal returns (uint256) {
//Get fromChain's Gas Pool Info
(bool zeroForOneOnInflow, uint24 priceImpactPercentage, address gasTokenGlobalAddress, address poolAddress) =
IPort(localPortAddress).getGasPoolInfo(_fromChain);
//Check if valid addresses
if (gasTokenGlobalAddress == address(0) || poolAddress == address(0)) revert InvalidGasPool();
//Move Gas hTokens from Branch to Root / Mint Sufficient hTokens to match new port deposit
IPort(localPortAddress).bridgeToRoot(address(this), gasTokenGlobalAddress, _amount, _amount, _fromChain);
//Save Gas Pool for future use
if (!approvedGasPool[poolAddress]) approvedGasPool[poolAddress] = true;
//Get sqrtPriceX96
(uint160 sqrtPriceX96,,,,,,) = IUniswapV3Pool(poolAddress).slot0();
// Calculate Price limit depending on pre-set price impact
uint160 exactSqrtPriceImpact = (sqrtPriceX96 * (priceImpactPercentage / 2)) / GLOBAL_DIVISIONER;
//Get limit
uint160 sqrtPriceLimitX96 =
zeroForOneOnInflow ? sqrtPriceX96 - exactSqrtPriceImpact : sqrtPriceX96 + exactSqrtPriceImpact;
//Swap imbalanced token as long as we haven't used the entire amountSpecified and haven't reached the price limit
try IUniswapV3Pool(poolAddress).swap(
address(this),
zeroForOneOnInflow,
int256(_amount),
sqrtPriceLimitX96,
abi.encode(SwapCallbackData({tokenIn: gasTokenGlobalAddress}))
) returns (int256 amount0, int256 amount1) {
return uint256(zeroForOneOnInflow ? amount1 : amount0);
} catch (bytes memory) {
_forceRevert();
return 0;
}
}
/**
* @notice Swaps gas tokens from the given root chain to the branch chain
* @param _amount amount of gas token to swap
* @param _toChain chain to swap to
*/
function _gasSwapOut(uint256 _amount, uint24 _toChain) internal returns (uint256, address) {
//Get fromChain's Gas Pool Info
(bool zeroForOneOnInflow, uint24 priceImpactPercentage, address gasTokenGlobalAddress, address poolAddress) =
IPort(localPortAddress).getGasPoolInfo(_toChain);
//Check if valid addresses
if (gasTokenGlobalAddress == address(0) || poolAddress == address(0)) revert InvalidGasPool();
//Save Gas Pool for future use
if (!approvedGasPool[poolAddress]) approvedGasPool[poolAddress] = true;
uint160 sqrtPriceLimitX96;
{
//Get sqrtPriceX96
(uint160 sqrtPriceX96,,,,,,) = IUniswapV3Pool(poolAddress).slot0();
// Calculate Price limit depending on pre-set price impact
uint160 exactSqrtPriceImpact = (sqrtPriceX96 * (priceImpactPercentage / 2)) / GLOBAL_DIVISIONER;
//Get limit
sqrtPriceLimitX96 =
zeroForOneOnInflow ? sqrtPriceX96 + exactSqrtPriceImpact : sqrtPriceX96 - exactSqrtPriceImpact;
}
//Swap imbalanced token as long as we haven't used the entire amountSpecified and haven't reached the price limit
(int256 amount0, int256 amount1) = IUniswapV3Pool(poolAddress).swap(
address(this),
!zeroForOneOnInflow,
int256(_amount),
sqrtPriceLimitX96,
abi.encode(SwapCallbackData({tokenIn: address(wrappedNativeToken)}))
);
return (uint256(!zeroForOneOnInflow ? amount1 : amount0), gasTokenGlobalAddress);
}
/**
* @notice Manages gas costs of bridging from Root to a given Branch.
* @param _toChain destination chain.
*/
function _manageGasOut(uint24 _toChain) internal returns (uint128) {
uint256 amountOut;
address gasToken;
uint256 _initialGas = initialGas;
if (_toChain == localChainId) {
//Transfer gasToBridgeOut Local Branch Bridge Agent if remote initiated call.
if (_initialGas > 0) {
address(wrappedNativeToken).safeTransfer(getBranchBridgeAgent[localChainId], userFeeInfo.gasToBridgeOut);
}
return uint128(userFeeInfo.gasToBridgeOut);
}
if (_initialGas > 0) {
if (userFeeInfo.gasToBridgeOut <= MIN_FALLBACK_RESERVE * tx.gasprice) revert InsufficientGasForFees();
(amountOut, gasToken) = _gasSwapOut(userFeeInfo.gasToBridgeOut, _toChain);
} else {
if (msg.value <= MIN_FALLBACK_RESERVE * tx.gasprice) revert InsufficientGasForFees();
wrappedNativeToken.deposit{value: msg.value}();
(amountOut, gasToken) = _gasSwapOut(msg.value, _toChain);
}
IPort(localPortAddress).burn(address(this), gasToken, amountOut, _toChain);
return amountOut.toUint128();
}
/*///////////////////////////////////////////////////////////////
ANYCALL INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/// @notice Internal function performs call to AnycallProxy Contract for cross-chain messaging.
function _performCall(bytes memory _calldata, uint256 _toChain) internal {
address callee = getBranchBridgeAgent[_toChain];
if (callee == address(0)) revert UnrecognizedBridgeAgent();
if (_toChain != localChainId) {
//Sends message to AnycallProxy
IAnycallProxy(localAnyCallAddress).anyCall(
callee, _calldata, _toChain, AnycallFlags.FLAG_ALLOW_FALLBACK_DST, ""
);
} else {
//Execute locally
IBranchBridgeAgent(callee).anyExecute(_calldata);
}
}
/**
* @notice Pays for the remote call execution gas. Demands that the user has enough gas to replenish gas for the anycall config contract or forces reversion.
* @param _depositedGas available user gas to pay for execution.
* @param _gasToBridgeOut amount of gas needed to bridge out.
* @param _initialGas initial gas used by the transaction.
* @param _fromChain chain remote action initiated from.
*/
function _payExecutionGas(uint128 _depositedGas, uint128 _gasToBridgeOut, uint256 _initialGas, uint24 _fromChain)
internal
{
//reset initial remote execution gas and remote execution fee information
delete(initialGas);
delete(userFeeInfo);
if (_fromChain == localChainId) return;
//Get Available Gas
uint256 availableGas = _depositedGas - _gasToBridgeOut;
//Get Root Environment Execution Cost
uint256 minExecCost = tx.gasprice * (MIN_EXECUTION_OVERHEAD + _initialGas - gasleft());
//Check if sufficient balance
if (minExecCost > availableGas) {
_forceRevert();
return;
}
//Replenish Gas
_replenishGas(minExecCost);
//Account for excess gas
accumulatedFees += availableGas - minExecCost;
}
/**
* @notice Updates the user deposit with the amount of gas needed to pay for the fallback function execution.
* @param _settlementNonce nonce of the failed settlement
* @param _initialGas initial gas available for this transaction
*/
function _payFallbackGas(uint32 _settlementNonce, uint256 _initialGas) internal virtual {
//Save gasleft
uint256 gasLeft = gasleft();
//Get Branch Environment Execution Cost
uint256 minExecCost = tx.gasprice * (MIN_FALLBACK_RESERVE + _initialGas - gasLeft);
//Check if sufficient balance
if (minExecCost > getSettlement[_settlementNonce].gasToBridgeOut) {
_forceRevert();
return;
}
//Update user deposit reverts if not enough gas
getSettlement[_settlementNonce].gasToBridgeOut -= minExecCost.toUint128();
}
function _replenishGas(uint256 _executionGasSpent) internal {
//Unwrap Gas
wrappedNativeToken.withdraw(_executionGasSpent);
IAnycallConfig(IAnycallProxy(localAnyCallAddress).config()).deposit{value: _executionGasSpent}(address(this));
}
/// @notice Internal function that return 'from' address and 'fromChain' Id by performing an external call to AnycallExecutor Context.
function _getContext() internal view returns (address from, uint256 fromChainId) {
(from, fromChainId,) = IAnycallExecutor(localAnyCallExecutorAddress).context();
}
/// @inheritdoc IApp
function anyExecute(bytes calldata data)
external
virtual
requiresExecutor
returns (bool success, bytes memory result)
{
//Get Initial Gas Checkpoint
uint256 _initialGas = gasleft();
uint24 fromChainId;
UserFeeInfo memory _userFeeInfo;
if (localAnyCallExecutorAddress == msg.sender) {
//Save initial gas
initialGas = _initialGas;
//Get fromChainId from AnyExecutor Context
(, uint256 _fromChainId) = _getContext();
//Save fromChainId
fromChainId = _fromChainId.toUint24();
//Swap in all deposited Gas
_userFeeInfo.depositedGas = _gasSwapIn(
uint256(uint128(bytes16(data[data.length - PARAMS_GAS_IN:data.length - PARAMS_GAS_OUT]))), fromChainId
).toUint128();
//Save Gas to Swap out to destination chain
_userFeeInfo.gasToBridgeOut = uint128(bytes16(data[data.length - PARAMS_GAS_OUT:data.length]));
} else {
//Local Chain initiated call
fromChainId = localChainId;
//Save depositedGas
_userFeeInfo.depositedGas = uint128(bytes16(data[data.length - 32:data.length - 16]));
//Save Gas to Swap out to destination chain
_userFeeInfo.gasToBridgeOut = _userFeeInfo.depositedGas;
}
if (_userFeeInfo.depositedGas < _userFeeInfo.gasToBridgeOut) {
_forceRevert();
//Return true to avoid triggering anyFallback in case of `_forceRevert()` failure
return (true, "Not enough gas to bridge out");
}
//Store User Fee Info
userFeeInfo = _userFeeInfo;
//Read Bridge Agent Action Flag attached from cross-chain message header.
bytes1 flag = data[0];
//DEPOSIT FLAG: 0 (System request / response)
if (flag == 0x00) {
//Get nonce
uint32 nonce = uint32(bytes4(data[PARAMS_START:PARAMS_TKN_START]));
//Check if tx has already been executed
if (executionHistory[fromChainId][nonce]) {
_forceRevert();
//Return true to avoid triggering anyFallback in case of `_forceRevert()` failure
return (true, "already executed tx");
}
//Try to execute remote request
try RootBridgeAgentExecutor(bridgeAgentExecutorAddress).executeSystemRequest(
localRouterAddress, data, fromChainId
) returns (bool, bytes memory res) {
(success, result) = (true, res);
} catch (bytes memory reason) {
//Interaction failure trigger fallback
(success, result) = (false, reason);
}
//Update tx state as executed
executionHistory[fromChainId][nonce] = true;
//DEPOSIT FLAG: 1 (Call without Deposit)
} else if (flag == 0x01) {
//Get Deposit Nonce
uint32 nonce = uint32(bytes4(data[PARAMS_START:PARAMS_TKN_START]));
//Check if tx has already been executed
if (executionHistory[fromChainId][nonce]) {
_forceRevert();
//Return true to avoid triggering anyFallback in case of `_forceRevert()` failure
return (true, "already executed tx");
}
//Try to execute remote request
try RootBridgeAgentExecutor(bridgeAgentExecutorAddress).executeNoDeposit(
localRouterAddress, data, fromChainId
) returns (bool, bytes memory res) {
(success, result) = (true, res);
} catch (bytes memory reason) {
//No new asset deposit no need to trigger fallback
(success, result) = (true, reason);
}
//Update tx state as executed
executionHistory[fromChainId][nonce] = true;
//DEPOSIT FLAG: 2 (Call with Deposit)
} else if (flag == 0x02) {
//Get Deposit Nonce
uint32 nonce = uint32(bytes4(data[PARAMS_START:PARAMS_TKN_START]));
//Check if tx has already been executed
if (executionHistory[fromChainId][nonce]) {
_forceRevert();
//Return true to avoid triggering anyFallback in case of `_forceRevert()` failure
return (true, "already executed tx");
}
//Try to execute remote request
try RootBridgeAgentExecutor(bridgeAgentExecutorAddress).executeWithDeposit(
localRouterAddress, data, fromChainId
) returns (bool, bytes memory res) {
(success, result) = (true, res);
} catch (bytes memory reason) {
result = reason;
}
//Update tx state as executed
executionHistory[fromChainId][nonce] = true;
//DEPOSIT FLAG: 3 (Call with multiple asset Deposit)
} else if (flag == 0x03) {
//Get deposit nonce
uint32 nonce = uint32(bytes4(data[2:6]));
//Check if tx has already been executed
if (executionHistory[fromChainId][nonce]) {
_forceRevert();
//Return true to avoid triggering anyFallback in case of `_forceRevert()` failure
return (true, "already executed tx");
}
//Try to execute remote request
try RootBridgeAgentExecutor(bridgeAgentExecutorAddress).executeWithDepositMultiple(