Skip to content

Commit c83257f

Browse files
committed
Add totalDeposited getter to public pool
1 parent dfdfa99 commit c83257f

File tree

8 files changed

+52
-41
lines changed

8 files changed

+52
-41
lines changed

contracts/LiquidityPool.sol

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
5959
IERC20 immutable public ASSETS;
6060

6161
BitMaps.BitMap private _usedNonces;
62-
uint256 public totalDeposited;
62+
uint256 internal _totalDeposited;
6363

6464
bool public paused;
6565
bool public borrowPaused;
6666
address public mpcAddress;
67+
address public signerAddress;
6768

6869
bytes32 private constant LIQUIDITY_ADMIN_ROLE = "LIQUIDITY_ADMIN_ROLE";
6970
bytes32 private constant WITHDRAW_PROFIT_ROLE = "WITHDRAW_PROFIT_ROLE";
@@ -72,8 +73,6 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
7273
bytes4 constant internal MAGICVALUE = 0x1626ba7e;
7374
IWrappedNativeToken immutable public WRAPPED_NATIVE_TOKEN;
7475

75-
address public signerAddress;
76-
7776
error ZeroAddress();
7877
error InvalidSignature();
7978
error NotEnoughToDeposit();
@@ -279,7 +278,7 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
279278

280279
// Admin functions
281280

282-
/// @notice Can withdraw a maximum of totalDeposited. If anything is left, it is meant to be withdrawn through
281+
/// @notice Can withdraw a maximum of _totalDeposited. If anything is left, it is meant to be withdrawn through
283282
/// a withdrawProfit().
284283
function withdraw(address to, uint256 amount)
285284
external
@@ -289,9 +288,9 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
289288
whenNotPaused()
290289
{
291290
require(to != address(0), ZeroAddress());
292-
uint256 deposited = totalDeposited;
291+
uint256 deposited = _totalDeposited;
293292
require(deposited >= amount, InsufficientLiquidity());
294-
totalDeposited = deposited - amount;
293+
_totalDeposited = deposited - amount;
295294
_withdrawLogic(to, amount);
296295
emit Withdraw(_msgSender(), to, amount);
297296
}
@@ -362,7 +361,7 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
362361
}
363362

364363
function _deposit(address caller, uint256 amount) private {
365-
totalDeposited += amount;
364+
_totalDeposited += amount;
366365
_depositLogic(amount);
367366
emit Deposit(caller, amount);
368367
}
@@ -521,7 +520,7 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
521520
function _withdrawProfitLogic(IERC20 token) internal virtual returns (uint256) {
522521
uint256 totalBalance = token.balanceOf(address(this));
523522
if (token == ASSETS) {
524-
uint256 deposited = totalDeposited;
523+
uint256 deposited = _totalDeposited;
525524
if (totalBalance < deposited) return 0;
526525
return totalBalance - deposited;
527526
}
@@ -539,6 +538,10 @@ contract LiquidityPool is ILiquidityPool, AccessControl, EIP712, ISigner {
539538

540539
// View functions
541540

541+
function totalDeposited() external view virtual override returns (uint256) {
542+
return _totalDeposited;
543+
}
544+
542545
function balance(IERC20 token) external view override returns (uint256) {
543546
if (token == NATIVE_TOKEN) token = WRAPPED_NATIVE_TOKEN;
544547
return _balance(token);

contracts/LiquidityPoolAave.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ contract LiquidityPoolAave is LiquidityPool {
202202
uint256 totalBalance = token.balanceOf(address(this));
203203
if (token == ASSETS) {
204204
// Calculate accrued interest from deposits.
205-
uint256 interest = ATOKEN.balanceOf(address(this)) - totalDeposited;
205+
uint256 interest = ATOKEN.balanceOf(address(this)) - _totalDeposited;
206206
if (interest > 0) {
207207
_withdrawLogic(address(this), interest);
208208
totalBalance += interest;

contracts/LiquidityPoolAaveLongTerm.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ contract LiquidityPoolAaveLongTerm is LiquidityPoolAave, ILiquidityPoolLongTerm
108108
uint256 totalBalance = token.balanceOf(address(this));
109109
if (token == ASSETS) {
110110
// Calculate accrued interest from deposits.
111-
uint256 interest = ATOKEN.balanceOf(address(this)) - totalDeposited;
111+
uint256 interest = ATOKEN.balanceOf(address(this)) - _totalDeposited;
112112
if (interest > 0) {
113113
_withdrawLogic(address(this), interest);
114114
totalBalance += interest;

contracts/LiquidityPoolStablecoin.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ contract LiquidityPoolStablecoin is LiquidityPool {
3131

3232
function _withdrawProfitLogic(IERC20 token) internal view override returns (uint256) {
3333
uint256 assetBalance = ASSETS.balanceOf(address(this));
34-
uint256 deposited = totalDeposited;
34+
uint256 deposited = _totalDeposited;
3535
require(assetBalance >= deposited, WithdrawProfitDenied());
3636
if (token == ASSETS) return assetBalance - deposited;
3737
return token.balanceOf(address(this));

contracts/PublicLiquidityPool.sol

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ contract PublicLiquidityPool is LiquidityPool, ERC4626 {
1919
using Math for uint256;
2020
using SafeCast for uint256;
2121

22-
uint256 private constant MULTIPLIER = 10000;
22+
uint256 private constant RATE_DENOMINATOR = 10000;
2323
bytes32 private constant FEE_SETTER_ROLE = "FEE_SETTER_ROLE";
2424

25-
uint128 private assetsWithLent;
25+
// Balance of the assets in the pool with fees, after all repayments will be done.
26+
uint128 private _virtualBalance;
2627
uint112 public protocolFee;
2728
uint16 public protocolFeeRate;
2829

@@ -89,12 +90,16 @@ contract PublicLiquidityPool is LiquidityPool, ERC4626 {
8990
revert NotImplemented();
9091
}
9192

93+
function totalDeposited() external view virtual override returns (uint256) {
94+
return _virtualBalance;
95+
}
96+
9297
function totalAssets() public view virtual override returns (uint256) {
93-
return assetsWithLent;
98+
return _virtualBalance - protocolFee;
9499
}
95100

96101
function _setProtocolFeeRate(uint16 protocolFeeRate_) internal {
97-
require(protocolFeeRate_ <= MULTIPLIER, InvalidProtocolFeeRate());
102+
require(protocolFeeRate_ <= RATE_DENOMINATOR, InvalidProtocolFeeRate());
98103
protocolFeeRate = protocolFeeRate_;
99104

100105
emit ProtocolFeeRateSet(protocolFeeRate_);
@@ -125,7 +130,7 @@ contract PublicLiquidityPool is LiquidityPool, ERC4626 {
125130
function _deposit(address caller, address receiver, uint256 assets, uint256 shares) internal override {
126131
require(receiver != address(0), ZeroAddress());
127132
super._deposit(caller, receiver, assets, shares);
128-
assetsWithLent = (uint256(assetsWithLent) + assets).toUint128();
133+
_virtualBalance = (uint256(_virtualBalance) + assets).toUint128();
129134
}
130135

131136
function _withdraw(
@@ -136,23 +141,20 @@ contract PublicLiquidityPool is LiquidityPool, ERC4626 {
136141
uint256 shares
137142
) internal override whenNotPaused() {
138143
require(receiver != address(0), ZeroAddress());
139-
assetsWithLent = (uint256(assetsWithLent) - assets).toUint128();
144+
_virtualBalance = (uint256(_virtualBalance) - assets).toUint128();
140145
super._withdraw(caller, receiver, owner, assets, shares);
141146
}
142147

143148
function _withdrawProfitLogic(IERC20 token) internal override returns (uint256) {
144149
uint256 totalBalance = token.balanceOf(address(this));
145150
if (token == ASSETS) {
146151
uint256 profit = protocolFee;
147-
if (profit > 0) {
148-
protocolFee = 0;
149-
return profit;
150-
}
151-
if (totalBalance > assetsWithLent) {
152+
protocolFee = 0;
153+
if (totalBalance > _virtualBalance) {
152154
// In case there are donations sent to the pool.
153-
return totalBalance - assetsWithLent;
155+
profit += totalBalance - _virtualBalance;
154156
}
155-
return 0;
157+
return profit;
156158
}
157159
return totalBalance;
158160
}
@@ -165,9 +167,9 @@ contract PublicLiquidityPool is LiquidityPool, ERC4626 {
165167
require(amountToReceive <= amount, InvalidFillAmount());
166168
uint256 totalFee = amount - amountToReceive;
167169
if (totalFee > 0) {
168-
uint256 protocolFeeIncrease = totalFee.mulDiv(protocolFeeRate, MULTIPLIER, Math.Rounding.Ceil);
170+
uint256 protocolFeeIncrease = totalFee.mulDiv(protocolFeeRate, RATE_DENOMINATOR, Math.Rounding.Ceil);
169171
protocolFee = (uint256(protocolFee) + protocolFeeIncrease).toUint112();
170-
assetsWithLent = (uint256(assetsWithLent) + (totalFee - protocolFeeIncrease)).toUint128();
172+
_virtualBalance = (uint256(_virtualBalance) + totalFee).toUint128();
171173
}
172174
return amountToReceive;
173175
}

contracts/interfaces/ILiquidityPoolBase.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ interface ILiquidityPoolBase {
1515
address to
1616
) external;
1717

18+
function totalDeposited() external view returns (uint256);
19+
1820
function paused() external view returns (bool);
1921

2022
function pause() external;

contracts/testing/TestLiquidityPool.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,8 @@ contract TestLiquidityPool is ILiquidityPool, AccessControl {
116116
function balance(IERC20 token) external view override returns (uint256) {
117117
return token.balanceOf(address(this));
118118
}
119+
120+
function totalDeposited() external pure override returns (uint256) {
121+
return 0;
122+
}
119123
}

test/PublicLiquidityPool.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe("PublicLiquidityPool", function () {
183183
await usdc.connect(lp).approve(liquidityPool, amount);
184184
await expect(liquidityPool.connect(lp)[ERC4626Deposit](amount, lp))
185185
.to.emit(liquidityPool, ERC4626DepositEvent).withArgs(lp, lp, amount, amount);
186-
expect(await liquidityPool.totalDeposited()).to.eq(0);
186+
expect(await liquidityPool.totalDeposited()).to.eq(amount);
187187
expect(await liquidityPool.totalAssets()).to.eq(amount);
188188
expect(await liquidityPool.totalSupply()).to.eq(amount);
189189
expect(await liquidityPool.balance(usdc)).to.eq(amount);
@@ -223,7 +223,7 @@ describe("PublicLiquidityPool", function () {
223223
expect(await usdc.balanceOf(user2)).to.eq(0n);
224224
await expect(liquidityPool.connect(user)[ERC4626Deposit](amount3, user))
225225
.to.emit(liquidityPool, ERC4626DepositEvent).withArgs(user, user, amount3, amount3);
226-
expect(await liquidityPool.totalDeposited()).to.eq(0);
226+
expect(await liquidityPool.totalDeposited()).to.eq(amount + amount2 + amount3);
227227
expect(await liquidityPool.totalAssets()).to.eq(amount + amount2 + amount3);
228228
expect(await liquidityPool.totalSupply()).to.eq(amount + amount2 + amount3);
229229
expect(await liquidityPool.balance(usdc)).to.eq(amount + amount2 + amount3);
@@ -241,7 +241,7 @@ describe("PublicLiquidityPool", function () {
241241
await usdc.connect(lp).approve(liquidityPool, amount);
242242
await expect(liquidityPool.connect(lp).mint(amount, lp))
243243
.to.emit(liquidityPool, ERC4626DepositEvent).withArgs(lp, lp, amount, amount);
244-
expect(await liquidityPool.totalDeposited()).to.eq(0);
244+
expect(await liquidityPool.totalDeposited()).to.eq(amount);
245245
expect(await liquidityPool.totalAssets()).to.eq(amount);
246246
expect(await liquidityPool.totalSupply()).to.eq(amount);
247247
expect(await liquidityPool.balance(usdc)).to.eq(amount);
@@ -286,7 +286,7 @@ describe("PublicLiquidityPool", function () {
286286
permitSig.s,
287287
);
288288
await expect(tx).to.emit(liquidityPool, ERC4626DepositEvent).withArgs(lp, user, amount, amount);
289-
expect(await liquidityPool.totalDeposited()).to.eq(0);
289+
expect(await liquidityPool.totalDeposited()).to.eq(amount);
290290
expect(await liquidityPool.totalAssets()).to.eq(amount);
291291
expect(await liquidityPool.totalSupply()).to.eq(amount);
292292
expect(await liquidityPool.balance(usdc)).to.eq(amount);
@@ -348,7 +348,7 @@ describe("PublicLiquidityPool", function () {
348348
expect(await usdc.balanceOf(liquidityPool)).to.eq(amountLiquidity - amountToReceive);
349349
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
350350
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
351-
expect(await liquidityPool.totalDeposited()).to.eq(0);
351+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
352352
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
353353
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
354354
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);
@@ -412,7 +412,7 @@ describe("PublicLiquidityPool", function () {
412412
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
413413
expect(await eurc.balanceOf(liquidityPool)).to.eq(0);
414414
expect(await eurc.balanceOf(mockTarget)).to.eq(fillAmount);
415-
expect(await liquidityPool.totalDeposited()).to.eq(0);
415+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
416416
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
417417
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
418418
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);
@@ -476,7 +476,7 @@ describe("PublicLiquidityPool", function () {
476476
expect(await usdc.balanceOf(liquidityPool)).to.eq(amountLiquidity - amountToReceive);
477477
expect(await usdc.balanceOf(mockBorrowSwap)).to.eq(amountToReceive);
478478
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
479-
expect(await liquidityPool.totalDeposited()).to.eq(0);
479+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
480480
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
481481
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
482482
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);
@@ -596,7 +596,7 @@ describe("PublicLiquidityPool", function () {
596596
expect(await usdc.balanceOf(liquidityPool)).to.eq(amountLiquidity - amountToReceive);
597597
expect(await usdc.balanceOf(mockBorrowSwap)).to.eq(amountToReceive);
598598
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
599-
expect(await liquidityPool.totalDeposited()).to.eq(0);
599+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
600600
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
601601
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
602602
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);
@@ -704,7 +704,7 @@ describe("PublicLiquidityPool", function () {
704704
await usdc.connect(usdcOwner).approve(liquidityPool, amountLiquidity);
705705
await expect(liquidityPool.connect(usdcOwner)[ERC4626Deposit](amountLiquidity, usdcOwner))
706706
.to.emit(liquidityPool, ERC4626DepositEvent).withArgs(usdcOwner, usdcOwner, amountLiquidity, amountLiquidity);
707-
expect(await liquidityPool.totalDeposited()).to.eq(0);
707+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + amountLiquidity);
708708
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + amountLiquidity);
709709
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity + amountLiquidity);
710710
expect(await liquidityPool.balance(usdc)).to.eq(amountLiquidity + amountLiquidity);
@@ -830,7 +830,7 @@ describe("PublicLiquidityPool", function () {
830830
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
831831
expect(await usdc.balanceOf(user)).to.eq(0);
832832
expect(await usdc.balanceOf(user2)).to.eq(0);
833-
expect(await liquidityPool.totalDeposited()).to.eq(0);
833+
expect(await liquidityPool.totalDeposited()).to.eq(totalLiquidity + fee);
834834
expect(await liquidityPool.totalAssets()).to.eq(totalLiquidity + fee - protocolFee);
835835
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
836836
expect(await liquidityPool.totalSupply()).to.eq(totalLiquidity);
@@ -851,7 +851,7 @@ describe("PublicLiquidityPool", function () {
851851
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
852852
expect(await usdc.balanceOf(user)).to.eq(2008n * USDC_DEC);
853853
expect(await usdc.balanceOf(user2)).to.eq(1004n * USDC_DEC);
854-
expect(await liquidityPool.totalDeposited()).to.eq(0);
854+
expect(await liquidityPool.totalDeposited()).to.eq(1008n * USDC_DEC);
855855
expect(await liquidityPool.totalAssets()).to.eq(1004n * USDC_DEC);
856856
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
857857
expect(await liquidityPool.totalSupply()).to.eq(1000n * USDC_DEC);
@@ -868,7 +868,7 @@ describe("PublicLiquidityPool", function () {
868868
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
869869
expect(await usdc.balanceOf(user)).to.eq(2008n * USDC_DEC);
870870
expect(await usdc.balanceOf(user2)).to.eq(502n * USDC_DEC);
871-
expect(await liquidityPool.totalDeposited()).to.eq(0);
871+
expect(await liquidityPool.totalDeposited()).to.eq(1510n * USDC_DEC);
872872
expect(await liquidityPool.totalAssets()).to.eq(1506n * USDC_DEC);
873873
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
874874
expect(await liquidityPool.totalSupply()).to.eq(1500n * USDC_DEC);
@@ -1660,7 +1660,7 @@ describe("PublicLiquidityPool", function () {
16601660
expect(await usdc.balanceOf(liquidityPool)).to.eq(amountLiquidity - amountToReceive);
16611661
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
16621662
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
1663-
expect(await liquidityPool.totalDeposited()).to.eq(0);
1663+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
16641664
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
16651665
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
16661666
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);
@@ -1712,7 +1712,7 @@ describe("PublicLiquidityPool", function () {
17121712
expect(await usdc.balanceOf(liquidityPool)).to.eq(amountLiquidity - amountToReceive);
17131713
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
17141714
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
1715-
expect(await liquidityPool.totalDeposited()).to.eq(0);
1715+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
17161716
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
17171717
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
17181718
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);
@@ -1764,7 +1764,7 @@ describe("PublicLiquidityPool", function () {
17641764
expect(await usdc.balanceOf(liquidityPool)).to.eq(amountLiquidity - amountToReceive);
17651765
expect(await usdc.balanceOf(mockTarget)).to.eq(amountToReceive);
17661766
expect(await usdc.allowance(liquidityPool, mockTarget)).to.eq(0);
1767-
expect(await liquidityPool.totalDeposited()).to.eq(0);
1767+
expect(await liquidityPool.totalDeposited()).to.eq(amountLiquidity + fee);
17681768
expect(await liquidityPool.totalAssets()).to.eq(amountLiquidity + fee - protocolFee);
17691769
expect(await liquidityPool.protocolFee()).to.eq(protocolFee);
17701770
expect(await liquidityPool.totalSupply()).to.eq(amountLiquidity);

0 commit comments

Comments
 (0)