Skip to content

Commit 29d6c29

Browse files
Merge pull request #94 from mlabs-haskell/sam/contract-check
Add contract exists endpoint
2 parents a8f6d67 + c6d6e51 commit 29d6c29

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/BotPlutusInterface/Contract.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ awaitTxStatusChange contractEnv txId = do
185185
case mTx of
186186
Nothing -> pure Unknown
187187
Just txState -> do
188+
printLog @w Debug $ "Found transaction in node, waiting " ++ show chainConstant ++ " blocks for it to settle."
188189
awaitNBlocks @w contractEnv (chainConstant + 1)
189190
-- Check if the tx is still present in chain-index, in case of a rollback
190191
-- we might not find it anymore.

src/BotPlutusInterface/Server.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ initState :: IO AppState
8686
initState = AppState <$> newTVarIO Map.empty
8787

8888
-- | Mock API Schema, stripped endpoints that we don't use in this project
89-
type API a = WebSocketEndpoint :<|> ActivateContractEndpoint a :<|> RawTxEndpoint
89+
type API a = WebSocketEndpoint :<|> ActivateContractEndpoint a :<|> ContractLookupEndpoint :<|> RawTxEndpoint
9090

9191
-- Endpoints are split up so it is easier to test them. In particular servant-client
9292
-- can not generate a client for the WebSocketEndpoint; this allows us to still
@@ -101,6 +101,13 @@ type ActivateContractEndpoint a =
101101
:> ReqBody '[JSON] (ContractActivationArgs a)
102102
:> Post '[JSON] ContractInstanceId -- Start a new instance.
103103

104+
type ContractLookupEndpoint =
105+
"api"
106+
:> "contract"
107+
:> "exists"
108+
:> ReqBody '[JSON] ContractInstanceId
109+
:> Post '[JSON] Bool
110+
104111
type RawTxEndpoint =
105112
"raw-tx"
106113
:> Capture "tx-id" TxIdCapture
@@ -131,6 +138,7 @@ server :: HasDefinitions t => PABConfig -> AppState -> Server (API t)
131138
server pabConfig state =
132139
websocketHandler state
133140
:<|> activateContractHandler pabConfig state
141+
:<|> contractLookupHandler state
134142
:<|> rawTxHandler pabConfig
135143

136144
apiProxy :: forall (t :: Type). Proxy (API t)
@@ -239,6 +247,14 @@ activateContractHandler pabConf state (ContractActivationArgs cardMessage _) =
239247
case getContract cardMessage of
240248
SomeBuiltin contract -> handleContract pabConf state contract
241249

250+
contractLookupHandler ::
251+
AppState ->
252+
ContractInstanceId ->
253+
Handler Bool
254+
contractLookupHandler (AppState s) contractInstanceId = liftIO . atomically $ do
255+
instances <- readTVar s
256+
return $ Map.member contractInstanceId instances
257+
242258
handleContract ::
243259
forall
244260
(w :: Type)

0 commit comments

Comments
 (0)