Skip to content

Commit

Permalink
Return new server coin too
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Sep 2, 2024
1 parent b73237f commit 207d186
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export declare function selectCoins(allCoins: Array<Coin>, totalAmount: bigint):
* @param {BigInt} offset - The offset to add.
*/
export declare function morphLauncherId(launcherId: Buffer, offset: bigint): Buffer
/** The new server coin and coin spends to create it. */
export interface NewServerCoin {
serverCoin: ServerCoin
coinSpends: Array<CoinSpend>
}
/**
* Creates a new mirror coin with the given URLs.
*
Expand All @@ -203,7 +208,7 @@ export declare function morphLauncherId(launcherId: Buffer, offset: bigint): Buf
* @param {BigInt} amount - The amount to use for the created coin.
* @param {BigInt} fee - The fee to use for the transaction.
*/
export declare function createServerCoin(syntheticKey: Buffer, selectedCoins: Array<Coin>, hint: Buffer, uris: Array<string>, amount: bigint, fee: bigint): Array<CoinSpend>
export declare function createServerCoin(syntheticKey: Buffer, selectedCoins: Array<Coin>, hint: Buffer, uris: Array<string>, amount: bigint, fee: bigint): NewServerCoin
/**
* Spends the mirror coins to make them unusable in the future.
*
Expand Down
23 changes: 17 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use chia_wallet_sdk::{
Peer as RustPeer, MAINNET_CONSTANTS, TESTNET11_CONSTANTS,
};
use conversions::{ConversionError, FromJs, ToJs};
use js::{Coin, CoinSpend, CoinState, EveProof, Proof};
use js::{Coin, CoinSpend, CoinState, EveProof, Proof, ServerCoin};
use napi::bindgen_prelude::*;
use napi::Result;
use std::{net::SocketAddr, sync::Arc};
Expand Down Expand Up @@ -703,6 +703,13 @@ pub fn morph_launcher_id(launcher_id: Buffer, offset: BigInt) -> napi::Result<Bu
.to_js()
}

/// The new server coin and coin spends to create it.
#[napi(object)]
pub struct NewServerCoin {
pub server_coin: ServerCoin,
pub coin_spends: Vec<CoinSpend>,
}

/// Creates a new mirror coin with the given URLs.
///
/// @param {Buffer} syntheticKey - The synthetic key used by the wallet.
Expand All @@ -719,8 +726,8 @@ pub fn create_server_coin(
uris: Vec<String>,
amount: BigInt,
fee: BigInt,
) -> napi::Result<Vec<CoinSpend>> {
let coin = wallet::create_server_coin(
) -> napi::Result<NewServerCoin> {
let (coin_spends, server_coin) = wallet::create_server_coin(
RustPublicKey::from_js(synthetic_key)?,
selected_coins
.into_iter()
Expand All @@ -733,9 +740,13 @@ pub fn create_server_coin(
)
.map_err(js::err)?;

coin.into_iter()
.map(|c| c.to_js())
.collect::<Result<Vec<CoinSpend>>>()
Ok(NewServerCoin {
coin_spends: coin_spends
.into_iter()
.map(|c| c.to_js())
.collect::<Result<Vec<CoinSpend>>>()?,
server_coin: server_coin.to_js()?,
})
}

/// Spends the mirror coins to make them unusable in the future.
Expand Down
20 changes: 15 additions & 5 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ pub fn create_server_coin(
uris: Vec<String>,
amount: u64,
fee: u64,
) -> Result<Vec<CoinSpend>, WalletError> {
let change_puzzle_hash = StandardArgs::curry_tree_hash(synthetic_key).into();
) -> Result<(Vec<CoinSpend>, ServerCoin), WalletError> {
let puzzle_hash = StandardArgs::curry_tree_hash(synthetic_key).into();

let mut memos = Vec::with_capacity(uris.len() + 1);
memos.push(hint.to_vec().into());

for url in uris {
for url in &uris {
memos.push(url.as_bytes().into());
}

Expand All @@ -219,10 +219,20 @@ pub fn create_server_coin(
&selected_coins,
conditions,
(amount + fee).try_into().unwrap(),
change_puzzle_hash,
puzzle_hash,
)?;

Ok(ctx.take())
let server_coin = ServerCoin {
coin: Coin::new(
selected_coins[0].coin_id(),
MirrorArgs::curry_tree_hash().into(),
amount,
),
p2_puzzle_hash: puzzle_hash,
memo_urls: uris,
};

Ok((ctx.take(), server_coin))
}

pub async fn spend_server_coins(
Expand Down

0 comments on commit 207d186

Please sign in to comment.