Skip to content

Commit

Permalink
change return type
Browse files Browse the repository at this point in the history
  • Loading branch information
zianksm committed Oct 10, 2022
1 parent 37de395 commit e95ffdb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
60 changes: 38 additions & 22 deletions src/implementation/io_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import {
ASYNC,
NEWLINE,
FAT_ARROW,
EMPTY_STRING,
PROMISE,
OPEN_ANGLE_BRACKET,
CLOSE_ANGLE_BRACKET,
OPEN_BRACKET,
CLOSE_BRACKET,
} from "./token";
import AbiGrouper from "./grouper";

Expand All @@ -44,7 +50,9 @@ export default class IoParser extends AbiGrouper {
}
private buildInputLiteral(input: iochild): string {
const inputType = this.determineType(input.type);
const inputName = input.name;
const inputName =
input.name.length === 0 ? this.getVarCounter() : input.name;
this.incrementCounter();
const inputLiteral = inputName.concat(COLON, SPACE, inputType);

return inputLiteral;
Expand All @@ -56,18 +64,16 @@ export default class IoParser extends AbiGrouper {
const inputLiteral = this.buildInputLiteral(input);
literal.push(inputLiteral);
}
this.resetCounter();

return literal;
}

// TODO : make return type in Promise<T>
private buildOutputLiteral(input: iochild) {
const outputType = this.determineType(input.type);
const outputName =
input.name.length === 0 ? this.getVarCounter() : input.name;
this.incrementCounter();
const outputLiteral = outputName.concat(COLON, SPACE, outputType);

return outputLiteral;
return outputType;
}
private parseOutput(value: iochild[]) {
let literal: string[] = [];
Expand All @@ -76,7 +82,6 @@ export default class IoParser extends AbiGrouper {
literal.push(outputLiteral);
}

this.resetCounter();
return literal;
}
private getStartingParam() {
Expand All @@ -90,25 +95,21 @@ export default class IoParser extends AbiGrouper {
// 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.writeIo(
fn.attributes.inputs.obj,
true
);
fn.attributes.outputs.literals = this.writeIo(
fn.attributes.outputs.obj,
false
fn.attributes.inputs.literals = this.writeInput(fn.attributes.inputs.obj);
fn.attributes.outputs.literals = this.writeOutput(
fn.attributes.outputs.obj
);
fn.signatureLiteral = this.parseFnSignature(fn);
}

return fnGroup;
}

private writeIo(value: iochild[], writeInput: boolean) {
private writeInput(value: iochild[]) {
if (value.length === 0) return OPEN_PAR.concat(CLOSE_PAR);
const lastIndex = value.length - 1;
let params = this.getStartingParam();
let literal = writeInput ? this.parseInput(value) : this.parseOutput(value);
let literal = this.parseInput(value);
for (const i in value) {
if (i == lastIndex.toString()) {
params = params.concat(literal[i]);
Expand All @@ -120,6 +121,26 @@ export default class IoParser extends AbiGrouper {
return params.concat(CLOSE_PAR, SPACE);
}

private writeOutput(value: iochild[]) {
if (value.length === 0) return EMPTY_STRING;
const lastIndex = value.length - 1;
let params = this.getAsyncOutput();
let literal = this.parseOutput(value);
for (const i in value) {
if (i == lastIndex.toString()) {
params = params.concat(literal[i]);
break;
}
params = params.concat(literal[i].concat(COMMA, SPACE));
}

return params.concat(CLOSE_BRACKET, CLOSE_ANGLE_BRACKET, SPACE);
}

private getAsyncOutput() {
return PROMISE.concat(OPEN_ANGLE_BRACKET, OPEN_BRACKET);
}

private parseFnSignature(fnObj: functionLiteral) {
const signature = EXPORT.concat(
SPACE,
Expand Down Expand Up @@ -149,12 +170,7 @@ export default class IoParser extends AbiGrouper {
if (_eval === 0 || _eval === undefined || _eval === null)
// make this Promise<T>
return COLON.concat(SPACE, fallbackMapping);
else
return COLON.concat(
SPACE,
fnObj.attributes.outputs.literals as any,
FAT_ARROW
);
else return COLON.concat(SPACE, fnObj.attributes.outputs.literals as any);
}

private determineType(value: string): string {
Expand Down
6 changes: 6 additions & 0 deletions src/implementation/token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const DEFAULT_SPACE_FORMAT = 4;
export const EMPTY_STRING = "";
export const OPEN_BRACE = "{";
export const CLOSE_BRACE = "}";
export const OPEN_PAR = "(";
Expand All @@ -10,3 +11,8 @@ export const ASYNC = "async";
export const EXPORT = "export";
export const NEWLINE = "\n";
export const FAT_ARROW = "=>";
export const PROMISE = "Promise";
export const OPEN_ANGLE_BRACKET = "<";
export const CLOSE_ANGLE_BRACKET = ">";
export const OPEN_BRACKET = "[";
export const CLOSE_BRACKET = "]";
4 changes: 2 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AbiReader from "./src/implementation/reader";
import Writer from "./src/implementation/writer";

const abi = AbiReader.read("./abi/standard/erc20.json");
new Writer().write("lol", abi, "./");
const abi = AbiReader.read("./abi/auctions/auction.json");
new Writer().write("auction", abi, "./");

0 comments on commit e95ffdb

Please sign in to comment.