Skip to content

Commit

Permalink
Merge pull request #8 from Datalayer-Storage/multi-output
Browse files Browse the repository at this point in the history
Allow multiple outputs in send_xch
  • Loading branch information
MichaelTaylor3D authored Sep 4, 2024
2 parents ec58d84 + 5f51079 commit 8148596
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
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

0 comments on commit 8148596

Please sign in to comment.