@@ -16,14 +16,36 @@ import {
16
16
tokenName ,
17
17
} from "./json" ;
18
18
19
+ /**
20
+ * Aiken alias
21
+ * Value is the JSON representation of Cardano data Value
22
+ */
19
23
export type Value = AssocMap < CurrencySymbol , AssocMap < TokenName , Integer > > ;
20
24
25
+ /**
26
+ * Aiken alias
27
+ * MValue is the Cardano data Value in Mesh Data type
28
+ */
21
29
export type MValue = Map < string , Map < string , bigint > > ;
22
30
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
+ */
23
36
export const value = ( assets : Asset [ ] ) => {
24
37
return MeshValue . fromAssets ( assets ) . toJSON ( ) ;
25
38
} ;
26
39
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
+
27
49
/**
28
50
* MeshValue provide utility to handle the Cardano value manipulation. It offers certain axioms:
29
51
* 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 {
132
154
133
155
/**
134
156
* 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
136
158
* @returns The quantity of the asset
137
159
*/
138
160
get = ( unit : string ) : bigint => {
@@ -158,6 +180,7 @@ export class MeshValue {
158
180
159
181
/**
160
182
* 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
161
184
* @param other - The value to compare against
162
185
* @returns boolean
163
186
*/
@@ -179,6 +202,7 @@ export class MeshValue {
179
202
180
203
/**
181
204
* 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
182
206
* @param other - The value to compare against
183
207
* @returns boolean
184
208
*/
@@ -191,7 +215,6 @@ export class MeshValue {
191
215
192
216
/**
193
217
* Check if the value is empty
194
- * @param
195
218
* @returns boolean
196
219
*/
197
220
isEmpty = ( ) : boolean => {
@@ -200,7 +223,7 @@ export class MeshValue {
200
223
201
224
/**
202
225
* Merge the given values
203
- * @param values
226
+ * @param values The other values to merge
204
227
* @returns this
205
228
*/
206
229
merge = ( values : MeshValue | MeshValue [ ] ) : this => {
@@ -218,6 +241,10 @@ export class MeshValue {
218
241
return this ;
219
242
} ;
220
243
244
+ /**
245
+ * Convert the MeshValue object into an array of Asset
246
+ * @returns The array of Asset
247
+ */
221
248
toAssets = ( ) : Asset [ ] => {
222
249
const assets : Asset [ ] = [ ] ;
223
250
Object . entries ( this . value ) . forEach ( ( [ unit , quantity ] ) => {
@@ -226,15 +253,41 @@ export class MeshValue {
226
253
return assets ;
227
254
} ;
228
255
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
+ } ;
230
280
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 => {
233
286
const valueMapToParse : [ CurrencySymbol , AssocMap < TokenName , Integer > ] [ ] =
234
287
[ ] ;
235
288
const valueMap : { [ key : string ] : { [ key : string ] : number } } = { } ;
236
289
237
- assets . forEach ( ( asset ) => {
290
+ this . toAssets ( ) . forEach ( ( asset ) => {
238
291
const sanitizedName = asset . unit . replace ( "lovelace" , "" ) ;
239
292
const policy = sanitizedName . slice ( 0 , 56 ) || "" ;
240
293
const token = sanitizedName . slice ( 56 ) || "" ;
0 commit comments