Skip to content

Commit

Permalink
finish writing support
Browse files Browse the repository at this point in the history
  • Loading branch information
zianksm committed Dec 12, 2022
1 parent 53de0d6 commit ef7b1a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/implementation/javascript/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class JavascriptMethodAssembler {
fn.attributes.outputs.literals = this.writeOutput(
fn.attributes.outputs.obj
);
fn.bodyLiteral = JavascriptBodyParser.parse(fn);
fn.bodyLiteral = JavascriptBodyParser.parse(fn, truffle);
fn.signatureLiteral = this.parseFnSignature(fn);
}

Expand Down
26 changes: 14 additions & 12 deletions src/implementation/javascript/body-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {
SEMI_COLON,
SPACE,
FORMAT_LINE,
TRUFFLE_FROM_ADDRESS_TOKEN,
TRUFFLE_FORM_ADDRESS_ARGUMENT_TOKEN,
} from "../token";
import { Branch } from "../type-mapping";

export class JavascriptBodyParser {
public static parse(fn: Branch) {
public static parse(fn: Branch, truffle: boolean) {
const txLiteral = NEWLINE.concat(
FORMAT_LINE,
FORMAT_LINE,
Expand All @@ -30,7 +32,7 @@ export class JavascriptBodyParser {
SPACE,
DEFAULT_CONTRACT_CALL,
fn.name,
this.parseInput(fn),
this.parseInput(fn, truffle),
SEMI_COLON,
NEWLINE,
FORMAT_LINE,
Expand All @@ -44,16 +46,16 @@ export class JavascriptBodyParser {
return txLiteral;
}

private static parseInput(fn: Branch) {
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);
}
private static parseInput(fn: Branch, truffle: boolean) {
let literals = fn.attributes.inputs.literals;

if (truffle) {
literals = literals.replace(
TRUFFLE_FROM_ADDRESS_TOKEN,
TRUFFLE_FORM_ADDRESS_ARGUMENT_TOKEN
);
}
input = input.concat(CLOSE_PAR);
return input;

return literals;
}
}
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const abi = fs.readFileSync("./abi/auctions/auction.json");
const writer = new Writer();
const output = writer.write("test", abi.toString("utf-8"), {
lang: "js",
truffle: true,
truffle: false,
});

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

0 comments on commit ef7b1a6

Please sign in to comment.