Skip to content

Commit

Permalink
Reduce the generated code size
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Sep 11, 2024
1 parent 68bbcde commit dabc857
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 80 deletions.
92 changes: 15 additions & 77 deletions artifacts/ts/MapTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export namespace MapTestTypes {
SignExecuteMethodTable[T]["params"];
export type SignExecuteMethodResult<T extends keyof SignExecuteMethodTable> =
SignExecuteMethodTable[T]["result"];

export type Maps = {
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
};
}

class Factory extends ContractFactory<MapTestInstance, {}> {
Expand All @@ -124,99 +130,35 @@ class Factory extends ContractFactory<MapTestInstance, {}> {
TestContractParams<
never,
{ key: Address; value: MapValue },
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
MapTestTypes.Maps
>,
"initialFields"
>
): Promise<
TestContractResult<
null,
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
): Promise<TestContractResult<null, MapTestTypes.Maps>> => {
return testMethod(this, "insert", params, getContractByCodeHash);
},
update: async (
params: Omit<
TestContractParams<
never,
{ key: Address },
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>,
TestContractParams<never, { key: Address }, MapTestTypes.Maps>,
"initialFields"
>
): Promise<
TestContractResult<
null,
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
): Promise<TestContractResult<null, MapTestTypes.Maps>> => {
return testMethod(this, "update", params, getContractByCodeHash);
},
remove: async (
params: Omit<
TestContractParams<
never,
{ key: Address },
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>,
TestContractParams<never, { key: Address }, MapTestTypes.Maps>,
"initialFields"
>
): Promise<
TestContractResult<
null,
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
): Promise<TestContractResult<null, MapTestTypes.Maps>> => {
return testMethod(this, "remove", params, getContractByCodeHash);
},
getValue: async (
params: Omit<
TestContractParams<
never,
{ key: Address },
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>,
TestContractParams<never, { key: Address }, MapTestTypes.Maps>,
"initialFields"
>
): Promise<
TestContractResult<
MapValue,
{
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
>
> => {
): Promise<TestContractResult<MapValue, MapTestTypes.Maps>> => {
return testMethod(this, "getValue", params, getContractByCodeHash);
},
};
Expand All @@ -225,11 +167,7 @@ class Factory extends ContractFactory<MapTestInstance, {}> {
initFields: {},
asset?: Asset,
address?: string,
maps?: {
map0?: Map<Address, MapValue>;
map1?: Map<bigint, bigint>;
map2?: Map<HexString, bigint>;
}
maps?: MapTestTypes.Maps
) {
return this.stateForTest_(initFields, asset, address, maps);
}
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ function genMapsType(contract: Contract): string {
const [key, value] = parseMapType(mapType)
return `${name}?: Map<${toTsType(key)}, ${toTsType(value)}>`
})
return `{ ${mapFields.join(', ')} }`
return `export type Maps = { ${mapFields.join(', ')} }`
}

function getMapsType(contract: Contract): string {
return `${contractTypes(contract.name)}.Maps`
}

function genTestMethod(contract: Contract, functionSig: FunctionSig): string {
Expand All @@ -340,7 +344,7 @@ function genTestMethod(contract: Contract, functionSig: FunctionSig): string {
: 'never'
const fieldsType = contractHasFields ? contractFieldType(contract.name, fieldsSig) : 'never'
const hasMapVars: boolean = contract.mapsSig !== undefined
const mapsType = genMapsType(contract)
const mapsType = getMapsType(contract)
const baseParamsType = hasMapVars
? `TestContractParams<${fieldsType}, ${argsType}, ${mapsType}>`
: `TestContractParamsWithoutMaps<${fieldsType}, ${argsType}>`
Expand Down Expand Up @@ -380,7 +384,7 @@ function genTestMethods(contract: Contract): string {

function genStateForTest(contract: Contract, fieldType: string): string {
const hasMap = contract.mapsSig !== undefined
const mapsParam = hasMap ? `, maps?: ${genMapsType(contract)}` : ''
const mapsParam = hasMap ? `, maps?: ${getMapsType(contract)}` : ''
const mapsValue = hasMap ? 'maps' : 'undefined'
return `
stateForTest(initFields: ${fieldType}, asset?: Asset, address?: string${mapsParam}) {
Expand Down Expand Up @@ -521,6 +525,7 @@ function genContract(contract: Contract, artifactRelativePath: string): string {
${contract.eventsSig.map((e) => genEventType(e)).join('\n')}
${genCallMethodTypes(contract)}
${genSignExecuteMethodTypes(contract)}
${genMapsType(contract)}
}
class Factory extends ContractFactory<${contract.name}Instance, ${fieldType}> {
Expand Down

0 comments on commit dabc857

Please sign in to comment.