Skip to content

Commit

Permalink
Fix lint warnings and type errors (and check types in CI) (#145)
Browse files Browse the repository at this point in the history
* Fix lint in typescript files

* Update types to allow permissioned pools for activation

* Fix solidity lint warnings

* Add type-check script

* Fix type check, add to github actions

* Fix format

* Compile before type-check
  • Loading branch information
venables authored Dec 9, 2022
1 parent f7e3198 commit f458dfb
Show file tree
Hide file tree
Showing 30 changed files with 77 additions and 99 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
cache: "npm"
- run: npm install
- run: npx hardhat compile
- run: npm run format:check
- run: npm run lint
- run: npm run type-check
- run: npm test
6 changes: 5 additions & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.16"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
"func-name-mixedcase": "off",
"func-visibility": ["warn", { "ignoreConstructors": true }],
"no-empty-blocks": "off",
"not-rely-on-time": "off",
"reason-string": ["warn", { "maxLength": 50 }]
}
}
23 changes: 12 additions & 11 deletions contracts/Loan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import "./upgrades/BeaconImplementation.sol";
*/
contract Loan is ILoan, BeaconImplementation {
using SafeMath for uint256;
uint256 constant RAY = 10**27;

IServiceConfiguration private _serviceConfiguration;
address private _factory;
Expand Down Expand Up @@ -153,10 +152,12 @@ contract Loan is ILoan, BeaconImplementation {
uint256 paymentsTotal = settings
.principal
.mul(settings.apr)
.mul(settings.duration.mul(RAY).div(360))
.div(RAY)
.mul(settings.duration.mul(LoanLib.RAY).div(360))
.div(LoanLib.RAY)
.div(10000);
payment = paymentsTotal.mul(RAY).div(paymentsRemaining).div(RAY);
payment = paymentsTotal.mul(LoanLib.RAY).div(paymentsRemaining).div(
LoanLib.RAY
);
}

/**
Expand Down Expand Up @@ -393,7 +394,7 @@ contract Loan is ILoan, BeaconImplementation {
IPool(_pool).serviceFeeBps(),
block.timestamp,
paymentDueDate,
RAY
LoanLib.RAY
);

LoanLib.payFees(
Expand Down Expand Up @@ -430,7 +431,7 @@ contract Loan is ILoan, BeaconImplementation {
IPool(_pool).serviceFeeBps(),
block.timestamp,
paymentDueDate,
RAY
LoanLib.RAY
);
}

Expand All @@ -445,23 +446,23 @@ contract Loan is ILoan, BeaconImplementation {
atState(ILoanLifeCycleState.Active)
returns (uint256)
{
uint256 scalingValue = RAY;
uint256 scalingValue = LoanLib.RAY;

if (settings.loanType == ILoanType.Open) {
// If an open term loan payment is not overdue, we will prorate the
// payment
if (paymentDueDate > block.timestamp) {
// Calculate the scaling value
// RAY - ((paymentDueDate - blocktimestamp) * RAY / paymentPeriod (seconds))
scalingValue = RAY.sub(
(paymentDueDate - block.timestamp).mul(RAY).div(
// LoanLib.RAY - ((paymentDueDate - blocktimestamp) * LoanLib.RAY / paymentPeriod (seconds))
scalingValue = LoanLib.RAY.sub(
(paymentDueDate - block.timestamp).mul(LoanLib.RAY).div(
settings.paymentPeriod * 1 days
)
);
}
} else {
// Fixed term loans must pay all outstanding interest payments and fees.
scalingValue = RAY.mul(paymentsRemaining);
scalingValue = LoanLib.RAY.mul(paymentsRemaining);
}

ILoanFees memory _fees = LoanLib.previewFees(
Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/LoanLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ library LoanLib {
using SafeERC20 for IERC20;
using SafeMath for uint256;

uint256 constant RAY = 10**27;
uint256 public constant RAY = 10**27;

/**
* @dev Emitted when loan is funded.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"lint:solidity": "solhint contracts/**/*.sol",
"test": "npx hardhat test",
"test:coverage": "npx hardhat coverage",
"test:gas": "REPORT_GAS=true npx hardhat test"
"test:gas": "REPORT_GAS=true npx hardhat test",
"type-check": "tsc --pretty --noEmit"
},
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
Expand Down
8 changes: 7 additions & 1 deletion scripts/deploy-permissionless.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { ethers } from "hardhat";
import hre from "hardhat";

type ExtendedHreNetworkConfig = typeof hre.network.config & {
usdcAddress: string | undefined;
};

async function main() {
// The token we use for the liquidity asset must exist. If it is not defined, we'll deploy a mock token.
let usdcAddress = hre.network.config.usdcAddress;
let usdcAddress = (hre.network.config as ExtendedHreNetworkConfig)
.usdcAddress;

if (!usdcAddress) {
const Usdc = await ethers.getContractFactory("MockERC20");
const usdc = await Usdc.deploy("USD Coin", "USDC", 6);
Expand Down
12 changes: 11 additions & 1 deletion scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { ethers } from "hardhat";
import hre from "hardhat";

type ExtendedHreNetworkConfig = typeof hre.network.config & {
usdcAddress: string | undefined;
};

async function main() {
// The token we use for the liquidity asset must exist. If it is not defined, we'll deploy a mock token.
let usdcAddress = hre.network.config.usdcAddress;
let usdcAddress = (hre.network.config as ExtendedHreNetworkConfig)
.usdcAddress;

if (!usdcAddress) {
const Usdc = await ethers.getContractFactory("MockERC20");
const usdc = await Usdc.deploy("USD Coin", "USDC", 6);
Expand Down Expand Up @@ -31,6 +37,8 @@ async function main() {
"ToSAcceptanceRegistry"
);
const toSAcceptanceRegistry = await ToSAcceptanceRegistry.deploy(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
serviceConfiguration.address
);
await toSAcceptanceRegistry.deployed();
Expand Down Expand Up @@ -60,6 +68,8 @@ async function main() {
"PoolAdminAccessControl"
);
const poolAdminAccessControl = await PoolAdminAccessControl.deploy(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
serviceConfiguration.address
);
await poolAdminAccessControl.deployed();
Expand Down
1 change: 0 additions & 1 deletion test/FirstLossVault.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { ethers } from "hardhat";
import { deployServiceConfiguration } from "./support/serviceconfiguration";

describe("FirstLossVault", () => {
const VAULT_BALANCE = ethers.BigNumber.from(100);
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/PoolController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ describe("PoolController", () => {

// double check that the funds are now available for withdraw
expect(await pool.maxRedeem(otherAccount.address)).to.equal(
redeemAmount - 2
redeemAmount.sub(2)
);

// check that totalAvailableAssets is dust
Expand Down
2 changes: 1 addition & 1 deletion test/permissioned/PermissionedLoan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getCommonSigners } from "../support/utils";

describe("PermissionedLoan", () => {
async function loadLoanFixture() {
const { operator, poolAdmin, borrower, lender } = await getCommonSigners();
const { poolAdmin, borrower, lender } = await getCommonSigners();

const { mockERC20: liquidityAsset } = await deployMockERC20();
const NftAsset = await ethers.getContractFactory("MockERC721");
Expand Down
6 changes: 2 additions & 4 deletions test/permissioned/PermissionedLoanFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import { expect } from "chai";
import { ethers } from "hardhat";
import { deployMockERC20 } from "../support/erc20";
import { deployPermissionedPool, DEFAULT_POOL_SETTINGS } from "../support/pool";
import { DEFAULT_LOAN_SETTINGS, deployPermissionedLoan } from "../support/loan";
import { deployPermissionedServiceConfiguration } from "../support/serviceconfiguration";
import { deployToSAcceptanceRegistry } from "../support/tosacceptanceregistry";
import { DEFAULT_LOAN_SETTINGS } from "../support/loan";
import { findEventByName, getCommonSigners } from "../support/utils";

describe("PermissionedLoanFactory", () => {
async function deployFixture() {
// Contracts are deployed using the first signer/account by default
const { operator, poolAdmin, borrower, otherAccounts, deployer } =
const { operator, poolAdmin, borrower, deployer } =
await getCommonSigners();

// Deploy the liquidity asset
Expand Down
14 changes: 3 additions & 11 deletions test/permissioned/PermissionedPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@ import {

describe("PermissionedPool", () => {
async function loadPoolFixture() {
const [
operator,
protocolAdmin,
poolAdmin,
otherAccount,
thirdAccount,
allowedLender
] = await ethers.getSigners();
const [poolAdmin, otherAccount, thirdAccount, allowedLender] =
await ethers.getSigners();
const {
pool,
liquidityAsset,
poolAccessControl,
tosAcceptanceRegistry,
poolController
} = await deployPermissionedPool({
operator,
poolAdmin: poolAdmin,
protocolAdmin: protocolAdmin
poolAdmin: poolAdmin
});

// allow allowedLender
Expand Down
3 changes: 1 addition & 2 deletions test/scenarios/business/1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("Business Scenario 1", () => {
}

async function loadFixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrowerOne, borrowerTwo] =
const [poolAdmin, lenderA, lenderB, borrowerOne, borrowerTwo] =
await ethers.getSigners();

const startTime = await time.latest();
Expand All @@ -66,7 +66,6 @@ describe("Business Scenario 1", () => {
6
);
const { pool, serviceConfiguration, poolController } = await deployPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
4 changes: 1 addition & 3 deletions test/scenarios/business/3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe("Business Scenario 3", () => {
}

async function fixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrower] =
await ethers.getSigners();
const [poolAdmin, lenderA, lenderB, borrower] = await ethers.getSigners();
const endTime = (await time.latest()) + 5_184_000; // 60 days.
const poolSettings = {
endDate: endTime, // Jan 1, 2050
Expand All @@ -52,7 +51,6 @@ describe("Business Scenario 3", () => {
6
);
const { pool, serviceConfiguration, poolController } = await deployPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
4 changes: 1 addition & 3 deletions test/scenarios/business/4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ describe("Business Scenario 4", () => {
}

async function fixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrower] =
await ethers.getSigners();
const [poolAdmin, lenderA, lenderB, borrower] = await ethers.getSigners();
const endTime = (await time.latest()) + 5_184_000; // 60 days.
const poolSettings = {
endDate: endTime, // Jan 1, 2050
Expand All @@ -52,7 +51,6 @@ describe("Business Scenario 4", () => {
6
);
const { pool, serviceConfiguration, poolController } = await deployPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
3 changes: 1 addition & 2 deletions test/scenarios/business/permissioned/1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("Permissioned Business Scenario 1", () => {
}

async function loadFixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrowerOne, borrowerTwo] =
const [poolAdmin, lenderA, lenderB, borrowerOne, borrowerTwo] =
await ethers.getSigners();

const startTime = await time.latest();
Expand All @@ -75,7 +75,6 @@ describe("Permissioned Business Scenario 1", () => {
poolAccessControl,
tosAcceptanceRegistry
} = await deployPermissionedPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
4 changes: 1 addition & 3 deletions test/scenarios/business/permissioned/2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ describe("Permissioned Business Scenario 2", () => {
}

async function fixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrower] =
await ethers.getSigners();
const [poolAdmin, lenderA, lenderB, borrower] = await ethers.getSigners();
const endTime = (await time.latest()) + 5_184_000; // 60 days.
const poolSettings = {
endDate: endTime, // Jan 1, 2050
Expand All @@ -60,7 +59,6 @@ describe("Permissioned Business Scenario 2", () => {
poolAccessControl,
tosAcceptanceRegistry
} = await deployPermissionedPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
4 changes: 1 addition & 3 deletions test/scenarios/business/permissioned/3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ describe("Permissioned Business Scenario 3", () => {
}

async function fixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrower] =
await ethers.getSigners();
const [poolAdmin, lenderA, lenderB, borrower] = await ethers.getSigners();
const endTime = (await time.latest()) + 5_184_000; // 60 days.
const poolSettings = {
endDate: endTime, // Jan 1, 2050
Expand All @@ -59,7 +58,6 @@ describe("Permissioned Business Scenario 3", () => {
poolAccessControl,
tosAcceptanceRegistry
} = await deployPermissionedPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
4 changes: 1 addition & 3 deletions test/scenarios/business/permissioned/4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ describe("Permissioned Business Scenario 4", () => {
}

async function fixtures() {
const [operator, poolAdmin, lenderA, lenderB, borrower] =
await ethers.getSigners();
const [poolAdmin, lenderA, lenderB, borrower] = await ethers.getSigners();
const endTime = (await time.latest()) + 5_184_000; // 60 days.
const poolSettings = {
endDate: endTime, // Jan 1, 2050
Expand All @@ -59,7 +58,6 @@ describe("Permissioned Business Scenario 4", () => {
poolAccessControl,
tosAcceptanceRegistry
} = await deployPermissionedPool({
operator,
poolAdmin: poolAdmin,
settings: poolSettings,
liquidityAsset: mockUSDC
Expand Down
6 changes: 1 addition & 5 deletions test/scenarios/pool/crank-variations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ describe("Crank Variations", () => {
const DEPOSIT_AMOUNT = 1_000_000;

async function loadPoolFixture() {
const [operator, poolAdmin, aliceLender, bobLender] =
await ethers.getSigners();
const [poolAdmin, aliceLender, bobLender] = await ethers.getSigners();

const { pool, liquidityAsset, withdrawController, poolController } =
await deployPool({
operator,
poolAdmin: poolAdmin
});

Expand Down Expand Up @@ -264,10 +262,8 @@ describe("Crank Variations", () => {
const {
pool,
aliceLender,
bobLender,
liquidityAsset,
poolAdmin,
withdrawRequestPeriodDuration,
withdrawController,
poolController
} = await loadFixture(loadPoolFixture);
Expand Down
1 change: 0 additions & 1 deletion test/scenarios/pool/direct-deposit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { ethers } from "hardhat";
import { deployLoan, fundLoan } from "../../support/loan";
import { activatePool, deployPool } from "../../support/pool";
import { getCommonSigners } from "../../support/utils";
Expand Down
Loading

0 comments on commit f458dfb

Please sign in to comment.