From 54763f05e107fd2799014baed87eed0b507f8876 Mon Sep 17 00:00:00 2001 From: sourabhxyz Date: Sun, 3 Nov 2024 18:21:40 +0530 Subject: [PATCH] feat(#365): add `valueAlter` and some typeclass instances for `GYTxWitness` --- CHANGELOG.md | 4 ++++ atlas-cardano.cabal | 5 +++-- src/GeniusYield/Types/Tx.hs | 2 +- src/GeniusYield/Types/Value.hs | 17 ++++++++++++++++- .../GeniusYield/Test/Privnet/SimpleScripts.hs | 2 -- 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..61036518 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +## 0.6.2 + +* Give `Eq`, `Semigroup`, `Monoid` instance for `GYTxWitness`. +* Adds `valueAlter` utility function inside `GeniusYield.Types.Value` module. \ No newline at end of file diff --git a/atlas-cardano.cabal b/atlas-cardano.cabal index bb5ce888..d204ad37 100644 --- a/atlas-cardano.cabal +++ b/atlas-cardano.cabal @@ -1,6 +1,6 @@ cabal-version: 3.8 name: atlas-cardano -version: 0.6.1 +version: 0.6.2 synopsis: Application backend for Plutus smart contracts on Cardano description: Atlas is an all-in-one, Haskell-native application backend for writing off-chain code for on-chain Plutus smart contracts. @@ -14,7 +14,8 @@ build-type: Simple category: Blockchain, Cardano, Framework homepage: https://github.com/geniusyield/atlas#readme bug-reports: https://github.com/geniusyield/atlas/issues -extra-source-files: README.md +extra-source-files: CHANGELOG.md + README.md tested-with: GHC ==9.6.5 || ==9.6.6 diff --git a/src/GeniusYield/Types/Tx.hs b/src/GeniusYield/Types/Tx.hs index 83e4f0b6..f786b08f 100644 --- a/src/GeniusYield/Types/Tx.hs +++ b/src/GeniusYield/Types/Tx.hs @@ -278,7 +278,7 @@ txIdFromPlutus (PlutusTxIdV3 (PlutusV3.TxId (Plutus.BuiltinByteString bs))) = tx -- | Wrapper around transaction witness set. Note that Babbage ledger also uses the same @TxWitness@ type defined in Alonzo ledger, which was updated for Plutus-V2 scripts and same is expected for Plutus-V3. newtype GYTxWitness = GYTxWitness (AlonzoTxWits (Conway.ConwayEra Crypto.StandardCrypto)) - deriving newtype Show + deriving newtype (Show, Eq, Semigroup, Monoid) instance Swagger.ToSchema GYTxWitness where declareNamedSchema _ = diff --git a/src/GeniusYield/Types/Value.hs b/src/GeniusYield/Types/Value.hs index 07709c80..f120c407 100644 --- a/src/GeniusYield/Types/Value.hs +++ b/src/GeniusYield/Types/Value.hs @@ -24,6 +24,7 @@ module GeniusYield.Types.Value ( valueTotalAssets, valueInsert, valueAdjust, + valueAlter, valueFromLovelace, valueFromApiTxOutValue, valueToApiTxOutValue, @@ -253,10 +254,24 @@ valueInsert :: GYAssetClass -> Integer -> GYValue -> GYValue valueInsert asc 0 (GYValue m) = GYValue (Map.delete asc m) valueInsert asc i (GYValue m) = GYValue (Map.insert asc i m) --- | Adjust the amount of a given 'GYAssetClass' in the given 'GYValue'. +{- | Adjust the amount of a given 'GYAssetClass' in the given 'GYValue'. +If the asset is not present in the value, original value is returned instead. +-} valueAdjust :: (Integer -> Integer) -> GYAssetClass -> GYValue -> GYValue valueAdjust f asc (GYValue m) = GYValue (Map.adjust f asc m) +{- | The expression (@'valueAlter' f asc val@) alters the value @x@ at @asc@, or absence thereof. +'valueAlter' can be used to insert, delete, or update a value in a 'GYValue'. + +>>> valueAlter (Just . maybe 1 (+ 1)) GYLovelace $ mempty +valueFromList [(GYLovelace,1)] + +>>> valueAlter (Just . maybe 1 (+ 1)) GYLovelace $ valueFromList [(GYLovelace, 1)] +valueFromList [(GYLovelace,2)] +-} +valueAlter :: (Maybe Integer -> Maybe Integer) -> GYAssetClass -> GYValue -> GYValue +valueAlter f asc (GYValue m) = GYValue (Map.alter f asc m) + -- | Set of assets within a 'GYValue' in non-zero quantities. valueAssets :: GYValue -> Set GYAssetClass valueAssets (GYValue m) = Map.keysSet m diff --git a/tests-privnet/GeniusYield/Test/Privnet/SimpleScripts.hs b/tests-privnet/GeniusYield/Test/Privnet/SimpleScripts.hs index 5d686615..d0e1eb7f 100644 --- a/tests-privnet/GeniusYield/Test/Privnet/SimpleScripts.hs +++ b/tests-privnet/GeniusYield/Test/Privnet/SimpleScripts.hs @@ -9,8 +9,6 @@ import GeniusYield.Test.Privnet.Ctx import GeniusYield.Test.Privnet.Setup import GeniusYield.TxBuilder import GeniusYield.Types -import GeniusYield.Types.Script (GYAnyScript (GYSimpleScript)) -import GeniusYield.Types.UTxO (GYUTxO (utxoRefScript)) import Test.Tasty (TestTree, testGroup) import Test.Tasty.HUnit (testCaseSteps)