Skip to main content

Tickets

The following functions are available under the Tezos. namespace.

cameligo

val Tezos.create_ticket : 'value -> nat -> ('value ticket) option

jsligo

let Tezos.create_ticket: 'value => nat => option<ticket<'value>>

To create a ticket, the value and the amount of tickets to be created needs to be provided. The ticket will also contain the contract address it originated from (which corresponds to Tezos.self). The resulting value is None if the amount is zero.

cameligo
let my_ticket1 = Option.unopt (Tezos.create_ticket 1 10n)
let my_ticket2 = Option.unopt (Tezos.create_ticket "one" 10n)
jsligo
const my_ticket1 = Option.unopt(Tezos.create_ticket(1, 10n));
const my_ticket2 = Option.unopt(Tezos.create_ticket("one", 10n));
cameligo

val Tezos.read_ticket : 'value ticket -> (address * ('value * nat)) * 'value ticket

jsligo

let Tezos.read_ticket: ticket<'value> => <<address, <'value , nat>> , ticket<'value>>

Reading a ticket will return a tuple with the ticket address, the value and the same ticket for later use. A ticket is only consumed when it is dropped (e.g. DROP-ed from the Michelson stack) so if the returned ticket isn't stored in some form by your contract, it will be fully consumed.

cameligo

To read the content of a ticket, you can either use tuple destructuring or pattern matching:

let v =
let (_addr, (payload, _amt)), _ticket = Tezos.read_ticket my_ticket1
in payload
jsligo

To read the content of a ticket, you need to use tuple destructuring:

const v2 = do {
let [[_addr, [payload, _amt]], _ticket] = Tezos.read_ticket (my_ticket2);
return payload;
}
cameligo

val Tezos.split_ticket : 'value ticket -> nat * nat -> ('value ticket * 'value ticket) option

jsligo

let Tezos.split_ticket: ticket<'value> => <nat , nat> => option <<ticket<'value>, ticket<'value>>>

To partially use/consume a ticket, you have to split it. Provided a ticket and two amounts, two new tickets will be returned to you if, and only if, the sum equals to the amount of the original ticket.

cameligo
let ta, tb =
match Tezos.split_ticket my_ticket1 (6n, 4n) with
None -> failwith "amt_a + amt_v <> amt"
| Some split_tickets -> split_tickets
jsligo
const [ta, tb] =
match(Tezos.split_ticket(my_ticket1, [6n, 4n])) {
when(None()): failwith("amt_a + amt_v != amt");
when(Some(split_tickets)): split_tickets
};
cameligo

val Tezos.join_tickets : 'value ticket * 'value ticket -> ('value ticket) option

jsligo

let Tezos.join_tickets = <ticket<'value>, ticket<'value>> => option <ticket<'value>>

To add two tickets, you have to join them. This works as the inverse of Tezos.split_ticket. Provided two tickets with the same ticketer and content, they are deleted and a new ticket will be returned with an amount equal to the sum of the amounts of the input tickets.

cameligo
let tc : int ticket option =
let ta = Option.unopt (Tezos.create_ticket 1 10n) in
let tb = Option.unopt (Tezos.create_ticket 1 5n) in
Tezos.join_tickets (ta, tb)
jsligo
const ta = Option.unopt(Tezos.create_ticket(1, 10n));
const tb = Option.unopt(Tezos.create_ticket(1, 5n));
const tc = Tezos.join_tickets([ta, tb]);