Skip to content

Commit

Permalink
Assert :FUNDS error if sender balance is insufficient for token offer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 8, 2023
1 parent b6a1f50 commit de59235
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion convex-core/src/main/cvx/asset/share.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
_ (cond (< off quantity) (fail :STATE "insufficient offer"))
bal (nth rec 0)
nbal (- bal quantity)
_ (cond (< nbal 0) (fail "insufficent balance to accept"))
_ (cond (< nbal 0) (fail :FUNDS "insufficent balance to accept"))
noff (- off quantity)
nos (cond (<= noff 0) (dissoc os receiver) (assoc os receiver noff))
nrec [nbal nos]]
Expand Down
4 changes: 3 additions & 1 deletion convex-core/src/main/cvx/convex/asset.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"If asset contains multiple assets, accepts each in turn. MUST fail if the asset cannot be accepted."]
:examples [{:code "(accept sender [fungible-token-address 1000])"}]
:signature [{:params [sender asset]}
{:params [sender asset-path quantity]}]}}
{:params [sender asset-path quantity]}]
:errors {:STATE "If there is no sufficient offer to accept."
:FUNDS "If sender has insufficient balance."}}}

([sender asset]
(cond
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/cvx/convex/fungible.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

(< sendbal
quantity)
(fail "Sender token balance is insufficient")
(fail :FUNDS "Sender token balance is insufficient")

(let [new-offer (- offer
quantity)]
Expand Down
9 changes: 7 additions & 2 deletions convex-core/src/test/java/convex/lib/AssetTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ public static void doFungibleTests (Context ctx, ACell token, Address user) {
assertCVMEquals(BAL,ctxx.getResult());
assertEquals(BAL,evalL(ctxx,"(asset/get-offer token *address* *address*)"));

// accepting one more token than offered at this point should be error
// accepting one more token than offered at this point should be :STATE error
assertError(step(ctxx,"(asset/accept *address* [token (inc BAL)])"));

ctxx=step(ctxx,"(asset/accept *address* token BAL)");
assertCVMEquals(BAL,ctxx.getResult());
assertCVMEquals(BAL,evalL(ctxx,"(asset/balance token)"));

// accepting one more token at this point should be error
// accepting one more token at this point should be :STATE error
assertStateError(step(ctxx,"(asset/accept *address* [token 1])"));

// Offer / accept of half balance should work
Expand All @@ -97,6 +97,11 @@ public static void doFungibleTests (Context ctx, ACell token, Address user) {
ctxx=step(ctxx,"(asset/accept *address* token (div BAL 2))");
assertCVMEquals(BAL/2,ctxx.getResult());
assertCVMEquals(BAL,evalL(ctxx,"(asset/balance token)"));

// Offer / accept of more than balance should fail with :FUNDS
ctxx = step (ctxx,"(asset/offer *address* token (inc BAL))");
assertCVMEquals(BAL+1,ctxx.getResult());
assertFundsError(step(ctxx,"(asset/accept *address* [token (inc BAL)])"));

// Set back to zero offer
ctxx = step(ctxx,"(asset/offer *address* [token 0])");
Expand Down

0 comments on commit de59235

Please sign in to comment.