Skip to content

Commit

Permalink
Fix more typescript type inconsistencies (compound-finance#711)
Browse files Browse the repository at this point in the history
This fixes some more type inconsistencies that Typescript was complaining about throughout the scenario codebase.
  • Loading branch information
kevincheng96 authored Mar 9, 2023
1 parent 91c91b6 commit c2155cb
Show file tree
Hide file tree
Showing 24 changed files with 106 additions and 82 deletions.
5 changes: 2 additions & 3 deletions plugins/deployment_manager/test/CacheTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'chai';
import { tempDir } from './TestHelpers';

import { Cache } from '../Cache';
import { objectFromMap } from '../Utils';

Expand Down Expand Up @@ -61,11 +60,11 @@ describe('Cache', () => {

expect(cache.cache).to.eql(new Map([['test-network', new Map([['test-deployment', new Map([['abc', new Map([['a', 5]])]])]])]]));

expect(objectFromMap(await cache.readCache({ rel: 'abc' }))).to.eql({a: 5});
expect(objectFromMap(await cache.readCache<Map<string, number>>({ rel: 'abc' }))).to.eql({a: 5});

await cache.storeMap({ rel: 'abc' }, new Map([['a', 6]]));

expect(objectFromMap(await cache.readCache({ rel: 'abc' }))).to.eql({a: 6});
expect(objectFromMap(await cache.readCache<Map<string, number>>({ rel: 'abc' }))).to.eql({a: 6});
});
});

Expand Down
12 changes: 6 additions & 6 deletions plugins/deployment_manager/test/MigrationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ describe('Migration', () => {
it('test a simple migration', async () => {
let x = [];
let m = migration('test migration', {
prepare: async (_deploymentManager) => {
prepare: async (_deploymentManager, _govDm) => {
x = [...x, 'step 1'];
return 'step 2';
},
enact: async (_deploymentManager, y) => {
enact: async (_deploymentManager, _govDm, y) => {
x = [...x, y];
}
});
let dm = new DeploymentManager('test-network', 'test-deployment', hre);
expect(m.name).to.eql('test migration');
expect(x).to.eql([]);
let v = await m.actions.prepare(dm);
let v = await m.actions.prepare(dm, dm);
expect(x).to.eql(['step 1']);
await m.actions.enact(dm, v);
await m.actions.enact(dm, dm, v);
expect(x).to.eql(['step 1', 'step 2']);
});

it('loads a simple migration', async () => {
let [m] = await loadMigrations([`${__dirname}/migration.ts`]);
let dm = new DeploymentManager('test-network', 'test-market', hre);
expect(m.name).to.eql('test migration');
expect(await m.actions.prepare(dm)).to.eql(['step 1']);
expect(await m.actions.enact(dm, [])).to.eql(undefined);
expect(await m.actions.prepare(dm, dm)).to.eql(['step 1']);
expect(await m.actions.enact(dm, dm, [])).to.eql(undefined);
});

it('returns proper artifact file spec', async () => {
Expand Down
2 changes: 1 addition & 1 deletion plugins/scenario/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Constraint, Initializer, Property, Scenario, Solution, Transformer } from './Scenario';
export { Constraint, Initializer, Property, Scenario, Solution, StaticConstraint, Transformer } from './Scenario';
export { Loader } from './Loader';
export { ForkSpec, World } from './World';
export { debug } from '../deployment_manager/Utils';
16 changes: 8 additions & 8 deletions scenario/BulkerScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ scenario(
expect(await comet.collateralBalanceOf(albert.address, collateralAsset.address)).to.be.equal(toSupplyCollateral);
expect(await comet.collateralBalanceOf(albert.address, wrappedNativeToken)).to.be.equal(toSupplyEth - toWithdrawEth);
expect(await baseAsset.balanceOf(albert.address)).to.be.equal(toBorrowBase);
expectBase(await comet.balanceOf(betty.address), baseTransferred);
expectBase(await comet.borrowBalanceOf(albert.address), toBorrowBase + toTransferBase);
expectBase((await comet.balanceOf(betty.address)).toBigInt(), baseTransferred);
expectBase((await comet.borrowBalanceOf(albert.address)).toBigInt(), toBorrowBase + toTransferBase);

return txn; // return txn to measure gas
}
Expand Down Expand Up @@ -150,8 +150,8 @@ scenario(
const baseTransferred = getExpectedBaseBalance(toTransferBase, baseIndexScale, baseSupplyIndex);
expect(await comet.collateralBalanceOf(albert.address, collateralAsset.address)).to.be.equal(toSupplyCollateral);
expect(await baseAsset.balanceOf(albert.address)).to.be.equal(toBorrowBase);
expectBase(await comet.balanceOf(betty.address), baseTransferred);
expectBase(await comet.borrowBalanceOf(albert.address), toBorrowBase + toTransferBase - (toSupplyEth - toWithdrawEth));
expectBase((await comet.balanceOf(betty.address)).toBigInt(), baseTransferred);
expectBase((await comet.borrowBalanceOf(albert.address)).toBigInt(), toBorrowBase + toTransferBase - (toSupplyEth - toWithdrawEth));

return txn; // return txn to measure gas
}
Expand Down Expand Up @@ -245,8 +245,8 @@ scenario(
expect(await baseAsset.balanceOf(albert.address)).to.be.equal(toBorrowBase);
expect(await comet.collateralBalanceOf(albert.address, wrappedNativeToken)).to.be.equal(toSupplyEth - toWithdrawEth);
expect(await albert.getErc20Balance(rewardTokenAddress)).to.be.equal(expectedFinalRewardBalance);
expectBase(await comet.balanceOf(betty.address), baseTransferred);
expectBase(await comet.borrowBalanceOf(albert.address), toBorrowBase + toTransferBase);
expectBase((await comet.balanceOf(betty.address)).toBigInt(), baseTransferred);
expectBase((await comet.borrowBalanceOf(albert.address)).toBigInt(), toBorrowBase + toTransferBase);

return txn; // return txn to measure gas
}
Expand Down Expand Up @@ -338,9 +338,9 @@ scenario(
expect(await comet.collateralBalanceOf(albert.address, collateralAsset.address)).to.be.equal(toSupplyCollateral);
expect(await baseAsset.balanceOf(albert.address)).to.be.equal(toBorrowBase);
expect(await albert.getErc20Balance(rewardTokenAddress)).to.be.equal(expectedFinalRewardBalance);
expectBase(await comet.balanceOf(betty.address), baseTransferred);
expectBase((await comet.balanceOf(betty.address)).toBigInt(), baseTransferred);
// NOTE: differs from the equivalent scenario for non-ETH markets
expectBase(await comet.borrowBalanceOf(albert.address), toBorrowBase + toTransferBase - (toSupplyEth - toWithdrawEth));
expectBase((await comet.borrowBalanceOf(albert.address)).toBigInt(), toBorrowBase + toTransferBase - (toSupplyEth - toWithdrawEth));

return txn; // return txn to measure gas
}
Expand Down
13 changes: 7 additions & 6 deletions scenario/MainnetBulkerScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { scenario } from './context/CometContext';
import CometAsset from './context/CometAsset';
import {
ERC20,
IWstETH
IWstETH,
MainnetBulker
} from '../build/types';
import { exp } from '../test/helpers';
import { expectApproximately, isBulkerSupported, matchesDeployment } from './utils';
Expand Down Expand Up @@ -40,7 +41,7 @@ scenario(
[comet.address, albert.address, toSupplyStEth]
);
const calldata = [supplyStEthCalldata];
const actions = [await bulker.ACTION_SUPPLY_STETH()];
const actions = [await (bulker as MainnetBulker).ACTION_SUPPLY_STETH()];

await albert.invoke({ actions, calldata });

Expand Down Expand Up @@ -71,8 +72,8 @@ scenario(
async ({ comet, actors, bulker }, context) => {
const { albert } = actors;

const stETH = await context.world.deploymentManager.contract('stETH');
const wstETH = await context.world.deploymentManager.contract('wstETH');
const stETH = await context.world.deploymentManager.getContractOrThrow('stETH');
const wstETH = await context.world.deploymentManager.getContractOrThrow('wstETH');

await albert.allow(bulker.address, true);

Expand All @@ -83,7 +84,7 @@ scenario(
[comet.address, albert.address, toWithdrawStEth]
);
const calldata = [withdrawStEthCalldata];
const actions = [await bulker.ACTION_WITHDRAW_STETH()];
const actions = [await (bulker as MainnetBulker).ACTION_WITHDRAW_STETH()];

await albert.invoke({ actions, calldata });

Expand Down Expand Up @@ -130,7 +131,7 @@ scenario(
[comet.address, albert.address, ethers.constants.MaxUint256]
);
const calldata = [withdrawStEthCalldata];
const actions = [await bulker.ACTION_WITHDRAW_STETH()];
const actions = [await (bulker as MainnetBulker).ACTION_WITHDRAW_STETH()];

await albert.invoke({ actions, calldata });

Expand Down
4 changes: 2 additions & 2 deletions scenario/SupplyScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContractReceipt } from 'ethers';

// XXX introduce a SupplyCapConstraint to separately test the happy path and revert path instead
// of testing them conditionally
async function testSupplyCollateral(context: CometContext, assetNum: number): Promise<null | ContractReceipt> {
async function testSupplyCollateral(context: CometContext, assetNum: number): Promise<void | ContractReceipt> {
const comet = await context.getComet();
const { albert } = await context.actors;
const { asset: assetAddress, scale: scaleBN, supplyCap } = await comet.getAssetInfo(assetNum);
Expand Down Expand Up @@ -36,7 +36,7 @@ async function testSupplyCollateral(context: CometContext, assetNum: number): Pr
}
}

async function testSupplyFromCollateral(context: CometContext, assetNum: number): Promise<null | ContractReceipt> {
async function testSupplyFromCollateral(context: CometContext, assetNum: number): Promise<void | ContractReceipt> {
const comet = await context.getComet();
const { albert, betty } = await context.actors;
const { asset: assetAddress, scale: scaleBN, supplyCap } = await comet.getAssetInfo(assetNum);
Expand Down
4 changes: 2 additions & 2 deletions scenario/TransferScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { expectApproximately, expectBase, expectRevertCustom, getInterest, isTriviallySourceable, isValidAssetIndex, MAX_ASSETS } from './utils';
import { ContractReceipt } from 'ethers';

async function testTransferCollateral(context: CometContext, assetNum: number): Promise<null | ContractReceipt> {
async function testTransferCollateral(context: CometContext, assetNum: number): Promise<void | ContractReceipt> {
const comet = await context.getComet();
const { albert, betty } = context.actors;
const { asset: assetAddress, scale } = await comet.getAssetInfo(assetNum);
Expand All @@ -19,7 +19,7 @@ async function testTransferCollateral(context: CometContext, assetNum: number):
return txn; // return txn to measure gas
}

async function testTransferFromCollateral(context: CometContext, assetNum: number): Promise<null | ContractReceipt> {
async function testTransferFromCollateral(context: CometContext, assetNum: number): Promise<void | ContractReceipt> {
const comet = await context.getComet();
const { albert, betty, charles } = context.actors;
const { asset: assetAddress, scale } = await comet.getAssetInfo(assetNum);
Expand Down
4 changes: 2 additions & 2 deletions scenario/WithdrawScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { expectApproximately, expectRevertCustom, isTriviallySourceable, isValidAssetIndex, MAX_ASSETS } from './utils';
import { ContractReceipt } from 'ethers';

async function testWithdrawCollateral(context: CometContext, assetNum: number): Promise<null | ContractReceipt> {
async function testWithdrawCollateral(context: CometContext, assetNum: number): Promise<void | ContractReceipt> {
const comet = await context.getComet();
const { albert } = context.actors;
const { asset: assetAddress, scale: scaleBN } = await comet.getAssetInfo(assetNum);
Expand All @@ -22,7 +22,7 @@ async function testWithdrawCollateral(context: CometContext, assetNum: number):
return txn; // return txn to measure gas
}

async function testWithdrawFromCollateral(context: CometContext, assetNum: number): Promise<null | ContractReceipt> {
async function testWithdrawFromCollateral(context: CometContext, assetNum: number): Promise<void | ContractReceipt> {
const comet = await context.getComet();
const { albert, betty } = context.actors;
const { asset: assetAddress, scale: scaleBN } = await comet.getAssetInfo(assetNum);
Expand Down
6 changes: 4 additions & 2 deletions scenario/constraints/CometBalanceConstraint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Constraint } from '../../plugins/scenario';
import { Constraint, Solution } from '../../plugins/scenario';
import { CometContext } from '../context/CometContext';
import CometActor from '../context/CometActor';
import { expect } from 'chai';
Expand Down Expand Up @@ -56,7 +56,7 @@ export class CometBalanceConstraint<T extends CometContext, R extends Requiremen

// XXX ideally when properties fail
// we can report the names of the solution which were applied
const solutions = [];
const solutions: Solution<T>[] = [];
solutions.push(async function barelyMeet(context: T) {
const comet = await context.getComet();
for (const assetName in actorsByAsset) {
Expand Down Expand Up @@ -100,6 +100,8 @@ export class CometBalanceConstraint<T extends CometContext, R extends Requiremen
return context;
});
return solutions;
} else {
return null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion scenario/constraints/Fuzzing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function getFuzzedValues(fuzzConfig: FuzzConfig): any[] {
}

export function getFuzzedRequirements(requirements: Requirements): Requirements[] {
let fuzzedRequirements = [];
let fuzzedRequirements: Requirements[] = [];
let keyValues: KV[][] = [];
// Create a list of fuzzed values for each key that needs to be fuzzed.
for (let key in requirements) {
Expand Down
12 changes: 7 additions & 5 deletions scenario/constraints/MigrationConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function getMigrations<T>(world: World): Promise<Migration<T>[]> {
}

async function isEnacted<T>(actions: Actions<T>, dm: DeploymentManager, govDm: DeploymentManager): Promise<boolean> {
return actions.enacted && await actions.enacted(dm, govDm);
return actions.enacted ? await actions.enacted(dm, govDm) : false;
}

export class MigrationConstraint<T extends CometContext> implements StaticConstraint<T> {
Expand Down Expand Up @@ -76,10 +76,12 @@ export class VerifyMigrationConstraint<T extends CometContext> implements Static
return [
async function (ctx: T): Promise<T> {
const govDeploymentManager = ctx.world.auxiliaryDeploymentManager || ctx.world.deploymentManager;
for (const migration of ctx.migrations) {
if (migration.actions.verify && !(await isEnacted(migration.actions, ctx.world.deploymentManager, govDeploymentManager))) {
await migration.actions.verify(ctx.world.deploymentManager, govDeploymentManager);
debug(`${label} Verified migration "${migration.name}"`);
if (ctx.migrations) {
for (const migration of ctx.migrations) {
if (migration.actions.verify && !(await isEnacted(migration.actions, ctx.world.deploymentManager, govDeploymentManager))) {
await migration.actions.verify(ctx.world.deploymentManager, govDeploymentManager);
debug(`${label} Verified migration "${migration.name}"`);
}
}
}
return ctx;
Expand Down
3 changes: 2 additions & 1 deletion scenario/constraints/ModernConstraint.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Constraint } from '../../plugins/scenario';
import { Solution } from '../../plugins/scenario/Scenario';
import { CometContext } from '../context/CometContext';
import { getFuzzedRequirements } from './Fuzzing';
import { Requirements } from './Requirements';

export class ModernConstraint<T extends CometContext, R extends Requirements> implements Constraint<T, R> {
async solve(requirements: R, _context: T) {
const fuzzed = await getFuzzedRequirements(requirements);
const solutions = [];
const solutions: Solution<T>[] = [];
for (const req of fuzzed) {
if (req.upgrade) {
solutions.push(async function solution(ctx: T): Promise<T> {
Expand Down
2 changes: 1 addition & 1 deletion scenario/constraints/NativeTokenConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class NativeTokenConstraint<T extends CometContext> implements StaticCons
async function (ctx: T): Promise<T> {
for (const symbol in ctx.assets) {
const contract = await ctx.world.deploymentManager.contract(symbol);
if (contract['deposit()']) {
if (contract && contract['deposit()']) {
const whales = await ctx.getWhales();
const amount = exp(200_000, await contract.decimals());
// can make this more sophisticated as needed...
Expand Down
3 changes: 2 additions & 1 deletion scenario/constraints/ProposalConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ async function getOpenProposals(deploymentManager: DeploymentManager, governor:
const block = await deploymentManager.hre.ethers.provider.getBlockNumber();
const filter = governor.filters.ProposalCreated();
const logs = await fetchLogs(governor, filter, block - searchBlocks, block);
const proposals = [];
const proposals: OpenProposal[] = [];
if (logs) {
for (let log of logs) {
if (log.args === undefined) continue;
const [id, , , , , , startBlock, endBlock] = log.args;
const state = await governor.state(id);
if ([
Expand Down
6 changes: 4 additions & 2 deletions scenario/constraints/SupplyCapConstraint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Constraint } from '../../plugins/scenario';
import { Constraint, Solution } from '../../plugins/scenario';
import { CometContext } from '../context/CometContext';
import { expect } from 'chai';
import { Requirements } from './Requirements';
Expand All @@ -9,7 +9,7 @@ export class SupplyCapConstraint<T extends CometContext, R extends Requirements>
async solve(requirements: R, _initialContext: T) {
const supplyCaps = requirements.supplyCaps;
if (supplyCaps !== undefined) {
const solutions = [];
const solutions: Solution<T>[] = [];
solutions.push(async function barelyMeet(context: T) {
const supplyAmountPerAsset = {};
for (const [assetName, rawAmount] of Object.entries(supplyCaps)) {
Expand All @@ -23,6 +23,8 @@ export class SupplyCapConstraint<T extends CometContext, R extends Requirements>
return context;
});
return solutions;
} else {
return null;
}
}

Expand Down
6 changes: 4 additions & 2 deletions scenario/constraints/TokenBalanceConstraint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Constraint } from '../../plugins/scenario';
import { Constraint, Solution } from '../../plugins/scenario';
import { CometContext } from '../context/CometContext';
import { expect } from 'chai';
import { Requirements } from './Requirements';
Expand Down Expand Up @@ -29,7 +29,7 @@ export class TokenBalanceConstraint<T extends CometContext, R extends Requiremen

// XXX ideally when properties fail
// we can report the names of the solution which were applied
const solutions = [];
const solutions: Solution<T>[] = [];
solutions.push(async function barelyMeet(context: T) {
for (const assetName in actorsByAsset) {
const asset = await getAssetFromName(assetName, context);
Expand All @@ -45,6 +45,8 @@ export class TokenBalanceConstraint<T extends CometContext, R extends Requiremen
return context;
});
return solutions;
} else {
return null;
}
}

Expand Down
10 changes: 4 additions & 6 deletions scenario/constraints/UtilizationConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface UtilizationConfig {
utilization?: number;
}

function getUtilizationConfig(requirements: object): UtilizationConfig | null {
function getUtilizationConfig(requirements: object): UtilizationConfig {
return {
utilization: optionalNumber(requirements, 'utilization'),
};
Expand All @@ -52,15 +52,15 @@ export class UtilizationConstraint<T extends CometContext, R extends Requirement
async solve(requirements: R, _context: T) {
let { utilization } = getUtilizationConfig(requirements);

if (utilization == null) {
if (utilization === undefined) {
return null;
} else {
// utilization is target number
return async (context: T): Promise<T> => {
let comet = await context.getComet();

let baseToken = context.getAssetByAddress(await comet.baseToken());
let utilizationFactor = factor(utilization);
let utilizationFactor = factor(utilization!);
let totalSupplyBase = (await comet.totalSupply()).toBigInt();
let totalBorrowBase = (await comet.totalBorrow()).toBigInt();

Expand Down Expand Up @@ -93,9 +93,7 @@ export class UtilizationConstraint<T extends CometContext, R extends Requirement
// everything will come out as zero.
if (toSupplyBase > 0n) {
// Add some supply, any amount will do
let supplyActor = await context.allocateActor('UtilizationConstraint{Supplier}', {
toSupplyBase,
});
let supplyActor = await context.allocateActor('UtilizationConstraint{Supplier}');

await baseToken.approve(supplyActor, comet);
await context.sourceTokens(toSupplyBase, baseToken, supplyActor);
Expand Down
Loading

0 comments on commit c2155cb

Please sign in to comment.