Attempting to create a contract is sending the wrong templateId

I’m attempting to use the @daml/ledger library in vanilla JS (really Vue.js but doesn’t matter).

However when I submit the following request to the ledger I get an error.

import Ledger from '@daml/ledger';
var ledger = new Ledger({token, ledgerUrl, reconnectThreshold: 1337});
var payload = {
  beer: {
  templateId: "Beer:Beer",
  giver: state.party,
  recipient: recipient
  }
};

try {
  await ledger.create("Beer:BeerProposal", payload);
}
catch (err) {
  console.log("Couldn't create contract " + err)
}

Error:

{kind: "DecoderError", input: Array(2), at: "input[0].templateId", message: "expected "Beer:BeerProposal", got "5185bd6f731b53b…703cb9b62835b567f9631e08121fef:Beer:BeerProposal""}

I’m running local sandbox on Daml 1.8 and this appears to be a contractId included in the templateId. I’m not quite sure where it’s coming from though.

@daml/ledger is really intended to be used with the JavaScript code generator. The first arguments to ledger.create is then the generated template. The code generator also works perfectly fine without React.

I used the codegen but am a bit unclear about how to use it in my project. The docs say to look at how @daml/react is implemented and I have but I’m not familiar with TypeScript so…what am I missing here?

The codegen generates something like @daml.js/proj. You can the import it as

import * as proj from '@daml.js/proj'

The generated code will then include something like proj.ModuleName.TemplateName. This is what you pass in to ledger.create. So something like:

ledger.create(proj.ModuleName.TemplateName, …)

You can see this put to use in DAVL’s admin CLI which is run via NodeJS without any UI framework.

1 Like

Awesome, this + yarn add ./daml.js/proj-version/ worked. Thanks!

1 Like

Good day, I am tring to do the same but I got

Couldn’t create contract TypeError: template.encode is not a function

when I run this line of code
image

I am using the CBDCv1 sample from here

best regards

1 Like

Hi @paleiov, you should pass in the template which is cdbc.CBDCv1.CBDC not cdbc.CBDCv1.CBDCTemplate.

2 Likes

thanks again, it was an error in the smart contract itself >D

1 Like