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

Method createLibrary

src/code-gen/word-packer.ts:90–153  ·  view source on GitHub ↗
(structsAndEnums: Array<AbiStruct | AbiEnum>, context: FileContext)

Source from the content-addressed store, hash-verified

88
89export class UnpackerGen {
90 static createLibrary(structsAndEnums: Array<AbiStruct | AbiEnum>, context: FileContext): {
91 code: string;
92 libraryName?: string;
93 externalCode: string;
94 hardhatTest: string
95 } {
96 const libraryCode: string[] = [];
97 const externalCode: string[] = [];
98 const hardhatTests: string[] = [];
99 let processedStruct: ProcessedStruct;
100
101 for (let structOrEnum of structsAndEnums) {
102 if (structOrEnum.dynamic) {
103 throw Error('Dynamic sized structs not currently supported');
104 }
105 if (structOrEnum.meta === 'enum') {
106 libraryCode.push(
107 arrJoiner(abiStructToSol(structOrEnum)),
108 ''
109 );
110 } else {
111 processedStruct = processStruct(structOrEnum, context)
112 const code = generateCoderLibrary(processedStruct, context)
113 if (!context.opts.constantsFile) {
114 libraryCode.push(arrJoiner(context.constants))
115 }
116 libraryCode.push(code, '');
117 const externalFns = generateExternalCoder(
118 context.functions,
119 structOrEnum.name,
120 `_${structOrEnum.name.toCamelCase()}`,
121 `${structOrEnum.name}Coder`
122 );
123 externalCode.push(arrJoiner(externalFns.externalCode))
124 hardhatTests.push(generateHardhatTest(processedStruct, externalFns.externalFunctions, structOrEnum.name))
125 context.clearCode();
126 }
127 }
128 if (libraryCode.length && libraryCode[libraryCode.length - 1] === '') {
129 libraryCode.pop();
130 }
131 // let code = arrJoiner([
132 // ...generateFileHeader(true, context.opts.unsafe, context.opts.constantsFile && ['import "./CoderConstants.sol";']),
133 // ...libraryCode,
134 // ])
135 // code = prettierFormat(
136 // context.opts.noComments ? strip(code) : code
137 // );
138 // const testCode = arrJoiner(generateExternalCoder(context.functions, ))
139 const structs = structsAndEnums.filter(f => f.meta === 'struct');
140 const libraryName = structs.length === 1 && `${structs[0].name}Coder`;
141 const code = generateSolFile(libraryCode, context, context.opts.constantsFile && ['import "./CoderConstants.sol";']);
142 const coderPath = isSolFile(context.opts.output) ? context.opts.output : path.join(context.opts.output, `${libraryName}.sol`);
143 const testPath = context.opts.testContractsDirectory || getDir(coderPath);
144
145 const relativePath = path.normalize(path.relative(testPath, coderPath))
146 const externalFile = generateSolFile(externalCode, context, [`import "${relativePath}";`]);
147 return {

Callers 1

cli.tsFile · 0.80

Calls 11

arrJoinerFunction · 0.90
abiStructToSolFunction · 0.90
processStructFunction · 0.90
generateExternalCoderFunction · 0.90
generateHardhatTestFunction · 0.90
isSolFileFunction · 0.90
getDirFunction · 0.90
generateCoderLibraryFunction · 0.85
generateSolFileFunction · 0.85
toCamelCaseMethod · 0.80
clearCodeMethod · 0.80

Tested by

no test coverage detected