Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zianksm committed Dec 11, 2022
1 parent 9d1f5e4 commit 51c510c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 43 deletions.
8 changes: 4 additions & 4 deletions src/implementation/body-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
SPACE,
FORMAT_LINE,
} from "./token";
import { functionLiteral } from "./type-mapping";
import { Tree } from "./type-mapping";

export default class BodyParser {
public static parse(fn: functionLiteral) {
export class TypescriptBodyParser {
public static parse(fn: Tree) {
const txLiteral = NEWLINE.concat(
FORMAT_LINE,
FORMAT_LINE,
Expand Down Expand Up @@ -44,7 +44,7 @@ export default class BodyParser {
return txLiteral;
}

private static parseInput(fn: functionLiteral) {
private static parseInput(fn: Tree) {
let input = OPEN_PAR;
const lastIndex = fn.attributes.inputs.obj.length;
for (const i in fn.attributes.inputs.obj) {
Expand Down
2 changes: 1 addition & 1 deletion src/implementation/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
OPT_TOKEN,
} from "./token";

export default class Parser {
export class TypescriptClassParser {
private defaultAbiParam: string;
private defaultInstanceName: string;
private defaultAddressName: string;
Expand Down
12 changes: 6 additions & 6 deletions src/implementation/grouper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import {
ABI,
abiChild,
fallbackMutabilityMapping,
functionLiteral,
Tree,
nonpayable,
nonpayableMapping,
payable,
payableMapping,
} from "./type-mapping";

export default class AbiGrouper {
public group(abi: ABI) {
let fnLiteral: functionLiteral[] = [];
export class TreeBuilder {
public build(abi: ABI) {
let fnLiteral: Tree[] = [];
for (const node of abi) {
const parsedChild: functionLiteral = this.groupAttributes(node);
const parsedChild: Tree = this.groupAttributes(node);

fnLiteral.push(parsedChild);
}
Expand All @@ -24,7 +24,7 @@ export default class AbiGrouper {
else if (stateMutability === nonpayable) return nonpayableMapping;
else return fallbackMutabilityMapping;
}
private groupAttributes(node: abiChild): functionLiteral {
private groupAttributes(node: abiChild): Tree {
return {
name: node.name,
constant: this.determineConstant(node.stateMutability),
Expand Down
27 changes: 10 additions & 17 deletions src/implementation/io-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
boolMapping,
fallbackMapping,
ABI,
functionLiteral,
Tree,
_function,
arrayExt,
} from "./type-mapping";
Expand All @@ -35,17 +35,12 @@ import {
FORMAT_LINE,
PUBLIC_IDENT,
} from "./token";
import AbiGrouper from "./grouper";
import BodyParser from "./body-parser";
import { TreeBuilder } from "./grouper";
import { TypescriptBodyParser } from "./body-parser";

const UNNAMED_VAR = "argv";
const SINGLE_ELEMENT = 1;
export class TypescriptIoParser {
private abiInferer: AbiGrouper;

constructor() {
this.abiInferer = new AbiGrouper();
}
export class TypescriptParser {
private unnamedCounter: number = 0;

private incrementCounter() {
Expand Down Expand Up @@ -102,22 +97,20 @@ export class TypescriptIoParser {
return SPACE.concat(OPEN_PAR);
}

public parse(abi: ABI) {
const fnGroup = this.abiInferer.group(abi);

for (const fn of fnGroup) {
public parse(tree: Tree[]) {
for (const fn of tree) {
// it is IMPORTANT that we parse signature literal AFTER parsing input and output literals.
// because we need input and output literals to complete function signature literals.

fn.attributes.inputs.literals = this.writeInput(fn.attributes.inputs.obj);
fn.attributes.outputs.literals = this.writeOutput(
fn.attributes.outputs.obj
);
fn.bodyLiteral = BodyParser.parse(fn);
fn.bodyLiteral = TypescriptBodyParser.parse(fn);
fn.signatureLiteral = this.parseFnSignature(fn);
}

return fnGroup;
return tree;
}

private writeInput(value: iochild[]) {
Expand Down Expand Up @@ -161,7 +154,7 @@ export class TypescriptIoParser {
else return PROMISE.concat(OPEN_ANGLE_BRACKET, OPEN_BRACKET);
}

private parseFnSignature(fnObj: functionLiteral) {
private parseFnSignature(fnObj: Tree) {
const signature = FORMAT_LINE.concat(
PUBLIC_IDENT,
SPACE,
Expand All @@ -186,7 +179,7 @@ export class TypescriptIoParser {
return signature;
}

private determineOutput(fnObj: functionLiteral) {
private determineOutput(fnObj: Tree) {
const _eval = fnObj.attributes.outputs.obj.length;
const constant = fnObj.constant;

Expand Down
12 changes: 6 additions & 6 deletions src/implementation/reader.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { ABI, abiChild, iochild, _function } from "./type-mapping";

export default class AbiReader {
public static read(abi: string) {
export class AbiReader {
public read(abi: string) {
const abiString = abi;
const abiRaw = this.parseRaw(abiString);
const bindABI = this.bindABI(abiRaw);

return bindABI;
}

private static parseRaw(raw: string) {
private parseRaw(raw: string) {
return JSON.parse(raw);
}

private static bindABI(raw: any): ABI {
private bindABI(raw: any): ABI {
let abi: ABI = [];

for (let i = 0; i < raw.length; i++) {
Expand All @@ -23,7 +23,7 @@ export default class AbiReader {
return abi;
}

private static bindChild(child: any): abiChild {
private bindChild(child: any): abiChild {
return {
constant: child.constant,
inputs: this.parseIo(child.inputs),
Expand All @@ -35,7 +35,7 @@ export default class AbiReader {
};
}

private static parseIo(input: any[]): iochild[] {
private parseIo(input: any[]): iochild[] {
if (input === undefined || input.length === 0) return [];
let childs: iochild[] = [];

Expand Down
2 changes: 1 addition & 1 deletion src/implementation/type-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type iochild = {
};

// TODO : make body using ethers js
export type functionLiteral = {
export type Tree = {
name: string;
constant: boolean;
signatureLiteral?: string;
Expand Down
40 changes: 34 additions & 6 deletions src/implementation/writer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as fs from "fs-extra";
import * as path from "path";
import { ABI } from "./type-mapping";
import { IoParser } from "./io-parser";
import AbiReader from "./reader";
import { TypescriptParser } from "./io-parser";
import { AbiReader } from "./reader";
import { TypescriptClassParser } from "./class-parser";
import { TreeBuilder } from "./grouper";

export type writerOtions =
| {
Expand All @@ -22,13 +24,25 @@ export type writerOtions =
truffle: boolean;
};

export class Writer extends IoParser {
export class Writer {
typeScriptTypescriptBodyParser: TypescriptParser;
typeScriptClassParser: TypescriptClassParser;
abiReader: AbiReader;
treeBuilder: TreeBuilder;

constructor() {
this.abiReader = new AbiReader();
this.typeScriptTypescriptBodyParser = new TypescriptParser();
this.typeScriptClassParser = new TypescriptClassParser();
this.treeBuilder = new TreeBuilder();
}

/**
* @param name the contract name
* @param abi the contracts abi in string format
* @param rawAbi the contracts abi in string format
*/
public write(name: string, abi: string, opt?: writerOtions) {
const abiObj = AbiReader.read(abi);
public write(name: string, rawAbi: string, opt?: writerOtions) {
const abiObj = AbiReader.read(rawAbi);
const tree = this.parse(abiObj);
let contract: string = "";

Expand All @@ -38,4 +52,18 @@ export class Writer extends IoParser {

return contract;
}

private inferAbi(raw: string) {
return this.abiReader.read(raw);
}

private buildSyntaxTree(abi: ABI) {
return this.treeBuilder.build(abi);
}

private buildTypescriptBinding(rawAbi: string) {
const abi = this.inferAbi(rawAbi);
}

private buildBody();
}
4 changes: 2 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Writer } from "./src/implementation/writer";
import * as fs from "fs";
import Parser from "./src/implementation/class-parser";
import { TypescriptClassParser } from "./src/implementation/class-parser";
import { ABI } from "./src/implementation/type-mapping";

const abi = fs.readFileSync("./abi/auctions/auction.json");
const writer = new Writer();
const body = writer.write("", abi.toString());
const output = new Parser().parse("test", body);
const output = new TypescriptClassParser().parse("test", body);

fs.writeFileSync("./output.ts", output);

0 comments on commit 51c510c

Please sign in to comment.