Skip to content

Commit

Permalink
Adjust code to comply with team coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Oct 20, 2020
1 parent 53d3128 commit b44ab3a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
Expand Up @@ -313,10 +313,12 @@ spec = describe "BYRON_HW_WALLETS" $ do
it "Can get coin selection" $ \ctx -> do
(w, mnemonics) <- fixtureIcarusWalletMws ctx
let pubKey = pubKeyFromMnemonics mnemonics
r <- request @ApiByronWallet ctx (Link.deleteWallet @'Byron w) Default Empty
r <- request
@ApiByronWallet ctx (Link.deleteWallet @'Byron w) Default Empty
expectResponseCode @IO HTTP.status204 r

source <- restoreWalletFromPubKey @ApiByronWallet @'Byron ctx pubKey restoredWalletName
source <- restoreWalletFromPubKey
@ApiByronWallet @'Byron ctx pubKey restoredWalletName
let [addr] = take 1 $ icarusAddresses @n mnemonics

let amount = Quantity minUTxOValue
Expand Down
Expand Up @@ -293,10 +293,12 @@ spec = describe "SHELLEY_HW_WALLETS" $ do
it "Can get coin selection" $ \ctx -> do
(w, mnemonics) <- fixtureWalletWithMnemonics ctx
let pubKey = pubKeyFromMnemonics mnemonics
r <- request @ApiWallet ctx (Link.deleteWallet @'Shelley w) Default Empty
r <- request
@ApiWallet ctx (Link.deleteWallet @'Shelley w) Default Empty
expectResponseCode @IO HTTP.status204 r

source <- restoreWalletFromPubKey @ApiWallet @'Shelley ctx pubKey restoredWalletName
source <- restoreWalletFromPubKey
@ApiWallet @'Shelley ctx pubKey restoredWalletName
target <- emptyWallet ctx
targetAddress : _ <- fmap (view #id) <$> listAddresses @n ctx target

Expand Down
Expand Up @@ -933,10 +933,14 @@ spec = describe "SHELLEY_WALLETS" $ do
)
selectCoins @_ @'Shelley ctx source (payment :| []) >>= flip verify
[ expectResponseCode HTTP.status200
, expectField #inputs (`shouldSatisfy` (not . null))
, expectField #inputs (`shouldSatisfy` all hasValidDerivationPath)
, expectField #outputs (`shouldSatisfy` ((> 1) . length))
, expectField #outputs (`shouldSatisfy` (payment `elem`))
, expectField #inputs
(`shouldSatisfy` (not . null))
, expectField #inputs
(`shouldSatisfy` all hasValidDerivationPath)
, expectField #outputs
(`shouldSatisfy` ((> 1) . length))
, expectField #outputs
(`shouldSatisfy` (payment `elem`))
]

let satisfy = flip shouldSatisfy
Expand Down
23 changes: 13 additions & 10 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -1273,23 +1273,23 @@ selectCoinsForPayment
-> Maybe TxMetadata
-> ExceptT (ErrSelectForPayment e) IO CoinSelection
selectCoinsForPayment ctx wid recipients withdrawal md = do
(utxo, pending, txp, minUtxo) <- withExceptT ErrSelectForPaymentNoSuchWallet $
selectCoinsSetup @ctx @s @k ctx wid
(utxo, pending, txp, minUtxo) <-
withExceptT ErrSelectForPaymentNoSuchWallet $
selectCoinsSetup @ctx @s @k ctx wid

let pendingWithdrawal = Set.lookupMin $ Set.filter hasWithdrawal pending
when (withdrawal /= Quantity 0 && isJust pendingWithdrawal) $ throwE $
ErrSelectForPaymentAlreadyWithdrawing (fromJust pendingWithdrawal)

cs <-
selectCoinsForPaymentFromUTxO @ctx @t @k @e ctx utxo txp minUtxo recipients withdrawal md
cs <- selectCoinsForPaymentFromUTxO
@ctx @t @k @e ctx utxo txp minUtxo recipients withdrawal md
withExceptT ErrSelectForPaymentMinimumUTxOValue $ except $
guardCoinSelection minUtxo cs
pure cs
where
hasWithdrawal :: Tx -> Bool
hasWithdrawal = not . null . withdrawals


-- | Retrieve wallet data which is needed for all types of coin selections.
selectCoinsSetup
:: forall ctx s k.
Expand Down Expand Up @@ -1511,7 +1511,8 @@ estimateFeeForPayment ctx wid recipients withdrawal md = do
(utxo, _, txp, minUtxo) <- withExceptT ErrSelectForPaymentNoSuchWallet $
selectCoinsSetup @ctx @s @k ctx wid

let selectCoins = selectCoinsForPaymentFromUTxO @ctx @t @k @e ctx utxo txp minUtxo recipients withdrawal md
let selectCoins = selectCoinsForPaymentFromUTxO
@ctx @t @k @e ctx utxo txp minUtxo recipients withdrawal md

cs <- selectCoins `catchE` handleNotSuccessfulCoinSelection
withExceptT ErrSelectForPaymentMinimumUTxOValue $ except $
Expand Down Expand Up @@ -1639,14 +1640,16 @@ signTx ctx wid pwd md (UnsignedTx inpsNE outs) = db & \DBLayer{..} -> do
let pwdP = preparePassphrase scheme pwd
nodeTip <- withExceptT ErrSignPaymentNetwork $ currentNodeTip nl
mapExceptT atomically $ do
cp <- withExceptT ErrSignPaymentNoSuchWallet $ withNoSuchWallet wid $
readCheckpoint (PrimaryKey wid)
cp <- withExceptT ErrSignPaymentNoSuchWallet
$ withNoSuchWallet wid
$ readCheckpoint (PrimaryKey wid)

let cs = mempty { inputs = inps, outputs = outs }
let keyFrom = isOwned (getState cp) (xprv, pwdP)
let rewardAcnt = getRawKey $ deriveRewardAccount @k pwdP xprv
(tx, sealedTx, txExp) <- withExceptT ErrSignPaymentMkTx $ ExceptT $
pure $ mkStdTx tl (rewardAcnt, pwdP) keyFrom (nodeTip ^. #slotNo) md cs
(tx, sealedTx, txExp) <- withExceptT ErrSignPaymentMkTx $
ExceptT $ pure $ mkStdTx
tl (rewardAcnt, pwdP) keyFrom (nodeTip ^. #slotNo) md cs

(time, meta) <- liftIO $
mkTxMeta ti (currentTip cp) (getState cp) tx cs txExp
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -1131,7 +1131,8 @@ selectCoins ctx genChange (ApiT wid) body =
liftHandler
$ W.selectCoinsExternal @_ @s @k wrk wid genChange
$ withExceptT ErrSelectCoinsExternalForPayment
$ W.selectCoinsForPayment @_ @s @t @k wrk wid outs withdrawal Nothing
$ W.selectCoinsForPayment
@_ @s @t @k wrk wid outs withdrawal Nothing

selectCoinsForJoin
:: forall ctx e s t n k.
Expand Down
15 changes: 10 additions & 5 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Expand Up @@ -626,11 +626,16 @@ spec = do
it "ApiCoinSelectionInput" $ property $ \x ->
let
x' = ApiCoinSelectionInput
{ id = id (x :: ApiCoinSelectionInput ('Testnet 0))
, index = index (x :: ApiCoinSelectionInput ('Testnet 0))
, address = address (x :: ApiCoinSelectionInput ('Testnet 0))
, amount = amount (x :: ApiCoinSelectionInput ('Testnet 0))
, derivationPath = derivationPath (x :: ApiCoinSelectionInput ('Testnet 0))
{ id = id
(x :: ApiCoinSelectionInput ('Testnet 0))
, index = index
(x :: ApiCoinSelectionInput ('Testnet 0))
, address = address
(x :: ApiCoinSelectionInput ('Testnet 0))
, amount = amount
(x :: ApiCoinSelectionInput ('Testnet 0))
, derivationPath = derivationPath
(x :: ApiCoinSelectionInput ('Testnet 0))
}
in
x' === x .&&. show x' === show x
Expand Down

0 comments on commit b44ab3a

Please sign in to comment.