Algorand TypeScript
    Preparing search index...

    Type Alias Box<TValue>

    Creates a Box proxy object offering methods of getting and setting the value stored in a single box.

    Options for creating the Box proxy

    type Box<TValue> = {
        exists: boolean;
        key: bytes;
        length: uint64;
        value: TValue;
        create(options?: { size?: uint64 }): boolean;
        delete(): boolean;
        extract(start: uint64, length: uint64): bytes;
        get(options: { default: TValue }): TValue;
        maybe(): readonly [TValue, boolean];
        replace(start: uint64, value: bytes): void;
        resize(newSize: uint64): void;
        splice(start: uint64, length: uint64, value: bytes): void;
    }

    Type Parameters

    • TValue

      The type of the data stored in the box. This value will be encoded to bytes when stored and decoded on retrieval.

    Index

    Properties

    exists: boolean

    Get a boolean indicating if the box exists or not

    key: bytes

    Get the key used by this box proxy

    length: uint64

    Returns the length of the box, or error if the box does not exist

    value: TValue

    Get or set the value stored in the box

    Get will error if the box does not exist

    Methods

    • Create the box for this proxy with a bzero value.

      • If options.size is specified, the box will be created with that length
      • Otherwise the box will be created with storage size of TValue. Errors if the size of TValue is not fixed

      No op if the box already exists with the same size Errors if the box already exists with a different size. Errors if the specified size is greater than the max box size (32,768)

      Parameters

      • Optionaloptions: { size?: uint64 }

      Returns boolean

      True if the box was created, false if it already existed

    • Delete the box associated with this proxy if it exists.

      Returns boolean

      True if the box existed and was deleted, else false

    • Extract a slice of bytes from the box

      Error if the box does not exist Error if start + length is greater than the box size

      Parameters

      • start: uint64

        The index to start extracting

      • length: uint64

        The number of bytes to extract

      Returns bytes

      The extracted bytes

    • Get the value stored in the box, or return a specified default value if the box does not exist

      Parameters

      • options: { default: TValue }

        Options to specify a default value to be returned if no other value exists

      Returns TValue

      The value if the box exists, else the default value

    • Get the value stored in the box if available, and a boolean indicating if the box exists.

      If the box does not exist, the value returned at position 0 should not be relied on to have a valid value.

      Returns readonly [TValue, boolean]

      A tuple with the first item being the box value, and the second item being a boolean indicating if the box exists.

    • Replace bytes in a box starting at start.

      Error if the box does not exist Error if start + value.length is greater than the box size

      Parameters

      • start: uint64

        The index to start replacing

      • value: bytes

        The value to be written

      Returns void

    • Resize the box to the specified size.

      Adds zero bytes to the end if the new size is larger Removes end bytes if the new size is smaller Error if the box does not exist

      Parameters

      • newSize: uint64

        The new size for the box

      Returns void

    • Splice the specified bytes into the box starting at start, removing length bytes from the existing value and replacing them with value before appending the remainder of the original box value.

      If the resulting byte value is larger than length, bytes will be trimmed from the end If the resulting byte value is smaller than length, zero bytes will be appended to the end Error if the box does not exist

      Parameters

      • start: uint64

        The index to start inserting the value

      • length: uint64

        The number of bytes after start to be omitted

      • value: bytes

        The value to be inserted

      Returns void