MCPcopy Create free account
hub / github.com/d1ll0n/stack-packer / generateCoderLibrary

Function generateCoderLibrary

src/code-gen/word-packer.ts:27–76  ·  view source on GitHub ↗
(struct: ProcessedStruct, context: FileContext)

Source from the content-addressed store, hash-verified

25}
26
27export function generateCoderLibrary(struct: ProcessedStruct, context: FileContext) {
28 if (struct.dynamic) {
29 throw Error('Does not support dynamic structs');
30 }
31
32 const { fields, groups } = struct
33
34 context.addFunctions([
35 getDecodeFunction(struct, fields),
36 getEncodeFunction(struct, fields, context),
37 ].filter(Boolean), struct.name)
38
39 for (const group of groups) {
40 const functions = [
41 getEncodeGroupFunction(struct, group, context),
42 getDecodeGroupFunction(struct, group)
43 ].filter(Boolean)
44 context.addFunctions(functions, `${struct.name} ${group.name} coders`)
45 }
46
47 for (const field of fields) {
48 context.addFunctions(generateFieldAccessors(struct, field, context), `${field.structName}.${field.originalName} coders`)
49 }
50
51 context.addFunctions(generateComparisonFunctions(struct), `${struct.name} comparison methods`)
52
53 const typeDef = [
54 `// struct ${struct.name} {`,
55 ...fields.map((field) => `// ${toTypeName(field.type)} ${field.originalName};`),
56 '// }',
57 `type ${struct.name} is uint256;`,
58 '',
59 `${struct.name} constant Default${struct.name} = ${struct.name}.wrap(0);`
60 ];
61 context.constants.sort();
62
63 const topLevel = [
64 ...typeDef,
65 '',
66 `library ${struct.name}Coder {`,
67 // decodeFunctionBlock,
68 // '',
69 // encodeFunctionBlock,
70 // '',
71 ...context.code,
72 `}`,
73 ];
74
75 return arrJoiner(topLevel);
76}
77
78function generateSolFile(codeLines: string[], context: FileContext, imports: string[]) {
79 let code = arrJoiner([

Callers 1

createLibraryMethod · 0.85

Calls 9

getDecodeFunctionFunction · 0.90
getEncodeFunctionFunction · 0.90
getEncodeGroupFunctionFunction · 0.90
getDecodeGroupFunctionFunction · 0.90
generateFieldAccessorsFunction · 0.90
toTypeNameFunction · 0.90
arrJoinerFunction · 0.90
addFunctionsMethod · 0.80

Tested by

no test coverage detected