Skip to content

Commit

Permalink
Merge pull request #367 from geniusyield/365-add-value-utility
Browse files Browse the repository at this point in the history
feat(#365): add `valueAlter` and some typeclass instances for `GYTxWi…
  • Loading branch information
sourabhxyz authored Nov 4, 2024
2 parents abc35af + 54763f0 commit 233122f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 0.6.2

* Give `Eq`, `Semigroup`, `Monoid` instance for `GYTxWitness`.
* Adds `valueAlter` utility function inside `GeniusYield.Types.Value` module.
5 changes: 3 additions & 2 deletions atlas-cardano.cabal
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/GeniusYield/Types/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ =
Expand Down
17 changes: 16 additions & 1 deletion src/GeniusYield/Types/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module GeniusYield.Types.Value (
valueTotalAssets,
valueInsert,
valueAdjust,
valueAlter,
valueFromLovelace,
valueFromApiTxOutValue,
valueToApiTxOutValue,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions tests-privnet/GeniusYield/Test/Privnet/SimpleScripts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 233122f

Please sign in to comment.