Skip to content

Commit

Permalink
Merge pull request #20 from ensuro/fix-max-deposit
Browse files Browse the repository at this point in the history
Fix maxDeposit bug when some etks had deposit limits and others don't
  • Loading branch information
gnarvaja authored Apr 22, 2024
2 parents 94447a2 + 0b537f0 commit 7405573
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
5 changes: 4 additions & 1 deletion contracts/ETokensBundleVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {EToken} from "@ensuro/core/contracts/EToken.sol";
import {ILPWhitelist} from "@ensuro/core/contracts/interfaces/ILPWhitelist.sol";
import {WadRayMath} from "@ensuro/core/contracts/dependencies/WadRayMath.sol";
import {MathUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol";
import {SafeMathUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";

/**
* @title ERC-4626 vault that invests in several eTokens with fixed allocations
Expand Down Expand Up @@ -172,8 +173,10 @@ contract ETokensBundleVault is AccessControlUpgradeable, UUPSUpgradeable, ERC462
/** @dev See {IERC4626-maxDeposit}. */
function maxDeposit(address receiver) public view virtual override returns (uint256 ret) {
if (!_isWhitelisted(receiver, true)) return 0;
bool addOk;
for (uint256 i; i < _underlying.length; i++) {
ret += _maxDepositInETK(_underlying[i].etk);
(addOk, ret) = SafeMathUpgradeable.tryAdd(ret, _maxDepositInETK(_underlying[i].etk));
if (!addOk) return type(uint256).max;
if (ret == type(uint256).max) return ret;
}
return ret;
Expand Down
30 changes: 20 additions & 10 deletions test/test-etokens-bundle-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
addRiskModule,
addEToken,
} = require("@ensuro/core/js/test-utils");
const { ETokenParameter } = require("@ensuro/core/js/enums");
const hre = require("hardhat");
const { ethers } = hre;
const { MaxUint256 } = ethers;
Expand All @@ -21,14 +22,6 @@ const { WEEK } = require("@ensuro/core/js/constants");
const helpers = require("@nomicfoundation/hardhat-network-helpers");
const { RiskModuleParameter, WhitelistStatus } = require("@ensuro/core/js/enums");

const ETokenParameter = {
/** TODO: add this to enums.js */
liquidityRequirement: 0,
minUtilizationRate: 1,
maxUtilizationRate: 2,
internalLoanInterestRate: 3,
};

describe("ETokensBundleVault contract tests", function () {
let _A;
let admin, anon, cust, lp, lp2, lp3, resolver;
Expand Down Expand Up @@ -696,21 +689,38 @@ describe("ETokensBundleVault contract tests", function () {
}
);

// LP3 deposits some funds to etks[0]
// LP3 deposits some funds to etks
await pool.connect(lp3).deposit(etks[0], _A(100));
await pool.connect(lp3).deposit(etks[1], _A(200));
await pool.connect(lp3).deposit(etks[2], _A(300));
await pool.connect(lp3).deposit(jrETKs[2], _A(1000));
await pool.connect(lp3).deposit(srETKs[1], _A(1000));

await etks[0].setParam(ETokenParameter.minUtilizationRate, _W("0.1"));

// Anyone can deposit because the other etks still accept money
expect(await vault.maxDeposit(anon)).to.be.equal(MaxUint256);
expect(await vault.maxMint(anon)).to.be.equal(MaxUint256);

// Lock some funds in etks[0] (jrETKs[1]) so UR is above 10% and accepts some deposits
let tx = await newPolicy(rms[1].connect(cust), _A(100), _A(50));
const policy0Evt = getTransactionEvent(pool.interface, await tx.wait(), "NewPolicy");

expect(await etks[0].utilizationRate()).to.be.equal(_W("0.5"));

// Anyone can deposit because the other etks still accept money
expect(await vault.maxDeposit(anon)).to.be.equal(MaxUint256);
expect(await vault.maxMint(anon)).to.be.equal(MaxUint256);

await etks[1].setParam(ETokenParameter.minUtilizationRate, _W("0.1"));
await etks[2].setParam(ETokenParameter.minUtilizationRate, _W("0.1"));

// etk[0] ts = 100, scr = 50. To UR = 10%, ts can increase by 400.
expect(await vault.maxDeposit(anon)).to.be.closeTo(_A(400), CENTS);
expect(await vault.maxMint(anon)).to.be.closeTo(_A(400), CENTS);

await rms[1].connect(resolver).resolvePolicy([...policy0Evt.args[1]], _A(0));

expect(await vault.maxDeposit(anon)).to.be.equal(0);
expect(await vault.maxMint(anon)).to.be.equal(0);

Expand Down Expand Up @@ -738,7 +748,7 @@ describe("ETokensBundleVault contract tests", function () {
);

// Lock some funds in etks[2], so UR is above 10% and accepts some deposits
let tx = await newPolicy(rms[2].connect(cust), _A(150), _A(75));
tx = await newPolicy(rms[2].connect(cust), _A(150), _A(75));
const policy1Evt = getTransactionEvent(pool.interface, await tx.wait(), "NewPolicy");

expect(await etks[2].utilizationRate()).to.be.equal(_W("0.5"));
Expand Down

0 comments on commit 7405573

Please sign in to comment.