chia_sdk_driver/layers/
standard_layer.rsuse chia_bls::PublicKey;
use chia_protocol::Coin;
use chia_puzzles::standard::{StandardArgs, StandardSolution, STANDARD_PUZZLE_HASH};
use chia_sdk_types::Conditions;
use clvm_traits::{clvm_quote, FromClvm};
use clvm_utils::{CurriedProgram, ToTreeHash, TreeHash};
use clvmr::{Allocator, NodePtr};
use crate::{DriverError, Layer, Puzzle, Spend, SpendContext, SpendWithConditions};
pub type P2DelegatedOrHiddenLayer = StandardLayer;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StandardLayer {
pub synthetic_key: PublicKey,
}
impl StandardLayer {
pub fn new(synthetic_key: PublicKey) -> Self {
Self { synthetic_key }
}
pub fn spend(
&self,
ctx: &mut SpendContext,
coin: Coin,
conditions: Conditions,
) -> Result<(), DriverError> {
let spend = self.spend_with_conditions(ctx, conditions)?;
ctx.spend(coin, spend)
}
pub fn delegated_inner_spend(
&self,
ctx: &mut SpendContext,
spend: Spend,
) -> Result<Spend, DriverError> {
self.construct_spend(
ctx,
StandardSolution {
original_public_key: None,
delegated_puzzle: spend.puzzle,
solution: spend.solution,
},
)
}
}
impl Layer for StandardLayer {
type Solution = StandardSolution<NodePtr, NodePtr>;
fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = CurriedProgram {
program: ctx.standard_puzzle()?,
args: StandardArgs::new(self.synthetic_key),
};
ctx.alloc(&curried)
}
fn construct_solution(
&self,
ctx: &mut SpendContext,
solution: Self::Solution,
) -> Result<NodePtr, DriverError> {
ctx.alloc(&solution)
}
fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
let Some(puzzle) = puzzle.as_curried() else {
return Ok(None);
};
if puzzle.mod_hash != STANDARD_PUZZLE_HASH {
return Ok(None);
}
let args = StandardArgs::from_clvm(allocator, puzzle.args)?;
Ok(Some(Self {
synthetic_key: args.synthetic_key,
}))
}
fn parse_solution(
allocator: &Allocator,
solution: NodePtr,
) -> Result<Self::Solution, DriverError> {
Ok(StandardSolution::from_clvm(allocator, solution)?)
}
}
impl SpendWithConditions for StandardLayer {
fn spend_with_conditions(
&self,
ctx: &mut SpendContext,
conditions: Conditions,
) -> Result<Spend, DriverError> {
let delegated_puzzle = ctx.alloc(&clvm_quote!(conditions))?;
self.construct_spend(
ctx,
StandardSolution {
original_public_key: None,
delegated_puzzle,
solution: NodePtr::NIL,
},
)
}
}
impl ToTreeHash for StandardLayer {
fn tree_hash(&self) -> TreeHash {
StandardArgs::curry_tree_hash(self.synthetic_key)
}
}
#[cfg(test)]
mod tests {
use chia_sdk_test::Simulator;
use super::*;
#[test]
fn test_flash_loan() -> anyhow::Result<()> {
let mut sim = Simulator::new();
let ctx = &mut SpendContext::new();
let (sk, pk, puzzle_hash, coin) = sim.new_p2(1)?;
let p2 = StandardLayer::new(pk);
p2.spend(
ctx,
coin,
Conditions::new().create_coin(puzzle_hash, u64::MAX, Vec::new()),
)?;
p2.spend(
ctx,
Coin::new(coin.coin_id(), puzzle_hash, u64::MAX),
Conditions::new().create_coin(puzzle_hash, 1, Vec::new()),
)?;
sim.spend_coins(ctx.take(), &[sk])?;
Ok(())
}
}