Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/mesh-common/src/types/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { DEFAULT_PROTOCOL_PARAMETERS } from "../constants";

export type CostModels = {
PlutusV1?: number[];
PlutusV2?: number[];
PlutusV3?: number[];
};

export type Protocol = {
epoch: number;
minFeeA: number;
Expand All @@ -22,12 +28,14 @@ export type Protocol = {
maxCollateralInputs: number;
coinsPerUtxoSize: number;
minFeeRefScriptCostPerByte: number;
costModels?: CostModels;
};

export const castProtocol = (
data: Partial<Record<keyof Protocol, any>>,
): Protocol => {
const result: Partial<Record<keyof Protocol, number | string>> = {};
const result: Partial<Record<keyof Protocol, number | string | CostModels>> =
{};

for (const rawKey in DEFAULT_PROTOCOL_PARAMETERS) {
const key = rawKey as keyof Protocol;
Expand All @@ -40,5 +48,9 @@ export const castProtocol = (
}
}

if (data.costModels && typeof data.costModels === "object") {
result.costModels = data.costModels as CostModels;
}

return result as Protocol;
};
14 changes: 14 additions & 0 deletions packages/mesh-common/test/types/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,19 @@ describe("Protocol", () => {
expect(casted.epoch).toBe(correct.epoch);
expect(casted.decentralisation).toBe(correct.decentralisation);
});

it("should preserve costModels when provided", () => {
const v3 = [100788, 420, 1, 1, 1000];
const casted = castProtocol({ costModels: { PlutusV3: v3 } });

expect(casted.costModels).toBeDefined();
expect(casted.costModels?.PlutusV3).toEqual(v3);
});

it("should leave costModels undefined when not provided", () => {
const casted = castProtocol({ minFeeA: 44 });

expect(casted.costModels).toBeUndefined();
});
});
});
18 changes: 9 additions & 9 deletions packages/mesh-core-cst/src/serializer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,15 +1528,15 @@ class CardanoSDKSerializerCore {

// After building tx witness set, we must hash it with the cost models
// and put the hash in the tx body
let costModelV1 = Serialization.CostModel.newPlutusV1(
DEFAULT_V1_COST_MODEL_LIST,
);
let costModelV2 = Serialization.CostModel.newPlutusV2(
DEFAULT_V2_COST_MODEL_LIST,
);
let costModelV3 = Serialization.CostModel.newPlutusV3(
DEFAULT_V3_COST_MODEL_LIST,
);
let v1CostList =
this.protocolParams.costModels?.PlutusV1 ?? DEFAULT_V1_COST_MODEL_LIST;
let v2CostList =
this.protocolParams.costModels?.PlutusV2 ?? DEFAULT_V2_COST_MODEL_LIST;
let v3CostList =
this.protocolParams.costModels?.PlutusV3 ?? DEFAULT_V3_COST_MODEL_LIST;
let costModelV1 = Serialization.CostModel.newPlutusV1(v1CostList);
let costModelV2 = Serialization.CostModel.newPlutusV2(v2CostList);
let costModelV3 = Serialization.CostModel.newPlutusV3(v3CostList);
let costModels = new Serialization.Costmdls();

if (this.usedLanguages[PlutusLanguageVersion.V1]) {
Expand Down