Skip to content

Commit

Permalink
Improve structure of token-related modules in `cardano-wallet-primiti…
Browse files Browse the repository at this point in the history
…ve`. (#4297)

## Issue

None. Spotted while reviewing.

## Description

This PR:
- breaks up large token-related modules in `cardano-wallet-primitive`
- extracts out the following new modules:
    - `Primitive.Types.AssetId`
    - `Primitive.Types.AssetId.Gen`
    - `Primitive.Types.TokenFingerprint`
    - `Primitive.Types.TokenFingerprintSpec`
    - `Primitive.Types.TokenMetadata`
    - `Primitive.Types.TokenName`
    - `Primitive.Types.TokenName.Gen`
    - `Primitive.Types.TokenPolicyId`
    - `Primitive.Types.TokenPolicyId.Gen`

In general, this PR arranges that:
- each distinct type `X` with its own language of related functions is
located in its own module:
    - `Primitive.X`
- generators and shrinkers for type `X` are located in:
    - `Primitive.X.Gen`

In particular, this PR retires the old `TokenPolicy` module, which had
become a menagerie of miscellaneous token-related types and functions.
☢️
  • Loading branch information
jonathanknowles authored Dec 5, 2023
2 parents f9c7091 + e3f035f commit 6630ea7
Show file tree
Hide file tree
Showing 76 changed files with 1,039 additions and 688 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ import qualified Cardano.Wallet.Primitive.Ledger.Convert as Convert
import qualified Cardano.Wallet.Primitive.Types.Address as W
( Address
)
import qualified Cardano.Wallet.Primitive.Types.AssetId as W
( AssetId (..)
)
import qualified Cardano.Wallet.Primitive.Types.Coin as W.Coin
import qualified Cardano.Wallet.Primitive.Types.Coin as W
( Coin (..)
Expand All @@ -293,9 +296,6 @@ import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W
( TokenBundle (..)
)
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W.TokenMap
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W
( AssetId (..)
)
import qualified Cardano.Wallet.Primitive.Types.TokenQuantity as W
( TokenQuantity (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ import qualified Cardano.CoinSelection.Context as SC
import qualified Cardano.Wallet.Primitive.Types.Address as W
( Address (..)
)
import qualified Cardano.Wallet.Primitive.Types.AssetId as W
( AssetId
)
import qualified Cardano.Wallet.Primitive.Types.Coin as W
( Coin
)
Expand All @@ -133,8 +136,7 @@ import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W
( TokenBundle (..)
)
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W
( AssetId
, TokenMap
( TokenMap
)
import qualified Cardano.Wallet.Primitive.Types.Tx.Constraints as W
( txOutMaxCoin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ import qualified Cardano.Address.Script as CA
import qualified Cardano.Wallet.Primitive.Types.Address as W
( Address (..)
)
import qualified Cardano.Wallet.Primitive.Types.AssetId as W
( AssetId (..)
)
import qualified Cardano.Wallet.Primitive.Types.Coin as W
( Coin (..)
)
import qualified Cardano.Wallet.Primitive.Types.Coin as W.Coin
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W.TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W
( AssetId (..)
)
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
import qualified Cardano.Wallet.Primitive.Types.TokenName as W
import qualified Cardano.Wallet.Primitive.Types.Tx.Constraints as W
( TxSize (..)
)
Expand Down
12 changes: 8 additions & 4 deletions lib/coin-selection/bench/UTxOIndexBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module Main where

import Prelude

import Cardano.Wallet.Primitive.Types.AssetId
( AssetId (..)
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand All @@ -22,12 +25,13 @@ import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..)
)
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId (..)
, TokenMap
( TokenMap
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
, TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..)
Expand Down
6 changes: 4 additions & 2 deletions lib/coin-selection/lib/Cardano/CoinSelection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ import Cardano.CoinSelection.Size
import Cardano.CoinSelection.UTxOSelection
( UTxOSelection
)
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle
)
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId
, TokenMap
( TokenMap
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity
Expand Down
6 changes: 4 additions & 2 deletions lib/coin-selection/lib/Cardano/CoinSelection/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,17 @@ import Cardano.CoinSelection.UTxOSelection
import Cardano.Numeric.Util
( padCoalesce
)
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..)
)
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId
, Lexicographic (..)
( Lexicographic (..)
, TokenMap
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
Expand Down
8 changes: 4 additions & 4 deletions lib/coin-selection/lib/Cardano/CoinSelection/Balance/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import Cardano.CoinSelection.Balance
import Cardano.CoinSelection.Context
( SelectionContext (..)
)
import Cardano.Wallet.Primitive.Types.AssetId.Gen
( genAssetId
, shrinkAssetId
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand All @@ -28,10 +32,6 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
( genTokenBundleSmallRange
, shrinkTokenBundleSmallRange
)
import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genAssetId
, shrinkAssetId
)
import Generics.SOP
( NP (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ import GHC.Generics
( Generic
)

import qualified Cardano.Wallet.Primitive.Types.AssetId as W
( AssetId
)
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W.TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W
( TokenBundle
)
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W.TokenMap
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W
( AssetId
)
import qualified Data.Foldable as F
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
Expand Down
24 changes: 15 additions & 9 deletions lib/coin-selection/test/spec/Cardano/CoinSelection/BalanceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ import Cardano.CoinSelection.UTxOSelection.Gen
import Cardano.Numeric.Util
( inAscendingPartialOrder
)
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId (..)
)
import Cardano.Wallet.Primitive.Types.AssetId.Gen
( genAssetId
, genAssetIdLargeRange
, shrinkAssetId
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand All @@ -149,23 +157,21 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
, shrinkTokenBundleSmallRangePositive
)
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId (..)
, TokenMap
( TokenMap
)
import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genAssetId
, genAssetIdLargeRange
, genTokenMapSmallRange
, shrinkAssetId
( genTokenMapSmallRange
, shrinkTokenMap
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
, TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicy.Gen
import Cardano.Wallet.Primitive.Types.TokenName.Gen
( genTokenName
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ import Cardano.CoinSelection.UTxOIndex.Internal
, categorizeTokenBundle
, checkInvariant
)
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId
)
import Cardano.Wallet.Primitive.Types.AssetId.Gen
( genAssetId
, shrinkAssetId
)
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle
)
import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
( genTokenBundleSmallRangePositive
, shrinkTokenBundleSmallRangePositive
)
import Cardano.Wallet.Primitive.Types.TokenMap
( AssetId
)
import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genAssetId
, shrinkAssetId
)
import Control.Monad
( void
)
Expand Down
6 changes: 4 additions & 2 deletions lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ import Cardano.CoinSelection.UTxOSelection.Gen
( genUTxOSelection
, shrinkUTxOSelection
)
import Cardano.Wallet.Primitive.Types.AssetId.Gen
( genAssetId
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand All @@ -87,8 +90,7 @@ import Cardano.Wallet.Primitive.Types.TokenMap
( TokenMap
)
import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genAssetId
, genTokenMap
( genTokenMap
, shrinkTokenMap
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
Expand Down
6 changes: 4 additions & 2 deletions lib/local-cluster/lib/Cardano/Wallet/Faucet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ import Cardano.Wallet.Primitive.Types.Coin
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
, TokenPolicyId
, nullTokenName
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..)
)
Expand Down
8 changes: 5 additions & 3 deletions lib/local-cluster/lib/Cardano/Wallet/Launch/Cluster.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ import Cardano.Startup
import Cardano.Wallet.Network.Ports
( randomUnusedTCPPorts
)
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId (..)
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
import Cardano.Wallet.Primitive.Types.TokenBundle
( AssetId (..)
, TokenBundle (..)
( TokenBundle (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
Expand Down
12 changes: 9 additions & 3 deletions lib/primitive/cardano-wallet-primitive.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ library
Cardano.Wallet.Primitive.Types.Address
Cardano.Wallet.Primitive.Types.Address.Gen
Cardano.Wallet.Primitive.Types.AnyExplicitScripts
Cardano.Wallet.Primitive.Types.AssetId
Cardano.Wallet.Primitive.Types.AssetId.Gen
Cardano.Wallet.Primitive.Types.Block
Cardano.Wallet.Primitive.Types.Block.Gen
Cardano.Wallet.Primitive.Types.BlockSummary
Expand Down Expand Up @@ -180,11 +182,15 @@ library
Cardano.Wallet.Primitive.Types.TokenBundle
Cardano.Wallet.Primitive.Types.TokenBundle.Gen
Cardano.Wallet.Primitive.Types.TokenBundleMaxSize
Cardano.Wallet.Primitive.Types.TokenFingerprint
Cardano.Wallet.Primitive.Types.TokenMap
Cardano.Wallet.Primitive.Types.TokenMap.Gen
Cardano.Wallet.Primitive.Types.TokenMapWithScripts
Cardano.Wallet.Primitive.Types.TokenPolicy
Cardano.Wallet.Primitive.Types.TokenPolicy.Gen
Cardano.Wallet.Primitive.Types.TokenMetadata
Cardano.Wallet.Primitive.Types.TokenName
Cardano.Wallet.Primitive.Types.TokenName.Gen
Cardano.Wallet.Primitive.Types.TokenPolicyId
Cardano.Wallet.Primitive.Types.TokenPolicyId.Gen
Cardano.Wallet.Primitive.Types.TokenQuantity
Cardano.Wallet.Primitive.Types.TokenQuantity.Gen
Cardano.Wallet.Primitive.Types.Tx
Expand Down Expand Up @@ -271,8 +277,8 @@ test-suite test
Cardano.Wallet.Primitive.Types.PoolSpec
Cardano.Wallet.Primitive.Types.RangeSpec
Cardano.Wallet.Primitive.Types.TokenBundleSpec
Cardano.Wallet.Primitive.Types.TokenFingerprintSpec
Cardano.Wallet.Primitive.Types.TokenMapSpec
Cardano.Wallet.Primitive.Types.TokenPolicySpec
Cardano.Wallet.Primitive.Types.TokenQuantitySpec
Cardano.Wallet.Primitive.Types.TxSpec
Cardano.Wallet.Primitive.Types.UTxOSpec
Expand Down
6 changes: 4 additions & 2 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Ledger/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ import Cardano.Wallet.Primitive.Types.Hash
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
, TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import Cardano.Wallet.Primitive.Ledger.Read.Tx.Features.Validity
import Cardano.Wallet.Primitive.Ledger.Read.Tx.Features.Withdrawals
( fromLedgerWithdrawals
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId
)
import Cardano.Wallet.Primitive.Types.Tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import Cardano.Wallet.Primitive.Types.TokenMapWithScripts
, ScriptReference (..)
, TokenMapWithScripts
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
import Cardano.Wallet.Primitive.Types.Tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import Cardano.Wallet.Primitive.Types.TokenMapWithScripts
, TokenMapWithScripts (TokenMapWithScripts)
, emptyTokenMapWithScripts
)
import Cardano.Wallet.Primitive.Types.TokenPolicy
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
import Cardano.Wallet.Read.Eras
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ import qualified Cardano.Ledger.Crypto as SL
import qualified Cardano.Ledger.Shelley.API as SL
import qualified Cardano.Wallet.Primitive.Ledger.Convert as Ledger
import qualified Cardano.Wallet.Primitive.Types.Address as W
import qualified Cardano.Wallet.Primitive.Types.AssetId as W
import qualified Cardano.Wallet.Primitive.Types.Coin as Coin
import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
import qualified Cardano.Wallet.Primitive.Types.TokenName as W
import qualified Cardano.Wallet.Primitive.Types.TokenPolicyId as W
import qualified Cardano.Wallet.Primitive.Types.TokenQuantity as W
import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut as W

Expand Down Expand Up @@ -155,7 +157,7 @@ fromCardanoValue = uncurry TokenBundle.fromFlatList . extract
| otherwise = internalError "negative token quantity"

mkBundle assets =
[ (TokenBundle.AssetId (mkPolicyId p) (mkTokenName n) , mkQuantity q)
[ (W.AssetId (mkPolicyId p) (mkTokenName n), mkQuantity q)
| (Cardano.AssetId p n, Cardano.Quantity q) <- assets
]

Expand Down
Loading

0 comments on commit 6630ea7

Please sign in to comment.