@subsquid/evm-typegen 1.0.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/main.d.ts.map +1 -1
- package/lib/main.js +9 -125
- package/lib/main.js.map +1 -1
- package/lib/typegen.d.ts +14 -0
- package/lib/typegen.d.ts.map +1 -0
- package/lib/typegen.js +315 -0
- package/lib/typegen.js.map +1 -0
- package/package.json +3 -2
- package/src/main.ts +11 -152
- package/src/typegen.ts +342 -0
package/lib/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAKA,wBAAgB,GAAG,IAAI,IAAI,CA4B1B"}
|
package/lib/main.js
CHANGED
|
@@ -5,11 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.run = void 0;
|
|
7
7
|
const commander_1 = require("commander");
|
|
8
|
-
const process_1 = __importDefault(require("process"));
|
|
9
8
|
const path_1 = __importDefault(require("path"));
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const abi_1 = require("@ethersproject/abi");
|
|
9
|
+
const process_1 = __importDefault(require("process"));
|
|
10
|
+
const typegen_1 = require("./typegen");
|
|
13
11
|
function run() {
|
|
14
12
|
commander_1.program.description(`
|
|
15
13
|
Generates TypeScript definitions for evm log events
|
|
@@ -21,8 +19,14 @@ for use within substrate-processor mapping handlers.
|
|
|
21
19
|
const options = commander_1.program.opts();
|
|
22
20
|
const inputPath = options.abi;
|
|
23
21
|
const outputPath = options.output;
|
|
22
|
+
if (path_1.default.parse(inputPath).ext !== ".json") {
|
|
23
|
+
throw new Error("invalid abi file extension");
|
|
24
|
+
}
|
|
25
|
+
if (path_1.default.parse(outputPath).ext !== ".ts") {
|
|
26
|
+
throw new Error("invalid output file extension");
|
|
27
|
+
}
|
|
24
28
|
try {
|
|
25
|
-
|
|
29
|
+
new typegen_1.Typegen(inputPath, outputPath).generate();
|
|
26
30
|
}
|
|
27
31
|
catch (err) {
|
|
28
32
|
console.error(`evm-typegen error: ${err.toString()}`);
|
|
@@ -30,124 +34,4 @@ for use within substrate-processor mapping handlers.
|
|
|
30
34
|
}
|
|
31
35
|
}
|
|
32
36
|
exports.run = run;
|
|
33
|
-
function generateTsFromAbi(inputPathRaw, outputPathRaw) {
|
|
34
|
-
const inputPath = path_1.default.parse(inputPathRaw);
|
|
35
|
-
const outputPath = path_1.default.parse(outputPathRaw);
|
|
36
|
-
if (inputPath.ext !== ".json") {
|
|
37
|
-
throw new Error("invalid abi file extension");
|
|
38
|
-
}
|
|
39
|
-
if (outputPath.ext !== ".ts") {
|
|
40
|
-
throw new Error("invalid output file extension");
|
|
41
|
-
}
|
|
42
|
-
const rawABI = JSON.parse(fs_1.default.readFileSync(inputPathRaw, { encoding: "utf-8" }));
|
|
43
|
-
const output = new util_internal_code_printer_1.Output();
|
|
44
|
-
output.line("import * as ethers from \"ethers\";");
|
|
45
|
-
output.line();
|
|
46
|
-
output.line("export const abi = new ethers.utils.Interface(getJsonAbi());");
|
|
47
|
-
output.line();
|
|
48
|
-
// validate the abi
|
|
49
|
-
const abi = new abi_1.Interface(rawABI);
|
|
50
|
-
const typeNames = {};
|
|
51
|
-
const abiEvents = Object.values(abi.events).map((event) => {
|
|
52
|
-
let signature = `${event.name}(`;
|
|
53
|
-
let eventTypeName = `${event.name}`;
|
|
54
|
-
if (typeNames[event.name]) {
|
|
55
|
-
eventTypeName += typeNames[event.name].toString();
|
|
56
|
-
typeNames[event.name]++;
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
eventTypeName += "0";
|
|
60
|
-
typeNames[event.name] = 1;
|
|
61
|
-
}
|
|
62
|
-
if (event.inputs.length > 0) {
|
|
63
|
-
signature += event.inputs[0].type;
|
|
64
|
-
}
|
|
65
|
-
for (let i = 1; i < event.inputs.length; ++i) {
|
|
66
|
-
const input = event.inputs[i];
|
|
67
|
-
signature += `,${input.type}`;
|
|
68
|
-
}
|
|
69
|
-
signature += ")";
|
|
70
|
-
return {
|
|
71
|
-
signature,
|
|
72
|
-
eventTypeName,
|
|
73
|
-
inputs: event.inputs,
|
|
74
|
-
};
|
|
75
|
-
});
|
|
76
|
-
for (const decl of abiEvents) {
|
|
77
|
-
output.block(`export interface ${decl.eventTypeName}Event`, () => {
|
|
78
|
-
for (const input of decl.inputs) {
|
|
79
|
-
output.line(`${input.name}: ${getType(input)};`);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
output.line("");
|
|
83
|
-
}
|
|
84
|
-
output.block("export interface EvmEvent", () => {
|
|
85
|
-
output.line("data: string;");
|
|
86
|
-
output.line("topics: string[];");
|
|
87
|
-
});
|
|
88
|
-
output.line();
|
|
89
|
-
output.block("export const events =", () => {
|
|
90
|
-
for (const decl of abiEvents) {
|
|
91
|
-
output.block(`"${decl.signature}": `, () => {
|
|
92
|
-
output.line(`topic: abi.getEventTopic("${decl.signature}"),`);
|
|
93
|
-
output.block(`decode(data: EvmEvent): ${decl.eventTypeName}Event`, () => {
|
|
94
|
-
output.line(`const result = abi.decodeEventLog(`);
|
|
95
|
-
output.indentation(() => {
|
|
96
|
-
output.line(`abi.getEvent("${decl.signature}"),`);
|
|
97
|
-
output.line(`data.data || "",`);
|
|
98
|
-
output.line("data.topics");
|
|
99
|
-
});
|
|
100
|
-
output.line(");");
|
|
101
|
-
output.block("return ", () => {
|
|
102
|
-
for (let i = 0; i < decl.inputs.length; ++i) {
|
|
103
|
-
const input = decl.inputs[i];
|
|
104
|
-
output.line(`${input.name}: ${`result[${i}]`},`);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
output.line(",");
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
output.line();
|
|
113
|
-
output.block("function getJsonAbi(): any", () => {
|
|
114
|
-
`return ${JSON.stringify(rawABI, null, 2)}`.split('\n').forEach(line => {
|
|
115
|
-
output.line(line);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
fs_1.default.writeFileSync(outputPathRaw, output.toString());
|
|
119
|
-
}
|
|
120
|
-
// taken from: https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/cli/src.ts/typescript.ts#L10
|
|
121
|
-
function getType(param, flexible) {
|
|
122
|
-
if (param.type === "address" || param.type === "string") {
|
|
123
|
-
return "string";
|
|
124
|
-
}
|
|
125
|
-
if (param.type === "bool") {
|
|
126
|
-
return "boolean";
|
|
127
|
-
}
|
|
128
|
-
if (param.type.substring(0, 5) === "bytes") {
|
|
129
|
-
if (flexible) {
|
|
130
|
-
return "string | ethers.utils.BytesLike";
|
|
131
|
-
}
|
|
132
|
-
return "string";
|
|
133
|
-
}
|
|
134
|
-
let match = param.type.match(/^(u?int)([0-9]+)$/);
|
|
135
|
-
if (match) {
|
|
136
|
-
if (flexible) {
|
|
137
|
-
return "ethers.BigNumberish";
|
|
138
|
-
}
|
|
139
|
-
if (parseInt(match[2]) < 53) {
|
|
140
|
-
return 'number';
|
|
141
|
-
}
|
|
142
|
-
return 'ethers.BigNumber';
|
|
143
|
-
}
|
|
144
|
-
if (param.baseType === "array") {
|
|
145
|
-
return "Array<" + getType(param.arrayChildren) + ">";
|
|
146
|
-
}
|
|
147
|
-
if (param.baseType === "tuple") {
|
|
148
|
-
let struct = param.components.map((p, i) => `${p.name || "p_" + i}: ${getType(p, flexible)}`);
|
|
149
|
-
return "{ " + struct.join(", ") + " }";
|
|
150
|
-
}
|
|
151
|
-
throw new Error("unknown type");
|
|
152
|
-
}
|
|
153
37
|
//# sourceMappingURL=main.js.map
|
package/lib/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;;;AAAA,yCAAiC;AACjC,gDAAuB;AACvB,sDAA6B;AAC7B,uCAAiC;AAEjC,SAAgB,GAAG;IACf,mBAAO,CAAC,WAAW,CAAC;;;KAGnB,CAAC,IAAI,EAAE,CAAC;SACJ,cAAc,CAAC,cAAc,EAAE,yBAAyB,CAAC;SACzD,cAAc,CAAC,iBAAiB,EAAE,iCAAiC,CAAC,CAAC;IAE1E,mBAAO,CAAC,KAAK,EAAE,CAAC;IAEhB,MAAM,OAAO,GAAG,mBAAO,CAAC,IAAI,EAAE,CAAC;IAC/B,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC;IAC9B,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAElC,IAAI,cAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,KAAK,OAAO,EAAE;QACvC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;KACjD;IAED,IAAI,cAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,GAAG,KAAK,KAAK,EAAE;QACtC,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;KACpD;IAED,IAAI;QACA,IAAI,iBAAO,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC,QAAQ,EAAE,CAAC;KACjD;IAAC,OAAO,GAAQ,EAAE;QACf,OAAO,CAAC,KAAK,CAAC,sBAAsB,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QACtD,iBAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACnB;AACL,CAAC;AA5BD,kBA4BC"}
|
package/lib/typegen.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class Typegen {
|
|
2
|
+
private rawAbi;
|
|
3
|
+
private abi;
|
|
4
|
+
private out;
|
|
5
|
+
constructor(abiFile: string, outPath: string);
|
|
6
|
+
generate(): void;
|
|
7
|
+
private generateEvents;
|
|
8
|
+
private generateFunctions;
|
|
9
|
+
private generateContract;
|
|
10
|
+
private getEvents;
|
|
11
|
+
private getFunctions;
|
|
12
|
+
private getCalls;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=typegen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typegen.d.ts","sourceRoot":"","sources":["../src/typegen.ts"],"names":[],"mappings":"AAKA,qBAAa,OAAO;IAChB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,GAAG,CAAW;IACtB,OAAO,CAAC,GAAG,CAAY;gBAEX,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAM5C,QAAQ,IAAI,IAAI;IAoBhB,OAAO,CAAC,cAAc;IA0CtB,OAAO,CAAC,iBAAiB;IA+BzB,OAAO,CAAC,gBAAgB;IAyExB,OAAO,CAAC,SAAS;IAqBjB,OAAO,CAAC,YAAY;IAuBpB,OAAO,CAAC,QAAQ;CAsBnB"}
|
package/lib/typegen.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Typegen = void 0;
|
|
16
|
+
const abi_1 = require("@ethersproject/abi");
|
|
17
|
+
const util_internal_code_printer_1 = require("@subsquid/util-internal-code-printer");
|
|
18
|
+
const util_internal_1 = require("@subsquid/util-internal");
|
|
19
|
+
const fs_1 = __importDefault(require("fs"));
|
|
20
|
+
class Typegen {
|
|
21
|
+
constructor(abiFile, outPath) {
|
|
22
|
+
this.rawAbi = JSON.parse(fs_1.default.readFileSync(abiFile, { encoding: "utf-8" }));
|
|
23
|
+
this.abi = new abi_1.Interface(this.rawAbi);
|
|
24
|
+
this.out = new util_internal_code_printer_1.FileOutput(outPath);
|
|
25
|
+
}
|
|
26
|
+
generate() {
|
|
27
|
+
this.out.line("import * as ethers from \"ethers\";");
|
|
28
|
+
this.out.line("import assert from \"assert\";");
|
|
29
|
+
this.out.line();
|
|
30
|
+
this.out.line("export const abi = new ethers.utils.Interface(getJsonAbi());");
|
|
31
|
+
this.out.line();
|
|
32
|
+
this.generateEvents();
|
|
33
|
+
this.out.line();
|
|
34
|
+
this.generateFunctions();
|
|
35
|
+
this.out.line();
|
|
36
|
+
this.generateContract();
|
|
37
|
+
this.out.line();
|
|
38
|
+
this.out.block("function getJsonAbi(): any", () => {
|
|
39
|
+
`return ${JSON.stringify(this.rawAbi, null, 2)}`.split('\n').forEach(line => {
|
|
40
|
+
this.out.line(line);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
this.out.write();
|
|
44
|
+
}
|
|
45
|
+
generateEvents() {
|
|
46
|
+
for (const decl of this.getEvents()) {
|
|
47
|
+
for (let i = 0; i < decl.overloads.length; i++) {
|
|
48
|
+
if (decl.overloads[i].inputs.length === 0)
|
|
49
|
+
continue;
|
|
50
|
+
this.out.line(`export type ${decl.name}${i}Event = ${getTupleType(decl.overloads[i].inputs)}`);
|
|
51
|
+
this.out.line();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.out.block("export interface EvmLog", () => {
|
|
55
|
+
this.out.line("data: string;");
|
|
56
|
+
this.out.line("topics: string[];");
|
|
57
|
+
});
|
|
58
|
+
this.out.line();
|
|
59
|
+
this.out.block(`function decodeEvent(signature: string, data: EvmLog): any`, () => {
|
|
60
|
+
this.out.line(`return abi.decodeEventLog(`);
|
|
61
|
+
this.out.indentation(() => {
|
|
62
|
+
this.out.line(`abi.getEvent(signature),`);
|
|
63
|
+
this.out.line(`data.data || "",`);
|
|
64
|
+
this.out.line("data.topics");
|
|
65
|
+
});
|
|
66
|
+
this.out.line(");");
|
|
67
|
+
});
|
|
68
|
+
this.out.line();
|
|
69
|
+
this.out.block(`export const events =`, () => {
|
|
70
|
+
for (const event of this.getEvents()) {
|
|
71
|
+
for (let i = 0; i < event.overloads.length; i++) {
|
|
72
|
+
const overload = event.overloads[i];
|
|
73
|
+
const signature = createSignature(event.name, overload.inputs);
|
|
74
|
+
this.out.block(`"${signature}":`, () => {
|
|
75
|
+
this.out.line(`topic: abi.getEventTopic("${signature}"),`);
|
|
76
|
+
if (event.overloads[i].inputs.length > 0) {
|
|
77
|
+
this.out.block(`decode(data: EvmLog): ${event.name}${i}Event`, () => {
|
|
78
|
+
this.out.line(`return decodeEvent("${signature}", data)`);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
this.out.line(",");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
generateFunctions() {
|
|
88
|
+
for (const func of this.getFunctions()) {
|
|
89
|
+
for (let i = 0; i < func.overloads.length; i++) {
|
|
90
|
+
if (func.overloads[i].inputs.length === 0)
|
|
91
|
+
continue;
|
|
92
|
+
this.out.line(`export type ${upperCaseFirst(func.name)}${i}Function = ${getTupleType(func.overloads[i].inputs)}`);
|
|
93
|
+
this.out.line();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
this.out.line();
|
|
97
|
+
this.out.block(`function decodeFunction(data: string): any`, () => {
|
|
98
|
+
this.out.line(`return abi.decodeFunctionData(data.slice(0, 10), data)`);
|
|
99
|
+
});
|
|
100
|
+
this.out.line();
|
|
101
|
+
this.out.block(`export const functions =`, () => {
|
|
102
|
+
for (const func of this.getFunctions()) {
|
|
103
|
+
for (let i = 0; i < func.overloads.length; i++) {
|
|
104
|
+
const overload = func.overloads[i];
|
|
105
|
+
const signature = createSignature(func.name, overload.inputs);
|
|
106
|
+
this.out.block(`"${signature}":`, () => {
|
|
107
|
+
this.out.line(`sighash: abi.getSighash("${signature}"),`);
|
|
108
|
+
if (func.overloads[i].inputs.length > 0)
|
|
109
|
+
this.out.block(`decode(input: string): ${upperCaseFirst(func.name)}${i}Function`, () => {
|
|
110
|
+
this.out.line(`return decodeFunction(input)`);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
this.out.line(",");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
generateContract() {
|
|
119
|
+
let abiCalls = this.getCalls();
|
|
120
|
+
this.out.block("interface ChainContext ", () => {
|
|
121
|
+
this.out.line(`_chain: Chain`);
|
|
122
|
+
});
|
|
123
|
+
this.out.line();
|
|
124
|
+
this.out.block("interface BlockContext ", () => {
|
|
125
|
+
this.out.line(`_chain: Chain`);
|
|
126
|
+
this.out.line(`block: Block`);
|
|
127
|
+
});
|
|
128
|
+
this.out.line();
|
|
129
|
+
this.out.block("interface Block ", () => {
|
|
130
|
+
this.out.line(`height: number`);
|
|
131
|
+
});
|
|
132
|
+
this.out.line();
|
|
133
|
+
this.out.block("interface Chain ", () => {
|
|
134
|
+
this.out.block("client: ", () => {
|
|
135
|
+
this.out.line(`call: <T=any>(method: string, params?: unknown[]) => Promise<T>`);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
this.out.line();
|
|
139
|
+
this.out.block("export class Contract ", () => {
|
|
140
|
+
this.out.line(`private readonly _chain: Chain`);
|
|
141
|
+
this.out.line(`private readonly blockHeight: number`);
|
|
142
|
+
this.out.line(`readonly address: string`);
|
|
143
|
+
this.out.line();
|
|
144
|
+
this.out.line(`constructor(ctx: BlockContext, address: string)`);
|
|
145
|
+
this.out.line(`constructor(ctx: ChainContext, block: Block, address: string)`);
|
|
146
|
+
this.out.block(`constructor(ctx: BlockContext, blockOrAddress: Block | string, address?: string)`, () => {
|
|
147
|
+
this.out.line(`this._chain = ctx._chain`);
|
|
148
|
+
this.out.block(`if (typeof blockOrAddress === 'string') `, () => {
|
|
149
|
+
this.out.line(`this.blockHeight = ctx.block.height`);
|
|
150
|
+
this.out.line(`this.address = ethers.utils.getAddress(blockOrAddress)`);
|
|
151
|
+
});
|
|
152
|
+
this.out.block(`else `, () => {
|
|
153
|
+
this.out.line(`assert(address != null)`);
|
|
154
|
+
this.out.line(`this.blockHeight = blockOrAddress.height`);
|
|
155
|
+
this.out.line(`this.address = ethers.utils.getAddress(address)`);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
this.out.line();
|
|
159
|
+
for (const decl of abiCalls) {
|
|
160
|
+
if (decl.overloads.length > 1) {
|
|
161
|
+
for (let overload of decl.overloads) {
|
|
162
|
+
const args = overload.inputs.map((i, n) => `${i.name || `arg${n}`}: ${getType(i)}`);
|
|
163
|
+
const returnType = overload.outputs.length == 1 ? getType(overload.outputs[0]) : getTupleType(overload.outputs);
|
|
164
|
+
this.out.line(`async ${decl.name}(${args}): Promise<${returnType}>`);
|
|
165
|
+
}
|
|
166
|
+
this.out.block(`async ${decl.name}(...args: any[])`, () => {
|
|
167
|
+
this.out.line(`return this.call("${decl.name}", args)`);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
const overload = decl.overloads[0];
|
|
172
|
+
const params = overload.inputs.map((i, n) => `${i.name || `arg${n}`}: ${getType(i)}`);
|
|
173
|
+
const returnType = overload.outputs.length == 1 ? getType(overload.outputs[0]) : getTupleType(overload.outputs);
|
|
174
|
+
this.out.block(`async ${decl.name}(${params.join(`, `)}): Promise<${returnType}>`, () => {
|
|
175
|
+
this.out.line(`return this.call("${decl.name}", [${overload.inputs.map((i, n) => `${i.name || `arg${n}`}`).join(`, `)}])`);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
this.out.line();
|
|
179
|
+
}
|
|
180
|
+
this.out.block(`private async call(name: string, args: any[]) : Promise<any>`, () => {
|
|
181
|
+
this.out.line(`const fragment = abi.getFunction(name)`);
|
|
182
|
+
this.out.line(`const data = abi.encodeFunctionData(fragment, args)`);
|
|
183
|
+
this.out.line(`const result = await this._chain.client.call('eth_call', [{to: this.address, data}, this.blockHeight])`);
|
|
184
|
+
this.out.line(`const decoded = abi.decodeFunctionResult(fragment, result)`);
|
|
185
|
+
this.out.line(`return decoded.length > 1 ? decoded : decoded[0]`);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
getEvents() {
|
|
190
|
+
let res = new Map();
|
|
191
|
+
for (let event of Object.values(this.abi.events)) {
|
|
192
|
+
let abiEvent = res.get(event.name);
|
|
193
|
+
if (abiEvent == null) {
|
|
194
|
+
abiEvent = {
|
|
195
|
+
name: event.name,
|
|
196
|
+
overloads: []
|
|
197
|
+
};
|
|
198
|
+
res.set(event.name, abiEvent);
|
|
199
|
+
}
|
|
200
|
+
abiEvent.overloads.push({
|
|
201
|
+
inputs: event.inputs || [],
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return [...res.values()];
|
|
205
|
+
}
|
|
206
|
+
getFunctions() {
|
|
207
|
+
let res = new Map();
|
|
208
|
+
for (let func of Object.values(this.abi.functions)) {
|
|
209
|
+
if (func.constant)
|
|
210
|
+
continue;
|
|
211
|
+
let abiFunc = res.get(func.name);
|
|
212
|
+
if (abiFunc == null) {
|
|
213
|
+
abiFunc = {
|
|
214
|
+
name: func.name,
|
|
215
|
+
overloads: []
|
|
216
|
+
};
|
|
217
|
+
res.set(func.name, abiFunc);
|
|
218
|
+
}
|
|
219
|
+
abiFunc.overloads.push({
|
|
220
|
+
inputs: func.inputs || [],
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return [...res.values()];
|
|
224
|
+
}
|
|
225
|
+
getCalls() {
|
|
226
|
+
let res = new Map();
|
|
227
|
+
for (let func of Object.values(this.abi.functions)) {
|
|
228
|
+
if (!func.constant || func.outputs == null)
|
|
229
|
+
continue;
|
|
230
|
+
let abiCall = res.get(func.name);
|
|
231
|
+
if (abiCall == null) {
|
|
232
|
+
abiCall = {
|
|
233
|
+
name: func.name,
|
|
234
|
+
overloads: []
|
|
235
|
+
};
|
|
236
|
+
res.set(func.name, abiCall);
|
|
237
|
+
}
|
|
238
|
+
abiCall.overloads.push({
|
|
239
|
+
inputs: func.inputs,
|
|
240
|
+
outputs: func.outputs || [],
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return [...res.values()];
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
__decorate([
|
|
247
|
+
util_internal_1.def,
|
|
248
|
+
__metadata("design:type", Function),
|
|
249
|
+
__metadata("design:paramtypes", []),
|
|
250
|
+
__metadata("design:returntype", Array)
|
|
251
|
+
], Typegen.prototype, "getEvents", null);
|
|
252
|
+
__decorate([
|
|
253
|
+
util_internal_1.def,
|
|
254
|
+
__metadata("design:type", Function),
|
|
255
|
+
__metadata("design:paramtypes", []),
|
|
256
|
+
__metadata("design:returntype", Array)
|
|
257
|
+
], Typegen.prototype, "getFunctions", null);
|
|
258
|
+
__decorate([
|
|
259
|
+
util_internal_1.def,
|
|
260
|
+
__metadata("design:type", Function),
|
|
261
|
+
__metadata("design:paramtypes", []),
|
|
262
|
+
__metadata("design:returntype", Array)
|
|
263
|
+
], Typegen.prototype, "getCalls", null);
|
|
264
|
+
exports.Typegen = Typegen;
|
|
265
|
+
// taken from: https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/cli/src.ts/typescript.ts#L10
|
|
266
|
+
function getType(param) {
|
|
267
|
+
if (param.type === "address" || param.type === "string") {
|
|
268
|
+
return "string";
|
|
269
|
+
}
|
|
270
|
+
if (param.type === "bool") {
|
|
271
|
+
return "boolean";
|
|
272
|
+
}
|
|
273
|
+
if (param.type.substring(0, 5) === "bytes") {
|
|
274
|
+
return "string";
|
|
275
|
+
}
|
|
276
|
+
let match = param.type.match(/^(u?int)([0-9]+)$/);
|
|
277
|
+
if (match) {
|
|
278
|
+
return parseInt(match[2]) < 53 ? 'number' : 'ethers.BigNumber';
|
|
279
|
+
}
|
|
280
|
+
if (param.baseType === "array") {
|
|
281
|
+
return "Array<" + getType(param.arrayChildren) + ">";
|
|
282
|
+
}
|
|
283
|
+
if (param.baseType === "tuple") {
|
|
284
|
+
return getTupleType(param.components);
|
|
285
|
+
}
|
|
286
|
+
throw new Error("unknown type");
|
|
287
|
+
}
|
|
288
|
+
function getTupleType(params) {
|
|
289
|
+
let tuple = '[' + params.map(p => {
|
|
290
|
+
return p.name ? `${p.name}: ${getType(p)}` : getType(p);
|
|
291
|
+
}).join(', ') + ']';
|
|
292
|
+
let fields = getStructFields(params);
|
|
293
|
+
if (fields.length == 0)
|
|
294
|
+
return tuple;
|
|
295
|
+
let struct = '{' + fields.map(f => `${f.name}: ${getType(f)}`).join(', ') + '}';
|
|
296
|
+
return `(${tuple} & ${struct})`;
|
|
297
|
+
}
|
|
298
|
+
// https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/abi/src.ts/coders/tuple.ts#L29
|
|
299
|
+
function getStructFields(params) {
|
|
300
|
+
let array = [];
|
|
301
|
+
let counts = {};
|
|
302
|
+
for (let p of params) {
|
|
303
|
+
if (p.name && array[p.name] == null) {
|
|
304
|
+
counts[p.name] = (counts[p.name] || 0) + 1;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return params.filter(p => counts[p.name] == 1);
|
|
308
|
+
}
|
|
309
|
+
function createSignature(name, inputs) {
|
|
310
|
+
return `${name}(${inputs.map((i) => i.type).join(`,`)})`;
|
|
311
|
+
}
|
|
312
|
+
function upperCaseFirst(s) {
|
|
313
|
+
return s[0].toUpperCase() + s.slice(1);
|
|
314
|
+
}
|
|
315
|
+
//# sourceMappingURL=typegen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typegen.js","sourceRoot":"","sources":["../src/typegen.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4CAAuD;AACvD,qFAAuE;AACvE,2DAA2C;AAC3C,4CAAmB;AAEnB,MAAa,OAAO;IAKhB,YAAY,OAAe,EAAE,OAAe;QACxC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,YAAE,CAAC,YAAY,CAAC,OAAO,EAAE,EAAC,QAAQ,EAAE,OAAO,EAAC,CAAC,CAAC,CAAA;QACvE,IAAI,CAAC,GAAG,GAAG,IAAI,eAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACrC,IAAI,CAAC,GAAG,GAAG,IAAI,uCAAU,CAAC,OAAO,CAAC,CAAA;IACtC,CAAC;IAED,QAAQ;QACJ,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAA;QACpD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAA;QAC/C,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAA;QACf,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,8DAA8D,CAAC,CAAA;QAC7E,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAA;QACf,IAAI,CAAC,cAAc,EAAE,CAAA;QACrB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAA;QACf,IAAI,CAAC,iBAAiB,EAAE,CAAA;QACxB,IAAI,CAAC,GAAG,C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GAAG,CAAC,GAAG,CAAA;AAC5D,CAAC;AA4BD,SAAS,cAAc,CAAC,CAAS;IAC7B,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;AAC1C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@subsquid/evm-typegen",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "CLI for generating typescript types and decode implementations for evm logs",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": "git@github.com:subsquid/squid.git",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"commander": "^9.3.0",
|
|
20
20
|
"@ethersproject/abi": "^5.6.4",
|
|
21
|
-
"@subsquid/util-internal-code-printer": "^0.0.2"
|
|
21
|
+
"@subsquid/util-internal-code-printer": "^0.0.2",
|
|
22
|
+
"@subsquid/util-internal": "^0.0.1"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"@types/node": "^16.11.41",
|
package/src/main.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import { Output } from "@subsquid/util-internal-code-printer";
|
|
6
|
-
import { EventFragment, Interface, ParamType } from "@ethersproject/abi";
|
|
1
|
+
import {program} from "commander"
|
|
2
|
+
import path from "path"
|
|
3
|
+
import process from "process"
|
|
4
|
+
import {Typegen} from "./typegen"
|
|
7
5
|
|
|
8
6
|
export function run(): void {
|
|
9
7
|
program.description(`
|
|
@@ -19,157 +17,18 @@ for use within substrate-processor mapping handlers.
|
|
|
19
17
|
const inputPath = options.abi;
|
|
20
18
|
const outputPath = options.output;
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
generateTsFromAbi(inputPath, outputPath);
|
|
24
|
-
} catch(err: any) {
|
|
25
|
-
console.error(`evm-typegen error: ${err.toString()}`);
|
|
26
|
-
process.exit(1);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function generateTsFromAbi(inputPathRaw: string, outputPathRaw: string): void {
|
|
31
|
-
const inputPath = path.parse(inputPathRaw);
|
|
32
|
-
const outputPath = path.parse(outputPathRaw);
|
|
33
|
-
|
|
34
|
-
if (inputPath.ext !== ".json") {
|
|
20
|
+
if (path.parse(inputPath).ext !== ".json") {
|
|
35
21
|
throw new Error("invalid abi file extension");
|
|
36
22
|
}
|
|
37
23
|
|
|
38
|
-
if (outputPath.ext !== ".ts") {
|
|
24
|
+
if (path.parse(outputPath).ext !== ".ts") {
|
|
39
25
|
throw new Error("invalid output file extension");
|
|
40
26
|
}
|
|
41
27
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
output.line();
|
|
48
|
-
output.line("export const abi = new ethers.utils.Interface(getJsonAbi());");
|
|
49
|
-
output.line();
|
|
50
|
-
|
|
51
|
-
// validate the abi
|
|
52
|
-
const abi = new Interface(rawABI);
|
|
53
|
-
|
|
54
|
-
const typeNames: { [key: string]: number } = {};
|
|
55
|
-
|
|
56
|
-
const abiEvents: Array<AbiEvent> = Object.values(abi.events).map((event: EventFragment): AbiEvent => {
|
|
57
|
-
let signature = `${event.name}(`;
|
|
58
|
-
let eventTypeName = `${event.name}`;
|
|
59
|
-
|
|
60
|
-
if(typeNames[event.name]) {
|
|
61
|
-
eventTypeName += typeNames[event.name].toString();
|
|
62
|
-
typeNames[event.name]++;
|
|
63
|
-
} else {
|
|
64
|
-
eventTypeName += "0";
|
|
65
|
-
typeNames[event.name] = 1;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if(event.inputs.length > 0) {
|
|
69
|
-
signature += event.inputs[0].type;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
for (let i=1; i<event.inputs.length; ++i) {
|
|
73
|
-
const input = event.inputs[i];
|
|
74
|
-
signature += `,${input.type}`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
signature += ")";
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
signature,
|
|
81
|
-
eventTypeName,
|
|
82
|
-
inputs: event.inputs,
|
|
83
|
-
};
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
for(const decl of abiEvents) {
|
|
87
|
-
output.block(`export interface ${decl.eventTypeName}Event`, () => {
|
|
88
|
-
for (const input of decl.inputs) {
|
|
89
|
-
output.line(`${input.name}: ${getType(input)};`);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
output.line("");
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
output.block("export interface EvmEvent", () => {
|
|
96
|
-
output.line("data: string;");
|
|
97
|
-
output.line("topics: string[];");
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
output.line();
|
|
101
|
-
|
|
102
|
-
output.block("export const events =", () => {
|
|
103
|
-
for(const decl of abiEvents) {
|
|
104
|
-
output.block(`"${decl.signature}": `, () => {
|
|
105
|
-
output.line(`topic: abi.getEventTopic("${decl.signature}"),`);
|
|
106
|
-
output.block(`decode(data: EvmEvent): ${decl.eventTypeName}Event`, () => {
|
|
107
|
-
output.line(`const result = abi.decodeEventLog(`);
|
|
108
|
-
output.indentation(() => {
|
|
109
|
-
output.line(`abi.getEvent("${decl.signature}"),`);
|
|
110
|
-
output.line(`data.data || "",`);
|
|
111
|
-
output.line("data.topics");
|
|
112
|
-
});
|
|
113
|
-
output.line(");");
|
|
114
|
-
output.block("return ", () => {
|
|
115
|
-
for (let i=0; i<decl.inputs.length; ++i) {
|
|
116
|
-
const input = decl.inputs[i];
|
|
117
|
-
output.line(`${input.name}: ${`result[${i}]`},`);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
output.line(",");
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
output.line();
|
|
127
|
-
|
|
128
|
-
output.block("function getJsonAbi(): any", () => {
|
|
129
|
-
`return ${JSON.stringify(rawABI, null, 2)}`.split('\n').forEach(line => {
|
|
130
|
-
output.line(line)
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
fs.writeFileSync(outputPathRaw, output.toString());
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// taken from: https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/cli/src.ts/typescript.ts#L10
|
|
138
|
-
function getType(param: ParamType, flexible?: boolean): string {
|
|
139
|
-
if (param.type === "address" || param.type === "string") { return "string"; }
|
|
140
|
-
|
|
141
|
-
if (param.type === "bool") { return "boolean" }
|
|
142
|
-
|
|
143
|
-
if (param.type.substring(0, 5) === "bytes") {
|
|
144
|
-
if (flexible) {
|
|
145
|
-
return "string | ethers.utils.BytesLike";
|
|
146
|
-
}
|
|
147
|
-
return "string"
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
let match = param.type.match(/^(u?int)([0-9]+)$/)
|
|
151
|
-
if (match) {
|
|
152
|
-
if (flexible) {
|
|
153
|
-
return "ethers.BigNumberish";
|
|
154
|
-
}
|
|
155
|
-
if (parseInt(match[2]) < 53) { return 'number'; }
|
|
156
|
-
return 'ethers.BigNumber';
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (param.baseType === "array") {
|
|
160
|
-
return "Array<" + getType(param.arrayChildren) + ">";
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (param.baseType === "tuple") {
|
|
164
|
-
let struct = param.components.map((p, i) => `${p.name || "p_" + i}: ${getType(p, flexible)}`);
|
|
165
|
-
return "{ " + struct.join(", ") + " }";
|
|
28
|
+
try {
|
|
29
|
+
new Typegen(inputPath, outputPath).generate();
|
|
30
|
+
} catch (err: any) {
|
|
31
|
+
console.error(`evm-typegen error: ${err.toString()}`);
|
|
32
|
+
process.exit(1);
|
|
166
33
|
}
|
|
167
|
-
|
|
168
|
-
throw new Error("unknown type");
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
interface AbiEvent {
|
|
172
|
-
signature: string;
|
|
173
|
-
eventTypeName: string;
|
|
174
|
-
inputs: ParamType[];
|
|
175
34
|
}
|
package/src/typegen.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import {Interface, ParamType} from "@ethersproject/abi"
|
|
2
|
+
import {FileOutput, Output} from "@subsquid/util-internal-code-printer"
|
|
3
|
+
import {def} from "@subsquid/util-internal"
|
|
4
|
+
import fs from "fs"
|
|
5
|
+
|
|
6
|
+
export class Typegen {
|
|
7
|
+
private rawAbi: any
|
|
8
|
+
private abi: Interface
|
|
9
|
+
private out: FileOutput
|
|
10
|
+
|
|
11
|
+
constructor(abiFile: string, outPath: string) {
|
|
12
|
+
this.rawAbi = JSON.parse(fs.readFileSync(abiFile, {encoding: "utf-8"}))
|
|
13
|
+
this.abi = new Interface(this.rawAbi)
|
|
14
|
+
this.out = new FileOutput(outPath)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
generate(): void {
|
|
18
|
+
this.out.line("import * as ethers from \"ethers\";")
|
|
19
|
+
this.out.line("import assert from \"assert\";")
|
|
20
|
+
this.out.line()
|
|
21
|
+
this.out.line("export const abi = new ethers.utils.Interface(getJsonAbi());")
|
|
22
|
+
this.out.line()
|
|
23
|
+
this.generateEvents()
|
|
24
|
+
this.out.line()
|
|
25
|
+
this.generateFunctions()
|
|
26
|
+
this.out.line()
|
|
27
|
+
this.generateContract()
|
|
28
|
+
this.out.line()
|
|
29
|
+
this.out.block("function getJsonAbi(): any", () => {
|
|
30
|
+
`return ${JSON.stringify(this.rawAbi, null, 2)}`.split('\n').forEach(line => {
|
|
31
|
+
this.out.line(line)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
this.out.write()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private generateEvents() {
|
|
38
|
+
for (const decl of this.getEvents()) {
|
|
39
|
+
for (let i = 0; i < decl.overloads.length; i++) {
|
|
40
|
+
if (decl.overloads[i].inputs.length === 0) continue
|
|
41
|
+
this.out.line(`export type ${decl.name}${i}Event = ${getTupleType(decl.overloads[i].inputs)}`)
|
|
42
|
+
this.out.line()
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this.out.block("export interface EvmLog", () => {
|
|
46
|
+
this.out.line("data: string;")
|
|
47
|
+
this.out.line("topics: string[];")
|
|
48
|
+
})
|
|
49
|
+
this.out.line()
|
|
50
|
+
this.out.block(`function decodeEvent(signature: string, data: EvmLog): any`, () => {
|
|
51
|
+
this.out.line(`return abi.decodeEventLog(`)
|
|
52
|
+
this.out.indentation(() => {
|
|
53
|
+
this.out.line(`abi.getEvent(signature),`)
|
|
54
|
+
this.out.line(`data.data || "",`)
|
|
55
|
+
this.out.line("data.topics")
|
|
56
|
+
})
|
|
57
|
+
this.out.line(");")
|
|
58
|
+
})
|
|
59
|
+
this.out.line()
|
|
60
|
+
this.out.block(`export const events =`, () => {
|
|
61
|
+
for (const event of this.getEvents()) {
|
|
62
|
+
for (let i = 0; i < event.overloads.length; i++) {
|
|
63
|
+
const overload = event.overloads[i]
|
|
64
|
+
const signature = createSignature(event.name, overload.inputs)
|
|
65
|
+
this.out.block(`"${signature}":`, () => {
|
|
66
|
+
this.out.line(`topic: abi.getEventTopic("${signature}"),`)
|
|
67
|
+
if (event.overloads[i].inputs.length > 0) {
|
|
68
|
+
this.out.block(`decode(data: EvmLog): ${event.name}${i}Event`, () => {
|
|
69
|
+
this.out.line(`return decodeEvent("${signature}", data)`)
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
this.out.line(",")
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private generateFunctions() {
|
|
80
|
+
for (const func of this.getFunctions()) {
|
|
81
|
+
for (let i = 0; i < func.overloads.length; i++) {
|
|
82
|
+
if (func.overloads[i].inputs.length === 0) continue
|
|
83
|
+
this.out.line(`export type ${upperCaseFirst(func.name)}${i}Function = ${getTupleType(func.overloads[i].inputs)}`)
|
|
84
|
+
this.out.line()
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
this.out.line()
|
|
88
|
+
this.out.block(`function decodeFunction(data: string): any`, () => {
|
|
89
|
+
this.out.line(`return abi.decodeFunctionData(data.slice(0, 10), data)`)
|
|
90
|
+
})
|
|
91
|
+
this.out.line()
|
|
92
|
+
this.out.block(`export const functions =`, () => {
|
|
93
|
+
for (const func of this.getFunctions()) {
|
|
94
|
+
for (let i = 0; i < func.overloads.length; i++) {
|
|
95
|
+
const overload = func.overloads[i]
|
|
96
|
+
const signature = createSignature(func.name, overload.inputs)
|
|
97
|
+
this.out.block(`"${signature}":`, () => {
|
|
98
|
+
this.out.line(`sighash: abi.getSighash("${signature}"),`)
|
|
99
|
+
if (func.overloads[i].inputs.length > 0)
|
|
100
|
+
this.out.block(`decode(input: string): ${upperCaseFirst(func.name)}${i}Function`, () => {
|
|
101
|
+
this.out.line(`return decodeFunction(input)`)
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
this.out.line(",")
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private generateContract() {
|
|
111
|
+
let abiCalls = this.getCalls()
|
|
112
|
+
|
|
113
|
+
this.out.block("interface ChainContext ", () => {
|
|
114
|
+
this.out.line(`_chain: Chain`)
|
|
115
|
+
})
|
|
116
|
+
this.out.line()
|
|
117
|
+
this.out.block("interface BlockContext ", () => {
|
|
118
|
+
this.out.line(`_chain: Chain`)
|
|
119
|
+
this.out.line(`block: Block`)
|
|
120
|
+
})
|
|
121
|
+
this.out.line()
|
|
122
|
+
this.out.block("interface Block ", () => {
|
|
123
|
+
this.out.line(`height: number`)
|
|
124
|
+
})
|
|
125
|
+
this.out.line()
|
|
126
|
+
this.out.block("interface Chain ", () => {
|
|
127
|
+
this.out.block("client: ", () => {
|
|
128
|
+
this.out.line(`call: <T=any>(method: string, params?: unknown[]) => Promise<T>`)
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
this.out.line()
|
|
132
|
+
this.out.block("export class Contract ", () => {
|
|
133
|
+
this.out.line(`private readonly _chain: Chain`)
|
|
134
|
+
this.out.line(`private readonly blockHeight: number`)
|
|
135
|
+
this.out.line(`readonly address: string`)
|
|
136
|
+
this.out.line()
|
|
137
|
+
this.out.line(`constructor(ctx: BlockContext, address: string)`)
|
|
138
|
+
this.out.line(`constructor(ctx: ChainContext, block: Block, address: string)`)
|
|
139
|
+
this.out.block(`constructor(ctx: BlockContext, blockOrAddress: Block | string, address?: string)`, () => {
|
|
140
|
+
this.out.line(`this._chain = ctx._chain`)
|
|
141
|
+
this.out.block(`if (typeof blockOrAddress === 'string') `, () => {
|
|
142
|
+
this.out.line(`this.blockHeight = ctx.block.height`)
|
|
143
|
+
this.out.line(`this.address = ethers.utils.getAddress(blockOrAddress)`)
|
|
144
|
+
})
|
|
145
|
+
this.out.block(`else `, () => {
|
|
146
|
+
this.out.line(`assert(address != null)`)
|
|
147
|
+
this.out.line(`this.blockHeight = blockOrAddress.height`)
|
|
148
|
+
this.out.line(`this.address = ethers.utils.getAddress(address)`)
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
this.out.line()
|
|
152
|
+
for (const decl of abiCalls) {
|
|
153
|
+
if (decl.overloads.length > 1) {
|
|
154
|
+
for (let overload of decl.overloads) {
|
|
155
|
+
const args = overload.inputs.map((i, n) => `${i.name || `arg${n}`}: ${getType(i)}`)
|
|
156
|
+
const returnType = overload.outputs.length == 1 ? getType(overload.outputs[0]) : getTupleType(overload.outputs)
|
|
157
|
+
this.out.line(`async ${decl.name}(${args}): Promise<${returnType}>`)
|
|
158
|
+
}
|
|
159
|
+
this.out.block(`async ${decl.name}(...args: any[])`, () => {
|
|
160
|
+
this.out.line(`return this.call("${decl.name}", args)`)
|
|
161
|
+
})
|
|
162
|
+
} else {
|
|
163
|
+
const overload = decl.overloads[0]
|
|
164
|
+
const params = overload.inputs.map((i, n) => `${i.name || `arg${n}`}: ${getType(i)}`)
|
|
165
|
+
const returnType = overload.outputs.length == 1 ? getType(overload.outputs[0]) : getTupleType(overload.outputs)
|
|
166
|
+
this.out.block(`async ${decl.name}(${params.join(`, `)}): Promise<${returnType}>`, () => {
|
|
167
|
+
this.out.line(`return this.call("${decl.name}", [${overload.inputs.map((i, n) => `${i.name || `arg${n}`}`).join(`, `)}])`)
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
this.out.line()
|
|
171
|
+
}
|
|
172
|
+
this.out.block(`private async call(name: string, args: any[]) : Promise<any>`, () => {
|
|
173
|
+
this.out.line(`const fragment = abi.getFunction(name)`)
|
|
174
|
+
this.out.line(`const data = abi.encodeFunctionData(fragment, args)`)
|
|
175
|
+
this.out.line(`const result = await this._chain.client.call('eth_call', [{to: this.address, data}, this.blockHeight])`)
|
|
176
|
+
this.out.line(`const decoded = abi.decodeFunctionResult(fragment, result)`)
|
|
177
|
+
this.out.line(`return decoded.length > 1 ? decoded : decoded[0]`)
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@def
|
|
183
|
+
private getEvents(): AbiEvent[] {
|
|
184
|
+
let res: Map<string, AbiEvent> = new Map()
|
|
185
|
+
for (let event of Object.values(this.abi.events)) {
|
|
186
|
+
let abiEvent = res.get(event.name)
|
|
187
|
+
if (abiEvent == null) {
|
|
188
|
+
abiEvent = {
|
|
189
|
+
name: event.name,
|
|
190
|
+
overloads: []
|
|
191
|
+
}
|
|
192
|
+
res.set(event.name, abiEvent)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
abiEvent.overloads.push({
|
|
196
|
+
inputs: event.inputs || [],
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return [...res.values()]
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
@def
|
|
204
|
+
private getFunctions(): AbiFunction[] {
|
|
205
|
+
let res: Map<string, AbiFunction> = new Map()
|
|
206
|
+
for (let func of Object.values(this.abi.functions)) {
|
|
207
|
+
if (func.constant) continue
|
|
208
|
+
|
|
209
|
+
let abiFunc = res.get(func.name)
|
|
210
|
+
if (abiFunc == null) {
|
|
211
|
+
abiFunc = {
|
|
212
|
+
name: func.name,
|
|
213
|
+
overloads: []
|
|
214
|
+
}
|
|
215
|
+
res.set(func.name, abiFunc)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
abiFunc.overloads.push({
|
|
219
|
+
inputs: func.inputs || [],
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return [...res.values()]
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
@def
|
|
227
|
+
private getCalls(): AbiCall[] {
|
|
228
|
+
let res: Map<string, AbiCall> = new Map()
|
|
229
|
+
for (let func of Object.values(this.abi.functions)) {
|
|
230
|
+
if (!func.constant || func.outputs == null) continue
|
|
231
|
+
|
|
232
|
+
let abiCall = res.get(func.name)
|
|
233
|
+
if (abiCall == null) {
|
|
234
|
+
abiCall = {
|
|
235
|
+
name: func.name,
|
|
236
|
+
overloads: []
|
|
237
|
+
}
|
|
238
|
+
res.set(func.name, abiCall)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
abiCall.overloads.push({
|
|
242
|
+
inputs: func.inputs,
|
|
243
|
+
outputs: func.outputs || [],
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return [...res.values()]
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
// taken from: https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/cli/src.ts/typescript.ts#L10
|
|
253
|
+
function getType(param: ParamType): string {
|
|
254
|
+
if (param.type === "address" || param.type === "string") {
|
|
255
|
+
return "string"
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (param.type === "bool") {
|
|
259
|
+
return "boolean"
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (param.type.substring(0, 5) === "bytes") {
|
|
263
|
+
return "string"
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let match = param.type.match(/^(u?int)([0-9]+)$/)
|
|
267
|
+
if (match) {
|
|
268
|
+
return parseInt(match[2]) < 53 ? 'number' : 'ethers.BigNumber'
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (param.baseType === "array") {
|
|
272
|
+
return "Array<" + getType(param.arrayChildren) + ">"
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (param.baseType === "tuple") {
|
|
276
|
+
return getTupleType(param.components)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
throw new Error("unknown type")
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
function getTupleType(params: ParamType[]) {
|
|
284
|
+
let tuple = '[' + params.map(p => {
|
|
285
|
+
return p.name ? `${p.name}: ${getType(p)}` : getType(p)
|
|
286
|
+
}).join(', ') + ']'
|
|
287
|
+
|
|
288
|
+
let fields = getStructFields(params)
|
|
289
|
+
if (fields.length == 0) return tuple
|
|
290
|
+
|
|
291
|
+
let struct = '{' + fields.map(f => `${f.name}: ${getType(f)}`).join(', ') + '}'
|
|
292
|
+
|
|
293
|
+
return `(${tuple} & ${struct})`
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
// https://github.com/ethers-io/ethers.js/blob/948f77050dae884fe88932fd88af75560aac9d78/packages/abi/src.ts/coders/tuple.ts#L29
|
|
298
|
+
function getStructFields(params: ParamType[]): ParamType[] {
|
|
299
|
+
let array: any = []
|
|
300
|
+
let counts: Record<string, number> = {}
|
|
301
|
+
for (let p of params) {
|
|
302
|
+
if (p.name && array[p.name] == null) {
|
|
303
|
+
counts[p.name] = (counts[p.name] || 0) + 1
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return params.filter(p => counts[p.name] == 1)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
function createSignature(name: string, inputs: ParamType[]) {
|
|
311
|
+
return `${name}(${inputs.map((i) => i.type).join(`,`)})`
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
interface AbiEvent {
|
|
316
|
+
name: string
|
|
317
|
+
overloads: {
|
|
318
|
+
inputs: ParamType[]
|
|
319
|
+
}[]
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
interface AbiFunction {
|
|
324
|
+
name: string
|
|
325
|
+
overloads: {
|
|
326
|
+
inputs: ParamType[]
|
|
327
|
+
}[]
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
interface AbiCall {
|
|
332
|
+
name: string
|
|
333
|
+
overloads: {
|
|
334
|
+
inputs: ParamType[]
|
|
335
|
+
outputs: ParamType[]
|
|
336
|
+
}[]
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
function upperCaseFirst(s: string): string {
|
|
341
|
+
return s[0].toUpperCase() + s.slice(1)
|
|
342
|
+
}
|