Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Nov 19, 2024
1 parent babc2a5 commit f3fa0d8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
18 changes: 9 additions & 9 deletions packages/core/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface Contract {
license: string;
parents: Parent[];
natspecTags: NatspecTag[];
imports: ParentContract[];
imports: ImportContract[];
functions: ContractFunction[];
constructorCode: string[];
constructorArgs: FunctionArgument[];
Expand All @@ -16,11 +16,11 @@ export interface Contract {
export type Value = string | number | { lit: string } | { note: string, value: Value };

export interface Parent {
contract: ParentContract;
contract: ImportContract;
params: Value[];
}

export interface ParentContract extends ReferencedContract {
export interface ImportContract extends ReferencedContract {
path: string;
}

Expand All @@ -29,7 +29,7 @@ export interface ReferencedContract {
}

export interface Using {
library: ParentContract;
library: ImportContract;
usingFor: string;
}

Expand Down Expand Up @@ -103,7 +103,7 @@ export class ContractBuilder implements Contract {
});
}

get imports(): ParentContract[] {
get imports(): ImportContract[] {
return [...this.importFromMap.entries()].map(([name, path]) => ({ name, path }));
}

Expand All @@ -115,15 +115,15 @@ export class ContractBuilder implements Contract {
return [...this.variableSet];
}

addParent(contract: ParentContract, params: Value[] = []): boolean {
addParent(contract: ImportContract, params: Value[] = []): boolean {
const present = this.parentMap.has(contract.name);
this.parentMap.set(contract.name, { contract, params });
this.addImportFrom(contract.name, contract.path);
this.addImportFrom(contract);
return !present;
}

addImportFrom(importName: string, from: string) {
this.importFromMap.set(importName, from);
addImportFrom(contract: ImportContract) {
this.importFromMap.set(contract.name, contract.path);
}

addOverride(parent: ReferencedContract, baseFn: BaseFunction, mutability?: FunctionMutability) {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ function addVotes(c: ContractBuilder, clockMode: ClockMode) {
c.addParent(ERC20Votes);
c.addOverride(ERC20Votes, functions._update);

c.addImportFrom('Nonces', '@openzeppelin/contracts/utils/Nonces.sol');
c.addImportFrom({
name: 'Nonces',
path: '@openzeppelin/contracts/utils/Nonces.sol',
});
c.addOverride({
name: 'Nonces',
}, functions.nonces);
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/governor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ function addCounting(c: ContractBuilder) {
function addVotes(c: ContractBuilder) {
const tokenArg = '_token';

c.addImportFrom('IVotes', `@openzeppelin/contracts/governance/extensions/GovernorVotes.sol`);
c.addImportFrom({
name: 'IVotes',
path: `@openzeppelin/contracts/governance/extensions/GovernorVotes.sol`,
});
c.addConstructorArgument({
type: {
name: 'IVotes',
Expand Down Expand Up @@ -360,7 +363,7 @@ function addTimelock(c: ContractBuilder, { timelock }: Required<GovernorOptions>
const timelockArg = '_timelock';
const { timelockType, timelockParent } = timelockModules[timelock];

c.addImportFrom(timelockType.name, timelockType.path);
c.addImportFrom(timelockType);
c.addConstructorArgument({
type: timelockType,
name: timelockArg,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import type { Contract, ReferencedContract, ParentContract } from './contract';
import type { Contract, ReferencedContract, ImportContract } from './contract';
import { inferTranspiled } from './infer-transpiled';

const upgradeableName = (n: string) => {
Expand All @@ -11,7 +11,7 @@ const upgradeableName = (n: string) => {
}
}

const upgradeableImport = (p: ParentContract): ParentContract => {
const upgradeableImport = (p: ImportContract): ImportContract => {
const { dir, ext, name } = path.parse(p.path);
// Use path.posix to get forward slashes
return {
Expand All @@ -26,7 +26,7 @@ const upgradeableImport = (p: ParentContract): ParentContract => {
};

export interface Options {
transformImport?: (parent: ParentContract) => ParentContract;
transformImport?: (parent: ImportContract) => ImportContract;
}

export interface Helpers extends Required<Options> {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/print.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'array.prototype.flatmap/auto';

import type { Contract, Parent, ContractFunction, FunctionArgument, Value, NatspecTag, ParentContract } from './contract';
import type { Contract, Parent, ContractFunction, FunctionArgument, Value, NatspecTag, ImportContract } from './contract';
import { Options, Helpers, withHelpers } from './options';

import { formatLines, spaceBetween, Lines } from './utils/format-lines';
Expand Down Expand Up @@ -247,9 +247,9 @@ function printNatspecTags(tags: NatspecTag[]): string[] {
return tags.map(({ key, value }) => `/// ${key} ${value}`);
}

function printImports(imports: ParentContract[], helpers: Helpers): string[] {
function printImports(imports: ImportContract[], helpers: Helpers): string[] {
// Combine imports by path
const pathMap = new Map<string, ParentContract[]>();
const pathMap = new Map<string, ImportContract[]>();
for (const imp of imports) {
const path = imp.path;
if (!pathMap.has(path)) {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/stablecoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ function addCustodian(c: ContractBuilder, access: Access) {
break;
}
case 'managed': {
c.addImportFrom('AuthorityUtils', `@openzeppelin/contracts/access/manager/AuthorityUtils.sol`);
c.addImportFrom({
name: 'AuthorityUtils',
path: `@openzeppelin/contracts/access/manager/AuthorityUtils.sol`,
});
const logic = [
`(bool immediate,) = AuthorityUtils.canCallWithDelay(authority(), user, address(this), bytes4(_msgData()[0:4]));`,
`return immediate;`
Expand Down Expand Up @@ -268,7 +271,10 @@ function addVotes(c: ContractBuilder, clockMode: ClockMode) {
c.addParent(ERC20Votes);
c.addOverride(ERC20Votes, functions._update);

c.addImportFrom('Nonces', '@openzeppelin/contracts/utils/Nonces.sol');
c.addImportFrom({
name: 'Nonces',
path: '@openzeppelin/contracts/utils/Nonces.sol',
});
c.addOverride({
name: 'Nonces',
}, functions.nonces);
Expand Down

0 comments on commit f3fa0d8

Please sign in to comment.