2023-07-28 15:11:38 +03:00
|
|
|
type NodeVarObject = { [key: string]: string };
|
2023-09-29 15:12:03 +03:00
|
|
|
type NodeVarArray = [string, string];
|
2023-07-28 15:11:38 +03:00
|
|
|
|
2023-07-28 15:43:53 +03:00
|
|
|
const solidityCodePadding = " ".repeat(8);
|
2023-07-28 15:11:38 +03:00
|
|
|
const pad = (decl: string, padding: string = solidityCodePadding) => padding + decl + "\n";
|
|
|
|
|
|
|
|
class DeclCalculator {
|
|
|
|
public constructor(
|
2023-09-29 15:12:03 +03:00
|
|
|
private declType: string,
|
|
|
|
private padding: string = solidityCodePadding,
|
|
|
|
private transformator: Function = (
|
|
|
|
() => (x: any) =>
|
|
|
|
x
|
|
|
|
)(),
|
|
|
|
private variableNameChanger: Function = (
|
2023-07-28 15:11:38 +03:00
|
|
|
() => (x: any) =>
|
|
|
|
x
|
|
|
|
)()
|
2023-09-29 15:12:03 +03:00
|
|
|
) {}
|
2023-07-28 15:11:38 +03:00
|
|
|
|
|
|
|
private displayVariableName(varObj: NodeVarObject) {
|
|
|
|
return Object.keys(varObj)[0];
|
|
|
|
}
|
|
|
|
|
2023-09-29 15:12:03 +03:00
|
|
|
public calculateDecl = (varInfo: NodeVarObject | NodeVarArray, type: string = "bytes32") => {
|
|
|
|
const solidityVariableName = this.variableNameChanger(Array.isArray(varInfo) ? varInfo[0] : this.displayVariableName(varInfo));
|
|
|
|
const solidityVariableValue = this.transformator(Array.isArray(varInfo) ? varInfo[1] : Object.values(varInfo)[0]);
|
2023-07-28 15:11:38 +03:00
|
|
|
const solidityDeclaration = `${this.declType || type} ${solidityVariableName} = ${solidityVariableValue};`;
|
|
|
|
|
|
|
|
return pad(solidityDeclaration, this.padding);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { DeclCalculator };
|