Choice Accept_invite
- ZerumOP
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.airlineflightCid <- exercise flightCid AddPassenger with passenger = ticket.passenger ticketCid <- create ticket return (ticketCid, flightCid) - rikotacards
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
Tickethere is being used as a record, see here
https://docs.daml.com/daml/intro/3_Data.html#recordsIt’s important to note that
Ticketis 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
Ticketto be created, we need theairlineparty and thepassengerparty.What is happening is that
FlightInviteis being created by the airline (as the signatory is only ticket.ariline)
The observer here is ticket.passenger, meaning thisFlightInviteis visible to the passenger.With the
FlightInvitecontract created, we have the signature of the airline.
When the passenger sees this contract, and exercisesAccept_Invite, the signature is passed down to AddPassengers, which a new flight contract is created with the additional passenger,and we
createthe ticket. (write to the ledger) - Zerum
Means “create ticket” is used to record the ticket IDs of invited passenger". I am just confused with this word “create ticket”.
- Gary_Verhaegen 1
Hi @Zerum,
The
createoperation creates a new Contract on the ledger and returns its Contract ID.To create a contract, the
createoperation 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
Ticketas if you had additionally typed:data Ticket = Ticket with name: Text, id: Intwhich allows you to create new
Ticketobjects (that do not have to correspond to data on the ledger) by just doing:let t = Ticket with name = "Gary", id = 1So, typically, when you use create, you’d see it written with an explicit object instanciation:
create Ticket with name = "Gary", id = 1which 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 tHope that helps.
- Zerum
But as we have “Ticket” Contract already. Than why we are creating ticket again here
ticketCid <- create ticketSorry just confused here .What exactly this line does in term of DAML as of one reply above
“”"The
Tickethere is being used as a record, see here
https://docs.daml.com/daml/intro/3_Data.html#recordsIt’s important to note that
Ticketis not being created (or written into the ledger at this moment, it is simply being used as a record type)""Secod Reply:
“Thecreateoperation creates a new Contract on the ledger and returns its Contract ID.” - Gary_Verhaegen 1
When the
FlightInvitecontract is created, it is created with a field calledticketof typeTicket. That type is the record type that corresponds to theTickettemplate. 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
TicketInvitewith any random data as theticketfield (provided it has the right “shape”).The
Accept_Invitechoice is then going to validate thatticketdata 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 lineticketCid <- create ticketactually creates a
Ticketcontract on the ledger with the data that was inticket.Is this clearer?
- Gary_Verhaegen
Zerum:What is the value of ticket before the create
Values in Daml are immutable. The value of
ticketis set by the party that created theFlightInviteand never changes after that.
Zerum:why is ticket an input to the command creating the ticket?
If you just said
create, how would Daml know what to do? What template to use, which values to fill it with?createneeds an argument in order to create an entry on the ledger; entries on the ledger have to be contracts, andcreateneeds to know what type of contract and with what data.