|
| 1 | +--- |
| 2 | +title: Delayed Transfer |
| 3 | +--- |
| 4 | + |
| 5 | +<Callout type="warn"> |
| 6 | +The example code snippets used in this guide are experimental and have not been audited. They simply help exemplify usage of the OpenZeppelin Sui Package. |
| 7 | +</Callout> |
| 8 | + |
| 9 | +The `delayed_transfer` module provides an ownership-transfer wrapper that enforces a configurable minimum delay before a privileged object can transfer or unwrap. |
| 10 | + |
| 11 | +## When to use it |
| 12 | + |
| 13 | +Use `delayed_transfer` when: |
| 14 | + |
| 15 | +- Your protocol requires on-chain lead time before authority changes. |
| 16 | +- Users, DAOs, or monitoring systems need a window to detect and respond. |
| 17 | +- The delay should be a reliable, inspectable commitment visible to anyone. |
| 18 | + |
| 19 | +## Import |
| 20 | + |
| 21 | +```move |
| 22 | +use openzeppelin_access::delayed_transfer; |
| 23 | +``` |
| 24 | + |
| 25 | +## Step 1: Wrap with a delay |
| 26 | + |
| 27 | +```move |
| 28 | +module my_sui_app::treasury; |
| 29 | +
|
| 30 | +use openzeppelin_access::delayed_transfer; |
| 31 | +
|
| 32 | +public struct TreasuryCap has key, store { id: UID } |
| 33 | +
|
| 34 | +const MIN_DELAY_MS: u64 = 86_400_000; // 24 hours |
| 35 | +
|
| 36 | +/// Creates the wrapper and transfers it to ctx.sender() internally. |
| 37 | +public fun wrap_treasury_cap(cap: TreasuryCap, ctx: &mut TxContext) { |
| 38 | + delayed_transfer::wrap(cap, MIN_DELAY_MS, ctx.sender(), ctx); |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +`wrap` creates a `DelayedTransferWrapper<TreasuryCap>`, stores the capability inside it as a dynamic object field, and transfers the wrapper to the specified `recipient`. Unlike `two_step_transfer::wrap`, which returns the wrapper, `delayed_transfer::wrap` handles the transfer internally and has no return value. |
| 43 | + |
| 44 | +## Step 2: Schedule a transfer |
| 45 | + |
| 46 | +```move |
| 47 | +/// Called by the current wrapper owner. |
| 48 | +wrapper.schedule_transfer(new_owner_address, &clock, ctx); |
| 49 | +/// Emits TransferScheduled with execute_after_ms = clock.timestamp_ms() + min_delay_ms |
| 50 | +``` |
| 51 | + |
| 52 | +The `Clock` object is Sui's shared on-chain clock. The deadline is computed as `clock.timestamp_ms() + min_delay_ms` and stored in the wrapper. Only one action can be pending at a time; scheduling a second without canceling the first aborts with `ETransferAlreadyScheduled`. |
| 53 | + |
| 54 | +During the delay window, the `TransferScheduled` event is visible on-chain. Monitoring systems, governance dashboards, or individual users watching the chain can detect the pending transfer and take action before it executes. |
| 55 | + |
| 56 | +<Callout type="warn"> |
| 57 | +The `recipient` in `schedule_transfer` must be a wallet address, not an object ID. If the wrapper is transferred to an object via transfer-to-object (TTO), both the wrapper and the capability inside it become permanently locked. The `delayed_transfer` module does not implement a `Receiving`-based retrieval mechanism, so there is no way to borrow, unwrap, or further transfer a wrapper that has been sent to an object. |
| 58 | +</Callout> |
| 59 | + |
| 60 | +## Step 3: Wait, then execute |
| 61 | + |
| 62 | +```move |
| 63 | +/// Callable after the delay window has passed. |
| 64 | +wrapper.execute_transfer(&clock, ctx); |
| 65 | +/// Emits OwnershipTransferred. Consumes the wrapper and delivers it to the recipient. |
| 66 | +``` |
| 67 | + |
| 68 | +`execute_transfer` consumes the wrapper by value. After this call, the wrapper has been transferred to the scheduled recipient and no longer exists in the caller's scope. Calling it before `execute_after_ms` aborts with `EDelayNotElapsed`. |
| 69 | + |
| 70 | +## Scheduling an unwrap |
| 71 | + |
| 72 | +The same delay enforcement applies to recovering the raw capability: |
| 73 | + |
| 74 | +```move |
| 75 | +/// Schedule the unwrap. |
| 76 | +wrapper.schedule_unwrap(&clock, ctx); |
| 77 | +/// Emits UnwrapScheduled. |
| 78 | +
|
| 79 | +/// After the delay has elapsed, execute the unwrap. |
| 80 | +let treasury_cap = wrapper.unwrap(&clock, ctx); |
| 81 | +/// Emits UnwrapExecuted. |
| 82 | +``` |
| 83 | + |
| 84 | +## Canceling |
| 85 | + |
| 86 | +The owner can cancel a pending action at any time before execution: |
| 87 | + |
| 88 | +```move |
| 89 | +wrapper.cancel_schedule(); |
| 90 | +``` |
| 91 | + |
| 92 | +This clears the pending slot immediately, allowing a new action to be scheduled. |
| 93 | + |
| 94 | +## Borrowing without unwrapping |
| 95 | + |
| 96 | +The module provides three ways to use the wrapped capability without changing ownership: |
| 97 | + |
| 98 | +```move |
| 99 | +let cap_ref = wrapper.borrow(); |
| 100 | +let cap_mut = wrapper.borrow_mut(); |
| 101 | +let (cap, borrow_token) = wrapper.borrow_val(); |
| 102 | +wrapper.return_val(cap, borrow_token); |
| 103 | +``` |
| 104 | + |
| 105 | +`borrow_val` uses a hot-potato guard, so the value must be returned to the same wrapper before the transaction ends. |
| 106 | + |
| 107 | +## API Reference |
| 108 | + |
| 109 | +For function-level signatures and error codes, see the [Access API reference](/contracts-sui/1.x/api/access#delayed_transfer). |
0 commit comments