use chia_protocol::{Bytes32, Coin, Program};
use chia_wallet_sdk::driver::{OptionContract, Puzzle, SpendContext};
use crate::error::Result;
#[derive(Clone, Debug)]
pub struct ParsedOption {
pub option: OptionContract,
pub launcher_id: Bytes32,
pub coin_id: Bytes32,
pub underlying_coin_id: Bytes32,
pub underlying_delegated_puzzle_hash: Bytes32,
pub p2_puzzle_hash: Bytes32,
}
impl ParsedOption {
fn from_option(option: OptionContract) -> Self {
Self {
launcher_id: option.info.launcher_id,
coin_id: option.coin.coin_id(),
underlying_coin_id: option.info.underlying_coin_id,
underlying_delegated_puzzle_hash: option.info.underlying_delegated_puzzle_hash,
p2_puzzle_hash: option.info.p2_puzzle_hash,
option,
}
}
}
pub fn parse(
ctx: &mut SpendContext,
coin: Coin,
puzzle_reveal: &Program,
solution: &Program,
) -> Result<Option<ParsedOption>> {
let puzzle_ptr = ctx.alloc(puzzle_reveal)?;
let puzzle = Puzzle::parse(ctx, puzzle_ptr);
let solution = ctx.alloc(solution)?;
let parsed = OptionContract::parse(ctx, coin, puzzle, solution)?;
Ok(parsed.map(|(option, _p2_puzzle, _p2_solution)| ParsedOption::from_option(option)))
}
pub fn parse_child(
ctx: &mut SpendContext,
parent_coin: Coin,
parent_puzzle_reveal: &Program,
parent_solution: &Program,
) -> Result<Option<ParsedOption>> {
let puzzle_ptr = ctx.alloc(parent_puzzle_reveal)?;
let puzzle = Puzzle::parse(ctx, puzzle_ptr);
let solution = ctx.alloc(parent_solution)?;
let child = OptionContract::parse_child(ctx, parent_coin, puzzle, solution)?;
Ok(child.map(ParsedOption::from_option))
}