Skip to content

Commit

Permalink
refactor into using maps for types
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Dec 17, 2024
1 parent c4ed9d8 commit 3de5f80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
7 changes: 6 additions & 1 deletion packages/abi/src/parser/specifications/v1/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
31 changes: 16 additions & 15 deletions packages/abi/src/parser/specifications/v1/resolvable-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
AbiComponentV1,
AbiConcreteTypeV1,
AbiMetadataTypeV1,
AbiSpecificationV1,
AbiTypeArgumentV1,
} from './specification';

Expand All @@ -23,15 +22,18 @@ export class ResolvableType {
components: ResolvableComponent[] | undefined;

constructor(
private abi: AbiSpecificationV1,
private abiTypeMaps: {
metadataTypes: Map<number, AbiMetadataTypeV1>;
concreteTypes: Map<string, AbiConcreteTypeV1>;
},
public metadataTypeId: number,
public typeParamsArgsMap: Array<[number, ResolvedType | ResolvableType]> | undefined
) {
this.metadataType = this.findMetadataType(metadataTypeId);
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) =>
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
);
Expand All @@ -147,7 +147,7 @@ export class ResolvableType {
});

return new ResolvableType(
this.abi,
this.abiTypeMaps,
type.metadataTypeId,
ResolvableType.mapTypeParametersAndArgs(metadataType, concreteTypeArgs)
).resolveInternal(type.concreteTypeId, undefined);
Expand Down Expand Up @@ -182,7 +182,7 @@ export class ResolvableType {

return (
resolvableTypeParameter ??
new ResolvableType(this.abi, metadataType.metadataTypeId, undefined)
new ResolvableType(this.abiTypeMaps, metadataType.metadataTypeId, undefined)
);
}

Expand All @@ -192,18 +192,19 @@ 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(
(typeArgument) => this.createResolvableComponent(parent, typeArgument).type
);

const resolvable = new ResolvableType(
this.abi,
this.abiTypeMaps,
metadataType.metadataTypeId,
!typeArgs?.length
? undefined
Expand Down

0 comments on commit 3de5f80

Please sign in to comment.