(struct: ProcessedStruct, context: FileContext)
| 25 | } |
| 26 | |
| 27 | export 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 | |
| 78 | function generateSolFile(codeLines: string[], context: FileContext, imports: string[]) { |
| 79 | let code = arrJoiner([ |
no test coverage detected