@@ -6,6 +6,7 @@ import {Role} from "../../../Role.sol";
6
6
7
7
import {CrossChain} from "./CrossChain.sol " ;
8
8
import {IBridgeAndCall} from "@lxly-bridge-and-call/IBridgeAndCall.sol " ;
9
+ import {IPolygonZkEVMBridge} from "@zkevm-contracts/interfaces/IPolygonZkEVMBridge.sol " ;
9
10
import {IERC20 } from "src/interface/IERC20.sol " ;
10
11
11
12
library PolygonAgglayerCrossChainStorage {
@@ -16,6 +17,7 @@ library PolygonAgglayerCrossChainStorage {
16
17
17
18
struct Data {
18
19
address router;
20
+ address bridge;
19
21
}
20
22
21
23
function data () internal pure returns (Data storage data_ ) {
@@ -35,21 +37,27 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
35
37
36
38
/// @notice Returns all implemented callback and fallback functions.
37
39
function getModuleConfig () external pure override returns (ModuleConfig memory config ) {
38
- config.fallbackFunctions = new FallbackFunction [](3 );
40
+ config.fallbackFunctions = new FallbackFunction [](7 );
39
41
40
42
config.fallbackFunctions[0 ] = FallbackFunction ({selector: this .getRouter.selector , permissionBits: 0 });
41
43
config.fallbackFunctions[1 ] =
42
44
FallbackFunction ({selector: this .setRouter.selector , permissionBits: Role._MANAGER_ROLE});
43
- config.fallbackFunctions[2 ] =
45
+ config.fallbackFunctions[2 ] = FallbackFunction ({selector: this .getBridge.selector , permissionBits: 0 });
46
+ config.fallbackFunctions[3 ] =
47
+ FallbackFunction ({selector: this .setBridge.selector , permissionBits: Role._MANAGER_ROLE});
48
+ config.fallbackFunctions[4 ] =
44
49
FallbackFunction ({selector: this .sendCrossChainTransaction.selector , permissionBits: 0 });
50
+ config.fallbackFunctions[5 ] = FallbackFunction ({selector: this .claimMessage.selector , permissionBits: 0 });
51
+ config.fallbackFunctions[6 ] = FallbackFunction ({selector: this .claimAsset.selector , permissionBits: 0 });
45
52
46
53
config.registerInstallationCallback = true ;
47
54
}
48
55
49
56
/// @dev Called by a Core into an Module during the installation of the Module.
50
57
function onInstall (bytes calldata data ) external {
51
- address router = abi.decode (data, (address ));
58
+ ( address router , address bridge ) = abi.decode (data, (address , address ));
52
59
_polygonAgglayerStorage ().router = router;
60
+ _polygonAgglayerStorage ().bridge = bridge;
53
61
}
54
62
55
63
/// @dev Called by a Core into an Module during the uninstallation of the Module.
@@ -69,16 +77,22 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
69
77
FALLBACK FUNCTIONS
70
78
//////////////////////////////////////////////////////////////*/
71
79
72
- /// @notice Returns whether transfers is enabled for the token.
73
80
function getRouter () external view override returns (address ) {
74
81
return _polygonAgglayerStorage ().router;
75
82
}
76
83
77
- /// @notice Set transferability for a token.
78
84
function setRouter (address router ) external override {
79
85
_polygonAgglayerStorage ().router = router;
80
86
}
81
87
88
+ function getBridge () external view returns (address ) {
89
+ return _polygonAgglayerStorage ().bridge;
90
+ }
91
+
92
+ function setBridge (address bridge ) external {
93
+ _polygonAgglayerStorage ().bridge = bridge;
94
+ }
95
+
82
96
function sendCrossChainTransaction (
83
97
uint64 _destinationChain ,
84
98
address _callAddress ,
@@ -93,20 +107,112 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
93
107
bytes memory permitData
94
108
) = abi.decode (_extraArgs, (address , bool , address , uint256 , bytes ));
95
109
96
- _bridgeAndCall (
97
- _token,
98
- _amount,
99
- permitData,
100
- uint32 (_destinationChain),
101
- _callAddress,
102
- _fallbackAddress,
103
- _payload,
104
- _forceUpdateGlobalExitRoot
105
- );
110
+ if (_token == address (0 ) && _amount == 0 ) {
111
+ _bridgeMessage (uint32 (_destinationChain), _callAddress, _forceUpdateGlobalExitRoot, _payload);
112
+ } else if (_payload.length == 0 ) {
113
+ _bridgeAsset (
114
+ uint32 (_destinationChain), _callAddress, _amount, _token, _forceUpdateGlobalExitRoot, permitData
115
+ );
116
+ } else {
117
+ _bridgeAndCall (
118
+ _token,
119
+ _amount,
120
+ permitData,
121
+ uint32 (_destinationChain),
122
+ _callAddress,
123
+ _fallbackAddress,
124
+ _payload,
125
+ _forceUpdateGlobalExitRoot
126
+ );
127
+ }
106
128
107
129
onCrossChainTransactionSent (_destinationChain, _callAddress, _payload, _extraArgs);
108
130
}
109
131
132
+ function claimMessage (
133
+ bytes32 [32 ] calldata smtProof ,
134
+ uint32 index ,
135
+ bytes32 mainnetExitRoot ,
136
+ bytes32 rollupExitRoot ,
137
+ uint32 originNetwork ,
138
+ address originAddress ,
139
+ uint32 destinationNetwork ,
140
+ address destinationAddress ,
141
+ uint256 amount ,
142
+ bytes calldata metadata
143
+ ) external {
144
+ IPolygonZkEVMBridge (_polygonAgglayerStorage ().bridge).claimMessage (
145
+ smtProof,
146
+ index,
147
+ mainnetExitRoot,
148
+ rollupExitRoot,
149
+ originNetwork,
150
+ originAddress,
151
+ destinationNetwork,
152
+ destinationAddress,
153
+ amount,
154
+ metadata
155
+ );
156
+ }
157
+
158
+ function claimAsset (
159
+ bytes32 [32 ] calldata smtProof ,
160
+ uint32 index ,
161
+ bytes32 mainnetExitRoot ,
162
+ bytes32 rollupExitRoot ,
163
+ uint32 originNetwork ,
164
+ address originTokenAddress ,
165
+ uint32 destinationNetwork ,
166
+ address destinationAddress ,
167
+ uint256 amount ,
168
+ bytes calldata metadata
169
+ ) external {
170
+ IPolygonZkEVMBridge (_polygonAgglayerStorage ().bridge).claimAsset (
171
+ smtProof,
172
+ index,
173
+ mainnetExitRoot,
174
+ rollupExitRoot,
175
+ originNetwork,
176
+ originTokenAddress,
177
+ destinationNetwork,
178
+ destinationAddress,
179
+ amount,
180
+ metadata
181
+ );
182
+ }
183
+
184
+ /*//////////////////////////////////////////////////////////////
185
+ INTERNAL FUNCTIONS
186
+ //////////////////////////////////////////////////////////////*/
187
+
188
+ function _bridgeMessage (
189
+ uint32 _destinationChain ,
190
+ address _callAddress ,
191
+ bool _forceUpdateGlobalExitRoot ,
192
+ bytes memory _payload
193
+ ) internal {
194
+ IPolygonZkEVMBridge (_polygonAgglayerStorage ().bridge).bridgeMessage (
195
+ uint32 (_destinationChain), _callAddress, _forceUpdateGlobalExitRoot, _payload
196
+ );
197
+ }
198
+
199
+ function _bridgeAsset (
200
+ uint32 _destinationChain ,
201
+ address _callAddress ,
202
+ uint256 _amount ,
203
+ address _token ,
204
+ bool _forceUpdateGlobalExitRoot ,
205
+ bytes memory permitData
206
+ ) internal {
207
+ address bridge = _polygonAgglayerStorage ().bridge;
208
+ IERC20 (_token).transferFrom (msg .sender , address (this ), _amount);
209
+ IERC20 (_token).approve (bridge, _amount);
210
+
211
+ IPolygonZkEVMBridge (bridge).bridgeAsset (
212
+ uint32 (_destinationChain), _callAddress, _amount, _token, _forceUpdateGlobalExitRoot, permitData
213
+ );
214
+ }
215
+
110
216
function _bridgeAndCall (
111
217
address _token ,
112
218
uint256 _amount ,
@@ -117,10 +223,11 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
117
223
bytes memory _payload ,
118
224
bool _forceUpdateGlobalExitRoot
119
225
) internal {
226
+ address router = _polygonAgglayerStorage ().router;
120
227
IERC20 (_token).transferFrom (msg .sender , address (this ), _amount);
121
- IERC20 (_token).approve (_polygonAgglayerStorage (). router, _amount);
228
+ IERC20 (_token).approve (router, _amount);
122
229
123
- IBridgeAndCall (_polygonAgglayerStorage (). router).bridgeAndCall (
230
+ IBridgeAndCall (router).bridgeAndCall (
124
231
_token,
125
232
_amount,
126
233
permitData,
@@ -132,10 +239,6 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
132
239
);
133
240
}
134
241
135
- /*//////////////////////////////////////////////////////////////
136
- INTERNAL FUNCTIONS
137
- //////////////////////////////////////////////////////////////*/
138
-
139
242
function onCrossChainTransactionSent (
140
243
uint64 _destinationChain ,
141
244
address _callAddress ,
0 commit comments