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-43] Update PermissionedLoan to use override modifier pattern #122

Merged
merged 7 commits into from
Dec 1, 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
34 changes: 26 additions & 8 deletions contracts/Loan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ contract Loan is ILoan {
_;
}

/**
* @dev Modifier that can be overriden by derived classes to enforce
* access control.
*/
modifier onlyPermittedBorrower() virtual {
_;
}

/**
* @dev Modifier that requires the loan not be in a terminal state.
*/
Expand Down Expand Up @@ -157,6 +165,7 @@ contract Loan is ILoan {
*/
function cancelCollateralized()
external
onlyPermittedBorrower
onlyBorrower
atState(ILoanLifeCycleState.Collateralized)
returns (ILoanLifeCycleState)
Expand Down Expand Up @@ -198,8 +207,8 @@ contract Loan is ILoan {
}

/**
* @dev Claims specific collateral types. Can be called by the borrower (when Canceled or Matured)
* or by the PA (when Defaulted)
* @dev Claims specific collateral types. Can be called by the borrower
* (when Canceled or Matured) or by the PA (when Defaulted)
*/
function claimCollateral(
address[] memory assets,
Expand All @@ -226,8 +235,9 @@ contract Loan is ILoan {
* @dev Post ERC20 tokens as collateral
*/
function postFungibleCollateral(address asset, uint256 amount)
public
external
virtual
onlyPermittedBorrower
onlyBorrower
onlyNonTerminalState
returns (ILoanLifeCycleState)
Expand All @@ -251,8 +261,9 @@ contract Loan is ILoan {
* @dev Post ERC721 tokens as collateral
*/
function postNonFungibleCollateral(address asset, uint256 tokenId)
public
external
virtual
onlyPermittedBorrower
onlyBorrower
onlyNonTerminalState
returns (ILoanLifeCycleState)
Expand Down Expand Up @@ -309,8 +320,9 @@ contract Loan is ILoan {
* @dev Drawdown the Loan
*/
function drawdown(uint256 amount)
public
external
virtual
onlyPermittedBorrower
onlyBorrower
returns (uint256)
{
Expand All @@ -331,7 +343,11 @@ contract Loan is ILoan {
* @dev Prepay principal.
* @dev Only callable by open term loans
*/
function paydownPrincipal(uint256 amount) external onlyBorrower {
function paydownPrincipal(uint256 amount)
external
onlyPermittedBorrower
onlyBorrower
{
require(outstandingPrincipal >= amount, "Loan: amount too high");
require(settings.loanType == ILoanType.Open, "Loan: invalid loan type");
LoanLib.paydownPrincipal(liquidityAsset, amount, fundingVault);
Expand All @@ -342,7 +358,8 @@ contract Loan is ILoan {
* @dev Complete the next payment according to loan schedule inclusive of all fees.
*/
function completeNextPayment()
public
external
onlyPermittedBorrower
onlyBorrower
atState(ILoanLifeCycleState.Active)
returns (uint256)
Expand Down Expand Up @@ -401,7 +418,8 @@ contract Loan is ILoan {
* @dev Complete the final payment of the loan.
*/
function completeFullPayment()
public
external
onlyPermittedBorrower
onlyBorrower
atState(ILoanLifeCycleState.Active)
returns (uint256)
Expand Down
40 changes: 2 additions & 38 deletions contracts/permissioned/PermissionedLoan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ contract PermissionedLoan is Loan {
/**
* @dev a modifier to only allow valid borrowers to perform an action
*/
modifier onlyValidBorrower() {
modifier onlyPermittedBorrower() override {
require(
poolAccessControl.isAllowed(msg.sender),
"caller is not a valid borrower"
"BORROWER_NOT_ALLOWED"
);
_;
}
Expand Down Expand Up @@ -50,40 +50,4 @@ contract PermissionedLoan is Loan {
{
poolAccessControl = PermissionedPool(pool).poolAccessControl();
}

/**
* @inheritdoc Loan
*/
function postFungibleCollateral(address asset, uint256 amount)
public
override
onlyValidBorrower
returns (ILoanLifeCycleState)
{
return super.postFungibleCollateral(asset, amount);
}

/**
* @inheritdoc Loan
*/
function postNonFungibleCollateral(address asset, uint256 tokenId)
public
override
onlyValidBorrower
returns (ILoanLifeCycleState)
{
return super.postNonFungibleCollateral(asset, tokenId);
}

/**
* @inheritdoc Loan
*/
function drawdown(uint256 amount)
public
override
onlyValidBorrower
returns (uint256)
{
return super.drawdown(amount);
}
}
5 changes: 1 addition & 4 deletions contracts/permissioned/PermissionedPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ contract PermissionedPool is Pool {
* @dev a modifier to only allow valid lenders to perform an action
*/
modifier onlyPermittedLender() override {
require(
poolAccessControl.isAllowed(msg.sender),
"caller is not a valid lender"
);
require(poolAccessControl.isAllowed(msg.sender), "LENDER_NOT_ALLOWED");
_;
}

Expand Down
46 changes: 43 additions & 3 deletions test/permissioned/PermissionedLoan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("PermissionedLoan", () => {
loan
.connect(borrower)
.postFungibleCollateral(liquidityAsset.address, 10)
).to.be.revertedWith("caller is not a valid borrower");
).to.be.revertedWith("BORROWER_NOT_ALLOWED");
});
});

Expand All @@ -200,7 +200,7 @@ describe("PermissionedLoan", () => {
loan
.connect(borrower)
.postNonFungibleCollateral(liquidityAsset.address, "123")
).to.be.revertedWith("caller is not a valid borrower");
).to.be.revertedWith("BORROWER_NOT_ALLOWED");
});
});

Expand Down Expand Up @@ -240,9 +240,49 @@ describe("PermissionedLoan", () => {
.connect(poolAdmin)
.removeParticipant(borrower.address);
await expect(loan.connect(borrower).drawdown(1)).to.be.revertedWith(
"caller is not a valid borrower"
"BORROWER_NOT_ALLOWED"
);
});
});

describe("cancelCollateralized()", () => {
it("reverts if borrower not an allowed participant", async () => {
const { loan, borrower } = await loadFixture(loadLoanFixture);

await expect(
loan.connect(borrower).cancelCollateralized()
).to.be.revertedWith("BORROWER_NOT_ALLOWED");
});
});

describe("paydownPrincipal()", () => {
it("reverts if borrower not an allowed participant", async () => {
const { loan, borrower } = await loadFixture(loadLoanFixture);

await expect(
loan.connect(borrower).paydownPrincipal(100)
).to.be.revertedWith("BORROWER_NOT_ALLOWED");
});
});

describe("completeNextPayment()", () => {
it("reverts if borrower not an allowed participant", async () => {
const { loan, borrower } = await loadFixture(loadLoanFixture);

await expect(
loan.connect(borrower).completeNextPayment()
).to.be.revertedWith("BORROWER_NOT_ALLOWED");
});
});

describe("completeFullPayment()", () => {
it("reverts if borrower not an allowed participant", async () => {
const { loan, borrower } = await loadFixture(loadLoanFixture);

await expect(
loan.connect(borrower).completeFullPayment()
).to.be.revertedWith("BORROWER_NOT_ALLOWED");
});
});
});
});
8 changes: 4 additions & 4 deletions test/permissioned/PermissionedPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("PermissionedPool", () => {

await expect(
pool.connect(otherAccount).deposit(10, otherAccount.address)
).to.be.revertedWith("caller is not a valid lender");
).to.be.revertedWith("LENDER_NOT_ALLOWED");
});

it("allows deposits if allowed lender", async () => {
Expand All @@ -126,7 +126,7 @@ describe("PermissionedPool", () => {

await expect(
pool.connect(otherAccount).mint(10, otherAccount.address)
).to.be.revertedWith("caller is not a valid lender");
).to.be.revertedWith("LENDER_NOT_ALLOWED");
});

it("allows minting if allowed lender", async () => {
Expand Down Expand Up @@ -155,7 +155,7 @@ describe("PermissionedPool", () => {
pool
.connect(otherAccount)
.redeem(10, otherAccount.address, otherAccount.address)
).to.be.revertedWith("caller is not a valid lender");
).to.be.revertedWith("LENDER_NOT_ALLOWED");
});

it("allows redeeming if allowed lender", async () => {
Expand Down Expand Up @@ -187,7 +187,7 @@ describe("PermissionedPool", () => {
pool
.connect(otherAccount)
.withdraw(10, otherAccount.address, otherAccount.address)
).to.be.revertedWith("caller is not a valid lender");
).to.be.revertedWith("LENDER_NOT_ALLOWED");
});

it("allows withdrawing if allowed lender", async () => {
Expand Down