Skip to content

Commit 180e7b3

Browse files
authored
Merge pull request #270 from stabilitydao/254-sial-bug-decreasing-ltv-on-exits
254 sial bug decreasing ltv on exits
2 parents 3da50f0 + 0938815 commit 180e7b3

14 files changed

+1456
-237
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.23;
3+
4+
interface IBalancerV3FlashCallback {
5+
function receiveFlashLoanV3(address token, uint amount, bytes memory userData) external;
6+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
pragma solidity ^0.8.24;
4+
5+
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
6+
7+
/// @notice Restored from 0xbA1333333333a1BA1108E8412f11850A5C319bA9 (sonic)
8+
interface IVaultMainV3 {
9+
struct TokenConfig {
10+
address token;
11+
uint8 tokenType;
12+
address rateProvider;
13+
bool paysYieldFees;
14+
}
15+
16+
struct PoolRoleAccounts {
17+
address pauseManager;
18+
address swapFeeManager;
19+
address poolCreator;
20+
}
21+
22+
struct HooksConfig {
23+
bool enableHookAdjustedAmounts;
24+
bool shouldCallBeforeInitialize;
25+
bool shouldCallAfterInitialize;
26+
bool shouldCallComputeDynamicSwapFee;
27+
bool shouldCallBeforeSwap;
28+
bool shouldCallAfterSwap;
29+
bool shouldCallBeforeAddLiquidity;
30+
bool shouldCallAfterAddLiquidity;
31+
bool shouldCallBeforeRemoveLiquidity;
32+
bool shouldCallAfterRemoveLiquidity;
33+
address hooksContract;
34+
}
35+
36+
struct LiquidityManagement {
37+
bool disableUnbalancedLiquidity;
38+
bool enableAddLiquidityCustom;
39+
bool enableRemoveLiquidityCustom;
40+
bool enableDonation;
41+
}
42+
43+
struct AddLiquidityParams {
44+
address pool;
45+
address to;
46+
uint256[] maxAmountsIn;
47+
uint256 minBptAmountOut;
48+
uint8 kind;
49+
bytes userData;
50+
}
51+
52+
struct BufferWrapOrUnwrapParams {
53+
uint8 kind;
54+
uint8 direction;
55+
address wrappedToken;
56+
uint256 amountGivenRaw;
57+
uint256 limitRaw;
58+
}
59+
60+
struct RemoveLiquidityParams {
61+
address pool;
62+
address from;
63+
uint256 maxBptAmountIn;
64+
uint256[] minAmountsOut;
65+
uint8 kind;
66+
bytes userData;
67+
}
68+
69+
struct VaultSwapParams {
70+
uint8 kind;
71+
address pool;
72+
address tokenIn;
73+
address tokenOut;
74+
uint256 amountGivenRaw;
75+
uint256 limitRaw;
76+
bytes userData;
77+
}
78+
79+
fallback() external payable;
80+
81+
function addLiquidity(AddLiquidityParams memory params)
82+
external
83+
returns (
84+
uint256[] memory amountsIn,
85+
uint256 bptAmountOut,
86+
bytes memory returnData
87+
);
88+
89+
function erc4626BufferWrapOrUnwrap(BufferWrapOrUnwrapParams memory params)
90+
external
91+
returns (
92+
uint256 amountCalculatedRaw,
93+
uint256 amountInRaw,
94+
uint256 amountOutRaw
95+
);
96+
97+
function getPoolTokenCountAndIndexOfToken(address pool, address token)
98+
external
99+
view
100+
returns (uint256, uint256);
101+
102+
function getVaultExtension() external view returns (address);
103+
104+
function reentrancyGuardEntered() external view returns (bool);
105+
106+
function removeLiquidity(RemoveLiquidityParams memory params)
107+
external
108+
returns (
109+
uint256 bptAmountIn,
110+
uint256[] memory amountsOut,
111+
bytes memory returnData
112+
);
113+
114+
function sendTo(address token, address to, uint256 amount) external;
115+
116+
function settle(address token, uint256 amountHint) external returns (uint256 credit);
117+
118+
function swap(VaultSwapParams memory vaultSwapParams)
119+
external
120+
returns (
121+
uint256 amountCalculated,
122+
uint256 amountIn,
123+
uint256 amountOut
124+
);
125+
126+
function transfer(address owner, address to, uint256 amount) external returns (bool);
127+
128+
function transferFrom(address spender, address from, address to, uint256 amount) external returns (bool);
129+
130+
/// @notice Creates a context for a sequence of operations (i.e., "unlocks" the Vault).
131+
/// @dev Performs a callback on msg.sender with arguments provided in `data`. The Callback is `transient`,
132+
/// meaning all balances for the caller have to be settled at the end.
133+
/// Implementation in balancer-v3-monorepo is following:
134+
/// function unlock(bytes calldata data) external transient returns (bytes memory result) {
135+
/// return (msg.sender).functionCall(data);
136+
/// }
137+
/// @param data Contains function signature and args to be passed to the msg.sender
138+
/// @return result Resulting data from the call
139+
function unlock(bytes memory data) external returns (bytes memory result);
140+
141+
receive() external payable;
142+
}
143+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity >=0.5.0;
3+
4+
/// @title Callback for IUniswapV3PoolActions#flash
5+
/// @notice Any contract that calls IUniswapV3PoolActions#flash must implement this interface
6+
interface IUniswapV3FlashCallback {
7+
/// @notice Called to `msg.sender` after transferring to the recipient from IUniswapV3Pool#flash.
8+
/// @dev In the implementation you must repay the pool the tokens sent by flash plus the computed fee amounts.
9+
/// The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.
10+
/// @param fee0 The fee amount in token0 due to the pool by the end of the flash
11+
/// @param fee1 The fee amount in token1 due to the pool by the end of the flash
12+
/// @param data Any data passed through by the caller via the IUniswapV3PoolActions#flash call
13+
function uniswapV3FlashCallback(
14+
uint256 fee0,
15+
uint256 fee1,
16+
bytes calldata data
17+
) external;
18+
}

src/interfaces/IControllable.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ interface IControllable {
2323
error NotTheOwner();
2424
error ETHTransferFailed();
2525
error IncorrectInitParams();
26+
error InsufficientBalance();
27+
error IncorrectLtv(uint ltv);
2628
//endregion -- Custom Errors -----
2729

2830
event ContractInitialized(address platform, uint ts, uint block);

src/interfaces/ILeverageLendingStrategy.sol

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface ILeverageLendingStrategy {
1212
event LeverageLendingHealth(uint ltv, uint leverage);
1313
event TargetLeveragePercent(uint value);
1414
event UniversalParams(uint[] params);
15+
event UniversalAddresses(address[] addresses);
1516

1617
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
1718
/* DATA TYPES */
@@ -53,6 +54,8 @@ interface ILeverageLendingStrategy {
5354
uint swapPriceImpactTolerance0;
5455
/// @dev Swap price impact tolerance on re-balance debt
5556
uint swapPriceImpactTolerance1;
57+
/// @notice Flash loan kind. 0 - balancer v2 (paid), 1 - balancer v3 (free)
58+
uint flashLoanKind;
5659
}
5760

5861
struct LeverageLendingStrategyBaseInitParams {
@@ -80,7 +83,17 @@ interface ILeverageLendingStrategy {
8083
Deposit,
8184
Withdraw,
8285
DecreaseLtv,
83-
IncreaseLtv
86+
/// @notice All available balances are used
87+
IncreaseLtv,
88+
/// @notice Amounts of collateral and borrow that can be used are limited through temp vars
89+
IncreaseLtvLimited
90+
}
91+
92+
enum FlashLoanKind {
93+
/// @notice Balancer V2
94+
Default_0,
95+
BalancerV3_1,
96+
UniswapV3_2
8497
}
8598

8699
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
@@ -90,18 +103,21 @@ interface ILeverageLendingStrategy {
90103
/// @notice Re-balance debt
91104
/// @param newLtv Target LTV after re-balancing with 4 decimals. 90_00 is 90%.
92105
/// @return resultLtv LTV after re-balance. For static calls.
93-
function rebalanceDebt(uint newLtv) external returns (uint resultLtv);
106+
/// @return resultSharePrice Share price after applying rebalance debt
107+
function rebalanceDebt(uint newLtv, uint minSharePrice) external returns (uint resultLtv, uint resultSharePrice);
94108

95109
/// @notice Change target leverage percent
96110
/// @param value Value with 4 decimals, 90_00 is 90%.
97111
function setTargetLeveragePercent(uint value) external;
98112

99113
/// @notice Change universal configurable params
100-
function setUniversalParams(uint[] memory params) external;
114+
function setUniversalParams(uint[] memory params, address[] memory addresses) external;
101115

102116
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
103117
/* VIEW FUNCTIONS */
104118
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
119+
/// @notice Get universal configurable params
120+
function getUniversalParams() external view returns (uint[] memory params, address[] memory addresses);
105121

106122
/// @notice Difference between collateral and debt
107123
/// @return tvl USD amount of user deposited assets

0 commit comments

Comments
 (0)