Skip to content
Discussions/App Development/Attempting to create a contract is sending the wrong templateIdForum ↗

Attempting to create a contract is sending the wrong templateId

App Development8 posts544 views6 likesLast activity Jun 2021
AN
anthonyOP
Jan 2021

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.

CO
cocreature
Jan 2021

@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.

AN
anthony
Jan 2021

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?

CO
cocreature
Jan 2021

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.

AN
anthony
Jan 2021

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

LI
lisandro_iraguen
Jun 2021

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

GitHub

GitHub - digital-asset/blog-cbdc-examples

Contribute to digital-asset/blog-cbdc-examples development by creating an account on GitHub.

best regards

CO
cocreature
Jun 2021

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

LI
lisandro_iraguen
Jun 2021

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

← Back to Discussions