Skip to main content

dig_options/
transfer.rs

1//! Transfer an option — move the option singleton to a new owner.
2//!
3//! [`transfer`] spends the option singleton through the current owner's p2 layer and recreates
4//! it at `new_owner_puzzle_hash` (hinted, so the new owner's wallet discovers it). Only the
5//! option ticket moves; the locked underlying and its terms are untouched. The returned
6//! [`OptionSpend`] carries the transferred option as its `created` handle so the caller can
7//! immediately track — and later exercise or claw back — the option in its new-owner state.
8
9use chia_protocol::Bytes32;
10use chia_wallet_sdk::driver::SpendContext;
11use chia_wallet_sdk::types::Conditions;
12
13use crate::error::{Error, Result};
14use crate::types::{CreatedOption, OptionSpend, Owner};
15
16/// Build the unsigned coin spend that TRANSFERS `created`'s option singleton to
17/// `new_owner_puzzle_hash`, authorized by its current `owner`.
18///
19/// Spends the option singleton through `owner`'s p2 layer and recreates it (same launcher id,
20/// same underlying, same amount) at `new_owner_puzzle_hash` with a hint so the recipient's
21/// wallet discovers it. The locked underlying coin and the option's terms are unchanged — only
22/// the ticket's `p2_puzzle_hash` moves.
23///
24/// A [`Owner::Standard`] `owner` whose puzzle hash does not match the option's current
25/// `p2_puzzle_hash` is rejected up front; a [`Owner::Custom`] owner cannot be checked here and
26/// relies on the consensus to reject a wrong-party spend.
27///
28/// **Pure: does NOT sign or broadcast.** Returns [`OptionSpend`] with `created: Some(..)` — the
29/// option in its NEW-owner state (its coin now lives at `new_owner_puzzle_hash`), so the caller
30/// can chain further transfers or an exercise/clawback against the transferred singleton once
31/// this spend is confirmed.
32pub fn transfer(
33    ctx: &mut SpendContext,
34    owner: &Owner,
35    created: &CreatedOption,
36    new_owner_puzzle_hash: Bytes32,
37) -> Result<OptionSpend> {
38    if let Some(puzzle_hash) = owner.standard_puzzle_hash() {
39        if puzzle_hash != created.option.info.p2_puzzle_hash {
40            return Err(Error::invalid(
41                "transfer owner does not match the option's current owner puzzle hash",
42            ));
43        }
44    }
45
46    // `OptionContract` is `Copy`; `transfer` consumes it and returns the recreated child at the
47    // new p2 puzzle hash (hinted for wallet discovery via the SDK). Only the singleton moves —
48    // the underlying stays locked under the same terms.
49    let transferred =
50        created
51            .option
52            .transfer(ctx, owner, new_owner_puzzle_hash, Conditions::new())?;
53
54    Ok(OptionSpend {
55        coin_spends: ctx.take(),
56        created: Some(CreatedOption {
57            option: transferred,
58            underlying: created.underlying,
59            underlying_coin: created.underlying_coin,
60        }),
61    })
62}