Skip to content

Commit

Permalink
Updates to coin distributor example
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Jan 9, 2025
1 parent 68bf846 commit 93a32fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
46 changes: 29 additions & 17 deletions convex-core/src/main/cvx/convex/lab/distributor.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,38 @@

;; Set the amount of available coins. Only a trusted allocator can do this
(defn ^:callable set-available [amount]
(when (> amount *balance*)
(fail :STATE "Insufficient balance"))
(if (not (trust/trusted? allocator *caller* :set-available amount))
(fail :TRUST "Not authorised as allocator"))

(when-not (trust/trusted? allocator *caller* :set-available amount)
(fail :TRUST "Not authorised as allocator"))
(if (< amount 0)
(fail :ARGUMENT "Negative amount!"))

(set! available-coins amount))
(set! available-coins (int amount)))

;; Distribute coins coins. Only a trusted distributor can do this
;; Distribute coins. Only a trusted distributor can do this
(defn ^:callable distribute [receiver amount]
(cond
(not (int? amount))
(fail :ARGUMENT "amount must be an integer")
(not (trust/trusted? distributor *caller* :distribute amount))
(fail :TRUST "Not authorised to distribute")
(> amount available-coins)
(fail :FUNDS "Insufficient available coins")
(do
(set! available-coins (- available-coins amount))
(transfer receiver amount))))
(if (not (int? amount))
(fail :ARGUMENT "amount must be an integer"))

(if (not (trust/trusted? distributor *caller* :distribute amount))
(fail :TRUST "Not authorised to distribute"))

(if (> amount available-coins)
(fail :FUNDS "Insufficient available coins"))

;; Every check passed, so:
;; 1. reduce available coins (Effect)
;; 2. Make an external transfer (interaction)
(do
(set! available-coins (- available-coins amount))
(transfer receiver amount)))

(defn ^:callable receive-coin [_ _ _]
(accept *offer*))
(accept *offer*))

(defn ^:callable withdraw [amount]
(if (not (trust/trusted? allocator *caller* :withdraw amount))
(fail :TRUST "Not authorised to withdraw"))

;; Transfer the withdrawn amount back to the caller
(transfer *caller* amount))
18 changes: 15 additions & 3 deletions convex-core/src/test/java/convex/actors/DistributorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ public class DistributorTest extends ACVMTest {
assertArgumentError(step(c,"(call DIST (distribute *address* nil))"));
assertArgumentError(step(c,"(call DIST (distribute *address* -1))"));
assertCastError(step(c,"(call DIST (distribute :foo 0))"));

// initially no actor balance, so can't set available coins greater than 0
assertStateError(step(c,"(call DIST (set-available 1))"));

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

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

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

}

@Test public void testWithdraw() {
Context c=context();

// set available coins works after transferring in some coins
c=exec(c,"(transfer DIST 3000000)");
assertEquals(3000000L,evalL(c,"(balance DIST)"));

c=exec(c,"(call DIST (withdraw 1000000))");
assertEquals(2000000L,evalL(c,"(balance DIST)"));

// withdraw more than is left = :FUNDS error
assertFundsError(step(c,"(call DIST (withdraw 2000001))"));
}
}

0 comments on commit 93a32fc

Please sign in to comment.