Skip to content

Add initial ability to make offers #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::*;
24 changes: 24 additions & 0 deletions crates/sage-api/src/requests/make_offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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,
pub fee: Amount,
}

#[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::*;
73 changes: 73 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,73 @@
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;
let mut parent = None;

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

if child.spent_height.is_some() {
parent = Some(child);
current_id = child.coin.coin_id();
sleep(Duration::from_secs(1)).await;
continue;
}

let parent = parent.expect("parent not found");

let (parent_puzzle_reveal, parent_solution) = timeout(
Duration::from_secs(15),
peer.fetch_puzzle_solution(
parent.coin.coin_id(),
parent.spent_height.ok_or(WalletError::PeerMisbehaved)?,
),
)
.await??;

let mut allocator = Allocator::new();
let parent_puzzle = parent_puzzle_reveal.to_clvm(&mut allocator)?;
let parent_puzzle = Puzzle::parse(&allocator, parent_puzzle);
let parent_solution = parent_solution.to_clvm(&mut allocator)?;

if let Some(nft) = Nft::<HashedPtr>::parse_child(
&mut allocator,
parent.coin,
parent_puzzle,
parent_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;
}

break;
}

Ok(offer_details)
}
Loading