Skip to content

Commit

Permalink
feat: inline doc of value
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Aug 3, 2024
1 parent 3ee20a0 commit d3c9778
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 8 deletions.
67 changes: 60 additions & 7 deletions packages/mesh-common/src/data/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,36 @@ import {
tokenName,
} from "./json";

/**
* Aiken alias
* Value is the JSON representation of Cardano data Value
*/
export type Value = AssocMap<CurrencySymbol, AssocMap<TokenName, Integer>>;

/**
* Aiken alias
* MValue is the Cardano data Value in Mesh Data type
*/
export type MValue = Map<string, Map<string, bigint>>;

/**
* The utility function to convert assets into Cardano data Value in JSON
* @param assets The assets to convert
* @returns The Cardano data Value in JSON
*/
export const value = (assets: Asset[]) => {
return MeshValue.fromAssets(assets).toJSON();
};

/**
* The utility function to convert assets into Cardano data Value in Mesh Data type
* @param assets The assets to convert
* @returns The Cardano data Value in Mesh Data type
*/
export const mValue = (assets: Asset[]) => {
return MeshValue.fromAssets(assets).toData();
};

/**
* MeshValue provide utility to handle the Cardano value manipulation. It offers certain axioms:
* 1. No duplication of asset - adding assets with same asset name will increase the quantity of the asset in the same record.
Expand Down Expand Up @@ -132,7 +154,7 @@ export class MeshValue {

/**
* Get the quantity of asset object per unit
* @param unit The unit to get the quantity of (e.g., "ADA")
* @param unit The unit to get the quantity of
* @returns The quantity of the asset
*/
get = (unit: string): bigint => {
Expand All @@ -158,6 +180,7 @@ export class MeshValue {

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

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

/**
* Check if the value is empty
* @param
* @returns boolean
*/
isEmpty = (): boolean => {
Expand All @@ -200,7 +223,7 @@ export class MeshValue {

/**
* Merge the given values
* @param values
* @param values The other values to merge
* @returns this
*/
merge = (values: MeshValue | MeshValue[]): this => {
Expand All @@ -218,6 +241,10 @@ export class MeshValue {
return this;
};

/**
* Convert the MeshValue object into an array of Asset
* @returns The array of Asset
*/
toAssets = (): Asset[] => {
const assets: Asset[] = [];
Object.entries(this.value).forEach(([unit, quantity]) => {
Expand All @@ -226,15 +253,41 @@ export class MeshValue {
return assets;
};

toData = () => {};
/**
* Convert the MeshValue object into Cardano data Value in Mesh Data type
*/
toData = (): MValue => {
const valueMap: MValue = new Map();
this.toAssets().forEach((asset) => {
const policy = asset.unit.slice(0, 56) || "";
const token = asset.unit.slice(56) || "";

if (!valueMap.has(policy)) {
valueMap.set(policy, new Map());
}

const tokenMap = valueMap.get(policy)!;
const quantity = tokenMap?.get(token);
if (!quantity) {
tokenMap.set(token, BigInt(asset.quantity));
} else {
tokenMap.set(token, quantity + BigInt(asset.quantity));
}
});

return valueMap;
};

toJSON = () => {
const assets = this.toAssets();
/**
* Convert the MeshValue object into a JSON representation of Cardano data Value
* @returns Cardano data Value in JSON
*/
toJSON = (): Value => {
const valueMapToParse: [CurrencySymbol, AssocMap<TokenName, Integer>][] =
[];
const valueMap: { [key: string]: { [key: string]: number } } = {};

assets.forEach((asset) => {
this.toAssets().forEach((asset) => {
const sanitizedName = asset.unit.replace("lovelace", "");
const policy = sanitizedName.slice(0, 56) || "";
const token = sanitizedName.slice(56) || "";
Expand Down
54 changes: 53 additions & 1 deletion packages/mesh-common/test/data/value/convertor.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
Asset,
byteString,
ByteString,
dict,
Dict,
Integer,
integer,
MeshValue,
MValue,
mValue,
Value,
value,
} from "@meshsdk/common";
Expand Down Expand Up @@ -69,6 +70,57 @@ describe("value", () => {
});
});

describe("mValue", () => {
it("should create a new Value instance with the correct 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);
expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
});
it("Simple token Value", () => {
const val: Asset[] = [
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461",
quantity: "345",
},
];
const datum: MValue = mValue(val);
const nameMap = new Map().set("74657374696e676e657777616c2e616461", 345);
const valMap = new Map().set(
"baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700",
nameMap,
);
expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
});
it("Complex Value", () => {
const val: Asset[] = [
{ unit: "lovelace", quantity: "1000000" },
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461",
quantity: "345",
},
{
unit: "baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234",
quantity: "567",
},
];
const datum: MValue = mValue(val);

const lovelaceMap = new Map().set("", 1000000);
const tokenMap = new Map()
.set("1234", 567)
.set("74657374696e676e657777616c2e616461", 345);
const valMap = new Map()
.set("", lovelaceMap)
.set(
"baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700",
tokenMap,
);
expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
});
});

describe("MeshValue class", () => {
describe("fromAssets", () => {
it("should create a new Value instance with the correct value", () => {
Expand Down

0 comments on commit d3c9778

Please sign in to comment.