Encountering an error (expected instance of CostModel) while integrating a Compact smart contract into a Next.js backend

Hello mates
Now I am integrating midnight smart contract ( developed by compact ) into Nest.js backend.
There is currently an error in the integration between Smart Contract and the backend.
This is the err msg : " Found error ‘Error: expected instance of CostModel’ "
What is the reason of this error and How can I fix this error.
Plz help me.
Thank you.

1 Like

Hey @Daiki_mid as @lolocoding mentioned, let’s move on to technical questions! Just like we discussed in the DM, could you please add more context? Could you include the contract function and the call to the function in the Nest backend here? A snippet would be helpful! Thanks!

Some quick points you could check:

  • i) Make sure the Midnight versions are up to date (check carefully; you can use the repository I shared as a reference)

  • ii) What is the call you’re making to CostModel? And how are you initializing it?

  • iii) CostModel is required but you’re not sending it.

I look forward to your response so I can help you better!

1 Like

@Daiki_mid do you do something like that in your function?

evaluateWithCostModel(fnName: string, args: any[]) { 
const costModel = new CostModel(defaultCostModel()); 
return evaluate(this.contract, { 
function: fnName, 
args, 
costModel, 
});
}
1 Like

@Daiki_mid let us examine it in detail:

A CostModel in Midnight is a class used for calculating transaction fees and resource usage in smart contract execution.

How to find out what functions require a CostModel :

  • Check the API documentation for the function signatures. For example, the runProgram function in the compact-runtime , onchain-runtime , and ledger packages all require a CostModel parameter runProgram (compact-runtime).,
  • The query and runTranscript methods in the QueryContext class also require a CostModel. [Link could not be added, maximum allowed (2)]

What is a CostModel used for?

  • It is used to calculate transaction fees and resource usage for contract execution.,
  • It helps determine the cost of running operations, which is important for fee calculation and preventing abuse of network resources.

You can use a default or testing instance CostModel.dummyCostModel() CostModel.dummyCostModel().

The error is due to a missing or incorrect CostModel instance in your backend integration, not a problem with your contract or CLI. Make sure to provide a valid CostModel when required by the API in your backend code.

  • Since you told me via DM that your code is private, try this and let me know what response you get:

Example function:

import { CostModel } from '@midnight-ntwrk/compact-runtime'; // or the correct package

const costModel = CostModel.dummyCostModel();
someMidnightFunction(..., costModel, ...);
2 Likes

FYI, I wouldn’t rely too much on the current cost model, as it hasn’t been set 100% and may like change until mainnet.
What are you trying to achieve? Why do you want to use CostModel? It would be very helpful if you could share a link to a repo with the code so we can understand what is not working.

1 Like

I am going to integratet the Compact contract into Next backend
But there are some err such as Found error ‘Error: expected instance of CostModel’
I tried several ways but I could’t fix this err
Plz help me

1 Like

This is the console msg :slight_smile:

[11:48:05.344] INFO (3126654): File path for save file not found, building wallet from scratch
[11:48:05.483] INFO (3126654): Your wallet seed is:  *********
[11:48:05.484] INFO (3126654): Your wallet address is: mn_shield-addr_test1duv8t5hytcat06splesmnd7s45pg09nfpgpgcph6w9kahkjelsksxqrgs3u665d7red3jne3pd3emhtvwvz8vfg60h3jekkas9jnneuhmy7c3jd7
[11:48:05.485] INFO (3126654): Your wallet balance is: 0
[11:48:05.485] INFO (3126654): Waiting to receive tokens...
[11:48:05.505] INFO (3126654): Waiting for funds. Backend lag: 0, wallet lag: 0, transactions=0
[11:48:17.592] INFO (3126654): Waiting for funds. Backend lag: 0, wallet lag: 0, transactions=3
[11:48:17.594] INFO (3126654): Your wallet balance is: 997534376
[11:48:17.611] INFO (3126654): Joining contract...
[11:48:17.818] INFO (3126654): Joined contract at address: 0200ba0235d3f1808ba327a123cf8c6379ae2a996c8cc6e78962200069a3e2223c9a
[11:48:17.819] INFO (3126654): Generating NFT ID...
[11:48:17.861] ERROR (3126654): Found error 'Error: expected instance of CostModel'
[11:48:17.862] INFO (3126654): Exiting...
[11:48:17.893] INFO (3126654): User NFT ID: undefined
1 Like