From d90879ce553c99ac3db804415c0a85b79883fdd9 Mon Sep 17 00:00:00 2001 From: SIDANPillow Date: Mon, 19 Aug 2024 18:51:05 +0800 Subject: [PATCH 1/2] feat:add value utilities documentation and sample code --- .../test/data/value/comparator.test.ts | 58 ++++++++++- .../test/data/value/convertor.test.ts | 99 +++++++++++++++++++ 2 files changed, 152 insertions(+), 5 deletions(-) diff --git a/packages/mesh-common/test/data/value/comparator.test.ts b/packages/mesh-common/test/data/value/comparator.test.ts index f035ca054..da30e69a6 100644 --- a/packages/mesh-common/test/data/value/comparator.test.ts +++ b/packages/mesh-common/test/data/value/comparator.test.ts @@ -21,6 +21,24 @@ describe("MeshValue class", () => { const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); expect(value.geq(target)).toBe(true); }); + + it("should return false if there is missing unit in the target value", () => { + const value = new MeshValue({ lovelace: 20n }); + const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + expect(value.geq(target)).toBe(false); + }); + + it("should return false if there is missing unit in the value.value", () => { + const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + const target = new MeshValue({ lovelace: 20n }); + expect(value.geq(target)).toBe(true); + }); + + it("should return false if there is missing unit in both value.value and target.value", () => { + const value = new MeshValue({ lovelace: 20n, somethingelse: 10n }); + const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + expect(value.geq(target)).toBe(false); + }); }); describe("geqUnit", () => { it("should return true if the value is greater than or equal to the target value for a specific unit", () => { @@ -46,8 +64,14 @@ describe("MeshValue class", () => { it("should return false if the unit does not exist in value.value", () => { const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); - const target = new MeshValue({ somethingElse: 5n }); - expect(value.geqUnit("somethingElse", target)).toBe(false); + const target = new MeshValue({ lovelace: 20n }); + expect(value.geqUnit(mockUnit, target)).toBe(false); + }); + + it("should return false if the unit does not exist in other.value", () => { + const value = new MeshValue({ lovelace: 20n }); + const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + expect(value.geqUnit(mockUnit, target)).toBe(false); }); }); describe("leq", () => { @@ -68,6 +92,24 @@ describe("MeshValue class", () => { const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); expect(value.leq(target)).toBe(true); }); + + it("should return false if there is missing unit in the target value", () => { + const value = new MeshValue({ lovelace: 20n }); + const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + expect(value.leq(target)).toBe(true); + }); + + it("should return false if there is missing unit in the value.value", () => { + const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + const target = new MeshValue({ lovelace: 20n }); + expect(value.leq(target)).toBe(false); + }); + + it("should return false if there is missing unit in both value.value and target.value", () => { + const value = new MeshValue({ lovelace: 20n, somethingelse: 10n }); + const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); + expect(value.leq(target)).toBe(false); + }); }); describe("leqUnit", () => { it("should return true if the value is less than or equal to the target value for a specific unit", () => { @@ -91,10 +133,16 @@ describe("MeshValue class", () => { expect(value.leqUnit(mockUnit, target)).toBe(true); }); - it("should return false if the unit does not exist in value.value", () => { + it("should return false if the unit does not exist in other.value", () => { + const value = new MeshValue({ lovelace: 20n }); + const target = new MeshValue({ lovelace: 5n, [mockUnit]: 10n }); + expect(value.leqUnit(mockUnit, target)).toBe(false); + }); + + it("should return false if the unit does not exist in this.value", () => { const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n }); - const target = new MeshValue({ somethingElse: 5n }); - expect(value.leqUnit("somethingElse", target)).toBe(false); + const target = new MeshValue({ lovelace: 20n }); + expect(value.leqUnit(mockUnit, target)).toBe(false); }); }); describe("isEmpty", () => { diff --git a/packages/mesh-common/test/data/value/convertor.test.ts b/packages/mesh-common/test/data/value/convertor.test.ts index bfff721f7..0ce51cebf 100644 --- a/packages/mesh-common/test/data/value/convertor.test.ts +++ b/packages/mesh-common/test/data/value/convertor.test.ts @@ -1,6 +1,8 @@ import { Asset, + assocMap, byteString, + currencySymbol, dict, Dict, Integer, @@ -8,6 +10,7 @@ import { MeshValue, MValue, mValue, + tokenName, Value, value, } from "@meshsdk/common"; @@ -170,4 +173,100 @@ describe("MeshValue class", () => { expect(JSON.stringify(val)).toBe(JSON.stringify(assets)); }); }); + describe("toData", () => { + test("Empty Value", () => { + const val: Asset[] = []; + const plutusValue: Value = value(val); + const data = MeshValue.fromValue(plutusValue).toData(); + const expected: MValue = mValue(val); + expect(JSON.stringify(expected)).toBe(JSON.stringify(data)); + }); + + test("Multiple Assets with Same Policy", () => { + 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); + + expect(JSON.stringify(expected)).toBe(JSON.stringify(data)); + }); + + test("Mixed Assets", () => { + const val: Asset[] = [ + { unit: "lovelace", quantity: "1000000" }, + { + unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234", + quantity: "567", + }, + { + unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461", + quantity: "345", + }, + { + unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234", + quantity: "100", + }, + ]; + const plutusValue: Value = value(val); + const data = MeshValue.fromValue(plutusValue).toData(); + const expected: MValue = mValue(val); + expect(JSON.stringify(expected)).toBe(JSON.stringify(data)); + }); + + test("Single Asset with Large Quantity", () => { + const val: Asset[] = [{ unit: "lovelace", quantity: "1000000000000" }]; + const plutusValue: Value = value(val); + const data = MeshValue.fromValue(plutusValue).toData(); + const expected: MValue = mValue(val); + expect(JSON.stringify(expected)).toBe(JSON.stringify(data)); + }); + }); + describe("toJSON", () => { + test("should correctly convert MeshValue to JSON with multiple assets", () => { + 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(); + expect(JSON.stringify(jsonValue)).toEqual(JSON.stringify(expectedValue)); + }); + + test("should correctly convert MeshValue to JSON with no asset", () => { + const assets: Asset[] = []; + + const expectedValue = assocMap([]); + + const meshValue = new MeshValue(); + meshValue.toAssets = () => assets; + + const jsonValue = meshValue.toJSON(); + expect(JSON.stringify(jsonValue)).toEqual(JSON.stringify(expectedValue)); + }); + }); }); From 617d9398d197a59da2d5446a37b22ba0af077fd0 Mon Sep 17 00:00:00 2001 From: SIDANPillow Date: Mon, 19 Aug 2024 18:53:51 +0800 Subject: [PATCH 2/2] feat:add value utilities documentation and sample code --- apps/playground/src/data/links-data.ts | 8 + .../src/pages/apis/data/value/accessor.tsx | 104 +++++ .../src/pages/apis/data/value/common.ts | 4 + .../src/pages/apis/data/value/comparator.tsx | 305 ++++++++++++++ .../src/pages/apis/data/value/convertor.tsx | 397 ++++++++++++++++++ .../src/pages/apis/data/value/index.tsx | 114 +++++ .../src/pages/apis/data/value/operators.tsx | 268 ++++++++++++ packages/mesh-common/src/data/value.ts | 2 +- 8 files changed, 1201 insertions(+), 1 deletion(-) create mode 100644 apps/playground/src/pages/apis/data/value/accessor.tsx create mode 100644 apps/playground/src/pages/apis/data/value/common.ts create mode 100644 apps/playground/src/pages/apis/data/value/comparator.tsx create mode 100644 apps/playground/src/pages/apis/data/value/convertor.tsx create mode 100644 apps/playground/src/pages/apis/data/value/index.tsx create mode 100644 apps/playground/src/pages/apis/data/value/operators.tsx diff --git a/apps/playground/src/data/links-data.ts b/apps/playground/src/data/links-data.ts index 97fd5cf2d..620978290 100644 --- a/apps/playground/src/data/links-data.ts +++ b/apps/playground/src/data/links-data.ts @@ -27,6 +27,13 @@ export const metaDataJson = { link: "/apis/data/json", icon: Bars3Icon, }; +// todoß +export const metaDataValue = { + title: "Value", + desc: "Manipulate Cardano Value Easily", + link: "/apis/data/value", + icon: Bars3Icon, +}; export const metaDataCbor = { title: "CBOR Data", desc: "Parse and manipulate Cardano data with CBOR", @@ -44,6 +51,7 @@ export const linksData: MenuItem[] = [ metaOverview, metaDataMesh, metaDataJson, + metaDataValue, // metaDataCbor, // metaDataUtils, ]; diff --git a/apps/playground/src/pages/apis/data/value/accessor.tsx b/apps/playground/src/pages/apis/data/value/accessor.tsx new file mode 100644 index 000000000..b41871623 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/accessor.tsx @@ -0,0 +1,104 @@ +import { MeshValue } from "@meshsdk/common"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; +import { mockUnit } from "./common"; + +export default function ValueAccessor() { + return ( + + ); +} + +function Left() { + return ( + <> + + + + ); +} + +function Section1() { + return ( + <> +

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

+
    +
  • + unit - the unit of the assets e.g. lovelace +
  • +
+ + ); +} + +function Section2() { + return ( + <> +

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

+ + ); +} + +function Right() { + return ( + <> + + + + ); +} + +function getCode() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue({ lovelace: 20n }); + return value.get("lovelace"); + `; +} + +async function runGetDemo() { + const value = new MeshValue({ lovelace: 20n }); + value.get("lovelace"); + return value; +} + +function getCode2() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue({ + lovelace: 20n, + [mockUnit]: 10n, + }); + return value.units(); + `; +} + +async function runUnitsDemo() { + const value = new MeshValue({ + lovelace: 20n, + [mockUnit]: 10n, + }); + return value.units(); +} diff --git a/apps/playground/src/pages/apis/data/value/common.ts b/apps/playground/src/pages/apis/data/value/common.ts new file mode 100644 index 000000000..8c531bda7 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/common.ts @@ -0,0 +1,4 @@ +export const mockPolicyId = + "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c"; +export const mockAssetName = "000643b04d65736820676f6f64"; +export const mockUnit = mockPolicyId + mockAssetName; diff --git a/apps/playground/src/pages/apis/data/value/comparator.tsx b/apps/playground/src/pages/apis/data/value/comparator.tsx new file mode 100644 index 000000000..2287154b7 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/comparator.tsx @@ -0,0 +1,305 @@ +import { + Asset, + byteString, + dict, + Dict, + Integer, + integer, + MeshValue, + MValue, + mValue, + Value, + value, +} from "@meshsdk/common"; +import { + assocMap, + AssocMap, + CurrencySymbol, + currencySymbol, + mBool, + TokenName, + tokenName, +} from "@meshsdk/core"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; +import { mockUnit } from "./common"; + +export default function ValueComparator() { + return ( + + ); +} + +function Left() { + return ( + <> + + + + + + + ); +} + +function Section1() { + return ( + <> +

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

+
    +
  • + other - The MeshValue to compare against +
  • +
+ + ); +} + +function Section2() { + return ( + <> +

+ 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 +
  • +
+ + ); +} + +function Section3() { + return ( + <> +

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

+
    +
  • + other - The MeshValue to compare against +
  • +
+ + ); +} + +function Section4() { + return ( + <> +

+ 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 +
  • +
+ + ); +} + +function Section5() { + return ( + <> +

+ isEmpty Check if the value is empty +

+ + ); +} + +function Right() { + return ( + <> + + + + + + + ); +} + +function getCode() { + return ` + 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); + `; +} + +async function rungeqDemo() { + const value = new MeshValue({ + lovelace: 20n, + c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: + 10n, + }); + const target = new MeshValue({ + lovelace: 10n, + c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: + 5n, + }); + return value.geq(target); +} + +function getCode2() { + return ` + 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 }; + `; +} + +async function runmgeqUnitDemo() { + 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 }; +} + +function getCode3() { + return ` + 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); + `; +} + +async function runleqDemo() { + const value = new MeshValue({ + lovelace: 20n, + c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: + 10n, + }); + const target = new MeshValue({ + lovelace: 30n, + c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64: + 15n, + }); + return value.leq(target); +} + +function getCode4() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue(); + value.value = { lovelace: 20n, [mockUnit]: 10n }; + value.negateAssets([ + { unit: "lovelace", quantity: "5" }, + { unit: mockUnit, quantity: "3" }, + ]); + return value.value; + `; +} + +async function runleqUnitDemo() { + 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 }; +} + +function getCode5() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue(); + return value.isEmpty(); + `; +} + +async function runisEmptyDemo() { + const value = new MeshValue(); + return value.isEmpty(); +} diff --git a/apps/playground/src/pages/apis/data/value/convertor.tsx b/apps/playground/src/pages/apis/data/value/convertor.tsx new file mode 100644 index 000000000..b0c896001 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/convertor.tsx @@ -0,0 +1,397 @@ +import { + Asset, + byteString, + dict, + Dict, + Integer, + integer, + MeshValue, + MValue, + mValue, + Value, + value, +} from "@meshsdk/common"; +import { + assocMap, + AssocMap, + CurrencySymbol, + currencySymbol, + mBool, + TokenName, + tokenName, +} from "@meshsdk/core"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; +import { mockUnit } from "./common"; + +export default function ValueConvertor() { + return ( + + ); +} + +function Left() { + return ( + <> + + + + + + + + + ); +} + +function Section1() { + return ( + <> +

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

+
    +
  • + val - Asset[] to convert +
  • +
+ + ); +} + +function Section2() { + return ( + <> +

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

+
    +
  • + val - Asset[] to convert +
  • +
+ + ); +} + +function Section3() { + return ( + <> +

+ fromAssets converts assets into MeshValue with parameters: +

+
    +
  • + assets - the assets to convert +
  • +
+ + ); +} + +function Section4() { + return ( + <> +

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

+
    +
  • + plutusValue - Convert Value (the JSON representation of Cardano + data Value) into MeshValue +
  • +
+ + ); +} + +function Section5() { + return ( + <> +

+ toAssets converts the MeshValue object into an array of + Asset +

+ + ); +} + +function Section6() { + return ( + <> +

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

+ + ); +} + +function Section7() { + return ( + <> +

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

+ + ); +} + +function Right() { + return ( + <> + + + + + + + + + ); +} + +function getCode() { + return ` + const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }]; + const datum: Value = value(val); + const nameMap = dict([[byteString(""), integer(1000000)]]); + const valMap = dict>([[byteString(""), nameMap]]); + if (JSON.stringify(datum) === JSON.stringify(valMap)) { + return true; + } + `; +} + +async function runvalueDemo() { + const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }]; + const datum: Value = value(val); + const nameMap = dict([[byteString(""), integer(1000000)]]); + const valMap = dict>([[byteString(""), nameMap]]); + if (JSON.stringify(datum) === JSON.stringify(valMap)) { + return true; + } +} + +function getCode2() { + return ` + 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; + `; +} + +async function runmValueDemo() { + 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; + } +} + +function getCode3() { + return ` + import { MeshValue } from "@meshsdk/common"; + const assets: Asset[] = [ + { unit: "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64", quantity: "100" }, + { unit: "lovelace", quantity: "10" }, + ]; + const value = MeshValue.fromAssets(assets); + return value; + `; +} + +async function runfromAssetsDemo() { + const assets: Asset[] = [ + { + unit: "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c000643b04d65736820676f6f64", + quantity: "100", + }, + { unit: "lovelace", quantity: "10" }, + ]; + const value = MeshValue.fromAssets(assets); + return value; +} + +function getCode4() { + return ` + 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; + `; +} + +async function runtoAssetsDemo() { + const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }]; + const plutusValue: Value = value(val); + const assets: Asset[] = MeshValue.fromValue(plutusValue).toAssets(); + return assets; +} + +function getCode5() { + return ` + 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; + `; +} + +async function runfromValueDemo() { + const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }]; + const plutusValue: Value = value(val); + const assets: Asset[] = MeshValue.fromValue(plutusValue).toAssets(); + return assets; +} + +function getCode6() { + return ` + 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; + } + `; +} + +async function runtoDataDemo() { + 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; + } +} + +function getCode7() { + return ` + 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; + } + `; +} + +async function runtoJSONDemo() { + 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; + } +} diff --git a/apps/playground/src/pages/apis/data/value/index.tsx b/apps/playground/src/pages/apis/data/value/index.tsx new file mode 100644 index 000000000..1a60aa1e4 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/index.tsx @@ -0,0 +1,114 @@ +import type { NextPage } from "next"; + +import SidebarFullwidth from "~/components/layouts/sidebar-fullwidth"; +import TitleIconDescriptionBody from "~/components/sections/title-icon-description-body"; +import Metatags from "~/components/site/metatags"; +import { metaDataValue } from "~/data/links-data"; +import ValueAccessor from "./accessor"; +import ValueComparator from "./comparator"; +import ValueConvertor from "./convertor"; +import ValueOperator from "./operators"; + +const ReactPage: NextPage = () => { + const sidebarItems = [ + { label: "Value - Convertor", to: "ValueConvertor" }, + { label: "Value - Operator", to: "ValueOperator" }, + { label: "Value - Accessor", to: "ValueAccessor" }, + { label: "Value - Comparator", to: "ValueComparator" }, + ]; + + return ( + <> + + + +

+ Mesh provides a full set of value methods to help accessing, + comparing, converting, and operating Cardano data. +

+

Value Types Support

+

Convertor

+
    +
  • + value +
  • +
  • + mValue +
  • +
  • + fromAssets +
  • +
  • + toAssets +
  • +
  • + fromValue +
  • +
  • + toData +
  • +
  • + toJSON +
  • +
+

Operators

+
    +
  • + addAsset +
  • +
  • + addAssets +
  • +
  • + negateAsset +
  • +
  • + negateAssets +
  • +
  • + merge +
  • +
+

Accessor

+
    +
  • + get +
  • +
  • + units +
  • +
+

Comparator

+
    +
  • + geq +
  • +
  • + geqUnit +
  • +
  • + leq +
  • +
  • + leqUnit +
  • +
  • + isEmpty +
  • +
+
+ + + + + +
+ + ); +}; + +export default ReactPage; diff --git a/apps/playground/src/pages/apis/data/value/operators.tsx b/apps/playground/src/pages/apis/data/value/operators.tsx new file mode 100644 index 000000000..e019d34b0 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/operators.tsx @@ -0,0 +1,268 @@ +import { + Asset, + byteString, + dict, + Dict, + Integer, + integer, + MeshValue, + MValue, + mValue, + Value, + value, +} from "@meshsdk/common"; +import { + assocMap, + AssocMap, + CurrencySymbol, + currencySymbol, + mBool, + TokenName, + tokenName, +} from "@meshsdk/core"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; +import { mockUnit } from "./common"; + +export default function ValueOperator() { + return ( + + ); +} + +function Left() { + return ( + <> + + + + + + + ); +} + +function Section1() { + return ( + <> +

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

+
    +
  • + asset - Asset to add +
  • +
+ + ); +} + +function Section2() { + return ( + <> +

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

+
    +
  • + assets - Asset[] to add +
  • +
+ + ); +} + +function Section3() { + return ( + <> +

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

+
    +
  • + asset - Asset to substract +
  • +
+ + ); +} + +function Section4() { + return ( + <> +

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

+
    +
  • + assets - Asset[] to substract +
  • +
+ + ); +} + +function Section5() { + return ( + <> +

+ merge Merge the given values +

+
    +
  • + values - MeshValue to merge +
  • +
+ + ); +} + +function Right() { + return ( + <> + + + + + + + ); +} + +function getCode() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue(); + const singleAsset: Asset = { unit: mockUnit, quantity: "100" }; + value.addAsset(singleAsset); + return value.value; + `; +} + +async function runaddAssetDemo() { + const value = new MeshValue(); + const singleAsset: Asset = { unit: mockUnit, quantity: "100" }; + value.addAsset(singleAsset); + return value.value; +} + +function getCode2() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue(); + const assets: Asset[] = [ + { unit: mockUnit, quantity: "100" }, + { unit: "lovelace", quantity: "10" }, + { unit: mockUnit, quantity: "100" }, + { unit: "lovelace", quantity: "10" }, + ]; + value.addAssets(assets); + return value.value; + `; +} + +async function runmaddAssetsDemo() { + const value = new MeshValue(); + const assets: Asset[] = [ + { unit: mockUnit, quantity: "100" }, + { unit: "lovelace", quantity: "10" }, + { unit: mockUnit, quantity: "100" }, + { unit: "lovelace", quantity: "10" }, + ]; + value.addAssets(assets); + return value.value; +} + +function getCode3() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue(); + value.value = { lovelace: 10n }; + value.negateAsset({ unit: "lovelace", quantity: "5" }); + return value.value; + `; +} + +async function runnegateAssetDemo() { + const value = new MeshValue(); + value.value = { lovelace: 10n }; + value.negateAsset({ unit: "lovelace", quantity: "5" }); + return value.value; +} + +function getCode4() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value = new MeshValue(); + value.value = { lovelace: 20n, [mockUnit]: 10n }; + value.negateAssets([ + { unit: "lovelace", quantity: "5" }, + { unit: mockUnit, quantity: "3" }, + ]); + return value.value; + `; +} + +async function runtonegateAssetsDemo() { + const value = new MeshValue(); + value.value = { lovelace: 20n, [mockUnit]: 10n }; + value.negateAssets([ + { unit: "lovelace", quantity: "5" }, + { unit: mockUnit, quantity: "3" }, + ]); + return value.value; +} + +function getCode5() { + return ` + import { MeshValue } from "@meshsdk/common"; + const value1 = new MeshValue(); + value1.value = { lovelace: 20n, [mockUnit]: 10n }; + const value2 = new MeshValue(); + value2.value = { lovelace: 10n, [mockUnit]: 5n }; + return value1.merge(value2).value; + `; +} + +async function runmergeDemo() { + const value1 = new MeshValue(); + value1.value = { lovelace: 20n, [mockUnit]: 10n }; + const value2 = new MeshValue(); + value2.value = { lovelace: 10n, [mockUnit]: 5n }; + return value1.merge(value2).value; +} diff --git a/packages/mesh-common/src/data/value.ts b/packages/mesh-common/src/data/value.ts index cca749b0c..f832c3b33 100644 --- a/packages/mesh-common/src/data/value.ts +++ b/packages/mesh-common/src/data/value.ts @@ -197,7 +197,7 @@ export class MeshValue { * @returns boolean */ leq = (other: MeshValue): boolean => { - return Object.keys(other.value).every((key) => this.leqUnit(key, other)); + return Object.keys(this.value).every((key) => this.leqUnit(key, other)); }; /**