diff --git a/changelog.md b/changelog.md index 23bcb6fa..0dba4d1c 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/base/lib/Convex/Class.hs b/src/base/lib/Convex/Class.hs index 4573365e..35ab88b1 100644 --- a/src/base/lib/Convex/Class.hs +++ b/src/base/lib/Convex/Class.hs @@ -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 (..)) @@ -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) diff --git a/src/base/lib/Convex/Utxos.hs b/src/base/lib/Convex/Utxos.hs index c563cd00..e76e28c0 100644 --- a/src/base/lib/Convex/Utxos.hs +++ b/src/base/lib/Convex/Utxos.hs @@ -11,7 +11,7 @@ module Convex.Utxos( -- * Utxo sets UtxoSet(..), - toUtxoTx, + fromUtxoTx, singleton, PrettyBalance(..), _UtxoSet, @@ -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 diff --git a/src/coin-selection/convex-coin-selection.cabal b/src/coin-selection/convex-coin-selection.cabal index 8d577f5a..fd85e24a 100644 --- a/src/coin-selection/convex-coin-selection.cabal +++ b/src/coin-selection/convex-coin-selection.cabal @@ -70,4 +70,5 @@ test-suite convex-coin-selection-test convex-mockchain, convex-base, convex-wallet, - cardano-api + cardano-api, + containers diff --git a/src/coin-selection/test/Spec.hs b/src/coin-selection/test/Spec.hs index fc4c61c5..77afe523 100644 --- a/src/coin-selection/test/Spec.hs +++ b/src/coin-selection/test/Spec.hs @@ -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, @@ -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) @@ -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)) @@ -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 diff --git a/src/trading-bot/lib/Convex/TradingBot/NodeClient/OrderClient.hs b/src/trading-bot/lib/Convex/TradingBot/NodeClient/OrderClient.hs index 5d3ca878..e0f96ae1 100644 --- a/src/trading-bot/lib/Convex/TradingBot/NodeClient/OrderClient.hs +++ b/src/trading-bot/lib/Convex/TradingBot/NodeClient/OrderClient.hs @@ -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 @@ -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_