-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCP-2372: Uniswap trace and scripts (#3370)
* SCP-2372: Uniswap trace and scripts
- Loading branch information
Showing
12 changed files
with
148 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
nix/pkgs/haskell/materialized-unix/.plan.nix/plutus-use-cases.nix
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-| Example trace for the uniswap contract | ||
-} | ||
module Plutus.Contracts.Uniswap.Trace( | ||
uniswapTrace | ||
-- | ||
, setupTokens | ||
, tokenNames | ||
, wallets | ||
) where | ||
|
||
import Control.Monad (forM_, void, when) | ||
import Control.Monad.Freer.Error (throwError) | ||
import qualified Data.Map as Map | ||
import qualified Data.Monoid as Monoid | ||
import qualified Data.Semigroup as Semigroup | ||
import Ledger | ||
import Ledger.Ada (adaSymbol, adaToken) | ||
import Ledger.Constraints | ||
import Ledger.Value as Value | ||
import Plutus.Contract hiding (throwError, when) | ||
import qualified Plutus.Contracts.Currency as Currency | ||
import Plutus.Contracts.Uniswap.OffChain as OffChain | ||
import Plutus.Contracts.Uniswap.Types as Types | ||
import Plutus.Trace.Emulator (EmulatorRuntimeError (GenericError), EmulatorTrace) | ||
import qualified Plutus.Trace.Emulator as Emulator | ||
import Wallet.Emulator.Types (Wallet (..), walletPubKey) | ||
|
||
-- | Set up a liquidity pool and call the "add" endpoint | ||
uniswapTrace :: EmulatorTrace () | ||
uniswapTrace = do | ||
cidInit <- Emulator.activateContract (Wallet 1) setupTokens "init" | ||
_ <- Emulator.waitNSlots 5 | ||
cs <- Emulator.observableState cidInit >>= \case | ||
Just (Semigroup.Last cur) -> pure (Currency.currencySymbol cur) | ||
_ -> throwError $ GenericError "failed to create currency" | ||
let coins = Map.fromList [(tn, Types.mkCoin cs tn) | tn <- tokenNames] | ||
ada = Types.mkCoin adaSymbol adaToken | ||
|
||
cidStart <- Emulator.activateContract (Wallet 1) ownerEndpoint "start" | ||
_ <- Emulator.waitNSlots 5 | ||
us <- Emulator.observableState cidStart >>= \case | ||
Monoid.Last (Just (Right v)) -> pure v | ||
_ -> throwError $ GenericError "initialisation failed" | ||
cid1 <- Emulator.activateContractWallet (Wallet 2) (userEndpoints us) | ||
cid2 <- Emulator.activateContractWallet (Wallet 3) (userEndpoints us) | ||
_ <- Emulator.waitNSlots 5 | ||
|
||
let cp = OffChain.CreateParams ada (coins Map.! "A") 100000 500000 | ||
|
||
Emulator.callEndpoint @"create" cid1 cp | ||
_ <- Emulator.waitNSlots 5 | ||
|
||
let ap = AddParams{apCoinA = ada, apCoinB = coins Map.! "A", apAmountA = 1000, apAmountB = 5000} | ||
Emulator.callEndpoint @"add" cid2 ap | ||
_ <- Emulator.waitNSlots 5 | ||
pure () | ||
|
||
-- | Create some sample tokens and distribute them to | ||
-- the emulated wallets | ||
setupTokens :: Contract (Maybe (Semigroup.Last Currency.OneShotCurrency)) Currency.CurrencySchema Currency.CurrencyError () | ||
setupTokens = do | ||
ownPK <- pubKeyHash <$> ownPubKey | ||
cur <- Currency.forgeContract ownPK [(tn, fromIntegral (length wallets) * amount) | tn <- tokenNames] | ||
let cs = Currency.currencySymbol cur | ||
v = mconcat [Value.singleton cs tn amount | tn <- tokenNames] | ||
forM_ wallets $ \w -> do | ||
let pkh = pubKeyHash $ walletPubKey w | ||
when (pkh /= ownPK) $ do | ||
tx <- submitTx $ mustPayToPubKey pkh v | ||
awaitTxConfirmed $ txId tx | ||
tell $ Just $ Semigroup.Last cur | ||
void $ waitNSlots 10 | ||
where | ||
amount = 1000000 | ||
|
||
wallets :: [Wallet] | ||
wallets = [Wallet i | i <- [1 .. 4]] | ||
|
||
tokenNames :: [TokenName] | ||
tokenNames = ["A", "B", "C", "D"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Spec.Uniswap( | ||
tests | ||
) where | ||
|
||
import Plutus.Contract.Test | ||
import qualified Plutus.Contracts.Uniswap.Trace as Uniswap | ||
import Test.Tasty | ||
|
||
tests :: TestTree | ||
tests = testGroup "uniswap" [ | ||
checkPredicate "can create a liquidity pool and add liquidity" | ||
assertNoFailedTransactions | ||
Uniswap.uniswapTrace | ||
] |