Skip to content

Commit

Permalink
Add reading and writing to file
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed May 2, 2024
1 parent 948419d commit 0f7fa3e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 57 deletions.
2 changes: 1 addition & 1 deletion concordium-client.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.24
-- see: https://github.com/sol/hpack

name: concordium-client
version: 6.3.0
version: 6.2.1
description: Please see the README on GitHub at <https://github.com/Concordium/concordium-client#readme>
homepage: https://github.com/Concordium/concordium-client#readme
bug-reports: https://github.com/Concordium/concordium-client/issues
Expand Down
28 changes: 23 additions & 5 deletions src/Concordium/Client/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ registerDataParser =
<|> (RegisterRaw <$> strOption (long "raw" <> metavar "FILE" <> help "File with raw bytes to be registered on chain."))

data TransactionCmd
= TransactionSubmit
= TransactionSignAndSubmit
{ tsFile :: !FilePath,
tsInteractionOpts :: !InteractionOpts
}
| TransactionSubmit
{ tsFile :: !FilePath,
tsInteractionOpts :: !InteractionOpts
}
Expand Down Expand Up @@ -432,6 +436,7 @@ data TransactionOpts energyOrMaybe = TransactionOpts

data InteractionOpts = InteractionOpts
{ ioConfirm :: !Bool,
ioSubmit :: !Bool,
ioTail :: !Bool
}
deriving (Show)
Expand Down Expand Up @@ -655,8 +660,8 @@ interactionOptsParser :: Parser InteractionOpts
interactionOptsParser =
InteractionOpts
<$> (not <$> switch (long "no-confirm" <> help "Do not ask for confirmation before proceeding to send the transaction."))
<*> (not <$> switch (long "no-submit" <> help "Do not submit transaction on-chain. Write signed transaction to file instead."))
<*> (not <$> switch (long "no-wait" <> help "Exit right after sending the transaction without waiting for it to be committed and finalized."))

programOptions :: Parser Options
programOptions =
Options
Expand Down Expand Up @@ -700,7 +705,8 @@ transactionCmds =
( info
( TransactionCmd
<$> hsubparser
( transactionSubmitCmd
( transactionSignAndSubmitCmd
<> transactionSubmitCmd
<> transactionStatusCmd
<> transactionSendCcdCmd
<> transactionWithScheduleCmd
Expand All @@ -712,16 +718,28 @@ transactionCmds =
(progDesc "Commands for submitting and inspecting transactions.")
)

transactionSignAndSubmitCmd :: Mod CommandFields TransactionCmd
transactionSignAndSubmitCmd =
command
"sign-and-submit"
( info
( TransactionSignAndSubmit
<$> strArgument (metavar "FILE" <> help "File containing the transaction parameters in JSON format.")
<*> interactionOptsParser
)
(progDesc "Create transaction, sign it, and send it to the node.")
)

transactionSubmitCmd :: Mod CommandFields TransactionCmd
transactionSubmitCmd =
command
"submit"
( info
( TransactionSubmit
<$> strArgument (metavar "FILE" <> help "File containing the transaction parameters in JSON format.")
<$> strArgument (metavar "FILE" <> help "File containing a signed transaction in JSON format.")
<*> interactionOptsParser
)
(progDesc "Parse transaction and send it to the node.")
(progDesc "Parse signed transaction and send it to the node.")
)

transactionDeployCredentialCmd :: Mod CommandFields TransactionCmd
Expand Down
64 changes: 32 additions & 32 deletions src/Concordium/Client/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Data.Bool
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Short as BSS
import Data.Either (isLeft)
import Data.Either (isRight)
import Data.Functor
import Data.List (foldl', intercalate, nub, partition, sortOn)
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -782,68 +782,68 @@ showEvent ::
Maybe String
showEvent verbose ciM = \case
Types.ModuleDeployed ref ->
verboseOrNothing $ printf "module '%s' deployed" (show ref)
verboseOrNothing $ printf "module '%s' deployed." (show ref)
Types.ContractInitialized{..} ->
verboseOrNothing $
[i|initialized contract '#{ecAddress}' using init function '#{ecInitName}' from module '#{ecRef}' |]
<> [i|with #{showCcd ecAmount}\n#{showLoggedEvents ecEvents}|]
<> [i|with #{showCcd ecAmount}.\n#{showLoggedEvents ecEvents}|]
Types.Updated{..} ->
verboseOrNothing $
[i|sent message to function '#{euReceiveName}' with #{showParameter euReceiveName euMessage} and #{showCcd euAmount} |]
<> [i|from #{showAddress euInstigator} to #{showAddress $ Types.AddressContract euAddress}\n|]
<> [i|from #{showAddress euInstigator} to #{showAddress $ Types.AddressContract euAddress}.\n|]
<> [i|#{showLoggedEvents euEvents}|]
Types.Transferred{..} ->
verboseOrNothing $ printf "transferred %s from %s to %s" (showCcd etAmount) (showAddress etFrom) (showAddress etTo)
verboseOrNothing $ printf "transferred %s from %s to %s." (showCcd etAmount) (showAddress etFrom) (showAddress etTo)
Types.AccountCreated addr ->
verboseOrNothing $ printf "account '%s' created" (show addr)
verboseOrNothing $ printf "account '%s' created." (show addr)
Types.CredentialDeployed{..} ->
verboseOrNothing $ printf "credential with registration '%s' deployed onto account '%s'" (show ecdRegId) (show ecdAccount)
verboseOrNothing $ printf "credential with registration '%s' deployed onto account '%s'." (show ecdRegId) (show ecdAccount)
Types.BakerAdded{..} ->
let restakeString :: String = if ebaRestakeEarnings then "Earnings are added to the stake." else "Earnings are not added to the stake."
in verboseOrNothing $ printf "validator %s added, staking %s CCD. %s" (showBaker ebaBakerId ebaAccount) (Types.amountToString ebaStake) restakeString
Types.BakerRemoved{..} ->
verboseOrNothing $ printf "validator %s, removed" (showBaker ebrBakerId ebrAccount)
verboseOrNothing $ printf "validator %s, removed." (showBaker ebrBakerId ebrAccount)
Types.BakerStakeIncreased{..} ->
verboseOrNothing $ printf "validator %s stake increased to %s" (showBaker ebsiBakerId ebsiAccount) (showCcd ebsiNewStake)
verboseOrNothing $ printf "validator %s stake increased to %s." (showBaker ebsiBakerId ebsiAccount) (showCcd ebsiNewStake)
Types.BakerStakeDecreased{..} ->
verboseOrNothing $ printf "validator %s stake decreased to %s" (showBaker ebsiBakerId ebsiAccount) (showCcd ebsiNewStake)
verboseOrNothing $ printf "validator %s stake decreased to %s." (showBaker ebsiBakerId ebsiAccount) (showCcd ebsiNewStake)
Types.BakerSetRestakeEarnings{..} ->
verboseOrNothing $ printf "validator %s restake earnings %s" (showBaker ebsreBakerId ebsreAccount) (if ebsreRestakeEarnings then "set" :: String else "unset")
verboseOrNothing $ printf "validator %s restake earnings %s." (showBaker ebsreBakerId ebsreAccount) (if ebsreRestakeEarnings then "set" :: String else "unset")
Types.BakerKeysUpdated{..} ->
verboseOrNothing $ printf "validator %s keys updated" (showBaker ebkuBakerId ebkuAccount)
verboseOrNothing $ printf "validator %s keys updated." (showBaker ebkuBakerId ebkuAccount)
Types.CredentialsUpdated{..} ->
verboseOrNothing $ [i|credentials on account #{cuAccount} have been updated.\nCredentials #{cuRemovedCredIds} have been removed, and credentials #{cuNewCredIds} have been added.\nThe new account threshold is #{cuNewThreshold}.|]
Types.BakerSetOpenStatus{..} ->
verboseOrNothing $ printf "validator %s open status changed to %s" (showBaker ebsosBakerId ebsosAccount) (show ebsosOpenStatus)
verboseOrNothing $ printf "validator %s open status changed to %s." (showBaker ebsosBakerId ebsosAccount) (show ebsosOpenStatus)
Types.BakerSetMetadataURL{..} ->
verboseOrNothing $ printf "validator %s URL changed to %s" (showBaker ebsmuBakerId ebsmuAccount) (show ebsmuMetadataURL)
verboseOrNothing $ printf "validator %s URL changed to %s." (showBaker ebsmuBakerId ebsmuAccount) (show ebsmuMetadataURL)
Types.BakerSetTransactionFeeCommission{..} ->
verboseOrNothing $ printf "validator %s changed transaction fee commission to %s" (showBaker ebstfcBakerId ebstfcAccount) (show ebstfcTransactionFeeCommission)
verboseOrNothing $ printf "validator %s changed transaction fee commission to %s." (showBaker ebstfcBakerId ebstfcAccount) (show ebstfcTransactionFeeCommission)
Types.BakerSetBakingRewardCommission{..} ->
verboseOrNothing $ printf "validator %s changed block reward commission to %s" (showBaker ebsbrcBakerId ebsbrcAccount) (show ebsbrcBakingRewardCommission)
verboseOrNothing $ printf "validator %s changed block reward commission to %s." (showBaker ebsbrcBakerId ebsbrcAccount) (show ebsbrcBakingRewardCommission)
Types.BakerSetFinalizationRewardCommission{..} ->
verboseOrNothing $ printf "validator %s changed finalization reward commission to %s" (showBaker ebsfrcBakerId ebsfrcAccount) (show ebsfrcFinalizationRewardCommission)
verboseOrNothing $ printf "validator %s changed finalization reward commission to %s." (showBaker ebsfrcBakerId ebsfrcAccount) (show ebsfrcFinalizationRewardCommission)
Types.DelegationStakeIncreased{..} ->
verboseOrNothing $ printf "delegator %s stake increased to %s" (showDelegator edsiDelegatorId edsiAccount) (showCcd edsiNewStake)
verboseOrNothing $ printf "delegator %s stake increased to %s." (showDelegator edsiDelegatorId edsiAccount) (showCcd edsiNewStake)
Types.DelegationStakeDecreased{..} ->
verboseOrNothing $ printf "delegator %s stake decreased to %s" (showDelegator edsdDelegatorId edsdAccount) (showCcd edsdNewStake)
verboseOrNothing $ printf "delegator %s stake decreased to %s." (showDelegator edsdDelegatorId edsdAccount) (showCcd edsdNewStake)
Types.DelegationSetRestakeEarnings{..} ->
verboseOrNothing $ printf "delegator %s restake earnings changed to %s" (showDelegator edsreDelegatorId edsreAccount) (show edsreRestakeEarnings)
verboseOrNothing $ printf "delegator %s restake earnings changed to %s." (showDelegator edsreDelegatorId edsreAccount) (show edsreRestakeEarnings)
Types.DelegationSetDelegationTarget{..} ->
verboseOrNothing $ printf "delegator %s delegation target changed to %s" (showDelegator edsdtDelegatorId edsdtAccount) (showDelegationTarget edsdtDelegationTarget)
verboseOrNothing $ printf "delegator %s delegation target changed to %s." (showDelegator edsdtDelegatorId edsdtAccount) (showDelegationTarget edsdtDelegationTarget)
Types.DelegationAdded{..} ->
verboseOrNothing $ printf "delegator %s added" (showDelegator edaDelegatorId edaAccount)
verboseOrNothing $ printf "delegator %s added." (showDelegator edaDelegatorId edaAccount)
Types.DelegationRemoved{..} ->
verboseOrNothing $ printf "delegator %s removed" (showDelegator edrDelegatorId edrAccount)
Types.CredentialKeysUpdated cid -> verboseOrNothing $ printf "credential keys updated for credential with credId %s" (show cid)
Types.NewEncryptedAmount{..} -> verboseOrNothing $ printf "shielded amount received on account '%s' with index '%s'" (show neaAccount) (show neaNewIndex)
Types.EncryptedAmountsRemoved{..} -> verboseOrNothing $ printf "shielded amounts removed on account '%s' up to index '%s' with a resulting self shielded amount of '%s'" (show earAccount) (show earUpToIndex) (show earNewAmount)
Types.AmountAddedByDecryption{..} -> verboseOrNothing $ printf "transferred %s from the shielded balance to the public balance on account '%s'" (showCcd aabdAmount) (show aabdAccount)
Types.EncryptedSelfAmountAdded{..} -> verboseOrNothing $ printf "transferred %s from the public balance to the shielded balance on account '%s' with a resulting self shielded balance of '%s'" (showCcd eaaAmount) (show eaaAccount) (show eaaNewAmount)
verboseOrNothing $ printf "delegator %s removed." (showDelegator edrDelegatorId edrAccount)
Types.CredentialKeysUpdated cid -> verboseOrNothing $ printf "credential keys updated for credential with credId %s." (show cid)
Types.NewEncryptedAmount{..} -> verboseOrNothing $ printf "shielded amount received on account '%s' with index '%s'." (show neaAccount) (show neaNewIndex)
Types.EncryptedAmountsRemoved{..} -> verboseOrNothing $ printf "shielded amounts removed on account '%s' up to index '%s' with a resulting self shielded amount of '%s'." (show earAccount) (show earUpToIndex) (show earNewAmount)
Types.AmountAddedByDecryption{..} -> verboseOrNothing $ printf "transferred %s from the shielded balance to the public balance on account '%s'." (showCcd aabdAmount) (show aabdAccount)
Types.EncryptedSelfAmountAdded{..} -> verboseOrNothing $ printf "transferred %s from the public balance to the shielded balance on account '%s' with a resulting self shielded balance of '%s'." (showCcd eaaAmount) (show eaaAccount) (show eaaNewAmount)
Types.UpdateEnqueued{..} ->
verboseOrNothing $ printf "Enqueued chain update, effective at %s:\n%s" (showTimeFormatted (timeFromTransactionExpiryTime ueEffectiveTime)) (show uePayload)
Types.TransferredWithSchedule{..} ->
verboseOrNothing $ printf "Sent transfer with schedule %s" (intercalate ", " . map (\(a, b) -> showTimeFormatted (Time.timestampToUTCTime a) ++ ": " ++ showCcd b) $ etwsAmount)
verboseOrNothing $ printf "Sent transfer with schedule %s." (intercalate ", " . map (\(a, b) -> showTimeFormatted (Time.timestampToUTCTime a) ++ ": " ++ showCcd b) $ etwsAmount)
Types.DataRegistered{} ->
verboseOrNothing [i|Registered data on chain.|]
Types.TransferMemo{..} ->
Expand Down Expand Up @@ -904,10 +904,10 @@ showEvent verbose ciM = \case
showLoggedEvents :: [Wasm.ContractEvent] -> String
showLoggedEvents [] = "No contract events were emitted."
showLoggedEvents evs =
[i|#{length evs} contract events were emitted|]
[i|#{length evs} contract #{if length evs > 1 then "events were" else ("event was" :: String)} emitted|]
<> ( if isNothing eventSchemaM
then [i| but no event schema was provided nor found in the contract module. |]
else [i|, of which #{length $ filter isLeft $ map showContractEvent evs} were succesfully parsed. |]
else [i|, of which #{length $ filter isRight $ map showContractEvent evs} #{if (length $ filter isRight $ map showContractEvent evs) > 1 then "were" else ("was" :: String)} successfully parsed. |]
)
<> [i|Got:\n|]
<> intercalate "\n" (map fromEither (map showContractEvent evs))
Expand All @@ -920,7 +920,7 @@ showEvent verbose ciM = \case
eventSchemaM = getEventSchema =<< ciM
-- Show a string representation of the contract event.
-- Returns @Right@ containing a JSON representation of the event if a schema was present
-- and the event could be succesfully parsed using it.
-- and the event could be successfully parsed using it.
-- Returns @Left@ containing a hexadecimal representation of the raw event data otherwise.
showContractEvent :: Wasm.ContractEvent -> Either String String
showContractEvent ce@(Wasm.ContractEvent bs) = case toJSONString eventSchemaM bs of
Expand Down
Loading

0 comments on commit 0f7fa3e

Please sign in to comment.