diff --git a/packages/mesh-common/src/types/protocol.ts b/packages/mesh-common/src/types/protocol.ts index 0a4d380b1..109299b49 100644 --- a/packages/mesh-common/src/types/protocol.ts +++ b/packages/mesh-common/src/types/protocol.ts @@ -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; @@ -22,12 +28,14 @@ export type Protocol = { maxCollateralInputs: number; coinsPerUtxoSize: number; minFeeRefScriptCostPerByte: number; + costModels?: CostModels; }; export const castProtocol = ( data: Partial>, ): Protocol => { - const result: Partial> = {}; + const result: Partial> = + {}; for (const rawKey in DEFAULT_PROTOCOL_PARAMETERS) { const key = rawKey as keyof Protocol; @@ -40,5 +48,9 @@ export const castProtocol = ( } } + if (data.costModels && typeof data.costModels === "object") { + result.costModels = data.costModels as CostModels; + } + return result as Protocol; }; diff --git a/packages/mesh-common/test/types/protocol.test.ts b/packages/mesh-common/test/types/protocol.test.ts index b04b34865..ca7e3d1f8 100644 --- a/packages/mesh-common/test/types/protocol.test.ts +++ b/packages/mesh-common/test/types/protocol.test.ts @@ -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(); + }); }); }); diff --git a/packages/mesh-core-cst/src/serializer/index.ts b/packages/mesh-core-cst/src/serializer/index.ts index 613640acc..b7b8b9dce 100644 --- a/packages/mesh-core-cst/src/serializer/index.ts +++ b/packages/mesh-core-cst/src/serializer/index.ts @@ -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]) {