From 168b29a80b50ce5049c2f7acdffdb46a13166693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Br=C3=BCnjes?= Date: Mon, 10 May 2021 21:47:35 +0200 Subject: [PATCH] oracle client --- code/week06/app/oracle-client.hs | 59 +++++++++++++++++++ code/week06/app/{oracle.hs => oracle-pab.hs} | 0 code/week06/app/swap-client.hs | 6 ++ code/week06/hie.yaml | 8 ++- .../plutus-pioneer-program-week06.cabal | 26 +++++++- 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 code/week06/app/oracle-client.hs rename code/week06/app/{oracle.hs => oracle-pab.hs} (100%) create mode 100644 code/week06/app/swap-client.hs diff --git a/code/week06/app/oracle-client.hs b/code/week06/app/oracle-client.hs new file mode 100644 index 0000000..b003f87 --- /dev/null +++ b/code/week06/app/oracle-client.hs @@ -0,0 +1,59 @@ +{-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE OverloadedStrings #-} + +module Main + ( main + ) where + +import Control.Concurrent +import Control.Monad (when) +import Control.Monad.IO.Class (MonadIO (..)) +import Data.ByteString (ByteString) +import Data.ByteString.Char8 (unpack) +import Data.Proxy (Proxy (..)) +import Data.Text (pack) +import Data.UUID +import Network.HTTP.Req +import Text.Regex.TDFA + +main :: IO () +main = do + uuid <- read <$> readFile "oracle.cid" + putStrLn $ "oracle contract instance id: " ++ show uuid + go uuid Nothing + where + go :: UUID -> Maybe Integer -> IO a + go uuid m = do + x <- getExchangeRate + let y = Just x + when (m /= y) $ + updateOracle uuid x + threadDelay 5_000_000 + go uuid y + +updateOracle :: UUID -> Integer -> IO () +updateOracle uuid x = runReq defaultHttpConfig $ do + v <- req + POST + (http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "update") + (ReqBodyJson x) + (Proxy :: Proxy (JsonResponse ())) + (port 8080) + liftIO $ putStrLn $ if responseStatusCode v == 200 + then "updated oracle to " ++ show x + else "error updating oracle" + +getExchangeRate :: IO Integer +getExchangeRate = runReq defaultHttpConfig $ do + v <- req + GET + (https "coinmarketcap.com" /: "currencies" /: "cardano") + NoReqBody + bsResponse + mempty + let priceRegex = "priceValue___11gHJ\">\\$([\\.0-9]*)" :: ByteString + (_, _, _, [bs]) = responseBody v =~ priceRegex :: (ByteString, ByteString, ByteString, [ByteString]) + d = read $ unpack bs :: Double + x = round $ 1_000_000 * d + liftIO $ putStrLn $ "queried exchange rate: " ++ show d + return x diff --git a/code/week06/app/oracle.hs b/code/week06/app/oracle-pab.hs similarity index 100% rename from code/week06/app/oracle.hs rename to code/week06/app/oracle-pab.hs diff --git a/code/week06/app/swap-client.hs b/code/week06/app/swap-client.hs new file mode 100644 index 0000000..fb42941 --- /dev/null +++ b/code/week06/app/swap-client.hs @@ -0,0 +1,6 @@ +module Main + ( main + ) where + +main :: IO () +main = putStrLn "oracle client" diff --git a/code/week06/hie.yaml b/code/week06/hie.yaml index 9be7de5..105b4e1 100644 --- a/code/week06/hie.yaml +++ b/code/week06/hie.yaml @@ -2,5 +2,9 @@ cradle: cabal: - path: "./src" component: "lib:plutus-pioneer-program-week06" - - path: "./app/oracle.hs" - component: "exe:oracle" + - path: "./app/oracle-pab.hs" + component: "exe:oracle-pab" + - path: "./app/oracle-client.hs" + component: "exe:oracle-client" + - path: "./app/swap-client.hs" + component: "exe:swap-client" diff --git a/code/week06/plutus-pioneer-program-week06.cabal b/code/week06/plutus-pioneer-program-week06.cabal index 389db5b..5e1d945 100644 --- a/code/week06/plutus-pioneer-program-week06.cabal +++ b/code/week06/plutus-pioneer-program-week06.cabal @@ -30,8 +30,8 @@ library default-language: Haskell2010 ghc-options: -Wall -fobject-code -fno-ignore-interface-pragmas -fno-omit-interface-pragmas -fno-strictness -fno-spec-constr -fno-specialise -executable oracle - main-is: oracle.hs +executable oracle-pab + main-is: oracle-pab.hs hs-source-dirs: app ghc-options: -Wall -threaded build-depends: aeson @@ -45,3 +45,25 @@ executable oracle , plutus-use-cases , prettyprinter , text + +executable oracle-client + main-is: oracle-client.hs + hs-source-dirs: app + ghc-options: -Wall + build-depends: aeson + , base ^>= 4.14.1.0 + , bytestring + , modern-uri + , regex-tdfa ^>= 1.3.1.0 + , req ^>= 3.9.0 + , text + , uuid + +executable swap-client + main-is: swap-client.hs + hs-source-dirs: app + ghc-options: -Wall + build-depends: aeson + , base ^>= 4.14.1.0 + , req ^>= 3.9.0 + , uuid