Skip to content

Commit d3c9778

Browse files
committed
feat: inline doc of value
1 parent 3ee20a0 commit d3c9778

File tree

2 files changed

+113
-8
lines changed

2 files changed

+113
-8
lines changed

packages/mesh-common/src/data/value.ts

+60-7
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,36 @@ import {
1616
tokenName,
1717
} from "./json";
1818

19+
/**
20+
* Aiken alias
21+
* Value is the JSON representation of Cardano data Value
22+
*/
1923
export type Value = AssocMap<CurrencySymbol, AssocMap<TokenName, Integer>>;
2024

25+
/**
26+
* Aiken alias
27+
* MValue is the Cardano data Value in Mesh Data type
28+
*/
2129
export type MValue = Map<string, Map<string, bigint>>;
2230

31+
/**
32+
* The utility function to convert assets into Cardano data Value in JSON
33+
* @param assets The assets to convert
34+
* @returns The Cardano data Value in JSON
35+
*/
2336
export const value = (assets: Asset[]) => {
2437
return MeshValue.fromAssets(assets).toJSON();
2538
};
2639

40+
/**
41+
* The utility function to convert assets into Cardano data Value in Mesh Data type
42+
* @param assets The assets to convert
43+
* @returns The Cardano data Value in Mesh Data type
44+
*/
45+
export const mValue = (assets: Asset[]) => {
46+
return MeshValue.fromAssets(assets).toData();
47+
};
48+
2749
/**
2850
* MeshValue provide utility to handle the Cardano value manipulation. It offers certain axioms:
2951
* 1. No duplication of asset - adding assets with same asset name will increase the quantity of the asset in the same record.
@@ -132,7 +154,7 @@ export class MeshValue {
132154

133155
/**
134156
* Get the quantity of asset object per unit
135-
* @param unit The unit to get the quantity of (e.g., "ADA")
157+
* @param unit The unit to get the quantity of
136158
* @returns The quantity of the asset
137159
*/
138160
get = (unit: string): bigint => {
@@ -158,6 +180,7 @@ export class MeshValue {
158180

159181
/**
160182
* Check if the specific unit of value is greater than or equal to that unit of another value
183+
* @param unit - The unit to compare
161184
* @param other - The value to compare against
162185
* @returns boolean
163186
*/
@@ -179,6 +202,7 @@ export class MeshValue {
179202

180203
/**
181204
* Check if the specific unit of value is less than or equal to that unit of another value
205+
* @param unit - The unit to compare
182206
* @param other - The value to compare against
183207
* @returns boolean
184208
*/
@@ -191,7 +215,6 @@ export class MeshValue {
191215

192216
/**
193217
* Check if the value is empty
194-
* @param
195218
* @returns boolean
196219
*/
197220
isEmpty = (): boolean => {
@@ -200,7 +223,7 @@ export class MeshValue {
200223

201224
/**
202225
* Merge the given values
203-
* @param values
226+
* @param values The other values to merge
204227
* @returns this
205228
*/
206229
merge = (values: MeshValue | MeshValue[]): this => {
@@ -218,6 +241,10 @@ export class MeshValue {
218241
return this;
219242
};
220243

244+
/**
245+
* Convert the MeshValue object into an array of Asset
246+
* @returns The array of Asset
247+
*/
221248
toAssets = (): Asset[] => {
222249
const assets: Asset[] = [];
223250
Object.entries(this.value).forEach(([unit, quantity]) => {
@@ -226,15 +253,41 @@ export class MeshValue {
226253
return assets;
227254
};
228255

229-
toData = () => {};
256+
/**
257+
* Convert the MeshValue object into Cardano data Value in Mesh Data type
258+
*/
259+
toData = (): MValue => {
260+
const valueMap: MValue = new Map();
261+
this.toAssets().forEach((asset) => {
262+
const policy = asset.unit.slice(0, 56) || "";
263+
const token = asset.unit.slice(56) || "";
264+
265+
if (!valueMap.has(policy)) {
266+
valueMap.set(policy, new Map());
267+
}
268+
269+
const tokenMap = valueMap.get(policy)!;
270+
const quantity = tokenMap?.get(token);
271+
if (!quantity) {
272+
tokenMap.set(token, BigInt(asset.quantity));
273+
} else {
274+
tokenMap.set(token, quantity + BigInt(asset.quantity));
275+
}
276+
});
277+
278+
return valueMap;
279+
};
230280

231-
toJSON = () => {
232-
const assets = this.toAssets();
281+
/**
282+
* Convert the MeshValue object into a JSON representation of Cardano data Value
283+
* @returns Cardano data Value in JSON
284+
*/
285+
toJSON = (): Value => {
233286
const valueMapToParse: [CurrencySymbol, AssocMap<TokenName, Integer>][] =
234287
[];
235288
const valueMap: { [key: string]: { [key: string]: number } } = {};
236289

237-
assets.forEach((asset) => {
290+
this.toAssets().forEach((asset) => {
238291
const sanitizedName = asset.unit.replace("lovelace", "");
239292
const policy = sanitizedName.slice(0, 56) || "";
240293
const token = sanitizedName.slice(56) || "";

packages/mesh-common/test/data/value/convertor.test.ts

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {
22
Asset,
33
byteString,
4-
ByteString,
54
dict,
65
Dict,
76
Integer,
87
integer,
98
MeshValue,
9+
MValue,
10+
mValue,
1011
Value,
1112
value,
1213
} from "@meshsdk/common";
@@ -69,6 +70,57 @@ describe("value", () => {
6970
});
7071
});
7172

73+
describe("mValue", () => {
74+
it("should create a new Value instance with the correct value", () => {
75+
const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }];
76+
const datum: MValue = mValue(val);
77+
const nameMap = new Map().set("", 1000000);
78+
const valMap = new Map().set("", nameMap);
79+
expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
80+
});
81+
it("Simple token Value", () => {
82+
const val: Asset[] = [
83+
{
84+
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461",
85+
quantity: "345",
86+
},
87+
];
88+
const datum: MValue = mValue(val);
89+
const nameMap = new Map().set("74657374696e676e657777616c2e616461", 345);
90+
const valMap = new Map().set(
91+
"baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700",
92+
nameMap,
93+
);
94+
expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
95+
});
96+
it("Complex Value", () => {
97+
const val: Asset[] = [
98+
{ unit: "lovelace", quantity: "1000000" },
99+
{
100+
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461",
101+
quantity: "345",
102+
},
103+
{
104+
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
105+
quantity: "567",
106+
},
107+
];
108+
const datum: MValue = mValue(val);
109+
110+
const lovelaceMap = new Map().set("", 1000000);
111+
const tokenMap = new Map()
112+
.set("1234", 567)
113+
.set("74657374696e676e657777616c2e616461", 345);
114+
const valMap = new Map()
115+
.set("", lovelaceMap)
116+
.set(
117+
"baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700",
118+
tokenMap,
119+
);
120+
expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
121+
});
122+
});
123+
72124
describe("MeshValue class", () => {
73125
describe("fromAssets", () => {
74126
it("should create a new Value instance with the correct value", () => {

0 commit comments

Comments
 (0)