|
| 1 | +module Main (main) 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) |
| 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 (PubKeyHash) |
| 16 | +import LockSpend (lockThenSpend) |
| 17 | +-- import LockSpendSingle (lockThenSpendSingle) |
| 18 | +import Plutus.PAB.Core.ContractInstance.STM (Activity (Active)) |
| 19 | +import Servant.Client (BaseUrl (BaseUrl), Scheme (Http)) |
| 20 | +import System.Directory (listDirectory) |
| 21 | +import System.Environment (getArgs, setEnv, getEnv) |
| 22 | +import System.FilePath ((</>)) |
| 23 | +import Wallet.Types (ContractInstanceId (ContractInstanceId)) |
| 24 | +import Prelude |
| 25 | + |
| 26 | +main :: IO () |
| 27 | +main = do |
| 28 | + -- TODO: export PATH=$PATH:/home/mike/dev/mlabs/local-cluster/node-bins |
| 29 | + let clusterDir = "/home/mike/dev/mlabs/local-cluster/data" |
| 30 | + [sockPath] <- getArgs |
| 31 | + setEnv "CARDANO_NODE_SOCKET_PATH" sockPath |
| 32 | + getEnv "PATH" >>= \p -> setEnv "PATH" (p ++ ":/home/mike/dev/mlabs/local-cluster/node-bins") |
| 33 | + let nodeInfo = BPI.NodeInfo Mainnet sockPath |
| 34 | + |
| 35 | + cEnv <- mkContractEnv nodeInfo clusterDir |
| 36 | + res <- BPI.runContract cEnv lockThenSpend |
| 37 | + -- res <- BPI.runContract cEnv lockThenSpendSingle |
| 38 | + putStrLn $ case res of |
| 39 | + Right _ -> "=== OK ===" |
| 40 | + Left e -> "=== FAILED ===\n" ++ show e |
| 41 | + |
| 42 | +mkContractEnv :: Monoid w => BPI.NodeInfo -> FilePath -> IO (ContractEnvironment w) |
| 43 | +mkContractEnv nodeInfo clusterDir = do |
| 44 | + (pparams, paramsFile) <- getPparams nodeInfo clusterDir |
| 45 | + contractInstanceID <- ContractInstanceId <$> UUID.nextRandom |
| 46 | + contractState <- newTVarIO (ContractState Active mempty) |
| 47 | + pkhs <- getPkhs clusterDir |
| 48 | + return $ |
| 49 | + ContractEnvironment |
| 50 | + { cePABConfig = mkPabConf pparams (Text.pack paramsFile) clusterDir (head pkhs), |
| 51 | + ceContractState = contractState, |
| 52 | + ceContractInstanceId = contractInstanceID |
| 53 | + } |
| 54 | + |
| 55 | +getPparams :: BPI.NodeInfo -> FilePath -> IO (ProtocolParameters, FilePath) |
| 56 | +getPparams nodeInfo clusterDir = do |
| 57 | + pparams :: ProtocolParameters <- getOrFailM $ BPI.protocolParams nodeInfo |
| 58 | + let ppath = clusterDir </> "pparams.json" |
| 59 | + JSON.encodeFile ppath pparams |
| 60 | + return (pparams, ppath) |
| 61 | + |
| 62 | +mkPabConf :: ProtocolParameters -> Text -> FilePath -> PubKeyHash -> PABConfig |
| 63 | +mkPabConf pparams pparamsFile clusterDir ownPkh = |
| 64 | + PABConfig |
| 65 | + { pcCliLocation = Local, |
| 66 | + pcNetwork = Mainnet, |
| 67 | + pcChainIndexUrl = BaseUrl Http "localhost" 9083 "", |
| 68 | + pcPort = 9080, |
| 69 | + pcProtocolParams = pparams, |
| 70 | + pcTipPollingInterval = 1_000_000, |
| 71 | + pcSlotConfig = def, |
| 72 | + pcOwnPubKeyHash = ownPkh, |
| 73 | + pcScriptFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/scripts", |
| 74 | + pcSigningKeyFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/signing-keys", |
| 75 | + pcTxFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/txs", |
| 76 | + pcDryRun = False, |
| 77 | + pcLogLevel = Error, |
| 78 | + pcProtocolParamsFile = pparamsFile, |
| 79 | + pcEnableTxEndpoint = True |
| 80 | + } |
| 81 | + |
| 82 | +getPkhs :: FilePath -> IO [PubKeyHash] |
| 83 | +getPkhs bpiDir = do |
| 84 | + let dir = bpiDir </> "bot-plutus-interface/signing-keys" |
| 85 | + replace = |
| 86 | + Text.unpack |
| 87 | + . Text.replace "signing-key-" "" |
| 88 | + . Text.replace ".skey" "" |
| 89 | + . Text.pack |
| 90 | + keyNames <- listDirectory dir |
| 91 | + return $ map (parseKey . replace) keyNames |
| 92 | + where |
| 93 | + parseKey :: String -> PubKeyHash |
| 94 | + parseKey key = |
| 95 | + let res = JSON.fromJSON $ JSON.object ["getPubKeyHash" .= key] |
| 96 | + in case res of |
| 97 | + JSON.Success pkh -> pkh |
| 98 | + _ -> error "failed to parse pkh" |
| 99 | + |
| 100 | +getOrFail :: Show e => Either e a -> a |
| 101 | +getOrFail = either (error . show) id |
| 102 | + |
| 103 | +getOrFailM :: (Show e, Functor f) => f (Either e b) -> f b |
| 104 | +getOrFailM = (getOrFail <$>) |
0 commit comments