-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ns/feat/abi-parser' into ns/feat/abi-typegen
- Loading branch information
Showing
5 changed files
with
135 additions
and
191 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
packages/abi/src/parser/specifications/v1/abi-type-mappers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
import type { | ||
AbiConcreteType, | ||
AbiMetadataType, | ||
AbiTypeArgument, | ||
AbiTypeComponent, | ||
} from '../../abi'; | ||
|
||
import type { ResolvableComponent, ResolvableType } from './resolvable-type'; | ||
import type { ResolvedType } from './resolved-type'; | ||
|
||
function mapMetadata(type: ResolvableType | ResolvedType) { | ||
const result: AbiTypeComponent['type']['metadata'] = { | ||
metadataTypeId: type.metadataType?.metadataTypeId as number, | ||
}; | ||
|
||
if (type.typeParamsArgsMap && type.metadataType?.typeParameters?.length) { | ||
result.typeArguments = type.typeParamsArgsMap.map((t) => toTypeArgument(t[1])); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function isResolvedType(type: ResolvableType | ResolvedType): type is ResolvedType { | ||
return 'typeId' in type; | ||
} | ||
|
||
function isResolvedConcreteType( | ||
type: ResolvableType | ResolvedType | ||
): type is ResolvedType & { typeId: string } { | ||
return isResolvedType(type) && typeof type.typeId === 'string'; | ||
} | ||
|
||
function mapComponentType(component: ResolvableComponent): AbiTypeComponent { | ||
const { name, type } = component; | ||
|
||
let result: AbiTypeComponent['type']; | ||
|
||
if (isResolvedConcreteType(type)) { | ||
result = { | ||
swayType: type.swayType, | ||
concreteTypeId: type.typeId, | ||
}; | ||
if (type.metadataType) { | ||
result.metadata = mapMetadata(type) as AbiConcreteType['metadata']; | ||
} | ||
} else { | ||
result = { | ||
swayType: type.swayType, | ||
metadata: mapMetadata(type), | ||
}; | ||
} | ||
|
||
if (type.components) { | ||
result.components = type.components.map(mapComponentType); | ||
} | ||
|
||
return { name, type: result }; | ||
} | ||
|
||
function toTypeArgument(type: ResolvableType | ResolvedType): AbiTypeArgument { | ||
// type args and components follow the same mapping logic | ||
return mapComponentType({ name: '', type }).type; | ||
} | ||
|
||
export function toAbiType(t: ResolvableType | ResolvedType): AbiConcreteType | AbiMetadataType { | ||
let result: AbiConcreteType | AbiMetadataType; | ||
|
||
if (isResolvedConcreteType(t)) { | ||
result = { | ||
concreteTypeId: t.typeId, | ||
swayType: t.swayType, | ||
}; | ||
|
||
if (t.metadataType) { | ||
result.metadata = mapMetadata(t) as AbiConcreteType['metadata']; | ||
} | ||
} else { | ||
result = { | ||
swayType: t.swayType, | ||
metadataTypeId: t.metadataType?.metadataTypeId as number, | ||
}; | ||
|
||
if (t.typeParamsArgsMap) { | ||
result.typeParameters = t.typeParamsArgsMap.map(([, rt]) => toAbiType(rt) as AbiMetadataType); | ||
} | ||
} | ||
|
||
if (t.components) { | ||
result.components = t.components.map(mapComponentType); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.