Skip to content

Commit

Permalink
[TypeTag] Adding explicit return types for TypeTag types, fix vectors…
Browse files Browse the repository at this point in the history
… `toString()` function, add aptosCoinStructTag (#144)

* Adding explicit string return values and other misc changes to typeTagParser.ts

* Formatting

* Fixing string format return value
  • Loading branch information
xbtmatt authored Oct 31, 2023
1 parent 7626d37 commit cf5ef70
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/transactions/typeTag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,7 +225,7 @@ export class TypeTagSigner extends TypeTag {
}

export class TypeTagReference extends TypeTag {
toString(): string {
toString(): `&${string}` {
return `&${this.value.toString()}`;
}

Expand All @@ -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}`;
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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]);
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/typeTagParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()));
});
Expand Down

0 comments on commit cf5ef70

Please sign in to comment.