Skip to content

Commit

Permalink
More Torus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 4, 2024
1 parent a5efcc6 commit ca49e7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
39 changes: 18 additions & 21 deletions convex-core/src/main/cvx/convex/torus/exchange.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@
own-holding (or (get-holding *caller*) 0)

;; Check withrawal amount is valid
_ (assert (<= 0 shares own-holding))
_ (or (<= 0 shares own-holding) (fail :FUNDS "Insufficient shares"))

proportion (if (> supply
0)
Expand All @@ -311,24 +311,21 @@
;; SECURITY:
;; 1. Update balances then transfer coins first. Risk of re-entrancy attack if transfers are made while
;; this actor is in an inconsistent state so we MUST do accounting first.
(def token-balance
(set! token-balance
(- token-balance
token-refund))
(set-holding *caller*
(- own-holding
shares))
(def supply
(- supply
shares))
;; 2. Transfer back coins. Be aware caller might do *anything* in transfer callbacks!
(transfer *caller*
coin-refund)
;; 3. Finally transfer asset. We've accounted this already, so safe
;; TODO. Decide which of these is best
;;(asset/transfer *caller* [token token-refund] :withdraw)
(fungible/transfer token
*caller*
token-refund)

(set-holding *caller* (- own-holding shares))

(set! supply (- supply shares))

;; 2. Transfer back coins. Be aware caller might do *anything* in transfer callbacks!
(transfer *caller* coin-refund)

;; 3. Finally transfer asset. Done accounting for this already, so safe
;; TODO. Decide which of these is best
;;(asset/transfer *caller* [token token-refund] :withdraw)
(fungible/transfer token *caller* token-refund)
shares)))])

(defn create-market
Expand Down Expand Up @@ -483,7 +480,7 @@
(sell-cvx with-token cvx-amount)))

(defn sell-cvx
^{:doc {:description "Sell an amount of CVX for a given token."
^{:doc {:description "Sell an amount of CVM for a given token."
:signature [{:params [token amount]}]}}
[token amount]
(let [market (or (get-market token)
Expand All @@ -494,7 +491,7 @@
(sell-cvx amount))))

(defn sell-quote
^{:doc {:description "Get a quote for selling an amount of a token, in CVX or optional swap token."
^{:doc {:description "Get a quote for selling an amount of a token, in CVM or optional swap token."
:signature [{:params [of-token amount]}
{:params [of-token amount with-token]}]}}
([of-token amount]
Expand All @@ -510,7 +507,7 @@
(sell-cvx-quote cvx-amount))))))

(defn sell-tokens
^{:doc {:description "Sell an amount of tokens at current CVX price"
^{:doc {:description "Sell tokens at current CVX price"
:signature [{:params [token amount]}]}}
[token amount]
(let [market (or (get-market token)
Expand All @@ -521,7 +518,7 @@
(call market (sell-tokens amount))))

(defn withdraw-liquidity
^{:doc {:description "Withdraw an amount of liquidity for a token."
^{:doc {:description "Withdraw liquidity for a token."
:signature [{:params [token shares]}]}}
[token shares]
(let [market (or (get-market token)
Expand Down
19 changes: 19 additions & 0 deletions convex-core/src/test/java/convex/actors/TorusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ public class TorusTest extends ACVMTest {
assertEquals(E_SHARES,eval(ctx,"(asset/balance BM *address*)"));
}

@Test public void testBadWithdraw() {
Context ctx=context();
ctx= exec(ctx,"(def TOK (deploy (@convex.fungible/build-token {:supply 10000})))");
ctx= exec(ctx,"(def TM (torus/get-market TOK))");

CVMLong E_SHARES=CVMLong.create(10000);
ctx= exec(ctx,"(torus/add-liquidity TOK 5000 20000)");
assertEquals(E_SHARES,ctx.getResult());
assertEquals(RT.cvm(4.0),eval(ctx,"(torus/price TOK)"));

assertFundsError(step(ctx,"(torus/withdraw-liquidity TOK 10001)"));
}

@Test public void testLiquidityZeroCVM() {
// Bug fix for #517, thanks Ash!
Context ctx=context();
Expand All @@ -235,6 +248,12 @@ public class TorusTest extends ACVMTest {
assertEquals(CVMDouble.ONE,eval(ctx,"(torus/price BROK)"));
assertEquals(E_SHARES,eval(ctx,"(asset/balance BM *address*)"));

// check withdrawing all shares
ctx= exec(ctx,"(torus/withdraw-liquidity BROK "+E_SHARES+")");
assertNull(eval(ctx,"(torus/price BROK)"));
assertEquals(CVMLong.ZERO,eval(ctx,"(asset/balance BM *address*)"));
assertEquals(CVMLong.ZERO,eval(ctx,"(asset/balance BROK BM)"));
assertEquals(CVMLong.ZERO,eval(ctx,"(balance BM)"));
}

@Test public void testTorusAPI() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public static void doMutationTest(Blob b) {
/* also OK */
} catch (MissingDataException e) {
/* also OK */
} catch (Exception e) {
System.err.println("Fuzz test bad blob: "+b);
throw e;
}
}
}

0 comments on commit ca49e7c

Please sign in to comment.