Skip to content

Commit

Permalink
wallet-api: Add payToScripts
Browse files Browse the repository at this point in the history
* We need a way to pay to multiple script outputs at the same time
  • Loading branch information
j-mueller committed Jan 2, 2019
1 parent 0d9dedf commit 6762a1f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions wallet-api/src/Wallet/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Wallet.API(
payToScript_,
payToPublicKey,
payToPublicKey_,
payToScripts,
payToScripts_,
collectFromScript,
collectFromScriptTxn,
ownPubKeyTxOut,
Expand Down Expand Up @@ -275,13 +277,25 @@ throwOtherError = throwError . OtherError
createPayment :: (Functor m, WalletAPI m) => Value -> m (Set.Set TxIn')
createPayment vl = fst <$> createPaymentWithChange vl

-- | Transfer some funds to a number of script addresses, returning the
-- transaction that was submitted.
payToScripts :: (Monad m, WalletAPI m) => [(Address', Value, DataScript)] -> m Tx
payToScripts ins = do
let
totalVal = getSum $ foldMap (Sum . view _2) ins
otherOutputs = fmap (\(addr, vl, ds) -> TxOut addr vl (PayToScript ds)) ins
(i, ownChange) <- createPaymentWithChange totalVal
signAndSubmit i (maybe otherOutputs (:otherOutputs) ownChange)

-- | Transfer some funds to a number of script addresses, returning the
-- transaction that was submitted.
payToScripts_ :: (Monad m, WalletAPI m) => [(Address', Value, DataScript)] -> m ()
payToScripts_ = void . payToScripts

-- | Transfer some funds to an address locked by a script, returning the
-- transaction that was submitted.
payToScript :: (Monad m, WalletAPI m) => Address' -> Value -> DataScript -> m Tx
payToScript addr v ds = do
(i, own) <- createPaymentWithChange v
let other = TxOut addr v (PayToScript ds)
signAndSubmit i (other : maybeToList own)
payToScript addr v ds = payToScripts [(addr, v, ds)]

-- | Transfer some funds to an address locked by a script.
payToScript_ :: (Monad m, WalletAPI m) => Address' -> Value -> DataScript -> m ()
Expand Down

0 comments on commit 6762a1f

Please sign in to comment.