Skip to content

Commit

Permalink
reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
zianksm committed Dec 11, 2022
1 parent e242d10 commit 795f22d
Show file tree
Hide file tree
Showing 3 changed files with 399 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/implementation/typescript/body-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
AWAIT_TOKEN,
CLOSE_PAR,
COMMA,
CONSTANT_TOKEN,
DEFAULT_CONTRACT_CALL,
DEFAULT_TX_NAME,
EQ_SIGN,
NEWLINE,
OPEN_PAR,
RETURN_TOKEN,
SEMI_COLON,
SPACE,
FORMAT_LINE,
} from "./token";
import { Tree } from "./type-mapping";

export class TypescriptBodyParser {
public static parse(fn: Tree) {
const txLiteral = NEWLINE.concat(
FORMAT_LINE,
FORMAT_LINE,
CONSTANT_TOKEN,
SPACE,
DEFAULT_TX_NAME,
SPACE,
EQ_SIGN,
SPACE,
AWAIT_TOKEN,
SPACE,
DEFAULT_CONTRACT_CALL,
fn.name,
this.parseInput(fn),
SEMI_COLON,
NEWLINE,
FORMAT_LINE,
FORMAT_LINE,
RETURN_TOKEN,
SPACE,
DEFAULT_TX_NAME,
SEMI_COLON
);

return txLiteral;
}

private static parseInput(fn: Tree) {
let input = OPEN_PAR;
const lastIndex = fn.attributes.inputs.obj.length;
for (const i in fn.attributes.inputs.obj) {
input = input.concat(fn.attributes.inputs.obj[i].name);
if (parseInt(i) !== lastIndex - 1) {
input = input.concat(COMMA);
}
}
input = input.concat(CLOSE_PAR);
return input;
}
}
130 changes: 130 additions & 0 deletions src/implementation/typescript/class-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import {
ANY_TOKEN,
CLOSE_BRACE,
COLON,
COMMA,
CONTRACT_TOKEN,
DEFAULT_ABI_PARAM,
DEFAULT_ADDRESS_NAME,
DEFAULT_INSTANCE_NAME,
NEWLINE,
OPEN_BRACE,
PRIVATE_IDENT,
SPACE,
STRING_TOKEN,
FORMAT_LINE,
SIGNER_OR_PROVIDER_TOKEN,
SIGNER_OR_PROVIDER_TYPE_TOKEN,
OPT_TOKEN,
} from "./token";

export class TypescriptClassParser {
private defaultAbiParam: string;
private defaultInstanceName: string;
private defaultAddressName: string;
private defaultSignerOrProviderName: string;

constructor() {
this.defaultInstanceName = DEFAULT_INSTANCE_NAME;
this.defaultAddressName = DEFAULT_ADDRESS_NAME;
this.defaultAbiParam = DEFAULT_ABI_PARAM;
this.defaultSignerOrProviderName = SIGNER_OR_PROVIDER_TOKEN;
}
public parse(name: string, body: string) {
const importDirective = 'import * as ethers from "ethers"';
const classSignature = `export default class ${name} `;
const contract = importDirective.concat(
NEWLINE,
NEWLINE,
classSignature,
OPEN_BRACE,
NEWLINE,
this.getMemberClass(),
NEWLINE,
this.getConstructor(),
NEWLINE,
body,
CLOSE_BRACE
);

return contract;
}

private getConstructor() {
const constructorLiteral = `constructor(${this.inputLiteral()})`;
return FORMAT_LINE.concat(constructorLiteral, this.getConstructorBody());
}
private inputLiteral() {
return this.defaultAddressName.concat(
COLON,
SPACE,
STRING_TOKEN,
COMMA,
SPACE,
this.defaultAbiParam,
COLON,
SPACE,
ANY_TOKEN,
COMMA,
SPACE,
SIGNER_OR_PROVIDER_TOKEN,
OPT_TOKEN,
COLON,
SPACE,
SIGNER_OR_PROVIDER_TYPE_TOKEN
);
}
private getConstructorBody() {
const instanceLiteral = `this.${this.defaultInstanceName} = new ethers.Contract(${this.defaultAddressName}, ${this.defaultAbiParam}, ${this.defaultSignerOrProviderName});`;
const addressLiteral = `this.${this.defaultAddressName} = ${this.defaultAddressName};`;

return SPACE.concat(
OPEN_BRACE,
NEWLINE,
FORMAT_LINE,
FORMAT_LINE,
instanceLiteral,
NEWLINE,
FORMAT_LINE,
FORMAT_LINE,
addressLiteral,
NEWLINE,
FORMAT_LINE,
CLOSE_BRACE,
NEWLINE
);
}

private getMemberClass() {
const contractInstanceName = this.getContractInstanceName();
const addressName = this.getAddressName();

return FORMAT_LINE.concat(
contractInstanceName,
NEWLINE,
FORMAT_LINE,
addressName,
NEWLINE
);
}

private getContractInstanceName() {
return PRIVATE_IDENT.concat(
SPACE,
this.defaultInstanceName,
COLON,
SPACE,
CONTRACT_TOKEN
);
}

private getAddressName() {
return PRIVATE_IDENT.concat(
SPACE,
this.defaultAddressName,
COLON,
SPACE,
STRING_TOKEN
);
}
}
Loading

0 comments on commit 795f22d

Please sign in to comment.