From 565c30e0073bc5f8cb44a11ec8022e423265490c Mon Sep 17 00:00:00 2001 From: mikera Date: Sat, 21 Sep 2024 12:23:27 +0100 Subject: [PATCH] Fungible edits --- .../src/main/cvx/convex/asset/fungible.cvx | 43 ++++++++----------- .../test/java/convex/lib/FungibleTest.java | 8 ++-- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/convex-core/src/main/cvx/convex/asset/fungible.cvx b/convex-core/src/main/cvx/convex/asset/fungible.cvx index 12f5b93d4..1378a759d 100644 --- a/convex-core/src/main/cvx/convex/asset/fungible.cvx +++ b/convex-core/src/main/cvx/convex/asset/fungible.cvx @@ -18,9 +18,9 @@ :examples [{:code "(deploy [(build-token {}) (add-mint {:minter *address* :max-supply 1000000000})])"}] :signature [{:params [config]}]}} [config] - (let [max-supply (get config :max-supply) + (let [max-supply (int (get config :max-supply 0)) minter (or (:minter config) *address*)] - (or (<= 0 (int max-supply)) (fail "Max supply must be non-negative")) + (or (<= 0 max-supply) (fail "Max supply must be non-negative")) `(do (import convex.trust :as trust) (declare balance transfer) @@ -38,28 +38,27 @@ (let [amount (int amount) bal (balance *caller*)] ;; Burn amount must be less than or equal to caller's balance. - (or (<= 0 amount bal) (fail :STATE "Burn amount not available" )) - (set-holding *caller* - (- bal - amount)) - (def supply (- supply - amount)))) + (cond (< amount 0) (fail :ARGUMENT "Cannot burn negative quantity")) + (cond (> amount bal) (fail :FUNDS "Burn amount not available" )) + + ;; do updates to balance and supply + (set-holding *caller* (- bal amount)) + (set! supply (- supply amount)))) (defn mint ^{:callable true} [amount] - (when-not (trust/trusted? minter *caller* :mint) (fail :TRUST "No rights to mint")) + (when-not (trust/trusted? minter *caller* :mint amount) (fail :TRUST "No rights to mint")) (let [amount (int amount) new-supply (+ supply amount) ;; note: might overflow, important check bal (balance *caller*) new-bal (+ bal amount)] + ;; New supply must be in valid range. ~(if max-supply `(when-not (<= 0 new-supply max-supply) (fail :STATE "Mint exceeds max supply"))) - (set-holding *caller* - new-bal) - (def supply - new-supply)))))) + (set-holding *caller* new-bal) + (set! supply new-supply)))))) (defn build-token ^{:doc {:description ["Creates deployable code for a new fungible token which follows the interface described in `convex.asset`." @@ -155,22 +154,18 @@ [addr amount data] (let [addr (address addr) amount (-qc amount) - bal (or (get-holding *caller*) - 0) - tbal (or (get-holding addr) - 0)] - ;; Amount must be in valid range. - ;; + bal (or (get-holding *caller*) 0) + tbal (or (get-holding addr) 0)] ~(if (:checked? config) `(if-let [f (check-transfer *caller* addr amount)] (fail :BLOCK f))) + ;; Amount must be in valid range. (cond (< amount 0) (fail :ARGUMENT "negative transfer")) (cond (> amount bal) (fail :FUNDS "insufficent token balance")) - ;; Need this check in case of self-transfers. - (when (= *caller* - addr) - (return amount)) + ;; Need this in case of self-transfers. + (when (= *caller* addr) (return amount)) + (set-holding *caller* (- bal amount)) (set-holding addr (+ tbal amount)))) @@ -185,7 +180,7 @@ (let [receiver (address receiver) quantity (-qc quantity)] (if (<= quantity 0) - (set! offers (dissoc-in offers [*caller* receiver])) + (set! offers (dissoc-in offers [*caller* receiver])) ;; dissoc if no positive offer (set! offers (assoc-in offers [*caller* receiver] quantity))) quantity)) diff --git a/convex-core/src/test/java/convex/lib/FungibleTest.java b/convex-core/src/test/java/convex/lib/FungibleTest.java index 1f8b2474a..f401b7429 100644 --- a/convex-core/src/test/java/convex/lib/FungibleTest.java +++ b/convex-core/src/test/java/convex/lib/FungibleTest.java @@ -169,12 +169,14 @@ private static State buildState() { c=exec(c,"(fungible/burn token 900)"); assertEquals(100L,evalL(c,"(fungible/balance token *address*)")); - assertStateError(step(c,"(fungible/burn token 101)")); // Fails, not held + assertFundsError(step(c,"(fungible/burn token 101)")); // Fails, not held + assertArgumentError(step(c,"(fungible/burn token -1)")); // Fails, can't burn negative + c=exec(c,"(fungible/burn token 100)"); assertEquals(0L,evalL(c,"(fungible/balance token *address*)")); - assertStateError(step(c,"(fungible/burn token 1)")); // Fails, not held + assertFundsError(step(c,"(fungible/burn token 1)")); // Fails, not held } @@ -186,7 +188,7 @@ private static State buildState() { c=exec(c,"(fungible/transfer token "+VILLAIN+" 800)"); assertEquals(200L,evalL(c,"(fungible/balance token *address*)")); - assertStateError(step(c,"(fungible/burn token 201)")); // Fails, not held + assertFundsError(step(c,"(fungible/burn token 201)")); // Fails, not held assertNotError(step(c,"(fungible/burn token 200)")); // OK since held }