@mimicprotocol/cli 0.0.1-rc.10
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/README.md +96 -0
- package/bin/run.cmd +3 -0
- package/bin/run.js +6 -0
- package/dist/commands/codegen.js +100 -0
- package/dist/commands/compile.js +91 -0
- package/dist/commands/deploy.js +122 -0
- package/dist/commands/init.js +102 -0
- package/dist/commands/test.js +63 -0
- package/dist/errors.js +32 -0
- package/dist/helpers.js +17 -0
- package/dist/index.js +2 -0
- package/dist/lib/AbisInterfaceGenerator/AbiTypeConverter.js +137 -0
- package/dist/lib/AbisInterfaceGenerator/ArrayHandler.js +67 -0
- package/dist/lib/AbisInterfaceGenerator/ContractClassGenerator.js +70 -0
- package/dist/lib/AbisInterfaceGenerator/FunctionHandler.js +173 -0
- package/dist/lib/AbisInterfaceGenerator/ImportManager.js +17 -0
- package/dist/lib/AbisInterfaceGenerator/NameManager.js +98 -0
- package/dist/lib/AbisInterfaceGenerator/TupleHandler.js +251 -0
- package/dist/lib/AbisInterfaceGenerator/index.js +11 -0
- package/dist/lib/AbisInterfaceGenerator/types.js +4 -0
- package/dist/lib/InputsInterfaceGenerator.js +96 -0
- package/dist/lib/ManifestHandler.js +134 -0
- package/dist/lib/index.js +12 -0
- package/dist/log.js +13 -0
- package/dist/templates/eslint.config.mjs +89 -0
- package/dist/templates/manifest.yaml +9 -0
- package/dist/templates/package.json +30 -0
- package/dist/templates/src/task.ts +7 -0
- package/dist/templates/tests/Task.spec.ts +47 -0
- package/dist/templates/tests/tsconfig.json +21 -0
- package/dist/templates/tsconfig.json +6 -0
- package/dist/types.js +25 -0
- package/dist/validators.js +26 -0
- package/package.json +58 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const capitalize_1 = __importDefault(require("lodash/capitalize"));
|
|
7
|
+
const types_1 = require("../../types");
|
|
8
|
+
const ArrayHandler_1 = __importDefault(require("./ArrayHandler"));
|
|
9
|
+
const TupleHandler_1 = __importDefault(require("./TupleHandler"));
|
|
10
|
+
class AbiTypeConverter {
|
|
11
|
+
constructor(importManager, tupleDefinitions) {
|
|
12
|
+
this.importManager = importManager;
|
|
13
|
+
this.tupleDefinitions = tupleDefinitions;
|
|
14
|
+
this.abiTypecastMap = this.generateAbiTypecastMap();
|
|
15
|
+
}
|
|
16
|
+
mapAbiType(param) {
|
|
17
|
+
const abiType = param.type;
|
|
18
|
+
if (ArrayHandler_1.default.isArrayType(abiType))
|
|
19
|
+
return ArrayHandler_1.default.generateLibTypeArray(param, this.mapAbiType.bind(this));
|
|
20
|
+
if (TupleHandler_1.default.isBaseTypeATuple(abiType)) {
|
|
21
|
+
const existingClassName = TupleHandler_1.default.getClassNameForTupleDefinition(param, this.tupleDefinitions);
|
|
22
|
+
if (existingClassName)
|
|
23
|
+
return existingClassName;
|
|
24
|
+
console.warn(`Tuple class name not found by AbiTypeConverter for: ${param.type}, internal: ${param.internalType}. It might be an anonymous or unextracted tuple.`);
|
|
25
|
+
return 'unknown';
|
|
26
|
+
}
|
|
27
|
+
const mapped = this.abiTypecastMap[abiType] || 'unknown';
|
|
28
|
+
if (mapped === 'unknown')
|
|
29
|
+
console.warn(`Unknown type: ${param.type}`);
|
|
30
|
+
if (Object.values(types_1.LibTypes).includes(mapped)) {
|
|
31
|
+
this.importManager.addType(mapped);
|
|
32
|
+
}
|
|
33
|
+
return mapped;
|
|
34
|
+
}
|
|
35
|
+
toLibType(paramType, paramName) {
|
|
36
|
+
if (ArrayHandler_1.default.isArrayType(paramType))
|
|
37
|
+
return paramName;
|
|
38
|
+
switch (paramType) {
|
|
39
|
+
case types_1.AssemblyPrimitiveTypes.bool:
|
|
40
|
+
this.importManager.addType(types_1.LibTypes.Bytes);
|
|
41
|
+
return `${types_1.LibTypes.Bytes}.fromBool(${paramName})`;
|
|
42
|
+
case types_1.AssemblyPrimitiveTypes.i8:
|
|
43
|
+
case types_1.AssemblyPrimitiveTypes.u8:
|
|
44
|
+
case types_1.AssemblyPrimitiveTypes.i16:
|
|
45
|
+
case types_1.AssemblyPrimitiveTypes.u16:
|
|
46
|
+
case types_1.AssemblyPrimitiveTypes.i32:
|
|
47
|
+
case types_1.AssemblyPrimitiveTypes.u32:
|
|
48
|
+
case types_1.AssemblyPrimitiveTypes.i64:
|
|
49
|
+
case types_1.AssemblyPrimitiveTypes.u64:
|
|
50
|
+
this.importManager.addType(types_1.LibTypes.BigInt);
|
|
51
|
+
return `${types_1.LibTypes.BigInt}.from${(0, capitalize_1.default)(paramType)}(${paramName})`;
|
|
52
|
+
case types_1.AssemblyPrimitiveTypes.string:
|
|
53
|
+
this.importManager.addType(types_1.LibTypes.Bytes);
|
|
54
|
+
return `${types_1.LibTypes.Bytes}.fromUTF8(${paramName})`;
|
|
55
|
+
case types_1.LibTypes.BigInt:
|
|
56
|
+
case types_1.LibTypes.Address:
|
|
57
|
+
case types_1.LibTypes.Bytes:
|
|
58
|
+
default:
|
|
59
|
+
return paramName;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
generateTypeConversion(type, valueVarName, isMapFunction, includeReturn = true) {
|
|
63
|
+
let conversion;
|
|
64
|
+
switch (type) {
|
|
65
|
+
case types_1.LibTypes.BigInt:
|
|
66
|
+
case types_1.LibTypes.Address:
|
|
67
|
+
conversion = `${type}.fromString(${valueVarName})`;
|
|
68
|
+
break;
|
|
69
|
+
case types_1.LibTypes.Bytes:
|
|
70
|
+
conversion = `${type}.fromHexString(${valueVarName})`;
|
|
71
|
+
break;
|
|
72
|
+
case types_1.AssemblyPrimitiveTypes.i8:
|
|
73
|
+
case types_1.AssemblyPrimitiveTypes.u8:
|
|
74
|
+
case types_1.AssemblyPrimitiveTypes.i16:
|
|
75
|
+
case types_1.AssemblyPrimitiveTypes.u16:
|
|
76
|
+
case types_1.AssemblyPrimitiveTypes.i32:
|
|
77
|
+
case types_1.AssemblyPrimitiveTypes.u32:
|
|
78
|
+
case types_1.AssemblyPrimitiveTypes.i64:
|
|
79
|
+
case types_1.AssemblyPrimitiveTypes.u64:
|
|
80
|
+
conversion = `${type}.parse(${valueVarName})`;
|
|
81
|
+
break;
|
|
82
|
+
case types_1.AssemblyPrimitiveTypes.bool:
|
|
83
|
+
conversion = `${types_1.AssemblyPrimitiveTypes.u8}.parse(${valueVarName}) as ${types_1.AssemblyPrimitiveTypes.bool}`;
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
conversion = TupleHandler_1.default.isTupleClassName(type, this.tupleDefinitions)
|
|
87
|
+
? `${type}.parse(${valueVarName})`
|
|
88
|
+
: valueVarName;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
return isMapFunction ? `${valueVarName} => ${conversion}` : includeReturn ? `return ${conversion}` : conversion;
|
|
92
|
+
}
|
|
93
|
+
generateIntegerTypeMappings() {
|
|
94
|
+
const mappings = {
|
|
95
|
+
int8: types_1.AssemblyPrimitiveTypes.i8,
|
|
96
|
+
uint8: types_1.AssemblyPrimitiveTypes.u8,
|
|
97
|
+
int16: types_1.AssemblyPrimitiveTypes.i16,
|
|
98
|
+
uint16: types_1.AssemblyPrimitiveTypes.u16,
|
|
99
|
+
int32: types_1.AssemblyPrimitiveTypes.i32,
|
|
100
|
+
uint32: types_1.AssemblyPrimitiveTypes.u32,
|
|
101
|
+
int64: types_1.AssemblyPrimitiveTypes.i64,
|
|
102
|
+
uint64: types_1.AssemblyPrimitiveTypes.u64,
|
|
103
|
+
int: types_1.LibTypes.BigInt,
|
|
104
|
+
uint: types_1.LibTypes.BigInt,
|
|
105
|
+
};
|
|
106
|
+
const START_BITS = 24;
|
|
107
|
+
const END_BITS = 256;
|
|
108
|
+
const STEP = 8;
|
|
109
|
+
for (let bits = START_BITS; bits <= END_BITS; bits += STEP) {
|
|
110
|
+
if (!mappings[`int${bits}`])
|
|
111
|
+
mappings[`int${bits}`] = types_1.LibTypes.BigInt;
|
|
112
|
+
if (!mappings[`uint${bits}`])
|
|
113
|
+
mappings[`uint${bits}`] = types_1.LibTypes.BigInt;
|
|
114
|
+
}
|
|
115
|
+
return mappings;
|
|
116
|
+
}
|
|
117
|
+
generateBytesTypeMappings() {
|
|
118
|
+
const mappings = {
|
|
119
|
+
bytes: types_1.LibTypes.Bytes,
|
|
120
|
+
};
|
|
121
|
+
const MAX_SIZE = 32;
|
|
122
|
+
for (let size = 1; size <= MAX_SIZE; size++) {
|
|
123
|
+
mappings[`bytes${size}`] = types_1.LibTypes.Bytes;
|
|
124
|
+
}
|
|
125
|
+
return mappings;
|
|
126
|
+
}
|
|
127
|
+
generateAbiTypecastMap() {
|
|
128
|
+
return {
|
|
129
|
+
...this.generateIntegerTypeMappings(),
|
|
130
|
+
...this.generateBytesTypeMappings(),
|
|
131
|
+
address: types_1.LibTypes.Address,
|
|
132
|
+
bool: types_1.AssemblyPrimitiveTypes.bool,
|
|
133
|
+
string: types_1.AssemblyPrimitiveTypes.string,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.default = AbiTypeConverter;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const types_1 = require("./types");
|
|
4
|
+
class ArrayHandler {
|
|
5
|
+
static isArrayType(abiOrMappedType) {
|
|
6
|
+
return abiOrMappedType.endsWith(']');
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Returns the base type of an array, e.g., "uint256[2][]" -> "uint256[2]".
|
|
10
|
+
*/
|
|
11
|
+
static getArrayType(type) {
|
|
12
|
+
if (!this.isArrayType(type))
|
|
13
|
+
return type;
|
|
14
|
+
const lastBracket = type.lastIndexOf('[');
|
|
15
|
+
if (lastBracket === -1)
|
|
16
|
+
return type;
|
|
17
|
+
return type.substring(0, lastBracket);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the depth of an array, e.g., "uint256[][]" -> 2.
|
|
21
|
+
*/
|
|
22
|
+
static getArrayDepth(abiType) {
|
|
23
|
+
let depth = 0;
|
|
24
|
+
let tempType = abiType;
|
|
25
|
+
while (tempType.endsWith(']')) {
|
|
26
|
+
depth++;
|
|
27
|
+
const lastBracket = tempType.lastIndexOf('[');
|
|
28
|
+
if (lastBracket === -1)
|
|
29
|
+
break;
|
|
30
|
+
tempType = tempType.substring(0, lastBracket);
|
|
31
|
+
}
|
|
32
|
+
return depth;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns the base type of a potentially nested ABI array type string.
|
|
36
|
+
* E.g., for "uint256[][]", it returns "uint256".
|
|
37
|
+
*/
|
|
38
|
+
static getBaseType(type) {
|
|
39
|
+
const bracketIndex = type.indexOf('[');
|
|
40
|
+
if (bracketIndex === -1)
|
|
41
|
+
return type;
|
|
42
|
+
return type.substring(0, bracketIndex);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Gets the mapped type of the base element of an array.
|
|
46
|
+
*/
|
|
47
|
+
static baseAbiTypeToLibType(param, mapBaseTypeFunc) {
|
|
48
|
+
const baseAbiType = this.getBaseType(param.type);
|
|
49
|
+
const baseParam = {
|
|
50
|
+
...param,
|
|
51
|
+
type: baseAbiType,
|
|
52
|
+
components: baseAbiType === types_1.TUPLE_ABI_TYPE ? param.components : undefined,
|
|
53
|
+
internalType: param.internalType ? this.getBaseType(param.internalType) : undefined,
|
|
54
|
+
};
|
|
55
|
+
return mapBaseTypeFunc(baseParam);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Constructs the full lib type array string, e.g., "BigInt[][]",
|
|
59
|
+
* based on the original ABI array type's depth.
|
|
60
|
+
*/
|
|
61
|
+
static generateLibTypeArray(originalParam, mapBaseTypeFunc) {
|
|
62
|
+
const mappedBaseType = this.baseAbiTypeToLibType(originalParam, mapBaseTypeFunc);
|
|
63
|
+
const depth = this.getArrayDepth(originalParam.type);
|
|
64
|
+
return mappedBaseType + '[]'.repeat(depth);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.default = ArrayHandler;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const types_1 = require("../../types");
|
|
7
|
+
const AbiTypeConverter_1 = __importDefault(require("./AbiTypeConverter"));
|
|
8
|
+
const FunctionHandler_1 = __importDefault(require("./FunctionHandler"));
|
|
9
|
+
const ImportManager_1 = __importDefault(require("./ImportManager"));
|
|
10
|
+
const NameManager_1 = __importDefault(require("./NameManager"));
|
|
11
|
+
const TupleHandler_1 = __importDefault(require("./TupleHandler"));
|
|
12
|
+
class ContractClassGenerator {
|
|
13
|
+
constructor(abi) {
|
|
14
|
+
this.abi = abi;
|
|
15
|
+
this.importManager = new ImportManager_1.default();
|
|
16
|
+
this.tupleDefinitions = TupleHandler_1.default.extractTupleDefinitions(this.abi);
|
|
17
|
+
this.abiTypeConverter = new AbiTypeConverter_1.default(this.importManager, this.tupleDefinitions);
|
|
18
|
+
}
|
|
19
|
+
generate(contractName) {
|
|
20
|
+
const mainClassCode = this.generateMainClass(contractName);
|
|
21
|
+
const tupleClassesCode = TupleHandler_1.default.generateTupleClassesCode(this.tupleDefinitions, this.importManager, this.abiTypeConverter);
|
|
22
|
+
// Note: this should be generated after any other generation
|
|
23
|
+
const importsCode = this.importManager.generateImportsCode();
|
|
24
|
+
const notice = '// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE MANUALLY.';
|
|
25
|
+
const separator = '\n\n';
|
|
26
|
+
let result = notice + separator + importsCode + separator + mainClassCode;
|
|
27
|
+
if (tupleClassesCode)
|
|
28
|
+
result += separator + tupleClassesCode;
|
|
29
|
+
return result.trim();
|
|
30
|
+
}
|
|
31
|
+
generateMainClass(contractName) {
|
|
32
|
+
const lines = [];
|
|
33
|
+
this.appendClassDefinition(lines, contractName);
|
|
34
|
+
const functions = NameManager_1.default.resolveMethodNames(this.getFunctions());
|
|
35
|
+
functions.forEach((fn) => FunctionHandler_1.default.appendMethod(lines, fn, this.importManager, this.tupleDefinitions, this.abiTypeConverter));
|
|
36
|
+
lines.push('}');
|
|
37
|
+
return lines.join('\n');
|
|
38
|
+
}
|
|
39
|
+
appendClassDefinition(lines, contractName) {
|
|
40
|
+
this.importManager.addType(types_1.LibTypes.Address);
|
|
41
|
+
this.importManager.addType(types_1.LibTypes.ChainId);
|
|
42
|
+
lines.push(`export class ${contractName} {`);
|
|
43
|
+
lines.push(` private _address: ${types_1.LibTypes.Address}`);
|
|
44
|
+
lines.push(` private _chainId: ${types_1.LibTypes.ChainId}`);
|
|
45
|
+
lines.push(` private _timestamp: ${types_1.AssemblyPrimitiveTypes.Date} | null`);
|
|
46
|
+
lines.push('');
|
|
47
|
+
lines.push(` constructor(address: ${types_1.LibTypes.Address}, chainId: ${types_1.LibTypes.ChainId}, timestamp: ${types_1.AssemblyPrimitiveTypes.Date} | null = null) {`);
|
|
48
|
+
lines.push(` this._address = address`);
|
|
49
|
+
lines.push(` this._chainId = chainId`);
|
|
50
|
+
lines.push(` this._timestamp = timestamp`);
|
|
51
|
+
lines.push(` }`);
|
|
52
|
+
lines.push('');
|
|
53
|
+
lines.push(` get address(): ${types_1.LibTypes.Address} {`);
|
|
54
|
+
lines.push(` return this._address.clone()`);
|
|
55
|
+
lines.push(` }`);
|
|
56
|
+
lines.push('');
|
|
57
|
+
lines.push(` get chainId(): ${types_1.LibTypes.ChainId} {`);
|
|
58
|
+
lines.push(` return this._chainId`);
|
|
59
|
+
lines.push(` }`);
|
|
60
|
+
lines.push('');
|
|
61
|
+
lines.push(` get timestamp(): ${types_1.AssemblyPrimitiveTypes.Date} | null {`);
|
|
62
|
+
lines.push(` return this._timestamp ? new ${types_1.AssemblyPrimitiveTypes.Date}(changetype<${types_1.AssemblyPrimitiveTypes.Date}>(this._timestamp).getTime()) : null`);
|
|
63
|
+
lines.push(` }`);
|
|
64
|
+
lines.push('');
|
|
65
|
+
}
|
|
66
|
+
getFunctions() {
|
|
67
|
+
return this.abi.filter((item) => item.type === 'function');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.default = ContractClassGenerator;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const helpers_1 = require("../../helpers");
|
|
40
|
+
const types_1 = require("../../types");
|
|
41
|
+
const ArrayHandler_1 = __importDefault(require("./ArrayHandler"));
|
|
42
|
+
const NameManager_1 = __importStar(require("./NameManager"));
|
|
43
|
+
const TupleHandler_1 = __importDefault(require("./TupleHandler"));
|
|
44
|
+
const types_2 = require("./types");
|
|
45
|
+
class FunctionHandler {
|
|
46
|
+
static appendMethod(lines, fn, importManager, tupleDefinitions, abiTypeConverter) {
|
|
47
|
+
const inputs = NameManager_1.default.resolveParameterNames(fn.inputs || [], NameManager_1.NameContext.FUNCTION_PARAMETER, 'param');
|
|
48
|
+
const methodParams = this.generateMethodParams(inputs, abiTypeConverter);
|
|
49
|
+
const returnType = this.getReturnType(fn, tupleDefinitions, abiTypeConverter);
|
|
50
|
+
const methodName = fn.escapedName || fn.name;
|
|
51
|
+
lines.push(` ${methodName}(${methodParams}): ${returnType} {`);
|
|
52
|
+
const callArgs = this.generateCallArguments(inputs, importManager, abiTypeConverter);
|
|
53
|
+
this.appendFunctionBody(lines, fn, returnType, callArgs, importManager, abiTypeConverter);
|
|
54
|
+
lines.push(` }`);
|
|
55
|
+
lines.push('');
|
|
56
|
+
}
|
|
57
|
+
static getReturnType(fn, tupleDefinitions, abiTypeConverter) {
|
|
58
|
+
if (this.isWriteFunction(fn))
|
|
59
|
+
return 'CallBuilder';
|
|
60
|
+
if (!fn.outputs || fn.outputs.length === 0)
|
|
61
|
+
return 'void';
|
|
62
|
+
if (fn.outputs.length === 1)
|
|
63
|
+
return abiTypeConverter.mapAbiType(fn.outputs[0]);
|
|
64
|
+
const name = TupleHandler_1.default.getOutputTupleClassName(fn.name);
|
|
65
|
+
const representativeOutputTuple = {
|
|
66
|
+
name,
|
|
67
|
+
type: types_2.TUPLE_ABI_TYPE,
|
|
68
|
+
internalType: `struct ${name}`,
|
|
69
|
+
components: fn.outputs,
|
|
70
|
+
};
|
|
71
|
+
const tupleClassName = TupleHandler_1.default.getClassNameForTupleDefinition(representativeOutputTuple, tupleDefinitions);
|
|
72
|
+
if (tupleClassName)
|
|
73
|
+
return tupleClassName;
|
|
74
|
+
console.error(`Could not determine tuple class name for outputs of function ${fn.name}`);
|
|
75
|
+
return 'unknown';
|
|
76
|
+
}
|
|
77
|
+
static generateMethodParams(inputs, abiTypeConverter) {
|
|
78
|
+
return inputs
|
|
79
|
+
.map((input) => {
|
|
80
|
+
const paramName = input.escapedName;
|
|
81
|
+
const type = abiTypeConverter.mapAbiType(input);
|
|
82
|
+
return `${paramName}: ${type}`;
|
|
83
|
+
})
|
|
84
|
+
.join(', ');
|
|
85
|
+
}
|
|
86
|
+
static buildEvmEncodeParamCode(valueIdentifier, paramDefinition, abiTypeConverter, importManager, depth = 0) {
|
|
87
|
+
importManager.addType('EvmEncodeParam');
|
|
88
|
+
const currentAbiTypeSignature = TupleHandler_1.default.isBaseTypeATuple(paramDefinition.type)
|
|
89
|
+
? TupleHandler_1.default.mapTupleType(paramDefinition.type)
|
|
90
|
+
: paramDefinition.type;
|
|
91
|
+
if (ArrayHandler_1.default.isArrayType(paramDefinition.type)) {
|
|
92
|
+
const elementLambdaVar = `s${depth}`;
|
|
93
|
+
const elementAbiDefinition = {
|
|
94
|
+
name: elementLambdaVar,
|
|
95
|
+
type: ArrayHandler_1.default.getArrayType(paramDefinition.type),
|
|
96
|
+
components: TupleHandler_1.default.isBaseTypeATuple(ArrayHandler_1.default.getArrayType(paramDefinition.type))
|
|
97
|
+
? paramDefinition.components
|
|
98
|
+
: undefined,
|
|
99
|
+
internalType: paramDefinition.internalType
|
|
100
|
+
? ArrayHandler_1.default.getArrayType(paramDefinition.internalType)
|
|
101
|
+
: undefined,
|
|
102
|
+
};
|
|
103
|
+
const nestedEvmParam = FunctionHandler.buildEvmEncodeParamCode(elementLambdaVar, elementAbiDefinition, abiTypeConverter, importManager, depth + 1);
|
|
104
|
+
return `EvmEncodeParam.fromValues('${currentAbiTypeSignature}', ${valueIdentifier}.map<EvmEncodeParam>((${elementLambdaVar}) => ${nestedEvmParam}))`;
|
|
105
|
+
}
|
|
106
|
+
if (TupleHandler_1.default.isBaseTypeATuple(paramDefinition.type)) {
|
|
107
|
+
return `EvmEncodeParam.fromValues('${currentAbiTypeSignature}', ${valueIdentifier}.toEvmEncodeParams())`;
|
|
108
|
+
}
|
|
109
|
+
const mappedParamType = abiTypeConverter.mapAbiType(paramDefinition);
|
|
110
|
+
const convertedValue = abiTypeConverter.toLibType(mappedParamType, valueIdentifier);
|
|
111
|
+
return `EvmEncodeParam.fromValue('${currentAbiTypeSignature}', ${convertedValue})`;
|
|
112
|
+
}
|
|
113
|
+
static generateCallArguments(inputs, importManager, abiTypeConverter) {
|
|
114
|
+
return inputs
|
|
115
|
+
.map((input) => {
|
|
116
|
+
const paramName = input.escapedName;
|
|
117
|
+
return FunctionHandler.buildEvmEncodeParamCode(paramName, input, abiTypeConverter, importManager, 0);
|
|
118
|
+
})
|
|
119
|
+
.join(', ');
|
|
120
|
+
}
|
|
121
|
+
static getDecodeAbiType(fn) {
|
|
122
|
+
const outputs = fn.outputs ?? [];
|
|
123
|
+
if (outputs.length === 0)
|
|
124
|
+
return '()';
|
|
125
|
+
if (outputs.length === 1) {
|
|
126
|
+
const [output] = outputs;
|
|
127
|
+
const { type, components } = output;
|
|
128
|
+
if (TupleHandler_1.default.isBaseTypeATuple(type) && components)
|
|
129
|
+
return TupleHandler_1.default.generateTupleTypeString(type, components);
|
|
130
|
+
return type;
|
|
131
|
+
}
|
|
132
|
+
return TupleHandler_1.default.generateTupleTypeString(types_2.TUPLE_ABI_TYPE, outputs);
|
|
133
|
+
}
|
|
134
|
+
static getReturnExpression(currentType, dataAccessString, importManager, abiTypeConverter, depth = 0) {
|
|
135
|
+
if (ArrayHandler_1.default.isArrayType(currentType)) {
|
|
136
|
+
importManager.addType('JSON');
|
|
137
|
+
const elementType = ArrayHandler_1.default.getArrayType(currentType);
|
|
138
|
+
const itemVar = `item${depth}`;
|
|
139
|
+
const subLogic = this.getReturnExpression(elementType, itemVar, importManager, abiTypeConverter, depth + 1);
|
|
140
|
+
return `${dataAccessString} === '' ? [] : JSON.parse<${types_1.AssemblyPrimitiveTypes.string}[]>(${dataAccessString}).map<${elementType}>(((${itemVar}: ${types_1.AssemblyPrimitiveTypes.string}) => ${subLogic}))`;
|
|
141
|
+
}
|
|
142
|
+
return abiTypeConverter.generateTypeConversion(currentType, dataAccessString, false, false);
|
|
143
|
+
}
|
|
144
|
+
static appendFunctionBody(lines, fn, returnType, callArgs, importManager, abiTypeConverter) {
|
|
145
|
+
const selector = (0, helpers_1.getFunctionSelector)(fn);
|
|
146
|
+
if (callArgs)
|
|
147
|
+
importManager.addType('evm');
|
|
148
|
+
const encodedCall = `'${selector}'${callArgs ? ` + evm.encode([${callArgs}])` : ''}`;
|
|
149
|
+
if (this.isWriteFunction(fn)) {
|
|
150
|
+
importManager.addType(types_1.LibTypes.Bytes);
|
|
151
|
+
importManager.addType('CallBuilder');
|
|
152
|
+
lines.push(` const encodedData = Bytes.fromHexString(${encodedCall})`);
|
|
153
|
+
lines.push(` return CallBuilder.forChain(this._chainId).addCall(this._address, encodedData)`);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
importManager.addType('environment');
|
|
157
|
+
const contractCallCode = `environment.contractCall(this._address, this._chainId, this._timestamp, ${encodedCall})`;
|
|
158
|
+
if (returnType === 'void') {
|
|
159
|
+
lines.push(` ${contractCallCode}`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
importManager.addType('EvmDecodeParam');
|
|
163
|
+
const decodeAbiType = this.getDecodeAbiType(fn);
|
|
164
|
+
lines.push(` const response = ${contractCallCode}`);
|
|
165
|
+
lines.push(` const decodedResponse = evm.decode(new EvmDecodeParam('${decodeAbiType}', response))`);
|
|
166
|
+
const returnExpression = this.getReturnExpression(returnType, 'decodedResponse', importManager, abiTypeConverter);
|
|
167
|
+
lines.push(` return ${returnExpression}`);
|
|
168
|
+
}
|
|
169
|
+
static isWriteFunction(fn) {
|
|
170
|
+
return ['nonpayable', 'payable'].includes(fn.stateMutability || '');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.default = FunctionHandler;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ImportManager {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.types = new Set();
|
|
6
|
+
}
|
|
7
|
+
addType(type) {
|
|
8
|
+
this.types.add(type);
|
|
9
|
+
}
|
|
10
|
+
generateImportsCode() {
|
|
11
|
+
if (this.types.size === 0)
|
|
12
|
+
return '';
|
|
13
|
+
const sortedTypes = [...this.types].sort((a, b) => String(a).localeCompare(String(b)));
|
|
14
|
+
return `import { ${sortedTypes.join(', ')} } from '@mimicprotocol/lib-ts'`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = ImportManager;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NameContext = void 0;
|
|
4
|
+
var NameContext;
|
|
5
|
+
(function (NameContext) {
|
|
6
|
+
NameContext["FUNCTION_PARAMETER"] = "function_parameter";
|
|
7
|
+
NameContext["LOCAL_VARIABLE"] = "local_variable";
|
|
8
|
+
NameContext["CLASS_PROPERTY"] = "class_property";
|
|
9
|
+
NameContext["METHOD_NAME"] = "method_name";
|
|
10
|
+
})(NameContext || (exports.NameContext = NameContext = {}));
|
|
11
|
+
class NameManager {
|
|
12
|
+
static resolveNameConflicts(names, context) {
|
|
13
|
+
const usedNames = new Set();
|
|
14
|
+
return names.map((originalName) => {
|
|
15
|
+
let escapedName = this.escapeName(originalName, context);
|
|
16
|
+
let counter = 1;
|
|
17
|
+
while (usedNames.has(escapedName)) {
|
|
18
|
+
const suffix = this.getSuffixForContext(context);
|
|
19
|
+
escapedName = `${originalName}${suffix}${counter}`;
|
|
20
|
+
counter++;
|
|
21
|
+
}
|
|
22
|
+
usedNames.add(escapedName);
|
|
23
|
+
return escapedName;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
static resolveParameterNames(parameters, context, defaultPrefix = 'param') {
|
|
27
|
+
const originalNames = parameters.map((param, index) => param.name && param.name.length > 0 ? param.name : `${defaultPrefix}${index}`);
|
|
28
|
+
const resolvedNames = this.resolveNameConflicts(originalNames, context);
|
|
29
|
+
return parameters.map((param, index) => ({
|
|
30
|
+
...param,
|
|
31
|
+
escapedName: resolvedNames[index],
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
static resolveMethodNames(functions) {
|
|
35
|
+
const methodNames = functions.map((fn) => fn.name);
|
|
36
|
+
const resolvedNames = this.resolveNameConflicts(methodNames, NameContext.METHOD_NAME);
|
|
37
|
+
return functions.map((fn, index) => ({
|
|
38
|
+
...fn,
|
|
39
|
+
escapedName: resolvedNames[index],
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
static hasConflict(name, context) {
|
|
43
|
+
if (this.RESERVED_BY_CONTEXT[context]?.has(name))
|
|
44
|
+
return true;
|
|
45
|
+
return this.INTERNAL_NAME_PATTERNS.some((pattern) => pattern.test(name));
|
|
46
|
+
}
|
|
47
|
+
static escapeName(name, context) {
|
|
48
|
+
if (!this.hasConflict(name, context))
|
|
49
|
+
return name;
|
|
50
|
+
const suffix = this.getSuffixForContext(context);
|
|
51
|
+
let escapedName = `${name}${suffix}`;
|
|
52
|
+
let counter = 1;
|
|
53
|
+
while (this.hasConflict(escapedName, context)) {
|
|
54
|
+
escapedName = `${name}${suffix}${counter}`;
|
|
55
|
+
counter++;
|
|
56
|
+
}
|
|
57
|
+
return escapedName;
|
|
58
|
+
}
|
|
59
|
+
static getSuffixForContext(context) {
|
|
60
|
+
switch (context) {
|
|
61
|
+
case NameContext.FUNCTION_PARAMETER:
|
|
62
|
+
return '_param';
|
|
63
|
+
case NameContext.LOCAL_VARIABLE:
|
|
64
|
+
return '_var';
|
|
65
|
+
case NameContext.CLASS_PROPERTY:
|
|
66
|
+
return '_prop';
|
|
67
|
+
case NameContext.METHOD_NAME:
|
|
68
|
+
return '_';
|
|
69
|
+
default:
|
|
70
|
+
return '_safe';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
NameManager.RESERVED_BY_CONTEXT = {
|
|
75
|
+
[NameContext.FUNCTION_PARAMETER]: new Set([
|
|
76
|
+
'response',
|
|
77
|
+
'decodedResponse',
|
|
78
|
+
'selector',
|
|
79
|
+
'callArgs',
|
|
80
|
+
'contractCallCode',
|
|
81
|
+
'inputs',
|
|
82
|
+
'methodParams',
|
|
83
|
+
'returnType',
|
|
84
|
+
'lines',
|
|
85
|
+
]),
|
|
86
|
+
[NameContext.LOCAL_VARIABLE]: new Set(['data', 'parts', 'response', 'decodedResponse', 'selector', 'encodedData']),
|
|
87
|
+
[NameContext.CLASS_PROPERTY]: new Set([
|
|
88
|
+
'constructor',
|
|
89
|
+
'parse',
|
|
90
|
+
'toEvmEncodeParams',
|
|
91
|
+
'address',
|
|
92
|
+
'chainId',
|
|
93
|
+
'timestamp',
|
|
94
|
+
]),
|
|
95
|
+
[NameContext.METHOD_NAME]: new Set(['constructor']),
|
|
96
|
+
};
|
|
97
|
+
NameManager.INTERNAL_NAME_PATTERNS = [/^item\d+$/, /^s\d+$/];
|
|
98
|
+
exports.default = NameManager;
|