plutus-pioneer-program/code/week06/app/swap-client.hs

87 lines
3.2 KiB
Haskell
Raw Normal View History

2021-05-10 22:41:10 +02:00
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
2021-05-10 21:47:35 +02:00
module Main
( main
) where
2021-05-10 22:41:10 +02:00
import Control.Concurrent
import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO (..))
import Data.Aeson (Result (..), fromJSON)
import Data.Monoid (Last (..))
import Data.Proxy (Proxy (..))
import Data.Text (pack)
import Data.UUID
import Ledger.Value (flattenValue)
import Network.HTTP.Req
import Plutus.PAB.Events.ContractInstanceState (PartiallyDecodedResponse (..))
import Plutus.PAB.Webserver.Types
import System.Environment (getArgs)
import Week06.Oracle.PAB (OracleContracts)
2021-05-10 21:47:35 +02:00
main :: IO ()
2021-05-10 22:41:10 +02:00
main = do
[(i :: Int)] <- map read <$> getArgs
uuid <- read <$> readFile ('W' : show i ++ ".cid")
putStrLn $ "swap contract instance id for Wallet " ++ show i ++ ": " ++ show uuid
getFunds uuid
getFunds :: UUID -> IO ()
getFunds uuid = runReq defaultHttpConfig $ do
v <- req
POST
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "endpoint" /: "funds")
(ReqBodyJson ())
(Proxy :: Proxy (JsonResponse ()))
(port 8080)
if responseStatusCode v /= 200
then liftIO $ putStrLn "error getting funds"
else do
w <- req
GET
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show uuid) /: "status")
NoReqBody
(Proxy :: Proxy (JsonResponse (ContractInstanceClientState OracleContracts)))
(port 8080)
liftIO $ putStrLn $ case fromJSON $ observableState $ cicCurrentState $ responseBody w of
Success (Last (Just f)) -> "fund: " ++ show (flattenValue f)
_ -> "error decoding state"
{-
liftIO $ putStrLn $ if responseStatusCode v == 200
then "updated oracle to " ++ show x
else "error updating oracle"
-}
{-
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
-}