@@ -9,14 +9,15 @@ import {ERC7201Helper} from "./utils/ERC7201Helper.sol";
99import {ILiquidityPool} from "./interfaces/ILiquidityPool.sol " ;
1010import {IRebalancer} from "./interfaces/IRebalancer.sol " ;
1111import {ICCTPTokenMessenger, ICCTPMessageTransmitter} from "./interfaces/ICCTP.sol " ;
12+ import {CCTPAdapter} from "./utils/CCTPAdapter.sol " ;
1213
1314/// @title Facilitates liquidity movement between Liquidity Pools on same/different chains.
1415/// Routes, which is a destination pool/domain and a bridging provider, have to be approved by admin.
1516/// Rebalancing only takes arbitrary input on the amount, then contract enforces correct processing.
1617/// REBALANCER_ROLE is needed to finalize/init rebalancing process.
1718/// @notice Upgradeable.
1819/// @author Oleksii Matiiasevych <[email protected] > 19- contract Rebalancer is IRebalancer , AccessControlUpgradeable {
20+ contract Rebalancer is IRebalancer , AccessControlUpgradeable , CCTPAdapter {
2021 using SafeERC20 for IERC20 ;
2122 using BitMaps for BitMaps.BitMap;
2223 using EnumerableSet for EnumerableSet.AddressSet;
@@ -41,8 +42,6 @@ contract Rebalancer is IRebalancer, AccessControlUpgradeable {
4142 error ZeroAmount ();
4243 error RouteDenied ();
4344 error InvalidRoute ();
44- error ProcessFailed ();
45- error UnsupportedDomain ();
4645 error UnsupportedProvider ();
4746 error InvalidLength ();
4847 error InvalidPoolAssets ();
@@ -189,7 +188,7 @@ contract Rebalancer is IRebalancer, AccessControlUpgradeable {
189188 _processRebalanceLOCAL (amount, destinationPool);
190189 } else
191190 if (provider == Provider.CCTP) {
192- _initiateRebalanceCCTP ( amount, destinationPool, destinationDomain);
191+ initiateTransferCCTP (CCTP_TOKEN_MESSENGER, ASSETS, amount, destinationPool, destinationDomain);
193192 } else {
194193 // Unreachable atm, but could become so when more providers are added to enum.
195194 revert UnsupportedProvider ();
@@ -204,7 +203,8 @@ contract Rebalancer is IRebalancer, AccessControlUpgradeable {
204203 require (isRouteAllowed (destinationPool, DOMAIN, Provider.LOCAL), RouteDenied ());
205204 uint256 depositAmount = 0 ;
206205 if (provider == Provider.CCTP) {
207- depositAmount = _processRebalanceCCTP (destinationPool, extraData);
206+ depositAmount = processTransferCCTP (CCTP_MESSAGE_TRANSMITTER, ASSETS, destinationPool, extraData);
207+ ILiquidityPool (destinationPool).deposit (depositAmount);
208208 } else {
209209 // Unreachable atm, but could become so when more providers are added to enum.
210210 revert UnsupportedProvider ();
@@ -213,21 +213,6 @@ contract Rebalancer is IRebalancer, AccessControlUpgradeable {
213213 emit ProcessRebalance (depositAmount, destinationPool, provider);
214214 }
215215
216- function _initiateRebalanceCCTP (
217- uint256 amount ,
218- address destinationPool ,
219- Domain destinationDomain
220- ) internal {
221- ASSETS.forceApprove (address (CCTP_TOKEN_MESSENGER), amount);
222- CCTP_TOKEN_MESSENGER.depositForBurnWithCaller (
223- amount,
224- domainCCTP (destinationDomain),
225- _addressToBytes32 (address (destinationPool)),
226- address (ASSETS),
227- _addressToBytes32 (address (this ))
228- );
229- }
230-
231216 function _processRebalanceLOCAL (
232217 uint256 amount ,
233218 address destinationPool
@@ -238,50 +223,6 @@ contract Rebalancer is IRebalancer, AccessControlUpgradeable {
238223 emit ProcessRebalance (amount, destinationPool, Provider.LOCAL);
239224 }
240225
241- function _processRebalanceCCTP (
242- address destinationPool ,
243- bytes calldata extraData
244- ) internal returns (uint256 ) {
245- uint256 balanceBefore = ASSETS.balanceOf (address (destinationPool));
246-
247- (bytes memory message , bytes memory attestation ) = abi.decode (extraData, (bytes , bytes ));
248- bool success = CCTP_MESSAGE_TRANSMITTER.receiveMessage (message, attestation);
249- require (success, ProcessFailed ());
250-
251- uint256 balanceAfter = ASSETS.balanceOf (address (destinationPool));
252- require (balanceAfter > balanceBefore, ProcessFailed ());
253- uint256 depositAmount = balanceAfter - balanceBefore;
254- ILiquidityPool (destinationPool).deposit (depositAmount);
255- return depositAmount;
256- }
257-
258- function domainCCTP (Domain destinationDomain ) public pure virtual returns (uint32 ) {
259- if (destinationDomain == Domain.ETHEREUM) {
260- return 0 ;
261- } else
262- if (destinationDomain == Domain.AVALANCHE) {
263- return 1 ;
264- } else
265- if (destinationDomain == Domain.OP_MAINNET) {
266- return 2 ;
267- } else
268- if (destinationDomain == Domain.ARBITRUM_ONE) {
269- return 3 ;
270- } else
271- if (destinationDomain == Domain.BASE) {
272- return 6 ;
273- } else
274- if (destinationDomain == Domain.POLYGON_MAINNET) {
275- return 7 ;
276- } else {
277- revert UnsupportedDomain ();
278- }
279- }
280-
281- function _addressToBytes32 (address addr ) internal pure returns (bytes32 ) {
282- return bytes32 (uint256 (uint160 (addr)));
283- }
284-
285226 function _getStorage () private pure returns (RebalancerStorage storage $) {
286227 assembly {
287228 $.slot := STORAGE_LOCATION
0 commit comments