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

Refactor Pool / loan factories to remove duplication and cover gaps #128

Merged
merged 4 commits into from
Dec 6, 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
22 changes: 17 additions & 5 deletions contracts/LoanFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ contract LoanFactory {
address pool,
address liquidityAsset,
ILoanSettings memory settings
) public virtual returns (address LoanAddress) {
) public returns (address LoanAddress) {
require(
_serviceConfiguration.paused() == false,
"LoanFactory: Protocol paused"
);
address addr = initializeLoan(borrower, pool, liquidityAsset, settings);
emit LoanCreated(addr);
_isLoan[addr] = true;
return addr;
}

/**
* @dev Internal initialization of Loan contract
*/
function initializeLoan(
address borrower,
address pool,
address liquidityAsset,
ILoanSettings memory settings
) internal virtual returns (address) {
Loan loan = new Loan(
_serviceConfiguration,
address(this),
Expand All @@ -49,10 +64,7 @@ contract LoanFactory {
liquidityAsset,
settings
);
address addr = address(loan);
emit LoanCreated(addr);
_isLoan[addr] = true;
return addr;
return address(loan);
}

/**
Expand Down
23 changes: 16 additions & 7 deletions contracts/PoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ contract PoolFactory is IPoolFactory {
/**
* @dev Reference to the ServiceConfiguration contract
*/
address private _serviceConfiguration;
address internal _serviceConfiguration;

/**
* @dev Reference to the WithdrawControllerFactory contract
*/
address private _withdrawControllerFactory;
address internal _withdrawControllerFactory;

/**
* @dev Reference to the PoolControllerFactory contract
*/
address private _poolControllerFactory;
address internal _poolControllerFactory;

constructor(
address serviceConfiguration,
Expand Down Expand Up @@ -83,6 +83,18 @@ contract PoolFactory is IPoolFactory {
);

// Create the pool
address addr = initializePool(liquidityAsset, settings);
emit PoolCreated(addr);
return addr;
}

/**
* @dev Creates the new Pool contract.
*/
function initializePool(
address liquidityAsset,
IPoolConfigurableSettings calldata settings
) internal virtual returns (address) {
Pool pool = new Pool(
liquidityAsset,
msg.sender,
Expand All @@ -93,9 +105,6 @@ contract PoolFactory is IPoolFactory {
"PerimeterPoolToken",
"PPT"
);

address addr = address(pool);
emit PoolCreated(addr);
return addr;
return address(pool);
}
}
14 changes: 4 additions & 10 deletions contracts/permissioned/PermissionedLoanFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ contract PermissionedLoanFactory is LoanFactory {

/**
* @inheritdoc LoanFactory
* @dev Deploys PermissionedLoan
*/
function createLoan(
function initializeLoan(
address borrower,
address pool,
address liquidityAsset,
ILoanSettings memory settings
) public virtual override returns (address LoanAddress) {
require(
_serviceConfiguration.paused() == false,
"LoanFactory: Protocol paused"
);
) internal override returns (address) {
Loan loan = new PermissionedLoan(
_serviceConfiguration,
address(this),
Expand All @@ -34,9 +31,6 @@ contract PermissionedLoanFactory is LoanFactory {
liquidityAsset,
settings
);
address addr = address(loan);
emit LoanCreated(addr);
_isLoan[addr] = true;
return addr;
return address(loan);
}
}
8 changes: 4 additions & 4 deletions contracts/permissioned/PermissionedPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ contract PermissionedPool is Pool {
address liquidityAsset,
address poolAdmin,
address serviceConfiguration,
address withdrawController,
address poolController,
address withdrawControllerFactory,
address poolControllerFactory,
address poolAccessControlFactory,
IPoolConfigurableSettings memory poolSettings,
string memory tokenName,
Expand All @@ -43,8 +43,8 @@ contract PermissionedPool is Pool {
liquidityAsset,
poolAdmin,
serviceConfiguration,
withdrawController,
poolController,
withdrawControllerFactory,
poolControllerFactory,
poolSettings,
tokenName,
tokenSymbol
Expand Down
70 changes: 31 additions & 39 deletions contracts/permissioned/PermissionedPoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,18 @@
pragma solidity ^0.8.16;

import "./interfaces/IPermissionedServiceConfiguration.sol";
import "../interfaces/IPoolFactory.sol";
import "../PoolFactory.sol";
import "./PermissionedPool.sol";

/**
* @title PermissionedPoolFactory
*/
contract PermissionedPoolFactory is IPoolFactory {
/**
* @dev Reference to the ServiceConfiguration contract
*/
address private _serviceConfiguration;

contract PermissionedPoolFactory is PoolFactory {
/**
* @dev Reference to a PoolAccessControlFactory
*/
address private _poolAccessControlFactory;

/**
* @dev Reference to the WithdrawControllerFactory contract
*/
address private _withdrawControllerFactory;

/**
* @dev Reference to the PoolControllerFactory contract
*/
address private _poolControllerFactory;

constructor(
address serviceConfiguration,
address withdrawControllerFactory,
address poolControllerFactory,
address poolAccessControlFactory
) {
_serviceConfiguration = serviceConfiguration;
_withdrawControllerFactory = withdrawControllerFactory;
_poolControllerFactory = poolControllerFactory;
_poolAccessControlFactory = poolAccessControlFactory;
}

/**
* @dev Check that `msg.sender` is a PoolAdmin.
*/
Expand All @@ -54,18 +27,40 @@ contract PermissionedPoolFactory is IPoolFactory {
_;
}

constructor(
address serviceConfiguration,
address withdrawControllerFactory,
address poolControllerFactory,
address poolAccessControlFactory
)
PoolFactory(
serviceConfiguration,
withdrawControllerFactory,
poolControllerFactory
)
{
_poolAccessControlFactory = poolAccessControlFactory;
}

/**
* @inheritdoc IPoolFactory
* @inheritdoc PoolFactory
* @dev Restricts callers to verified PoolAdmins
*/
function createPool(
address liquidityAsset,
IPoolConfigurableSettings calldata settings
) public override onlyVerifiedPoolAdmin returns (address poolAddress) {
require(
settings.withdrawRequestPeriodDuration > 0,
"PoolFactory: Invalid duration"
);
) public override onlyVerifiedPoolAdmin returns (address) {
return super.createPool(liquidityAsset, settings);
}

/**
* @inheritdoc PoolFactory
* @dev Injects access control into the PermissionedPool
*/
function initializePool(
address liquidityAsset,
IPoolConfigurableSettings calldata settings
) internal override returns (address) {
PermissionedPool pool = new PermissionedPool(
liquidityAsset,
msg.sender,
Expand All @@ -77,9 +72,6 @@ contract PermissionedPoolFactory is IPoolFactory {
"PerimeterPoolToken",
"PPT"
);
address addr = address(pool);

emit PoolCreated(addr);
return addr;
return address(pool);
}
}
5 changes: 5 additions & 0 deletions test/permissioned/PermissionedPoolFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ describe("PermissionedPoolFactory", () => {
.connect(operator)
.updateTermsOfService("https://terms.example");

// Add liquidity asset
await permissionedServiceConfiguration
.connect(operator)
.setLiquidityAsset(liquidityAsset.address, true);

// Deploy the PoolAdminAccessControl contract
const PoolAdminAccessControl = await ethers.getContractFactory(
"PoolAdminAccessControl"
Expand Down
9 changes: 5 additions & 4 deletions test/permissioned/PoolAccessControl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ describe("PoolAccessControl", () => {
async function deployFixture() {
const [operator, poolAdmin, verifier, poolParticipant, ...otherAccounts] =
await ethers.getSigners();
const { pool, tosAcceptanceRegistry } = await deployPermissionedPool({
operator,
poolAdmin
});
const { pool, tosAcceptanceRegistry, liquidityAsset } =
await deployPermissionedPool({
operator,
poolAdmin
});

await tosAcceptanceRegistry.updateTermsOfService("http://circle.com");

Expand Down
4 changes: 4 additions & 0 deletions test/support/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export async function deployPermissionedPool({
poolAdminAccessControl
} = await deployPermissionedServiceConfiguration(operator);

await serviceConfiguration
.connect(operator)
.setLiquidityAsset(liquidityAsset.address, true);

await tosAcceptanceRegistry.connect(poolAdmin).acceptTermsOfService();
await performVeriteVerification(poolAdminAccessControl, operator, poolAdmin);

Expand Down