Skip to content
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

WIP options #154

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
332 changes: 185 additions & 147 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ specta-typescript = "0.0.7"
tauri-specta = "2.0.0-rc.20"

# Chia
chia = { git = "https://github.com/Chia-Network/chia_rs", rev = "cb4f521fe32675a0238453a7c850083405f2952a", version = "0.16.0" }
chia = "0.17.0"
clvmr = "0.10.0"
chia-wallet-sdk = { features = ["rustls"], git = "https://github.com/xch-dev/chia-wallet-sdk", rev = "1a2d2320050263f7f36cbf33a1424e6185d906f5" }
chia-wallet-sdk = { features = ["rustls", "offers"], path = "../wallet-sdk" }
bip39 = "2.0.0"
bech32 = "0.9.1"

Expand Down
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,13 +2,15 @@ mod actions;
mod data;
mod keys;
mod offers;
mod options;
mod settings;
mod transactions;

pub use actions::*;
pub use data::*;
pub use keys::*;
pub use offers::*;
pub use options::*;
pub use settings::*;
pub use transactions::*;

Expand Down
23 changes: 23 additions & 0 deletions crates/sage-api/src/requests/options.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, CoinSpendJson, TransactionSummary};

use super::Assets;

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct MintOption {
pub requested_assets: Assets,
pub offered_assets: Assets,
pub fee: Amount,
pub expires_at_second: u64,
pub did_id: String,
#[serde(default)]
pub auto_submit: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct MintOptionResponse {
pub summary: TransactionSummary,
pub coin_spends: Vec<CoinSpendJson>,
}
23 changes: 12 additions & 11 deletions crates/sage-wallet/src/child_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use chia::{
protocol::{Bytes32, Coin, Program},
puzzles::{nft::NftMetadata, singleton::SINGLETON_LAUNCHER_PUZZLE_HASH, LineageProof, Proof},
};
use chia_wallet_sdk::{run_puzzle, Cat, Condition, Did, DidInfo, HashedPtr, Nft, NftInfo, Puzzle};
use chia_wallet_sdk::{
run_puzzle, Cat, Condition, Did, DidInfo, HashedPtr, Memos, Nft, NftInfo, Puzzle,
};
use clvmr::{Allocator, NodePtr};
use tracing::{debug_span, warn};

Expand Down Expand Up @@ -77,23 +79,22 @@ impl ChildKind {
return Ok(Self::Launcher);
}

let Some(mut create_coin) = conditions
let Some(create_coin) = conditions
.into_iter()
.filter_map(Condition::into_create_coin)
.find(|cond| {
cond.puzzle_hash == coin.puzzle_hash
&& cond.amount == coin.amount
&& !cond.memos.is_empty()
&& cond.memos[0].len() == 32
})
.find(|cond| cond.puzzle_hash == coin.puzzle_hash && cond.amount == coin.amount)
else {
return Ok(Self::Unknown { hint: None });
};

let hint = Bytes32::try_from(create_coin.memos.remove(0).into_inner())
.expect("the hint is always 32 bytes, as checked above");
let hint = if let Some(memos) = create_coin.memos {
let memos = Memos::<(Bytes32, NodePtr)>::from_clvm(allocator, memos.value).ok();
memos.map(|memos| memos.value.0)
} else {
None
};

let unknown = Self::Unknown { hint: Some(hint) };
let unknown = Self::Unknown { hint };

match Cat::parse_children(allocator, parent_coin, parent_puzzle, parent_solution) {
// If there was an error parsing the CAT, we can exit early.
Expand Down
2 changes: 2 additions & 0 deletions crates/sage-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ mod did_assign;
mod dids;
mod nfts;
mod offer;
mod option;
mod p2_coin_management;
mod p2_send;
mod p2_spends;
mod signing;

pub use nfts::WalletNftMint;
pub use offer::*;
pub use option::*;

#[derive(Debug)]
pub struct Wallet {
Expand Down
86 changes: 45 additions & 41 deletions crates/sage-wallet/src/wallet/cat_coin_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,35 @@ impl Wallet {
}

if fee_change > 0 {
fee_conditions = fee_conditions.create_coin(p2_puzzle_hash, fee_change, Vec::new());
fee_conditions = fee_conditions.create_coin(p2_puzzle_hash, fee_change, None);
}

let mut ctx = SpendContext::new();

self.spend_p2_coins(&mut ctx, fee_coins, fee_conditions)
.await?;

self.spend_cat_coins(
&mut ctx,
cats.into_iter().enumerate().map(|(i, cat)| {
if i == 0 {
(
cat,
Conditions::new().create_coin(
p2_puzzle_hash,
cat_total.try_into().expect("output amount overflow"),
vec![p2_puzzle_hash.into()],
),
)
} else {
(cat, Conditions::new())
}
}),
)
.await?;
let mut cat_spends = Vec::new();

let hint = ctx.hint(p2_puzzle_hash)?;

for (i, cat) in cats.into_iter().enumerate() {
cat_spends.push(if i == 0 {
(
cat,
Conditions::new().create_coin(
p2_puzzle_hash,
cat_total.try_into().expect("output amount overflow"),
Some(hint),
),
)
} else {
(cat, Conditions::new())
});
}

self.spend_cat_coins(&mut ctx, cat_spends.into_iter())
.await?;

Ok(ctx.take())
}
Expand Down Expand Up @@ -104,39 +107,40 @@ impl Wallet {
}

if fee_change > 0 {
fee_conditions = fee_conditions.create_coin(derivations[0], fee_change, Vec::new());
fee_conditions = fee_conditions.create_coin(derivations[0], fee_change, None);
}

self.spend_p2_coins(&mut ctx, fee_coins, fee_conditions)
.await?;

self.spend_cat_coins(
&mut ctx,
cats.into_iter().map(|cat| {
let mut conditions = Conditions::new();
let mut cat_spends = Vec::new();

for cat in cats {
let mut conditions = Conditions::new();

for &derivation in &derivations {
if remaining_count == 0 {
break;
}
for &derivation in &derivations {
if remaining_count == 0 {
break;
}

let amount: u64 = (max_individual_amount as u128)
.min(remaining_amount)
.try_into()
.expect("output amount overflow");
let amount: u64 = (max_individual_amount as u128)
.min(remaining_amount)
.try_into()
.expect("output amount overflow");

remaining_amount -= amount as u128;
remaining_amount -= amount as u128;

conditions =
conditions.create_coin(derivation, amount, vec![derivation.into()]);
let hint = ctx.hint(derivation)?;
conditions = conditions.create_coin(derivation, amount, Some(hint));

remaining_count -= 1;
}
remaining_count -= 1;
}

(cat, conditions)
}),
)
.await?;
cat_spends.push((cat, conditions));
}

self.spend_cat_coins(&mut ctx, cat_spends.into_iter())
.await?;

Ok(ctx.take())
}
Expand Down
29 changes: 12 additions & 17 deletions crates/sage-wallet/src/wallet/cats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ impl Wallet {

let mut ctx = SpendContext::new();

let eve_conditions = Conditions::new().create_coin(
p2_puzzle_hash,
amount,
vec![p2_puzzle_hash.to_vec().into()],
);
let hint = ctx.hint(p2_puzzle_hash)?;

let eve_conditions = Conditions::new().create_coin(p2_puzzle_hash, amount, Some(hint));

let (mut conditions, eve) = match multi_issuance_key {
Some(pk) => {
Expand All @@ -49,7 +47,7 @@ impl Wallet {
}

if change > 0 {
conditions = conditions.create_coin(p2_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(p2_puzzle_hash, change, None);
}

self.spend_p2_coins(&mut ctx, coins, conditions).await?;
Expand Down Expand Up @@ -95,7 +93,7 @@ impl Wallet {
conditions = conditions.reserve_fee(fee);

if fee_change > 0 {
conditions = conditions.create_coin(change_puzzle_hash, fee_change, Vec::new());
conditions = conditions.create_coin(change_puzzle_hash, fee_change, None);
}
}

Expand All @@ -106,25 +104,22 @@ impl Wallet {
.await?;
}

let hint = ctx.hint(puzzle_hash)?;
let change_hint = ctx.hint(change_puzzle_hash)?;

self.spend_cat_coins(
&mut ctx,
cats.into_iter().enumerate().map(|(i, cat)| {
if i != 0 {
return (cat, Conditions::new());
}

let mut conditions = mem::take(&mut conditions).create_coin(
puzzle_hash,
amount,
vec![puzzle_hash.into()],
);
let mut conditions =
mem::take(&mut conditions).create_coin(puzzle_hash, amount, Some(hint));

if cat_change > 0 {
conditions = conditions.create_coin(
change_puzzle_hash,
cat_change,
vec![change_puzzle_hash.into()],
);
conditions =
conditions.create_coin(change_puzzle_hash, cat_change, Some(change_hint));
}

(cat, conditions)
Expand Down
2 changes: 1 addition & 1 deletion crates/sage-wallet/src/wallet/did_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Wallet {
.reserve_fee(fee);

if change > 0 {
conditions = conditions.create_coin(change_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(change_puzzle_hash, change, None);
}

if let Some(did_coin_id) = did_coin_id {
Expand Down
4 changes: 2 additions & 2 deletions crates/sage-wallet/src/wallet/dids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Wallet {
}

if change > 0 {
conditions = conditions.create_coin(p2_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(p2_puzzle_hash, change, None);
}

self.spend_p2_coins(&mut ctx, coins, conditions).await?;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Wallet {
.reserve_fee(fee);

if change > 0 {
conditions = conditions.create_coin(p2_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(p2_puzzle_hash, change, None);
}

self.spend_p2_coins(&mut ctx, coins, conditions).await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/sage-wallet/src/wallet/nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Wallet {
}

if change > 0 {
conditions = conditions.create_coin(p2_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(p2_puzzle_hash, change, None);
}

self.spend_p2_coins(&mut ctx, coins, conditions).await?;
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Wallet {
.reserve_fee(fee);

if change > 0 {
conditions = conditions.create_coin(change_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(change_puzzle_hash, change, None);
}

self.spend_p2_coins(&mut ctx, coins, conditions).await?;
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Wallet {
.reserve_fee(fee);

if change > 0 {
conditions = conditions.create_coin(p2_puzzle_hash, change, Vec::new());
conditions = conditions.create_coin(p2_puzzle_hash, change, None);
}

self.spend_p2_coins(&mut ctx, coins, conditions).await?;
Expand Down
Loading
Loading