mirror of
https://github.com/FiloSpaTeam/plutus-pioneer-program.git
synced 2024-11-22 06:42:00 +01:00
refactored funds
This commit is contained in:
parent
2643aa2cb2
commit
395ebef4a8
6 changed files with 57 additions and 27 deletions
|
@ -43,6 +43,8 @@ main = void $ Simulator.runSimulationWith handlers $ do
|
||||||
Simulator.logString @(Builtin OracleContracts) "Starting Oracle PAB webserver on port 8080. Press enter to exit."
|
Simulator.logString @(Builtin OracleContracts) "Starting Oracle PAB webserver on port 8080. Press enter to exit."
|
||||||
shutdown <- PAB.Server.startServerDebug
|
shutdown <- PAB.Server.startServerDebug
|
||||||
|
|
||||||
|
cidOracle <- Simulator.activateContract (Wallet 1) Oracle
|
||||||
|
|
||||||
void $ liftIO getLine
|
void $ liftIO getLine
|
||||||
shutdown
|
shutdown
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ License-files: LICENSE
|
||||||
library
|
library
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
exposed-modules: Week06.Oracle.Core
|
exposed-modules: Week06.Oracle.Core
|
||||||
|
Week06.Oracle.Funds
|
||||||
Week06.Oracle.Playground
|
Week06.Oracle.Playground
|
||||||
Week06.Oracle.Swap
|
Week06.Oracle.Swap
|
||||||
Week06.Oracle.Test
|
Week06.Oracle.Test
|
||||||
|
|
|
@ -152,7 +152,7 @@ startOracle op = do
|
||||||
, oFee = opFees op
|
, oFee = opFees op
|
||||||
, oAsset = AssetClass (opSymbol op, opToken op)
|
, oAsset = AssetClass (opSymbol op, opToken op)
|
||||||
}
|
}
|
||||||
logInfo @String $ "forged oracle state token for oracle " ++ show oracle
|
logInfo @String $ "started oracle " ++ show oracle
|
||||||
return oracle
|
return oracle
|
||||||
|
|
||||||
updateOracle :: forall w s. HasBlockchainActions s => Oracle -> Integer -> Contract w s Text ()
|
updateOracle :: forall w s. HasBlockchainActions s => Oracle -> Integer -> Contract w s Text ()
|
||||||
|
|
41
code/week06/src/Week06/Oracle/Funds.hs
Normal file
41
code/week06/src/Week06/Oracle/Funds.hs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
{-# LANGUAGE DataKinds #-}
|
||||||
|
{-# LANGUAGE DeriveAnyClass #-}
|
||||||
|
{-# LANGUAGE DeriveGeneric #-}
|
||||||
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
|
{-# LANGUAGE NoImplicitPrelude #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
{-# LANGUAGE TemplateHaskell #-}
|
||||||
|
{-# LANGUAGE TypeApplications #-}
|
||||||
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
|
{-# LANGUAGE TypeOperators #-}
|
||||||
|
|
||||||
|
module Week06.Oracle.Funds
|
||||||
|
( ownFunds
|
||||||
|
, ownFunds'
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Control.Monad hiding (fmap)
|
||||||
|
import qualified Data.Map as Map
|
||||||
|
import Data.Monoid (Last (..))
|
||||||
|
import Data.Text (Text)
|
||||||
|
import Plutus.Contract as Contract hiding (when)
|
||||||
|
import PlutusTx.Prelude hiding ((<$>))
|
||||||
|
import Prelude ((<$>))
|
||||||
|
import Ledger hiding (singleton)
|
||||||
|
import Ledger.Value as Value
|
||||||
|
|
||||||
|
ownFunds :: HasBlockchainActions s => Contract w s Text Value
|
||||||
|
ownFunds = do
|
||||||
|
pk <- ownPubKey
|
||||||
|
utxos <- utxoAt $ pubKeyAddress pk
|
||||||
|
let v = mconcat $ Map.elems $ txOutValue . txOutTxOut <$> utxos
|
||||||
|
logInfo @String $ "own funds: " ++ show (Value.flattenValue v)
|
||||||
|
return v
|
||||||
|
|
||||||
|
ownFunds' :: Contract (Last Value) BlockchainActions Text ()
|
||||||
|
ownFunds' = do
|
||||||
|
handleError logError $ ownFunds >>= tell . Last . Just
|
||||||
|
void $ Contract.waitNSlots 1
|
||||||
|
ownFunds'
|
|
@ -20,7 +20,6 @@ import Control.Monad hiding (fmap)
|
||||||
import Data.List (find)
|
import Data.List (find)
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Maybe (mapMaybe)
|
import Data.Maybe (mapMaybe)
|
||||||
import Data.Monoid (Last (..))
|
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Plutus.Contract as Contract hiding (when)
|
import Plutus.Contract as Contract hiding (when)
|
||||||
import qualified PlutusTx
|
import qualified PlutusTx
|
||||||
|
@ -33,6 +32,7 @@ import Ledger.Value as Value
|
||||||
import Prelude (Semigroup (..), (<$>))
|
import Prelude (Semigroup (..), (<$>))
|
||||||
|
|
||||||
import Week06.Oracle.Core
|
import Week06.Oracle.Core
|
||||||
|
import Week06.Oracle.Funds
|
||||||
|
|
||||||
{-# INLINABLE price #-}
|
{-# INLINABLE price #-}
|
||||||
price :: Integer -> Integer -> Integer
|
price :: Integer -> Integer -> Integer
|
||||||
|
@ -153,14 +153,6 @@ retrieveSwaps oracle = do
|
||||||
awaitTxConfirmed $ txId ledgerTx
|
awaitTxConfirmed $ txId ledgerTx
|
||||||
logInfo @String $ "retrieved " ++ show (length xs) ++ " swap(s)"
|
logInfo @String $ "retrieved " ++ show (length xs) ++ " swap(s)"
|
||||||
|
|
||||||
ownFunds :: HasBlockchainActions s => Contract w s Text Value
|
|
||||||
ownFunds = do
|
|
||||||
pk <- ownPubKey
|
|
||||||
utxos <- utxoAt $ pubKeyAddress pk
|
|
||||||
let v = mconcat $ Map.elems $ txOutValue . txOutTxOut <$> utxos
|
|
||||||
logInfo @String $ "own funds: " ++ show (Value.flattenValue v)
|
|
||||||
return v
|
|
||||||
|
|
||||||
useSwap :: forall w s. HasBlockchainActions s => Oracle -> Contract w s Text ()
|
useSwap :: forall w s. HasBlockchainActions s => Oracle -> Contract w s Text ()
|
||||||
useSwap oracle = do
|
useSwap oracle = do
|
||||||
funds <- ownFunds
|
funds <- ownFunds
|
||||||
|
@ -204,31 +196,24 @@ type SwapSchema =
|
||||||
.\/ Endpoint "offer" Integer
|
.\/ Endpoint "offer" Integer
|
||||||
.\/ Endpoint "retrieve" ()
|
.\/ Endpoint "retrieve" ()
|
||||||
.\/ Endpoint "use" ()
|
.\/ Endpoint "use" ()
|
||||||
.\/ Endpoint "funds" ()
|
|
||||||
|
|
||||||
swap :: Oracle -> Contract (Last Value) SwapSchema Text ()
|
swap :: Oracle -> Contract () SwapSchema Text ()
|
||||||
swap oracle = (offer `select` retrieve `select` use `select` funds) >> swap oracle
|
swap oracle = (offer `select` retrieve `select` use) >> swap oracle
|
||||||
where
|
where
|
||||||
offer :: Contract (Last Value) SwapSchema Text ()
|
offer :: Contract () SwapSchema Text ()
|
||||||
offer = h $ do
|
offer = h $ do
|
||||||
amt <- endpoint @"offer"
|
amt <- endpoint @"offer"
|
||||||
offerSwap oracle amt
|
offerSwap oracle amt
|
||||||
|
|
||||||
retrieve :: Contract (Last Value) SwapSchema Text ()
|
retrieve :: Contract () SwapSchema Text ()
|
||||||
retrieve = h $ do
|
retrieve = h $ do
|
||||||
endpoint @"retrieve"
|
endpoint @"retrieve"
|
||||||
retrieveSwaps oracle
|
retrieveSwaps oracle
|
||||||
|
|
||||||
use :: Contract (Last Value) SwapSchema Text ()
|
use :: Contract () SwapSchema Text ()
|
||||||
use = h $ do
|
use = h $ do
|
||||||
endpoint @"use"
|
endpoint @"use"
|
||||||
useSwap oracle
|
useSwap oracle
|
||||||
|
|
||||||
funds :: Contract (Last Value) SwapSchema Text ()
|
h :: Contract () SwapSchema Text () -> Contract () SwapSchema Text ()
|
||||||
funds = h $ do
|
|
||||||
endpoint @"funds"
|
|
||||||
v <- ownFunds
|
|
||||||
tell $ Last $ Just v
|
|
||||||
|
|
||||||
h :: Contract (Last Value) SwapSchema Text () -> Contract (Last Value) SwapSchema Text ()
|
|
||||||
h = handleError logError
|
h = handleError logError
|
||||||
|
|
|
@ -30,6 +30,7 @@ import Prelude (Semigroup(..))
|
||||||
import Wallet.Emulator.Wallet
|
import Wallet.Emulator.Wallet
|
||||||
|
|
||||||
import Week06.Oracle.Core
|
import Week06.Oracle.Core
|
||||||
|
import Week06.Oracle.Funds
|
||||||
import Week06.Oracle.Swap
|
import Week06.Oracle.Swap
|
||||||
|
|
||||||
assetSymbol :: CurrencySymbol
|
assetSymbol :: CurrencySymbol
|
||||||
|
@ -73,6 +74,10 @@ myTrace = do
|
||||||
callEndpoint @"update" h1 1_500_000
|
callEndpoint @"update" h1 1_500_000
|
||||||
void $ Emulator.waitNSlots 3
|
void $ Emulator.waitNSlots 3
|
||||||
|
|
||||||
|
void $ activateContractWallet (Wallet 3) ownFunds'
|
||||||
|
void $ activateContractWallet (Wallet 4) ownFunds'
|
||||||
|
void $ activateContractWallet (Wallet 5) ownFunds'
|
||||||
|
|
||||||
h3 <- activateContractWallet (Wallet 3) $ swap oracle
|
h3 <- activateContractWallet (Wallet 3) $ swap oracle
|
||||||
h4 <- activateContractWallet (Wallet 4) $ swap oracle
|
h4 <- activateContractWallet (Wallet 4) $ swap oracle
|
||||||
h5 <- activateContractWallet (Wallet 5) $ swap oracle
|
h5 <- activateContractWallet (Wallet 5) $ swap oracle
|
||||||
|
@ -96,10 +101,6 @@ myTrace = do
|
||||||
callEndpoint @"retrieve" h3 ()
|
callEndpoint @"retrieve" h3 ()
|
||||||
callEndpoint @"retrieve" h4 ()
|
callEndpoint @"retrieve" h4 ()
|
||||||
void $ Emulator.waitNSlots 3
|
void $ Emulator.waitNSlots 3
|
||||||
|
|
||||||
callEndpoint @"funds" h3 ()
|
|
||||||
callEndpoint @"funds" h4 ()
|
|
||||||
callEndpoint @"funds" h5 ()
|
|
||||||
where
|
where
|
||||||
getOracle :: ContractHandle (Last Oracle) OracleSchema Text -> EmulatorTrace Oracle
|
getOracle :: ContractHandle (Last Oracle) OracleSchema Text -> EmulatorTrace Oracle
|
||||||
getOracle h = do
|
getOracle h = do
|
||||||
|
|
Loading…
Reference in a new issue