first complete solution

This commit is contained in:
Lars Brünjes 2021-05-10 23:10:43 +02:00
parent 7152bc5e02
commit 54e726eb95
No known key found for this signature in database
GPG key ID: B488B9045DC1A087

View file

@ -7,7 +7,7 @@ module Main
) where ) where
import Control.Concurrent import Control.Concurrent
import Control.Monad (when) import Control.Exception
import Control.Monad.IO.Class (MonadIO (..)) import Control.Monad.IO.Class (MonadIO (..))
import Data.Aeson (Result (..), fromJSON) import Data.Aeson (Result (..), fromJSON)
import Data.Monoid (Last (..)) import Data.Monoid (Last (..))
@ -19,18 +19,40 @@ import Network.HTTP.Req
import Plutus.PAB.Events.ContractInstanceState (PartiallyDecodedResponse (..)) import Plutus.PAB.Events.ContractInstanceState (PartiallyDecodedResponse (..))
import Plutus.PAB.Webserver.Types import Plutus.PAB.Webserver.Types
import System.Environment (getArgs) import System.Environment (getArgs)
import System.IO
import Text.Read (readMaybe)
import Week06.Oracle.PAB (OracleContracts) import Week06.Oracle.PAB (OracleContracts)
main :: IO () main :: IO ()
main = do main = do
[(i :: Int)] <- map read <$> getArgs [i :: Int] <- map read <$> getArgs
uuid <- read <$> readFile ('W' : show i ++ ".cid") uuid <- read <$> readFile ('W' : show i ++ ".cid")
hSetBuffering stdout NoBuffering
putStrLn $ "swap contract instance id for Wallet " ++ show i ++ ": " ++ show uuid putStrLn $ "swap contract instance id for Wallet " ++ show i ++ ": " ++ show uuid
getFunds uuid go uuid
where
go :: UUID -> IO a
go uuid = do
cmd <- readCommand
case cmd of
Offer amt -> offer uuid amt
Retrieve -> retrieve uuid
Use -> use uuid
Funds -> getFunds uuid
go uuid
readCommand :: IO Command
readCommand = do
putStr "enter command (Offer amt, Retrieve or Use): "
s <- getLine
maybe readCommand return $ readMaybe s
data Command = Offer Integer | Retrieve | Use | Funds
deriving (Show, Read, Eq, Ord)
getFunds :: UUID -> IO () getFunds :: UUID -> IO ()
getFunds uuid = runReq defaultHttpConfig $ do getFunds uuid = handle h $ runReq defaultHttpConfig $ do
v <- req v <- req
POST POST
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "funds") (http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "funds")
@ -47,40 +69,55 @@ getFunds uuid = runReq defaultHttpConfig $ do
(Proxy :: Proxy (JsonResponse (ContractInstanceClientState OracleContracts))) (Proxy :: Proxy (JsonResponse (ContractInstanceClientState OracleContracts)))
(port 8080) (port 8080)
liftIO $ putStrLn $ case fromJSON $ observableState $ cicCurrentState $ responseBody w of liftIO $ putStrLn $ case fromJSON $ observableState $ cicCurrentState $ responseBody w of
Success (Last (Just f)) -> "fund: " ++ show (flattenValue f) Success (Last (Just f)) -> "funds: " ++ show (flattenValue f)
_ -> "error decoding state" _ -> "error decoding state"
where
h :: HttpException -> IO ()
h _ = threadDelay 1_000_000 >> getFunds uuid
{- offer :: UUID -> Integer -> IO ()
liftIO $ putStrLn $ if responseStatusCode v == 200 offer uuid amt = handle h $ runReq defaultHttpConfig $ do
then "updated oracle to " ++ show x
else "error updating oracle"
-}
{-
updateOracle :: UUID -> Integer -> IO ()
updateOracle uuid x = runReq defaultHttpConfig $ do
v <- req v <- req
POST POST
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "update") (http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "offer")
(ReqBodyJson x) (ReqBodyJson amt)
(Proxy :: Proxy (JsonResponse ())) (Proxy :: Proxy (JsonResponse ()))
(port 8080) (port 8080)
liftIO $ putStrLn $ if responseStatusCode v == 200 liftIO $ putStrLn $ if responseStatusCode v == 200
then "updated oracle to " ++ show x then "offered swap of " ++ show amt ++ " lovelace"
else "error updating oracle" else "error offering swap"
where
h :: HttpException -> IO ()
h _ = threadDelay 1_000_000 >> offer uuid amt
getExchangeRate :: IO Integer retrieve :: UUID -> IO ()
getExchangeRate = runReq defaultHttpConfig $ do retrieve uuid = handle h $ runReq defaultHttpConfig $ do
v <- req v <- req
GET POST
(https "coinmarketcap.com" /: "currencies" /: "cardano") (http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "retrieve")
NoReqBody (ReqBodyJson ())
bsResponse (Proxy :: Proxy (JsonResponse ()))
mempty (port 8080)
let priceRegex = "priceValue___11gHJ\">\\$([\\.0-9]*)" :: ByteString liftIO $ putStrLn $ if responseStatusCode v == 200
(_, _, _, [bs]) = responseBody v =~ priceRegex :: (ByteString, ByteString, ByteString, [ByteString]) then "retrieved swaps"
d = read $ unpack bs :: Double else "error retrieving swaps"
x = round $ 1_000_000 * d where
liftIO $ putStrLn $ "queried exchange rate: " ++ show d h :: HttpException -> IO ()
return x h _ = threadDelay 1_000_000 >> retrieve uuid
-}
use :: UUID -> IO ()
use uuid = handle h $ runReq defaultHttpConfig $ do
v <- req
POST
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "use")
(ReqBodyJson ())
(Proxy :: Proxy (JsonResponse ()))
(port 8080)
liftIO $ putStrLn $ if responseStatusCode v == 200
then "used swap"
else "error using swap"
where
h :: HttpException -> IO ()
h _ = threadDelay 1_000_000 >> use uuid