-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Description
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:
- The central object needs to be defined within the same module, or
- 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?