Skip to content
CCPEDIAby Unity Nodes
App Development

Return multiple dynamic contracts

2 posts186 viewsLast activity Jun 2023

  1. Teja_SatyaOP

    Hi Team,

    Here is my choice

    
       choice AcceptRequest : (ContractId VendorRequest,ContractId CC_INFO or ContractId LOAN_INFO or ContractId BASIC_INFO or None )
         with 
           name: Optional Text
           pan : Optional Text
           date_of_birth : Optional Date
           address : Optional Text
           income : Optional Decimal
           credit_score : Optional Int
           employment_history : Optional Text
           bankruptcy_history : Optional Text
           debt_to_income_ratio : Optional Decimal
           phone_number : Optional Text
           employment_information : Optional Text
           credit_history : Optional Text
           assets : Optional Text
           liabilities : Optional Text
           collateral : Optional Text
           loan_type : Optional Text
           loan_amount : Optional Decimal
           interest_rate : Optional Decimal
           term : Optional Int
           status : Optional Text
           date_created : Optional Date
           date_updated : Optional Date  
         controller recipient
          do
           assert(recipient /= sender)
           assert(response == "" )
           a<-create this with
             sender = sender
             recipient = recipient
             response = "Request accepted" 
    
           if requestType == "CC_INFO"
             then do b<-create CC_INFO with name = name,pan = pan,dateOfBirth = date_of_birth,address = address, income = income, creditScore = credit_score,employmentHistory = employment_history,bankruptcyHistory = bankruptcy_history,debtToIncomeRatio = debt_to_income_ratio, owner1=recipient,owner2=sender 
                     pure(a,b)
             else  
               if (requestType == "LOAN_INFO") 
                 then
                   do b<- create LOAN_INFO with   name = name, pan = pan,date_of_birth = date_of_birth,address = address,phone_number = phone_number,employment_information = employment_information,credit_history = credit_history,assets = assets, liabilities = liabilities, collateral = collateral, loan_type = loan_type, loan_amount = loan_amount, interest_rate = interest_rate, term = term, status = status,date_created = date_created,date_updated = date_updated
                      pure(a,b)
                 else  
                   if requestType == "BASIC_INFO" 
                     then do b<- create BASIC_INFO with name = name, pan = pan , dob = date_of_birth,address = address, owner1=recipient,owner2=sender
                             pure(a,b)
                      else 
                        do pure(None)
    

    I want to return

    VendorRequest and (CC_INFO or LOAN_INFO or BASIC_INFO ) based on the request type

    None - if the request type does not match either CC_INFO or LOAN_INFO or BASIC_INFO
    please help me to resolve on returning the Contracts

    Thanks in advance :slight_smile:

  2. Gary_Verhaegen

    You may be better off with three separate choices here.

    If you really want to go down this path, here are a few comments.

    First off, you can simplify your if chain at the end by using a case instead:

    case requestType of
      "CC_INFO" -> do ...
      "LOAN_INFO" -> do ...
      "BASIC_INFO" -> do ...
      _ -> pure None
    

    That would read better than chained if-then-else in my opinion.

    Second, if you’re going to use these strings as dispatch mechanisms, you may want to define a data type for them, to prevent typos. Something like:

    data InformationRetrieval = CC_INFO | LOAN_INFO | BASIC_INFO | NO_INFO
    

    Depending on your use-case, it’s entirely possible that you won’t need the NO_INFO case, e.g. if you put it there just to make the compiler happy but you never expect a valid request to supply another value than these three.

    Lastly, you seem to be confused about your return types here. The four branches of your conditional seem to return four different types. That is not possible in Daml. What you can do for these situations is to create a wrapper type that has all four types as variants, something like:

    data AcceptResult
     = wrap_CC_INFO (ContractId CC_INFO)
     | wrap_LOAN_INFO (ContractId LOAN_INFO)
     | wrap_BASIC_INFO (ContractId BASIC_INFO)
     | wrap_NOTHING
    

    then your choice can always return a result of type AcceptResult.