From cf5ef706a852ae981e013c137d70d47b8e4d0d83 Mon Sep 17 00:00:00 2001 From: Matt <90358481+xbtmatt@users.noreply.github.com> Date: Tue, 31 Oct 2023 00:55:49 -0700 Subject: [PATCH] [TypeTag] Adding explicit return types for TypeTag types, fix vectors `toString()` function, add aptosCoinStructTag (#144) * Adding explicit string return values and other misc changes to typeTagParser.ts * Formatting * Fixing string format return value --- src/transactions/typeTag/index.ts | 20 +++++++++++++------- tests/unit/typeTagParser.test.ts | 8 ++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/transactions/typeTag/index.ts b/src/transactions/typeTag/index.ts index 2dd34e02f..9a7de8c8a 100644 --- a/src/transactions/typeTag/index.ts +++ b/src/transactions/typeTag/index.ts @@ -9,6 +9,7 @@ import { Serializable, Serializer } from "../../bcs/serializer"; import { AccountAddress } from "../../core"; import { Identifier } from "../instances/identifier"; import { TypeTagVariants } from "../../types"; +import { APTOS_COIN } from "../../utils/const"; export abstract class TypeTag extends Serializable { abstract serialize(serializer: Serializer): void; @@ -224,7 +225,7 @@ export class TypeTagSigner extends TypeTag { } export class TypeTagReference extends TypeTag { - toString(): string { + toString(): `&${string}` { return `&${this.value.toString()}`; } @@ -248,7 +249,7 @@ export class TypeTagReference extends TypeTag { * used as a type directly. */ export class TypeTagGeneric extends TypeTag { - toString(): string { + toString(): `T${number}` { return `T${this.value}`; } @@ -268,8 +269,8 @@ export class TypeTagGeneric extends TypeTag { } export class TypeTagVector extends TypeTag { - toString(): string { - return `vector${this.value.toString()}`; + toString(): `vector<${string}>` { + return `vector<${this.value.toString()}>`; } constructor(public readonly value: TypeTag) { @@ -288,7 +289,7 @@ export class TypeTagVector extends TypeTag { } export class TypeTagStruct extends TypeTag { - toString(): string { + toString(): `0x${string}::${string}::${string}` { // Collect type args and add it if there are any let typePredicate = ""; if (this.value.type_args.length > 0) { @@ -368,8 +369,13 @@ export class StructTag extends Serializable { } } -export const stringStructTag = () => - new StructTag(AccountAddress.ONE, new Identifier("string"), new Identifier("String"), []); +export function aptosCoinStructTag(): StructTag { + return new StructTag(AccountAddress.ONE, new Identifier("aptos_coin"), new Identifier("AptosCoin"), []); +} + +export function stringStructTag(): StructTag { + return new StructTag(AccountAddress.ONE, new Identifier("string"), new Identifier("String"), []); +} export function optionStructTag(typeArg: TypeTag): StructTag { return new StructTag(AccountAddress.ONE, new Identifier("option"), new Identifier("Option"), [typeArg]); diff --git a/tests/unit/typeTagParser.test.ts b/tests/unit/typeTagParser.test.ts index 7d9a13b07..297389dbc 100644 --- a/tests/unit/typeTagParser.test.ts +++ b/tests/unit/typeTagParser.test.ts @@ -24,8 +24,10 @@ import { TypeTagParserError, TypeTagParserErrorType, TypeTagReference, + aptosCoinStructTag, } from "../../src"; import { Identifier } from "../../src/transactions/instances"; +import { APTOS_COIN } from "../../src/utils/const"; const MODULE_NAME = new Identifier("tag"); const STRUCT_NAME = new Identifier("Tag"); @@ -162,6 +164,12 @@ describe("TypeTagParser", () => { ); }); + test("aptos coin", () => { + const aptosCoin = new TypeTagStruct(aptosCoinStructTag()); + expect(parseTypeTag("0x1::aptos_coin::AptosCoin")).toEqual(aptosCoin); + expect(parseTypeTag(APTOS_COIN)).toEqual(aptosCoin); + }); + test("string", () => { expect(parseTypeTag("0x1::string::String")).toEqual(new TypeTagStruct(stringStructTag())); });