|
| 1 | +module PlutipRun (plutipRun) where |
| 2 | + |
| 3 | +import BotPlutusInterface.Contract qualified as BPI |
| 4 | +import BotPlutusInterface.QueryNode qualified as BPI |
| 5 | +import BotPlutusInterface.Types |
| 6 | +import Cardano.Api (NetworkId (Mainnet)) |
| 7 | +import Cardano.Api.Shelley (ProtocolParameters) |
| 8 | +import Control.Concurrent.STM (newTVarIO, readTVarIO) |
| 9 | +import Data.Aeson ((.=)) |
| 10 | +import Data.Aeson qualified as JSON |
| 11 | +import Data.Default (def) |
| 12 | +import Data.Text (Text) |
| 13 | +import Data.Text qualified as Text |
| 14 | +import Data.UUID.V4 qualified as UUID |
| 15 | +import Ledger (POSIXTime (POSIXTime), PubKeyHash) |
| 16 | +import TimeDebugContract qualified |
| 17 | + |
| 18 | +import GHC.IO.Encoding (setLocaleEncoding, utf8) |
| 19 | +import Ledger.TimeSlot (SlotConfig (scSlotZeroTime)) |
| 20 | +import Plutus.PAB.Core.ContractInstance.STM (Activity (Active)) |
| 21 | +import Servant.Client (BaseUrl (BaseUrl), Scheme (Http)) |
| 22 | +import System.Directory (listDirectory) |
| 23 | +import System.Environment (getArgs, getEnv, setEnv) |
| 24 | +import System.FilePath ((</>)) |
| 25 | +import Wallet.Types (ContractInstanceId (ContractInstanceId)) |
| 26 | +import Prelude |
| 27 | + |
| 28 | +plutipRun :: IO () |
| 29 | +plutipRun = do |
| 30 | + setLocaleEncoding utf8 |
| 31 | + [sockPath, clusterDir, cliDir] <- getArgs |
| 32 | + setEnv "CARDANO_NODE_SOCKET_PATH" sockPath |
| 33 | + getEnv "PATH" >>= \p -> setEnv "PATH" (p ++ ":" ++ cliDir) |
| 34 | + let nodeInfo = BPI.NodeInfo Mainnet sockPath |
| 35 | + |
| 36 | + cEnv <- mkContractEnv nodeInfo clusterDir |
| 37 | + putStrLn "Running contract" |
| 38 | + res <- BPI.runContract cEnv TimeDebugContract.unlockWithTimeCheck |
| 39 | + putStrLn $ case res of |
| 40 | + Right r -> "=== OK ===\n" ++ show r |
| 41 | + Left e -> "=== FAILED ===\n" ++ show e |
| 42 | + |
| 43 | + stats <- readTVarIO (ceContractStats cEnv) |
| 44 | + putStrLn $ "=== Stats ===\n" ++ show stats |
| 45 | + |
| 46 | +mkContractEnv :: Monoid w => BPI.NodeInfo -> FilePath -> IO (ContractEnvironment w) |
| 47 | +mkContractEnv nodeInfo clusterDir = do |
| 48 | + (pparams, paramsFile) <- getPparams nodeInfo clusterDir |
| 49 | + contractInstanceID <- ContractInstanceId <$> UUID.nextRandom |
| 50 | + contractState <- newTVarIO (ContractState Active mempty) |
| 51 | + contractStats <- newTVarIO (ContractStats mempty) |
| 52 | + pkhs <- getPkhs clusterDir |
| 53 | + return $ |
| 54 | + ContractEnvironment |
| 55 | + { cePABConfig = mkPabConf pparams (Text.pack paramsFile) clusterDir (head pkhs) |
| 56 | + , ceContractState = contractState |
| 57 | + , ceContractInstanceId = contractInstanceID |
| 58 | + , ceContractStats = contractStats |
| 59 | + } |
| 60 | + |
| 61 | +getPparams :: BPI.NodeInfo -> FilePath -> IO (ProtocolParameters, FilePath) |
| 62 | +getPparams nodeInfo clusterDir = do |
| 63 | + pparams :: ProtocolParameters <- getOrFailM $ BPI.queryProtocolParams nodeInfo |
| 64 | + let ppath = clusterDir </> "pparams.json" |
| 65 | + JSON.encodeFile ppath pparams |
| 66 | + return (pparams, ppath) |
| 67 | + |
| 68 | +mkPabConf :: ProtocolParameters -> Text -> FilePath -> PubKeyHash -> PABConfig |
| 69 | +mkPabConf pparams pparamsFile clusterDir ownPkh = |
| 70 | + PABConfig |
| 71 | + { pcCliLocation = Local |
| 72 | + , pcNetwork = Mainnet |
| 73 | + , pcChainIndexUrl = BaseUrl Http "localhost" 9083 "" |
| 74 | + , pcPort = 9080 |
| 75 | + , pcProtocolParams = pparams |
| 76 | + , pcTipPollingInterval = 1_000_000 |
| 77 | + , pcSlotConfig = def {scSlotZeroTime = POSIXTime $ 1652956123 * 1000} |
| 78 | + , pcOwnPubKeyHash = ownPkh |
| 79 | + , pcOwnStakePubKeyHash = Nothing |
| 80 | + , pcScriptFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/scripts" |
| 81 | + , pcSigningKeyFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/signing-keys" |
| 82 | + , pcTxFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/txs" |
| 83 | + , pcDryRun = False |
| 84 | + , pcLogLevel = Error |
| 85 | + , pcProtocolParamsFile = pparamsFile |
| 86 | + , pcEnableTxEndpoint = True |
| 87 | + , pcCollectStats = True |
| 88 | + , pcMetadataDir = Text.pack $ clusterDir </> "bot-plutus-interface/metadata" |
| 89 | + } |
| 90 | + |
| 91 | +getPkhs :: FilePath -> IO [PubKeyHash] |
| 92 | +getPkhs bpiDir = do |
| 93 | + let dir = bpiDir </> "bot-plutus-interface/signing-keys" |
| 94 | + replace = |
| 95 | + Text.unpack |
| 96 | + . Text.replace "signing-key-" "" |
| 97 | + . Text.replace ".skey" "" |
| 98 | + . Text.pack |
| 99 | + keyNames <- listDirectory dir |
| 100 | + return $ map (parseKey . replace) keyNames |
| 101 | + where |
| 102 | + parseKey :: String -> PubKeyHash |
| 103 | + parseKey key = |
| 104 | + let res = JSON.fromJSON $ JSON.object ["getPubKeyHash" .= key] |
| 105 | + in case res of |
| 106 | + JSON.Success pkh -> pkh |
| 107 | + _ -> error "failed to parse pkh" |
| 108 | + |
| 109 | +getOrFail :: Show e => Either e a -> a |
| 110 | +getOrFail = either (error . show) id |
| 111 | + |
| 112 | +getOrFailM :: (Show e, Functor f) => f (Either e b) -> f b |
| 113 | +getOrFailM = (getOrFail <$>) |
0 commit comments