Skip to content
CCPEDIAby Unity Nodes
App Development

Error happened when using the sample code in Github: Testing in Daml Lab1

4 posts178 viewsLast activity Oct 2023

  1. skylerOP

    I copied the code of Lab1, and Main under a project but got an error. Same thing happened when using the code in “Navigator” course.
    How to deal with this?
    Code in Lab1.daml

    module Lab1 where 
    
    import DA.Optional
    import DA.Date (toDateUTC)
    
    template CLPApplication 
        with 
            customer: Party 
            airline: Party 
    
            id: Text  --this is external id that is unique for a customer, e.g. driving-license no., SSN, etc.
            name: Text
            address: Text
            email: Text
            phone: Optional Text
            timestamp: Time 
            dob: Date 
           
        where 
            signatory customer
            observer airline 
             
            choice SubmitApplication: ContractId CLPApplication 
                with 
                    appCustomer: Party 
                    appAirline: Party
    
                    customerId: Text
                    customerName: Text
                    customerAddress: Text
                    customerEmail: Text
                    customerPhone: Text
                    appTimeStamp: Time 
                    customerDob: Date 
                controller customer 
                    do 
                        create CLPApplication  
                            with 
                                customer = appCustomer
                                airline = appAirline
    
                                id = customerId
                                name = customerName
                                address = customerAddress
                                email = customerEmail
                                phone = Some customerPhone
                                timestamp = appTimeStamp
                                dob = customerDob 
                            
            choice ReviewApplication: Optional (ContractId CLPAccount)
                controller airline 
                    do 
                        account <- lookupByKey @CLPAccount (airline, id)
                        now <- getTime
                        if (isSome account) then 
                            return None
                        else do
                            account <- create CLPAccount with                                
                                    timestamp = now                                 
                                    points = 0   
                                    ..
                            return (Some account)
    
    template CLPAccount 
        with
            customer: Party 
            airline: Party 
    
            id: Text 
            name: Text
            address: Text
            email: Text
            phone: Optional Text
            timestamp: Time 
            dob: Date 
            points: Int
    
        where 
            signatory customer, airline 
    
            key (airline, id): (Party, Text)
            maintainer key._1
    
    
            choice AddPoints : ContractId CLPAccount 
                with 
                    newPoints: Int 
                controller airline 
                do 
                    create CLPAccount with 
                        points = points + newPoints
                        ..
    
    template AirlineService 
        with 
            customer: Party
            airline: Party 
        where 
            signatory airline 
            observer customer 
    
            nonconsuming choice CreateBlankApplication: ContractId CLPApplication
                controller customer 
                    do 
                        now <- getTime
                        create CLPApplication 
                            with 
                            customer 
                            airline 
    
                            id = ""
                            name = ""
                            address = ""
                            email = ""
                            phone = Some ""
                            timestamp = now
                            dob = toDateUTC now
    

    Code in Main.daml

    module Main where
    
    import Daml.Script
    import Lab1
    
    
    -- Lab1 Problem 1
    setup: Script ()
    setup = script do 
    
      -- allocate parties with the given display name 
      airline <- allocatePartyWithHint "Epic Airlines" (PartyIdHint "EA")
      customer <- allocatePartyWithHint "Customer" (PartyIdHint "Cust")
    
      -- construct user-ids from text 
      airlineId <- validateUserId "Epic"
      aliceId <- validateUserId "Alice"
    
      -- create users with the given rights 
      createUser (User airlineId (Some airline)) [CanActAs airline]
      createUser (User aliceId (Some customer)) [CanActAs customer]
    
      
      submit airline do 
        createCmd AirlineService with 
            customer = customer
            airline = airline
      return ()
    

    Error I got:
    Script execution failed:
    Unhandled exception: Daml.Script:InvalidUserId@1784e0500b69eb19eb1995f7ea11cb9ba0133f339dc0cf4da16a9e72e6c9e30f with
    m =
    “User ID “Epic” does not match regex “[a-z0-9@^$.!`-#+'~_|:]{1,128}””

    Ledger time: 1970-01-01T00:00:00Z

  2. a_putkov

    @skyler, thank you very much for reporting the issue. We’ll fix it on Github asap.
    The root cause is that validateUserId function requires the input text to be all lower case. To fix the problem one can either change the text value passed to the function to all lower case (replace “Epic” with “epic” and “Alice” with “alice”). Alternatively one can apply asciiToLower function to the input text before passing the result to validateUserId function.

    airlineId <- validateUserId $ asciiToLower "Epic"
    aliceId <- validateUserId $ asciiToLower "Alice"
    

    In the latter case one would also need to add an import of DA.Text module, which contains asciiToLower function.

  3. a_putkov

    Upon closer look, the requirement for all lower case input for validateUserId function was enforced in earlier versions of Daml SDK. Starting from SDK v2.5.0 it’s possible to use upper case letters in the text value passed as argument to validateUserId function. So, instead of modifying the code in the Main module, as I suggested earlier, you can bump up the SDK version number in daml.yaml file in your project. You’ll need to save the daml.yaml file and restart VS Code for the changes to take effect. Or, instead of copying and pasting modules from the repo into your own project, you can clone the repo from Github. The repo includes daml.yaml file that specifies the version of Daml SDK, that the project was created with, as v2.6.1.

  4. skyler

    Thanks. I tried the solution and it worked!