Skip to content

Instantly share code, notes, and snippets.

@oteku
Last active December 9, 2021 01:57
Show Gist options
  • Save oteku/45a29535df10f41db863d11b0f4c3039 to your computer and use it in GitHub Desktop.
Save oteku/45a29535df10f41db863d11b0f4c3039 to your computer and use it in GitHub Desktop.
CCG Ligo smartcontract
// MAGIC CARD DESIGN
// type color =
// | Blue
// | Green
// | Red
// | Black
// | White
// type card_color =
// | Colored of color
// | Multicoled of color list
// | Coloredless
// type card_type =
// | Land
// | Creature
// | Artifact
// | Enchantment
// | Planeswalker
// type rarity =
// | Common
// | Uncommon
// | Rare
// type mana_cost = (color, int) map
// type card_data {
// name: string
// ; card_type: card_type
// ; keywords: string list
// ; color: card_color
// ; text: string
// ; permanent: bool
// ; mana_cost: mana_cost
// ; rarity: rarity
// }
// DOMAIN TYPES
// Simplify the design of a card to its name first
type card_name = string
type card_price = tez
type quantity = int
type price = tez
// CONTRACT TYPES
type parameter =
| Configure of address
| Supply of (card_name * card_price * quantity)//manager may add more cards
| Buy of (card_name * price * address) // one want to buy a card and transfert to the rollup
| Withdraw // one want to get card back to tz1 address after a L2 withdraw
type storage = {
vendor: address
; cards: (card_name , (card_price * quantity)) big_map
}
type return = operation list * storage
// CONTRACT FUNCTIONS
let configure (vendor: address) (store: storage): return =
let store = {store with vendor = vendor } in
(([]: operation list), store)
let supply (name: card_name) (price: card_price) (qty: quantity) (store: storage): return =
if Tezos.source <> store.vendor
then (failwith ("Access Denied"): return)
else let cards = Big_map.update name (Some(price, qty)) store.cards in
let store = { store with cards = cards} in
(([]: operation list), store)
let purchase (name: card_name) (price: card_price) (buyer: address) (store: storage): return =
match (Big_map.find_opt name store.cards) with
| None -> (failwith ("Unknown card"): return)
| Some (cp, stock) -> if stock < 1
then (failwith ("The card is out of stock. Try to trade"): return)
else if price <> cp
then (failwith ("Incorrect price"): return)
else (
// Get a contract to pay the vendor
let paiement_receiver: unit contract = Tezos.get_contract_with_error store.vendor "Vendor cannot receive tez!" in
// Get a contract to send the card
let card_receiver: (( string * tez ) ticket) contract = Tezos.get_contract_with_error buyer "Buyer cannot receive the card!" in
// create a ticket for my card
let my_card_ticket = Tezos.create_ticket (name, cp) 1n in
// update the storage
let stock = stock - 1 in
let cards = if stock > 0
then Big_map.update name (Some(cp, stock)) store.cards
else Big_map.remove name store.cards in
let store = { store with cards = cards} in
// pay the card
let op_payment: operation = Tezos.transaction () price paiement_receiver in
// transfer card to L2
let op_ticket_transfert_to_rollup: operation = Tezos.transaction my_card_ticket 0tez card_receiver in
([op_payment; op_ticket_transfert_to_rollup], store)
)
let main (p, s : parameter * storage) : return =
match p with
| Configure vendor -> configure vendor s
| Supply (card, price, qty) -> supply card price qty s
| Buy (card, price, buyer) -> purchase card price buyer s
| Withdraw -> (failwith("Not implemented"): return)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment