Skip to content
CCPEDIAby Unity Nodes
App Development

Canton Console how to represent ENUM

5 posts354 views2 likesLast activity May 2022

  1. Dorrit_DuOP 1

    Hi Team,

    Similar to the questions that I raised last week from here .

    Now, we are adding the complexity to the template as shown below (with partyType with Enum type)

    data PartyType = Delegates | Entity deriving (Eq, Show)
    
    template PartyInfo3
      with
        operator : Party -- party onboarding --
        referenceId : Map.Map Text Text
        partyType : PartyType
      where
        signatory operator
    

    Looking at the sample contract from return, I understand that Enum is required but still puzzle to construct enumId & constructor part.

     label = "partyType",
                  value = Some(
                    value = Value(
                      sum = Enum(
                        value = Enum(
                          enumId = Some(value = Identifier(packageId = "fb64fe96f2f08c34e344864507e65b0aeb779e9c808f21e21dbb13059a9ebc02", moduleName = "PartyInfo", entityName = "PartyType")),
                          constructor = "Delegates"
    

    Can you please help giving the example? thanks in advanced

    Cheers,
    Dorrit

  2. Phoebe_Nichols

    Hi Dorrit,

    You can instantiate the enum PartyType like this:

    22:05:24 ~/repos/canton-1/enterprise/app/target/release/canton-enterprise-2.3.0-SNA
    PSHOT [enum]
    🌸 ./bin/canton -c examples/01-simple-topology/simple-topology.conf
       _____            _
      / ____|          | |
     | |     __ _ _ __ | |_ ___  _ __
     | |    / _` | '_ \| __/ _ \| '_ \
     | |___| (_| | | | | || (_) | | | |
      \_____\__,_|_| |_|\__\___/|_| |_|
    
      Welcome to Canton!
      Type `help` to get started. `exit` to leave.
    
    @ import com.digitalasset.canton.examples.PhoTest.PartyInfo3
    import com.digitalasset.canton.examples.PhoTest.PartyInfo3
    
    @ import com.digitalasset.canton.examples.PhoTest.PartyType.Delegates
    import com.digitalasset.canton.examples.PhoTest.PartyType.Delegates
    
    @ import com.digitalasset.canton.examples.PhoTest.PartyType.Entity
    import com.digitalasset.canton.examples.PhoTest.PartyType.Entity
    
    @ import com.daml.ledger.client.binding.Primitive.GenMap
    import com.daml.ledger.client.binding.Primitive.GenMap
    
    @ import com.daml.ledger.client.binding.Primitive.Text
    import com.daml.ledger.client.binding.Primitive.Text
    
    @ 
    val partyInfo3 = PartyInfo3(operator = operator.toPrim, referenceId = Map.empty[Tex
    t,Text], partyType = Delegates)
    partyInfo3: PartyInfo3 = PartyInfo3(
      operator = "operator::1220a01c682ce1d460746ec7175cac1d56effcea97060a8383d6abde2b9
    c186baefd",
      referenceId = Map(),
      partyType = Delegates
    )
    
    
    @
    val partyInfo3Again = PartyInfo3(operator = operator.toPrim, referenceId = Map.empty[Tex
    t,Text], partyType = Entity)
    partyInfo3: PartyInfo3 = PartyInfo3(
      operator = "operator::1220a01c682ce1d460746ec7175cac1d56effcea97060a8383d6abde2b9
    c186baefd",
      referenceId = Map(),
      partyType = Entity
    )
    
    

    After this point you can do val create = partyInfo3.create.command etc to create the contract.

    Does that answer your question?

    Phoebe :smile:

  3. Dorrit_Du

    Thanks Phoebe, is there a way that we don’t use binding library? Because client doesn’t want to use codegen?

  4. Phoebe_Nichols 1

    Hi Dorrit,

    You can do this without the binding library, yep. The important bit is

    val createCmd = ledger_api_utils.create(pkgIdStr,"PhoTest","PartyInfo3",Map("operat
    or" -> operator,"referenceId" -> Value.Sum.GenMap(GenMap.of(Seq.empty)),"partyType"
     -> Value.Sum.Enum(Enum(constructor = "Delegates"))))
    

    Here is all the operations that make this work

    🌸 ./bin/canton -c examples/01-simple-topology/simple-topology.conf
       _____            _
      / ____|          | |
     | |     __ _ _ __ | |_ ___  _ __
     | |    / _` | '_ \| __/ _ \| '_ \
     | |___| (_| | | | | || (_) | | | |
      \_____\__,_|_| |_|\__\___/|_| |_|
    
      Welcome to Canton!
      Type `help` to get started. `exit` to leave.
    
    
    @
    val pkgIdStr = "cee0a65ec62d682a2025935a28810dac8ad722ec3ff5593ed646eaf88ad8cc9f"
    pkgIdStr: String = "cee0a65ec62d682a2025935a28810dac8ad722ec3ff5593ed646eaf88ad8cc9
    f"
    
    @ val operator = participant1.parties.enable("operator")
    operator: PartyId = operator::12202818444d...
    
    @ participant1.dars.upload("dars/CantonExamples.dar")
    res7: String = "12201cc2b4b2d2cce61fb8ce2274c34f9aa0611457c510b0d09071c8ea4a8a09fd8
    f"
    
    @ import com.daml.ledger.api.v1.value.GenMap
    import com.daml.ledger.api.v1.value.GenMap
    
    @ import com.daml.ledger.api.v1.value.Value
    import com.daml.ledger.api.v1.value.Value
    
    @ import com.daml.ledger.api.v1.value.Enum
    import com.daml.ledger.api.v1.value.Enum
    
    @ participant1.domains.connect_local(mydomain)
    
    @
    val createCmd = ledger_api_utils.create(pkgIdStr,"PhoTest","PartyInfo3",Map("operat
    or" -> operator,"referenceId" -> Value.Sum.GenMap(GenMap.of(Seq.empty)),"partyType"
     -> Value.Sum.Enum(Enum(constructor = "Delegates"))))
    createCmd: com.daml.ledger.api.v1.commands.Command = Command(
      command = Create(
        value = CreateCommand(
          templateId = Some(
            value = Identifier(
              packageId = "cee0a65ec62d682a2025935a28810dac8ad722ec3ff5593ed646eaf88ad8cc9f"
    ... <<some more stuff>>>...
                RecordField(
                  label = "partyType",
                  value = Some(
                    value = Value(
                      sum = Enum(
                        value = Enum(enumId = None, constructor = "Delegates")
                      )
    ...
    
    
    @ participant1.ledger_api.commands.submit(Seq(operator), Seq(createCmd))
    res31: com.daml.ledger.api.v1.transaction.TransactionTree = TransactionTree(
      transactionId = "122067e87bd5bfaf00ba8c7be021836738fcb3cd8d0173718bb3d1c638b80821
    0a7d",
      commandId = "32d1b35f-4038-4129-bd71-692205617274",
      workflowId = "",
      effectiveAt = Some(
        value = Timestamp(
        ...
    
    

    Does that work for you?

    Phoebe

  5. Dorrit_Du

    @Phoebe_Nichols Work perfectly! thanks for your help