Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Aug 8, 2022
1 parent 3f750fb commit a9bf9e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/BalanceTx/BalanceTx.purs
Expand Up @@ -36,8 +36,7 @@ module BalanceTx
, GetPublicKeyTransactionInputError(CannotConvertScriptOutputToTxInput)
, GetWalletAddressError(CouldNotGetWalletAddress)
, GetWalletCollateralError
( CannotRequestCollateralForWallet
, CouldNotGetCollateral
( CouldNotGetCollateral
, WalletNotSpecified
)
, TxInputLockedError(TxInputLockedError)
Expand Down Expand Up @@ -168,8 +167,7 @@ instance Show GetWalletAddressError where
show = genericShow

data GetWalletCollateralError
= CannotRequestCollateralForWallet ImpossibleError
| CouldNotGetCollateral
= CouldNotGetCollateral
| WalletNotSpecified

derive instance Generic GetWalletCollateralError _
Expand Down Expand Up @@ -445,28 +443,10 @@ _redeemersTxIns = lens' \(UnattachedUnbalancedTx rec@{ redeemersTxIns }) ->

setCollateral
:: Transaction
-> Utxos
-> QueryM (Either GetWalletCollateralError Transaction)
setCollateral transaction utxos = runExceptT do
wallet <-
ExceptT $ asks (_.runtime >>> _.wallet) <#> note WalletNotSpecified
collateral <- ExceptT $ selectCollateral wallet
-- TODO: https://github.com/Plutonomicon/cardano-transaction-lib/pull/707
setCollateral transaction = runExceptT do
collateral <- ExceptT $ getWalletCollateral <#> note CouldNotGetCollateral
pure $ addTxCollateral collateral transaction
where
selectCollateral
:: Wallet
-> QueryM (Either GetWalletCollateralError (Array TransactionUnspentOutput))
selectCollateral (KeyWallet keyWallet) =
-- TODO: Combine with getWalletCollateral and supply with fee estimate
-- https://github.com/Plutonomicon/cardano-transaction-lib/issues/510
(unwrap keyWallet).selectCollateral <$> filterLockedUtxos utxos
<#> note CouldNotGetCollateral <<< map Array.singleton
selectCollateral wallet
| isJust (cip30Wallet wallet) =
getWalletCollateral <#> note CouldNotGetCollateral
| otherwise =
pure $ Left $ CannotRequestCollateralForWallet Impossible

addTxCollateral :: Array TransactionUnspentOutput -> Transaction -> Transaction
addTxCollateral utxos transaction =
Expand Down Expand Up @@ -507,7 +487,7 @@ balanceTxWithAddress
if Array.null (unattachedTx ^. _redeemersTxIns)
-- Don't set collateral if tx doesn't contain phase-2 scripts:
then pure unbalancedTx'
else ExceptT $ setCollateral unbalancedTx' utxos
else ExceptT $ setCollateral unbalancedTx'
<#> lmap GetWalletCollateralError'

let
Expand Down
1 change: 1 addition & 0 deletions src/QueryM/Utxos.purs
Expand Up @@ -153,6 +153,7 @@ getWalletCollateral = do
networkId <- asks $ _.config >>> _.networkId
addr <- liftAff $ (unwrap kw).address networkId
utxos <- utxosAt addr <#> map unwrap >>> fromMaybe Map.empty
>>= filterLockedUtxos
pure $ Array.singleton <$> (unwrap kw).selectCollateral utxos
for_ mbCollateralUTxOs \collateralUTxOs -> do
pparams <- asks $ _.runtime >>> _.pparams
Expand Down

0 comments on commit a9bf9e6

Please sign in to comment.