Value

Manipulate Cardano Value Easily

We all know the pain of conducting Value operation in Cardano. Mesh provides a full set of value methods to help converting, operating, accessing and comparing Cardano data.

Value Types Support

Convertors

Convertor functions provide utilities around round trip among Cardano onchain data and off chain JSON and Data type.

Operators

Operator functions provide utilities into performing value manipulation. They are useful in DApps which check against value payment involving calculation in value.

Accessor

Accessor functions provide utilities in obtaining keys or values of the Value type.

Comparator

Comparator functions provide utilities in comparing different Value. It helps with offchain validation before using for transaction building.

Convertor - converts assets into Cardano data Value in JSON

value converts assets into Cardano data Value in JSON with parameters:

  • assets - Asset[] to convert
value

Converts assets into MeshValue with parameters - asset[] e.g. ada value, simple token token, complex value.


  const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }];
  const datum: Value = value(val);
  const nameMap = dict<Integer>([[byteString(""), integer(1000000)]]);
  const valMap = dict<Dict<Integer>>([[byteString(""), nameMap]]);
  if (JSON.stringify(datum) === JSON.stringify(valMap)) {
  return true; 
  

Convertor - converts assets into Cardano data Value in Mesh Data type

mValue converts assets into Cardano data value in Mesh Data type with parameters:

  • assets - Asset[] to convert
mValue

Converts assets into MeshValue with parameters - asset[] e.g. ada value, simple token token, complex value.


  const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }];
  const datum: MValue = mValue(val); 
  const nameMap = new Map().set("", 1000000);
  const valMap = new Map().set("", nameMap);
  if (JSON.stringify(datum) === JSON.stringify(valMap)) {
  return true;
    

Convertor - converts assets into MeshValue with parameters - asset[]

fromAssets converts assets into MeshValue with parameters:

  • assets - the assets to convert
fromAssets

Converts assets into MeshValue with parameters - asset[] e.g. ada value, simple token token, complex value.


  import { MeshValue } from "@meshsdk/common";
  const assets: Asset[] = [
    { unit: "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64", quantity: "100" },
    { unit: "lovelace", quantity: "10" },
  ];
  const value = MeshValue.fromAssets(assets);
  return value;
  

Convertor - converts the MeshValue object into an array of Asset

toAssets Convert the MeshValue object into an array of Asset

toAssets

Converts the MeshValue object into an array of Asset


  import { MeshValue } from "@meshsdk/common";
  const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }];
  const plutusValue: Value = value(val);
  const assets: Asset[] = MeshValue.fromValue(plutusValue).toAssets();
  return assets;
  

Convertor - converts Value (the JSON representation of Cardano data Value) into MeshValue

fromValue Convert Value (the JSON representation of Cardano data Value) into MeshValue with parameters:

  • plutusValue - the value to convert
fromValue

Convert Value (the JSON representation of Cardano data Value) into MeshValue.


  import { MeshValue } from "@meshsdk/common";
  const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }];
  const plutusValue: Value = value(val);
  const assets: Asset[] = MeshValue.fromValue(plutusValue).toAssets();
  return assets;
  

Convertor - converts the MeshValue object into Cardano data Value in Mesh Data type

toData Convert the MashValue object into Cardano data Value in Mesh Data type

toData

Converts the MeshValue object into Cardano data Value in Mesh Data type


  import { MeshValue } from "@meshsdk/common";
  const val: Asset[] = [
    {
      unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
      quantity: "100",
    },
    {
      unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
      quantity: "200",
    },
  ];
  const plutusValue: Value = value(val);
  const data = MeshValue.fromValue(plutusValue).toData();
  const expected: MValue = mValue(val);
  if (JSON.stringify(expected) === JSON.stringify(data)) {
    return true;
    

Convertor - converts the MeshValue object into a JSON representation of Cardano data Value

toJSON Converts the MeshValue object into a JSON representation of Cardano data Value

toJSON

Converts the MeshValue object into a JSON representation of Cardano data Value


  import { MeshValue } from "@meshsdk/common";
  const assets: Asset[] = [
  { unit: "lovelace", quantity: "1000000" },
  {
    unit: "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64",
    quantity: "500",
  },
  ];

  const expectedValue = assocMap([
    [currencySymbol(""), assocMap([[tokenName(""), integer(1000000)]])],
    [
      currencySymbol(
        "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c",
      ),
      assocMap([[tokenName("000643b04d65736820676f6f64"), integer(500)]]),
    ],
  ]);

  const meshValue = new MeshValue();
  meshValue.toAssets = () => assets;

  const jsonValue = meshValue.toJSON();
  if (JSON.stringify(jsonValue) === JSON.stringify(expectedValue)) {
    return true;
  }
  

Operator - add an asset to the Value class's value record with parameters - asset

addAsset Add an asset to the Value class's value record with parameters:

    asset - Asset to add
addAsset

Add an asset to the Value class's value record with parameters - asset


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue();
  const singleAsset: Asset = { unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234", quantity: "100" };
  value.addAsset(singleAsset);
  return value.value;
    

Operator - add an array of assets to the Value class's value record with parameters - assets

addAssets Add an array of assets to the Value class's value record with parameters:

    assets - Asset[] to add
addAssets

Add an array of assets to the Value class's value record with parameters - assets


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue();
  const assets: Asset[] = [
      { unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234", quantity: "100" },
      { unit: "lovelace", quantity: "10" },
      { unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234", quantity: "100" },
      { unit: "lovelace", quantity: "10" },
  ];
  value.addAssets(assets);
  return value.value;
  

Operator - substract an asset from the Value class's value record with parameters - asset

negateAsset Substract an asset from the Value class's value record with parameters:

    asset - Asset to substract
negateAsset

Substract an asset from the Value class's value record with parameters - asset


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue();
  value.value = { lovelace: 10n };
  value.negateAsset({ unit: "lovelace", quantity: "5" });
  return value.value;
  

Operator - substract an array of assets from the Value class's value record with parameters - assets

negateAssets Substract an array of assets from the Value class's value record with parameters:

    assets - Asset[] to substract
negateAssets

Substract an array of assets from the Value class's value record with parameters - assets


  const value = new MeshValue();
  value.value = { lovelace: 20n, "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234": 10n };
  value.negateAssets([
      { unit: "lovelace", quantity: "5" },
      { unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234", quantity: "3" },
  ]);
  return value.value;
  

Operator - merge the given values with parameters - values

merge Merge the given values

    values - The other values to merge
merge

Merge the given values with parameters - values


  const value1 = new MeshValue();
  value1.value = { lovelace: 20n, "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234": 10n };
  const value2 = new MeshValue();
  value2.value = { lovelace: 10n, "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234": 5n };
  return value1.merge(value2).value;
  

Accessor - get the quantity of asset object per lovelace unit

get get the quantity of asset object per unit, with parameters

  • unit - the unit to get the quantity of the assets e.g. lovelace
get

Get the quantity of asset object per unit


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue({ lovelace: 20n });
  value.get("lovelace");
  return value;
 

Accessor - get all asset units with no parameters needed

units get all asset units with no parameters (e.g. unit) needed

units

Get all asset units with no parameters needed


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue({
      lovelace: 20n,
      "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234": 10n,
      });
  return value.units();
  

Comparator - check if the value is greater than or equal to another value with parameters - other

geq Check if the value is greater than or equal to another value with parameters:

  • other - The MeshValue to compare against
geq

Check if the value is greater than or equal to another value with parameters - other


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue({
    lovelace: 20n,
    c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: 10n,
    });
  const target = new MeshValue({
    lovelace: 10n,
    c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: 5n,
    });
  return value.geq(target);
  

Comparator - check if the value is greater than or equal to another value with parameters - unit, other

geqUnit Check if the value is greater than or equal to another value with parameters:

  • unit - The unit to compare
  • other - The MeshValue to compare against
geqUnit

Check if the value is greater than or equal to another value with parameters - unit, other


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue({
      lovelace: 20n,
      c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64:
          10n,
      });
  const target = new MeshValue({
      lovelace: 10n,
      c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64:
          5n,
      });
  const resultLovelace = value.geqUnit("lovelace", target);
  const resultmockvalue = value.geqUnit(
      "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64",
      target,
      );
  
  return { resultLovelace, resultmockvalue };
  }
  

Comparator - check if the value is less than or equal to another value with parameters - other

leq Check if the value is less than or equal to another value with parameters:

  • other - The MeshValue to compare against
leq

Check if the value is less than or equal to another value with parameters - other


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue({
      lovelace: 20n,
      c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64:
          10n,
      });
  const target = new MeshValue({
      lovelace: 30n,
      c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64:
          15n,
      });
  return value.leq(target);
  

Comparator - check if the specific unit of value is less than or equal to that unit of another value with parameters - unit, other

leqUnit Check if the specific unit of value is less than or equal to that unit of another value with parameters:

  • unit - The unit to compare
  • other - The MeshValue to compare against
lequnit

Check if the specific unit of value is less than or equal to that unit of another value with parameters - unit, other


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue({
   lovelace: 20n,
   c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: 10n,
  });
  const target = new MeshValue({
    lovelace: 30n,
    c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: 15n,
  });
  const resultLovelace = value.leqUnit("lovelace", target);
  const resultmockvalue = value.leqUnit(
    "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64",
    target,
  );

  return { resultLovelace, resultmockvalue };
  

Comparator - check if the value is empty

isEmpty Check if the value is empty

isEmpty

Check if the value is empty


  import { MeshValue } from "@meshsdk/common";
  const value = new MeshValue();
  return value.isEmpty();