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

Emit LoanFunded and LoanDrawnDown events #45

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -216,7 +216,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 collateral is posted to the loan.
*/
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 @@ -360,7 +360,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 @@ -370,8 +371,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 @@ -436,6 +451,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