ligo/src/parser/pascaligo/Tests/crowdfunding.ligo

56 lines
1.6 KiB
Plaintext
Raw Normal View History

2019-05-12 20:56:22 +00:00
type store is
record [
goal : mutez;
2019-05-12 20:56:22 +00:00
deadline : timestamp;
backers : map (address, nat);
funded : bool;
]
function back (var store : store) : list (operation) * store is
var operations : list (operation) := list []
2019-05-12 20:56:22 +00:00
begin
if now > store.deadline then
2019-05-12 20:56:22 +00:00
fail "Deadline passed";
else
case store.backers[sender] of [
None -> store.backers[sender] := amount
// or: None -> patch store.backers with map sender -> amount end
2019-05-12 20:56:22 +00:00
| _ -> skip
]
end with (operations, store)
2019-05-12 20:56:22 +00:00
function claim (var store : store) : list (operation) * store is
var operations : list (operation) := nil
2019-05-12 20:56:22 +00:00
begin
if now <= store.deadline then
fail "Too soon."
2019-05-12 20:56:22 +00:00
else
case store.backers[sender] of
None ->
fail "Not a backer."
2019-05-12 20:56:22 +00:00
| Some (amount) ->
if balance >= store.goal or store.funded then
fail "Goal reached: no refund."
2019-05-12 20:56:22 +00:00
else
begin
operations := list [transaction (unit, sender, amount)];
2019-05-12 20:56:22 +00:00
remove sender from map store.backers
end
end
end with (operations, store)
function withdraw (var store : store) : list (operation) * store is
var operations : list (operation) := list end
begin
if sender = owner then
if now >= store.deadline then
if balance >= store.goal then {
store.funded := True;
// or: patch store with record funded = True end;
operations := list [Transfer (owner, balance)];
};
else fail "Below target."
else fail "Too soon.";
else skip
end with (operations, store)