Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAL-91 (cont.) Prevent deploying capital to new loans unless it exists in the pool #99

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ contract Pool is IPool, ERC20 {
ILoan loan = ILoan(addr);
uint256 principal = loan.principal();

require(totalAvailableAssets() >= principal, "Pool: not enough assets");
require(liquidityPoolAssets() >= principal, "Pool: not enough assets");

_liquidityAsset.safeApprove(addr, principal);
loan.fund();
Expand Down
59 changes: 56 additions & 3 deletions test/controllers/PoolController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe("PoolController", () => {
serviceConfiguration
);

const { loan: otherLoan } = await deployLoan(
pool.address,
borrower.address,
liquidityAsset.address,
serviceConfiguration
);

return {
operator,
poolAdmin,
Expand All @@ -38,6 +45,7 @@ describe("PoolController", () => {
otherAccounts,
pool,
loan,
otherLoan,
liquidityAsset,
collateralAsset,
poolController
Expand Down Expand Up @@ -482,7 +490,7 @@ describe("PoolController", () => {
);
});

it("reverts if loan amount exceeds totalAvailableAssets in the pool", async () => {
it("reverts if trying to fund loan with withdrawal-earmarked funds", async () => {
const {
pool,
poolController,
Expand All @@ -499,15 +507,15 @@ describe("PoolController", () => {
pool,
otherAccount,
liquidityAsset,
DEFAULT_LOAN_SETTINGS.principal
DEFAULT_LOAN_SETTINGS.principal * 2
);

// Now request withdraw
const redeemAmount = await pool.maxRedeemRequest(otherAccount.address);
await pool.connect(otherAccount).requestRedeem(redeemAmount);

// fast forward and crank
const { withdrawRequestPeriodDuration } = await poolController.settings();
const { withdrawRequestPeriodDuration } = await pool.settings();
await time.increase(withdrawRequestPeriodDuration);
await pool.crank();

Expand All @@ -518,11 +526,56 @@ describe("PoolController", () => {

// check that totalAvailableAssets is dust
expect(await pool.totalAvailableAssets()).to.lessThan(10);

// Check that there is technically enough funds to cover the loan
expect(await liquidityAsset.balanceOf(pool.address)).is.greaterThan(
await loan.principal()
);

// ...but that the pool won't allow it
await expect(
poolController.connect(poolAdmin).fundLoan(loan.address)
).to.be.revertedWith("Pool: not enough assets");
});

it("reverts if trying to fund loan with liquidity already deployed", async () => {
const {
pool,
liquidityAsset,
otherAccount,
borrower,
poolAdmin,
loan,
otherLoan,
poolController
} = await loadFixture(loadPoolFixture);

await activatePool(pool, poolAdmin, liquidityAsset);
await collateralizeLoan(loan, borrower, liquidityAsset);
await depositToPool(
pool,
otherAccount,
liquidityAsset,
DEFAULT_LOAN_SETTINGS.principal * 1.5
);

// fund first loan
expect(await pool.totalAvailableAssets()).to.equal(
DEFAULT_LOAN_SETTINGS.principal * 1.5
);
await fundLoan(loan, poolController, poolAdmin);

// total value locked by the Pool is the same, since the funds just shifted to the loan
expect(await pool.totalAvailableAssets()).to.equal(
DEFAULT_LOAN_SETTINGS.principal * 1.5
);

// confirm that funding a new loan will fail
await expect(
poolController.connect(poolAdmin).fundLoan(otherLoan.address)
).to.be.revertedWith("Pool: not enough assets");
});

it("reverts if not called by Pool Controller", async () => {
const { poolController, otherAccount } = await loadFixture(
loadPoolFixture
Expand Down