Value

scalus.cardano.onchain.plutus.v1.Value
See theValue companion class
object Value

Attributes

Companion
class
Graph
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
Value.type

Members list

Type members

Inherited and Abstract types

type MirroredElemLabels <: Tuple

The names of the product elements

The names of the product elements

Attributes

Inherited from:
Mirror
type MirroredLabel <: String

The name of the type

The name of the type

Attributes

Inherited from:
Mirror

Value members

Concrete methods

def apply(cs: PolicyId, tn: TokenName, v: BigInt): Value

Creates a Value containing the specified amount of a specific policy id and token. If the amount is zero, it returns Value.zero.

Creates a Value containing the specified amount of a specific policy id and token. If the amount is zero, it returns Value.zero.

Value parameters

cs

The policy id

tn

The token name

v

The amount of the token

Attributes

Returns

A new Value containing the specified amount of the token, or Value.zero if the amount is zero

Example
 Value(Value.adaPolicyId, Value.adaTokenName, BigInt(1000000)) === Value.lovelace(BigInt(1000000))
 val policyId: PolicyId = ByteString.fromString("policyId")
 val tokenName: TokenName = ByteString.fromString("tokenName")
 val value = Value(policyId, tokenName, BigInt(100))
 value.quantityOf(policyId, tokenName) === BigInt(100)
 value.getLovelace === BigInt(0)
 value.isZero === false
 Value(policyId, tokenName, BigInt(0)) === Value.zero
def debugToString(v: Value): String

Converts a Value to a debug string representation.

Converts a Value to a debug string representation.

Formats the Value as a string showing policy IDs and token amounts in a human-readable format. Each policy ID and its associated tokens are displayed with their hex representations and amounts.

Value parameters

v

The Value to convert to string

Attributes

Returns

A formatted string representation of the Value

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 // Prints: { policy# -> { #: 1000000 }, policy#6666 -> { #544f4b454e: 100 } }
 println(Value.debugToString(value))
def eq(a: Value, b: Value): Boolean

Tests if two Value instances are equal.

Tests if two Value instances are equal.

Compares two Value instances by checking if they contain the same policy ids and tokens with equal amounts, treating absent tokens as having zero amount.

Value parameters

a

First Value instance

b

Second Value instance

Attributes

Returns

true if both Value instances are equal, false otherwise

Example
 val value1 = Value.lovelace(BigInt(1000000))
 val value2 = Value.lovelace(BigInt(1000000))
 Value.eq(value1, value2) === true
 val value3 = Value.lovelace(BigInt(500000))
 Value.eq(value1, value3) === false
def equalsAssets(a: SortedMap[TokenName, BigInt], b: SortedMap[TokenName, BigInt]): Boolean

Tests if two token asset maps contain exactly the same tokens with equal amounts.

Tests if two token asset maps contain exactly the same tokens with equal amounts.

Compares two maps of token names to amounts, treating absent tokens as having zero amount. The comparison checks if each token has equal amounts in both maps.

Value parameters

a

First map of token names to amounts

b

Second map of token names to amounts

Attributes

Returns

true if both maps contain the same tokens with equal amounts, false otherwise

Example
 val assets1 = SortedMap.fromList(
   List.Cons(
     (ByteString.fromString("TOKEN1"), BigInt(100)),
     List.Cons(
       (ByteString.fromString("TOKEN2"), BigInt(200)),
       List.Nil
     )
   )
 )
 val assets2 = SortedMap.fromList(
   List.Cons(
     (ByteString.fromString("TOKEN1"), BigInt(100)),
     List.Cons(
       (ByteString.fromString("TOKEN2"), BigInt(200)),
       List.Nil
     )
   )
 )
 Value.equalsAssets(assets1, assets2) === true
def fromList(list: List[(PolicyId, List[(TokenName, BigInt)])]): Value

Creates a Value from a list of policy ids paired with their token amounts, filtering out zero amounts.

Creates a Value from a list of policy ids paired with their token amounts, filtering out zero amounts.

This method safely constructs a Value by:

  • Removing all tokens with zero amounts
  • Removing policy ids that have no remaining tokens after filtering

Value parameters

list

A list of tuples containing policy ids and their associated token amounts

Attributes

Returns

A Value with zero amounts filtered out

See also

unsafeFromList for an unfiltered unsafe version or fromStrictlyAscendingListWithNonZeroAmounts for a faster stricter version

Example
 val tokens = List.Cons(
   (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
   Cons(
     (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(0)), List.Nil)),
     List.Nil
   )
 )
 // The second entry will be filtered out due to zero amount
 Value.fromList(tokens) === Value.lovelace(BigInt(1000000))

Creates a Value from a strictly ascending list of policy ids and token amounts, requiring non-zero amounts.

Creates a Value from a strictly ascending list of policy ids and token amounts, requiring non-zero amounts.

This method enforces stricter requirements than fromList:

  • The input list must be strictly ascending by policy id
  • Each token list must be strictly ascending by token name
  • All token amounts must be non-zero
  • Token lists cannot be empty

Value parameters

list

A strictly ascending list of tuples containing policy ids and their associated token amounts

Attributes

Returns

A Value constructed from the strictly ascending lists

Throws
scalus.cardano.onchain.RequirementError

If any token amount is zero or any token list is empty

See also

unsafeFromList for an unsafe fast version or fromList for a more permissive slow version that filters invalid entries

Example
 // Successful case - ascending order and non-zero amounts
 val validTokens = List.Cons(
   (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
   Cons(
     (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
     List.Nil
   )
 )
 Value.fromStrictlyAscendingListWithNonZeroAmounts(validTokens) // Succeeds
 // Error case - contains zero amount
 val invalidTokens = List.Cons(
   (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(0)), List.Nil)),
   List.Nil
 )
 Value.fromStrictlyAscendingListWithNonZeroAmounts(invalidTokens) // Throws RequirementError
def lovelace(v: BigInt): Value

Creates a Value representing a specific amount of ADA in lovelace.

Creates a Value representing a specific amount of ADA in lovelace.

This is a convenience method for creating a Value with the ADA policy id and token name, where the amount is specified in lovelace (1 ADA = 1,000,000 lovelace).

Value parameters

v

The amount of Lovelace

Attributes

Returns

A new Value containing only the specified amount of Lovelace

Example
 Value.lovelace(BigInt(1000000)) === Value(Value.adaPolicyId, Value.adaTokenName, BigInt(1000000))
 Value.lovelace(BigInt(1000000)).getLovelace === BigInt(1000000)
 Value.lovelace(BigInt(1000000)).quantityOf(ByteString.fromString("policyId"), ByteString.fromString("tokenName")) === BigInt(0)
 Value.lovelace(BigInt(0)) === Value.zero
def minus(a: Value, b: Value): Value

Subtracts the second Value from the first, effectively performing element-wise subtraction of token amounts.

Subtracts the second Value from the first, effectively performing element-wise subtraction of token amounts.

This method subtracts the token amounts in the second Value from those in the first, treating absent tokens as having zero amount.

At PV11 (vanRossem) with Options.valueBuiltins enabled (the default) this lowers to the CIP-153 scaleValue + unionValue builtins, which require both values in canonical form and fail on a result outside the signed 128-bit range. See Options.valueBuiltins.

Value parameters

a

The Value to subtract from

b

The Value to subtract

Attributes

Returns

A new Value containing the result of the subtraction

Example
 val value1 = Value.lovelace(BigInt(1000000))
 val value2 = Value.lovelace(BigInt(500000))
 Value.minus(value1, value2) === Value.lovelace(BigInt(500000))
def multiply(v: Value, factor: BigInt): Value

Multiplies all token amounts in a Value by a specified factor.

Multiplies all token amounts in a Value by a specified factor.

This method scales each token amount in the Value by the given factor, effectively multiplying all amounts by the same integer.

At PV11 (vanRossem) with Options.valueBuiltins enabled (the default) this lowers to the CIP-153 scaleValue builtin, which requires the value in canonical form and fails on a scaled amount outside the signed 128-bit range. See Options.valueBuiltins.

Value parameters

factor

The factor to multiply each token amount by

v

The Value to multiply

Attributes

Returns

A new Value with all token amounts multiplied by the factor, or Value.zero if the factor is zero

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 Value.multiply(value, BigInt(2)) === Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(2000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(200)), List.Nil)),
       List.Nil
     )
   )
 )
 Value.multiply(value, BigInt(0)) === Value.zero
def negate(v: Value): Value

Negates all token amounts in a Value, effectively creating the additive inverse.

Negates all token amounts in a Value, effectively creating the additive inverse.

This method traverses the Value and negates each token amount by subtracting it from zero.

At PV11 (vanRossem) with Options.valueBuiltins enabled (the default) this lowers to the CIP-153 scaleValue builtin, which requires the value in canonical form and fails on a result outside the signed 128-bit range. See Options.valueBuiltins.

Value parameters

v

The Value to negate

Attributes

Returns

A new Value with all token amounts negated

Example
 val value1 = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 val value2 = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(-1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(-100)), List.Nil)),
       List.Nil
     )
   )
 )
 Value.negate(value1) === value2
def nonEq(a: Value, b: Value): Boolean

Tests if two Value instances are not equal.

Tests if two Value instances are not equal.

Compares two Value instances by checking if they contain different policy ids or tokens with unequal amounts, treating absent tokens as having zero amount.

Value parameters

a

First Value instance

b

Second Value instance

Attributes

Returns

true if the Value instances are not equal, false otherwise

Example
 val value1 = Value.lovelace(BigInt(1000000))
 val value2 = Value.lovelace(BigInt(1000000))
 Value.nonEq(value1, value2) === false
 val value3 = Value.lovelace(BigInt(500000))
 Value.nonEq(value1, value3) === true
def plus(a: Value, b: Value): Value

Adds two Value instances together, combining their token amounts.

Adds two Value instances together, combining their token amounts.

This method performs an element-wise addition of the token amounts in both Value instances, treating absent tokens as having zero amount.

At PV11 (vanRossem) with Options.valueBuiltins enabled (the default) this lowers to the CIP-153 unionValue builtin, which requires both values in canonical form and fails on a sum outside the signed 128-bit range. See Options.valueBuiltins.

Value parameters

a

First Value instance

b

Second Value instance

Attributes

Returns

A new Value containing the combined token amounts

Example
 val value1 = Value.lovelace(BigInt(1000000))
 val value2 = Value.lovelace(BigInt(500000))
 Value.plus(value1, value2) === Value.lovelace(BigInt(1500000))
def unsafeFromList(list: List[(PolicyId, List[(TokenName, BigInt)])]): Value

Creates a Value from a list of policy ids paired with their token amounts, without validation.

Creates a Value from a list of policy ids paired with their token amounts, without validation.

This method directly constructs a Value from the input list without checking for zero amounts or empty token lists. Use with caution as it may create invalid states.

Value parameters

list

A list of tuples containing policy ids and their associated token amounts

Attributes

Returns

A Value constructed directly from the input list

See also
Example
 val tokens = List.Cons(
   (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
   Cons(
     (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
     List.Nil
   )
 )
 Value.unsafeFromList(tokens)

Creates a Value from a sorted map of policy ids to their token amounts, without validation.

Creates a Value from a sorted map of policy ids to their token amounts, without validation.

This method directly constructs a Value from the input sorted map without checking for zero amounts or empty token lists. Use with caution as it may create invalid states.

Value parameters

sm

A sorted map of policy ids to their associated token amounts

Attributes

Returns

A Value constructed directly from the input sorted map

See also
Example
 val tokens = SortedMap.fromList(
   List.Cons(
     (Value.adaPolicyId, SortedMap.fromList(List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil))),
     Cons(
       (ByteString.fromString("ff"), SortedMap.fromList(List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil))),
       List.Nil
     )
   )
 )
 Value.unsafeFromSortedMap(tokens)

Implementation of the scalus.uplc.builtin.FromData type class for Value with validation. Validates that:

Implementation of the scalus.uplc.builtin.FromData type class for Value with validation. Validates that:

  • All token amounts are non-zero
  • No currency symbol has an empty token list

This method ensures that the Value is well-formed and meets the expected structure.

Attributes

Example
 val data: Data = ... // Some Data representation of a Value
 val value = Value.valueFromDataWithValidation(data) // Converts from Data to Value with validation

Concrete fields

The policy id for ADA, represented as an empty ByteString.

The policy id for ADA, represented as an empty ByteString.

Attributes

Example
 Value.adaPolicyId === ByteString.empty

The token name for ADA, represented as an empty ByteString.

The token name for ADA, represented as an empty ByteString.

Attributes

Example
 Value.adaTokenName === ByteString.empty
lazy val sirDeps: List[SIRModuleWithDeps]
lazy val sirModule: Module

Implementation of the scalus.cardano.onchain.plutus.prelude.Ord type class for Value. Only makes sense as a key for collections.

Implementation of the scalus.cardano.onchain.plutus.prelude.Ord type class for Value. Only makes sense as a key for collections.

Provides total ordering for Value instances by comparing their underlying sorted maps. The ordering is determined by:

  1. First comparing policy ids
  2. For equal policy ids, comparing their token maps
  3. For equal token names, comparing their amounts

Attributes

Example
val value1 = Value.lovelace(BigInt(1000000))
val value2 = Value.lovelace(BigInt(500000))
value1  value2 // Returns Order.Greater
val zero: Value

A value representing zero units of any policy id or token.

A value representing zero units of any policy id or token.

Attributes

Example
 Value.zero.isZero === true
 Value.zero.quantityOf(ByteString.fromString("policyId"), ByteString.fromString("tokenName")) === BigInt(0)
 Value.zero.getLovelace === BigInt(0)

Givens

Givens

given valueEq: Eq[Value]

Implementation of the scalus.cardano.onchain.plutus.prelude.Eq type class for Value.

Implementation of the scalus.cardano.onchain.plutus.prelude.Eq type class for Value.

Provides equality comparison between two Value instances by delegating to Value.eq. Two Value instances are considered equal if they contain exactly the same policy ids and tokens with equal amounts.

Attributes

Example
 val value1 = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Nil
   )
 )
 val value2 = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Nil
   )
 )
 value1 === value2 // true, using derived Eq instance

Implementation of the scalus.uplc.builtin.FromData type class for Value.

Implementation of the scalus.uplc.builtin.FromData type class for Value.

Converts a Data representation back into a Value. This method assumes that the input data is well-formed and does not perform any validation on the token amounts or structure.

Attributes

Example
 val data: Data = ... // Some Data representation of a Value
 val value = Value.fromData(data) // Converts from Data to Value

Implementation of the scalus.uplc.builtin.ToData type class for Value.

Implementation of the scalus.uplc.builtin.ToData type class for Value.

Converts a Value to a Data representation by converting its sorted map structure.

Attributes

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Nil
   )
 )
 val data = value.toData // Converts to Data representation

Extensions

Extensions

extension (v: Value)
inline def *(factor: BigInt): Value

Extension alias for Value.multiply.

Extension alias for Value.multiply.

Attributes

inline def +(other: Value): Value

Extension alias for Value.plus.

Extension alias for Value.plus.

Attributes

inline def -(other: Value): Value

Extension alias for Value.minus.

Extension alias for Value.minus.

Attributes

def containsAtLeast(other: Value): Boolean

Tests whether this Value contains at least the amounts in other.

Tests whether this Value contains at least the amounts in other.

For every (policy id, token name, amount) entry in other, this Value must hold at least that amount. Fails (throws) when either value contains a negative amount - mirroring the CIP-153 valueContains builtin: for values in canonical form the result is identical on every protocol version. For non-canonical values (zero amounts, empty inner maps - only constructible via the unsafe constructors) the PV11 builtin lowering may fail or differ.

At PV11 (vanRossem) this method lowers to the valueContains builtin, which requires both values to be in canonical form (strictly ascending keys, no zero amounts, no empty inner maps, keys at most 32 bytes, amounts within the signed 128-bit range); a non-canonical value makes the script fail. See Options.valueBuiltins.

Attributes

Example
 val a = Value.lovelace(BigInt(1000))
 val b = Value.lovelace(BigInt(400))
 a.containsAtLeast(b) === true
 b.containsAtLeast(a) === false
def flatten: List[(PolicyId, TokenName, BigInt)]

Flattens the Value into a list of policy id, token name, and amount triples.

Flattens the Value into a list of policy id, token name, and amount triples.

Converts the nested map structure into a flat list representation where each element contains the policy id, token name, and corresponding amount.

Attributes

Returns

A flattened list of tuples containing (policyId, tokenName, amount)

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 value.flatten ===
 List.Cons(
    (Value.adaPolicyId, Value.adaTokenName, BigInt(1000000)),
    List.Cons(
      (ByteString.fromString("ff"), ByteString.fromString("TOKEN"), BigInt(100)),
      List.Nil
    )
 )
def getLovelace: BigInt

Returns the amount of Lovelace in this Value.

Returns the amount of Lovelace in this Value.

If the Value contains no Lovelace, it returns zero.

At PV11 (vanRossem) with Options.valueBuiltins enabled (the default) this delegates to quantityOf and thus lowers to the CIP-153 lookupCoin builtin, which requires the value in canonical form (strictly ascending keys, no zero amounts, no empty inner maps, keys at most 32 bytes, amounts within the signed 128-bit range) and makes the script fail otherwise. See Options.valueBuiltins.

Attributes

Returns

The amount of Lovelace in this Value

Example
 val value = Value.lovelace(BigInt(1000000))
 value.getLovelace === BigInt(1000000)
 val emptyValue = Value.zero
 emptyValue.getLovelace === BigInt(0)
inline def hasNft(cs: PolicyId, tn: TokenName): Boolean

Checks that exactly one unit of the (cs, tn) asset is present. Other assets, under this policy or any other, are tolerated.

Checks that exactly one unit of the (cs, tn) asset is present. Other assets, under this policy or any other, are tolerated.

This is the state-token / beacon check. The strict twin, which also rejects any other token under cs, is hasOnly with amount 1. A quantity compared with > 0 is a different predicate for any asset that is not an NFT; prefer this one for tokens that are minted exactly once.

Value parameters

cs

The policy id

tn

The token name

Attributes

Returns

true if quantityOf(cs, tn) is exactly 1

Example
 Value(utf8"pid", utf8"BEACON", 1).hasNft(utf8"pid", utf8"BEACON") === true
 Value(utf8"pid", utf8"BEACON", 2).hasNft(utf8"pid", utf8"BEACON") === false
 Value.lovelace(1).hasNft(utf8"pid", utf8"BEACON") === false
def hasOnly(cs: PolicyId, tn: TokenName, amount: BigInt): Boolean

Tests whether the tokens under cs are exactly {tn -> amount}.

Tests whether the tokens under cs are exactly {tn -> amount}.

True iff this Value holds amount of tn under cs and NO other token of that policy id. Other policy ids are not constrained, so the check composes with other scripts acting in the same transaction. Negative amounts work too, e.g. hasOnly(cs, tn, -1) checks an exact single-token burn.

This is the recommended way to verify an exact mint, e.g. that a minting policy forges exactly one beacon NFT and nothing else under its own policy id: tx.mint.hasOnly(ownPolicyId, tn, 1). It costs a single equalsData on the policy's token map instead of an element-wise SortedMap comparison.

The equality is on the underlying Data encoding, so it assumes this Value is in canonical form (strictly ascending keys, no zero amounts, no empty inner maps) - true for every ledger-provided value such as tx.mint or an output's value. A non-canonical value (constructible only via the unsafe constructors) may compare unequal to a semantically equal one. In particular amount == 0 returns false on canonical values: they never store zero amounts.

Value parameters

amount

The exact amount required for tn

cs

The policy id whose tokens must be exactly {tn -> amount}

tn

The single token name allowed under cs

Attributes

Example
 val beacon = Value(utf8"pid", utf8"BEACON", 1)
 beacon.hasOnly(utf8"pid", utf8"BEACON", 1) === true
 beacon.hasOnly(utf8"pid", utf8"BEACON", 2) === false
 beacon.hasOnly(utf8"other", utf8"BEACON", 1) === false
 val two = Value.unsafeFromList(
   List((utf8"pid", List((utf8"BEACON", BigInt(1)), (utf8"BEACON1", BigInt(1)))))
 )
 two.hasOnly(utf8"pid", utf8"BEACON", 1) === false
def hasSameTokensAndAtLeastAda(expected: Value): Boolean

Checks that every non-ADA asset of this Value is exactly equal to expected's and its lovelace is at least expected's.

Checks that every non-ADA asset of this Value is exactly equal to expected's and its lovelace is at least expected's.

This is the continuing-output value check. Neither plain comparison is right: === rejects a valid transaction whenever the builder must add lovelace to clear the minimum-ADA requirement, and a whole-value >= lets one output satisfy two "at least" obligations. Tokens exact, ADA open above.

Value parameters

expected

The value the output must carry, up to extra lovelace

Attributes

Returns

true if the non-ADA parts are equal and this value's lovelace is not below expected's

Example
 val expected = Value.lovelace(2_000_000) + Value(utf8"pid", utf8"TOKEN", 5)
 (expected + Value.lovelace(500_000)).hasSameTokensAndAtLeastAda(expected) === true
 expected.hasSameTokensAndAtLeastAda(expected) === true
 (expected - Value.lovelace(1)).hasSameTokensAndAtLeastAda(expected) === false
 (expected + Value(utf8"pid", utf8"TOKEN", 1)).hasSameTokensAndAtLeastAda(expected) === false
def insertCoin(cs: PolicyId, tn: TokenName, amount: BigInt): Value

Returns a new Value with the amount of (cs, tn) set to amount.

Returns a new Value with the amount of (cs, tn) set to amount.

The amount is REPLACED, not added - unlike +. An amount of zero deletes the token, and the policy entry too when it held no other token, keeping the result canonical. Mirrors the CIP-153 insertCoin builtin.

At PV11 (vanRossem) this method lowers to the insertCoin builtin, which requires the value to be in canonical form (strictly ascending keys, no zero amounts, no empty inner maps, keys at most 32 bytes, amounts within the signed 128-bit range) and rejects a cs/tn longer than 32 bytes (for non-zero amount) or an amount outside the signed 128-bit range; a violation makes the script fail. See Options.valueBuiltins.

Value parameters

amount

The new amount; zero deletes the coin

cs

The policy id of the coin to set

tn

The token name of the coin to set

Attributes

Example
 val value = Value(utf8"pid", utf8"TOKEN", BigInt(5))
 value.insertCoin(utf8"pid", utf8"TOKEN", BigInt(7)) === Value(utf8"pid", utf8"TOKEN", BigInt(7))
 value.insertCoin(utf8"pid", utf8"TOKEN", BigInt(0)) === Value.zero
inline def isPositive: Boolean

Checks if this Value is non-zero and positive, meaning it contains at least one token or currency symbol with a non-zero positive amount and all amounts are positive.

Checks if this Value is non-zero and positive, meaning it contains at least one token or currency symbol with a non-zero positive amount and all amounts are positive.

Attributes

Returns

true if the Value is non-empty and has all positive amounts, false otherwise

inline def isZero: Boolean

Checks if this Value is zero, meaning it contains no tokens or currency symbols.

Checks if this Value is zero, meaning it contains no tokens or currency symbols.

Attributes

Returns

true if the Value is empty, false otherwise

Example
 val value = Value.zero
 value.isZero === true
 val nonZeroValue = Value.lovelace(BigInt(1000000))
 nonZeroValue.isZero === false
def lovelaceAmount: BigInt

Returns the amount of Lovelace in this Value.

Returns the amount of Lovelace in this Value.

This is more efficient version of getLovelace. If the Value contains no lovelace, it fails.

This is useful when we are guaranteed to have lovelace in a Value and want to avoid the overhead of a map lookup. For example, Value in TxOut always has lovelace.

Attributes

Returns

The amount of Lovelace in this Value

Example
 val value = Value.lovelace(BigInt(1000000))
 value.lovelaceAmount === BigInt(1000000)
inline def nonZero: Boolean

Checks if this Value is non-zero, meaning it contains at least one token or currency symbol with a non-zero amount.

Checks if this Value is non-zero, meaning it contains at least one token or currency symbol with a non-zero amount.

Attributes

Returns

true if the Value is non-empty, false otherwise

Example
 val value = Value.zero
 value.nonZero === false
 val nonZeroValue = Value.lovelace(BigInt(1000000))
 nonZeroValue.nonZero === true

A list of all policy ids in that scalus.cardano.onchain.plutus.v1.Value with non-zero tokens.

A list of all policy ids in that scalus.cardano.onchain.plutus.v1.Value with non-zero tokens.

Attributes

Returns

A list of sorted scalus.cardano.onchain.plutus.v1.PolicyId

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 value.policyIds ===
 List.Cons(
    Value.adaPolicyId,
    List.Cons(
      ByteString.fromString("ff"),
      List.Nil
    )
 )
def quantityOf(cs: PolicyId, tn: TokenName): BigInt

Gets the amount of a specific token in a policy id from a Value.

Gets the amount of a specific token in a policy id from a Value.

Returns the token amount for the given policy id and token name pair. If either the policy id or token name is not found, returns zero.

At PV11 (vanRossem) with Options.valueBuiltins enabled (the default) this lowers to the CIP-153 lookupCoin builtin, which requires the value in canonical form (strictly ascending keys, no zero amounts, no empty inner maps, keys at most 32 bytes, amounts within the signed 128-bit range) and makes the script fail otherwise. See Options.valueBuiltins.

Value parameters

cs

The policy id to look up

tn

The token name to look up within that policy id

Attributes

Returns

The amount of the specified token, or zero if not found

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 value.quantityOf(Value.adaPolicyId, Value.adaTokenName) === BigInt(1000000)
 value.quantityOf(ByteString.fromString("ff"), ByteString.fromString("TOKEN")) === BigInt(100)
 value.quantityOf(ByteString.fromString("missing"), ByteString.fromString("TOKEN")) === BigInt(0)
inline def showDebug: String

Extension alias for Value.debugToString.

Extension alias for Value.debugToString.

Attributes

def tokens(cs: PolicyId): SortedMap[TokenName, BigInt]

Get all tokens associated with a given policy.

Get all tokens associated with a given policy.

Returns the token SortedMap for the given policy id. If the policy id is not found, returns an empty SortedMap.

Value parameters

cs

The policy id to look up

Attributes

Returns

The SortedMap of the specified token, or an empty SortedMap if not found

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (utf8"ff", List.Cons((utf8"TOKEN", BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 value.quantityOf(Value.adaPolicyId) === SortedMap.singleton(Value.adaTokenName, BigInt(1000000))
 value.quantityOf(utf8"ff") === SortedMap.singleton(utf8"TOKEN", BigInt(100))
 value.quantityOf(utf8"missing") === SortedMap.empty
inline def unary_-: Value

Extension alias for Value.negate.

Extension alias for Value.negate.

Attributes

Returns a new Value with the ADA/Lovelace coin removed.

Returns a new Value with the ADA/Lovelace coin removed.

Deletes the (adaPolicyId, adaTokenName) coin - and the empty-policy entry with it when it held no other token - while preserving all other tokens. Equivalent to insertCoin(adaPolicyId, adaTokenName, 0) and shares its PV11 lowering to the CIP-153 insertCoin builtin, including its canonical-form requirement. See Options.valueBuiltins.

Attributes

Returns

A new Value without any Lovelace tokens

Example
 val value = Value.fromList(
   List.Cons(
     (Value.adaPolicyId, List.Cons((Value.adaTokenName, BigInt(1000000)), List.Nil)),
     List.Cons(
       (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
       List.Nil
     )
   )
 )
 val withoutAda = Value.fromList(
   List.Cons(
     (ByteString.fromString("ff"), List.Cons((ByteString.fromString("TOKEN"), BigInt(100)), List.Nil)),
     List.Nil
   )
 )
 value.withoutLovelace === withoutAda

Inherited extensions

extension (v: Value)

Converts this Value to a scalus.cardano.ledger.Value

Converts this Value to a scalus.cardano.ledger.Value

Attributes

Throws
IllegalArgumentException

if AssetName exceeds 32 bytes (thrown by AssetName constructor)

Inherited from:
ValueOffchainOps (hidden)