Choice in DAML

Hi Team,

I am trying to learn DAML and learning from the Airline Model example. Can you explain me about the following choice. What exactly Accept_Invite choice does here? I just want to know the process involved in this choice.

choice Accept_Invite : (ContractId Ticket, ContractId Flight)
with
flightCid : ContractId Flight
controller ticket.passenger
do
flight ← fetch flightCid
ticket.flightNumber === flight.flightNumber
ticket.airline === flight.airline

    flightCid <- exercise flightCid AddPassenger with
      passenger = ticket.passenger
    ticketCid <- create ticket

    return (ticketCid, flightCid)

Thank you
Erum

Accept_Invite returns two Cids, ContractId Ticket, and ContractId Flight
choice Accept_Invite : (ContractId Ticket, ContractId Flight)

The choice must be exercised with one argument, which is the ContractId for a Flight contract,
with flightCid : ContractId Flight

and only the passenger denoted in the Ticket can exercise this choice.

Within the do block, we are fetching the Flight contract
a new flightCid is created upon exercising the choice AddPassenger with the argument ticket.passenger
We then create a ticket contract, and store it in the variable ticketCid,
and finally we return the ticketCid, and flightCid. This return type is stated at the very beginning
choice Accept_Invite : (ContractId Ticket, ContractId Flight)

1 Like

@Zerum
To add to the response by @Max, who provided a detailed technical breakdown of the code in the choice, here’s the rationale for the choice and the workflow it facilitates.
The purpose of the FlightInvite template is to obtain passenger’s authority using Propose-Accept pattern, which is ubiquitous in Daml projects. The Flight contract is initially created by the airline with the empty list of passengers. The airline then sends an invite for the flight to each invited passenger by creating a FlightInvite contract for each invited passenger. By accepting the invite (exercising Accept_Invite choice) the invited passenger provides his or her authority for the issuance of a ticket (create Ticket) and to be added to the list of passengers on the flight (exercise flightCid AddPassenger with passenger = ticket.passenger). Neither of these actions is possible without passenger’s authority (passenger is a signatory on both the Ticket and the Flight contracts), which represents that the airline cannot issue a ticket or add passenger to the flight without passenger’s consent.

1 Like

Thank you for your detailed reply. This is really useful for me.

Regards
Erum

1 Like

No problem, feel free to ask whenever you have questions! Also if any of the comments resolved your question, could you help us mark it as a solution? Thanks!