{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}

module Cardano.CLI.Byron.UpdateProposal
  ( runProposalCreation
  , readByronUpdateProposal
  , submitByronUpdateProposal
  )
where

import Cardano.Api
import Cardano.Api.Byron
  ( AsType (AsByronUpdateProposal)
  , ByronProtocolParametersUpdate
  , ByronUpdateProposal
  , makeByronUpdateProposal
  , toByronLedgerUpdateProposal
  )
import Cardano.Api.Byron qualified as Byron

import Cardano.CLI.Byron.Key (readByronSigningKey)
import Cardano.CLI.Byron.Tx (nodeSubmitTx)
import Cardano.CLI.Compatible.Exception
import Cardano.CLI.Helper (ensureNewFileLBS)
import Cardano.CLI.Orphan ()
import Cardano.CLI.Read
import Cardano.CLI.Type.Common

import Control.Tracer (stdoutTracer, traceWith)

runProposalCreation
  :: NetworkId
  -> SigningKeyFile In
  -> Byron.ProtocolVersion
  -> Byron.SoftwareVersion
  -> Byron.SystemTag
  -> Byron.InstallerHash
  -> FilePath
  -> ByronProtocolParametersUpdate
  -> CIO e ()
runProposalCreation :: forall e.
NetworkId
-> SigningKeyFile 'In
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> FilePath
-> ByronProtocolParametersUpdate
-> CIO e ()
runProposalCreation
  NetworkId
nw
  SigningKeyFile 'In
sKey
  ProtocolVersion
pVer
  SoftwareVersion
sVer
  SystemTag
sysTag
  InstallerHash
insHash
  FilePath
outputFp
  ByronProtocolParametersUpdate
params = do
    sK <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
NonLegacyByronKeyFormat SigningKeyFile 'In
sKey
    let proposal = NetworkId
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> SomeByronSigningKey
-> ByronProtocolParametersUpdate
-> ByronUpdateProposal
makeByronUpdateProposal NetworkId
nw ProtocolVersion
pVer SoftwareVersion
sVer SystemTag
sysTag InstallerHash
insHash SomeByronSigningKey
sK ByronProtocolParametersUpdate
params
    fromExceptTCli $
      ensureNewFileLBS outputFp $
        serialiseToRawBytes proposal

readByronUpdateProposal :: FilePath -> CIO e ByronUpdateProposal
readByronUpdateProposal :: forall e. FilePath -> CIO e ByronUpdateProposal
readByronUpdateProposal FilePath
fp = do
  proposalBs <-
    FilePath -> RIO e ByteString
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
FilePath -> m ByteString
readFileCli FilePath
fp
  let proposalResult = AsType ByronUpdateProposal
-> ByteString
-> Either SerialiseAsRawBytesError ByronUpdateProposal
forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either SerialiseAsRawBytesError a
deserialiseFromRawBytes AsType ByronUpdateProposal
AsByronUpdateProposal ByteString
proposalBs
  fromEitherCli proposalResult

submitByronUpdateProposal
  :: SocketPath
  -> NetworkId
  -> FilePath
  -> CIO e ()
submitByronUpdateProposal :: forall e. SocketPath -> NetworkId -> FilePath -> CIO e ()
submitByronUpdateProposal SocketPath
nodeSocketPath NetworkId
network FilePath
proposalFp = do
  proposal <- FilePath -> CIO e ByronUpdateProposal
forall e. FilePath -> CIO e ByronUpdateProposal
readByronUpdateProposal FilePath
proposalFp
  let genTx = ByronUpdateProposal -> GenTx ByronBlock
toByronLedgerUpdateProposal ByronUpdateProposal
proposal
  traceWith stdoutTracer $ "Update proposal TxId: " ++ condense (txId genTx)
  fromExceptTCli $ nodeSubmitTx nodeSocketPath network genTx