sell-quote | (fn ([of-token amount] (cond (#66/get-market %0) (let [market *result* ] (call* %2 0 sell-tokens-quote %1)))) ([of-token amount with-token] (cond (#66/get-market %2) (let [market *result* ] (cond (#66/sell-quote %0 %1) (let [cvx-amount *result* ] (call* %3 0 sell-cvx-quote %4))))))) | "Get a quote for selling an amount of a token, in CVM or optional swap token." |
asset | #65 | nil |
sell | (fn [of-token amount with-token] (let [cvx-amount (#66/sell-tokens %0 %1) ] (#66/sell-cvx %2 %3))) | "Sells a given amount of a fungible token in exchange for another token" |
fungible | #64 | nil |
price | (fn ([token] (cond (#66/get-market %0) (let [market *result* ] (call* %1 0 price)))) ([token currency] (let [market.token (cond (#66/get-market %0) *result* (return nil)) price.cvx (cond (call* %2 0 price) *result* (return nil)) market.currency (cond (#66/get-market %1) *result* (return nil)) price.currency (cond (call* %4 0 price) *result* (return nil)) ] (/ %3 %5)))) | "Gets the current price for a token, in CVM or an optional given currency. Returns nil if a market with liquidity does not exist." |
sell-cvx | (fn [token amount] (let [market (cond (#66/get-market %0) *result* (fail :LIQUIDITY (str "Torus: market does not exist for token: " %0))) ] (call* %2 %1 sell-cvx %1))) | "Sell an amount of CVM for a given token." |
buy-quote | (fn ([of-token amount] (cond (#66/get-market %0) (let [market *result* ] (call* %2 0 buy-tokens-quote %1)))) ([of-token amount with-token] (cond (#66/get-market %2) (let [market *result* ] (cond (#66/buy-quote %0 %1) (let [cvx-amount *result* ] (call* %3 0 buy-cvx-quote %4))))))) | "Gets a quote to buy an amount of a specifies token, in CVX or oprtional token to swap." |
sell-tokens | (fn [token amount] (let [market (cond (#66/get-market %0) *result* (fail :LIQUIDITY (str "Torus: market does not exist for token: " %0))) ] (#65/offer %2 (vector %0 %1)) (call* %2 0 sell-tokens %1))) | "Sell tokens at current CVM price" |
buy-tokens | (fn [token amount] (let [market (cond (#66/get-market %0) *result* (fail :LIQUIDITY (str "Torus: market does not exist for token: " %0))) ] (call* %2 *balance* buy-tokens %1))) | "Buy a specified amount of a token, using whatever CVX required." |
buy | (fn [of-token amount with-token] (let [market (cond (#66/get-market %0) *result* (fail :STATE (str "No Torus market for token: " %0))) cvx-amount (cond (call* %3 0 buy-tokens-quote %1) *result* (fail :LIQUIDITY "No liquidity available to buy token")) sold (#66/buy-cvx %2 %4) ] (#66/buy-tokens %0 %1) %5)) | "Buy an amount of a token using a given token (token swap)." |
trust | #10 | nil |
get-market | (fn [token] (get #66/markets %0)) | "Gets the canonical market for a token. Returns nil if the market does not exist." |
buy-cvx | (fn [token amount] (let [market (cond (#66/get-market %0) *result* (fail :LIQUIDITY (str "Torus: market does not exist for token: " %0))) ] (#65/offer %2 (vector %0 (#64/balance %0 *address*))) (call* %2 0 buy-cvx %1))) | "Buy CVM using a given token." |
add-liquidity | (fn ([token token-amount] (cond (#66/price %0) (let [p *result* ] (recur %0 %1 (inc (int (* %2 %1))))) (fail "No liquidity"))) ([token token-amount cvx-amount] (let [market (#66/create-market %0) ] (#65/offer %3 %0 %1) (call* %3 (int %2) add-liquidity %1)))) | "Adds an amount of a token to the liquidity pool, matched by an amount of CVX. " |
withdraw-liquidity | (fn [token shares] (let [market (cond (#66/get-market %0) *result* (fail :STATE "No market exists to withdraw liquidity")) ] (call* %2 0 withdraw-liquidity %1))) | "Withdraw liquidity for a token." |
markets | {#14297 #14298,#14291 #14292,#207 #208,#130 #131,#128 #129,#14293 #14294,#14289 #14290} | nil |
create-market | (fn [token] (do (cond (= *address* #66) nil (return (call* #66 0 create-market %0))) (cond (callable? %0) nil (fail :ASSERT "Assert failed: (callable? token)")) (let [existing-market (get #66/markets %0) ] (cond %1 *result* (let [market (deploy (#66/build-market %0 *address*)) ] (def markets (assoc #66/markets %0 %2)) %2))))) | "Gets or creates the canonical market for a token." |
build-market | (fn [token torus] (vector (#64/build-token (hash-map :supply 0)) (list do (import convex.asset :as asset) (import convex.core :as core) (import convex.fungible :as fungible) (list def token %0) (list def torus %1) (def token-balance 0) (set-controller torus) (defn -qc [q] (cond (int? q) q (nil? q) 0 (fail :ARGUMENT "Invalid token quantity"))) (defn add-liquidity ^{:callable true} [amount] (let [amount (-qc amount) price (price) initial-cvm-balance *balance* liquidity (sqrt (* (double initial-cvm-balance) token-balance)) has-liquidity (> liquidity 0) cvm ((lookup core accept) (if has-liquidity (long (* price amount)) *offer*)) _ ((lookup asset accept) *caller* token amount) new-token-balance (+ token-balance amount) new-liquidity (sqrt (* (double *balance*) new-token-balance)) delta (if has-liquidity (int (* (- new-liquidity liquidity) (/ supply liquidity))) (int new-liquidity))] (set-holding *caller* (+ delta (or (get-holding *caller*) 0))) (set! supply (+ supply delta)) (set! token-balance new-token-balance) delta)) (defn buy-cvx ^{:callable true} [amount] (let [amount (int amount) _ (cond (< amount 0) (fail :ARGUMENT "Cannot buy negative coin quantity")) required-tokens (or (buy-cvx-quote amount) (fail :LIQUIDITY "Pool cannot supply this amount of CVX"))] ((lookup asset accept) *caller* [token required-tokens]) (def token-balance (+ token-balance required-tokens)) ((lookup core transfer) *caller* amount) required-tokens)) (defn buy-cvx-quote ^{:callable true} [amount] (cond (< amount 0) (return nil) (>= amount *balance*) (return nil)) (let [cvx-balance *balance* pool (* (double token-balance) cvx-balance) rate (calc-rate)] (int (ceil (* (+ 1.0 rate) (- (/ pool (- cvx-balance amount)) token-balance)))))) (defn buy-tokens ^{:callable true} [amount] (let [amount (int amount) _ (cond (< amount 0) (fail :ARGUMENT "Cannot buy negative token quantity")) required-cvx (or (buy-tokens-quote amount) (fail :LIQUIDITY "Pool cannot supply this amount of tokens"))] ((lookup core accept) required-cvx) (def token-balance (- token-balance amount)) ((lookup fungible transfer) token *caller* amount) required-cvx)) (defn buy-tokens-quote ^{:callable true} [amount] (cond (< amount 0) (return nil) (>= amount token-balance) (return nil)) (let [cvx-balance *balance* pool (* (double token-balance) cvx-balance) rate (calc-rate)] (int (ceil (* (+ 1.0 rate) (- (/ pool (- token-balance amount)) cvx-balance)))))) (defn calc-rate [] 0.001) (defn price ^{:callable true} [] (let [tok token-balance cvm *balance*] (cond (and (> tok 0) (> cvm 0)) (/ cvm tok)))) (defn sell-cvx ^{:callable true} [amount] (let [amount (int amount) gained-tokens (or (sell-cvx-quote amount) (fail :ARGUMENT "Cannot sell negative coin amount"))] ((lookup core accept) amount) (def token-balance (- token-balance gained-tokens)) ((lookup asset transfer) *caller* [token gained-tokens]) gained-tokens)) (defn sell-cvx-quote ^{:callable true} [amount] (cond (< amount 0) (return nil)) (let [cvx-balance *balance* pool (* (double token-balance) cvx-balance) rate (calc-rate) new-cvx-balance (+ cvx-balance amount)] (int (/ (- token-balance (/ pool new-cvx-balance)) (+ 1.0 rate))))) (defn sell-tokens ^{:callable true} [amount] (let [amount (int amount) gained-cvx (or (sell-tokens-quote amount) (fail :ARGUMENT "Cannot sell this negative token amount"))] ((lookup asset accept) *caller* [token amount]) (def token-balance (+ token-balance amount)) ((lookup core transfer) *caller* gained-cvx) gained-cvx)) (defn sell-tokens-quote ^{:callable true} [amount] (cond (< amount 0) (return nil)) (let [cvx-balance *balance* pool (* (double token-balance) cvx-balance) rate (calc-rate) new-token-balance (+ token-balance amount)] (int (/ (- cvx-balance (/ pool new-token-balance)) (+ 1.0 rate))))) (defn withdraw-liquidity ^{:callable true} [shares] (let [shares (-qc shares) own-holding (or (get-holding *caller*) 0) _ (cond (> shares own-holding) (fail :FUNDS "Insufficient shares") (< shares 0) (fail :ARGUMENT "Negative withdrawal")) proportion (if (> supply 0) (/ (double shares) supply) 0.0) coin-refund (int (* proportion *balance*)) token-refund (int (* proportion token-balance))] (set! token-balance (- token-balance token-refund)) (set-holding *caller* (- own-holding shares)) (set! supply (- supply shares)) (transfer *caller* coin-refund) ((lookup fungible transfer) token *caller* token-refund) shares))))) | "Creates deployable code for a new Torus token market." |