-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWizardCodeGenerator.ts
161 lines (145 loc) · 4.68 KB
/
WizardCodeGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { Schema } from 'genson-js/dist/types';
import type { Event, Method } from '@/pages/api/generateCode';
export interface GeneratedCode {
jsCode: string;
sqlCode: string;
}
interface Column {
name: string;
sql: string;
}
function sanitizeTableName(tableName: string): string {
// Convert to PascalCase
let pascalCaseTableName = tableName
// Replace special characters with underscores
.replace(/[^a-zA-Z0-9_]/g, '_')
// Makes first letter and any letters following an underscore upper case
.replace(/^([a-zA-Z])|_([a-zA-Z])/g, (match: string) => match.toUpperCase())
// Removes all underscores
.replace(/_/g, '');
// Add underscore if first character is a number
if (/^[0-9]/.test(pascalCaseTableName)) {
pascalCaseTableName = '_' + pascalCaseTableName;
}
return pascalCaseTableName;
}
const createColumn = (columnName: string, schema: Schema): Column => {
let type: string;
switch (schema.type) {
case 'string':
type = 'TEXT';
break;
case 'integer':
type = 'INT';
break;
case 'number':
type = 'FLOAT';
break;
case 'boolean':
type = 'BOOLEAN';
break;
case 'array':
type = 'TEXT[]';
break;
case 'object':
type = 'JSONB';
break;
default:
type = 'TEXT';
}
return { name: columnName, sql: `"${columnName}" ${type}` };
};
export class WizardCodeGenerator {
constructor(private contractFilter: string, private selectedMethods: Method[], private selectedEvents?: Event[]) {}
private getColumns(method: Method): Column[] {
if (!method.schema.properties) {
return [];
}
return Object.entries(method.schema.properties).map(([k, v]) => createColumn(k, v));
}
private getTableName(method: Method): { contextDbName: string; tableName: string } {
const tableName = `calls_to_${method.method_name}`;
return { tableName, contextDbName: sanitizeTableName(tableName) };
}
private generateSQLForMethod(method: Method): string {
if (!method.schema.properties) {
return '';
}
const { tableName } = this.getTableName(method);
const columns = this.getColumns(method);
// TODO: add NULLABLE for optional fields
return `
CREATE TABLE ${tableName}
(
"block_height" INT,
"block_timestamp" TIMESTAMP,
"signer_id" TEXT,
"receipt_id" TEXT,
${columns.map((c) => ` ${c.sql},`).join('\n')}
PRIMARY KEY ("receipt_id")
);
-- Consider adding an index (https://www.postgresql.org/docs/14/sql-createindex.html) on a frequently queried column, e.g.:
${columns.map((c) => `-- CREATE INDEX "${tableName}_${c.name}_key" ON "${tableName}" ("${c.name}" ASC);`).join('\n')}
`;
}
private generateJSForMethod(method: Method): string {
const columnNames = this.getColumns(method).map((c) => c.name);
const primaryKeys = ['receipt_id'];
const { contextDbName } = this.getTableName(method);
const methodName = method.method_name;
return `
// Extract and upsert ${methodName} function calls
const callsTo${methodName} = extractFunctionCallEntity("${this.contractFilter}", "${methodName}", ${JSON.stringify(
columnNames,
)});
try {
await context.db.${contextDbName}.upsert(callsTo${methodName}, ${JSON.stringify(primaryKeys)}, ${JSON.stringify(
columnNames,
)});
} catch(e) {
context.error(\`Unable to upsert ${methodName} function calls: \$\{e.message\}\`);
}
`;
}
private generateJSCode(): string {
return `
function extractFunctionCallEntity(contractFilter, methodName, argsToInclude) {
const jsonify = (v) => {
if ((typeof v === "object" && v !== null) || Array.isArray(v))
return JSON.stringify(v);
return v;
};
return block
.functionCallsToReceiver(contractFilter, methodName)
.map((fc) => {
let fcArgs = {};
try {
fcArgs = fc.argsAsJSON();
} catch (e) {
console.log(
\`Failed to parse args \$\{fc.args\} into JSON for \$\{fc.methodName\}\`
);
}
const extractedArgs = Object.fromEntries(
Object.entries(fcArgs)
.filter(([k]) => argsToInclude.includes(k))
.map(([k, v]) => [k, jsonify(v)])
);
return {
block_height: block.blockHeight,
block_timestamp: block.timestamp,
signer_id: fc.signerId,
receipt_id: fc.receiptId,
...extractedArgs,
};
});
}
${this.selectedMethods.map((m) => this.generateJSForMethod(m)).join('\n')}
`;
}
public generateCode(): GeneratedCode {
const jsCode = this.generateJSCode();
const sqlCode = this.selectedMethods.map((m) => this.generateSQLForMethod(m)).join('\n');
return { jsCode, sqlCode };
}
}