Skip to content

Commit

Permalink
CLI: Add transaction create --ttl option
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Oct 26, 2020
1 parent 3b72db3 commit fcefefb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/cli/cardano-wallet-cli.cabal
Expand Up @@ -47,6 +47,7 @@ library
, servant-client-core
, text
, text-class
, time
, optparse-applicative
hs-source-dirs:
src
Expand Down
23 changes: 19 additions & 4 deletions lib/cli/src/Cardano/CLI.hs
Expand Up @@ -206,6 +206,8 @@ import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..), showT )
import Data.Text.Read
( decimal )
import Data.Time.Clock
( NominalDiffTime )
import Data.Void
( Void )
import Fmt
Expand Down Expand Up @@ -707,6 +709,7 @@ data TransactionCreateArgs t = TransactionCreateArgs
, _id :: WalletId
, _payments :: NonEmpty Text
, _metadata :: ApiTxMetadata
, _timeToLive :: Maybe NominalDiffTime
}

cmdTransactionCreate
Expand All @@ -723,8 +726,9 @@ cmdTransactionCreate isShelley mkTxClient mkWalletClient =
<$> portOption
<*> walletIdArgument
<*> fmap NE.fromList (some paymentOption)
<*> if isShelley then metadataOption else pure (ApiTxMetadata Nothing)
exec (TransactionCreateArgs wPort wId wAddressAmounts md) = do
<*> (if isShelley then metadataOption else pure (ApiTxMetadata Nothing))
<*> (if isShelley then timeToLiveOption else pure Nothing)
exec (TransactionCreateArgs wPort wId wAddressAmounts md ttl) = do
wPayments <- either (fail . getTextDecodingError) pure $
traverse (fromText @(AddressAmount Text)) wAddressAmounts
res <- sendRequest wPort $ getWallet mkWalletClient $ ApiT wId
Expand All @@ -738,6 +742,7 @@ cmdTransactionCreate isShelley mkTxClient mkWalletClient =
[ "payments" .= wPayments
, "passphrase" .= ApiT wPwd
, "metadata" .= md
, "time_to_live" .= ttl
]
)
Left _ ->
Expand All @@ -757,8 +762,9 @@ cmdTransactionFees isShelley mkTxClient mkWalletClient =
<$> portOption
<*> walletIdArgument
<*> fmap NE.fromList (some paymentOption)
<*> if isShelley then metadataOption else pure (ApiTxMetadata Nothing)
exec (TransactionCreateArgs wPort wId wAddressAmounts md) = do
<*> (if isShelley then metadataOption else pure (ApiTxMetadata Nothing))
<*> (if isShelley then timeToLiveOption else pure Nothing)
exec (TransactionCreateArgs wPort wId wAddressAmounts md ttl) = do
wPayments <- either (fail . getTextDecodingError) pure $
traverse (fromText @(AddressAmount Text)) wAddressAmounts
res <- sendRequest wPort $ getWallet mkWalletClient $ ApiT wId
Expand All @@ -770,6 +776,7 @@ cmdTransactionFees isShelley mkTxClient mkWalletClient =
(Aeson.object
[ "payments" .= wPayments
, "metadata" .= md
, "time_to_live" .= ttl
])
Left _ ->
handleResponse Aeson.encodePretty res
Expand Down Expand Up @@ -1391,6 +1398,14 @@ metadataOption = option txMetadataReader $ mempty
txMetadataReader :: ReadM ApiTxMetadata
txMetadataReader = eitherReader (Aeson.eitherDecode' . BL8.pack)

timeToLiveOption :: Parser (Maybe NominalDiffTime)
timeToLiveOption = optional $ option diffTime $ mempty
<> long "ttl"
<> metavar "SECONDS"
<> help "Time-to-live value in seconds. Default is 2 hours."
where
diffTime = fromIntegral <$> (auto :: ReadM Int)

-- | <address=ADDRESS>
addressIdArgument :: Parser Text
addressIdArgument = argumentT $ mempty
Expand Down
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-wallet-cli.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fcefefb

Please sign in to comment.