Skip to content

[move-std] Relaxing UID creation for sui::derived_object::claim #24248

@admin-aftermath

Description

@admin-aftermath

Overview

Currently object creation enforces the UID is fresh. Is you provide a non-fresh UID, you receive the following error:

error[Sui E01001]: invalid object construction
    ┌─ ./sources/<module>.move:<line>:<column>
    │  
232 │       let object = Object {
    │ ╭──────────────────^
233 │ │         id: uid,
    │ │         --  ------------------------- Non fresh UID from this position
    │ │         │    
    │ │         The UID must come directly from `sui::object::new`, or `sui::derived_object::claim`. For tests, it can come from `sui::test_scenario::new_object`
234 │ │     };
    │ ╰─────^ Invalid object creation without a newly created UID.

This means that in order to use a central object to derive all UIDs for the objects created within your package either:

  1. The central object needs to be defined within the same module, or
  2. The central object needs to expose a public(package) fun borrow_mut_id(object: &mut <Object>): &mut UID.

(1) starts to break down as you encapsulate logic / objects across many modules and (2) is not a great pattern to allow. Also both of these methods break down if you want to derive a UID using an object from another package, a case I have run into a few times already with packages that separate logic across many packages, let alone modules.

It would be great if we can define a wrapper around sui::derived_object::claim for a locally defined object with a signature like:

// or public(package)
public fun claim<K: copy + drop + store>(
    object: &mut Object,
    key: K,
): UID {
    sui::derived:object(object.id, key)
}

And then from other modules use this object to derive UIDs through:

let local_object = LocalObject {
    id: object.claim(LocalKey {})
};

Currently the above is not possible due to the error[Sui E01001]: invalid object construction error.

Would it be possible to alleviate the above restriction to allow wrapping sui::derived_object::claim into a function while the returned UID is deemed fresh?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions