-
Notifications
You must be signed in to change notification settings - Fork 0
/
allowance.mligo
31 lines (27 loc) · 1.23 KB
/
allowance.mligo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import "errors.mligo" "Errors"
type t = (address, nat) map
let get_allowed_amount (a:t) (spender:address) : nat =
match Map.find_opt spender a with
| Some v -> v
| None -> 0n
let set_allowed_amount (a:t) (spender:address) (allowed_amount: nat) : t =
Map.add spender allowed_amount a
let decrease_allowance (a:t) (spender:address) (decrease_amount: nat) : t =
match Map.find_opt spender a with
| Some v ->
let _ = if (v < decrease_amount) then
// TZIP-7 specifies that it should fail with requested allowance and current allowance
([%Michelson ({| { FAILWITH } |} : string * (nat * nat) -> unit)]
(Errors.not_enough_allowance, (decrease_amount, v)) : unit)
else
()
in
let new_allowed_amount = abs(v - decrease_amount) in
if new_allowed_amount > 0n then
Map.update spender (Some(new_allowed_amount)) a
else
Map.remove spender a
| None ->
// TZIP-7 specifies that it should fail with requested allowance and current allowance
([%Michelson ({| { FAILWITH } |} : string * (nat * nat) -> t)]
(Errors.not_enough_allowance, (decrease_amount, 0n)) : t)