15
15
16
16
;; Set the amount of available coins. Only a trusted allocator can do this
17
17
(defn ^:callable set-available [amount]
18
- (when (> amount *balance*)
19
- (fail :STATE "Insufficient balance "))
18
+ (if (not (trust/trusted? allocator *caller* :set-available amount))
19
+ (fail :TRUST "Not authorised as allocator "))
20
20
21
- (when-not (trust/trusted? allocator *caller* :set-available amount)
22
- (fail :TRUST "Not authorised as allocator "))
21
+ (if (< amount 0)
22
+ (fail :ARGUMENT "Negative amount! "))
23
23
24
- (set! available-coins amount))
24
+ (set! available-coins (int amount) ))
25
25
26
- ;; Distribute coins coins . Only a trusted distributor can do this
26
+ ;; Distribute coins. Only a trusted distributor can do this
27
27
(defn ^:callable distribute [receiver amount]
28
- (cond
29
- (not (int? amount))
30
- (fail :ARGUMENT "amount must be an integer")
31
- (not (trust/trusted? distributor *caller* :distribute amount))
32
- (fail :TRUST "Not authorised to distribute")
33
- (> amount available-coins)
34
- (fail :FUNDS "Insufficient available coins")
35
- (do
36
- (set! available-coins (- available-coins amount))
37
- (transfer receiver amount))))
28
+ (if (not (int? amount))
29
+ (fail :ARGUMENT "amount must be an integer"))
30
+
31
+ (if (not (trust/trusted? distributor *caller* :distribute amount))
32
+ (fail :TRUST "Not authorised to distribute"))
33
+
34
+ (if (> amount available-coins)
35
+ (fail :FUNDS "Insufficient available coins"))
36
+
37
+ ;; Every check passed, so:
38
+ ;; 1. reduce available coins (Effect)
39
+ ;; 2. Make an external transfer (interaction)
40
+ (do
41
+ (set! available-coins (- available-coins amount))
42
+ (transfer receiver amount)))
38
43
39
44
(defn ^:callable receive-coin [_ _ _]
40
- (accept *offer*))
45
+ (accept *offer*))
46
+
47
+ (defn ^:callable withdraw [amount]
48
+ (if (not (trust/trusted? allocator *caller* :withdraw amount))
49
+ (fail :TRUST "Not authorised to withdraw"))
50
+
51
+ ;; Transfer the withdrawn amount back to the caller
52
+ (transfer *caller* amount))
0 commit comments