1
1
module Coop.Cli.TxBuilderGrpc (txBuilderService , TxBuilderGrpcOpts (.. )) where
2
2
3
- import Control.Lens ((&) , (.~) , (^.) )
3
+ import Control.Lens (makeLenses , (&) , (.~) , (^.) )
4
4
import Network.GRPC.HTTP2.Encoding as Encoding (
5
5
gzip ,
6
6
uncompressed ,
@@ -22,42 +22,51 @@ import BotPlutusInterface.Config (loadPABConfig)
22
22
import BotPlutusInterface.Files (txFileName )
23
23
import BotPlutusInterface.Types (PABConfig , RawTx (_cborHex ), pcOwnPubKeyHash , pcTxFileDir )
24
24
import Cardano.Proto.Aux (ProtoCardano (toCardano ))
25
- import Coop.Pab (runMintFsTx )
25
+ import Coop.Pab (runGcFsTx , runMintFsTx )
26
26
import Coop.Pab.Aux (runBpi )
27
27
import Coop.Types (CoopDeployment )
28
28
import Data.Aeson (decodeFileStrict )
29
29
import Data.Maybe (fromMaybe )
30
30
import Data.ProtoLens (Message (defMessage ))
31
31
import Data.Text (Text , unpack )
32
+ import Data.Text qualified as Text
32
33
import GHC.Exts (fromString )
33
34
import Ledger (PaymentPubKeyHash (PaymentPubKeyHash ), TxId )
34
35
import Proto.Plutus_Fields (cborBase16 )
35
- import Proto.TxBuilderService_Fields (alreadyPublished , mintFsSuccess , mintFsTx , msg , otherErr , submitter )
36
+ import Proto.TxBuilderService_Fields (alreadyPublished , gcFsTx , info , mintFsSuccess , mintFsTx , msg , otherErr , submitter , success )
36
37
import Proto.TxBuilderService_Fields qualified as Proto.TxBuilderService
37
38
import System.Directory (doesFileExist , makeAbsolute )
38
39
import System.FilePath ((</>) )
39
40
40
41
data TxBuilderGrpcOpts = TxBuilderGrpcOpts
41
- { tbgo'pabConfig :: FilePath
42
- , tbgo'coopDeploymentFile :: FilePath
43
- , tbgo'authWallets :: [PubKeyHash ]
44
- , tbgo'fee :: (PubKeyHash , AssetClass , Integer )
45
- , tbgo'grpcAddress :: String
46
- , tbgo'grpcPort :: Int
47
- , tbgo'tlsCertFile :: FilePath
48
- , tbgo'tlsKeyFile :: FilePath
42
+ { _pabConfig :: FilePath
43
+ , _coopDeploymentFile :: FilePath
44
+ , _authWallets :: [PubKeyHash ]
45
+ , _fee :: (PubKeyHash , AssetClass , Integer )
46
+ , _grpcAddress :: String
47
+ , _grpcPort :: Int
48
+ , _tlsCertFile :: FilePath
49
+ , _tlsKeyFile :: FilePath
50
+ , _mintFsTxValidityMinutes :: Integer
49
51
}
50
52
deriving stock (Show , Eq )
51
53
54
+ makeLenses ''TxBuilderGrpcOpts
55
+
52
56
txBuilderService :: TxBuilderGrpcOpts -> IO ()
53
57
txBuilderService opts = do
54
- coopDeployment <- fromMaybe (error " txBuilderService: Must have a CoopDeployment file in JSON" ) <$> decodeFileStrict @ CoopDeployment (tbgo'coopDeploymentFile opts)
55
- pabConf <- either (\ err -> error $ " txBuilderService: Must have a PABConfig file in Config format: " <> err) id <$> loadPABConfig (tbgo'pabConfig opts)
58
+ coopDeployment <- fromMaybe (error " txBuilderService: Must have a CoopDeployment file in JSON" ) <$> decodeFileStrict @ CoopDeployment (opts ^. coopDeploymentFile )
59
+ pabConf <- either (\ err -> error $ " txBuilderService: Must have a PABConfig file in Config format: " <> err) id <$> loadPABConfig (opts ^. pabConfig )
56
60
57
- let (feeCollector, feeAc, feeQ) = tbgo'fee opts
61
+ let (feeCollector, feeAc, feeQ) = opts ^. fee
58
62
feeValue = assetClassValue feeAc feeQ
59
- authenticators = PaymentPubKeyHash <$> tbgo'authWallets opts
60
- runMintFsTxOnReq = runMintFsTx coopDeployment authenticators (feeValue, PaymentPubKeyHash feeCollector)
63
+ authenticators = PaymentPubKeyHash <$> opts ^. authWallets
64
+ runMintFsTxOnReq =
65
+ runMintFsTx
66
+ coopDeployment
67
+ authenticators
68
+ (feeValue, PaymentPubKeyHash feeCollector)
69
+ (False , opts ^. mintFsTxValidityMinutes)
61
70
62
71
handleCreateMintFsTx :: Server. UnaryHandler IO CreateMintFsTxReq CreateMintFsTxResp
63
72
handleCreateMintFsTx _ req = do
@@ -78,8 +87,12 @@ txBuilderService opts = do
78
87
)
79
88
( \ txId -> do
80
89
mayRawTx <- readSignedTx pabConf txId
81
- maybe
82
- (return $ defMessage & Proto.TxBuilderService. error . otherErr . msg .~ " Unable to authenticate transaction" )
90
+ either
91
+ ( \ err ->
92
+ return $
93
+ defMessage
94
+ & Proto.TxBuilderService. error . otherErr . msg .~ (" Failed creating mint-fact-statement-tx: " <> err)
95
+ )
83
96
( \ rawTx ->
84
97
return $
85
98
defMessage
@@ -92,6 +105,50 @@ txBuilderService opts = do
92
105
)
93
106
errOrAcs
94
107
108
+ runGcFsTxOnReq =
109
+ runGcFsTx
110
+ coopDeployment
111
+ False
112
+
113
+ handleCreateGcFsTx :: Server. UnaryHandler IO CreateGcFsTxReq CreateGcFsTxResp
114
+ handleCreateGcFsTx _ req = do
115
+ sub <- toCardano (req ^. submitter)
116
+ (_, errOrAcs) <-
117
+ runBpi @ Text
118
+ pabConf
119
+ { pcOwnPubKeyHash = sub
120
+ }
121
+ (runGcFsTxOnReq req)
122
+ either
123
+ (\ err -> return $ defMessage & Proto.TxBuilderService. error . otherErr . msg .~ err)
124
+ ( \ (mayTxId, info') -> do
125
+ maybe
126
+ ( return $
127
+ defMessage
128
+ & Proto.TxBuilderService. error . otherErr . msg .~ " Failed creating a gc-fact-statement-tx"
129
+ & info .~ info'
130
+ )
131
+ ( \ txId -> do
132
+ mayRawTx <- readSignedTx pabConf txId
133
+ either
134
+ ( \ err ->
135
+ return $
136
+ defMessage
137
+ & Proto.TxBuilderService. error . otherErr . msg .~ (" Failed creating a gc-fact-statement-tx: " <> err)
138
+ & info .~ info'
139
+ )
140
+ ( \ rawTx ->
141
+ return $
142
+ defMessage
143
+ & success . gcFsTx . cborBase16 .~ rawTx
144
+ & info .~ info'
145
+ )
146
+ mayRawTx
147
+ )
148
+ mayTxId
149
+ )
150
+ errOrAcs
151
+
95
152
routes :: [ServiceHandler ]
96
153
routes =
97
154
[ Server. unary (RPC :: RPC TxBuilder " createMintFsTx" ) handleCreateMintFsTx
@@ -100,8 +157,8 @@ txBuilderService opts = do
100
157
101
158
runServer
102
159
routes
103
- (fromString $ tbgo' grpcAddress opts, tbgo'grpcPort opts )
104
- (tbgo' tlsCertFile opts, tbgo'tlsKeyFile opts )
160
+ (fromString $ opts ^. grpcAddress, opts ^. grpcPort )
161
+ (opts ^. tlsCertFile, opts ^. tlsKeyFile )
105
162
106
163
runServer :: [ServiceHandler ] -> (Warp. HostPreference , Int ) -> (FilePath , FilePath ) -> IO ()
107
164
runServer routes (h, p) (certFile, keyFile) = do
@@ -117,13 +174,7 @@ runServer routes (h, p) (certFile, keyFile) = do
117
174
, Encoding. gzip
118
175
]
119
176
120
- handleCreateGcFsTx :: Server. UnaryHandler IO CreateGcFsTxReq CreateGcFsTxResp
121
- handleCreateGcFsTx _ _ =
122
- return $
123
- defMessage
124
- & Proto.TxBuilderService. error . otherErr . msg .~ " Finally"
125
-
126
- readSignedTx :: PABConfig -> TxId -> IO (Maybe Text )
177
+ readSignedTx :: PABConfig -> TxId -> IO (Either Text Text )
127
178
readSignedTx pabConf txId = do
128
179
txFolderPath <- makeAbsolute (unpack . pcTxFileDir $ pabConf)
129
180
let path :: FilePath
@@ -134,11 +185,9 @@ readSignedTx pabConf txId = do
134
185
mayRawTx <- decodeFileStrict @ RawTx path
135
186
maybe
136
187
( do
137
- print $ " Must have a properly formatter RawTx in Json at " <> path
138
- return Nothing
188
+ return . Left . Text. pack $ " Must have a properly formatter RawTx in Json at " <> path
139
189
)
140
- (return . Just . _cborHex)
190
+ (return . Right . _cborHex)
141
191
mayRawTx
142
192
else do
143
- print $ " Must find signed transaction file at " <> path
144
- return Nothing
193
+ return . Left . Text. pack $ " Must find signed transaction file at " <> path
0 commit comments