Skip to content

Commit 93a32fc

Browse files
committed
Updates to coin distributor example
1 parent 68bf846 commit 93a32fc

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

convex-core/src/main/cvx/convex/lab/distributor.cvx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,38 @@
1515

1616
;; Set the amount of available coins. Only a trusted allocator can do this
1717
(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"))
2020

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!"))
2323

24-
(set! available-coins amount))
24+
(set! available-coins (int amount)))
2525

26-
;; Distribute coins coins. Only a trusted distributor can do this
26+
;; Distribute coins. Only a trusted distributor can do this
2727
(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)))
3843

3944
(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))

convex-core/src/test/java/convex/actors/DistributorTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ public class DistributorTest extends ACVMTest {
4141
assertArgumentError(step(c,"(call DIST (distribute *address* nil))"));
4242
assertArgumentError(step(c,"(call DIST (distribute *address* -1))"));
4343
assertCastError(step(c,"(call DIST (distribute :foo 0))"));
44-
45-
// initially no actor balance, so can't set available coins greater than 0
46-
assertStateError(step(c,"(call DIST (set-available 1))"));
4744

4845
// zero distribution is OK
4946
c=exec(c,"(call DIST (distribute *address* 0))");
5047

48+
assertTrustError(step(c,"(query-as #0 `(call ~DIST (set-available 1000)))"));
5149
}
5250

5351
@Test public void testDistributuion() {
@@ -70,4 +68,18 @@ public class DistributorTest extends ACVMTest {
7068
assertEquals(700000L,evalL(c,"DIST/available-coins"));
7169

7270
}
71+
72+
@Test public void testWithdraw() {
73+
Context c=context();
74+
75+
// set available coins works after transferring in some coins
76+
c=exec(c,"(transfer DIST 3000000)");
77+
assertEquals(3000000L,evalL(c,"(balance DIST)"));
78+
79+
c=exec(c,"(call DIST (withdraw 1000000))");
80+
assertEquals(2000000L,evalL(c,"(balance DIST)"));
81+
82+
// withdraw more than is left = :FUNDS error
83+
assertFundsError(step(c,"(call DIST (withdraw 2000001))"));
84+
}
7385
}

0 commit comments

Comments
 (0)