@@ -6,6 +6,7 @@ import {Role} from "../../../Role.sol";
66
77import {CrossChain} from "./CrossChain.sol " ;
88import {IBridgeAndCall} from "@lxly-bridge-and-call/IBridgeAndCall.sol " ;
9+ import {IPolygonZkEVMBridge} from "@zkevm-contracts/interfaces/IPolygonZkEVMBridge.sol " ;
910import {IERC20 } from "src/interface/IERC20.sol " ;
1011
1112library PolygonAgglayerCrossChainStorage {
@@ -16,6 +17,7 @@ library PolygonAgglayerCrossChainStorage {
1617
1718 struct Data {
1819 address router;
20+ address bridge;
1921 }
2022
2123 function data () internal pure returns (Data storage data_ ) {
@@ -35,21 +37,27 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
3537
3638 /// @notice Returns all implemented callback and fallback functions.
3739 function getModuleConfig () external pure override returns (ModuleConfig memory config ) {
38- config.fallbackFunctions = new FallbackFunction [](3 );
40+ config.fallbackFunctions = new FallbackFunction [](7 );
3941
4042 config.fallbackFunctions[0 ] = FallbackFunction ({selector: this .getRouter.selector , permissionBits: 0 });
4143 config.fallbackFunctions[1 ] =
4244 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 ] =
4449 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 });
4552
4653 config.registerInstallationCallback = true ;
4754 }
4855
4956 /// @dev Called by a Core into an Module during the installation of the Module.
5057 function onInstall (bytes calldata data ) external {
51- address router = abi.decode (data, (address ));
58+ ( address router , address bridge ) = abi.decode (data, (address , address ));
5259 _polygonAgglayerStorage ().router = router;
60+ _polygonAgglayerStorage ().bridge = bridge;
5361 }
5462
5563 /// @dev Called by a Core into an Module during the uninstallation of the Module.
@@ -69,16 +77,22 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
6977 FALLBACK FUNCTIONS
7078 //////////////////////////////////////////////////////////////*/
7179
72- /// @notice Returns whether transfers is enabled for the token.
7380 function getRouter () external view override returns (address ) {
7481 return _polygonAgglayerStorage ().router;
7582 }
7683
77- /// @notice Set transferability for a token.
7884 function setRouter (address router ) external override {
7985 _polygonAgglayerStorage ().router = router;
8086 }
8187
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+
8296 function sendCrossChainTransaction (
8397 uint64 _destinationChain ,
8498 address _callAddress ,
@@ -93,20 +107,112 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
93107 bytes memory permitData
94108 ) = abi.decode (_extraArgs, (address , bool , address , uint256 , bytes ));
95109
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+ }
106128
107129 onCrossChainTransactionSent (_destinationChain, _callAddress, _payload, _extraArgs);
108130 }
109131
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+
110216 function _bridgeAndCall (
111217 address _token ,
112218 uint256 _amount ,
@@ -117,10 +223,11 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
117223 bytes memory _payload ,
118224 bool _forceUpdateGlobalExitRoot
119225 ) internal {
226+ address router = _polygonAgglayerStorage ().router;
120227 IERC20 (_token).transferFrom (msg .sender , address (this ), _amount);
121- IERC20 (_token).approve (_polygonAgglayerStorage (). router, _amount);
228+ IERC20 (_token).approve (router, _amount);
122229
123- IBridgeAndCall (_polygonAgglayerStorage (). router).bridgeAndCall (
230+ IBridgeAndCall (router).bridgeAndCall (
124231 _token,
125232 _amount,
126233 permitData,
@@ -132,10 +239,6 @@ contract PolygonAgglayerCrossChain is Module, CrossChain {
132239 );
133240 }
134241
135- /*//////////////////////////////////////////////////////////////
136- INTERNAL FUNCTIONS
137- //////////////////////////////////////////////////////////////*/
138-
139242 function onCrossChainTransactionSent (
140243 uint64 _destinationChain ,
141244 address _callAddress ,
0 commit comments