@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
448 lines • 15 kB
JavaScript
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
import { MAX_U128_BIG_INT, MAX_U16_NUMBER, MAX_U32_NUMBER, MAX_U64_BIG_INT, MAX_U8_NUMBER, MAX_U256_BIG_INT, MIN_I8_NUMBER, MAX_I8_NUMBER, MIN_I16_NUMBER, MAX_I16_NUMBER, MIN_I32_NUMBER, MAX_I32_NUMBER, MIN_I64_BIG_INT, MAX_I64_BIG_INT, MIN_I128_BIG_INT, MAX_I128_BIG_INT, MIN_I256_BIG_INT, MAX_I256_BIG_INT, } from "../consts.js";
import { Serializable, ensureBoolean, serializeEntryFunctionBytesCompat, validateNumberInRange, } from "../serializer.js";
import { ScriptTransactionArgumentVariants, } from "../../types/index.js";
/**
* Represents a boolean value that can be serialized and deserialized.
* This class extends the Serializable class and provides methods to serialize
* the boolean value for different contexts, such as entry functions and script functions.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class Bool extends Serializable {
value;
/**
* Constructs a new instance with a specified value.
* This ensures that the value is validated to be within the acceptable range.
*
* @param value - The number to be validated and assigned, which must be between 0 and MAX_U256_BIG_INT.
* @group Implementation
* @category BCS
*/
constructor(value) {
super();
/**
* Ensures that the provided value is of type boolean.
* This function throws an error if the value is not a boolean, helping to enforce type safety in your code.
*
* @param value - The value to be checked for boolean type.
* @throws {Error} Throws an error if the value is not a boolean.
* @group Implementation
* @category BCS
*/
ensureBoolean(value);
this.value = value;
}
/**
* Serializes the value using the provided serializer.
* This function is essential for converting the value into a format suitable for transmission or storage.
*
* @param serializer - The serializer instance used to perform the serialization.
* @group Implementation
* @category BCS
*/
serialize(serializer) {
serializer.serializeBool(this.value);
}
/**
* Serializes the current instance for use in an entry function by converting it to a byte sequence.
* This allows the instance to be properly formatted for serialization in transactions.
* Uses serializeAsBytes when available, with a fallback for older Serializer versions.
*
* @param serializer - The serializer instance used to serialize the byte sequence.
* @group Implementation
* @category BCS
*/
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
/**
* Serializes the current instance for use in a script function.
* This allows for the conversion of the instance into a format suitable for transmission or storage.
*
* @param serializer - The serializer used to perform the serialization.
* @group Implementation
* @category BCS
*/
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.Bool);
serializer.serialize(this);
}
/**
* Deserializes a U256 value from the provided deserializer.
*
* @param deserializer - The deserializer instance used to read the U256 data.
* @group Implementation
* @category BCS
*/
deserialize(deserializer) {
return new U256(deserializer.deserializeU256());
}
static deserialize(deserializer) {
return new Bool(deserializer.deserializeBool());
}
}
/**
* Represents an unsigned 8-bit integer (U8) value.
* This class extends the Serializable class and provides methods for serialization and deserialization of U8 values.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class U8 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, 0, MAX_U8_NUMBER);
this.value = value;
}
serialize(serializer) {
serializer.serializeU8(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U8);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new U8(deserializer.deserializeU8());
}
}
/**
* Represents a 16-bit unsigned integer (U16) value.
* This class extends the Serializable class and provides methods for serialization
* and deserialization of the U16 value.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class U16 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, 0, MAX_U16_NUMBER);
this.value = value;
}
serialize(serializer) {
serializer.serializeU16(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U16);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new U16(deserializer.deserializeU16());
}
}
/**
* Represents a 32-bit unsigned integer (U32) that can be serialized and deserialized.
* This class ensures that the value is within the valid range for a U32.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class U32 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, 0, MAX_U32_NUMBER);
this.value = value;
}
serialize(serializer) {
serializer.serializeU32(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U32);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new U32(deserializer.deserializeU32());
}
}
/**
* Represents a 64-bit unsigned integer (U64) and provides methods for serialization.
*
* This class ensures that the value is within the valid range for a U64 and provides
* functionality to serialize the value for various use cases, including entry functions
* and script functions.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class U64 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, BigInt(0), MAX_U64_BIG_INT);
this.value = BigInt(value);
}
serialize(serializer) {
serializer.serializeU64(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U64);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new U64(deserializer.deserializeU64());
}
}
/**
* Represents a 128-bit unsigned integer value.
* This class provides methods for serialization and deserialization
* of U128 values, ensuring that the values are within the valid range.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class U128 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, BigInt(0), MAX_U128_BIG_INT);
this.value = BigInt(value);
}
serialize(serializer) {
serializer.serializeU128(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U128);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new U128(deserializer.deserializeU128());
}
}
/**
* Represents a 256-bit unsigned integer (U256) that extends the Serializable class.
* This class provides methods for serialization and deserialization of U256 values,
* ensuring that the values are within the valid range.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class U256 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, BigInt(0), MAX_U256_BIG_INT);
this.value = BigInt(value);
}
serialize(serializer) {
serializer.serializeU256(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U256);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new U256(deserializer.deserializeU256());
}
}
/**
* Represents an 8-bit signed integer (I8) value.
* This class extends the Serializable class and provides methods for serialization and deserialization of I8 values.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class I8 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, MIN_I8_NUMBER, MAX_I8_NUMBER);
this.value = value;
}
serialize(serializer) {
serializer.serializeI8(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.I8);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new I8(deserializer.deserializeI8());
}
}
/**
* Represents a 16-bit signed integer (I16) value.
* This class extends the Serializable class and provides methods for serialization
* and deserialization of the I16 value.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class I16 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, MIN_I16_NUMBER, MAX_I16_NUMBER);
this.value = value;
}
serialize(serializer) {
serializer.serializeI16(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.I16);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new I16(deserializer.deserializeI16());
}
}
/**
* Represents a 32-bit signed integer (I32) that can be serialized and deserialized.
* This class ensures that the value is within the valid range for an I32.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class I32 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, MIN_I32_NUMBER, MAX_I32_NUMBER);
this.value = value;
}
serialize(serializer) {
serializer.serializeI32(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.I32);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new I32(deserializer.deserializeI32());
}
}
/**
* Represents a 64-bit signed integer (I64) and provides methods for serialization.
*
* This class ensures that the value is within the valid range for an I64 and provides
* functionality to serialize the value for various use cases, including entry functions
* and script functions.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class I64 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, MIN_I64_BIG_INT, MAX_I64_BIG_INT);
this.value = BigInt(value);
}
serialize(serializer) {
serializer.serializeI64(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.I64);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new I64(deserializer.deserializeI64());
}
}
/**
* Represents a 128-bit signed integer value.
* This class provides methods for serialization and deserialization
* of I128 values, ensuring that the values are within the valid range.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class I128 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, MIN_I128_BIG_INT, MAX_I128_BIG_INT);
this.value = BigInt(value);
}
serialize(serializer) {
serializer.serializeI128(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.I128);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new I128(deserializer.deserializeI128());
}
}
/**
* Represents a 256-bit signed integer (I256) that extends the Serializable class.
* This class provides methods for serialization and deserialization of I256 values,
* ensuring that the values are within the valid range.
*
* @extends Serializable
* @group Implementation
* @category BCS
*/
export class I256 extends Serializable {
value;
constructor(value) {
super();
validateNumberInRange(value, MIN_I256_BIG_INT, MAX_I256_BIG_INT);
this.value = BigInt(value);
}
serialize(serializer) {
serializer.serializeI256(this.value);
}
serializeForEntryFunction(serializer) {
serializeEntryFunctionBytesCompat(serializer, this);
}
serializeForScriptFunction(serializer) {
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.I256);
serializer.serialize(this);
}
static deserialize(deserializer) {
return new I256(deserializer.deserializeI256());
}
}
//# sourceMappingURL=movePrimitives.js.map