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..dfebc7447 --- /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 "./"; + +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/comparator.tsx b/apps/playground/src/pages/apis/data/value/comparator.tsx new file mode 100644 index 000000000..cfbf70fba --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/comparator.tsx @@ -0,0 +1,283 @@ +import { MeshValue } from "@meshsdk/common"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; + +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..e62b2e2b2 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/convertor.tsx @@ -0,0 +1,388 @@ +import { + Asset, + byteString, + dict, + Dict, + Integer, + integer, + MeshValue, + MValue, + mValue, + Value, + value, +} from "@meshsdk/common"; +import { assocMap, currencySymbol, tokenName } from "@meshsdk/core"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; + +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 index 89f1d1a6a..0e3e0105b 100644 --- a/apps/playground/src/pages/apis/data/value/index.tsx +++ b/apps/playground/src/pages/apis/data/value/index.tsx @@ -157,3 +157,8 @@ const ReactPage: NextPage = () => { }; export default ReactPage; + +export const mockPolicyId = + "c21d710605bb00e69f3c175150552fc498316d80e7efdb1b186db38c"; +export const mockAssetName = "000643b04d65736820676f6f64"; +export const mockUnit = mockPolicyId + mockAssetName; 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..24687ec09 --- /dev/null +++ b/apps/playground/src/pages/apis/data/value/operators.tsx @@ -0,0 +1,247 @@ +import { Asset, MeshValue } from "@meshsdk/common"; + +import LiveCodeDemo from "~/components/sections/live-code-demo"; +import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; +import { mockUnit } from "./"; + +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/apps/playground/src/pages/smart-contracts/vesting/deposit-fund.tsx b/apps/playground/src/pages/smart-contracts/vesting/deposit-fund.tsx index 6dad39bec..4a48f4379 100644 --- a/apps/playground/src/pages/smart-contracts/vesting/deposit-fund.tsx +++ b/apps/playground/src/pages/smart-contracts/vesting/deposit-fund.tsx @@ -72,6 +72,25 @@ function Right() { } let code = ``; + code += `const assets: Asset[] = [\n`; + code += ` {\n`; + code += ` unit: "lovelace",\n`; + code += ` quantity: '${userInput}',\n`; + code += ` },\n`; + code += `];\n`; + code += `\n`; + code += `const lockUntilTimeStamp = new Date();\n`; + code += `lockUntilTimeStamp.setMinutes(lockUntilTimeStamp.getMinutes() + 1);\n`; + code += `\n`; + code += `const beneficiary = '${userInput2}';\n`; + code += `\n`; + code += `const tx = await contract.depositFund(\n`; + code += ` assets,\n`; + code += ` lockUntilTimeStamp.getTime(),\n`; + code += ` beneficiary,\n`; + code += `);\n`; + code += `const signedTx = await wallet.signTx(tx);\n`; + code += `const txHash = await wallet.submitTx(signedTx);\n`; return (