Skip to content

Commit

Permalink
Emit LoanFunded and LoanDrawnDown events (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bricestacey authored Oct 11, 2022
1 parent d4d1f6b commit 9be4b75
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
10 changes: 5 additions & 5 deletions contracts/FundingVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract FundingVault {
using SafeERC20 for IERC20;

address private immutable _loan;
IERC20 private immutable _asset;
IERC20 public immutable asset;

/**
* @dev Modifier restricting access to pool
Expand All @@ -26,18 +26,18 @@ contract FundingVault {
/**
* @dev Constructor for the vault
* @param loan address of loan
* @param asset asset held by vault
* @param asset_ asset held by vault
*/
constructor(address loan, address asset) {
constructor(address loan, address asset_) {
_loan = loan;
_asset = IERC20(asset);
asset = IERC20(asset_);
}

/**
* @dev Allows withdrawal of funds held by vault.
*/
function withdraw(uint256 amount, address receiver) external onlyLoan {
require(receiver != address(0), "FundingVault: 0 address");
_asset.safeTransfer(receiver, amount);
asset.safeTransfer(receiver, amount);
}
}
6 changes: 5 additions & 1 deletion contracts/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ contract Pool is IPool, ERC20 {
/**
* @dev Called by the pool manager, this transfers liquidity from the pool to a given loan.
*/
function fundLoan(address addr) external onlyManager {
function fundLoan(address addr)
external
onlyManager
atState(IPoolLifeCycleState.Active)
{
ILoan loan = ILoan(addr);

_liquidityAsset.safeApprove(address(loan), loan.principal());
Expand Down
10 changes: 10 additions & 0 deletions contracts/interfaces/ILoan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ struct ILoanNonFungibleCollateral {
}

interface ILoan {
/**
* @dev Emitted when loan is funded.
*/
event LoanFunded(address asset, uint256 amount);

/**
* @dev Emitted when the loan is drawn down.
*/
event LoanDrawnDown(address asset, uint256 amount);

/**
* @dev Emitted when a Loan's lifecycle state transitions
*/
Expand Down
12 changes: 12 additions & 0 deletions contracts/libraries/LoanLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ library LoanLib {
using SafeERC20 for IERC20;
using SafeMath for uint256;

/**
* @dev Emitted when loan is funded.
*/
event LoanFunded(address asset, uint256 amount);

/**
* @dev Emitted when the loan is drawn down.
*/
event LoanDrawnDown(address asset, uint256 amount);

/**
* @dev Emitted when collateral is posted to the loan.
*/
Expand Down Expand Up @@ -168,6 +178,7 @@ library LoanLib {
address(fundingVault),
amount
);
emit LoanFunded(liquidityAsset, amount);
return ILoanLifeCycleState.Funded;
}

Expand All @@ -180,5 +191,6 @@ library LoanLib {
address receiver
) public {
fundingVault.withdraw(amount, receiver);
emit LoanDrawnDown(address(fundingVault.asset()), amount);
}
}
29 changes: 26 additions & 3 deletions test/Loan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ describe("Loan", () => {
it("transitions Loan to Funded state", async () => {
const fixture = await loadFixture(deployFixture);
let { loan } = fixture;
const { borrower, collateralAsset, pool, poolManager } = fixture;
const { borrower, collateralAsset, liquidityAsset, pool, poolManager } =
fixture;

// Connect as borrower
loan = loan.connect(borrower);
Expand All @@ -376,8 +377,22 @@ describe("Loan", () => {
await expect(loan.postFungibleCollateral(collateralAsset.address, 100))
.not.to.be.reverted;
expect(await loan.state()).to.equal(1);
await expect(pool.connect(poolManager).fundLoan(loan.address)).not.to.be
.reverted;
const fundTx = pool.connect(poolManager).fundLoan(loan.address);
await expect(fundTx).not.to.be.reverted;
await expect(fundTx)
.to.emit(loan, "LoanFunded")
.withArgs(loan.liquidityAsset, 500_000);
await expect(fundTx).to.changeTokenBalance(
liquidityAsset,
await loan.fundingVault(),
500_000
);
await expect(fundTx).to.changeTokenBalance(
liquidityAsset,
pool,
-500_000
);

expect(await loan.state()).to.equal(4);
});

Expand Down Expand Up @@ -442,6 +457,14 @@ describe("Loan", () => {
borrower.address,
500_000
);
await expect(drawDownTx).to.changeTokenBalance(
liquidityAsset,
await loan.fundingVault(),
-500_000
);
await expect(drawDownTx)
.to.emit(loan, "LoanDrawnDown")
.withArgs(loan.liquidityAsset, 500_000);

// Try again
const drawDownTx2 = loan.connect(borrower).drawdown();
Expand Down

0 comments on commit 9be4b75

Please sign in to comment.