Skip to content
CCPEDIAby Unity Nodes
App Development

Using @daml/ledger to query ledger - declaring template`

2 posts614 views3 likesLast activity Mar 2021

  1. alexkosiorOP 1

    Hi,

    I am trying to use @daml/ledger to perform an api call to v1/query and I am having troubles passing the template to the function. (Ledger connection works fine as ledger.listPackages() returns a correct result).

    import Ledger from  '@daml/ledger';
    import { Choice, ContractId, List, lookupTemplate, Party, Template, Text} from '@daml/types';
    var authToken = {'token':token,'httpBaseUrl':httpurl,'wsBaseUrl':wsurl,'reconnectThreshold':12000};
    var reportLedger = new Ledger(authToken);
    
    var template_details = ???
    
    var query_result = reportLedger.query(template_details)
    

    I got the implementation of the template from the @daml/types and it appears that it requires the following variables:

    • templateId,
    • sdkVersion
    • keyDecoder
    • keyEncode
    • Archive
    • decoder
    • encode

    The first two are easy enough, but the latter are not.

    But tbf I have no idea of what I am missing.

    I tried to implement the guidance from DAML documentation of HTTP JSON API Service to create basic CURL requests like below:

    curl -H "Authorization: Bearer my_token" -H "Content-Type: application/json" -d '{"templateIds": ["Iou:Iou"],"query": {"amount": 999.99}}' -X POST http://localhost:7500/v1/query

    However, that also returned unexpected results (I expected an empty response):

    {"errors":["JwtDecoder.Error: 'decode, The token was expected to have 3 parts, but got 1."],"status":401}

    I am not using any authentication and I am running daml json-api with --allow-insecure-tokens option.

    Is there any detailed documentation that explains this?

  2. cocreature 2

    Hi Alex, @daml/ledger is really intended to be used in combination with the JavaScript codegen which will generate the template for you. I recommend the related post at Attempting to create a contract is sending the wrong templateId for some more details on that.