Skip to content

Commit

Permalink
Simple offer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 14, 2024
1 parent e07901e commit a4f686d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/sage-wallet/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration};

use chia::{
bls::{master_to_wallet_unhardened_intermediate, DerivableKey, SecretKey},
protocol::{Bytes32, CoinSpend},
protocol::{Bytes32, CoinSpend, SpendBundle},
puzzles::{standard::StandardArgs, DeriveSynthetic},
};
use chia_wallet_sdk::{
Expand Down Expand Up @@ -117,6 +117,12 @@ impl TestWallet {
)
.await?;

self.push_bundle(spend_bundle).await?;

Ok(())
}

pub async fn push_bundle(&self, spend_bundle: SpendBundle) -> anyhow::Result<()> {
let mut tx = self.wallet.db.tx().await?;

let subscriptions = insert_transaction(
Expand Down
63 changes: 63 additions & 0 deletions crates/sage-wallet/src/wallet/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,66 @@ pub use parse_offer::*;
pub use royalties::*;
pub use take_offer::*;
pub use unlock_assets::*;

#[cfg(test)]
mod tests {
use chia_wallet_sdk::{AggSigConstants, TESTNET11_CONSTANTS};
use indexmap::{indexmap, IndexMap};
use sqlx::SqlitePool;
use test_log::test;

use crate::{MakerSide, SyncEvent, TakerSide, TestWallet};

#[test(sqlx::test)]
async fn test_offer_xch_for_cat(pool: SqlitePool) -> anyhow::Result<()> {
let mut test = TestWallet::new(pool, 2000).await?;

// Issue CAT
let (coin_spends, asset_id) = test.wallet.issue_cat(1000, 0, None, false, true).await?;
test.transact(coin_spends).await?;
test.consume_until(SyncEvent::CoinState).await;

// Create offer
let offer = test
.wallet
.make_offer(
MakerSide {
xch: 750,
cats: IndexMap::new(),
nfts: Vec::new(),
fee: 250,
},
TakerSide {
xch: 0,
cats: indexmap! { asset_id => 1000 },
nfts: IndexMap::new(),
},
false,
true,
)
.await?;
let offer = test
.wallet
.sign_make_offer(
offer,
&AggSigConstants::new(TESTNET11_CONSTANTS.agg_sig_me_additional_data),
test.master_sk.clone(),
)
.await?;

// Take offer
let offer = test.wallet.take_offer(offer, 0, false, true).await?;
let spend_bundle = test
.wallet
.sign_take_offer(
offer,
&AggSigConstants::new(TESTNET11_CONSTANTS.agg_sig_me_additional_data),
test.master_sk.clone(),
)
.await?;
test.push_bundle(spend_bundle).await?;
test.consume_until(SyncEvent::CoinState).await;

Ok(())
}
}

0 comments on commit a4f686d

Please sign in to comment.