Skip to content

Commit

Permalink
updated tests with proxy deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
PlayJok3r committed Jan 2, 2024
1 parent 55a4105 commit a07c734
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 56 deletions.
52 changes: 45 additions & 7 deletions test/BaseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,44 @@ export async function deployProxyContract(Contract: ContractFactory) {
const contractImpl = await Contract.deploy();
await contractImpl.deployed();

const Proxy = await ethers.getContractFactory("ERC1967Proxy");
const Proxy = await ethers.getContractFactory("ERC1967ProxyAccessControl");
const contractProxy = await Proxy.deploy(contractImpl.address, "0x");
await contractProxy.deployed();
const contract = await Contract.attach(contractProxy.address);
return contract;
}

export async function deployProxyContractUpgradeable(Contract: ContractFactory) {
// const Contract = await ethers.getContractFactory(ContractFactory);
const contractImpl = await Contract.deploy();
await contractImpl.deployed();

const Proxy = await ethers.getContractFactory("ERC1967Proxy");
const calldata = "0x";
const contractProxy = await Proxy.deploy(contractImpl.address, calldata);
await contractProxy.deployed();
const contract = await Contract.attach(contractProxy.address);
return contract;
}

export async function deployProxyContractUpgradeableInitialized(
Contract: ContractFactory,
initFunction: string = "initialize",
initParams?: unknown[],
) {
// const Contract = await ethers.getContractFactory(ContractFactory);
const contractImpl = await Contract.deploy();
await contractImpl.deployed();

const Proxy = await ethers.getContractFactory("ERC1967Proxy");
let fragment = await Contract.interface.getFunction(initFunction);
let calldata = await Contract.interface.encodeFunctionData(fragment, initParams);
const contractProxy = await Proxy.deploy(contractImpl.address, calldata);
await contractProxy.deployed();
const contract = await Contract.attach(contractProxy.address);
return contract;
}

export async function deployProtocolContracts(
protocolOwner: SignerWithAddress,
treasury: SignerWithAddress,
Expand Down Expand Up @@ -222,8 +253,9 @@ export async function deployPoolContracts(
)) as FirstLossCover;

const TranchesPolicy = await getTranchesPolicyContractFactory(tranchesPolicyContractName);
const tranchesPolicyContract = await TranchesPolicy.deploy();
await tranchesPolicyContract.deployed();
const tranchesPolicyContract = (await deployProxyContract(
TranchesPolicy,
)) as BaseTranchesPolicy;

const Pool = await ethers.getContractFactory("Pool");
const poolContract = (await deployProxyContract(Pool)) as Pool;
Expand All @@ -232,8 +264,12 @@ export async function deployPoolContracts(
const epochManagerContract = (await deployProxyContract(EpochManager)) as EpochManager;

const TrancheVault = await ethers.getContractFactory("TrancheVault");
const seniorTrancheVaultContract = (await deployProxyContract(TrancheVault)) as TrancheVault;
const juniorTrancheVaultContract = (await deployProxyContract(TrancheVault)) as TrancheVault;
const seniorTrancheVaultContract = (await deployProxyContractUpgradeable(
TrancheVault,
)) as TrancheVault;
const juniorTrancheVaultContract = (await deployProxyContractUpgradeable(
TrancheVault,
)) as TrancheVault;

const Calendar = await ethers.getContractFactory("Calendar");
const calendarContract = await Calendar.deploy();
Expand All @@ -253,9 +289,11 @@ export async function deployPoolContracts(
)) as CreditManagerContractType;

const Receivable = await ethers.getContractFactory("Receivable");
const receivableContract = (await deployProxyContract(Receivable)) as Receivable;
const receivableContract = (await deployProxyContractUpgradeableInitialized(
Receivable,
)) as Receivable;

await receivableContract.initialize();
// await receivableContract.initialize();
await receivableContract.grantRole(
receivableContract.DEFAULT_ADMIN_ROLE(),
poolOwner.getAddress(),
Expand Down
17 changes: 10 additions & 7 deletions test/FirstLossCoverTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import {
TrancheVault,
} from "../typechain-types";
import { FirstLossCoverStorage } from "../typechain-types/contracts/FirstLossCover";
import { CONSTANTS, deployAndSetupPoolContracts, deployProtocolContracts } from "./BaseTest";
import {
CONSTANTS,
deployAndSetupPoolContracts,
deployProtocolContracts,
deployProxyContract,
} from "./BaseTest";
import {
minBigNumber,
overrideFirstLossCoverConfig,
Expand Down Expand Up @@ -154,8 +159,7 @@ describe("FirstLossCover Tests", function () {
) {
await spendAllowance();
const PoolConfig = await ethers.getContractFactory("PoolConfig");
const newPoolConfigContract = await PoolConfig.deploy();
await newPoolConfigContract.deployed();
const newPoolConfigContract = (await deployProxyContract(PoolConfig)) as PoolConfig;

// Update the contract addresses.
await newPoolConfigContract.initialize("Test Pool", [
Expand Down Expand Up @@ -192,8 +196,8 @@ describe("FirstLossCover Tests", function () {
describe("When both the pool safe and the underlying token contracts are updated", function () {
it("Should reset the allowance of the pool safe contract", async function () {
const PoolSafe = await ethers.getContractFactory("PoolSafe");
const newPoolSafeContract = await PoolSafe.deploy();
await newPoolSafeContract.deployed();
const newPoolSafeContract = (await deployProxyContract(PoolSafe)) as PoolSafe;

const MockToken = await ethers.getContractFactory("MockToken");
const newMockTokenContract = await MockToken.deploy();
await newMockTokenContract.deployed();
Expand Down Expand Up @@ -235,8 +239,7 @@ describe("FirstLossCover Tests", function () {
describe("When only the pool safe contract is updated", function () {
it("Should reset the allowance of the pool safe contract", async function () {
const PoolSafe = await ethers.getContractFactory("PoolSafe");
const newPoolSafeContract = await PoolSafe.deploy();
await newPoolSafeContract.deployed();
const newPoolSafeContract = (await deployProxyContract(PoolSafe)) as PoolSafe;
await performUpdate(newPoolSafeContract, mockTokenContract);

// Make sure the old allowance has been reduced to 0, and the new allowance has been increase to uint256.max.
Expand Down
5 changes: 2 additions & 3 deletions test/PoolConfigCacheTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
RiskAdjustedTranchesPolicy,
TrancheVault,
} from "../typechain-types";
import { deployPoolContracts, deployProtocolContracts } from "./BaseTest";
import { deployPoolContracts, deployProtocolContracts, deployProxyContract } from "./BaseTest";

let defaultDeployer: SignerWithAddress,
protocolOwner: SignerWithAddress,
Expand Down Expand Up @@ -135,8 +135,7 @@ describe("PoolConfigCache Test", function () {

it("Should set new pool config", async function () {
const PoolConfig = await ethers.getContractFactory("PoolConfig");
const newPoolConfigContract = await PoolConfig.deploy();
await newPoolConfigContract.deployed();
const newPoolConfigContract = (await deployProxyContract(PoolConfig)) as PoolConfig;

await newPoolConfigContract.initialize("Test New Pool", [
humaConfigContract.address,
Expand Down
47 changes: 23 additions & 24 deletions test/PoolConfigTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
PayPeriodDuration,
deployAndSetupPoolContracts,
deployProtocolContracts,
deployProxyContract,
} from "./BaseTest";
import {
getMinFirstLossCoverRequirement,
Expand Down Expand Up @@ -96,60 +97,58 @@ describe("PoolConfig Tests", function () {
poolOwner,
);
const PoolConfig = await ethers.getContractFactory("PoolConfig");
poolConfigContract = await PoolConfig.deploy();
await poolConfigContract.deployed();
poolConfigContract = (await deployProxyContract(PoolConfig)) as PoolConfig;
await poolConfigContract.grantRole(
await poolConfigContract.DEFAULT_ADMIN_ROLE(),
poolOwner.address,
);

const PoolFeeManager = await ethers.getContractFactory("PoolFeeManager");
poolFeeManagerContract = await PoolFeeManager.deploy();
await poolFeeManagerContract.deployed();
poolFeeManagerContract = (await deployProxyContract(PoolFeeManager)) as PoolFeeManager;

const PoolSafe = await ethers.getContractFactory("PoolSafe");
poolSafeContract = await PoolSafe.deploy();
await poolSafeContract.deployed();
poolSafeContract = (await deployProxyContract(PoolSafe)) as PoolSafe;

const FirstLossCover = await ethers.getContractFactory("FirstLossCover");
borrowerFirstLossCoverContract = await FirstLossCover.deploy();
await borrowerFirstLossCoverContract.deployed();
affiliateFirstLossCoverContract = await FirstLossCover.deploy();
await affiliateFirstLossCoverContract.deployed();
borrowerFirstLossCoverContract = (await deployProxyContract(
FirstLossCover,
)) as FirstLossCover;
affiliateFirstLossCoverContract = (await deployProxyContract(
FirstLossCover,
)) as FirstLossCover;

const TranchesPolicy = await ethers.getContractFactory("RiskAdjustedTranchesPolicy");
tranchesPolicyContract = await TranchesPolicy.deploy();
await tranchesPolicyContract.deployed();
tranchesPolicyContract = (await deployProxyContract(
TranchesPolicy,
)) as RiskAdjustedTranchesPolicy;

const Pool = await ethers.getContractFactory("Pool");
poolContract = await Pool.deploy();
await poolContract.deployed();
poolContract = (await deployProxyContract(Pool)) as Pool;

const EpochManager = await ethers.getContractFactory("EpochManager");
epochManagerContract = await EpochManager.deploy();
await epochManagerContract.deployed();

const TrancheVault = await ethers.getContractFactory("TrancheVault");
seniorTrancheVaultContract = await TrancheVault.deploy();
await seniorTrancheVaultContract.deployed();
juniorTrancheVaultContract = await TrancheVault.deploy();
await juniorTrancheVaultContract.deployed();
seniorTrancheVaultContract = (await deployProxyContract(TrancheVault)) as TrancheVault;
juniorTrancheVaultContract = (await deployProxyContract(TrancheVault)) as TrancheVault;

const Calendar = await ethers.getContractFactory("Calendar");
calendarContract = await Calendar.deploy();
await calendarContract.deployed();

const Credit = await ethers.getContractFactory("MockPoolCredit");
creditContract = await Credit.deploy();
await creditContract.deployed();
creditContract = (await deployProxyContract(Credit)) as MockPoolCredit;

const CreditDueManager = await ethers.getContractFactory("CreditDueManager");
creditDueManagerContract = await CreditDueManager.deploy();
await creditDueManagerContract.deployed();
creditDueManagerContract = (await deployProxyContract(
CreditDueManager,
)) as CreditDueManager;

const CreditManager = await ethers.getContractFactory("BorrowerLevelCreditManager");
creditManagerContract = await CreditManager.deploy();
await creditManagerContract.deployed();
creditManagerContract = (await deployProxyContract(
CreditManager,
)) as BorrowerLevelCreditManager;
}

beforeEach(async function () {
Expand Down
20 changes: 13 additions & 7 deletions test/PoolFeeManagerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
RiskAdjustedTranchesPolicy,
TrancheVault,
} from "../typechain-types";
import { CONSTANTS, deployAndSetupPoolContracts, deployProtocolContracts } from "./BaseTest";
import {
CONSTANTS,
deployAndSetupPoolContracts,
deployProtocolContracts,
deployProxyContract,
} from "./BaseTest";
import {
overrideFirstLossCoverConfig,
overrideLPConfig,
Expand Down Expand Up @@ -169,8 +174,7 @@ describe("PoolFeeManager Tests", function () {
) {
await spendAllowance();
const PoolConfig = await ethers.getContractFactory("PoolConfig");
const newPoolConfigContract = await PoolConfig.deploy();
await newPoolConfigContract.deployed();
const newPoolConfigContract = (await deployProxyContract(PoolConfig)) as PoolConfig;

// Update the contract addresses.
await newPoolConfigContract.initialize("Test Pool", [
Expand Down Expand Up @@ -207,8 +211,9 @@ describe("PoolFeeManager Tests", function () {
describe("When both the first loss cover and the underlying token contracts are updated", function () {
it("Should reset the allowance of the first loss cover contract", async function () {
const FirstLossCover = await ethers.getContractFactory("FirstLossCover");
const newFirstLossCoverContract = await FirstLossCover.deploy();
await newFirstLossCoverContract.deployed();
const newFirstLossCoverContract = (await deployProxyContract(
FirstLossCover,
)) as FirstLossCover;
const MockToken = await ethers.getContractFactory("MockToken");
const newMockTokenContract = await MockToken.deploy();
await newMockTokenContract.deployed();
Expand Down Expand Up @@ -250,8 +255,9 @@ describe("PoolFeeManager Tests", function () {
describe("When only the first loss cover contract is updated", function () {
it("Should reset the allowance of the first loss cover contract", async function () {
const FirstLossCover = await ethers.getContractFactory("FirstLossCover");
const newFirstLossCoverContract = await FirstLossCover.deploy();
await newFirstLossCoverContract.deployed();
const newFirstLossCoverContract = (await deployProxyContract(
FirstLossCover,
)) as FirstLossCover;
await performUpdate(newFirstLossCoverContract, mockTokenContract);

// Make sure the old allowance has been reduced to 0, and the new allowance has been increase to uint256.max.
Expand Down
6 changes: 5 additions & 1 deletion test/ReceivableTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ describe("Receivable Test", function () {
poolOperator,
[lender],
);

await receivableContract
.connect(poolOwner)
.grantRole(receivableContract.MINTER_ROLE(), borrower.address);

console.log("borrower", borrower.address);
console.log(await receivableContract.MINTER_ROLE());
console.log(
await receivableContract.hasRole(receivableContract.MINTER_ROLE(), borrower.address),
);
await receivableContract.connect(borrower).createReceivable(
0, // currencyCode
1000,
Expand Down
15 changes: 8 additions & 7 deletions test/credit/ReceivableBackedCreditLineTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe("ReceivableBackedCreditLine Tests", function () {
creditContract as unknown,
creditDueManagerContract,
creditManagerContract as unknown,
receivableContract,
] = await deployAndSetupPoolContracts(
humaConfigContract,
mockTokenContract,
Expand All @@ -136,14 +137,14 @@ describe("ReceivableBackedCreditLine Tests", function () {
[lender, borrower],
);

const Receivable = await ethers.getContractFactory("Receivable");
receivableContract = await Receivable.deploy();
await receivableContract.deployed();
await receivableContract
.connect(poolOwner)
.grantRole(receivableContract.MINTER_ROLE(), poolOwner.address);

await receivableContract
.connect(poolOwner)
.grantRole(receivableContract.MINTER_ROLE(), borrower.address);

await receivableContract.connect(poolOwner).initialize();
await receivableContract.connect(poolOwner).addAdmin(poolOwner.address);
await receivableContract.connect(poolOwner).addMinter(borrower.address);
await receivableContract.connect(poolOwner).addMinter(poolOwner.address);
await poolConfigContract.connect(poolOwner).setReceivableAsset(receivableContract.address);

await borrowerFirstLossCoverContract
Expand Down

0 comments on commit a07c734

Please sign in to comment.