Skip to content

Commit

Permalink
Rename TokenName to AssetName. (#4305)
Browse files Browse the repository at this point in the history
## Summary

This PR renames the `TokenName` type to `AssetName`, correcting a
terminological inconsistency between `cardano-wallet` and the [Cardano
Multi-Asset Ledger
Specification](https://github.com/input-output-hk/cardano-ledger/releases/latest/download/mary-ledger.pdf).

## Details

The [Cardano Multi-Asset Ledger
Specification](https://github.com/input-output-hk/cardano-ledger/releases/latest/download/mary-ledger.pdf)
uses the term `AssetName`:

>
![mary-ledger-asset-name](https://github.com/cardano-foundation/cardano-wallet/assets/206319/988c61bc-dbb0-4f45-97e4-14fab3f6f839)

The `cardano-wallet` HTTP REST API also uses the derived terms
`assetName` and `asset_name`:
>
https://github.com/cardano-foundation/cardano-wallet/blob/85b7a721778ba0034b8eab3533de3b513cd9a937/specifications/api/swagger.yaml#L7165

>
https://github.com/cardano-foundation/cardano-wallet/blob/85b7a721778ba0034b8eab3533de3b513cd9a937/specifications/api/swagger.yaml#L1028

However, the equivalent `cardano-wallet` Haskell type was called
`TokenName`.

This PR corrects this discrepancy, and:
- renames `TokenName` to `AssetName`;
- renames related functions, strings, and comments;
- **avoids** making any change to database schemas;
- **avoids** making any change to the HTTP REST API.
  • Loading branch information
jonathanknowles authored Dec 6, 2023
2 parents 85b7a72 + 42b80d9 commit 19da2a5
Show file tree
Hide file tree
Showing 50 changed files with 393 additions and 393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ import qualified Cardano.Wallet.Primitive.Types.Address as W
import qualified Cardano.Wallet.Primitive.Types.AssetId as W
( AssetId (..)
)
import qualified Cardano.Wallet.Primitive.Types.AssetName as W
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.TokenName as W
import qualified Cardano.Wallet.Primitive.Types.Tx.Constraints as W
( TxSize (..)
)
Expand Down Expand Up @@ -314,8 +314,8 @@ estimateTxSize skeleton =
-- We consider "native asset" to just be the "multiasset<uint>" part of the
-- above, hence why we don't also include the size of the coin. Where this
-- is used, the size of the coin and array are are added too.
sizeOf_NativeAsset W.AssetId {tokenName}
= sizeOf_MultiAsset sizeOf_LargeUInt tokenName
sizeOf_NativeAsset W.AssetId {assetName}
= sizeOf_MultiAsset sizeOf_LargeUInt assetName

-- multiasset<a> = { * policy_id => { * asset_name => a } }
-- policy_id = scripthash
Expand All @@ -329,7 +329,7 @@ estimateTxSize skeleton =

-- asset_name = bytes .size (0..32)
sizeOf_AssetName name
= 2 + fromIntegral (BS.length $ W.unTokenName name)
= 2 + fromIntegral (BS.length $ W.unAssetName name)

-- Coins can really vary so it's very punishing to always assign them the
-- upper bound. They will typically be between 3 and 9 bytes (only 6 bytes
Expand Down
12 changes: 6 additions & 6 deletions lib/coin-selection/bench/UTxOIndexBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import Prelude
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId (..)
)
import Cardano.Wallet.Primitive.Types.AssetName
( AssetName (..)
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand All @@ -27,9 +30,6 @@ import Cardano.Wallet.Primitive.Types.TokenBundle
import Cardano.Wallet.Primitive.Types.TokenMap
( TokenMap
)
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
Expand Down Expand Up @@ -241,13 +241,13 @@ testUTxOMapKeys = [0 ..]
makeTestAssetId :: Natural -> AssetId
makeTestAssetId n = AssetId
(makeTestTokenPolicyId (n `div` 4))
(makeTestTokenName (n `mod` 4))
(makeTestAssetName (n `mod` 4))

makeTestTokenPolicyId :: Natural -> TokenPolicyId
makeTestTokenPolicyId = UnsafeTokenPolicyId . mockHash

makeTestTokenName :: Natural -> TokenName
makeTestTokenName = UnsafeTokenName . getHash . mockHash
makeTestAssetName :: Natural -> AssetName
makeTestAssetName = UnsafeAssetName . getHash . mockHash

minimalCoin :: Coin
minimalCoin = Coin 1
Expand Down
42 changes: 21 additions & 21 deletions lib/coin-selection/test/spec/Cardano/CoinSelection/BalanceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ import Cardano.Wallet.Primitive.Types.AssetId.Gen
, genAssetIdLargeRange
, shrinkAssetId
)
import Cardano.Wallet.Primitive.Types.AssetName
( AssetName (..)
)
import Cardano.Wallet.Primitive.Types.AssetName.Gen
( genAssetName
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand Down Expand Up @@ -163,12 +169,6 @@ import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genTokenMapSmallRange
, shrinkTokenMap
)
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
)
import Cardano.Wallet.Primitive.Types.TokenName.Gen
( genTokenName
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
Expand Down Expand Up @@ -530,13 +530,13 @@ spec = describe "Cardano.CoinSelection.BalanceSpec" $
it "prop_runRoundRobin_identity" $
property $ prop_runRoundRobin_identity @Int
it "prop_runRoundRobin_iterationCount" $
property $ prop_runRoundRobin_iterationCount @TokenName @Word8
property $ prop_runRoundRobin_iterationCount @AssetName @Word8
it "prop_runRoundRobin_iterationOrder" $
property $ prop_runRoundRobin_iterationOrder @TokenName @Word8
property $ prop_runRoundRobin_iterationOrder @AssetName @Word8
it "prop_runRoundRobin_generationCount" $
property $ prop_runRoundRobin_generationCount @TokenName @Word8
property $ prop_runRoundRobin_generationCount @AssetName @Word8
it "prop_runRoundRobin_generationOrder" $
property $ prop_runRoundRobin_generationOrder @TokenName @Word8
property $ prop_runRoundRobin_generationOrder @AssetName @Word8

describe "Utility functions" $ do

Expand Down Expand Up @@ -3124,13 +3124,13 @@ unit_makeChange =
. fmap (second TokenQuantity)

assetA :: AssetId
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeTokenName "1")
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeAssetName "1")

assetB :: AssetId
assetB = AssetId (UnsafeTokenPolicyId $ Hash "B") (UnsafeTokenName "")
assetB = AssetId (UnsafeTokenPolicyId $ Hash "B") (UnsafeAssetName "")

assetC :: AssetId
assetC = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeTokenName "2")
assetC = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeAssetName "2")

--------------------------------------------------------------------------------
-- Collating non-user-specified asset quantities.
Expand Down Expand Up @@ -3418,7 +3418,7 @@ unit_assignCoinsToChangeMaps =
. fmap (second TokenQuantity)

assetA :: AssetId
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeTokenName "1")
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeAssetName "1")

--------------------------------------------------------------------------------
-- Making change for coins
Expand Down Expand Up @@ -3523,7 +3523,7 @@ unit_makeChangeForNonUserSpecifiedAsset =
m = TokenMap.fromFlatList

assetA :: AssetId
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeTokenName "1")
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeAssetName "1")

--------------------------------------------------------------------------------
-- Making change for multiple non-user-specified assets
Expand Down Expand Up @@ -3827,13 +3827,13 @@ unit_makeChangeForUserSpecifiedAsset =
m = TokenMap.fromFlatList

assetA :: AssetId
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeTokenName "1")
assetA = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeAssetName "1")

assetB :: AssetId
assetB = AssetId (UnsafeTokenPolicyId $ Hash "B") (UnsafeTokenName "")
assetB = AssetId (UnsafeTokenPolicyId $ Hash "B") (UnsafeAssetName "")

assetC :: AssetId
assetC = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeTokenName "2")
assetC = AssetId (UnsafeTokenPolicyId $ Hash "A") (UnsafeAssetName "2")

--------------------------------------------------------------------------------
-- Splitting bundles with excessive asset counts
Expand Down Expand Up @@ -4309,7 +4309,7 @@ matchSingletonList = \case
_ -> Nothing

mockAsset :: ByteString -> AssetId
mockAsset a = AssetId (UnsafeTokenPolicyId $ Hash a) (UnsafeTokenName "1")
mockAsset a = AssetId (UnsafeTokenPolicyId $ Hash a) (UnsafeAssetName "1")

mockAssetQuantity :: ByteString -> Natural -> (AssetId, TokenQuantity)
mockAssetQuantity a q = (mockAsset a, TokenQuantity q)
Expand Down Expand Up @@ -4375,8 +4375,8 @@ instance Arbitrary AssetId where
instance Arbitrary MakeChangeData where
arbitrary = genMakeChangeData

instance Arbitrary (MockRoundRobinState TokenName Word8) where
arbitrary = genMockRoundRobinState genTokenName arbitrary
instance Arbitrary (MockRoundRobinState AssetName Word8) where
arbitrary = genMockRoundRobinState genAssetName arbitrary
shrink = shrinkMockRoundRobinState shrink

instance Arbitrary TokenBundle where
Expand Down
26 changes: 13 additions & 13 deletions lib/local-cluster/lib/Cardano/Wallet/Faucet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Cardano.Wallet.Faucet
, nextWallet

-- * Sea horses
, seaHorseTokenName
, seaHorseAssetName
, seaHorsePolicyId

-- * Integration test funds
Expand Down Expand Up @@ -51,15 +51,15 @@ import Cardano.Mnemonic
( Mnemonic
, SomeMnemonic (..)
)
import Cardano.Wallet.Primitive.Types.AssetName
( AssetName (..)
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle
)
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId
)
Expand Down Expand Up @@ -102,8 +102,8 @@ import qualified Cardano.Address as CA
import qualified Cardano.Address.Style.Icarus as Icarus
import qualified Cardano.Wallet.Faucet.Addresses as Addresses
import qualified Cardano.Wallet.Faucet.Mnemonics as Mnemonics
import qualified Cardano.Wallet.Primitive.Types.AssetName as AssetName
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenName as TokenName
import qualified Data.ByteString.Char8 as B8
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
Expand Down Expand Up @@ -311,13 +311,13 @@ maryIntegrationTestFunds tips =

bundle p assets = TokenBundle.fromNestedList tips [(p, NE.fromList assets)]

simple p = bundle p [(TokenName.empty, TokenQuantity 1_000_000_000)]
simple p = bundle p [(AssetName.empty, TokenQuantity 1_000_000_000)]
fruit p =
bundle
p
[ (UnsafeTokenName "apple", TokenQuantity 65_000_000)
, (UnsafeTokenName "banana", TokenQuantity 66_000_000)
, (UnsafeTokenName "cherry", TokenQuantity 67_000_000)
[ (UnsafeAssetName "apple", TokenQuantity 65_000_000)
, (UnsafeAssetName "banana", TokenQuantity 66_000_000)
, (UnsafeAssetName "cherry", TokenQuantity 67_000_000)
]
combined p = simple p `TokenBundle.add` fruit p

Expand All @@ -340,7 +340,7 @@ seaHorseTestAssets nPerAddr c addrs =
mint :: (t -> a) -> (t, b) -> (a, [b])
mint mk (pid, info) = (mk pid, [info])
seaHorse is p = bundle p $ flip map is $ \i ->
(seaHorseTokenName i, TokenQuantity 1)
(seaHorseAssetName i, TokenQuantity 1)
bundle p assets = TokenBundle.fromNestedList c [(p, NE.fromList assets)]

seaHorsePolicyId :: TokenPolicyId
Expand All @@ -359,9 +359,9 @@ seaHorseAssetScript =
)
)

seaHorseTokenName :: Int -> TokenName
seaHorseTokenName i =
UnsafeTokenName
seaHorseAssetName :: Int -> AssetName
seaHorseAssetName i =
UnsafeAssetName
$ B8.pack
$ "00000000000000000SeaHorse" <> show i

Expand Down
8 changes: 4 additions & 4 deletions lib/local-cluster/lib/Cardano/Wallet/Launch/Cluster.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ import Cardano.Wallet.Network.Ports
import Cardano.Wallet.Primitive.Types.AssetId
( AssetId (..)
)
import Cardano.Wallet.Primitive.Types.AssetName
( AssetName (..)
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..)
)
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
)
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..)
)
Expand Down Expand Up @@ -2271,7 +2271,7 @@ sendFaucet tr config conn what targets = do
++ map (("+ " ++) . cliAsset) (TokenMap.toFlatList tokens)
]
cliAsset (aid, (TokenQuantity q)) = unwords [show q, cliAssetId aid]
cliAssetId (AssetId pid (UnsafeTokenName name)) =
cliAssetId (AssetId pid (UnsafeAssetName name)) =
mconcat
[ T.unpack (toText pid)
, if B8.null name then "" else "."
Expand Down
4 changes: 2 additions & 2 deletions lib/primitive/cardano-wallet-primitive.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ library
Cardano.Wallet.Primitive.Types.AnyExplicitScripts
Cardano.Wallet.Primitive.Types.AssetId
Cardano.Wallet.Primitive.Types.AssetId.Gen
Cardano.Wallet.Primitive.Types.AssetName
Cardano.Wallet.Primitive.Types.AssetName.Gen
Cardano.Wallet.Primitive.Types.Block
Cardano.Wallet.Primitive.Types.Block.Gen
Cardano.Wallet.Primitive.Types.BlockSummary
Expand Down Expand Up @@ -187,8 +189,6 @@ library
Cardano.Wallet.Primitive.Types.TokenMap.Gen
Cardano.Wallet.Primitive.Types.TokenMapWithScripts
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
Expand Down
32 changes: 16 additions & 16 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Ledger/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Cardano.Wallet.Primitive.Ledger.Convert
, toLedgerCoin
, toLedgerTokenBundle
, toLedgerTokenPolicyId
, toLedgerTokenName
, toLedgerAssetName
, toLedgerTokenQuantity
, toLedgerTimelockScript

Expand All @@ -28,7 +28,7 @@ module Cardano.Wallet.Primitive.Ledger.Convert
, toWalletCoin
, toWalletTokenBundle
, toWalletTokenPolicyId
, toWalletTokenName
, toWalletAssetName
, toWalletTokenQuantity
, toWalletScript
, toWalletScriptFromShelley
Expand Down Expand Up @@ -61,6 +61,9 @@ import Cardano.Slotting.Slot
import Cardano.Wallet.Primitive.Types.Address
( Address (..)
)
import Cardano.Wallet.Primitive.Types.AssetName
( AssetName (..)
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
Expand All @@ -70,9 +73,6 @@ import Cardano.Wallet.Primitive.Types.Hash
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..)
)
import Cardano.Wallet.Primitive.Types.TokenName
( TokenName (..)
)
import Cardano.Wallet.Primitive.Types.TokenPolicyId
( TokenPolicyId (..)
)
Expand Down Expand Up @@ -198,7 +198,7 @@ toLedgerTokenBundle bundle =
& Map.map mapInner
& Ledger.MultiAsset
mapInner inner = inner
& Map.mapKeys toLedgerTokenName
& Map.mapKeys toLedgerAssetName
& Map.map toLedgerTokenQuantity

toWalletTokenBundle :: Ledger.MaryValue StandardCrypto -> TokenBundle
Expand All @@ -211,24 +211,24 @@ toWalletTokenBundle
& Map.mapKeys toWalletTokenPolicyId
& Map.map mapInner
mapInner inner = inner
& Map.mapKeys toWalletTokenName
& Map.mapKeys toWalletAssetName
& Map.map toWalletTokenQuantity

--------------------------------------------------------------------------------
-- Conversions for 'TokenName'
-- Conversions for 'AssetName'
--------------------------------------------------------------------------------

instance Convert TokenName Ledger.AssetName where
toLedger = toLedgerTokenName
toWallet = toWalletTokenName
instance Convert AssetName Ledger.AssetName where
toLedger = toLedgerAssetName
toWallet = toWalletAssetName

toLedgerTokenName :: TokenName -> Ledger.AssetName
toLedgerTokenName (UnsafeTokenName bytes) =
toLedgerAssetName :: AssetName -> Ledger.AssetName
toLedgerAssetName (UnsafeAssetName bytes) =
Ledger.AssetName $ toShort bytes

toWalletTokenName :: Ledger.AssetName -> TokenName
toWalletTokenName (Ledger.AssetName bytes) =
UnsafeTokenName $ fromShort bytes
toWalletAssetName :: Ledger.AssetName -> AssetName
toWalletAssetName (Ledger.AssetName bytes) =
UnsafeAssetName $ fromShort bytes

--------------------------------------------------------------------------------
-- Conversions for 'TokenPolicyId'
Expand Down
Loading

0 comments on commit 19da2a5

Please sign in to comment.