Skip to content

Commit

Permalink
Add assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 14, 2024
1 parent 74d833a commit 9bea0fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
5 changes: 3 additions & 2 deletions crates/sage-wallet/src/wallet/offer/make_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ impl Wallet {

let trade_prices = calculate_trade_prices(&taker_amounts, maker.nfts.len())?;

// TODO: Assert royalties. This is IMPORTANT.
let (assertions, builder) = builder.finish();
let extra_conditions = Conditions::new().extend(assertions);
let extra_conditions = Conditions::new()
.extend(assertions)
.extend(maker_royalties.assertions());

// Spend the assets.
self.lock_assets(
Expand Down
45 changes: 43 additions & 2 deletions crates/sage-wallet/src/wallet/offer/royalties.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use chia::{
protocol::Bytes32,
puzzles::{cat::CatArgs, offer::SETTLEMENT_PAYMENTS_PUZZLE_HASH},
puzzles::{
cat::CatArgs,
offer::{NotarizedPayment, Payment, SETTLEMENT_PAYMENTS_PUZZLE_HASH},
},
};
use chia_wallet_sdk::{
calculate_nft_royalty, calculate_nft_trace_price, payment_assertion, AssertPuzzleAnnouncement,
TradePrice,
};
use chia_wallet_sdk::{calculate_nft_royalty, calculate_nft_trace_price, TradePrice};
use indexmap::IndexMap;

use crate::WalletError;
Expand Down Expand Up @@ -38,6 +44,28 @@ impl Royalties {

amounts
}

pub fn assertions(&self) -> Vec<AssertPuzzleAnnouncement> {
let mut assertions = Vec::new();

for royalty in &self.xch {
assertions.push(payment_assertion(
SETTLEMENT_PAYMENTS_PUZZLE_HASH.into(),
&royalty.notarized_payment(),
));
}

for (&asset_id, royalties) in &self.cats {
for royalty in royalties {
assertions.push(payment_assertion(
CatArgs::curry_tree_hash(asset_id, SETTLEMENT_PAYMENTS_PUZZLE_HASH).into(),
&royalty.notarized_payment(),
));
}
}

assertions
}
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -47,6 +75,19 @@ pub struct RoyaltyPayment {
pub amount: u64,
}

impl RoyaltyPayment {
pub fn notarized_payment(&self) -> NotarizedPayment {
NotarizedPayment {
nonce: self.nft_id,
payments: vec![Payment::with_memos(
self.p2_puzzle_hash,
self.amount,
vec![self.p2_puzzle_hash.into()],
)],
}
}
}

#[derive(Debug, Clone, Copy)]
pub struct NftRoyaltyInfo {
pub launcher_id: Bytes32,
Expand Down
5 changes: 3 additions & 2 deletions crates/sage-wallet/src/wallet/offer/take_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ impl Wallet {
// Calculate trade prices for the maker side.
let trade_prices = calculate_trade_prices(&maker_amounts, requested_payments.nfts.len())?;

// TODO: Assert royalties. This is IMPORTANT.
let extra_conditions = Conditions::new().extend(assertions);
let extra_conditions = Conditions::new()
.extend(assertions)
.extend(taker_royalties.assertions());

// Spend the assets.
let payment_coins = self
Expand Down

0 comments on commit 9bea0fd

Please sign in to comment.