2020-02-21 20:16:53 +04:00
|
|
|
type taco_supply is
|
|
|
|
record [
|
2019-06-13 04:50:55 +04:00
|
|
|
current_stock : nat;
|
2020-02-21 20:16:53 +04:00
|
|
|
max_price : tez
|
|
|
|
]
|
2019-06-13 04:50:55 +04:00
|
|
|
|
2020-02-21 20:16:53 +04:00
|
|
|
type taco_shop_storage is map (nat, taco_supply)
|
|
|
|
|
|
|
|
type return is list (operation) * taco_shop_storage
|
|
|
|
|
2020-02-21 21:13:09 +04:00
|
|
|
function main (const taco_kind_index : nat ; var taco_shop_storage : taco_shop_storage) : return is
|
2020-02-21 20:16:53 +04:00
|
|
|
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-13 04:50:55 +04:00
|
|
|
|
|
|
|
if amount =/= current_purchase_price then
|
2020-02-21 20:16:53 +04:00
|
|
|
// 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-13 04:50:55 +04:00
|
|
|
|
|
|
|
// Update the storage with the refreshed taco_kind
|
2020-02-21 20:16:53 +04:00
|
|
|
taco_shop_storage[taco_kind_index] := taco_kind
|
|
|
|
} with ((nil : list (operation)), taco_shop_storage)
|