Skip to content

Commit

Permalink
Initial offers
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 11, 2024
1 parent 908b7f8 commit 5660514
Show file tree
Hide file tree
Showing 23 changed files with 1,014 additions and 462 deletions.
673 changes: 319 additions & 354 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ bigdecimal = "0.4.5"
base64 = "0.22.1"

# Tauri
tauri = "=2.0.0-rc.8"
tauri-plugin-clipboard-manager = "=2.0.0-rc.3"
tauri-plugin-shell = "=2.0.0-rc.3"
tauri-build = "=2.0.0-rc.7"
tauri = "2.1.0"
tauri-plugin-clipboard-manager = "2.0.2"
tauri-plugin-shell = "2.0.2"
tauri-build = "2.0.3"

# Specta
specta = "2.0.0-rc.20"
specta-typescript = "0.0.7"
tauri-specta = "=2.0.0-rc.18"
tauri-specta = "2.0.0-rc.20"

# Chia
chia = "0.15.0"
Expand Down Expand Up @@ -105,3 +105,6 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-appender = "0.2.3"
log = "0.4.22"

[patch.crates-io]
chia-wallet-sdk = { git = "https://github.com/xch-dev/chia-wallet-sdk", rev = "543743574fc15662395dc5d15b1cbf250783f802" }
2 changes: 2 additions & 0 deletions crates/sage-api/src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mod bulk_mint_nfts;
mod get_collection_nfts;
mod get_nft_collections;
mod get_nfts;
mod make_offer;

pub use bulk_mint_nfts::*;
pub use get_collection_nfts::*;
pub use get_nft_collections::*;
pub use get_nfts::*;
pub use make_offer::*;
23 changes: 23 additions & 0 deletions crates/sage-api/src/requests/make_offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use serde::{Deserialize, Serialize};
use specta::Type;

use crate::Amount;

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct MakeOffer {
pub requested_assets: Assets,
pub offered_assets: Assets,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct Assets {
pub xch: Amount,
pub cats: Vec<CatAmount>,
pub nfts: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct CatAmount {
pub asset_id: String,
pub amount: Amount,
}
6 changes: 6 additions & 0 deletions crates/sage-wallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ pub enum WalletError {

#[error("Missing NFT with id {0}")]
MissingNft(Bytes32),

#[error("Invalid trade price")]
InvalidTradePrice,

#[error("Invalid royalty amount")]
InvalidRoyaltyAmount,
}
4 changes: 2 additions & 2 deletions crates/sage-wallet/src/sync_manager/peer_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl SyncManager {

let mut state = self.state.lock().await;

for (peer, height) in state.peers_with_heights() {
for (existing_peer, height) in state.peers_with_heights() {
if message.height < height.saturating_sub(3) {
debug!(
"Peer {} is behind by more than 3 blocks, disconnecting",
Expand All @@ -243,7 +243,7 @@ impl SyncManager {
return false;
} else if message.height > height.saturating_add(3) {
state.ban(
peer.socket_addr().ip(),
existing_peer.socket_addr().ip(),
Duration::from_secs(900),
"peer is behind",
);
Expand Down
2 changes: 2 additions & 0 deletions crates/sage-wallet/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod fetch_nft_did;
mod fetch_nft_offer_details;
mod fetch_uri;
mod offchain_metadata;

pub use fetch_nft_did::*;
pub use fetch_nft_offer_details::*;
pub use fetch_uri::*;
pub use offchain_metadata::*;
60 changes: 60 additions & 0 deletions crates/sage-wallet/src/utils/fetch_nft_offer_details.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::time::Duration;

use chia::{
clvm_traits::{FromClvm, ToClvm},
protocol::{Bytes32, Program},
};
use chia_wallet_sdk::{HashedPtr, Nft, Puzzle};
use clvmr::Allocator;
use tokio::time::{sleep, timeout};

use crate::{NftOfferDetails, WalletError, WalletPeer};

pub async fn fetch_nft_offer_details(
peer: &WalletPeer,
launcher_id: Bytes32,
) -> Result<Option<NftOfferDetails>, WalletError> {
let mut offer_details = None::<NftOfferDetails>;
let mut current_id = launcher_id;

loop {
let Some(child) =
timeout(Duration::from_secs(5), peer.try_fetch_child(current_id)).await??
else {
break;
};

let spent_height = child.spent_height.ok_or(WalletError::PeerMisbehaved)?;
current_id = child.coin.coin_id();

let (puzzle_reveal, solution) = timeout(
Duration::from_secs(15),
peer.fetch_puzzle_solution(current_id, spent_height),
)
.await??;

let mut allocator = Allocator::new();

let puzzle_reveal = puzzle_reveal.to_clvm(&mut allocator)?;
let puzzle = Puzzle::parse(&allocator, puzzle_reveal);
let solution = solution.to_clvm(&mut allocator)?;

if let Some(nft) =
Nft::<HashedPtr>::parse_child(&mut allocator, child.coin, puzzle, solution)
.ok()
.flatten()
{
offer_details = Some(NftOfferDetails {
metadata: Program::from_clvm(&allocator, nft.info.metadata.ptr())?,
metadata_updater_puzzle_hash: nft.info.metadata_updater_puzzle_hash,
royalty_puzzle_hash: nft.info.royalty_puzzle_hash,
royalty_ten_thousandths: nft.info.royalty_ten_thousandths,
});
break;
}

sleep(Duration::from_secs(1)).await;
}

Ok(offer_details)
}
Loading

0 comments on commit 5660514

Please sign in to comment.