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

Allow multiple outputs in send_xch #8

Merged
merged 1 commit into from
Sep 4, 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
12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,20 @@ export interface UnspentCoinsResponse {
* @returns {Vec<Coin>} Array of selected coins.
*/
export declare function selectCoins(allCoins: Array<Coin>, totalAmount: bigint): Array<Coin>
/** An output puzzle hash and amount. */
export interface Output {
puzzleHash: Buffer
amount: bigint
}
/**
* Sends XCH to a given puzzle hash.
* Sends XCH to a given set of puzzle hashes.
*
* @param {Buffer} syntheticKey - The synthetic key used by the wallet.
* @param {Vec<Coin>} selectedCoins - Coins to be spent, as retured by `select_coins`.
* @param {Buffer} puzzleHash - The puzzle hash to send to.
* @param {BigInt} amount - The amount to use for the created coin.
* @param {Vec<Output>} outputs - The output amounts to create.
* @param {BigInt} fee - The fee to use for the transaction.
*/
export declare function sendXch(syntheticKey: Buffer, selectedCoins: Array<Coin>, puzzleHash: Buffer, amount: bigint, fee: bigint): Array<CoinSpend>
export declare function sendXch(syntheticKey: Buffer, selectedCoins: Array<Coin>, outputs: Array<Output>, fee: bigint): Array<CoinSpend>
/**
* Adds an offset to a launcher id to make it deterministically unique from the original.
*
Expand Down
27 changes: 20 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,29 +726,42 @@ pub fn select_coins(all_coins: Vec<Coin>, total_amount: BigInt) -> napi::Result<
.collect::<Result<Vec<Coin>>>()
}

/// Sends XCH to a given puzzle hash.
/// An output puzzle hash and amount.
#[napi(object)]
pub struct Output {
pub puzzle_hash: Buffer,
pub amount: BigInt,
}

/// Sends XCH to a given set of puzzle hashes.
///
/// @param {Buffer} syntheticKey - The synthetic key used by the wallet.
/// @param {Vec<Coin>} selectedCoins - Coins to be spent, as retured by `select_coins`.
/// @param {Buffer} puzzleHash - The puzzle hash to send to.
/// @param {BigInt} amount - The amount to use for the created coin.
/// @param {Vec<Output>} outputs - The output amounts to create.
/// @param {BigInt} fee - The fee to use for the transaction.
#[napi]
pub fn send_xch(
synthetic_key: Buffer,
selected_coins: Vec<Coin>,
puzzle_hash: Buffer,
amount: BigInt,
outputs: Vec<Output>,
fee: BigInt,
) -> napi::Result<Vec<CoinSpend>> {
let mut items = Vec::new();

for output in outputs {
items.push((
RustBytes32::from_js(output.puzzle_hash)?,
u64::from_js(output.amount)?,
));
}

let coin_spends = wallet::send_xch(
RustPublicKey::from_js(synthetic_key)?,
&selected_coins
.into_iter()
.map(RustCoin::from_js)
.collect::<Result<Vec<RustCoin>>>()?,
RustBytes32::from_js(puzzle_hash)?,
u64::from_js(amount)?,
&items,
u64::from_js(fee)?,
)
.map_err(js::err)?;
Expand Down
17 changes: 11 additions & 6 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,25 @@ fn spend_coins_together(
pub fn send_xch(
synthetic_key: PublicKey,
coins: &[Coin],
puzzle_hash: Bytes32,
amount: u64,
outputs: &[(Bytes32, u64)],
fee: u64,
) -> Result<Vec<CoinSpend>, WalletError> {
let mut ctx = SpendContext::new();

let mut conditions = Conditions::new().reserve_fee(fee);
let mut total_amount = fee;

for output in outputs {
conditions = conditions.create_coin(output.0, output.1, Vec::new());
total_amount += output.1;
}

spend_coins_together(
&mut ctx,
synthetic_key,
coins,
Conditions::new()
.create_coin(puzzle_hash, amount, Vec::new())
.reserve_fee(fee),
(amount + fee).try_into().unwrap(),
conditions,
total_amount.try_into().unwrap(),
StandardArgs::curry_tree_hash(synthetic_key).into(),
)?;

Expand Down