2021-05-23 16:37:21 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
|
|
|
{-# LANGUAGE NumericUnderscores #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
|
|
|
|
module Week08.TraceTokenSale where
|
|
|
|
|
|
|
|
import Control.Monad hiding (fmap)
|
|
|
|
import Control.Monad.Freer.Extras as Extras
|
|
|
|
import Data.Default (Default (..))
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import Data.Monoid (Last (..))
|
|
|
|
import Ledger
|
|
|
|
import Ledger.Value
|
|
|
|
import Ledger.Ada as Ada
|
|
|
|
import Plutus.Trace.Emulator as Emulator
|
|
|
|
import PlutusTx.Prelude
|
2021-05-25 21:31:41 +02:00
|
|
|
import Prelude (IO, String, Show (..))
|
2021-05-23 16:37:21 +02:00
|
|
|
import Wallet.Emulator.Wallet
|
|
|
|
|
|
|
|
import Week08.TokenSale
|
|
|
|
|
|
|
|
test :: IO ()
|
|
|
|
test = runEmulatorTraceIO' def emCfg myTrace
|
|
|
|
where
|
|
|
|
emCfg :: EmulatorConfig
|
|
|
|
emCfg = EmulatorConfig $ Left $ Map.fromList
|
|
|
|
[ (Wallet w, v)
|
|
|
|
| w <- [1 .. 3]
|
|
|
|
]
|
|
|
|
|
|
|
|
v :: Value
|
2021-05-25 22:49:40 +02:00
|
|
|
v = Ada.lovelaceValueOf 1000_000_000 <> assetClassValue token 1000
|
2021-05-23 16:37:21 +02:00
|
|
|
|
2021-05-25 22:49:40 +02:00
|
|
|
currency :: CurrencySymbol
|
|
|
|
currency = "aa"
|
2021-05-23 16:37:21 +02:00
|
|
|
|
2021-05-25 22:49:40 +02:00
|
|
|
name :: TokenName
|
|
|
|
name = "A"
|
2021-05-23 16:37:21 +02:00
|
|
|
|
2021-05-25 22:49:40 +02:00
|
|
|
token :: AssetClass
|
|
|
|
token = AssetClass (currency, name)
|
2021-05-23 16:37:21 +02:00
|
|
|
|
|
|
|
myTrace :: EmulatorTrace ()
|
|
|
|
myTrace = do
|
2021-05-25 22:49:40 +02:00
|
|
|
h <- activateContractWallet (Wallet 1) startEndpoint
|
|
|
|
callEndpoint @"start" h (currency, name)
|
2021-05-23 16:37:21 +02:00
|
|
|
void $ Emulator.waitNSlots 5
|
2021-05-25 22:49:40 +02:00
|
|
|
Last m <- observableState h
|
2021-05-23 16:37:21 +02:00
|
|
|
case m of
|
|
|
|
Nothing -> Extras.logError @String "error starting token sale"
|
|
|
|
Just ts -> do
|
|
|
|
Extras.logInfo $ "started token sale " ++ show ts
|
|
|
|
|
2021-05-25 22:49:40 +02:00
|
|
|
h1 <- activateContractWallet (Wallet 1) $ useEndpoints ts
|
|
|
|
h2 <- activateContractWallet (Wallet 2) $ useEndpoints ts
|
|
|
|
h3 <- activateContractWallet (Wallet 3) $ useEndpoints ts
|
2021-05-23 16:37:21 +02:00
|
|
|
|
|
|
|
callEndpoint @"set price" h1 1_000_000
|
|
|
|
void $ Emulator.waitNSlots 5
|
|
|
|
|
|
|
|
callEndpoint @"add tokens" h1 100
|
|
|
|
void $ Emulator.waitNSlots 5
|
|
|
|
|
|
|
|
callEndpoint @"buy tokens" h2 20
|
|
|
|
void $ Emulator.waitNSlots 5
|
|
|
|
|
|
|
|
callEndpoint @"buy tokens" h3 5
|
|
|
|
void $ Emulator.waitNSlots 5
|
|
|
|
|
|
|
|
callEndpoint @"withdraw" h1 (40, 10_000_000)
|
|
|
|
void $ Emulator.waitNSlots 5
|