Skip to content

Commit

Permalink
Make inner functions of constructTransaction more concise.
Browse files Browse the repository at this point in the history
Use `LambdaCase` instead of repeating patterns.
Use `Coin.fromQuantity` instead of `fromIntegral`.
  • Loading branch information
jonathanknowles committed Nov 2, 2022
1 parent 4dbad4f commit e308cb1
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2468,40 +2468,55 @@ constructTransaction

singleton x = [x]

toUnsignedTxChange initialOuts (WalletOutput (ApiWalletOutput (ApiT addr, _) (Quantity c) (ApiT tmap) derPath)) =
let txchange = TxChange addr (Coin $ fromIntegral c) tmap (NE.map getApiT derPath)
txout = TxOut addr (TokenBundle (Coin $ fromIntegral c) tmap)
in if txout `L.notElem` initialOuts then
Just txchange
else
Nothing
toUnsignedTxChange _initialOuts (ExternalOutput _) =
Nothing

toUnsignedTxOut initialOuts (WalletOutput (ApiWalletOutput (ApiT addr, _) (Quantity c) (ApiT tmap) _)) =
let txout = TxOut addr (TokenBundle (Coin $ fromIntegral c) tmap)
in if txout `L.elem` initialOuts then
Just txout
else
Nothing
toUnsignedTxOut initialOuts (ExternalOutput (AddressAmount (ApiT addr, _) (Quantity c) (ApiT tmap))) =
let txout = TxOut addr (TokenBundle (Coin $ fromIntegral c) tmap)
in if txout `L.elem` initialOuts then
Just txout
else
Nothing

toUsignedTxWdrl p (ApiWithdrawalGeneral (ApiT rewardAcc,_) (Quantity amt) Our) =
Just (rewardAcc, Coin $ fromIntegral amt, p)
toUsignedTxWdrl _p (ApiWithdrawalGeneral _ _ External) =
Nothing
toUnsignedTxChange initialOuts = \case
WalletOutput o ->
let address = getApiT (fst (o ^. #address))
derivationPath = fmap getApiT (o ^. #derivationPath)
coin = Coin.fromQuantity (o ^. #amount)
assets = getApiT (o ^. #assets)
txChange = TxChange address coin assets derivationPath
txOut = TxOut address (TokenBundle coin assets)
in
if txOut `L.notElem` initialOuts then Just txChange else Nothing
ExternalOutput _ ->
Nothing

toUnsignedTxInp (WalletInput (ApiWalletInput (ApiT txid) ix (ApiT addr,_) derPath (Quantity c) (ApiT tmap) )) =
Just ( TxIn txid ix
, TxOut addr (TokenBundle (Coin $ fromIntegral c) tmap)
, NE.map getApiT derPath)
toUnsignedTxInp (ExternalInput _) =
Nothing
toUnsignedTxOut initialOuts = \case
WalletOutput o ->
let address = getApiT (fst (o ^. #address))
coin = Coin.fromQuantity (o ^. #amount)
assets = getApiT (o ^. #assets)
txOut = TxOut address (TokenBundle coin assets)
in
if txOut `L.elem` initialOuts then Just txOut else Nothing
ExternalOutput o ->
let address = getApiT (fst (o ^. #address))
coin = Coin.fromQuantity (o ^. #amount)
assets = getApiT (o ^. #assets)
txOut = TxOut address (TokenBundle coin assets)
in
if txOut `L.elem` initialOuts then Just txOut else Nothing

toUsignedTxWdrl p = \case
ApiWithdrawalGeneral (ApiT rewardAcc, _) amount Our ->
Just (rewardAcc, Coin.fromQuantity amount, p)
ApiWithdrawalGeneral _ _ External ->
Nothing

toUnsignedTxInp = \case
WalletInput i ->
let txId = getApiT (i ^. #id)
index = i ^. #index
address = getApiT (fst (i ^. #address))
derivationPath = fmap getApiT (i ^. #derivationPath)
coin = Coin.fromQuantity (i ^. #amount)
assets = getApiT (i ^. #assets)
txIn = TxIn txId index
txOut = TxOut address (TokenBundle coin assets)
in
Just (txIn, txOut, derivationPath)
ExternalInput _ ->
Nothing

unsignedTx path initialOuts decodedTx = UnsignedTx
{ unsignedCollateral =
Expand Down

0 comments on commit e308cb1

Please sign in to comment.