Skip to content

Commit bc3eb47

Browse files
viatrixlastperson
andauthored
Add repayer contract (#48)
* Add repayer contract * Remove Repaid event from USDC pool * Apply suggestions from code review Co-authored-by: Oleksii Matiiasevych <[email protected]> * Fix import * Add repayer to deploy scripts * Fix lint * Fix tryResolve * Add task for setting repayer routes * Fix task * Fix set routes tasks --------- Co-authored-by: Oleksii Matiiasevych <[email protected]>
1 parent 520def8 commit bc3eb47

20 files changed

+1521
-140
lines changed

contracts/LiquidityHub.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ contract LiquidityHub is ILiquidityHub, ERC4626Upgradeable, AccessControlUpgrade
196196
function depositProfit(uint256 assets) external onlyRole(DEPOSIT_PROFIT_ROLE) {
197197
LiquidityHubStorage storage $ = _getStorage();
198198
SafeERC20.safeTransferFrom(IERC20(asset()), _msgSender(), address(LIQUIDITY_POOL), assets);
199-
uint256 totalAssets = $.totalAssets;
200-
require(totalAssets > 0, EmptyHub());
201-
require(assets <= _assetsIncreaseHardLimit(totalAssets), AssetsExceedHardLimit());
202-
uint256 newAssets = totalAssets + assets;
199+
uint256 currentAssets = $.totalAssets;
200+
require(currentAssets > 0, EmptyHub());
201+
require(assets <= _assetsIncreaseHardLimit(currentAssets), AssetsExceedHardLimit());
202+
uint256 newAssets = currentAssets + assets;
203203
$.totalAssets = newAssets;
204204
LIQUIDITY_POOL.deposit(assets);
205205
emit DepositProfit(_msgSender(), assets);

contracts/LiquidityPoolAave.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ contract LiquidityPoolAave is LiquidityPool {
4848
event BorrowTokenLTVSet(address token, uint256 oldLTV, uint256 newLTV);
4949
event HealthFactorSet(uint256 oldHealthFactor, uint256 newHealthFactor);
5050
event DefaultLTVSet(uint256 oldDefaultLTV, uint256 newDefaultLTV);
51-
event Repaid(address borrowToken, uint256 repaidAmount);
5251
event WithdrawnFromAave(address to, uint256 amount);
52+
event Repaid(address borrowToken, uint256 repaidAmount);
5353

5454
constructor(
5555
address liquidityToken,

contracts/Rebalancer.sol

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {ERC7201Helper} from "./utils/ERC7201Helper.sol";
99
import {ILiquidityPool} from "./interfaces/ILiquidityPool.sol";
1010
import {IRebalancer} from "./interfaces/IRebalancer.sol";
1111
import {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

Comments
 (0)