Skip to content

Commit

Permalink
Merge pull request #42 from Rigidity/cat-melt-example
Browse files Browse the repository at this point in the history
Add CAT melt test
  • Loading branch information
Rigidity authored Jun 6, 2024
2 parents 84ee963 + 97db980 commit 56ec32a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
46 changes: 46 additions & 0 deletions crates/chia-sdk-driver/src/puzzles/cat/cat_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,50 @@ mod tests {

Ok(())
}

#[tokio::test]
async fn test_cat_melt() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;

let sk = sim.secret_key().await?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 10000).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let (issue_cat, issuance) = IssueCat::new(coin.coin_id())
.create_hinted_coin(ctx, puzzle_hash, 10000, puzzle_hash)?
.multi_issuance(ctx, pk, 10000)?;

StandardSpend::new()
.chain(issue_cat)
.finish(ctx, coin, pk)?;

let inner_spend = StandardSpend::new()
.create_hinted_coin(ctx, puzzle_hash, 7000, puzzle_hash)?
.run_multi_issuance_tail(ctx, pk)?
.inner_spend(ctx, pk)?;

let cat_puzzle_hash =
CatArgs::curry_tree_hash(issuance.asset_id, puzzle_hash.into()).into();
let cat_coin = Coin::new(issuance.eve_coin.coin_id(), cat_puzzle_hash, 10000);

CatSpend::new(issuance.asset_id)
.spend(cat_coin, inner_spend, issuance.lineage_proof, -3000)
.finish(ctx)?;

test_transaction(
&peer,
ctx.take_spends(),
&[sk],
sim.config().genesis_challenge,
)
.await;

Ok(())
}
}
52 changes: 51 additions & 1 deletion crates/chia-sdk-driver/src/spend_builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use chia_bls::PublicKey;
use chia_protocol::{Bytes, Bytes32};
use chia_puzzles::cat::{EverythingWithSignatureTailArgs, GenesisByCoinIdTailArgs};
use chia_sdk_types::conditions::{
AssertBeforeHeightAbsolute, AssertBeforeHeightRelative, AssertBeforeSecondsAbsolute,
AssertBeforeSecondsRelative, AssertCoinAnnouncement, AssertHeightAbsolute,
AssertHeightRelative, AssertPuzzleAnnouncement, AssertSecondsAbsolute, AssertSecondsRelative,
CreateCoin, CreateCoinAnnouncement, CreatePuzzleAnnouncement, ReserveFee,
CreateCoin, CreateCoinAnnouncement, CreatePuzzleAnnouncement, ReserveFee, RunTail,
};
use clvm_traits::ToClvm;
use clvm_utils::CurriedProgram;
use clvmr::{
sha2::{Digest, Sha256},
NodePtr,
Expand Down Expand Up @@ -162,6 +166,52 @@ pub trait P2Spend: Sized {
) -> Result<Self, SpendError> {
Ok(self.raw_condition(ctx.alloc(&AssertHeightAbsolute { height })?))
}

fn run_single_issuance_tail(
self,
ctx: &mut SpendContext<'_>,
genesis_coin_id: Bytes32,
) -> Result<Self, SpendError> {
let genesis_by_coin_id_tail_puzzle = ctx.genesis_by_coin_id_tail_puzzle()?;

self.run_custom_tail(
ctx,
CurriedProgram {
program: genesis_by_coin_id_tail_puzzle,
args: GenesisByCoinIdTailArgs::new(genesis_coin_id),
},
(),
)
}

fn run_multi_issuance_tail(
self,
ctx: &mut SpendContext<'_>,
issuance_key: PublicKey,
) -> Result<Self, SpendError> {
let everything_with_signature_tail_puzzle = ctx.everything_with_signature_tail_puzzle()?;

self.run_custom_tail(
ctx,
CurriedProgram {
program: everything_with_signature_tail_puzzle,
args: EverythingWithSignatureTailArgs::new(issuance_key),
},
(),
)
}

fn run_custom_tail(
self,
ctx: &mut SpendContext<'_>,
tail_program: impl ToClvm<NodePtr>,
tail_solution: impl ToClvm<NodePtr>,
) -> Result<Self, SpendError> {
Ok(self.raw_condition(ctx.alloc(&RunTail {
program: tail_program,
solution: tail_solution,
})?))
}
}

#[derive(Debug, Default, Clone)]
Expand Down

0 comments on commit 56ec32a

Please sign in to comment.