Choice Accept_invite

Hi Team,
In the following choice, Can you explain this in term of DAML, As we already have Ticket contract than why we have “create ticket” here? And how it will execute in this choice?

ticketCid ← create ticket

Thank you

Erum

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)

Hi Zerum,

template FlightInvite
  with
    ticket : Ticket
  where
    signatory ticket.airline
    observer ticket.passenger

    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)

The Ticket here is being used as a record, see here
https://docs.daml.com/daml/intro/3_Data.html#records

It’s important to note that Ticket is not being created (or written into the ledger at this moment, it is simply being used as a record type)

We know that in order for the contract Ticket to be created, we need the airline party and the passenger party.

What is happening is that FlightInvite is being created by the airline (as the signatory is only ticket.ariline)
The observer here is ticket.passenger, meaning this FlightInvite is visible to the passenger.

With the FlightInvite contract created, we have the signature of the airline.
When the passenger sees this contract, and exercises Accept_Invite, the signature is passed down to AddPassengers, which a new flight contract is created with the additional passenger,

and we create the ticket. (write to the ledger)

Means “create ticket” is used to record the ticket IDs of invited passenger". I am just confused with this word “create ticket”.

Hi @Zerum,

The create operation creates a new Contract on the ledger and returns its Contract ID.

To create a contract, the create operation takes a single argument, which is of the data type associated with a template. For example, when you write:

template Ticket
  with
    name: Text
    id: Int
  where
    ...

this implicitly creates a data type for Ticket as if you had additionally typed:

data Ticket = Ticket with name: Text, id: Int

which allows you to create new Ticket objects (that do not have to correspond to data on the ledger) by just doing:

let t = Ticket with name = "Gary", id = 1

So, typically, when you use create, you’d see it written with an explicit object instanciation:

create Ticket with name = "Gary", id = 1

which parses as

create (Ticket with name = "Gary", id = 1)

and is thus the exact same as:

let t = Ticket with name = "Gary", id = 1
create t

Hope that helps.

1 Like

But as we have “Ticket” Contract already. Than why we are creating ticket again here

ticketCid <- create ticket

Sorry just confused here .What exactly this line does in term of DAML as of one reply above

“”"The Ticket here is being used as a record, see here
https://docs.daml.com/daml/intro/3_Data.html#records

It’s important to note that Ticket is not being created (or written into the ledger at this moment, it is simply being used as a record type)""

Secod Reply:
“The create operation creates a new Contract on the ledger and returns its Contract ID.”

When the FlightInvite contract is created, it is created with a field called ticket of type Ticket. That type is the record type that corresponds to the Ticket template. As I explained in my previous message, this record may or may not correspond to an actual Ticket contract on the ledger.

In other words, you can create a TicketInvite with any random data as the ticket field (provided it has the right “shape”).

The Accept_Invite choice is then going to validate that ticket data against data that is actually on the ledger (namely the data fetched from the ledger based on a Contract ID for a given flight), and, if this all checks out, the line

ticketCid <- create ticket

actually creates a Ticket contract on the ledger with the data that was in ticket.

Is this clearer?

1 Like

Values in Daml are immutable. The value of ticket is set by the party that created the FlightInvite and never changes after that.

If you just said create, how would Daml know what to do? What template to use, which values to fill it with? create needs an argument in order to create an entry on the ledger; entries on the ledger have to be contracts, and create needs to know what type of contract and with what data.