ligo/gitlab-pages/docs/tutorials/get-started/tezos-taco-shop-payout.ligo

55 lines
1.9 KiB
Plaintext
Raw Normal View History

type taco_supply is
record [
2019-06-21 16:45:52 +04:00
current_stock : nat;
max_price : tez
]
2019-06-21 16:45:52 +04:00
type taco_shop_storage is map (nat, taco_supply)
2019-06-21 16:45:52 +04:00
type return is list (operation) * taco_shop_storage
const ownerAddress : address = "tz1TGu6TN5GSez2ndXXeDX6LgUDvLzPLqgYV"
const donationAddress : address = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx"
function buy_taco (const taco_kind_index : nat; var taco_shop_storage : taco_shop_storage) : return is
block {
// Retrieve the taco_kind from the contract's storage or fail
const taco_kind : taco_supply =
case taco_shop_storage[taco_kind_index] of
Some (kind) -> kind
| None -> (failwith ("Unknown kind of taco.") : taco_supply)
end;
const current_purchase_price : tez =
taco_kind.max_price / taco_kind.current_stock;
2019-06-21 16:45:52 +04:00
if amount =/= current_purchase_price then
// We won't sell tacos if the amount is not correct
failwith ("Sorry, the taco you are trying to purchase has a different price");
else skip;
// Decrease the stock by 1n, because we have just sold one
taco_kind.current_stock := abs (taco_kind.current_stock - 1n);
2019-06-21 16:45:52 +04:00
// Update the storage with the refreshed taco_kind
taco_shop_storage[taco_kind_index] := taco_kind;
2020-03-31 16:39:26 +04:00
const receiver : contract (unit) =
case (Tezos.get_contract_opt (ownerAddress): option(contract (unit))) of
Some (contract) -> contract
| None -> (failwith ("Not a contract") : contract (unit))
end;
const donationReceiver : contract (unit) =
case (Tezos.get_contract_opt (donationAddress): option(contract (unit))) of
Some (contract) -> contract
| None -> (failwith ("Not a contract") : contract (unit))
end;
2019-06-21 16:45:52 +04:00
const donationAmount : tez = amount / 10n;
2019-06-21 16:45:52 +04:00
const operations : list (operation) = list [
2020-03-31 16:39:26 +04:00
Tezos.transaction (unit, amount - donationAmount, receiver);
Tezos.transaction (unit, donationAmount, donationReceiver);
]
} with (operations, taco_shop_storage)