Skip to content

Commit

Permalink
Rename a function; fix stylish haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mueller committed Jul 19, 2023
1 parent 13ad5a2 commit 38d1ad8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

* Update `queryTip` and `queryTipSlotNo` in `Convex.Devnet.NodeQueries` to also return `SlotLength`.
* Rename `toUtxoTx` to `fromUtxoTx` in `Convex.Utxos`

### Added

Expand Down
6 changes: 3 additions & 3 deletions src/base/lib/Convex/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import Cardano.Api.Shelley (BabbageEra,
TxId)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Shelley.API (UTxO)
import Cardano.Slotting.Time (SystemStart,
SlotLength)
import Cardano.Slotting.Time (SlotLength,
SystemStart)
import Control.Lens (_1, view)
import Control.Monad.Except (MonadError)
import Control.Monad.IO.Class (MonadIO (..))
Expand Down Expand Up @@ -254,7 +254,7 @@ instance (MonadFail m, MonadLog m, MonadIO m) => MonadBlockchain (MonadBlockchai
fail msg
(eraHistory@(EraHistory _ interpreter), systemStart) <- (,) <$> queryEraHistory <*> querySystemStart
slotNo <- runQuery (C.QueryChainPoint C.CardanoMode) >>= \case
C.ChainPointAtGenesis -> pure $ fromIntegral (0 :: Integer)
C.ChainPointAtGenesis -> pure $ fromIntegral (0 :: Integer)
C.ChainPoint slot _hsh -> pure slot
utctime <- either logErr pure (slotToUtcTime eraHistory systemStart slotNo)
either (logErr . show) (\l -> pure (slotNo, l, utctime)) (interpretQuery interpreter $ slotToSlotLength slotNo)
Expand Down
8 changes: 4 additions & 4 deletions src/base/lib/Convex/Utxos.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
module Convex.Utxos(
-- * Utxo sets
UtxoSet(..),
toUtxoTx,
fromUtxoTx,
singleton,
PrettyBalance(..),
_UtxoSet,
Expand Down Expand Up @@ -110,10 +110,10 @@ deriving instance (ToJSON a, ToJSON (C.TxOut ctx C.BabbageEra)) => ToJSON (UtxoS
singleton :: TxIn -> (C.TxOut ctx C.BabbageEra, a) -> UtxoSet ctx a
singleton txi = UtxoSet . Map.singleton txi

{-| Change the context of the outputs in this utxo set
{-| Change the context of the outputs in this utxo set to 'CtxUTxO'
-}
toUtxoTx :: UtxoSet C.CtxTx a -> UtxoSet C.CtxUTxO a
toUtxoTx = UtxoSet . fmap (first C.toCtxUTxOTxOut) . _utxos
fromUtxoTx :: UtxoSet C.CtxTx a -> UtxoSet C.CtxUTxO a
fromUtxoTx = UtxoSet . fmap (first C.toCtxUTxOTxOut) . _utxos

makePrisms ''UtxoSet

Expand Down
3 changes: 2 additions & 1 deletion src/coin-selection/convex-coin-selection.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ test-suite convex-coin-selection-test
convex-mockchain,
convex-base,
convex-wallet,
cardano-api
cardano-api,
containers
20 changes: 17 additions & 3 deletions src/coin-selection/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Main(main) where

import qualified Cardano.Api.Shelley as C
import Control.Lens (mapped, over, (&))
import Control.Lens (_4, mapped, over, view, (&))
import Control.Monad (void)
import Convex.BuildTx (assetValue, mintPlutusV1,
payToAddress, payToPlutusV1,
Expand All @@ -24,6 +24,8 @@ import Convex.MockChain.Utils (mockchainSucceeds)
import Convex.Wallet (Wallet)
import qualified Convex.Wallet as Wallet
import qualified Convex.Wallet.MockWallet as Wallet
import qualified Data.Map as Map
import qualified Data.Set as Set
import Test.Tasty (TestTree, defaultMain,
testGroup)
import Test.Tasty.HUnit (Assertion, testCase)
Expand All @@ -40,6 +42,7 @@ tests = testGroup "unit tests"
, testGroup "scripts"
[ testCase "paying to a plutus script" (mockchainSucceeds payToPlutusScript)
, testCase "spending a plutus script output" (mockchainSucceeds (payToPlutusScript >>= spendPlutusScript))
, testCase "creating a reference script output" (mockchainSucceeds $ putReferenceScript Wallet.w1)
, testCase "using a reference script" (mockchainSucceeds (payToPlutusScript >>= spendPlutusScriptReference))
, testCase "minting a token" (mockchainSucceeds mintingPlutus)
, testCase "making payments with tokens" (mockchainSucceeds (mintingPlutus >>= spendTokens))
Expand Down Expand Up @@ -75,11 +78,22 @@ spendPlutusScript ref = do

putReferenceScript :: Wallet -> Mockchain C.TxIn
putReferenceScript wallet = do
let s = C.toScriptInAnyLang $ C.PlutusScript C.PlutusScriptV1 txInscript
let tx = emptyTx
& payToPlutusV1Inline (Wallet.addressInEra Defaults.networkId wallet) txInscript (C.lovelaceToValue 1_000_000)
& payToPlutusV1Inline (Wallet.addressInEra Defaults.networkId wallet) txInscript (C.lovelaceToValue 10_000_000)
& setMinAdaDepositAll Defaults.ledgerProtocolParameters
txId <- C.getTxId . C.getTxBody <$> balanceAndSubmit wallet tx
pure (C.TxIn txId (C.TxIx 0))
let outRef = C.TxIn txId (C.TxIx 0)
case C.toScriptInEra C.BabbageEra s of
Nothing -> fail "toScriptInEra: Nothing"
_ -> pure ()
C.UTxO utxo <- utxoByTxIn (Set.singleton outRef)
case Map.lookup outRef utxo of
Nothing -> fail "Utxo not found"
Just out -> case view (L._TxOut . _4) out of
C.ReferenceScript{} -> pure ()
_ -> fail "No reference script found"
pure outRef

spendPlutusScriptReference :: C.TxIn -> Mockchain C.TxId
spendPlutusScriptReference txIn = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import Convex.NodeClient.Fold (CatchingUp (..), catchingUp,
import Convex.NodeClient.Resuming (resumingClient)
import Convex.NodeClient.Types (PipelinedLedgerStateClient)
import Convex.TradingBot.Cli.Config (Order (..))
import Convex.Utxos (UtxoSet, apply, toUtxoTx)
import Convex.Utxos (UtxoSet, apply, fromUtxoTx)
import qualified Convex.Utxos as Utxos
import Convex.Wallet (Wallet)
import qualified Convex.Wallet as Wallet
Expand Down Expand Up @@ -67,7 +67,7 @@ applyBlock info logEnv ns wallet tx (catchingUp -> isCatchingUp) state block = K

when (not isCatchingUp) $ do
void $ runMonadBlockchainCardanoNodeT info $ do
(tx_, change_) <- balanceForWallet wallet (toUtxoTx state) tx
(tx_, change_) <- balanceForWallet wallet (fromUtxoTx state) tx
logInfoS (show tx_)
logInfoS (show change_)
sendTx tx_
Expand Down

0 comments on commit 38d1ad8

Please sign in to comment.