Is there a way to programmatically get all the TokenName
s belonging to a CurrencySymbol
with Plutus?
2 Answers
Yes. Using ghci in Plutus repo I'll show you how. I will use fully qualified functions for clarity, and don't forget to :set -XOverloadedStrings
.
Plutus observes all tokens in an output using txOutValue of TxOut. To define our own Value we can do (taken from PPP Lecture#5):
> let v = Plutus.V1.Ledger.Value.singleton "a8ff" "ABC" 7 <> Plutus.V1.Ledger.Ada.lovelaceValueOf 42 <> Plutus.V1.Ledger.Value.singleton "a8ff" "XYZ" 100
> v
Value (Map [(,Map [("",42)]),(a8ff,Map [("ABC",7),("XYZ",100)])])
To filter by CurrencySymbol we can do:
> PlutusTx.AssocMap.keys $ Data.Maybe.fromJust $ PlutusTx.AssocMap.lookup "a8ff" (Plutus.V1.Ledger.Value.getValue v)
["ABC","XYZ"]
This gives you TokenName
s for CurrencySymbol
a8ff. Ada's CurrencySymbol
of ""
has been filtered out.
You cannot have multiple TokenNames belonging to the same CurrencySymbol. Together, TokenName & CurrencySymbol can form an asset class. Once this asset class is formed, it can be wrapped in a Value list.
The reason that TokenName & CurrencySymbol must be 1-1 is that the CurrencySymbol is the hash of the monitary policy script used to validate the Token.
If you had multiple tokens with different TokenNames using the same CurrencySymbol, they would all use the same monitary policy, and that would create some issues with validation.
-
1
-
-
2According to lecture #5 of the PPP, Lars demonstrated on the command line how to combine tokens using the Value.singleton function...
singleton "a8ff" "ABC" 7 <> lovelaceValueOf 42 <> singleton "a8ff" "XYZ" 100
thereby producing:Value (Map [(,Map [("",42)]),(a8ff,Map [("ABC",7),("XYZ",100)])])
... Hence, I fail to see how multiple TokenNames can't map to a single CurrencySymbol/Monetary Policy. Jun 23, 2021 at 12:33 -
1Mmmm I see the confusion. Value is simply a container for a token or multiple tokens of different types(as shown above). It is different from the CurrencySymbol. In the above example, each one of these singleton tokens have their own TokenName and CurrencySymbol. They are then grouped using the <> operator into a Value. Jun 24, 2021 at 12:50
-
1Yes, you are correct in your explanation. But the fact remains that both
TokenName
s "XYZ" and "ABC" belong to the sameCurrencySymbol
"a8ff" Jul 1, 2021 at 11:48
TokenName
s ever minted by a policy, or within a specified scope?TokenName
s ever minted by a policy. But I've come to realize that it may only be possible using an Oracle. I may be wrong tho.