diff --git a/packages/abi/src/parser/specifications/v1/parser.ts b/packages/abi/src/parser/specifications/v1/parser.ts index 36b05fc737..0048ea802f 100644 --- a/packages/abi/src/parser/specifications/v1/parser.ts +++ b/packages/abi/src/parser/specifications/v1/parser.ts @@ -18,8 +18,13 @@ export class AbiParserV1 { static parse(abi: AbiSpecificationV1): Abi { const cleanAbi = cleanupAbi(abi); + const abiTypeMaps = { + metadataTypes: new Map(cleanAbi.metadataTypes.map((type) => [type.metadataTypeId, type])), + concreteTypes: new Map(cleanAbi.concreteTypes.map((type) => [type.concreteTypeId, type])), + }; + const resolvableTypes = cleanAbi.metadataTypes.map( - (metadataType) => new ResolvableType(cleanAbi, metadataType.metadataTypeId, undefined) + (metadataType) => new ResolvableType(abiTypeMaps, metadataType.metadataTypeId, undefined) ); const concreteTypes = cleanAbi.concreteTypes.map((concreteType) => { diff --git a/packages/abi/src/parser/specifications/v1/resolvable-type.ts b/packages/abi/src/parser/specifications/v1/resolvable-type.ts index 8a297214e6..6f85aad337 100644 --- a/packages/abi/src/parser/specifications/v1/resolvable-type.ts +++ b/packages/abi/src/parser/specifications/v1/resolvable-type.ts @@ -8,7 +8,6 @@ import type { AbiComponentV1, AbiConcreteTypeV1, AbiMetadataTypeV1, - AbiSpecificationV1, AbiTypeArgumentV1, } from './specification'; @@ -23,7 +22,10 @@ export class ResolvableType { components: ResolvableComponent[] | undefined; constructor( - private abi: AbiSpecificationV1, + private abiTypeMaps: { + metadataTypes: Map; + concreteTypes: Map; + }, public metadataTypeId: number, public typeParamsArgsMap: Array<[number, ResolvedType | ResolvableType]> | undefined ) { @@ -31,7 +33,7 @@ export class ResolvableType { this.swayType = this.metadataType.type; this.typeParamsArgsMap ??= this.metadataType.typeParameters?.map((tp) => [ tp, - new ResolvableType(this.abi, tp, undefined), + new ResolvableType(this.abiTypeMaps, tp, undefined), ]); this.components = this.metadataType.components?.map((c) => @@ -47,9 +49,8 @@ export class ResolvableType { * @throws If the metadata type can not be found in the ABI. */ private findMetadataType(metadataTypeId: number): AbiMetadataTypeV1 { - const metadataType = this.abi.metadataTypes.find( - (type) => type.metadataTypeId === metadataTypeId - ); + const metadataType = this.abiTypeMaps.metadataTypes.get(metadataTypeId); + if (!metadataType) { throw new FuelError( FuelError.CODES.TYPE_NOT_FOUND, @@ -67,9 +68,8 @@ export class ResolvableType { * @throws If the concrete type can not be found in the ABI. */ private findConcreteType(concreteTypeId: string): AbiConcreteTypeV1 { - const concreteType = this.abi.concreteTypes.find( - (type) => type.concreteTypeId === concreteTypeId - ); + const concreteType = this.abiTypeMaps.concreteTypes.get(concreteTypeId); + if (!concreteType) { throw new FuelError( FuelError.CODES.TYPE_NOT_FOUND, @@ -128,7 +128,7 @@ export class ResolvableType { * This would be the case for e.g. non-generic structs and enums. */ if (!type.typeArguments) { - return new ResolvableType(this.abi, type.metadataTypeId, undefined).resolveInternal( + return new ResolvableType(this.abiTypeMaps, type.metadataTypeId, undefined).resolveInternal( type.concreteTypeId, undefined ); @@ -147,7 +147,7 @@ export class ResolvableType { }); return new ResolvableType( - this.abi, + this.abiTypeMaps, type.metadataTypeId, ResolvableType.mapTypeParametersAndArgs(metadataType, concreteTypeArgs) ).resolveInternal(type.concreteTypeId, undefined); @@ -182,7 +182,7 @@ export class ResolvableType { return ( resolvableTypeParameter ?? - new ResolvableType(this.abi, metadataType.metadataTypeId, undefined) + new ResolvableType(this.abiTypeMaps, metadataType.metadataTypeId, undefined) ); } @@ -192,10 +192,11 @@ export class ResolvableType { * if they aren't used _directly_ in a function-input/function-output/log/configurable/messageType * These types are characterized by not having components and we can resolve them as-is */ - return new ResolvableType(this.abi, metadataType.metadataTypeId, undefined).resolveInternal( + return new ResolvableType( + this.abiTypeMaps, metadataType.metadataTypeId, undefined - ); + ).resolveInternal(metadataType.metadataTypeId, undefined); } const typeArgs = typeArguments?.map( @@ -203,7 +204,7 @@ export class ResolvableType { ); const resolvable = new ResolvableType( - this.abi, + this.abiTypeMaps, metadataType.metadataTypeId, !typeArgs?.length ? undefined