2021-06-07 22:49:01 +02:00
|
|
|
{-# LANGUAGE NumericUnderscores #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
|
|
|
module Main
|
|
|
|
( main
|
|
|
|
) where
|
|
|
|
|
2021-06-08 00:16:05 +02:00
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Exception
|
2021-06-08 23:35:13 +02:00
|
|
|
import Control.Monad (when)
|
2021-06-08 00:16:05 +02:00
|
|
|
import Control.Monad.IO.Class (MonadIO (..))
|
2021-06-08 23:35:13 +02:00
|
|
|
import Data.Aeson (Result (..), ToJSON, decode, fromJSON)
|
2021-06-08 00:16:05 +02:00
|
|
|
import qualified Data.ByteString.Lazy as LB
|
|
|
|
import Data.Monoid (Last (..))
|
|
|
|
import Data.Proxy (Proxy (..))
|
2021-06-08 23:35:13 +02:00
|
|
|
import Data.String (IsString (..))
|
2021-06-08 10:38:44 +02:00
|
|
|
import Data.Text (Text, pack)
|
2021-06-08 23:35:13 +02:00
|
|
|
import Data.UUID hiding (fromString)
|
|
|
|
import Ledger.Value (AssetClass (..), CurrencySymbol, flattenValue)
|
2021-06-08 00:16:05 +02:00
|
|
|
import Network.HTTP.Req
|
2021-06-08 22:27:06 +02:00
|
|
|
import qualified Plutus.Contracts.Uniswap as US
|
2021-06-08 00:16:05 +02:00
|
|
|
import Plutus.PAB.Events.ContractInstanceState (PartiallyDecodedResponse (..))
|
|
|
|
import Plutus.PAB.Webserver.Types
|
|
|
|
import System.Environment (getArgs)
|
|
|
|
import System.Exit (exitFailure)
|
|
|
|
import Text.Read (readMaybe)
|
|
|
|
import Wallet.Emulator.Types (Wallet (..))
|
|
|
|
|
2021-06-08 10:38:44 +02:00
|
|
|
import Uniswap (cidFile, UniswapContracts)
|
2021-06-07 22:49:01 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2021-06-08 00:16:05 +02:00
|
|
|
w <- Wallet . read . head <$> getArgs
|
|
|
|
cid <- read <$> readFile (cidFile w)
|
2021-06-08 10:45:43 +02:00
|
|
|
mcs <- decode <$> LB.readFile "symbol.json"
|
2021-06-08 23:35:13 +02:00
|
|
|
case mcs of
|
|
|
|
Nothing -> putStrLn "invalid symbol.json" >> exitFailure
|
|
|
|
Just cs -> do
|
2021-06-08 10:38:44 +02:00
|
|
|
putStrLn $ "cid: " ++ show cid
|
2021-06-08 10:45:43 +02:00
|
|
|
putStrLn $ "symbol: " ++ show (cs :: CurrencySymbol)
|
2021-06-08 23:35:13 +02:00
|
|
|
go cid cs
|
2021-06-08 22:27:06 +02:00
|
|
|
where
|
2021-06-08 23:35:13 +02:00
|
|
|
go :: UUID -> CurrencySymbol -> IO a
|
|
|
|
go cid cs = do
|
2021-06-08 22:27:06 +02:00
|
|
|
cmd <- readCommandIO
|
|
|
|
case cmd of
|
2021-06-08 23:35:13 +02:00
|
|
|
Funds -> getFunds cid
|
|
|
|
Pools -> getPools cid
|
|
|
|
Create amtA tnA amtB tnB -> createPool cid $ toCreateParams cs amtA tnA amtB tnB
|
|
|
|
go cid cs
|
2021-06-08 22:27:06 +02:00
|
|
|
|
2021-06-08 23:35:13 +02:00
|
|
|
data Command =
|
|
|
|
Funds
|
|
|
|
| Pools
|
|
|
|
| Create Integer Char Integer Char
|
2021-06-08 22:27:06 +02:00
|
|
|
deriving (Show, Read, Eq, Ord)
|
|
|
|
|
|
|
|
readCommandIO :: IO Command
|
|
|
|
readCommandIO = do
|
2021-06-08 23:35:13 +02:00
|
|
|
putStrLn "Enter a command: Funds, Pools, Create amtA tnA amtB tnB"
|
2021-06-08 22:27:06 +02:00
|
|
|
s <- getLine
|
|
|
|
maybe readCommandIO return $ readMaybe s
|
2021-06-08 10:38:44 +02:00
|
|
|
|
2021-06-08 23:35:13 +02:00
|
|
|
toCreateParams :: CurrencySymbol -> Integer -> Char -> Integer -> Char -> US.CreateParams
|
|
|
|
toCreateParams cs amtA tnA amtB tnB = US.CreateParams (toCoin tnA) (toCoin tnB) (US.Amount amtA) (US.Amount amtB)
|
|
|
|
where
|
|
|
|
toCoin :: Char -> US.Coin c
|
|
|
|
toCoin tn = US.Coin $ AssetClass (cs, fromString [tn])
|
|
|
|
|
2021-06-08 10:38:44 +02:00
|
|
|
getFunds :: UUID -> IO ()
|
2021-06-08 23:35:13 +02:00
|
|
|
getFunds cid = do
|
|
|
|
callEndpoint cid "funds" ()
|
|
|
|
threadDelay 2_000_000
|
|
|
|
go
|
2021-06-08 22:27:06 +02:00
|
|
|
where
|
2021-06-08 23:35:13 +02:00
|
|
|
go = do
|
|
|
|
e <- getStatus cid
|
|
|
|
case e of
|
|
|
|
Right (US.Funds v) -> putStrLn $ "funds: " ++ show (flattenValue v)
|
|
|
|
_ -> go
|
2021-06-08 22:27:06 +02:00
|
|
|
|
|
|
|
getPools :: UUID -> IO ()
|
2021-06-08 23:35:13 +02:00
|
|
|
getPools cid = do
|
|
|
|
callEndpoint cid "pools" ()
|
|
|
|
threadDelay 2_000_000
|
|
|
|
go
|
2021-06-08 10:38:44 +02:00
|
|
|
where
|
2021-06-08 23:35:13 +02:00
|
|
|
go = do
|
|
|
|
e <- getStatus cid
|
|
|
|
case e of
|
|
|
|
Right (US.Pools ps) -> putStrLn $ "pools: " ++ show ps
|
|
|
|
_ -> go
|
2021-06-07 22:49:01 +02:00
|
|
|
|
2021-06-08 23:35:13 +02:00
|
|
|
createPool :: UUID -> US.CreateParams -> IO ()
|
|
|
|
createPool cid cps = do
|
|
|
|
callEndpoint cid "create" cps
|
|
|
|
threadDelay 2_000_000
|
|
|
|
go
|
2021-06-07 22:49:01 +02:00
|
|
|
where
|
2021-06-08 23:35:13 +02:00
|
|
|
go = do
|
|
|
|
e <- getStatus cid
|
|
|
|
case e of
|
|
|
|
Right US.Created -> putStrLn "created"
|
|
|
|
Left err' -> putStrLn $ "error: " ++ show err'
|
|
|
|
_ -> go
|
2021-06-07 22:49:01 +02:00
|
|
|
|
2021-06-08 23:35:13 +02:00
|
|
|
getStatus :: UUID -> IO (Either Text US.UserContractState)
|
|
|
|
getStatus cid = runReq defaultHttpConfig $ do
|
|
|
|
w <- req
|
|
|
|
GET
|
|
|
|
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show cid) /: "status")
|
|
|
|
NoReqBody
|
|
|
|
(Proxy :: Proxy (JsonResponse (ContractInstanceClientState UniswapContracts)))
|
2021-06-07 22:49:01 +02:00
|
|
|
(port 8080)
|
2021-06-08 23:35:13 +02:00
|
|
|
case fromJSON $ observableState $ cicCurrentState $ responseBody w of
|
|
|
|
Success (Last Nothing) -> liftIO $ threadDelay 1_000_000 >> getStatus cid
|
|
|
|
Success (Last (Just e)) -> return e
|
|
|
|
_ -> liftIO $ ioError $ userError "error decoding state"
|
2021-06-07 22:49:01 +02:00
|
|
|
|
2021-06-08 23:35:13 +02:00
|
|
|
callEndpoint :: ToJSON a => UUID -> String -> a -> IO ()
|
|
|
|
callEndpoint cid name a = handle h $ runReq defaultHttpConfig $ do
|
2021-06-07 22:49:01 +02:00
|
|
|
v <- req
|
|
|
|
POST
|
2021-06-08 23:35:13 +02:00
|
|
|
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: pack (show cid) /: "endpoint" /: pack name)
|
|
|
|
(ReqBodyJson a)
|
2021-06-07 22:49:01 +02:00
|
|
|
(Proxy :: Proxy (JsonResponse ()))
|
|
|
|
(port 8080)
|
2021-06-08 23:35:13 +02:00
|
|
|
when (responseStatusCode v /= 200) $
|
|
|
|
liftIO $ ioError $ userError $ "error calling endpoint " ++ name
|
2021-06-07 22:49:01 +02:00
|
|
|
where
|
|
|
|
h :: HttpException -> IO ()
|
2021-06-08 23:35:13 +02:00
|
|
|
h = ioError . userError . show
|