Skip to content

Commit

Permalink
Tighten fungible token tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 21, 2024
1 parent 565c30e commit bdfbaa5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
10 changes: 4 additions & 6 deletions convex-core/src/main/cvx/convex/asset/fungible.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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)))

Expand Down
39 changes: 18 additions & 21 deletions convex-core/src/main/cvx/convex/asset/multi-token.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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)))

8 changes: 4 additions & 4 deletions convex-core/src/main/cvx/convex/asset/share.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 4 additions & 3 deletions convex-core/src/main/cvx/convex/asset/wrap/convex.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion convex-core/src/test/java/convex/lib/AssetTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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)");
Expand Down

0 comments on commit bdfbaa5

Please sign in to comment.