From bdfbaa5d7709445e1459f700aa09b086c700eec4 Mon Sep 17 00:00:00 2001 From: mikera Date: Sat, 21 Sep 2024 13:10:17 +0100 Subject: [PATCH] Tighten fungible token tests --- .../src/main/cvx/convex/asset/fungible.cvx | 10 ++--- .../src/main/cvx/convex/asset/multi-token.cvx | 39 +++++++++---------- .../src/main/cvx/convex/asset/share.cvx | 8 ++-- .../src/main/cvx/convex/asset/wrap/convex.cvx | 7 ++-- .../src/test/java/convex/lib/AssetTester.java | 7 +++- 5 files changed, 36 insertions(+), 35 deletions(-) diff --git a/convex-core/src/main/cvx/convex/asset/fungible.cvx b/convex-core/src/main/cvx/convex/asset/fungible.cvx index 1378a759d..7013be93f 100644 --- a/convex-core/src/main/cvx/convex/asset/fungible.cvx +++ b/convex-core/src/main/cvx/convex/asset/fungible.cvx @@ -242,16 +242,14 @@ :examples [{:code "(mint my-token-address 1000)"}] :signature [{:params [token amount]}]}} [token amount] - (call token - (burn amount))) + (call token (burn amount))) (defn mint ^{:doc {:description "Mints an amount of tokens for the given token. User must have minting privileges. Amount may be negative to burn fungible tokens." :examples [{:code "(mint my-token-address 1000)"}] :signature [{:params [token amount]}]}} [token amount] - (call token - (mint amount))) + (call token (mint amount))) (defn transfer ^{:doc {:description "Transfers balance of a fungible token." @@ -263,8 +261,8 @@ (cond ;; First check if receiver has a callable receive-asset function. If so, use this (callable? target 'receive-asset) - (do - (offer target token amopunt) ;; Offer correct quantity + (let [amount (int amount)] + (offer target token amount) ;; Offer correct quantity (call target (receive-asset token amount nil))) diff --git a/convex-core/src/main/cvx/convex/asset/multi-token.cvx b/convex-core/src/main/cvx/convex/asset/multi-token.cvx index 74c3f83b1..b19fdbf51 100644 --- a/convex-core/src/main/cvx/convex/asset/multi-token.cvx +++ b/convex-core/src/main/cvx/convex/asset/multi-token.cvx @@ -23,12 +23,12 @@ ;;;;;;;;; Private functions (defn -qc - ^{:doc {:description "Checks a token quantity."} - :private? true} - [q] - (cond (int? q) q ;; base case, quantity should always be an integer - (nil? q) 0 - (fail :ARGUMENT "Invalid token quantity"))) + ^{:doc {:description "Checks a token quantity."} + :private true} + [q] + (cond (int? q) q ;; base case, quantity should always be an integer + (nil? q) 0 + (fail :ARGUMENT "Invalid token quantity"))) (defn -set-balance [addr id bal] @@ -77,17 +77,14 @@ ^{:callable true} [addr amount data] (let [addr (address addr) - amount (if amount - (int amount) - 0) + amount (-qc amount) id *scope* bal (-get-balance *caller* id) tbal (-get-balance addr id)] ;; Amount must be in valid range. - ;; - (assert (<= 0 - amount - bal)) + (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) @@ -172,9 +169,9 @@ ^{:callable true} [amount] (let [id *scope* - token (or (get tokens id) (fail "token does not exist")) + token (or (get tokens id) (fail :STATE "token does not exist")) [controller supply] token] - (when-not (trust/trusted? controller *caller* :mint) + (when-not (trust/trusted? controller *caller* :mint amount) (fail :TRUST "No rights to mint")) (let [amount (-qc amount) ;; Mint amount. @@ -197,21 +194,21 @@ (defn quantity-add ^{:callable true} [a b] - (let [a (if a (int a) 0) - b (if b (int b) 0)] + (let [a (cond a (int a) 0) + b (cond b (int b) 0)] (+ a b))) (defn quantity-sub ^{:callable true} [a b] - (let [a (if a (int a) 0) - b (if b (int b) 0)] + (let [a (cond a (int a) 0) + b (cond b (int b) 0)] (if (>= a b) (- a b) 0))) (defn quantity-subset? ^{:callable true} [a b] - (let [a (if a (int a) 0) - b (if b (int b) 0)] + (let [a (cond a (int a) 0) + b (cond b (int b) 0)] (<= a b))) diff --git a/convex-core/src/main/cvx/convex/asset/share.cvx b/convex-core/src/main/cvx/convex/asset/share.cvx index 8282fde69..d0165b6ad 100644 --- a/convex-core/src/main/cvx/convex/asset/share.cvx +++ b/convex-core/src/main/cvx/convex/asset/share.cvx @@ -96,14 +96,14 @@ bal (-get-balance *caller* id) tbal (-get-balance addr id)] ;; Amount must be in valid range. - ;; - (assert (<= 0 - amount - bal)) + (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)) + ;; TODO: need to transfer unclaimed quantity (-set-balance *caller* id (- bal amount)) (-set-balance addr id (+ tbal amount)) diff --git a/convex-core/src/main/cvx/convex/asset/wrap/convex.cvx b/convex-core/src/main/cvx/convex/asset/wrap/convex.cvx index 546d097c0..7f6b3b02c 100644 --- a/convex-core/src/main/cvx/convex/asset/wrap/convex.cvx +++ b/convex-core/src/main/cvx/convex/asset/wrap/convex.cvx @@ -44,10 +44,11 @@ amount (-qc amount) bal (-get-balance *caller*) tbal (-get-balance addr)] + ;; Amount must be in valid range. - (assert (<= 0 - amount - bal)) + (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)) diff --git a/convex-core/src/test/java/convex/lib/AssetTester.java b/convex-core/src/test/java/convex/lib/AssetTester.java index 146ee87dc..e43fbad55 100644 --- a/convex-core/src/test/java/convex/lib/AssetTester.java +++ b/convex-core/src/test/java/convex/lib/AssetTester.java @@ -150,7 +150,7 @@ public static void doFungibleTests(Context ctx, ACell token, Address user) { ctx = step(ctx, "(asset/transfer *address* [token " + BAL + "])"); assertEquals(BAL, RT.jvm(ctx.getResult())); assertEquals(BAL, evalL(ctx, "(asset/balance token *address*)")); - + // transfer nothing to self, should not affect balance ctx = step(ctx, "(asset/transfer *address* [token nil])"); assertEquals(0L, (long) RT.jvm(ctx.getResult())); @@ -160,6 +160,11 @@ public static void doFungibleTests(Context ctx, ACell token, Address user) { ctx = step(ctx, "(asset/offer *address* [token 0])"); assertCVMEquals(0, ctx.getResult()); assertCVMEquals(0, eval(ctx, "(asset/get-offer token *address* *address*)")); + + // negative transfers fail, even to self + assertArgumentError(step(ctx,"(asset/transfer *address* [token -1])")); + assertArgumentError(step(ctx,"(fungible/transfer token *address* -1)")); + // set a non-zero offer ctx = step(ctx, "(asset/offer *address* token 666)");