From 24e3596598df9d1c603d7e46e10477ed3b83ef64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Br=C3=BCnjes?= Date: Wed, 9 Jun 2021 13:54:29 +0200 Subject: [PATCH] implemented swap --- code/week10/app/uniswap-client.hs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/code/week10/app/uniswap-client.hs b/code/week10/app/uniswap-client.hs index 94f7f9f..35229ee 100644 --- a/code/week10/app/uniswap-client.hs +++ b/code/week10/app/uniswap-client.hs @@ -48,12 +48,14 @@ main = do Funds -> getFunds cid Pools -> getPools cid Create amtA tnA amtB tnB -> createPool cid $ toCreateParams cs amtA tnA amtB tnB + Swap amtA tnA tnB -> swap cid $ toSwapParams cs amtA tnA tnB go cid cs data Command = Funds | Pools | Create Integer Char Integer Char + | Swap Integer Char Char deriving (Show, Read, Eq, Ord) readCommandIO :: IO Command @@ -62,11 +64,14 @@ readCommandIO = do s <- getLine maybe readCommandIO return $ readMaybe s +toCoin :: CurrencySymbol -> Char -> US.Coin c +toCoin cs tn = US.Coin $ AssetClass (cs, fromString [tn]) + 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]) +toCreateParams cs amtA tnA amtB tnB = US.CreateParams (toCoin cs tnA) (toCoin cs tnB) (US.Amount amtA) (US.Amount amtB) + +toSwapParams :: CurrencySymbol -> Integer -> Char -> Char -> US.SwapParams +toSwapParams cs amtA tnA tnB = US.SwapParams (toCoin cs tnA) (toCoin cs tnB) (US.Amount amtA) (US.Amount 0) getFunds :: UUID -> IO () getFunds cid = do @@ -93,8 +98,8 @@ getPools cid = do _ -> go createPool :: UUID -> US.CreateParams -> IO () -createPool cid cps = do - callEndpoint cid "create" cps +createPool cid cp = do + callEndpoint cid "create" cp threadDelay 2_000_000 go where @@ -105,6 +110,19 @@ createPool cid cps = do Left err' -> putStrLn $ "error: " ++ show err' _ -> go +swap :: UUID -> US.SwapParams -> IO () +swap cid sp = do + callEndpoint cid "swap" sp + threadDelay 2_000_000 + go + where + go = do + e <- getStatus cid + case e of + Right US.Swapped -> putStrLn "swapped" + Left err' -> putStrLn $ "error: " ++ show err' + _ -> go + getStatus :: UUID -> IO (Either Text US.UserContractState) getStatus cid = runReq defaultHttpConfig $ do w <- req