Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[string] Add toString for most classes #220

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bcs/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,8 @@ export class Deserializer {
}
return vector;
}

toString(): string {
return `Deserializer(offset: ${this.offset} buffer: ${this.buffer})`;
}
}
5 changes: 5 additions & 0 deletions src/bcs/serializable/entryFunctionBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Deserializer } from "../deserializer";
import { FixedBytes } from "./fixedBytes";
import { EntryFunctionArgument } from "../../transactions/instances/transactionArgument";
import { HexInput } from "../../types";
import { Hex } from "../../core/hex";

/**
* This class exists solely to represent a sequence of fixed bytes as a serialized entry function, because
Expand Down Expand Up @@ -58,4 +59,8 @@ export class EntryFunctionBytes extends Serializable implements EntryFunctionArg
const fixedBytes = FixedBytes.deserialize(deserializer, length);
return new EntryFunctionBytes(fixedBytes.value);
}

toString(): string {
return Hex.fromHexInput(this.value.value).toString();
}
}
4 changes: 4 additions & 0 deletions src/bcs/serializable/fixedBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ export class FixedBytes extends Serializable implements TransactionArgument {
const bytes = deserializer.deserializeFixedBytes(length);
return new FixedBytes(bytes);
}

toString(): string {
return Hex.fromHexInput(this.value).toString();
}
}
28 changes: 28 additions & 0 deletions src/bcs/serializable/movePrimitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export class Bool extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): Bool {
return new Bool(deserializer.deserializeBool());
}

toString(): string {
return this.value.toString();
}
}

export class U8 extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -68,6 +72,10 @@ export class U8 extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): U8 {
return new U8(deserializer.deserializeU8());
}

toString(): string {
return this.value.toString();
}
}

export class U16 extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -96,6 +104,10 @@ export class U16 extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): U16 {
return new U16(deserializer.deserializeU16());
}

toString(): string {
return this.value.toString();
}
}

export class U32 extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -124,6 +136,10 @@ export class U32 extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): U32 {
return new U32(deserializer.deserializeU32());
}

toString(): string {
return this.value.toString();
}
}

export class U64 extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -152,6 +168,10 @@ export class U64 extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): U64 {
return new U64(deserializer.deserializeU64());
}

toString(): string {
return this.value.toString();
}
}

export class U128 extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -180,6 +200,10 @@ export class U128 extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): U128 {
return new U128(deserializer.deserializeU128());
}

toString(): string {
return this.value.toString();
}
}

export class U256 extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -208,4 +232,8 @@ export class U256 extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): U256 {
return new U256(deserializer.deserializeU256());
}

toString(): string {
return this.value.toString();
}
}
105 changes: 79 additions & 26 deletions src/bcs/serializable/moveStructs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ export class MoveVector<T extends Serializable & EntryFunctionArgument>
}
return new MoveVector(values);
}

toString(): string {
return `[${this.values.map((v) => v.toString()).join(",")}]`;
}
}

export class MoveString extends Serializable implements TransactionArgument {
Expand Down Expand Up @@ -250,6 +254,10 @@ export class MoveString extends Serializable implements TransactionArgument {
static deserialize(deserializer: Deserializer): MoveString {
return new MoveString(deserializer.deserializeStr());
}

toString(): string {
return this.value;
}
}

export class MoveOption<T extends Serializable & EntryFunctionArgument>
Expand Down Expand Up @@ -279,8 +287,10 @@ export class MoveOption<T extends Serializable & EntryFunctionArgument>
/**
* Retrieves the inner value of the MoveOption.
*
* This method is inspired by Rust's `Option<T>.unwrap()`.
* In Rust, attempting to unwrap a `None` value results in a panic.
* This method is inspired by Rust's `;
Option<T>.unwrap()`.
* In Rust, attempting to unwrap a `;
None` value results in a panic.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format here is a bit weird

*
* Similarly, this method will throw an error if the value is not present.
*
Expand Down Expand Up @@ -312,121 +322,156 @@ export class MoveOption<T extends Serializable & EntryFunctionArgument>
}

/**
* Factory method to generate a MoveOption<U8> from a `number` or `undefined`.
* Factory method to generate a MoveOption<U8> from a `;
number` or `;
undefined`.
*
* @example
* MoveOption.U8(1).isSome() === true;
* MoveOption.U8().isSome() === false;
* MoveOption.U8(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<U8> with an inner value `value`
* @returns a MoveOption<U8> with an inner value `;
value`
*/
static U8(value?: number | null): MoveOption<U8> {
return new MoveOption<U8>(value !== null && value !== undefined ? new U8(value) : undefined);
}

/**
* Factory method to generate a MoveOption<U16> from a `number` or `undefined`.
* Factory method to generate a MoveOption<U16> from a `;
number` or `;
undefined`.
*
* @example
* MoveOption.U16(1).isSome() === true;
* MoveOption.U16().isSome() === false;
* MoveOption.U16(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<U16> with an inner value `value`
* @returns a MoveOption<U16> with an inner value `;
value`
*/
static U16(value?: number | null): MoveOption<U16> {
return new MoveOption<U16>(value !== null && value !== undefined ? new U16(value) : undefined);
}

/**
* Factory method to generate a MoveOption<U32> from a `number` or `undefined`.
* Factory method to generate a MoveOption<U32> from a `;
number` or `;
undefined`.
*
* @example
* MoveOption.U32(1).isSome() === true;
* MoveOption.U32().isSome() === false;
* MoveOption.U32(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<U32> with an inner value `value`
* @returns a MoveOption<U32> with an inner value `;
value`
*/
static U32(value?: number | null): MoveOption<U32> {
return new MoveOption<U32>(value !== null && value !== undefined ? new U32(value) : undefined);
}

/**
* Factory method to generate a MoveOption<U64> from a `number` or a `bigint` or `undefined`.
* Factory method to generate a MoveOption<U64> from a `;
number` or a `;
bigint` or `;
undefined`.
*
* @example
* MoveOption.U64(1).isSome() === true;
* MoveOption.U64().isSome() === false;
* MoveOption.U64(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<U64> with an inner value `value`
* @returns a MoveOption<U64> with an inner value `;
value`
*/
static U64(value?: AnyNumber | null): MoveOption<U64> {
return new MoveOption<U64>(value !== null && value !== undefined ? new U64(value) : undefined);
}

/**
* Factory method to generate a MoveOption<U128> from a `number` or a `bigint` or `undefined`.
* Factory method to generate a MoveOption<U128> from a `;
number` or a `;
bigint` or `;
undefined`.
*
* @example
* MoveOption.U128(1).isSome() === true;
* MoveOption.U128().isSome() === false;
* MoveOption.U128(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<U128> with an inner value `value`
* @returns a MoveOption<U128> with an inner value `;
value`
*/
static U128(value?: AnyNumber | null): MoveOption<U128> {
return new MoveOption<U128>(value !== null && value !== undefined ? new U128(value) : undefined);
}

/**
* Factory method to generate a MoveOption<U256> from a `number` or a `bigint` or `undefined`.
* Factory method to generate a MoveOption<U256> from a `;
number` or a `;
bigint` or `;
undefined`.
*
* @example
* MoveOption.U256(1).isSome() === true;
* MoveOption.U256().isSome() === false;
* MoveOption.U256(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<U256> with an inner value `value`
* @returns a MoveOption<U256> with an inner value `;
value`
*/
static U256(value?: AnyNumber | null): MoveOption<U256> {
return new MoveOption<U256>(value !== null && value !== undefined ? new U256(value) : undefined);
}

/**
* Factory method to generate a MoveOption<Bool> from a `boolean` or `undefined`.
* Factory method to generate a MoveOption<Bool> from a `;
boolean` or `;
undefined`.
*
* @example
* MoveOption.Bool(true).isSome() === true;
* MoveOption.Bool().isSome() === false;
* MoveOption.Bool(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<Bool> with an inner value `value`
* @returns a MoveOption<Bool> with an inner value `;
value`
*/
static Bool(value?: boolean | null): MoveOption<Bool> {
return new MoveOption<Bool>(value !== null && value !== undefined ? new Bool(value) : undefined);
}

/**
* Factory method to generate a MoveOption<MoveString> from a `string` or `undefined`.
* Factory method to generate a MoveOption<MoveString> from a `;
string` or `;
undefined`.
*
* @example
* MoveOption.MoveString("hello").isSome() === true;
* MoveOption.MoveString("").isSome() === true;
* MoveOption.MoveString().isSome() === false;
* MoveOption.MoveString(undefined).isSome() === false;
* @params value: the value used to fill the MoveOption. If `value` is undefined
* @params value: the value used to fill the MoveOption. If `;
value` is undefined
* the resulting MoveOption's .isSome() method will return false.
* @returns a MoveOption<MoveString> with an inner value `value`
* @returns a MoveOption<MoveString> with an inner value `;
value`
*/
static MoveString(value?: string | null): MoveOption<MoveString> {
return new MoveOption<MoveString>(value !== null && value !== undefined ? new MoveString(value) : undefined);
Expand All @@ -439,4 +484,12 @@ export class MoveOption<T extends Serializable & EntryFunctionArgument>
const vector = MoveVector.deserialize(deserializer, cls);
return new MoveOption(vector.values[0]);
}

toString(): string {
if (this.value === undefined) {
return "Option(None)";
}
return `;
Option(Some(${this.value}))`;
}
}
4 changes: 4 additions & 0 deletions src/bcs/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ export class Serializer {
item.serialize(this);
});
}

toString(): string {
return `Serializer(offset: ${this.offset} buffer: ${this.buffer})`;
}
}

export function ensureBoolean(value: unknown): asserts value is boolean {
Expand Down
4 changes: 4 additions & 0 deletions src/core/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,8 @@ export class Account {
const rawMessage = Hex.fromHexInput(message).toUint8Array();
return this.publicKey.verifySignature({ message: rawMessage, signature });
}

toString(): string {
return `Account(address:${this.accountAddress}, scheme: ${this.signingScheme}, publicKey: ${this.publicKey})`;
}
}
Loading
Loading