From 6723bad5cd83922ac3e389c0b970fedbf29be9ab Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 4 Oct 2021 19:58:56 +0800 Subject: [PATCH 01/16] max slippage guard added (#262) --- clarity/contracts/alex-vault.clar | 1 - .../equations/weighted-equation.clar | 2 - ...lash-loan-user-margin-usda-wbtc-23040.clar | 4 +- ...lash-loan-user-margin-usda-wbtc-23670.clar | 4 +- ...lash-loan-user-margin-usda-wbtc-34560.clar | 4 +- ...flash-loan-user-margin-usda-wbtc-5760.clar | 4 +- ...lash-loan-user-margin-usda-wbtc-59760.clar | 4 +- ...lash-loan-user-margin-usda-wbtc-74880.clar | 4 +- ...lash-loan-user-margin-wbtc-usda-23040.clar | 4 +- ...lash-loan-user-margin-wbtc-usda-34560.clar | 4 +- ...flash-loan-user-margin-wbtc-usda-5760.clar | 4 +- ...lash-loan-user-margin-wbtc-usda-74880.clar | 4 +- clarity/contracts/lib/math-fixed-point.clar | 2 - .../new-lib/math-new-fixed-point.clar | 2 - clarity/contracts/pool/alex-reserve-pool.clar | 2 - .../pool/collateral-rebalancing-pool.clar | 27 ++++----- clarity/contracts/pool/fixed-weight-pool.clar | 19 ++++--- .../pool/liquidity-bootstrapping-pool.clar | 27 ++++----- clarity/contracts/pool/yield-token-pool.clar | 42 ++++---------- .../models/alex-tests-fixed-weight-pool.ts | 10 ++-- .../models/alex-tests-yield-token-pool.ts | 10 ++-- init-js-tool/deploy.js | 8 +-- init-js-tool/index.js | 55 ++++++++++--------- 23 files changed, 111 insertions(+), 136 deletions(-) diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index a0f8406e..c6e2661e 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -45,7 +45,6 @@ (ok (asserts! (default-to false (map-get? approved-contracts sender)) ERR-NOT-AUTHORIZED)) ) -;; TODO: multisig (define-public (set-flash-loan-fee-rate (fee uint)) (begin (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) diff --git a/clarity/contracts/equations/weighted-equation.clar b/clarity/contracts/equations/weighted-equation.clar index 18a46ee2..bb2316f6 100644 --- a/clarity/contracts/equations/weighted-equation.clar +++ b/clarity/contracts/equations/weighted-equation.clar @@ -191,8 +191,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-v2-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; TODO: overflow causes runtime error, should handle before operation rather than after - ;; constants ;; (define-constant SCALE_UP_OVERFLOW (err u5001)) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar index 9bf6a32d..81db31d1 100644 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-23040", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.clar index 3cc1d55e..8165a282 100644 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.clar +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-59760", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar index 9825072e..457b8c09 100644 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-34560", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar index db88ea80..350497ab 100644 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-5760", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.clar index 3cc1d55e..8165a282 100644 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.clar +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-59760", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-74880.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-74880.clar index 9b2e4f2b..bd10e3a3 100644 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-74880.clar +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-74880.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-74880", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar index f918c6b4..5a4a942e 100644 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-23040", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar index ecf42b98..9a417f7a 100644 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-34560", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar index 38597d96..58491376 100644 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-5760", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-74880.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-74880.clar index ab576887..654ed824 100644 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-74880.clar +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-74880.clar @@ -19,8 +19,8 @@ ) ;; swap token to collateral so we can return flash-loan (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) ) (print { object: "flash-loan-user-margin-usda-wbtc-74880", action: "execute", data: gross-amount }) diff --git a/clarity/contracts/lib/math-fixed-point.clar b/clarity/contracts/lib/math-fixed-point.clar index 4ee1c601..a036c159 100644 --- a/clarity/contracts/lib/math-fixed-point.clar +++ b/clarity/contracts/lib/math-fixed-point.clar @@ -3,8 +3,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-v2-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; TODO: overflow causes runtime error, should handle before operation rather than after - ;; constants ;; (define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places diff --git a/clarity/contracts/new-lib/math-new-fixed-point.clar b/clarity/contracts/new-lib/math-new-fixed-point.clar index 9114c95f..ccf97a0a 100644 --- a/clarity/contracts/new-lib/math-new-fixed-point.clar +++ b/clarity/contracts/new-lib/math-new-fixed-point.clar @@ -3,8 +3,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-v2-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; TODO: overflow causes runtime error, should handle before operation rather than after - ;; constants ;; (define-constant ONE_10 (pow u10 u10)) ;; 10 decimal places diff --git a/clarity/contracts/pool/alex-reserve-pool.clar b/clarity/contracts/pool/alex-reserve-pool.clar index 839089a1..1438ee10 100644 --- a/clarity/contracts/pool/alex-reserve-pool.clar +++ b/clarity/contracts/pool/alex-reserve-pool.clar @@ -108,8 +108,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-v2-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; TODO: overflow causes runtime error, should handle before operation rather than after - ;; constants ;; (define-constant SCALE_UP_OVERFLOW (err u5001)) diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index 48be796a..152e026a 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -39,7 +39,6 @@ (define-constant a3 u97200) (define-constant a4 u7810800) -;; TODO: need to be defined properly (define-data-var contract-owner principal tx-sender) (define-data-var oracle-src (string-ascii 32) "coingecko") ;; data maps and vars @@ -239,11 +238,9 @@ (spot (unwrap! (get-spot token collateral expiry) ERR-GET-ORACLE-PRICE-FAIL)) (now (* block-height ONE_8)) - ;; TODO: assume 15secs per block + ;; assume 15secs per block (t (unwrap! (div-down (unwrap! (sub-fixed expiry now) ERR-MATH-CALL) (* u2102400 ONE_8)) ERR-MATH-CALL)) - ;; TODO: APYs need to be calculated from the prevailing yield token price. - ;; TODO: ln(S/K) approximated as (S/K - 1) ;; we calculate d1 first (spot-term (unwrap! (div-up spot strike) ERR-MATH-CALL)) @@ -367,7 +364,7 @@ ( (minted-yield-token (get yield-token (try! (add-to-position token collateral the-yield-token the-key-token dx)))) ) - (contract-call? .yield-token-pool swap-y-for-x the-yield-token token minted-yield-token) + (contract-call? .yield-token-pool swap-y-for-x the-yield-token token minted-yield-token none) ) ) @@ -405,8 +402,8 @@ (dy-weighted (if (is-eq token-x token-y) dx-to-dy (if (is-some (contract-call? .fixed-weight-pool get-pool-exists token collateral u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x token collateral u50000000 u50000000 dx-to-dy))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y collateral token u50000000 u50000000 dx-to-dy))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x token collateral u50000000 u50000000 dx-to-dy none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y collateral token u50000000 u50000000 dx-to-dy none))) ) ) ) @@ -459,8 +456,8 @@ (begin (as-contract (try! (contract-call? .alex-vault transfer-ft collateral balance-x tx-sender tx-sender))) (if (is-some (contract-call? .fixed-weight-pool get-pool-exists collateral token u50000000 u50000000)) - (get dy (as-contract (try! (contract-call? .fixed-weight-pool swap-x-for-y collateral token u50000000 u50000000 balance-x)))) - (get dx (as-contract (try! (contract-call? .fixed-weight-pool swap-y-for-x token collateral u50000000 u50000000 balance-x)))) + (get dy (as-contract (try! (contract-call? .fixed-weight-pool swap-x-for-y collateral token u50000000 u50000000 balance-x none)))) + (get dx (as-contract (try! (contract-call? .fixed-weight-pool swap-y-for-x token collateral u50000000 u50000000 balance-x none)))) ) ) ) @@ -510,8 +507,8 @@ (as-contract (unwrap! (contract-call? token transfer (if (is-eq token-y .token-usda) amount-to-swap (if (is-some (contract-call? .fixed-weight-pool get-pool-exists token .token-usda u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x token .token-usda u50000000 u50000000 amount-to-swap))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda token u50000000 u50000000 amount-to-swap))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x token .token-usda u50000000 u50000000 amount-to-swap none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda token u50000000 u50000000 amount-to-swap none))) ) ) tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) ) @@ -781,8 +778,8 @@ (if (is-eq token-x .token-usda) fee-x (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda collateral u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda collateral u50000000 u50000000 fee-x))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y collateral .token-usda u50000000 u50000000 fee-x))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda collateral u50000000 u50000000 fee-x none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y collateral .token-usda u50000000 u50000000 fee-x none))) ) ) ) @@ -800,8 +797,8 @@ (if (is-eq token-y .token-usda) fee-y (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda token u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token u50000000 u50000000 fee-y))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token .token-usda u50000000 u50000000 fee-y))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token u50000000 u50000000 fee-y none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token .token-usda u50000000 u50000000 fee-y none))) ) ) ) diff --git a/clarity/contracts/pool/fixed-weight-pool.clar b/clarity/contracts/pool/fixed-weight-pool.clar index 4c6dcd90..8c5cdf3f 100644 --- a/clarity/contracts/pool/fixed-weight-pool.clar +++ b/clarity/contracts/pool/fixed-weight-pool.clar @@ -24,6 +24,7 @@ (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) (define-constant ERR-MATH-CALL (err u2010)) (define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) +(define-constant ERR-EXCEEDS-MAX-SLIPPAGE (err u2020)) (define-constant alex-symbol "alex") (define-constant reserve-usdc-symbol "usdc") @@ -236,11 +237,9 @@ ) ) -(define-public (swap-x-for-y (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (dx uint)) +(define-public (swap-x-for-y (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (dx uint) (min-dy (optional uint))) (begin - (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) + (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (let ( (token-x (contract-of token-x-trait)) @@ -266,6 +265,8 @@ ) ) ) + + (asserts! (< (default-to u0 min-dy) dy) ERR-EXCEEDS-MAX-SLIPPAGE) (unwrap! (contract-call? token-x-trait transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) (try! (contract-call? .alex-vault transfer-ft token-y-trait dy (as-contract tx-sender) tx-sender)) @@ -278,10 +279,8 @@ ) ) -(define-public (swap-y-for-x (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (dy uint)) +(define-public (swap-y-for-x (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (dy uint) (min-dx (optional uint))) (begin - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) (let ( @@ -309,6 +308,8 @@ ) ) ) + + (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) (try! (contract-call? .alex-vault transfer-ft token-x-trait dx (as-contract tx-sender) tx-sender)) (unwrap! (contract-call? token-y-trait transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) @@ -427,7 +428,7 @@ (contract-call? .alex-reserve-pool transfer-to-mint (if (is-eq token-x .token-usda) fee-x - (get dx (try! (swap-y-for-x .token-usda token-x-trait u50000000 u50000000 fee-x))) + (get dx (try! (swap-y-for-x .token-usda token-x-trait u50000000 u50000000 fee-x none))) ) ) ) @@ -443,7 +444,7 @@ (contract-call? .alex-reserve-pool transfer-to-mint (if (is-eq token-y .token-usda) fee-y - (get dx (try! (swap-y-for-x .token-usda token-y-trait u50000000 u50000000 fee-y))) + (get dx (try! (swap-y-for-x .token-usda token-y-trait u50000000 u50000000 fee-y none))) ) ) ) diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index 73d0e414..41d108f5 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -23,7 +23,7 @@ (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) (define-constant ERR-MATH-CALL (err u2010)) (define-constant internal-get-weight-err (err u2012)) - +(define-constant ERR-EXCEEDS-MAX-SLIPPAGE (err u2020)) ;; data maps and vars ;; @@ -247,12 +247,10 @@ ) ) -(define-public (swap-x-for-y (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint)) +(define-public (swap-x-for-y (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint) (min-dy (optional uint))) (begin ;; swap is allowed only until expiry (asserts! (< (* block-height ONE_8) expiry) already-ERR-EXPIRY) - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) (let ( (token-x (contract-of token-x-trait)) @@ -282,6 +280,9 @@ ) ) ) + + (asserts! (< (default-to u0 min-dy) dy) ERR-EXCEEDS-MAX-SLIPPAGE) + (unwrap! (contract-call? token-x-trait transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) (try! (contract-call? .alex-vault transfer-ft token-y-trait dy (as-contract tx-sender) tx-sender)) ;; post setting @@ -292,13 +293,10 @@ ) ) -(define-public (swap-y-for-x (token-x-trait ) (token-y-trait ) (expiry uint) (dy uint)) +(define-public (swap-y-for-x (token-x-trait ) (token-y-trait ) (expiry uint) (dy uint) (min-dx (optional uint))) (begin ;; swap is allowed only until expiry - (asserts! (< (* block-height ONE_8) expiry) already-ERR-EXPIRY) - - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) + (asserts! (< (* block-height ONE_8) expiry) already-ERR-EXPIRY) (let ( (token-x (contract-of token-x-trait)) @@ -328,6 +326,9 @@ ) ) ) + + (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) + (try! (contract-call? .alex-vault transfer-ft token-x-trait dx (as-contract tx-sender) tx-sender)) (unwrap! (contract-call? token-y-trait transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) ;; post setting @@ -444,8 +445,8 @@ (if (is-eq token-x .token-usda) fee-x (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda token-x-trait u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token-x-trait u50000000 u50000000 fee-x))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token-x-trait .token-usda u50000000 u50000000 fee-x))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token-x-trait u50000000 u50000000 fee-x none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token-x-trait .token-usda u50000000 u50000000 fee-x none))) ) ) ) @@ -464,8 +465,8 @@ (if (is-eq token-y .token-usda) fee-y (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda token-y-trait u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token-y-trait u50000000 u50000000 fee-y))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token-y-trait .token-usda u50000000 u50000000 fee-y))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token-y-trait u50000000 u50000000 fee-y none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token-y-trait .token-usda u50000000 u50000000 fee-y none))) ) ) ) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 03094753..a0fbdc23 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -26,6 +26,7 @@ (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) (define-constant ERR-GET-SYMBOL-FAIL (err u6000)) +(define-constant ERR-EXCEEDS-MAX-SLIPPAGE (err u2020)) ;; TODO: need to be defined properly (define-data-var contract-owner principal tx-sender) @@ -290,7 +291,7 @@ ) ) -(define-public (swap-x-for-y (the-aytoken ) (the-token ) (dx uint)) +(define-public (swap-x-for-y (the-aytoken ) (the-token ) (dx uint) (min-dy (optional uint))) (begin (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (let @@ -322,8 +323,9 @@ ) ) ) - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) + + (asserts! (< (default-to u0 min-dy) dy) ERR-EXCEEDS-MAX-SLIPPAGE) + (and (> dx u0) (unwrap! (contract-call? the-token transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED)) (and (> dy u0) (try! (contract-call? .alex-vault transfer-yield the-aytoken dy (as-contract tx-sender) tx-sender))) @@ -335,7 +337,7 @@ ) ) -(define-public (swap-y-for-x (the-aytoken ) (the-token ) (dy uint)) +(define-public (swap-y-for-x (the-aytoken ) (the-token ) (dy uint) (min-dx (optional uint))) (begin (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) (let @@ -365,8 +367,7 @@ ) ) ) - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) + (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx (as-contract tx-sender) tx-sender))) (and (> dy u0) (unwrap! (contract-call? the-aytoken transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) @@ -473,8 +474,8 @@ (if (is-eq aytoken .token-usda) fee-x (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda the-aytoken u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda the-aytoken u50000000 u50000000 fee-x))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y the-aytoken .token-usda u50000000 u50000000 fee-x))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda the-aytoken u50000000 u50000000 fee-x none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y the-aytoken .token-usda u50000000 u50000000 fee-x none))) ) ) ) @@ -492,8 +493,8 @@ (if (is-eq token .token-usda) fee-y (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda the-token u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda the-token u50000000 u50000000 fee-y))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y the-token .token-usda u50000000 u50000000 fee-y))) + (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda the-token u50000000 u50000000 fee-y none))) + (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y the-token .token-usda u50000000 u50000000 fee-y none))) ) ) ) @@ -1053,24 +1054,3 @@ ) ) ) - -(define-read-only (test) - (let - ( - (x (* u7 (pow u10 u6))) - (y (* u233 (pow u10 u6))) - (x-int (to-int x)) - (y-int (to-int y)) - (lnx (unwrap-panic (ln-priv x-int))) - (logx-times-y (/ (* lnx y-int) iONE_8)) - ;;(r (exp-pos (* -1 logx-times-y))) - - ;;(arg (* 69 iONE_8)) - ;;(r (exp-pos arg)) - ;;(x_product (fold accumulate_product x_a_list {x: arg, product: iONE_8})) - ) - ;;(ok logx-times-y) - ;;x_product - (ok (pow-fixed x y)) - ) -) diff --git a/clarity/tests/models/alex-tests-fixed-weight-pool.ts b/clarity/tests/models/alex-tests-fixed-weight-pool.ts index bb459174..435f3433 100644 --- a/clarity/tests/models/alex-tests-fixed-weight-pool.ts +++ b/clarity/tests/models/alex-tests-fixed-weight-pool.ts @@ -69,27 +69,29 @@ import { return block.receipts[0].result; } - swapXForY(user: Account, tokenX: string, tokenY: string, weightX: number, weightY: number, dx: number) { + swapXForY(user: Account, tokenX: string, tokenY: string, weightX: number, weightY: number, dx: number, dy_min: number) { let block = this.chain.mineBlock([ Tx.contractCall("fixed-weight-pool", "swap-x-for-y", [ types.principal(tokenX), types.principal(tokenY), types.uint(weightX), types.uint(weightY), - types.uint(dx) + types.uint(dx), + types.some(types.uint(dy_min)) ], user.address), ]); return block.receipts[0].result; } - swapYForX(user: Account, tokenX: string, tokenY: string, weightX: number, weightY: number, dy: number) { + swapYForX(user: Account, tokenX: string, tokenY: string, weightX: number, weightY: number, dy: number, dx_min: number) { let block = this.chain.mineBlock([ Tx.contractCall("fixed-weight-pool", "swap-y-for-x", [ types.principal(tokenX), types.principal(tokenY), types.uint(weightX), types.uint(weightY), - types.uint(dy) + types.uint(dy), + types.some(types.uint(dx_min)) ], user.address), ]); return block.receipts[0].result; diff --git a/clarity/tests/models/alex-tests-yield-token-pool.ts b/clarity/tests/models/alex-tests-yield-token-pool.ts index c1d33ff2..7b5fcb1e 100644 --- a/clarity/tests/models/alex-tests-yield-token-pool.ts +++ b/clarity/tests/models/alex-tests-yield-token-pool.ts @@ -78,23 +78,25 @@ import { return block.receipts[0].result; } - swapXForY(user: Account, aytoken: string, token: string, dX: number) { + swapXForY(user: Account, aytoken: string, token: string, dX: number, dy_min: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "swap-x-for-y", [ types.principal(aytoken), types.principal(token), - types.uint(dX) + types.uint(dX), + types.some(types.uint(dy_min)) ], user.address), ]); return block.receipts[0].result; } - swapYForX(user: Account, aytoken: string, token: string, dY: number) { + swapYForX(user: Account, aytoken: string, token: string, dY: number, dx_min: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "swap-y-for-x", [ types.principal(aytoken), types.principal(token), - types.uint(dY) + types.uint(dY), + types.some(types.uint(dx_min)) ], user.address), ]); return block.receipts[0].result; diff --git a/init-js-tool/deploy.js b/init-js-tool/deploy.js index 66b24d3d..eb9a1b13 100644 --- a/init-js-tool/deploy.js +++ b/init-js-tool/deploy.js @@ -86,10 +86,10 @@ let contract_paths = [ "flash-loan-user-margin-usda-wbtc-5760.clar", "flash-loan-user-margin-wbtc-usda-5760.clar", - "key-token/key-usda-23040-yield-usda-74880.clar", - "multisig/multisig-crp-usda-23040-yield-usda-74880.clar", - "pool-token/fwp-usda-23040-usda-74880-50-50.clar", - "multisig/multisig-fwp-usda-23040-usda-74880-50-50.clar", + // "key-token/key-usda-23040-yield-usda-74880.clar", + // "multisig/multisig-crp-usda-23040-yield-usda-74880.clar", + // "pool-token/fwp-usda-23040-usda-74880-50-50.clar", + // "multisig/multisig-fwp-usda-23040-usda-74880-50-50.clar", // "yield-token/yield-wbtc-59760.clar", // "key-token/key-wbtc-59760-usda.clar", diff --git a/init-js-tool/index.js b/init-js-tool/index.js index 59986be4..bb776acc 100644 --- a/init-js-tool/index.js +++ b/init-js-tool/index.js @@ -411,7 +411,7 @@ async function arbitrage_ytp(){ let result = await ytpSwapXforY(_list[key]['yield_token'], _list[key]['token'], dx.value.value); if (!result) { dx_i = dx.value.value; - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 3; i++) { dx_i /= 2n; await ytpSwapXforY(_list[key]['yield_token'], _list[key]['token'], dx_i); } @@ -424,7 +424,7 @@ async function arbitrage_ytp(){ let result = await ytpSwapYforX(_list[key]['yield_token'], _list[key]['token'], dy.value.value); if (!result) { dy_i = dy.value.value; - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 3; i++) { dy_i /= 2n; await ytpSwapYforX(_list[key]['yield_token'], _list[key]['token'], dy_i); } @@ -497,12 +497,12 @@ async function get_pool_details_crp(){ _list = { 1: { token: 'token-wbtc', collateral: 'token-usda', expiry: 5760e+8 }, 2: { token: 'token-usda', collateral: 'token-wbtc', expiry: 5760e+8 }, - // 3: { token: 'token-wbtc', collateral: 'token-usda', expiry: 23040e+8 }, - // 4: { token: 'token-usda', collateral: 'token-wbtc', expiry: 23040e+8 }, - // 5: { token: 'token-wbtc', collateral: 'token-usda', expiry: 34560e+8 }, - // 6: { token: 'token-usda', collateral: 'token-wbtc', expiry: 34560e+8 }, - // 7: { token: 'token-wbtc', collateral: 'token-usda', expiry: 74880e+8 }, - // 8: { token: 'token-usda', collateral: 'token-wbtc', expiry: 74880e+8 }, + 3: { token: 'token-wbtc', collateral: 'token-usda', expiry: 23040e+8 }, + 4: { token: 'token-usda', collateral: 'token-wbtc', expiry: 23040e+8 }, + 5: { token: 'token-wbtc', collateral: 'token-usda', expiry: 34560e+8 }, + 6: { token: 'token-usda', collateral: 'token-wbtc', expiry: 34560e+8 }, + 7: { token: 'token-wbtc', collateral: 'token-usda', expiry: 74880e+8 }, + 8: { token: 'token-usda', collateral: 'token-wbtc', expiry: 74880e+8 }, } for (const key in _list){ @@ -518,6 +518,7 @@ async function get_pool_details_crp(){ } async function get_pool_details_fwp(){ + printResult(await fwpGetPoolDetails('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8)); } @@ -562,20 +563,20 @@ async function reduce_position_ytp(percent){ } _white_list = { - Hadan: 'STBAY5N5TRTEHHXRP4MH5H3W5FK2EXJDJWDYFA02', - Chiente: 'ST3N9GSEWX710RE5PSD110APZGKSD1EFMBEC7PFWK', - Marvin: 'SP1YMQJR0T1P52RT1VVPZZYZEQXQ5HBE6VWR36HFE', - James: 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', - Jing: 'ST2Q086N22CPRA5RK306CT5T0QFG6GNMJQBY4HXZC', - Chan: 'ST3BQ65DRM8DMTYDD5HWMN60EYC0JFS5NC262MM33', - Sidney: 'ST14YKBTNC0V2QXS3DSGFVCBJHQ7RM396511TJBTJ', - Liming: 'ST27WEWFJ3R3A8P20SRZHYJT1RP7GQRSB999RBN31', + // Hadan: 'STBAY5N5TRTEHHXRP4MH5H3W5FK2EXJDJWDYFA02', + // Chiente: 'ST3N9GSEWX710RE5PSD110APZGKSD1EFMBEC7PFWK', + // Marvin: 'SP1YMQJR0T1P52RT1VVPZZYZEQXQ5HBE6VWR36HFE', + // James: 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', + // Jing: 'ST2Q086N22CPRA5RK306CT5T0QFG6GNMJQBY4HXZC', + // Chan: 'ST3BQ65DRM8DMTYDD5HWMN60EYC0JFS5NC262MM33', + // Sidney: 'ST14YKBTNC0V2QXS3DSGFVCBJHQ7RM396511TJBTJ', + // Liming: 'ST27WEWFJ3R3A8P20SRZHYJT1RP7GQRSB999RBN31', Rachel: 'STY8YN3BJBF96FA3T916D5MFQQJ2GMKBNQW10NT5', - Tiger: 'ST17MVDJT37DGB5QRRS1H4HQ4MKVFKA3KAA4YGFH4', - Noise: 'ST290HKX9PWEQ7C3T3MFH3GZ4MXDP10F68K5GPSM2', - Oscar: 'ST19VTXARP3J5NFH1T69DCVJZ01CYYTWP0ME2VTX0', - Oscar2: 'ST2HRPEK5BC4C9CGNRT95Z85M9B9C3M99C6V7A6EZ', - Shawn: 'ST27TPYFGSGT3YGTEBTVMHZXD511AE6JGP15XVZDS' + // Tiger: 'ST17MVDJT37DGB5QRRS1H4HQ4MKVFKA3KAA4YGFH4', + // Noise: 'ST290HKX9PWEQ7C3T3MFH3GZ4MXDP10F68K5GPSM2', + // Oscar: 'ST19VTXARP3J5NFH1T69DCVJZ01CYYTWP0ME2VTX0', + // Oscar2: 'ST2HRPEK5BC4C9CGNRT95Z85M9B9C3M99C6V7A6EZ', + // Shawn: 'ST27TPYFGSGT3YGTEBTVMHZXD511AE6JGP15XVZDS' } async function run(){ @@ -595,14 +596,14 @@ async function run(){ // await test_spot_trading(); // await test_margin_trading(); // await get_pool_details_fwp(); - await get_pool_details_crp(); + // await get_pool_details_crp(); // await get_pool_details_ytp(); // await reduce_position_ytp(0.5e+8); // TODO: still doesn't work - // for(const key in _white_list){ - // await get_some_token(_white_list[key]); - // // await burn('token-wbtc', _white_list[key], 5); - // // await burn('token-usda', _white_list[key], 500000e+6); - // } + for(const key in _white_list){ + await get_some_token(_white_list[key]); + // await burn('token-wbtc', _white_list[key], 5); + // await burn('token-usda', _white_list[key], 500000e+6); + } // await mint_some_tokens('STB2BWB0K5XZGS3FXVTG3TKS46CQVV66NAK3YVN8'); } run(); \ No newline at end of file From 3f8270876d121e9a4c013c14df346a67c8d73c59 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 4 Oct 2021 21:32:54 +0800 Subject: [PATCH 02/16] tests fixed to support min-dx/min-dy --- .../pool/collateral-rebalancing-pool.clar | 13 ++--- .../tests/collateral-rebalancing-pool_test.ts | 40 +++++++------- clarity/tests/fixed-weight-pool_test.ts | 24 ++++----- clarity/tests/flash-loan_test.ts | 2 +- .../alex-tests-collateral-rebalancing-pool.ts | 10 ++-- clarity/tests/yield-token-pool_test.ts | 53 ++++++++----------- 6 files changed, 69 insertions(+), 73 deletions(-) diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index 152e026a..3e6ba347 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -32,6 +32,7 @@ (define-constant ERR-GET-BALANCE-FAIL (err u6001)) (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-LTV-GREATER-THAN-ONE (err u2019)) +(define-constant ERR-EXCEEDS-MAX-SLIPPAGE (err u2020)) (define-constant a1 u27839300) @@ -568,10 +569,8 @@ ) ;; split of balance to yield and key is transparent to traders -(define-public (swap-x-for-y (token ) (collateral ) (expiry uint) (dx uint)) +(define-public (swap-x-for-y (token ) (collateral ) (expiry uint) (dx uint) (min-dy (optional uint))) (begin - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (asserts! (<= (* block-height ONE_8) expiry) ERR-EXPIRY) @@ -610,6 +609,8 @@ ) ) + (asserts! (< (default-to u0 min-dy) dy) ERR-EXCEEDS-MAX-SLIPPAGE) + (unwrap! (contract-call? collateral transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) (try! (contract-call? .alex-vault transfer-ft token dy (as-contract tx-sender) tx-sender)) @@ -622,10 +623,8 @@ ) ;; split of balance to yield and key is transparent to traders -(define-public (swap-y-for-x (token ) (collateral ) (expiry uint) (dy uint)) +(define-public (swap-y-for-x (token ) (collateral ) (expiry uint) (dy uint) (min-dx (optional uint))) (begin - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) (asserts! (<= (* block-height ONE_8) expiry) ERR-EXPIRY) ;; swap is supported only if token /= collateral @@ -663,6 +662,8 @@ ) ) + (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) + (try! (contract-call? .alex-vault transfer-ft collateral dx (as-contract tx-sender) tx-sender)) (unwrap! (contract-call? token transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) diff --git a/clarity/tests/collateral-rebalancing-pool_test.ts b/clarity/tests/collateral-rebalancing-pool_test.ts index ce4fe855..693fc19e 100644 --- a/clarity/tests/collateral-rebalancing-pool_test.ts +++ b/clarity/tests/collateral-rebalancing-pool_test.ts @@ -102,13 +102,13 @@ Clarinet.test({ position['moving-average'].expectUint(moving_average); // arbtrageur selling 100 usda for wbtc - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); position['dy'].expectUint(199764); // arbtrageur selling 0.002 wbtc for usda - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.002 * ONE_8); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.002 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(22007078006); position['dy'].expectUint(0.002 * ONE_8); @@ -151,7 +151,7 @@ Clarinet.test({ call.result.expectOk().expectUint(10056633); // arbtrageur selling 100 usda for wbtc - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); position['dy'].expectUint(116875); @@ -176,7 +176,7 @@ Clarinet.test({ call.result.expectOk().expectUint(92358839); // arbtrageur selling 100 usda for wbtc - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); position['dy'].expectUint(939812); @@ -208,7 +208,7 @@ Clarinet.test({ call.result.expectOk().expectUint(99900000); // arbtrageur selling 100 usda for wbtc - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); position['dy'].expectUint(8558); @@ -233,11 +233,11 @@ Clarinet.test({ // simulate to expiry + 1 chain.mineEmptyBlockUntil((expiry / ONE_8) + 1) // swap is no longer allowed - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8, 0); position = result.expectErr().expectUint(2017); // swap is no longer allowed - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8, 0); position = result.expectErr().expectUint(2017); call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); @@ -412,10 +412,10 @@ Clarinet.test({ // let's do some arb call = await FWPTest.getYgivenPrice(wbtcAddress, usdaAddress, weightX, weightY, Math.round(wbtcPrice * 1.25)); call.result.expectOk().expectUint(52654365137886); - result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 52654365137886) + result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 52654365137886, 0) call = await FWPTest.getYgivenPrice(wbtcAddress, usdaAddress, weightX, weightY, Math.round(wbtcPrice * 1.25 * 1.2)); call.result.expectOk().expectUint(47460820304103); - result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 47460820304103) + result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 47460820304103, 0) // simulate to expiry + 1 chain.mineEmptyBlockUntil((expiry / ONE_8) + 1) @@ -551,7 +551,7 @@ Clarinet.test({ call = await CRPTest.getXgivenPrice(wbtcAddress, usdaAddress, expiry, Math.round( ONE_8 / (wbtcPrice * 1.1 / ONE_8))); call.result.expectOk().expectUint(111379129197); - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 111379129197); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 111379129197, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(111379129197); position['dy'].expectUint(2127973); @@ -563,7 +563,7 @@ Clarinet.test({ call = await CRPTest.getYgivenPrice(wbtcAddress, usdaAddress, expiry, Math.round( ONE_8 / (wbtcPrice * 1.1 * 0.98/ ONE_8))); call.result.expectOk().expectUint(1223378); - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 1223378); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 1223378, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(256645012356); position['dy'].expectUint(1223378); @@ -587,7 +587,7 @@ Clarinet.test({ result.expectOk().expectBool(true); // sell some yield-token to create a positive yield - result = YTPTest.swapYForX(wallet_1, yieldwbtc59760Address, wbtcAddress, 5*ONE_8); + result = YTPTest.swapYForX(wallet_1, yieldwbtc59760Address, wbtcAddress, 5*ONE_8, 0); let position:any = result.expectOk().expectTuple(); let call = await YTPTest.getPrice(yieldwbtc59760Address); @@ -631,7 +631,7 @@ Clarinet.test({ // pegged CRP throws error if someone tries to swap call = await CRPTest.getXgivenPrice(wbtcAddress, wbtcAddress, expiry, Math.round( ONE_8 / (wbtcPrice * 1.1 / ONE_8))); call.result.expectOk().expectUint(9516775442); - result = CRPTest.swapXForY(wallet_1, wbtcAddress, wbtcAddress, expiry, 9516775442); + result = CRPTest.swapXForY(wallet_1, wbtcAddress, wbtcAddress, expiry, 9516775442, 0); position = result.expectErr().expectUint(2001); }, }); @@ -701,35 +701,35 @@ Clarinet.test({ position = result.expectErr().expectUint(4002) // arbtrageur attepmts to swap zero value - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 0); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 0, 0); position = result.expectErr().expectUint(2003) // arbtrageur attepmts to swap small value - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 0.1* ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 0.1* ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(0.1* ONE_8); position['dy'].expectUint(198); // arbtrageur attepmts to swap in full value - result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, ONE_8 * ONE_8); + result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, ONE_8 * ONE_8, 0); position = result.expectErr().expectUint(4001) // arbtrageur attepmts to swap zero value - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0, 0); position = result.expectErr().expectUint(2003) // arbtrageur attepmts to swap small value - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.000001 * ONE_8); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.000001 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(10545754); position['dy'].expectUint(0.000001 * ONE_8); // arbtrageur attepmts to swap in full value - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, ONE_8 * ONE_8); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, ONE_8 * ONE_8, 0); position = result.expectErr().expectUint(4002) // arbtrageur selling 0.01 wbtc for usda - result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.01 * ONE_8); + result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.01 * ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(111855897259); position['dy'].expectUint(0.01 * ONE_8); diff --git a/clarity/tests/fixed-weight-pool_test.ts b/clarity/tests/fixed-weight-pool_test.ts index 685fe930..09e88750 100644 --- a/clarity/tests/fixed-weight-pool_test.ts +++ b/clarity/tests/fixed-weight-pool_test.ts @@ -81,25 +81,25 @@ Clarinet.test({ position['dy'].expectUint(499999999999878); // attempt to trade too much (> 90%) will be rejected - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 91*ONE_8); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 91*ONE_8, 0); position = result.expectErr().expectUint(4001); // swap some wbtc into usda - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(ONE_8); position['dy'].expectUint(4950462120393); // swap some usda into wbtc - result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, wbtcPrice*ONE_8); + result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, wbtcPrice*ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(103049813); position['dy'].expectUint(wbtcPrice*ONE_8); // attempt to swap zero throws an error - result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); + result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0, 0); result.expectErr().expectUint(2003); - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0, 0); result.expectErr().expectUint(2003); }, }); @@ -168,13 +168,13 @@ Clarinet.test({ result.expectOk().expectUint(5000000) // Swapping - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(95000000); // 5% Fee Charged on ONE_8 position['dy'].expectUint(4714125000000); // Corresponding dy value // Swapping - result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8*wbtcPrice); + result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8*wbtcPrice, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(97192466); // Corresponding dx value position['dy'].expectUint(4750000000000); // 5% Fee Charged on ONE_8*wbtcPrice @@ -232,17 +232,17 @@ Clarinet.test({ result.expectErr().expectUint(3000); // Swapping - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8, 0); position = result.expectOk().expectTuple(); position['dx'].expectUint(ONE_8); position['dy'].expectUint(4960262500000); // Attempts to trade more than limit - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8*ONE_8); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8*ONE_8, 0); result.expectErr().expectUint(4001); // Zero swapping - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0, 0); result.expectErr().expectUint(2003); // Fee Setting @@ -321,7 +321,7 @@ Clarinet.test({ // let's do some arb call = await FWPTest.getYgivenPrice(wbtcAddress, usdaAddress, weightX, weightY, wbtcPrice*ONE_8*1.1); call.result.expectOk().expectUint(23268715000000); - result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 23268715000000) + result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 23268715000000, 0) position = result.expectOk().expectTuple(); position['dy'].expectUint(23268715000000); position['dx'].expectUint(488087600); @@ -344,7 +344,7 @@ Clarinet.test({ // we need to call get-x-given-price call = await FWPTest.getXgivenPrice(wbtcAddress, usdaAddress, weightX, weightY, wbtcPrice*ONE_8*1.1 *0.95); call.result.expectOk().expectUint(248161895); - result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 248161895) + result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 248161895, 0) position = result.expectOk().expectTuple(); position['dx'].expectUint(248161895); position['dy'].expectUint(13304708837897); diff --git a/clarity/tests/flash-loan_test.ts b/clarity/tests/flash-loan_test.ts index e150d3c8..21ecc2b7 100644 --- a/clarity/tests/flash-loan_test.ts +++ b/clarity/tests/flash-loan_test.ts @@ -87,7 +87,7 @@ Clarinet.test({ call.result.expectOk().expectUint(74520874); // sell some yield-token - result = YTPTest.swapYForX(wallet_1, yieldusda23040Address, usdaAddress, 10000*ONE_8); + result = YTPTest.swapYForX(wallet_1, yieldusda23040Address, usdaAddress, 10000*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(1017223843528); position['dy'].expectUint(10000*ONE_8); diff --git a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts index 56d11209..1d157266 100644 --- a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts +++ b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts @@ -134,25 +134,27 @@ import { return block.receipts[0].result; } - swapXForY(user: Account, token: string, collateral: string, expiry: number, dX: number) { + swapXForY(user: Account, token: string, collateral: string, expiry: number, dX: number, dy_min: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "swap-x-for-y", [ types.principal(token), types.principal(collateral), types.uint(expiry), - types.uint(dX) + types.uint(dX), + types.some(types.uint(dy_min)) ], user.address), ]); return block.receipts[0].result; } - swapYForX(user: Account, token: string, collateral: string, expiry: number, dY: number) { + swapYForX(user: Account, token: string, collateral: string, expiry: number, dY: number, min_dx: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "swap-y-for-x", [ types.principal(token), types.principal(collateral), types.uint(expiry), - types.uint(dY) + types.uint(dY), + types.some(types.uint(min_dx)) ], user.address), ]); return block.receipts[0].result; diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index 8bf8df2f..b0b19d80 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -103,7 +103,7 @@ Clarinet.test({ call.result.expectOk().expectUint(200212341) // sell some yield-token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 2*ONE_8); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 2*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(200212341); position['dy'].expectUint(2*ONE_8); @@ -124,13 +124,13 @@ Clarinet.test({ call.result.expectOk().expectUint(400212); // 0.4% // buy back some yield token - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8, 0); position = result.expectOk().expectTuple() position['dx'].expectUint(ONE_8); position['dy'].expectUint(100352095); // attempt to sell more than max allowed yield token (50% of pool) must throw an error - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 501*ONE_8); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 501*ONE_8, 0); position =result.expectErr().expectUint(4002) call = chain.callReadOnlyFn("yield-token-pool", "get-pool-details", @@ -148,7 +148,7 @@ Clarinet.test({ call.result.expectOk().expectUint(199860); // 0.2% // we sell close to maximum allowed of yield token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(1927749539); position['dy'].expectUint(19*ONE_8); @@ -192,7 +192,7 @@ Clarinet.test({ call.result.expectOk().expectUint(4028722); // 4% // sell some (a lot of) yield-token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(1879746070); position['dy'].expectUint(19*ONE_8); @@ -212,7 +212,7 @@ Clarinet.test({ call.result.expectOk().expectUint(7811636); // 78% //buy back some yield token - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(19*ONE_8); position['dy'].expectUint(1956741242); @@ -234,7 +234,7 @@ Clarinet.test({ call.result.expectOk().expectUint(3952408); // 4% // buy some yield-token - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(19*ONE_8); position['dy'].expectUint(1900016846); @@ -275,7 +275,7 @@ Clarinet.test({ call = await YTPTest.getYgivenYield(yieldwbtc59760Address, 0.001*ONE_8); call.result.expectOk().expectUint(49979000); - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 49979000); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 49979000, 0); position = result.expectOk().expectTuple(); position['dy'].expectUint(49979000); position['dx'].expectUint(49878123); @@ -289,7 +289,7 @@ Clarinet.test({ call = await YTPTest.getXgivenYield(yieldwbtc59760Address, 0.0005*ONE_8); call.result.expectOk().expectUint(24903573); - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 24903573); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 24903573, 0); position = result.expectOk().expectTuple(); position['dy'].expectUint(24990123); position['dx'].expectUint(24903573); @@ -338,7 +338,7 @@ Clarinet.test({ position['balance-aytoken'].expectUint(0); position['balance-virtual'].expectUint(10*ONE_8); - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(99951128); //99954689 position['dy'].expectUint(ONE_8); @@ -457,43 +457,36 @@ Clarinet.test({ position =result.expectErr().expectUint(1) // False swap value -- Filter added - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0, 0); position =result.expectErr().expectUint(2003) - // False swap value -- Filter added - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0.0000001 * ONE_8); - //position =result.expectErr().expectUint(2003) - position =result.expectOk().expectTuple(); - position['dx'].expectUint(0); - position['dy'].expectUint(0.0000001 * ONE_8); - // call = await YTPTest.getPoolDetails(yieldwbtc59760Address); - // position = call.result.expectErr(); + // Too small => < max-slippage + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0.0000001 * ONE_8, 0); + position =result.expectErr().expectUint(2020); - // Fixed - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0.001 * ONE_8); - position =result.expectOk().expectTuple(); - position['dx'].expectUint(0); - position['dy'].expectUint(0.001 * ONE_8); + // Too small => < max-slippage + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0.001 * ONE_8, 0); + position =result.expectErr().expectUint(2020); // Attempt for Swapping - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8); + result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(99760122); + position['dx'].expectUint(99787067); position['dy'].expectUint(ONE_8); // Attempts for zero value swapping - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 0); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 0, 0); position =result.expectErr().expectUint(2003) // Attempts to swap more than available balance in the pool - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 100*ONE_8); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 100*ONE_8, 0); position =result.expectErr().expectUint(2016) // Swap - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 0.1 * ONE_8); + result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 0.1 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(0.1 * ONE_8); - position['dy'].expectUint(10178704); + position['dy'].expectUint(10161454); }, }); \ No newline at end of file From 51adf7f8b9c092a0726a573b1e6b99206246ab50 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sun, 24 Oct 2021 15:42:20 +0800 Subject: [PATCH 03/16] working --- clarity/contracts/pool/liquidity-bootstrapping-pool.clar | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index b5bb2e85..140fde16 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -46,16 +46,13 @@ total-supply: uint, balance-x: uint, balance-y: uint, - fee-balance-x: uint, - fee-balance-y: uint, - fee-to-address: principal, pool-token: principal, listed: uint, weight-x-0: uint, weight-x-1: uint, weight-x-t: uint, - fee-rate-x: uint, - fee-rate-y: uint + price-x-min: uint, + price-x-max: uint } ) From 7db2f8246e80ffa5331b569556ba45c45c79b18f Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sun, 24 Oct 2021 16:47:50 +0800 Subject: [PATCH 04/16] oracle instant/resilient added (#294) --- clarity/contracts/pool/fixed-weight-pool.clar | 99 ++++++++++++++++--- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/clarity/contracts/pool/fixed-weight-pool.clar b/clarity/contracts/pool/fixed-weight-pool.clar index 0c183669..308fb424 100644 --- a/clarity/contracts/pool/fixed-weight-pool.clar +++ b/clarity/contracts/pool/fixed-weight-pool.clar @@ -23,12 +23,11 @@ (define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) (define-constant ERR-MATH-CALL (err u2010)) -(define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) +(define-constant ERR-ORACLE-NOT-ENABLED (err u7002)) +(define-constant ERR-ORACLE-ALREADY-ENABLED (err u7003)) +(define-constant ERR-ORACLE-AVERAGE-BIGGER-THAN-ONE (err u7004)) -(define-constant alex-symbol "alex") -(define-constant reserve-usdc-symbol "usdc") (define-data-var contract-owner principal tx-sender) -(define-data-var oracle-src (string-ascii 32) "coingecko") ;; data maps and vars (define-map pools-map @@ -59,7 +58,10 @@ fee-rate-x: uint, fee-rate-y: uint, token-x-symbol: (string-ascii 32), - token-y-symbol: (string-ascii 32) + token-y-symbol: (string-ascii 32), + oracle-enabled: bool, + oracle-average: uint, + oracle-resilient: uint } ) @@ -113,15 +115,71 @@ ) ) -(define-read-only (get-oracle-src) - (ok (var-get oracle-src)) +(define-read-only (get-oracle-enabled (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) + (ok (get + oracle-enabled + (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR))) ) -(define-public (set-oracle-src (new-oracle-src (string-ascii 32))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set oracle-src new-oracle-src)) - ) +;; oracle can only be enabled +(define-public (set-oracle-enabled (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR)) + (pool-updated (merge pool {oracle-enabled: true})) + ) + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (asserts! (not (get oracle-enabled pool)) ERR-ORACLE-ALREADY-ENABLED) + (map-set pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y } pool-updated) + (ok true) + ) +) + +(define-read-only (get-oracle-average (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) + (ok (get + oracle-average + (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR))) +) + +(define-public (set-oracle-average (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (new-oracle-average uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR)) + (pool-updated (merge pool { + oracle-average: new-oracle-average, + oracle-resilient: (try! (get-oracle-instant token-x-trait token-y-trait weight-x weight-y)) + })) + ) + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) + (asserts! (< new-oracle-average ONE_8) ERR-ORACLE-AVERAGE-BIGGER-THAN-ONE) + (map-set pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y } pool-updated) + (ok true) + ) +) + +(define-read-only (get-oracle-resilient (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR)) + ) + (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) + (ok (+ (unwrap! (mul-down (- ONE_8 (get oracle-average pool)) (try! (get-oracle-instant token-x-trait token-y-trait weight-x weight-y))) ERR-MATH-CALL) + (unwrap! (mul-down (get oracle-average pool) (get oracle-resilient pool)) ERR-MATH-CALL))) + ) +) + +(define-read-only (get-oracle-instant (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR)) + ) + (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) + (ok (unwrap! + (div-down (unwrap! (mul-down (get balance-y pool) weight-x) ERR-MATH-CALL) + (unwrap! (mul-down (get balance-x pool) weight-y) ERR-MATH-CALL)) ERR-MATH-CALL) + ) + ) ) (define-public (create-pool (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) @@ -141,7 +199,10 @@ fee-rate-x: u0, fee-rate-y: u0, token-x-symbol: (try! (contract-call? token-x-trait get-symbol)), - token-y-symbol: (try! (contract-call? token-y-trait get-symbol)) + token-y-symbol: (try! (contract-call? token-y-trait get-symbol)), + oracle-enabled: false, + oracle-average: u0, + oracle-resilient: u0 }) ) (asserts! @@ -261,7 +322,11 @@ { balance-x: (+ balance-x dx-net-fees), balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), - fee-balance-x: (+ fee (get fee-balance-x pool)) + fee-balance-x: (+ fee (get fee-balance-x pool)), + oracle-resilient: (if (get oracle-enabled pool) + (try! (get-oracle-resilient token-x-trait token-y-trait weight-x weight-y)) + u0 + ) } ) ) @@ -304,7 +369,11 @@ { balance-x: (if (<= balance-x dx) u0 (- balance-x dx)), balance-y: (+ balance-y dy-net-fees), - fee-balance-y: (+ fee fee-balance-y) + fee-balance-y: (+ fee fee-balance-y), + oracle-resilient: (if (get oracle-enabled pool) + (try! (get-oracle-resilient token-x-trait token-y-trait weight-x weight-y)) + u0 + ) } ) ) From 33e16a8d9d740077357bcc7d1ecf9fef242a254e Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sun, 24 Oct 2021 20:57:32 +0800 Subject: [PATCH 05/16] #296 #297 --- .../pool/liquidity-bootstrapping-pool.clar | 313 ++++-------------- 1 file changed, 68 insertions(+), 245 deletions(-) diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index 140fde16..73892026 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -10,20 +10,16 @@ (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-INVALID-POOL-ERR (err u2001)) -(define-constant ERR-NO-LIQUIDITY (err u2002)) (define-constant ERR-INVALID-LIQUIDITY (err u2003)) (define-constant ERR-TRANSFER-X-FAILED (err u3001)) (define-constant ERR-TRANSFER-Y-FAILED (err u3002)) (define-constant ERR-POOL-ALREADY-EXISTS (err u2000)) (define-constant ERR-TOO-MANY-POOLS (err u2004)) (define-constant ERR-PERCENT_GREATER_THAN_ONE (err u5000)) -(define-constant ERR-NO-FEE (err u2005)) -(define-constant ERR-NO-FEE-Y (err u2006)) -(define-constant already-ERR-EXPIRY (err u2010)) -(define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) +(define-constant ERR-ALREADY-EXPIRED (err u2011)) (define-constant ERR-MATH-CALL (err u2010)) -(define-constant internal-get-weight-err (err u2012)) - +(define-constant ERR-PRICE-LOWER-THAN-MIN (err u2021)) +(define-constant ERR-PRICE-GREATER-THAN-MAX (err u2022)) ;; data maps and vars ;; @@ -46,6 +42,7 @@ total-supply: uint, balance-x: uint, balance-y: uint, + pool-multisig: principal, pool-token: principal, listed: uint, weight-x-0: uint, @@ -104,13 +101,7 @@ ) (define-read-only (get-pool-contracts (pool-id uint)) - (let - ( - (pool (map-get? pools-map {pool-id: pool-id})) - ) - (asserts! (is-some pool) ERR-INVALID-POOL-ERR) - (ok pool) - ) + (ok (map-get? pools-map {pool-id: pool-id})) ) (define-read-only (get-pools) @@ -118,20 +109,12 @@ ) (define-read-only (get-pool-details (token-x-trait ) (token-y-trait ) (expiry uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry })) - ) - (asserts! (is-some pool) ERR-INVALID-POOL-ERR) - (ok pool) - ) + (ok (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry })) ) (define-read-only (get-weight-x (token-x-trait ) (token-y-trait ) (expiry uint)) (begin - (asserts! (< (* block-height ONE_8) expiry) already-ERR-EXPIRY) + (asserts! (< (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) (let ( (token-x (contract-of token-x-trait)) @@ -148,22 +131,42 @@ (expiry-to-listed (- expiry listed)) (weight-diff (- weight-x-0 weight-x-1)) (time-ratio (unwrap! (div-down now-to-listed expiry-to-listed) ERR-MATH-CALL)) - (weight-change (unwrap! (mul-down weight-diff time-ratio) ERR-MATH-CALL)) - (weight-t (- weight-x-0 weight-change)) + (weight-change (unwrap! (mul-down weight-diff time-ratio) ERR-MATH-CALL)) ) - - (ok weight-t) + (ok (- weight-x-0 weight-change)) ) ) ) +(define-read-only (get-price-range (token-x-trait ) (token-y-trait ) (expiry uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (ok {min-price: (get price-x-min pool), max-price: (get price-x-max pool)}) + ) +) + +(define-public (set-price-range (token-x-trait ) (token-y-trait ) (expiry uint) (min-price uint) (max-price uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool-updated (merge pool { + price-x-min: min-price, + price-x-max: max-price + })) + ) + (asserts! (is-eq contract-caller (get pool-multisig pool)) ERR-NOT-AUTHORIZED) + (map-set pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry } pool-updated) + (ok true) + ) +) + ;; get overall balances for the pair (define-read-only (get-balances (token-x-trait ) (token-y-trait ) (expiry uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) (err ERR-INVALID-POOL-ERR))) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (ok {balance-x: (get balance-x pool), balance-y: (get balance-y pool)}) ) @@ -179,25 +182,17 @@ total-supply: u0, balance-x: u0, balance-y: u0, - fee-balance-x: u0, - fee-balance-y: u0, - fee-to-address: (contract-of multisig-vote), + pool-multisig: (contract-of multisig-vote), pool-token: (contract-of the-pool-token), listed: (* block-height ONE_8), weight-x-0: weight-x-0, weight-x-1: weight-x-1, weight-x-t: weight-x-0, - fee-rate-x: u0, - fee-rate-y: u0 + price-x-min: u0, + price-x-max: (* ONE_8 ONE_8) ;; something big }) ) - (asserts! - (and - (is-none (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry })) - (is-none (map-get? pools-data-map { token-x: token-y, token-y: token-x, expiry: expiry })) - ) - ERR-POOL-ALREADY-EXISTS - ) + (asserts! (is-none (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry })) ERR-POOL-ALREADY-EXISTS) (map-set pools-map { pool-id: pool-id } { token-x: token-x, token-y: token-y, expiry: expiry }) (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-data) @@ -247,9 +242,7 @@ (define-public (swap-x-for-y (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint)) (begin ;; swap is allowed only until expiry - (asserts! (< (* block-height ONE_8) expiry) already-ERR-EXPIRY) - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) + (asserts! (< (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) (let ( (token-x (contract-of token-x-trait)) @@ -257,34 +250,26 @@ (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) - (fee-rate-x (get fee-rate-x pool)) - - ;; fee = dx * fee-rate-x - (fee (unwrap! (mul-up dx fee-rate-x) ERR-MATH-CALL)) - (dx-net-fees (if (<= dx fee) u0 (- dx fee))) ;; swap triggers update of weight (weight-x (try! (get-weight-x token-x-trait token-y-trait expiry))) (weight-y (- ONE_8 weight-x)) - (dy (try! (contract-call? .weighted-equation get-y-given-x balance-x balance-y weight-x weight-y dx-net-fees))) - - (pool-updated - (merge pool - { - balance-x: (+ balance-x dx-net-fees), - balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), - fee-balance-x: (+ fee (get fee-balance-x pool)), - weight-x-t: weight-x - } - ) - ) + (dy (try! (contract-call? .weighted-equation get-y-given-x balance-x balance-y weight-x weight-y dx))) + + (pool-updated (merge pool { + balance-x: (+ balance-x dx), + balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), + weight-x-t: weight-x })) ) + (asserts! (<= (get price-x-min pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-LOWER-THAN-MIN) + (asserts! (>= (get price-x-max pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-GREATER-THAN-MAX) + (unwrap! (contract-call? token-x-trait transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) (try! (contract-call? .alex-vault transfer-ft token-y-trait dy (as-contract tx-sender) tx-sender)) ;; post setting (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) (print { object: "pool", action: "swap-x-for-y", data: pool-updated }) - (ok {dx: dx-net-fees, dy: dy}) + (ok {dx: dx, dy: dy}) ) ) ) @@ -292,10 +277,7 @@ (define-public (swap-y-for-x (token-x-trait ) (token-y-trait ) (expiry uint) (dy uint)) (begin ;; swap is allowed only until expiry - (asserts! (< (* block-height ONE_8) expiry) already-ERR-EXPIRY) - - ;; TODO : Check whether dy or dx value is valid - ;; (asserts! (< min-dy dy) too-much-slippage-err) + (asserts! (< (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) (let ( (token-x (contract-of token-x-trait)) @@ -303,187 +285,38 @@ (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) - (fee-rate-y (get fee-rate-y pool)) - - ;; fee = dy * fee-rate-y - (fee (unwrap! (mul-up dy fee-rate-y) ERR-MATH-CALL)) - (dy-net-fees (if (<= dy fee) u0 (- dy fee))) ;; swap triggers update of weight (weight-x (try! (get-weight-x token-x-trait token-y-trait expiry))) (weight-y (- ONE_8 weight-x)) - (dx (try! (contract-call? .weighted-equation get-x-given-y balance-x balance-y weight-x weight-y dy-net-fees))) - - (pool-updated - (merge pool - { - balance-x: (if (<= balance-x dx) u0 (- balance-x dx)), - balance-y: (+ balance-y dy-net-fees), - fee-balance-y: (+ fee (get fee-balance-y pool)), - weight-x-t: weight-x - } - ) - ) + (dx (try! (contract-call? .weighted-equation get-x-given-y balance-x balance-y weight-x weight-y dy))) + + (pool-updated (merge pool { + balance-x: (if (<= balance-x dx) u0 (- balance-x dx)), + balance-y: (+ balance-y dy), + weight-x-t: weight-x})) ) + (asserts! (<= (get price-x-min pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-LOWER-THAN-MIN) + (asserts! (>= (get price-x-max pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-GREATER-THAN-MAX) + (try! (contract-call? .alex-vault transfer-ft token-x-trait dx (as-contract tx-sender) tx-sender)) (unwrap! (contract-call? token-y-trait transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) ;; post setting (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) (print { object: "pool", action: "swap-y-for-x", data: pool-updated }) - (ok {dx: dx, dy: dy-net-fees}) - ) - ) -) - - -(define-read-only (get-fee-rate-x (token-x-trait ) (token-y-trait ) (expiry uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-rate-x pool)) - ) -) - -(define-read-only (get-fee-rate-y (token-x-trait ) (token-y-trait ) (expiry uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-rate-y pool)) - ) -) - -(define-public (set-fee-rate-x (token-x-trait ) (token-y-trait ) (expiry uint) (fee-rate-x uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - - (map-set pools-data-map - { - token-x: token-x, token-y: token-y, expiry: expiry - } - (merge pool { fee-rate-x: fee-rate-x }) - ) - (ok true) - ) -) - -(define-public (set-fee-rate-y (token-x-trait ) (token-y-trait ) (expiry uint) (fee-rate-y uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - - (map-set pools-data-map - { - token-x: token-x, token-y: token-y, expiry: expiry - } - (merge pool { fee-rate-y: fee-rate-y }) - ) - (ok true) - ) -) - -(define-read-only (get-fee-to-address (token-x-trait ) (token-y-trait ) (expiry uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) (err ERR-INVALID-POOL-ERR))) - ) - (ok (get fee-to-address pool)) - ) -) - -(define-read-only (get-fees (token-x-trait ) (token-y-trait ) (expiry uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) (err ERR-INVALID-POOL-ERR))) + (ok {dx: dx, dy: dy}) ) - (ok {fee-balance-x: (get fee-balance-x pool), fee-balance-y: (get fee-balance-y pool)}) ) ) -;; TODO: implement reserve pool logic -(define-public (collect-fees (token-x-trait ) (token-y-trait ) (expiry uint)) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (address (get fee-to-address pool)) - (fee-x (get fee-balance-x pool)) - (fee-y (get fee-balance-y pool)) - (rebate-rate (unwrap-panic (contract-call? .alex-reserve-pool get-rebate-rate))) - ) - (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - (and (> fee-x u0) - (and - ;; first transfer fee-x to tx-sender - (try! (contract-call? .alex-vault transfer-ft token-x-trait fee-x (as-contract tx-sender) tx-sender)) - ;; send fee-x to reserve-pool to mint alex - (try! - (contract-call? .alex-reserve-pool transfer-to-mint - (if (is-eq token-x .token-usda) - fee-x - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda token-x-trait u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token-x-trait u50000000 u50000000 fee-x))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token-x-trait .token-usda u50000000 u50000000 fee-x))) - ) - ) - ) - ) - ) - ) - - (and (> fee-y u0) - (and - ;; first transfer fee-y to tx-sender - ;; (unwrap! (contract-call? token-y-trait transfer fee-y .alex-vault tx-sender none) ERR-TRANSFER-Y-FAILED) - (try! (contract-call? .alex-vault transfer-ft token-y-trait fee-y (as-contract tx-sender) tx-sender)) - ;; send fee-y to reserve-pool to mint alex - (try! - (contract-call? .alex-reserve-pool transfer-to-mint - (if (is-eq token-y .token-usda) - fee-y - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-usda token-y-trait u50000000 u50000000)) - (get dx (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda token-y-trait u50000000 u50000000 fee-y))) - (get dy (try! (contract-call? .fixed-weight-pool swap-x-for-y token-y-trait .token-usda u50000000 u50000000 fee-y))) - ) - ) - ) - ) - ) - ) - - (map-set pools-data-map - { token-x: token-x, token-y: token-y, expiry: expiry} - (merge pool { fee-balance-x: u0, fee-balance-y: u0 }) - ) - (ok {fee-x: fee-x, fee-y: fee-y}) - ) +(define-read-only (get-pool-multisig (token-x-trait ) (token-y-trait ) (expiry uint)) + (ok (get pool-multisig (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) (define-read-only (get-y-given-x (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (weight-x (get weight-x-t pool)) @@ -496,9 +329,7 @@ (define-read-only (get-x-given-y (token-x-trait ) (token-y-trait ) (expiry uint) (dy uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (weight-x (get weight-x-t pool)) @@ -511,9 +342,7 @@ (define-read-only (get-x-given-price (token-x-trait ) (token-y-trait ) (expiry uint) (price uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (weight-x (get weight-x-t pool)) @@ -526,9 +355,7 @@ (define-read-only (get-token-given-position (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint) (dy uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (total-supply (get total-supply pool)) @@ -542,9 +369,7 @@ (define-read-only (get-position-given-mint (token-x-trait ) (token-y-trait ) (expiry uint) (shares uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (total-supply (get total-supply pool)) @@ -558,9 +383,7 @@ (define-read-only (get-position-given-burn (token-x-trait ) (token-y-trait ) (expiry uint) (shares uint)) (let ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (total-supply (get total-supply pool)) From 86221f7659b528e990cc359b7e087290265a6bd5 Mon Sep 17 00:00:00 2001 From: hyossid Date: Mon, 25 Oct 2021 22:56:14 +0800 Subject: [PATCH 06/16] basic setting for lbp testing --- .../liquidity-bootstrapping-pool_test.ts | 107 ++++++++++ ...alex-tests-liquidity-bootstrapping-pool.ts | 197 ++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 clarity/tests/liquidity-bootstrapping-pool_test.ts create mode 100644 clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts diff --git a/clarity/tests/liquidity-bootstrapping-pool_test.ts b/clarity/tests/liquidity-bootstrapping-pool_test.ts new file mode 100644 index 00000000..bafe14fb --- /dev/null +++ b/clarity/tests/liquidity-bootstrapping-pool_test.ts @@ -0,0 +1,107 @@ + +import { Clarinet, Tx, Chain, Account, types } from 'https://deno.land/x/clarinet@v0.14.0/index.ts'; +import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts'; + +import { + LBPTestAgent, + } from './models/alex-tests-liquidity-bootstrapping-pool.ts'; + +import { + MS_FWP_WBTC_USDA_5050, +} from './models/alex-tests-multisigs.ts'; +import { OracleManager } from './models/alex-tests-oracle-mock.ts'; + +import { + USDAToken, + ALEXToken, + } from './models/alex-tests-tokens.ts'; + +// Deployer Address Constants +const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" +const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" +const alexAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-alex" + +const lbpalexusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.lbp-alex-59760-usda" +const multisigAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-lbp-alex-59760-usda" + +const ONE_8 = 100000000 + +const weightX1 = 50000000 //0.5 +const weightX2 = 50000000 //0.5 + +const wbtcPrice = 50000 +const usdaPrice = 1 +const expiry = 59760 * ONE_8 + +const wbtcQ = 100*ONE_8 + + +Clarinet.test({ + name: "LBP : Pool creation, adding values and reducing values", + + async fn(chain: Chain, accounts: Map) { + let deployer = accounts.get("deployer")!; + let LBPTest = new LBPTestAgent(chain, deployer); + + // Deployer creating a pool, initial tokens injected to the pool + let result = LBPTest.createPool(deployer, alexAddress, usdaAddress, weightX1, weightX2, expiry,lbpalexusdaAddress,multisigAddress,1000*ONE_8, 1000*ONE_8); + result.expectOk().expectBool(true); + + // Check pool details and print + let call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + let position:any = call.result.expectOk().expectTuple(); + position['total-supply'].expectUint(2236067605752); + position['balance-x'].expectUint(wbtcQ); + position['balance-y'].expectUint(wbtcQ*wbtcPrice); + + // // Add extra liquidity (1/4 of initial liquidity) + // result = FWPTest.addToPosition(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, wbtcQ / 4, wbtcQ*wbtcPrice / 4); + // position = result.expectOk().expectTuple(); + // position['supply'].expectUint(2236067605752 / 4); + // position['dx'].expectUint(wbtcQ / 4); + // position['dy'].expectUint(wbtcQ*wbtcPrice / 4); + + // // Check pool details and print + // call = await FWPTest.getPoolDetails(wbtcAddress, usdaAddress,weightX, weightY); + // position = call.result.expectOk().expectTuple(); + // position['total-supply'].expectUint(2795084507190); + // position['balance-x'].expectUint(5/4 * wbtcQ); + // position['balance-y'].expectUint(5/4 * wbtcQ*wbtcPrice); + + // // Reduce all liquidlity + // result = FWPTest.reducePosition(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, ONE_8); + // position = result.expectOk().expectTuple(); + // position['dx'].expectUint(12499622000); + // position['dy'].expectUint(624981100000000); + + // // Add back some liquidity + // result = FWPTest.addToPosition(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, wbtcQ, wbtcQ*wbtcPrice); + // position = result.expectOk().expectTuple(); + // position['supply'].expectUint(2235639947089); + // position['dx'].expectUint(wbtcQ); + // position['dy'].expectUint(499999999999878); + + // // attempt to trade too much (> 90%) will be rejected + // result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 91*ONE_8); + // position = result.expectErr().expectUint(4001); + + // // swap some wbtc into usda + // result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8); + // position = result.expectOk().expectTuple(); + // position['dx'].expectUint(ONE_8); + // position['dy'].expectUint(4950462120393); + + // // swap some usda into wbtc + // result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, wbtcPrice*ONE_8); + // position = result.expectOk().expectTuple(); + // position['dx'].expectUint(103049813); + // position['dy'].expectUint(wbtcPrice*ONE_8); + + // // attempt to swap zero throws an error + // result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); + // result.expectErr().expectUint(2003); + // result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); + // result.expectErr().expectUint(2003); + }, +}); + diff --git a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts new file mode 100644 index 00000000..dc37219f --- /dev/null +++ b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts @@ -0,0 +1,197 @@ +import { + Account, + Chain, + Clarinet, + Tx, + types, + } from "https://deno.land/x/clarinet@v0.14.0/index.ts"; + + class LBPTestAgent { + chain: Chain; + deployer: Account; + + constructor(chain: Chain, deployer: Account) { + this.chain = chain; + this.deployer = deployer; + } + + getPoolDetails(tokenX: string, tokenY: string, expiry: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-pool-details", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + ], this.deployer.address); + } + + createPool(user: Account, tokenX: string, tokenY: string, weightX1: number, weightX2: number, expiry :number, pooltoken: string, multisig: string, dX: number, dY: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "create-pool", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(weightX1), + types.uint(weightX2), + types.uint(expiry), + types.principal(pooltoken), + types.principal(multisig), + types.uint(dX), + types.uint(dY), + ], user.address), + ]); + return block.receipts[0].result; + } + + addToPosition(user: Account, tokenX: string, tokenY: string, expiry :number, pooltoken: string, dX: number, dY: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "add-to-position", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.principal(pooltoken), + types.uint(dX), + types.uint(dY), + ], user.address), + ]); + return block.receipts[0].result; + } + + reducePosition(user: Account, tokenX: string, tokenY: string, expiry :number, pooltoken: string, percentage: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "reduce-position", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.principal(pooltoken), + types.uint(percentage), + ], user.address), + ]); + return block.receipts[0].result; + } + + swapXForY(user: Account, tokenX: string, tokenY: string, expiry: number, dx: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "swap-x-for-y", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.uint(dx) + ], user.address), + ]); + return block.receipts[0].result; + } + + swapYForX(user: Account, tokenX: string, tokenY: string, expiry: number, dy: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "swap-y-for-x", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.uint(dy) + ], user.address), + ]); + return block.receipts[0].result; + } + + getXgivenPrice(tokenX: string, tokenY: string, expiry: number, price: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-x-given-price", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.uint(price) + ], this.deployer.address); + } + + getYgivenPrice(tokenX: string, tokenY: string, expiry: number, price: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-y-given-price", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.uint(price) + ], this.deployer.address); + } + + setFeetoAddress(user: Account, tokenX: string, tokenY: string, expiry: number, address: string) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "set-fee-to-address", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.principal(address) + ], user.address), + ]); + return block.receipts[0].result; + } + + getFeetoAddress(user: Account, tokenX: string, tokenY: string, expiry: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "get-fee-to-address", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + ], user.address), + ]); + return block.receipts[0].result; + } + + getFeeX(user: Account, tokenX: string, tokenY: string, expiry: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "get-fee-rate-x", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + ], user.address), + ]); + return block.receipts[0].result; + } + + getFeeY(user: Account, tokenX: string, tokenY: string, expiry: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "get-fee-rate-y", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + ], user.address), + ]); + return block.receipts[0].result; + } + + + collectFees(user: Account, tokenX: string, tokenY: string, expiry: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "collect-fees", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + ], user.address), + ]); + return block.receipts[0].result; + } + + + + setFeeRateX(user: Account, tokenX: string, tokenY: string, expiry: number, feerate:number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "set-fee-rate-x", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.uint(feerate) + ], user.address), + ]); + return block.receipts[0].result; + } + + setFeeRateY(user: Account, tokenX: string, tokenY: string, expiry: number, feerate:number) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "set-fee-rate-y", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.uint(feerate) + ], user.address), + ]); + return block.receipts[0].result; + } + + } + + export { LBPTestAgent }; \ No newline at end of file From 19928a7e3e29d17241060adaa19e98edebd3d072 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Tue, 26 Oct 2021 21:40:55 +0800 Subject: [PATCH 07/16] working --- clarity/Clarinet.toml | 8 + .../pool/liquidity-bootstrapping-pool.clar | 2 +- .../liquidity-bootstrapping-pool_test.ts | 65 ++--- ...alex-tests-liquidity-bootstrapping-pool.ts | 96 +------ clarity/tests/token-alex-src/Clarinet.toml | 264 ------------------ 5 files changed, 42 insertions(+), 393 deletions(-) delete mode 100644 clarity/tests/token-alex-src/Clarinet.toml diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index a5a9368d..bdd5437d 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -184,3 +184,11 @@ depends_on = ["ytp-yield-usda-23040-usda", "trait-yield-token", "trait-sip-010", [contracts.multisig-crp-usda-23040-wbtc] path = "contracts/multisig/multisig-crp-usda-23040-wbtc.clar" depends_on = ["yield-usda-23040", "key-usda-23040-wbtc", "token-wbtc", "token-usda", "trait-sip-010"] + +[contracts.lbp-alex-usda-90-10] +path = "contracts/pool-token/lbp-alex-usda-90-10.clar" +depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] + +[contracts.multisig-lbp-alex-usda-90-10] +path = "contracts/multisig/multisig-lbp-alex-usda-90-10.clar" +depends_on = [] diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index 73892026..416ecb73 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -109,7 +109,7 @@ ) (define-read-only (get-pool-details (token-x-trait ) (token-y-trait ) (expiry uint)) - (ok (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry })) + (ok (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (define-read-only (get-weight-x (token-x-trait ) (token-y-trait ) (expiry uint)) diff --git a/clarity/tests/liquidity-bootstrapping-pool_test.ts b/clarity/tests/liquidity-bootstrapping-pool_test.ts index bafe14fb..7ab0487b 100644 --- a/clarity/tests/liquidity-bootstrapping-pool_test.ts +++ b/clarity/tests/liquidity-bootstrapping-pool_test.ts @@ -1,39 +1,24 @@ import { Clarinet, Tx, Chain, Account, types } from 'https://deno.land/x/clarinet@v0.14.0/index.ts'; -import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts'; +import { LBPTestAgent } from './models/alex-tests-liquidity-bootstrapping-pool.ts'; -import { - LBPTestAgent, - } from './models/alex-tests-liquidity-bootstrapping-pool.ts'; - -import { - MS_FWP_WBTC_USDA_5050, -} from './models/alex-tests-multisigs.ts'; -import { OracleManager } from './models/alex-tests-oracle-mock.ts'; - -import { - USDAToken, - ALEXToken, - } from './models/alex-tests-tokens.ts'; - -// Deployer Address Constants -const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" +// Deployer Address Constants const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" const alexAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-alex" +const poolTokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.lbp-alex-usda-90-10" +const multisigAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-lbp-alex-usda-90-10" -const lbpalexusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.lbp-alex-59760-usda" -const multisigAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-lbp-alex-59760-usda" - -const ONE_8 = 100000000 +const ONE_8 = 1e+8; -const weightX1 = 50000000 //0.5 -const weightX2 = 50000000 //0.5 +const weightX1 = 0.9 * ONE_8; +const weightX2 = 0.1 * ONE_8; +const expiry = 1000 * ONE_8; -const wbtcPrice = 50000 -const usdaPrice = 1 -const expiry = 59760 * ONE_8 +const priceMax = 1 * ONE_8; +const priceMin = 0.2 * ONE_8; -const wbtcQ = 100*ONE_8 +const alexQty = 10000 * ONE_8; +const usdaQty = Math.round(priceMax * alexQty * (ONE_8 - weightX1) / weightX1 / ONE_8); Clarinet.test({ @@ -42,24 +27,28 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; let LBPTest = new LBPTestAgent(chain, deployer); + + console.log('alex qty: ', alexQty / ONE_8, 'usda qty: ', usdaQty / ONE_8); + + let call = chain.callReadOnlyFn("token-alex", "get-balance", + [types.principal(deployer.address) + ], deployer.address); + call.result.expectOk().expectUint(10000 * ONE_8); // Deployer creating a pool, initial tokens injected to the pool - let result = LBPTest.createPool(deployer, alexAddress, usdaAddress, weightX1, weightX2, expiry,lbpalexusdaAddress,multisigAddress,1000*ONE_8, 1000*ONE_8); + let result = LBPTest.createPool(deployer, alexAddress, usdaAddress, weightX1, weightX2, expiry, poolTokenAddress, multisigAddress, alexQty, usdaQty); result.expectOk().expectBool(true); // Check pool details and print - let call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); let position:any = call.result.expectOk().expectTuple(); - position['total-supply'].expectUint(2236067605752); - position['balance-x'].expectUint(wbtcQ); - position['balance-y'].expectUint(wbtcQ*wbtcPrice); + position['total-supply'].expectUint(802741422430); + position['balance-x'].expectUint(alexQty); + position['balance-y'].expectUint(usdaQty); - // // Add extra liquidity (1/4 of initial liquidity) - // result = FWPTest.addToPosition(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, wbtcQ / 4, wbtcQ*wbtcPrice / 4); - // position = result.expectOk().expectTuple(); - // position['supply'].expectUint(2236067605752 / 4); - // position['dx'].expectUint(wbtcQ / 4); - // position['dy'].expectUint(wbtcQ*wbtcPrice / 4); + console.log(deployer.address); + result = LBPTest.setPriceRange(multisigAddress, alexAddress, usdaAddress, expiry, priceMin, priceMax); + result.expectOk(); // // Check pool details and print // call = await FWPTest.getPoolDetails(wbtcAddress, usdaAddress,weightX, weightY); diff --git a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts index dc37219f..3c0659f4 100644 --- a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts +++ b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts @@ -39,17 +39,16 @@ import { ]); return block.receipts[0].result; } - - addToPosition(user: Account, tokenX: string, tokenY: string, expiry :number, pooltoken: string, dX: number, dY: number) { + + setPriceRange(multisig: string, tokenX: string, tokenY: string, expiry :number, min_price: number, max_price: number) { let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "add-to-position", [ + Tx.contractCall("liquidity-bootstrapping-pool", "set-price-range", [ types.principal(tokenX), types.principal(tokenY), types.uint(expiry), - types.principal(pooltoken), - types.uint(dX), - types.uint(dY), - ], user.address), + types.uint(min_price), + types.uint(max_price) + ], multisig), ]); return block.receipts[0].result; } @@ -107,89 +106,6 @@ import { types.uint(expiry), types.uint(price) ], this.deployer.address); - } - - setFeetoAddress(user: Account, tokenX: string, tokenY: string, expiry: number, address: string) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "set-fee-to-address", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - types.principal(address) - ], user.address), - ]); - return block.receipts[0].result; - } - - getFeetoAddress(user: Account, tokenX: string, tokenY: string, expiry: number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "get-fee-to-address", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - ], user.address), - ]); - return block.receipts[0].result; - } - - getFeeX(user: Account, tokenX: string, tokenY: string, expiry: number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "get-fee-rate-x", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - ], user.address), - ]); - return block.receipts[0].result; - } - - getFeeY(user: Account, tokenX: string, tokenY: string, expiry: number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "get-fee-rate-y", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - ], user.address), - ]); - return block.receipts[0].result; - } - - - collectFees(user: Account, tokenX: string, tokenY: string, expiry: number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "collect-fees", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - ], user.address), - ]); - return block.receipts[0].result; - } - - - - setFeeRateX(user: Account, tokenX: string, tokenY: string, expiry: number, feerate:number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "set-fee-rate-x", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - types.uint(feerate) - ], user.address), - ]); - return block.receipts[0].result; - } - - setFeeRateY(user: Account, tokenX: string, tokenY: string, expiry: number, feerate:number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "set-fee-rate-y", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - types.uint(feerate) - ], user.address), - ]); - return block.receipts[0].result; } } diff --git a/clarity/tests/token-alex-src/Clarinet.toml b/clarity/tests/token-alex-src/Clarinet.toml deleted file mode 100644 index 42d85e59..00000000 --- a/clarity/tests/token-alex-src/Clarinet.toml +++ /dev/null @@ -1,264 +0,0 @@ -[project] -name = "alex-v1" -requirements = [] -[contracts.alex-reserve-pool] -path = "contracts/pool/alex-reserve-pool.clar" -depends_on = ["token-alex", "token-usda", "open-oracle", "math-fixed-point", "trait-ownable"] - -[contracts.alex-vault] -path = "contracts/alex-vault.clar" -depends_on = ["trait-vault", "trait-sip-010", "trait-flash-loan-user", "math-fixed-point", "trait-ownable"] - -[contracts.collateral-rebalancing-pool] -path = "contracts/pool/collateral-rebalancing-pool.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "trait-vault", "math-fixed-point", "weighted-equation", "open-oracle", "fixed-weight-pool", "token-usda", "token-alex", "alex-reserve-pool", "yield-token-pool"] - -[contracts.faucet] -path = "contracts/faucet.clar" -depends_on = ["token-wbtc", "token-usda"] - -[contracts.fixed-weight-pool] -path = "contracts/pool/fixed-weight-pool.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-vault", "math-fixed-point", "weighted-equation", "token-alex", "open-oracle", "alex-reserve-pool", "token-usda", "trait-multisig-vote"] - -[contracts.fwp-wbtc-usda-50-50] -path = "contracts/pool-token/fwp-wbtc-usda-50-50.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.liquidity-bootstrapping-pool] -path = "contracts/pool/liquidity-bootstrapping-pool.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-vault", "math-fixed-point", "weighted-equation", "token-alex", "open-oracle", "alex-reserve-pool", "token-usda", "fixed-weight-pool", "trait-multisig-vote"] - -[contracts.math-fixed-point] -path = "contracts/lib/math-fixed-point.clar" -depends_on = ["math-log-exp"] - -[contracts.math-log-exp] -path = "contracts/lib/math-log-exp.clar" -depends_on = [] - -[contracts.math-new-fixed-point] -path = "contracts/new-lib/math-new-fixed-point.clar" -depends_on = ["math-new-log-exp"] - -[contracts.math-new-log-exp] -path = "contracts/new-lib/math-new-log-exp.clar" -depends_on = [] - -[contracts.multisig-fwp-wbtc-usda-50-50] -path = "contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar" -depends_on = [] - -[contracts.open-oracle] -path = "contracts/open-oracle.clar" -depends_on = ["trait-yield-token", "trait-oracle", "trait-sip-010"] - -[contracts.token-usda] -path = "contracts/token/token-usda.clar" -depends_on = ["trait-pool-token", "trait-ownable"] - -[contracts.token-wbtc] -path = "contracts/token/token-wbtc.clar" -depends_on = ["trait-pool-token", "trait-ownable"] - -[contracts.trait-flash-loan-user] -path = "contracts/traits/trait-flash-loan-user.clar" -depends_on = ["trait-sip-010"] - -[contracts.trait-multisig-vote] -path = "contracts/traits/trait-multisig-vote.clar" -depends_on = ["trait-sip-010"] - -[contracts.trait-oracle] -path = "contracts/traits/trait-oracle.clar" -depends_on = [] - -[contracts.trait-ownable] -path = "contracts/traits/trait-ownable.clar" -depends_on = [] - -[contracts.trait-pool-token] -path = "contracts/traits/trait-pool-token.clar" -depends_on = [] - -[contracts.trait-sip-010] -path = "contracts/traits/trait-sip-010.clar" -depends_on = [] - -[contracts.trait-vault] -path = "contracts/traits/trait-vault.clar" -depends_on = ["trait-sip-010", "trait-flash-loan-user"] - -[contracts.trait-yield-token] -path = "contracts/traits/trait-yield-token.clar" -depends_on = [] - -[contracts.weighted-equation] -path = "contracts/equations/weighted-equation.clar" -depends_on = ["math-fixed-point"] - -[contracts.yield-token-equation] -path = "contracts/equations/yield-token-equation.clar" -depends_on = ["math-fixed-point"] - -[contracts.yield-token-pool] -path = "contracts/pool/yield-token-pool.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-vault", "trait-flash-loan-user", "math-fixed-point", "yield-token-equation", "trait-yield-token", "open-oracle", "token-alex", "token-usda", "fixed-weight-pool", "alex-reserve-pool", "trait-multisig-vote"] - -[contracts.yield-wbtc-59760] -path = "contracts/yield-token/yield-wbtc-59760.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.multisig-ytp-yield-wbtc-59760-wbtc] -path = "contracts/multisig/multisig-ytp-yield-wbtc-59760-wbtc.clar" -depends_on = ["ytp-yield-wbtc-59760-wbtc", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-wbtc-59760"] - -[contracts.flash-loan-user-margin-usda-wbtc-59760] -path = "contracts/flash-loan-user-margin-usda-wbtc-59760.clar" -depends_on = ["token-usda", "token-wbtc", "yield-wbtc-59760", "key-wbtc-59760-usda", "trait-sip-010"] - -[contracts.multisig-crp-wbtc-59760-usda] -path = "contracts/multisig/multisig-crp-wbtc-59760-usda.clar" -depends_on = ["yield-wbtc-59760", "key-wbtc-59760-usda", "token-wbtc", "token-usda", "trait-sip-010"] - -[contracts.multisig-crp-wbtc-59760-wbtc] -path = "contracts/multisig/multisig-crp-wbtc-59760-wbtc.clar" -depends_on = ["yield-wbtc-59760", "key-wbtc-59760-wbtc", "token-wbtc", "trait-sip-010"] - -[contracts.key-wbtc-59760-usda] -path = "contracts/key-token/key-wbtc-59760-usda.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.key-wbtc-59760-wbtc] -path = "contracts/key-token/key-wbtc-59760-wbtc.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.ytp-yield-wbtc-59760-wbtc] -path = "contracts/pool-token/ytp-yield-wbtc-59760-wbtc.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.multisig-crp-wbtc-79760-usda] -path = "contracts/multisig/multisig-crp-wbtc-79760-usda.clar" -depends_on = ["yield-wbtc-79760", "key-wbtc-79760-usda", "token-wbtc", "token-usda", "trait-sip-010"] - -[contracts.yield-wbtc-79760] -path = "contracts/yield-token/yield-wbtc-79760.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.multisig-ytp-yield-wbtc-79760-wbtc] -path = "contracts/multisig/multisig-ytp-yield-wbtc-59760-wbtc.clar" -depends_on = ["ytp-yield-wbtc-79760-wbtc", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-wbtc-79760"] - -[contracts.key-wbtc-79760-usda] -path = "contracts/key-token/key-wbtc-79760-usda.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.ytp-yield-wbtc-79760-wbtc] -path = "contracts/pool-token/ytp-yield-wbtc-79760-wbtc.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.yield-usda-74880] -path = "contracts/yield-token/yield-usda-74880.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-usda", "trait-ownable"] - -[contract.multisig-fwp-usda-23040-usda-74880-50-50] -path = "contracts/multisig/multisig-fwp-usda-23040-usda-74880-50-50.clar" -depends_on = ["yield-usda-23040", "yield-usda-74880", "fwp-usda-23040-usda-74880", "trait-yield-token", "trait-sip-010", "yield-token-pool"] - -[contract.fwp-usda-23040-usda-74880-50-50] -path = "contracts/pool-token/fwp-usda-23040-usda-74880-50-50.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.yield-usda-23040] -path = "contracts/yield-token/yield-usda-23040.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-usda", "trait-ownable"] - -[contracts.flash-loan-user-margin-wbtc-usda-23040] -path = "contracts/flash-loan-user-margin-wbtc-usda-23040.clar" -depends_on = ["token-usda", "token-wbtc", "yield-usda-23040", "key-usda-23040-wbtc", "trait-sip-010"] - -[contracts.key-usda-23040-wbtc] -path = "contracts/key-token/key-usda-23040-wbtc.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.ytp-yield-usda-23040-usda] -path = "contracts/pool-token/ytp-yield-usda-23040-usda.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.multisig-ytp-yield-usda-23040-usda] -path = "contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar" -depends_on = ["ytp-yield-usda-23040-usda", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-usda-23040"] - -[contracts.multisig-crp-usda-23040-wbtc] -path = "contracts/multisig/multisig-crp-usda-23040-wbtc.clar" -depends_on = ["yield-usda-23040", "key-usda-23040-wbtc", "token-wbtc", "token-usda", "trait-sip-010"] - - -[contracts.yield-wbtc-240] -path = "contracts/yield-token/yield-wbtc-240.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.yield-usda-240] -path = "contracts/yield-token/yield-usda-240.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-usda", "trait-ownable"] - -[contracts.multisig-ytp-yield-wbtc-240-wbtc] -path = "contracts/multisig/multisig-ytp-yield-wbtc-240-wbtc.clar" -depends_on = ["ytp-yield-wbtc-240-wbtc", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-wbtc-240"] - -[contracts.multisig-ytp-yield-usda-240-usda] -path = "contracts/multisig/multisig-ytp-yield-usda-240-usda.clar" -depends_on = ["ytp-yield-usda-240-usda", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-usda-240"] - -[contracts.flash-loan-user-margin-usda-wbtc-240] -path = "contracts/flash-loan-user-margin-usda-wbtc-240.clar" -depends_on = ["token-usda", "token-wbtc", "yield-wbtc-240", "key-wbtc-240-usda", "trait-sip-010"] - -[contracts.flash-loan-user-margin-wbtc-usda-240] -path = "contracts/flash-loan-user-margin-wbtc-usda-240.clar" -depends_on = ["token-usda", "token-wbtc", "yield-usda-240", "key-usda-240-wbtc", "trait-sip-010"] - -[contracts.multisig-crp-wbtc-240-usda] -path = "contracts/multisig/multisig-crp-wbtc-240-usda.clar" -depends_on = ["yield-wbtc-240", "key-wbtc-240-usda", "token-wbtc", "token-usda", "trait-sip-010"] - -[contracts.multisig-crp-usda-240-wbtc] -path = "contracts/multisig/multisig-crp-usda-240-wbtc.clar" -depends_on = ["yield-usda-240", "key-usda-240-wbtc", "token-usda", "token-wbtc", "trait-sip-010"] - -[contracts.stacked-poxl-pool] -path = "contracts/pool/stacked-poxl-pool.clar" -depends_on = [] - -[contracts.token-alex-vrf] -path = "contracts/token/token-alex-vrf.clar" -depends_on = [] - -[contracts.token-alex-core-trait] -path = "contracts/token/token-alex-core-trait.clar" -depends_on = [] - -[contracts.token-alex] -path = "contracts/token/token-alex.clar" -depends_on = ["trait-ownable", "trait-sip-010", "token-alex-auth", "token-alex-trait"] - -[contracts.token-alex-core-v1] -path = "contracts/token/token-alex-core-v1.clar" -depends_on = ["token-alex-auth", "token-alex", "token-alex-vrf", "token-alex-core-trait"] - - -[contracts.key-wbtc-240-usda] -path = "contracts/key-token/key-wbtc-240-usda.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.key-usda-240-wbtc] -path = "contracts/key-token/key-usda-240-wbtc.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-usda", "trait-ownable"] - -[contracts.ytp-yield-wbtc-240-wbtc] -path = "contracts/pool-token/ytp-yield-wbtc-240-wbtc.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.ytp-yield-usda-240-usda] -path = "contracts/pool-token/ytp-yield-usda-240-usda.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] From 801ff313b2ce51325f7b59e57b0d2cdd30a3e6f6 Mon Sep 17 00:00:00 2001 From: Nouman Ahmad Date: Tue, 26 Oct 2021 19:06:25 +0500 Subject: [PATCH 08/16] Fix issue 276 simplify fee rate methods --- .../pool/collateral-rebalancing-pool.clar | 36 +++++-------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index 00f9f76e..17981c65 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -661,40 +661,24 @@ ) ) -(define-read-only (get-fee-rate-x (token ) (collateral ) (expiry uint)) - (let - ( - (token-x (contract-of collateral)) - (token-y (contract-of token)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-rate-x pool)) - ) +(define-read-only (get-fee-rate-x (token ) (collateral ) (expiry uint)) + (ok (get fee-rate-x (try! (get-pool-details token collateral expiry)))) ) -(define-read-only (get-fee-rate-y (token ) (collateral ) (expiry uint)) - (let - ( - (token-x (contract-of collateral)) - (token-y (contract-of token)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-rate-y pool)) - ) +(define-read-only (get-fee-rate-y (token ) (collateral ) (expiry uint)) + (ok (get fee-rate-y (try! (get-pool-details token collateral expiry)))) ) (define-public (set-fee-rate-x (token ) (collateral ) (expiry uint) (fee-rate-x uint)) (let ( - (token-x (contract-of collateral)) - (token-y (contract-of token)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool (try! (get-pool-details token collateral expiry))) ) (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) (map-set pools-data-map { - token-x: token-x, token-y: token-y, expiry: expiry + token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry } (merge pool { fee-rate-x: fee-rate-x }) ) @@ -704,16 +688,14 @@ (define-public (set-fee-rate-y (token ) (collateral ) (expiry uint) (fee-rate-y uint)) (let - ( - (token-x (contract-of collateral)) - (token-y (contract-of token)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ( + (pool (try! (get-pool-details token collateral expiry))) ) (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) (map-set pools-data-map { - token-x: token-x, token-y: token-y, expiry: expiry + token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry } (merge pool { fee-rate-y: fee-rate-y }) ) From 9f7e40123b2b770d0c29b6312cf0b70c6f1ce03f Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Tue, 26 Oct 2021 23:16:28 +0800 Subject: [PATCH 09/16] set-pool-multisig added for testing purposes --- .../pool/liquidity-bootstrapping-pool.clar | 17 +++++++++++++++++ .../tests/liquidity-bootstrapping-pool_test.ts | 6 ++++-- .../alex-tests-liquidity-bootstrapping-pool.ts | 16 ++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index 416ecb73..cdb5f17c 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -21,6 +21,8 @@ (define-constant ERR-PRICE-LOWER-THAN-MIN (err u2021)) (define-constant ERR-PRICE-GREATER-THAN-MAX (err u2022)) +(define-constant CONTRACT-OWNER tx-sender) + ;; data maps and vars ;; (define-map pools-map @@ -313,6 +315,21 @@ (ok (get pool-multisig (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) +;; testing only +(define-public (set-pool-multisig (token-x-trait ) (token-y-trait ) (expiry uint) (new-multisig principal)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool-updated (merge pool { + pool-multisig: new-multisig + })) + ) + (asserts! (is-eq contract-caller CONTRACT-OWNER) ERR-NOT-AUTHORIZED) + (map-set pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry } pool-updated) + (ok true) + ) +) + (define-read-only (get-y-given-x (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint)) (let ( diff --git a/clarity/tests/liquidity-bootstrapping-pool_test.ts b/clarity/tests/liquidity-bootstrapping-pool_test.ts index 7ab0487b..55ee69d2 100644 --- a/clarity/tests/liquidity-bootstrapping-pool_test.ts +++ b/clarity/tests/liquidity-bootstrapping-pool_test.ts @@ -46,8 +46,10 @@ Clarinet.test({ position['balance-x'].expectUint(alexQty); position['balance-y'].expectUint(usdaQty); - console.log(deployer.address); - result = LBPTest.setPriceRange(multisigAddress, alexAddress, usdaAddress, expiry, priceMin, priceMax); + result = LBPTest.setPoolMultisig(deployer, alexAddress, usdaAddress, expiry, deployer.address); + result.expectOk(); + + result = LBPTest.setPriceRange(deployer, alexAddress, usdaAddress, expiry, priceMin, priceMax); result.expectOk(); // // Check pool details and print diff --git a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts index 3c0659f4..da40758b 100644 --- a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts +++ b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts @@ -40,7 +40,7 @@ import { return block.receipts[0].result; } - setPriceRange(multisig: string, tokenX: string, tokenY: string, expiry :number, min_price: number, max_price: number) { + setPriceRange(user: Account, tokenX: string, tokenY: string, expiry :number, min_price: number, max_price: number) { let block = this.chain.mineBlock([ Tx.contractCall("liquidity-bootstrapping-pool", "set-price-range", [ types.principal(tokenX), @@ -48,10 +48,22 @@ import { types.uint(expiry), types.uint(min_price), types.uint(max_price) - ], multisig), + ], user.address), ]); return block.receipts[0].result; } + + setPoolMultisig(user: Account, tokenX: string, tokenY: string, expiry :number, new_multisig: string) { + let block = this.chain.mineBlock([ + Tx.contractCall("liquidity-bootstrapping-pool", "set-pool-multisig", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry), + types.principal(new_multisig) + ], user.address), + ]); + return block.receipts[0].result; + } reducePosition(user: Account, tokenX: string, tokenY: string, expiry :number, pooltoken: string, percentage: number) { let block = this.chain.mineBlock([ From 7f489b7c00afa9d2e7107eb486a4c55f6dc95234 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:08:41 +0800 Subject: [PATCH 10/16] pool-token/multisig contracts added --- .../equations/weighted-equation.clar | 7 +- .../multisig-lbp-alex-usda-90-10.clar | 253 ++++++++++++++++++ .../pool-token/lbp-alex-usda-90-10.clar | 97 +++++++ .../pool/liquidity-bootstrapping-pool.clar | 86 +++--- .../liquidity-bootstrapping-pool_test.ts | 160 +++++++---- ...alex-tests-liquidity-bootstrapping-pool.ts | 40 +-- 6 files changed, 530 insertions(+), 113 deletions(-) create mode 100644 clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar create mode 100644 clarity/contracts/pool-token/lbp-alex-usda-90-10.clar diff --git a/clarity/contracts/equations/weighted-equation.clar b/clarity/contracts/equations/weighted-equation.clar index 782dd4d1..15dc7800 100644 --- a/clarity/contracts/equations/weighted-equation.clar +++ b/clarity/contracts/equations/weighted-equation.clar @@ -53,8 +53,9 @@ (denominator (+ balance-x dx)) (base (unwrap-panic (div-up balance-x denominator))) (uncapped-exponent (unwrap-panic (div-up weight-x weight-y))) - (bound (unwrap-panic (get-exp-bound))) - (exponent (if (< uncapped-exponent bound) uncapped-exponent bound)) + ;; (bound (unwrap-panic (get-exp-bound))) + ;; (exponent (if (< uncapped-exponent bound) uncapped-exponent bound)) + (exponent uncapped-exponent) (power (unwrap-panic (pow-up base exponent))) (complement (if (<= ONE_8 power) u0 (- ONE_8 power))) (dy (unwrap-panic (mul-down balance-y complement))) @@ -79,7 +80,7 @@ ( (denominator (if (<= balance-y dy) u0 (- balance-y dy))) (base (unwrap-panic (div-down balance-y denominator))) - (uncapped-exponent (unwrap-panic (div-down weight-x weight-y))) + (uncapped-exponent (unwrap-panic (div-down weight-y weight-x))) (bound (unwrap-panic (get-exp-bound))) (exponent (if (< uncapped-exponent bound) uncapped-exponent bound)) (power (unwrap-panic (pow-down base exponent))) diff --git a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar new file mode 100644 index 00000000..376a7892 --- /dev/null +++ b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar @@ -0,0 +1,253 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, + new-fee-rate-y: u0 + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .lbp-alex-usda-90-10) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let ( + (proposer-balance (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-balance tx-sender))) + (total-supply (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-total-supply))) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, + new-fee-rate-y: u0 + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-total-supply))) + (threshold-count (unwrap-panic (contract-call? .math-fixed-point mul-up total-supply threshold-percent))) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; ;; Execute the proposal when the yes-vote passes threshold-count. + ;; (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true) + ) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) diff --git a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar new file mode 100644 index 00000000..66ef902e --- /dev/null +++ b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token lbp-alex-usda-90-10) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .liquidity-bootstrapping-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply lbp-alex-usda-90-10))) +) + +(define-read-only (get-name) + (ok "lbp-alex-usda-90-10") +) + +(define-read-only (get-symbol) + (ok "lbp-alex-usda-90-10") +) + +(define-read-only (get-decimals) + (ok u0) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance lbp-alex-usda-90-10 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? lbp-alex-usda-90-10 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? lbp-alex-usda-90-10 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? lbp-alex-usda-90-10 (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index cdb5f17c..fc10c18f 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -93,7 +93,42 @@ (ok true) ) ) -) +) + +(define-private (swap-x-for-y (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint)) + (begin + ;; swap is allowed only until expiry + (asserts! (<= (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) + (let + ( + (token-x (contract-of token-x-trait)) + (token-y (contract-of token-y-trait)) + (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + + ;; swap triggers update of weight + (weight-x (try! (get-weight-x token-x-trait token-y-trait expiry))) + (weight-y (- ONE_8 weight-x)) + (dy (try! (get-y-given-x token-x-trait token-y-trait expiry dx))) + + (pool-updated (merge pool { + balance-x: (+ balance-x dx), + balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), + weight-x-t: weight-x })) + ) + (asserts! (<= (get price-x-min pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-LOWER-THAN-MIN) + (asserts! (>= (get price-x-max pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-GREATER-THAN-MAX) + + (unwrap! (contract-call? token-x-trait transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) + (try! (contract-call? .alex-vault transfer-ft token-y-trait dy (as-contract tx-sender) tx-sender)) + ;; post setting + (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) + (print { object: "pool", action: "swap-x-for-y", data: pool-updated }) + (ok {dx: dx, dy: dy}) + ) + ) +) ;; public functions ;; @@ -116,7 +151,7 @@ (define-read-only (get-weight-x (token-x-trait ) (token-y-trait ) (expiry uint)) (begin - (asserts! (< (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) + (asserts! (<= (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) (let ( (token-x (contract-of token-x-trait)) @@ -226,7 +261,7 @@ (pool-updated (merge pool { total-supply: (if (<= total-supply shares) u0 (- total-supply shares)), balance-x: (if (<= balance-x dx) u0 (- balance-x dx)), - balance-y: (if (<= balance-y dx) u0 (- balance-y dy)) + balance-y: (if (<= balance-y dy) u0 (- balance-y dy)) }) ) ) @@ -241,45 +276,10 @@ ) ) -(define-public (swap-x-for-y (token-x-trait ) (token-y-trait ) (expiry uint) (dx uint)) - (begin - ;; swap is allowed only until expiry - (asserts! (< (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) - (let - ( - (token-x (contract-of token-x-trait)) - (token-y (contract-of token-y-trait)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - - ;; swap triggers update of weight - (weight-x (try! (get-weight-x token-x-trait token-y-trait expiry))) - (weight-y (- ONE_8 weight-x)) - (dy (try! (contract-call? .weighted-equation get-y-given-x balance-x balance-y weight-x weight-y dx))) - - (pool-updated (merge pool { - balance-x: (+ balance-x dx), - balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), - weight-x-t: weight-x })) - ) - (asserts! (<= (get price-x-min pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-LOWER-THAN-MIN) - (asserts! (>= (get price-x-max pool) (unwrap! (div-down dy dx) ERR-MATH-CALL)) ERR-PRICE-GREATER-THAN-MAX) - - (unwrap! (contract-call? token-x-trait transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) - (try! (contract-call? .alex-vault transfer-ft token-y-trait dy (as-contract tx-sender) tx-sender)) - ;; post setting - (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (print { object: "pool", action: "swap-x-for-y", data: pool-updated }) - (ok {dx: dx, dy: dy}) - ) - ) -) - (define-public (swap-y-for-x (token-x-trait ) (token-y-trait ) (expiry uint) (dy uint)) (begin ;; swap is allowed only until expiry - (asserts! (< (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) + (asserts! (<= (* block-height ONE_8) expiry) ERR-ALREADY-EXPIRED) (let ( (token-x (contract-of token-x-trait)) @@ -291,7 +291,7 @@ ;; swap triggers update of weight (weight-x (try! (get-weight-x token-x-trait token-y-trait expiry))) (weight-y (- ONE_8 weight-x)) - (dx (try! (contract-call? .weighted-equation get-x-given-y balance-x balance-y weight-x weight-y dy))) + (dx (try! (get-x-given-y token-x-trait token-y-trait expiry dy))) (pool-updated (merge pool { balance-x: (if (<= balance-x dx) u0 (- balance-x dx)), @@ -347,12 +347,8 @@ (let ( (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of token-x-trait), token-y: (contract-of token-y-trait), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (weight-x (get weight-x-t pool)) - (weight-y (- ONE_8 weight-x)) ) - (contract-call? .weighted-equation get-x-given-y balance-x balance-y weight-x weight-y dy) + (contract-call? .weighted-equation get-x-given-y (get balance-x pool) (get balance-y pool) (get weight-x-t pool) (- ONE_8 (get weight-x-t pool)) dy) ) ) diff --git a/clarity/tests/liquidity-bootstrapping-pool_test.ts b/clarity/tests/liquidity-bootstrapping-pool_test.ts index 55ee69d2..87f8c76d 100644 --- a/clarity/tests/liquidity-bootstrapping-pool_test.ts +++ b/clarity/tests/liquidity-bootstrapping-pool_test.ts @@ -14,11 +14,12 @@ const weightX1 = 0.9 * ONE_8; const weightX2 = 0.1 * ONE_8; const expiry = 1000 * ONE_8; -const priceMax = 1 * ONE_8; -const priceMin = 0.2 * ONE_8; +const priceMax = 1.5 * ONE_8; +const priceMin = 0.1 * ONE_8; +const price0 = 1 * ONE_8; -const alexQty = 10000 * ONE_8; -const usdaQty = Math.round(priceMax * alexQty * (ONE_8 - weightX1) / weightX1 / ONE_8); +const alexQty = 1000 * ONE_8; +const usdaQty = Math.round(price0 * alexQty * (ONE_8 - weightX1) / weightX1 / ONE_8); Clarinet.test({ @@ -28,7 +29,7 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let LBPTest = new LBPTestAgent(chain, deployer); - console.log('alex qty: ', alexQty / ONE_8, 'usda qty: ', usdaQty / ONE_8); + console.log('alex qty: ', alexQty, 'usda qty: ', usdaQty); let call = chain.callReadOnlyFn("token-alex", "get-balance", [types.principal(deployer.address) @@ -42,57 +43,122 @@ Clarinet.test({ // Check pool details and print call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); let position:any = call.result.expectOk().expectTuple(); - position['total-supply'].expectUint(802741422430); + position['total-supply'].expectUint(80274141756); position['balance-x'].expectUint(alexQty); position['balance-y'].expectUint(usdaQty); + position['weight-x-t'].expectUint(90000000); result = LBPTest.setPoolMultisig(deployer, alexAddress, usdaAddress, expiry, deployer.address); result.expectOk(); - + result = LBPTest.setPriceRange(deployer, alexAddress, usdaAddress, expiry, priceMin, priceMax); result.expectOk(); - // // Check pool details and print - // call = await FWPTest.getPoolDetails(wbtcAddress, usdaAddress,weightX, weightY); - // position = call.result.expectOk().expectTuple(); - // position['total-supply'].expectUint(2795084507190); - // position['balance-x'].expectUint(5/4 * wbtcQ); - // position['balance-y'].expectUint(5/4 * wbtcQ*wbtcPrice); - - // // Reduce all liquidlity - // result = FWPTest.reducePosition(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, ONE_8); - // position = result.expectOk().expectTuple(); - // position['dx'].expectUint(12499622000); - // position['dy'].expectUint(624981100000000); - - // // Add back some liquidity - // result = FWPTest.addToPosition(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, wbtcQ, wbtcQ*wbtcPrice); - // position = result.expectOk().expectTuple(); - // position['supply'].expectUint(2235639947089); - // position['dx'].expectUint(wbtcQ); - // position['dy'].expectUint(499999999999878); - - // // attempt to trade too much (> 90%) will be rejected - // result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 91*ONE_8); - // position = result.expectErr().expectUint(4001); - - // // swap some wbtc into usda - // result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, ONE_8); - // position = result.expectOk().expectTuple(); - // position['dx'].expectUint(ONE_8); - // position['dy'].expectUint(4950462120393); + call = await LBPTest.getPriceRange(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['min-price'].expectUint(priceMin); + position['max-price'].expectUint(priceMax); + + // implied price is 0.995064480178316 + result = LBPTest.swapYForX(deployer, alexAddress, usdaAddress, expiry, ONE_8); + position = result.expectOk().expectTuple(); + position['dy'].expectUint(ONE_8); + position['dx'].expectUint(100496000); + + // swap triggers change in weight + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['weight-x-t'].expectUint(89759760); + + // half time passed + chain.mineEmptyBlockUntil(500); + + // no swaps, so weight shouldn't have changed. + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['weight-x-t'].expectUint(89759760); + + // buy some alex so it doesn't fall below min-price. + result = LBPTest.swapYForX(deployer, alexAddress, usdaAddress, expiry, 30 * ONE_8); + position = result.expectOk().expectTuple(); + position['dy'].expectUint(30 * ONE_8); + position['dx'].expectUint(3613023403); + + // after swap, weight now halves. + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['weight-x-t'].expectUint(50040041); + + // implied price is now 0.14679128195479 + result = LBPTest.swapYForX(deployer, alexAddress, usdaAddress, expiry, ONE_8); + position = result.expectOk().expectTuple(); + position['dy'].expectUint(ONE_8); + position['dx'].expectUint(681239367); + + // Check pool details and print + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['total-supply'].expectUint(80274141756); + position['balance-x'].expectUint(95605241230); + position['balance-y'].expectUint(14311111111); + + // launch not going well, so withdraw liquidity + result = LBPTest.reducePosition(deployer, alexAddress, usdaAddress, expiry, poolTokenAddress, 0.5 * ONE_8); + position = result.expectOk().expectTuple(); + position['dx'].expectUint(47758469158); + position['dy'].expectUint(7148946541); + + // Check pool details and print + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['total-supply'].expectUint(40174141756); + position['balance-x'].expectUint(47846772072); + position['balance-y'].expectUint(7162164570); + + chain.mineEmptyBlockUntil(998); + + // no trades between blocks 500 and 998, so weight doesn't change, price doesn't change + // until swap occurs. + result = LBPTest.swapYForX(deployer, alexAddress, usdaAddress, expiry, ONE_8); + position = result.expectOk().expectTuple(); + position['dy'].expectUint(ONE_8); + position['dx'].expectUint(678596892); - // // swap some usda into wbtc - // result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, wbtcPrice*ONE_8); - // position = result.expectOk().expectTuple(); - // position['dx'].expectUint(103049813); - // position['dy'].expectUint(wbtcPrice*ONE_8); - - // // attempt to swap zero throws an error - // result = FWPTest.swapYForX(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); - // result.expectErr().expectUint(2003); - // result = FWPTest.swapXForY(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0); - // result.expectErr().expectUint(2003); + // and weight now is at min. + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['weight-x-t'].expectUint(10160161); + + // resulting in alex price falling below min-price, throwing error + result = LBPTest.swapYForX(deployer, alexAddress, usdaAddress, expiry, ONE_8); + position = result.expectErr().expectUint(2021); + + // all time passed + chain.mineEmptyBlockUntil(1001); + + // already expired + call = await LBPTest.getWeightX(alexAddress, usdaAddress, expiry); + call.result.expectErr().expectUint(2011); + + // Check pool details and print + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['total-supply'].expectUint(40174141756); + position['balance-x'].expectUint(47168175180); + position['balance-y'].expectUint(7262164570); + + // withdraw all remaining liquidity + result = LBPTest.reducePosition(deployer, alexAddress, usdaAddress, expiry, poolTokenAddress, ONE_8); + position = result.expectOk().expectTuple(); + position['dx'].expectUint(47081125841); + position['dy'].expectUint(7248762172); + + // Check pool details and print + call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + position = call.result.expectOk().expectTuple(); + position['total-supply'].expectUint(0); + position['balance-x'].expectUint(0); + position['balance-y'].expectUint(0); }, }); diff --git a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts index da40758b..bc65ee46 100644 --- a/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts +++ b/clarity/tests/models/alex-tests-liquidity-bootstrapping-pool.ts @@ -53,6 +53,22 @@ import { return block.receipts[0].result; } + getPriceRange(tokenX: string, tokenY: string, expiry: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-price-range", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry) + ], this.deployer.address); + } + + getWeightX(tokenX: string, tokenY: string, expiry: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-weight-x", [ + types.principal(tokenX), + types.principal(tokenY), + types.uint(expiry) + ], this.deployer.address); + } + setPoolMultisig(user: Account, tokenX: string, tokenY: string, expiry :number, new_multisig: string) { let block = this.chain.mineBlock([ Tx.contractCall("liquidity-bootstrapping-pool", "set-pool-multisig", [ @@ -78,18 +94,6 @@ import { return block.receipts[0].result; } - swapXForY(user: Account, tokenX: string, tokenY: string, expiry: number, dx: number) { - let block = this.chain.mineBlock([ - Tx.contractCall("liquidity-bootstrapping-pool", "swap-x-for-y", [ - types.principal(tokenX), - types.principal(tokenY), - types.uint(expiry), - types.uint(dx) - ], user.address), - ]); - return block.receipts[0].result; - } - swapYForX(user: Account, tokenX: string, tokenY: string, expiry: number, dy: number) { let block = this.chain.mineBlock([ Tx.contractCall("liquidity-bootstrapping-pool", "swap-y-for-x", [ @@ -102,21 +106,21 @@ import { return block.receipts[0].result; } - getXgivenPrice(tokenX: string, tokenY: string, expiry: number, price: number) { - return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-x-given-price", [ + getXgivenY(tokenX: string, tokenY: string, expiry: number, dy: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-x-given-y", [ types.principal(tokenX), types.principal(tokenY), types.uint(expiry), - types.uint(price) + types.uint(dy) ], this.deployer.address); } - getYgivenPrice(tokenX: string, tokenY: string, expiry: number, price: number) { - return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-y-given-price", [ + getYgivenX(tokenX: string, tokenY: string, expiry: number, dx: number) { + return this.chain.callReadOnlyFn("liquidity-bootstrapping-pool", "get-y-given-x", [ types.principal(tokenX), types.principal(tokenY), types.uint(expiry), - types.uint(price) + types.uint(dx) ], this.deployer.address); } From af7a68534b0f62661c42b8c6716d35b20e5f5807 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 27 Oct 2021 21:30:37 +0800 Subject: [PATCH 11/16] crp test cases updated to reflect the bug fixed in weighted-equations --- .../tests/collateral-rebalancing-pool_test.ts | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/clarity/tests/collateral-rebalancing-pool_test.ts b/clarity/tests/collateral-rebalancing-pool_test.ts index 65018401..54808249 100644 --- a/clarity/tests/collateral-rebalancing-pool_test.ts +++ b/clarity/tests/collateral-rebalancing-pool_test.ts @@ -110,30 +110,30 @@ Clarinet.test({ // arbtrageur selling 0.002 wbtc for usda result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.002 * ONE_8); position = result.expectOk().expectTuple(); - position['dx'].expectUint(22007078006); + position['dx'].expectUint(18384661200); position['dy'].expectUint(0.002 * ONE_8); // borrow $5,000 more and convert to wbtc // remember, the first sell creates profit to LP result = CRPTest.addToPositionAndSwitch(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, 5000 * ONE_8); position = result.expectOk().expectTuple(); - position['dy'].expectUint(8044367); - position['dx'].expectUint(8044549); + position['dy'].expectUint(8038536); + position['dx'].expectUint(8038686); // supply increased call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); - position['balance-x'].expectUint(3575918101994); + position['balance-x'].expectUint(3579540518800); position['balance-y'].expectUint(38322648); - position['yield-supply'].expectUint(88851727); - position['key-supply'].expectUint(88851727); + position['yield-supply'].expectUint(88845896); + position['key-supply'].expectUint(88845896); // pool value increases after adding positions call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(109841010); + call.result.expectOk().expectUint(109913458); call = await CRPTest.getPoolValueInCollateral(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(5492050501994) + call.result.expectOk().expectUint(5495672918800) // let's check what is the weight to wbtc (token) call = await CRPTest.getWeightY(wbtcAddress, usdaAddress, expiry, 50000 * ONE_8, bs_vol); @@ -154,15 +154,15 @@ Clarinet.test({ result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); - position['dy'].expectUint(116875); + position['dy'].expectUint(116756); // swap triggers weight change call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); position['weight-y'].expectUint(10056633); position['weight-x'].expectUint(ONE_8 - 10056633); - position['yield-supply'].expectUint(88851727); - position['key-supply'].expectUint(88851727); + position['yield-supply'].expectUint(88845896); + position['key-supply'].expectUint(88845896); // simulate to half way to expiry chain.mineEmptyBlockUntil((expiry / ONE_8) / 2) @@ -179,14 +179,14 @@ Clarinet.test({ result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); - position['dy'].expectUint(939812); + position['dy'].expectUint(938875); call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); position['weight-y'].expectUint(92358839); position['weight-x'].expectUint(ONE_8 - 92358839); - position['yield-supply'].expectUint(88851727); - position['key-supply'].expectUint(88851727); + position['yield-supply'].expectUint(88845896); + position['key-supply'].expectUint(88845896); // simulate to 3 / 4 to expiry chain.mineEmptyBlockUntil((expiry / ONE_8) * 3 / 4) @@ -197,11 +197,11 @@ Clarinet.test({ // we hold over 60% in usda, so pool value in wbtc goes down call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(91749568); + call.result.expectOk().expectUint(91805509); // and ltv crossed conversion_ltv call = await CRPTest.getLtv(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(96841575); + call.result.expectOk().expectUint(96776214); // crossing convertion_ltv triggers 100% allocation to collateral call = await CRPTest.getWeightY(wbtcAddress, usdaAddress, expiry, 50000 * ONE_8, bs_vol); @@ -211,17 +211,17 @@ Clarinet.test({ result = CRPTest.swapXForY(deployer, wbtcAddress, usdaAddress, expiry, 100 * ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(100 * ONE_8); - position['dy'].expectUint(8558); + position['dy'].expectUint(8550); // swap triggers weight change - once crossed conversion_ltv, all in token call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); position['weight-y'].expectUint(99900000); position['weight-x'].expectUint(ONE_8 - 99900000); - position['yield-supply'].expectUint(88851727); - position['key-supply'].expectUint(88851727); - position['balance-x'].expectUint(3605918101994); - position['balance-y'].expectUint(37257403); + position['yield-supply'].expectUint(88845896); + position['key-supply'].expectUint(88845896); + position['balance-x'].expectUint(3609540518800); + position['balance-y'].expectUint(37258467); // simulate to expiry chain.mineEmptyBlockUntil((expiry / ONE_8)) @@ -242,16 +242,16 @@ Clarinet.test({ call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); - position['balance-x'].expectUint(3605918101994); - position['balance-y'].expectUint(37257403); - position['yield-supply'].expectUint(88851727); - position['key-supply'].expectUint(88851727); + position['balance-x'].expectUint(3609540518800); + position['balance-y'].expectUint(37258467); + position['yield-supply'].expectUint(88845896); + position['key-supply'].expectUint(88845896); call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(91892525); + call.result.expectOk().expectUint(91948474); call = await CRPTest.getLtv(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(96690919); + call.result.expectOk().expectUint(96625742); // deployer holds less than total supply because he sold some yield-wbtc for wbtc result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, ONE_8); @@ -263,39 +263,39 @@ Clarinet.test({ call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); position['balance-x'].expectUint(0); - position['balance-y'].expectUint(9899567); - position['yield-supply'].expectUint(8044367); - position['key-supply'].expectUint(88851727); + position['balance-y'].expectUint(9899726); + position['yield-supply'].expectUint(8038536); + position['key-supply'].expectUint(88845896); call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); - call.result.expectOk().expectUint(9899567); + call.result.expectOk().expectUint(9899726); call = await CRPTest.getPositionGivenBurnKey(wbtcAddress, usdaAddress, expiry, ONE_8); position = call.result.expectOk().expectTuple(); position['dx'].expectUint(0); - position['dy'].expectUint(2087972); + position['dy'].expectUint(2094851); call = chain.callReadOnlyFn("key-wbtc-59760-usda", "get-balance", [types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(88851727); + call.result.expectOk().expectUint(88845896); call = chain.callReadOnlyFn("key-wbtc-59760-usda", "get-total-supply", [], deployer.address); - call.result.expectOk().expectUint(88851727); + call.result.expectOk().expectUint(88845896); // also remove all key tokens result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, keywbtc59760Address, ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(0); - position['dy'].expectUint(1855199); + position['dy'].expectUint(1861189); call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); - position['yield-supply'].expectUint(8044367); + position['yield-supply'].expectUint(8038536); position['key-supply'].expectUint(0); position['balance-x'].expectUint(0); - position['balance-y'].expectUint(8044368); + position['balance-y'].expectUint(8038537); }, }); @@ -568,7 +568,7 @@ Clarinet.test({ call.result.expectOk().expectUint(1223378); result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 1223378); position = result.expectOk().expectTuple(); - position['dx'].expectUint(256645012356); + position['dx'].expectUint(76010081782); position['dy'].expectUint(1223378); }, }); @@ -724,7 +724,7 @@ Clarinet.test({ // arbtrageur attepmts to swap small value result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.000001 * ONE_8); position = result.expectOk().expectTuple(); - position['dx'].expectUint(10545754); + position['dx'].expectUint(8782583); position['dy'].expectUint(0.000001 * ONE_8); // arbtrageur attepmts to swap in full value @@ -734,7 +734,7 @@ Clarinet.test({ // arbtrageur selling 0.01 wbtc for usda result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.01 * ONE_8); position = result.expectOk().expectTuple(); - position['dx'].expectUint(111855897259); + position['dx'].expectUint(93241485590); position['dy'].expectUint(0.01 * ONE_8); // simulate to expiry + 1 @@ -785,7 +785,7 @@ Clarinet.test({ result.expectErr().expectUint(2001); result = await CRPTest.getXgivenY(deployer, wbtcAddress, usdaAddress, expiry, 500); - result.expectOk().expectUint(98205150); + result.expectOk().expectUint(24684357); result = await CRPTest.getXgivenY(deployer, wbtcAddress, usdaAddress, expiry, 0); result.expectOk().expectUint(0); From 5b07f87e01cce780126a8eef60418f88a3ad536f Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 27 Oct 2021 22:54:02 +0800 Subject: [PATCH 12/16] new active contracts added --- ...lash-loan-user-margin-usda-wbtc-23040.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-34560.clar | 29 -- ...flash-loan-user-margin-usda-wbtc-5760.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-23040.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-34560.clar | 29 -- ...flash-loan-user-margin-wbtc-usda-5760.clar | 29 -- .../key-token/key-usda-23040-wbtc.clar | 107 ------- .../key-token/key-usda-34560-wbtc.clar | 107 ------- .../key-token/key-usda-5760-wbtc.clar | 107 ------- .../key-token/key-wbtc-23040-usda.clar | 107 ------- .../key-token/key-wbtc-34560-usda.clar | 107 ------- .../key-token/key-wbtc-5760-usda.clar | 107 ------- .../multisig-crp-usda-23040-wbtc.clar | 277 ------------------ .../multisig-crp-usda-34560-wbtc.clar | 277 ------------------ .../multisig/multisig-crp-usda-5760-wbtc.clar | 277 ------------------ .../multisig-crp-wbtc-23040-usda.clar | 277 ------------------ .../multisig-crp-wbtc-34560-usda.clar | 277 ------------------ .../multisig/multisig-crp-wbtc-5760-usda.clar | 277 ------------------ .../multisig-ytp-yield-usda-23040-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-34560-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-5760-usda.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-23040-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-34560-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-5760-wbtc.clar | 272 ----------------- .../pool-token/ytp-yield-usda-23040-usda.clar | 97 ------ .../pool-token/ytp-yield-usda-34560-usda.clar | 97 ------ .../pool-token/ytp-yield-usda-5760-usda.clar | 97 ------ .../pool-token/ytp-yield-wbtc-23040-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-34560-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-5760-wbtc.clar | 97 ------ .../yield-token/yield-usda-23040.clar | 112 ------- .../yield-token/yield-usda-34560.clar | 112 ------- .../yield-token/yield-usda-5760.clar | 112 ------- .../yield-token/yield-wbtc-23040.clar | 112 ------- .../yield-token/yield-wbtc-34560.clar | 112 ------- .../yield-token/yield-wbtc-5760.clar | 112 ------- init-js-tool/deploy.js | 209 ++++--------- 37 files changed, 61 insertions(+), 5512 deletions(-) delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar delete mode 100644 clarity/contracts/key-token/key-usda-23040-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-34560-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-5760-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-23040-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-34560-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-5760-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-34560-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-5760-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-34560-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-5760-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-34560-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-5760-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-34560-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-5760-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-34560-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-5760-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-34560-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-5760-wbtc.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-23040.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-34560.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-5760.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-23040.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-34560.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-5760.clar diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar deleted file mode 100644 index d3e1127e..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-wbtc-23040 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-23040))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-23040 .key-wbtc-23040-usda gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-23040", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar deleted file mode 100644 index d629094a..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-34560.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-wbtc-34560 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-34560))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-34560 .key-wbtc-34560-usda gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-34560", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar deleted file mode 100644 index cb16c2bc..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-5760.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-wbtc-5760 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-5760))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-5760 .key-wbtc-5760-usda gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-5760", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar deleted file mode 100644 index c9b6d214..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-usda-23040 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-usda-23040))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-23040 .key-usda-23040-wbtc gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-23040", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar deleted file mode 100644 index 395ada19..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-34560.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-usda-34560 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-usda-34560))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-34560 .key-usda-34560-wbtc gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-34560", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar deleted file mode 100644 index 950edb8c..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-5760.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-usda-5760 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-usda-5760))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-5760 .key-usda-5760-wbtc gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-5760", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-23040-wbtc.clar b/clarity/contracts/key-token/key-usda-23040-wbtc.clar deleted file mode 100644 index f47610c4..00000000 --- a/clarity/contracts/key-token/key-usda-23040-wbtc.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-usda-23040-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u2304000000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-usda-23040-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-23040-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-23040-wbtc") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-usda-23040-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-usda-23040-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-usda-23040-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-usda-23040-wbtc (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-34560-wbtc.clar b/clarity/contracts/key-token/key-usda-34560-wbtc.clar deleted file mode 100644 index 5e68ca58..00000000 --- a/clarity/contracts/key-token/key-usda-34560-wbtc.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-usda-34560-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u3456000000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-usda-34560-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-34560-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-34560-wbtc") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-usda-34560-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-usda-34560-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-usda-34560-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-usda-34560-wbtc (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-5760-wbtc.clar b/clarity/contracts/key-token/key-usda-5760-wbtc.clar deleted file mode 100644 index 7931c332..00000000 --- a/clarity/contracts/key-token/key-usda-5760-wbtc.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-usda-5760-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u576000000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-usda-5760-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-5760-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-5760-wbtc") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-usda-5760-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-usda-5760-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-usda-5760-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-usda-5760-wbtc (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-23040-usda.clar b/clarity/contracts/key-token/key-wbtc-23040-usda.clar deleted file mode 100644 index d2f07647..00000000 --- a/clarity/contracts/key-token/key-wbtc-23040-usda.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-wbtc-23040-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u2304000000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-wbtc-23040-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-23040-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-23040-usda") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-wbtc-23040-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-wbtc-23040-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-wbtc-23040-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-wbtc-23040-usda (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-34560-usda.clar b/clarity/contracts/key-token/key-wbtc-34560-usda.clar deleted file mode 100644 index ff7ba9b1..00000000 --- a/clarity/contracts/key-token/key-wbtc-34560-usda.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-wbtc-34560-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u3456000000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-wbtc-34560-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-34560-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-34560-usda") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-wbtc-34560-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-wbtc-34560-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-wbtc-34560-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-wbtc-34560-usda (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-5760-usda.clar b/clarity/contracts/key-token/key-wbtc-5760-usda.clar deleted file mode 100644 index d76dba3a..00000000 --- a/clarity/contracts/key-token/key-wbtc-5760-usda.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-wbtc-5760-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u576000000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-wbtc-5760-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-5760-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-5760-usda") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-wbtc-5760-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-wbtc-5760-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-wbtc-5760-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-wbtc-5760-usda (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar deleted file mode 100644 index dd1f503d..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-usda-23040) (is-eq (contract-of token) .key-usda-23040-wbtc)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-23040 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-23040-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-23040 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-23040-wbtc get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-23040 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-23040-wbtc get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u2304000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u2304000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-usda-34560-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-34560-wbtc.clar deleted file mode 100644 index bc39e8b0..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-34560-wbtc.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-usda-34560) (is-eq (contract-of token) .key-usda-34560-wbtc)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-34560 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-34560-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-34560 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-34560-wbtc get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-34560 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-34560-wbtc get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u3456000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u3456000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-usda-5760-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-5760-wbtc.clar deleted file mode 100644 index 5c5037aa..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-5760-wbtc.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-usda-5760) (is-eq (contract-of token) .key-usda-5760-wbtc)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-5760 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-5760-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-5760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-5760-wbtc get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-5760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-5760-wbtc get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u576000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u576000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar deleted file mode 100644 index cea5fa52..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-wbtc-23040) (is-eq (contract-of token) .key-wbtc-23040-usda)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-23040 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-23040-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-23040 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-23040-usda get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-23040 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-23040-usda get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u2304000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u2304000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-34560-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-34560-usda.clar deleted file mode 100644 index 57376d9f..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-34560-usda.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-wbtc-34560) (is-eq (contract-of token) .key-wbtc-34560-usda)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-34560 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-34560-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-34560 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-34560-usda get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-34560 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-34560-usda get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u3456000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u3456000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-5760-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-5760-usda.clar deleted file mode 100644 index 9d086b16..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-5760-usda.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-wbtc-5760) (is-eq (contract-of token) .key-wbtc-5760-usda)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-5760 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-5760-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-5760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-5760-usda get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-5760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-5760-usda get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u576000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u576000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar deleted file mode 100644 index 1408cf3f..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-usda-23040-usda) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-23040-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-23040-usda get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-23040-usda get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-23040 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-23040 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-34560-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-34560-usda.clar deleted file mode 100644 index 7b346d7a..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-34560-usda.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-usda-34560-usda) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-34560-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-34560-usda get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-34560-usda get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-34560 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-34560 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-5760-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-5760-usda.clar deleted file mode 100644 index 28a492ca..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-5760-usda.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-usda-5760-usda) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-5760-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-5760-usda get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-5760-usda get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-5760 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-5760 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar deleted file mode 100644 index c749fbab..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-wbtc-23040-wbtc) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23040-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23040-wbtc get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23040-wbtc get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-23040 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-23040 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-34560-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-34560-wbtc.clar deleted file mode 100644 index d0cdfe78..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-34560-wbtc.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-wbtc-34560-wbtc) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-34560-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-34560-wbtc get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-34560-wbtc get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-34560 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-34560 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-5760-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-5760-wbtc.clar deleted file mode 100644 index ba5ce550..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-5760-wbtc.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-wbtc-5760-wbtc) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-5760-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-5760-wbtc get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-5760-wbtc get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-5760 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-5760 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar deleted file mode 100644 index 362f3084..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-usda-23040-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-23040-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-23040-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-23040-usda") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-23040-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-usda-23040-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-usda-23040-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-usda-23040-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-34560-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-34560-usda.clar deleted file mode 100644 index b137c16a..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-34560-usda.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-usda-34560-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-34560-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-34560-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-34560-usda") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-34560-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-usda-34560-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-usda-34560-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-usda-34560-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-5760-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-5760-usda.clar deleted file mode 100644 index 9d3a88a7..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-5760-usda.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-usda-5760-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-5760-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-5760-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-5760-usda") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-5760-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-usda-5760-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-usda-5760-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-usda-5760-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar deleted file mode 100644 index 69522189..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-wbtc-23040-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-23040-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-23040-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-23040-wbtc") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-23040-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-wbtc-23040-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-wbtc-23040-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-wbtc-23040-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-34560-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-34560-wbtc.clar deleted file mode 100644 index bad105cb..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-34560-wbtc.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-wbtc-34560-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-34560-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-34560-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-34560-wbtc") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-34560-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-wbtc-34560-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-wbtc-34560-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-wbtc-34560-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-5760-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-5760-wbtc.clar deleted file mode 100644 index 6fd44327..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-5760-wbtc.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-wbtc-5760-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-5760-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-5760-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-5760-wbtc") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-5760-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-wbtc-5760-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-wbtc-5760-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-wbtc-5760-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-23040.clar b/clarity/contracts/yield-token/yield-usda-23040.clar deleted file mode 100644 index f6643c65..00000000 --- a/clarity/contracts/yield-token/yield-usda-23040.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-usda-23040) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u2304000000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-usda-23040))) -) - -(define-read-only (get-name) - (ok "yield-usda-23040") -) - -(define-read-only (get-symbol) - (ok "yield-usda-23040") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-usda-23040 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-usda-23040 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-usda-23040 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-usda-23040 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-usda-23040 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-34560.clar b/clarity/contracts/yield-token/yield-usda-34560.clar deleted file mode 100644 index 33dc6a13..00000000 --- a/clarity/contracts/yield-token/yield-usda-34560.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-usda-34560) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u3456000000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-usda-34560))) -) - -(define-read-only (get-name) - (ok "yield-usda-34560") -) - -(define-read-only (get-symbol) - (ok "yield-usda-34560") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-usda-34560 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-usda-34560 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-usda-34560 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-usda-34560 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-usda-34560 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-5760.clar b/clarity/contracts/yield-token/yield-usda-5760.clar deleted file mode 100644 index b696c0b0..00000000 --- a/clarity/contracts/yield-token/yield-usda-5760.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-usda-5760) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u576000000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-usda-5760))) -) - -(define-read-only (get-name) - (ok "yield-usda-5760") -) - -(define-read-only (get-symbol) - (ok "yield-usda-5760") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-usda-5760 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-usda-5760 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-usda-5760 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-usda-5760 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-usda-5760 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-23040.clar b/clarity/contracts/yield-token/yield-wbtc-23040.clar deleted file mode 100644 index 6335989d..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-23040.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-wbtc-23040) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u2304000000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-wbtc-23040))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-23040") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-23040") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-wbtc-23040 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-wbtc-23040 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-wbtc-23040 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-wbtc-23040 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-wbtc-23040 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-34560.clar b/clarity/contracts/yield-token/yield-wbtc-34560.clar deleted file mode 100644 index b0ac7876..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-34560.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-wbtc-34560) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u3456000000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-wbtc-34560))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-34560") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-34560") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-wbtc-34560 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-wbtc-34560 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-wbtc-34560 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-wbtc-34560 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-wbtc-34560 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-5760.clar b/clarity/contracts/yield-token/yield-wbtc-5760.clar deleted file mode 100644 index c8d1b274..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-5760.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-wbtc-5760) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u576000000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-wbtc-5760))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-5760") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-5760") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-wbtc-5760 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-wbtc-5760 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-wbtc-5760 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-wbtc-5760 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-wbtc-5760 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/init-js-tool/deploy.js b/init-js-tool/deploy.js index 1907a7ca..60c12651 100644 --- a/init-js-tool/deploy.js +++ b/init-js-tool/deploy.js @@ -11,158 +11,71 @@ const { exit } = require('process'); let contract_records = {"Contracts":[]} let VERSION; let contract_paths = [ - // "lib/math-log-exp.clar", - // "lib/math-fixed-point.clar", - // "traits/trait-sip-010.clar", - // "traits/trait-flash-loan-user.clar", - // "traits/trait-oracle.clar", - // "traits/trait-pool-token.clar", - // "traits/trait-yield-token.clar", - // "traits/trait-ownable.clar", - // "traits/trait-vault.clar", - // "traits/trait-multisig-vote.clar", - // "equations/weighted-equation.clar", - // "equations/yield-token-equation.clar", - // "token/token-alex.clar", - // "token/token-usda.clar", - // "token/token-wbtc.clar", - // "alex-vault.clar", - // "open-oracle.clar", - // "pool/alex-reserve-pool.clar", - // "pool/fixed-weight-pool.clar", - // "pool/liquidity-bootstrapping-pool.clar", - // "pool/yield-token-pool.clar", - // "pool/collateral-rebalancing-pool.clar", - // "faucet.clar", - // "pool-token/fwp-wbtc-usda-50-50.clar", - // "multisig/multisig-fwp-wbtc-usda-50-50.clar", + "lib/math-log-exp.clar", + "lib/math-fixed-point.clar", + "traits/trait-sip-010.clar", + "traits/trait-flash-loan-user.clar", + "traits/trait-oracle.clar", + "traits/trait-pool-token.clar", + "traits/trait-yield-token.clar", + "traits/trait-ownable.clar", + "traits/trait-vault.clar", + "traits/trait-multisig-vote.clar", + "equations/weighted-equation.clar", + "equations/yield-token-equation.clar", + "token/token-alex.clar", + "token/token-usda.clar", + "token/token-wbtc.clar", + "alex-vault.clar", + "open-oracle.clar", + "pool/alex-reserve-pool.clar", + "pool/fixed-weight-pool.clar", + "pool/liquidity-bootstrapping-pool.clar", + "pool/yield-token-pool.clar", + "pool/collateral-rebalancing-pool.clar", + "faucet.clar", + "pool-token/fwp-wbtc-usda-50-50.clar", + "multisig/multisig-fwp-wbtc-usda-50-50.clar", - // "yield-token/yield-wbtc-40555.clar", - // "yield-token/yield-usda-40555.clar", - // "key-token/key-usda-40555-wbtc.clar", - // "key-token/key-wbtc-40555-usda.clar", - // "pool-token/ytp-yield-wbtc-40555-wbtc.clar", - // "pool-token/ytp-yield-usda-40555-usda.clar", - // "multisig/multisig-crp-wbtc-40555-usda.clar", - // "multisig/multisig-crp-usda-40555-wbtc.clar", - // "multisig/multisig-ytp-yield-wbtc-40555-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-40555-usda.clar", - // "flash-loan-user-margin-usda-wbtc-40555.clar", - // "flash-loan-user-margin-wbtc-usda-40555.clar", + "yield-token/yield-wbtc-11520.clar", + "yield-token/yield-usda-11520.clar", + "key-token/key-usda-11520-wbtc.clar", + "key-token/key-wbtc-11520-usda.clar", + "pool-token/ytp-yield-wbtc-11520-wbtc.clar", + "pool-token/ytp-yield-usda-11520-usda.clar", + "multisig/multisig-crp-wbtc-11520-usda.clar", + "multisig/multisig-crp-usda-11520-wbtc.clar", + "multisig/multisig-ytp-yield-wbtc-11520-wbtc.clar", + "multisig/multisig-ytp-yield-usda-11520-usda.clar", + "flash-loan-user-margin-usda-wbtc-11520.clar", + "flash-loan-user-margin-wbtc-usda-11520.clar", - // "yield-token/yield-wbtc-5760.clar", - // "yield-token/yield-usda-5760.clar", - // "key-token/key-usda-5760-wbtc.clar", - // "key-token/key-wbtc-5760-usda.clar", - // "pool-token/ytp-yield-wbtc-5760-wbtc.clar", - // "pool-token/ytp-yield-usda-5760-usda.clar", - // "multisig/multisig-crp-wbtc-5760-usda.clar", - // "multisig/multisig-crp-usda-5760-wbtc.clar", - // "multisig/multisig-ytp-yield-wbtc-5760-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-5760-usda.clar", - // "flash-loan-user-margin-usda-wbtc-5760.clar", - // "flash-loan-user-margin-wbtc-usda-5760.clar", + "yield-token/yield-wbtc-51840.clar", + "yield-token/yield-usda-51840.clar", + "key-token/key-usda-51840-wbtc.clar", + "key-token/key-wbtc-51840-usda.clar", + "pool-token/ytp-yield-wbtc-51840-wbtc.clar", + "pool-token/ytp-yield-usda-51840-usda.clar", + "multisig/multisig-crp-wbtc-51840-usda.clar", + "multisig/multisig-crp-usda-51840-wbtc.clar", + "multisig/multisig-ytp-yield-wbtc-51840-wbtc.clar", + "multisig/multisig-ytp-yield-usda-51840-usda.clar", + "flash-loan-user-margin-usda-wbtc-51840.clar", + "flash-loan-user-margin-wbtc-usda-51840.clar", - // "yield-token/yield-wbtc-80875.clar", - // "yield-token/yield-usda-80875.clar", - // "key-token/key-usda-80875-wbtc.clar", - // "key-token/key-wbtc-80875-usda.clar", - // "pool-token/ytp-yield-wbtc-80875-wbtc.clar", - // "pool-token/ytp-yield-usda-80875-usda.clar", - // "multisig/multisig-crp-wbtc-80875-usda.clar", - // "multisig/multisig-crp-usda-80875-wbtc.clar", - // "multisig/multisig-ytp-yield-wbtc-80875-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-80875-usda.clar", - "flash-loan-user-margin-usda-wbtc-80875.clar", - "flash-loan-user-margin-wbtc-usda-80875.clar", - - // "pool-token/ytp-yield-wbtc-23040-wbtc.clar", - // "yield-token/yield-wbtc-23040.clar", - // "key-token/key-wbtc-23040-usda.clar", - // "multisig/multisig-crp-wbtc-23040-usda.clar", - // "multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar", - // "flash-loan-user-margin-usda-wbtc-23040.clar", - // "pool-token/ytp-yield-usda-23040-usda.clar", - // "yield-token/yield-usda-23040.clar", - // "key-token/key-usda-23040-wbtc.clar", - // "multisig/multisig-crp-usda-23040-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-23040-usda.clar", - // "flash-loan-user-margin-wbtc-usda-23040.clar", - - // "pool-token/ytp-yield-wbtc-34560-wbtc.clar", - // "yield-token/yield-wbtc-34560.clar", - // "key-token/key-wbtc-34560-usda.clar", - // "multisig/multisig-crp-wbtc-34560-usda.clar", - // "multisig/multisig-ytp-yield-wbtc-34560-wbtc.clar", - // "flash-loan-user-margin-usda-wbtc-34560.clar", - // "pool-token/ytp-yield-usda-34560-usda.clar", - // "yield-token/yield-usda-34560.clar", - // "key-token/key-usda-34560-wbtc.clar", - // "multisig/multisig-crp-usda-34560-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-34560-usda.clar", - // "flash-loan-user-margin-wbtc-usda-34560.clar", - - // "pool-token/ytp-yield-wbtc-121195-wbtc.clar", - // "yield-token/yield-wbtc-121195.clar", - // "key-token/key-wbtc-121195-usda.clar", - // "multisig/multisig-crp-wbtc-121195-usda.clar", - // "multisig/multisig-ytp-yield-wbtc-121195-wbtc.clar", - "flash-loan-user-margin-usda-wbtc-121195.clar", - // "pool-token/ytp-yield-usda-121195-usda.clar", - // "key-token/key-usda-121195-wbtc.clar", - // "yield-token/yield-usda-121195.clar", - // "multisig/multisig-crp-usda-121195-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-121195-usda.clar", - "flash-loan-user-margin-wbtc-usda-121195.clar", - - // "pool-token/ytp-yield-wbtc-161515-wbtc.clar", - // "yield-token/yield-wbtc-161515.clar", - // "key-token/key-wbtc-161515-usda.clar", - // "multisig/multisig-crp-wbtc-161515-usda.clar", - // "multisig/multisig-ytp-yield-wbtc-161515-wbtc.clar", -<<<<<<< HEAD - "flash-loan-user-margin-usda-wbtc-161515.clar", -======= - // "flash-loan-user-margin-usda-wbtc-161515.clar", ->>>>>>> dev - // "pool-token/ytp-yield-usda-161515-usda.clar", - // "key-token/key-usda-161515-wbtc.clar", - // "yield-token/yield-usda-161515.clar", - // "multisig/multisig-crp-usda-161515-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-161515-usda.clar", -<<<<<<< HEAD - "flash-loan-user-margin-wbtc-usda-161515.clar", -] -======= - // "flash-loan-user-margin-wbtc-usda-161515.clar", - - // "pool-token/ytp-yield-wbtc-200335-wbtc.clar", - // "yield-token/yield-wbtc-200335.clar", - // "key-token/key-wbtc-200335-usda.clar", - // "multisig/multisig-crp-wbtc-200335-usda.clar", - // "multisig/multisig-ytp-yield-wbtc-200335-wbtc.clar", - // "flash-loan-user-margin-usda-wbtc-200335.clar", - // "pool-token/ytp-yield-usda-200335-usda.clar", - // "key-token/key-usda-200335-wbtc.clar", - // "yield-token/yield-usda-200335.clar", - // "multisig/multisig-crp-usda-200335-wbtc.clar", - // "multisig/multisig-ytp-yield-usda-200335-usda.clar", - // "flash-loan-user-margin-wbtc-usda-200335.clar", ->>>>>>> dev - - "pool-token/ytp-yield-wbtc-240655-wbtc.clar", - "yield-token/yield-wbtc-240655.clar", - "key-token/key-wbtc-240655-usda.clar", - "multisig/multisig-crp-wbtc-240655-usda.clar", - "multisig/multisig-ytp-yield-wbtc-240655-wbtc.clar", - "flash-loan-user-margin-usda-wbtc-240655.clar", - "pool-token/ytp-yield-usda-240655-usda.clar", - "key-token/key-usda-240655-wbtc.clar", - "yield-token/yield-usda-240655.clar", - "multisig/multisig-crp-usda-240655-wbtc.clar", - "multisig/multisig-ytp-yield-usda-240655-usda.clar", - "flash-loan-user-margin-wbtc-usda-240655.clar", + "yield-token/yield-wbtc-92160.clar", + "yield-token/yield-usda-92160.clar", + "key-token/key-usda-92160-wbtc.clar", + "key-token/key-wbtc-92160-usda.clar", + "pool-token/ytp-yield-wbtc-92160-wbtc.clar", + "pool-token/ytp-yield-usda-92160-usda.clar", + "multisig/multisig-crp-wbtc-92160-usda.clar", + "multisig/multisig-crp-usda-92160-wbtc.clar", + "multisig/multisig-ytp-yield-wbtc-92160-wbtc.clar", + "multisig/multisig-ytp-yield-usda-92160-usda.clar", + "flash-loan-user-margin-usda-wbtc-92160.clar", + "flash-loan-user-margin-wbtc-usda-92160.clar" ] async function get_version(){ From 441c69177a9554775c1b2d569767a9c71a2b61fb Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 27 Oct 2021 23:08:43 +0800 Subject: [PATCH 13/16] 23040 added back as it breaks tests --- ...lash-loan-user-margin-usda-wbtc-40555.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-40555.clar | 29 -- .../key-token/key-usda-40555-wbtc.clar | 107 ------- .../key-token/key-wbtc-40555-usda.clar | 107 ------- .../multisig-crp-usda-40555-wbtc.clar | 277 ------------------ .../multisig-crp-wbtc-40555-usda.clar | 277 ------------------ .../multisig-ytp-yield-usda-40555-usda.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-40555-wbtc.clar | 272 ----------------- .../pool-token/ytp-yield-usda-40555-usda.clar | 97 ------ .../pool-token/ytp-yield-wbtc-40555-wbtc.clar | 97 ------ .../yield-token/yield-usda-40555.clar | 112 ------- .../yield-token/yield-wbtc-40555.clar | 112 ------- 12 files changed, 1788 deletions(-) delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-40555.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-40555.clar delete mode 100644 clarity/contracts/key-token/key-usda-40555-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-40555-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-40555-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-40555-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-40555-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-40555-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-40555-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-40555-wbtc.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-40555.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-40555.clar diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-40555.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-40555.clar deleted file mode 100644 index 6cf9c53d..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-40555.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-wbtc-40555 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-40555))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-40555 .key-wbtc-40555-usda gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-40555", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-40555.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-40555.clar deleted file mode 100644 index 1d496ce0..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-40555.clar +++ /dev/null @@ -1,29 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant math-call-err (err u2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint)) - (let - ( - ;; gross amount * ltv / price = amount - ;; gross amount = amount * price / ltv - (expiry (unwrap! (contract-call? .yield-usda-40555 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) - (price (try! (contract-call? .yield-token-pool get-price .yield-usda-40555))) - (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-40555 .key-usda-40555-wbtc gross-amount)))) - ) - ;; swap token to collateral so we can return flash-loan - (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-40555", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-40555-wbtc.clar b/clarity/contracts/key-token/key-usda-40555-wbtc.clar deleted file mode 100644 index 07c6e9e4..00000000 --- a/clarity/contracts/key-token/key-usda-40555-wbtc.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-usda-40555-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u4055500000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-usda-40555-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-40555-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-40555-wbtc") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-usda-40555-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-usda-40555-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-usda-40555-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-usda-40555-wbtc (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-40555-usda.clar b/clarity/contracts/key-token/key-wbtc-40555-usda.clar deleted file mode 100644 index a3bb803b..00000000 --- a/clarity/contracts/key-token/key-wbtc-40555-usda.clar +++ /dev/null @@ -1,107 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-wbtc-40555-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u4055500000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply key-wbtc-40555-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-40555-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-40555-usda") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-wbtc-40555-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? key-wbtc-40555-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? key-wbtc-40555-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? key-wbtc-40555-usda (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-usda-40555-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-40555-wbtc.clar deleted file mode 100644 index 984fa8ef..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-40555-wbtc.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-usda-40555) (is-eq (contract-of token) .key-usda-40555-wbtc)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-40555 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-40555-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-40555 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-40555-wbtc get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-40555 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-40555-wbtc get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u4055500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u4055500000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-40555-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-40555-usda.clar deleted file mode 100644 index ffa7b8bb..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-40555-usda.clar +++ /dev/null @@ -1,277 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-x: uint, - new-fee-rate-y: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: u0, ;; Default token feerate - new-fee-rate-y: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (or (is-eq (contract-of token) .yield-wbtc-40555) (is-eq (contract-of token) .key-wbtc-40555-usda)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-x uint) - (new-fee-rate-y uint) - ) - (let - ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-40555 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-40555-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-40555 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-40555-usda get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-x: new-fee-rate-x, - new-fee-rate-y: new-fee-rate-y - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-40555 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-40555-usda get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-x (get new-fee-rate-x proposal)) - (new-fee-rate-y (get new-fee-rate-y proposal)) - ) - - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u4055500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u4055500000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-40555-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-40555-usda.clar deleted file mode 100644 index dd569b38..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-40555-usda.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-usda-40555-usda) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-40555-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-40555-usda get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-40555-usda get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-40555 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-40555 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-40555-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-40555-wbtc.clar deleted file mode 100644 index 0935f96d..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-40555-wbtc.clar +++ /dev/null @@ -1,272 +0,0 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - - -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. - -;; Voting can be done by locking up the corresponding pool token. -;; This prototype is for ayusda-usda pool token. -;; Common Trait and for each pool, implementation is required. -;; - -;; Errors -(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) -(define-constant ERR-NO-FEE-CHANGE (err u8001)) -(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) -(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) -(define-constant ERR-NOT-AUTHORIZED (err u1000)) -(define-constant ERR-MATH-CALL (err u2010)) - -(define-constant ONE_8 u100000000) - -;; Constants -(define-constant DEFAULT_OWNER tx-sender) - -;; Proposal variables -;; With Vote, we can set : -;; 1. contract to have right to mint/burn token -;; 2. Set Feerate / Fee address / Collect Fees -(define-map proposals - { id: uint } - { - id: uint, - proposer: principal, - title: (string-utf8 256), - url: (string-utf8 256), - is-open: bool, - start-block-height: uint, - end-block-height: uint, - yes-votes: uint, - no-votes: uint, - new-fee-rate-token: uint, - new-fee-rate-aytoken: uint - } -) - -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% - -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) - -(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) -(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) - -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) -) - -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) -) - -;; Get votes for a member on proposal -(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) - (default-to - { vote-count: u0 } - (map-get? votes-by-member { proposal-id: proposal-id, member: member }) - ) -) - -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - title: u"", - url: u"", - is-open: false, - start-block-height: u0, - end-block-height: u0, - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: u0, ;; Default token feerate - new-fee-rate-aytoken: u0 ;; default aytoken feerate - } - (map-get? proposals { id: proposal-id }) - ) -) - -;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. -(define-read-only (is-token-accepted (token )) - (is-eq (contract-of token) .ytp-yield-wbtc-40555-wbtc) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-aytoken uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-40555-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-40555-wbtc get-total-supply)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) - - ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) - ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - title: title, - url: url, - is-open: true, - start-block-height: start-block-height, - end-block-height: (+ start-block-height u1440), - yes-votes: u0, - no-votes: u0, - new-fee-rate-token: new-fee-rate-token, - new-fee-rate-aytoken: new-fee-rate-aytoken - } - ) - (var-set proposal-count proposal-id) - (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) - (ok proposal-id) - ) -) - -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - - ) - - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) - (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) - ) - ;; Can vote with corresponding pool token - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - ;; Proposal should be open for voting - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - ;; Vote should be casted after the start-block-height - (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) - - ;; Mutate - (map-set proposals - { id: proposal-id } - (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) - (map-set votes-by-member - { proposal-id: proposal-id, member: tx-sender } - { vote-count: (+ amount vote-count) }) - (map-set tokens-by-member - { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let ((proposal (get-proposal-by-id proposal-id)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-40555-wbtc get-total-supply)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) - - (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id - (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) - - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) - - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) -) - -;; Return votes to voter(member) -;; This function needs to be called for all members -(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) - (let - ( - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) - (proposal (get-proposal-by-id proposal-id)) - ) - - (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) - (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) - (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) - - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-40555 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-40555 new-fee-rate-aytoken)) - - (ok true) - ) -) diff --git a/clarity/contracts/pool-token/ytp-yield-usda-40555-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-40555-usda.clar deleted file mode 100644 index ae2d9fa2..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-40555-usda.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-usda-40555-usda) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-40555-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-40555-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-40555-usda") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-40555-usda account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-usda-40555-usda (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-usda-40555-usda (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-usda-40555-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-40555-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-40555-wbtc.clar deleted file mode 100644 index 61b043d5..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-40555-wbtc.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token ytp-yield-wbtc-40555-wbtc) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .yield-token-pool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-40555-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-40555-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-40555-wbtc") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-40555-wbtc account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? ytp-yield-wbtc-40555-wbtc (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? ytp-yield-wbtc-40555-wbtc (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? ytp-yield-wbtc-40555-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-40555.clar b/clarity/contracts/yield-token/yield-usda-40555.clar deleted file mode 100644 index 66707fec..00000000 --- a/clarity/contracts/yield-token/yield-usda-40555.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-usda-40555) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u4055500000000) -(define-data-var underlying-token principal .token-usda) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-usda-40555))) -) - -(define-read-only (get-name) - (ok "yield-usda-40555") -) - -(define-read-only (get-symbol) - (ok "yield-usda-40555") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-usda get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-usda-40555 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-usda-40555 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-usda-40555 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-usda-40555 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-usda-40555 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-40555.clar b/clarity/contracts/yield-token/yield-wbtc-40555.clar deleted file mode 100644 index e7939648..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-40555.clar +++ /dev/null @@ -1,112 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-wbtc-40555) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u4055500000000) -(define-data-var underlying-token principal .token-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) -) - -(define-read-only (fixed-to-decimals (amount uint)) - (/ (* amount (pow-decimals)) ONE_8) -) - -(define-private (decimals-to-fixed (amount uint)) - (/ (* amount ONE_8) (pow-decimals)) -) - -(define-read-only (get-owner) - (ok (var-get contract-owner)) -) - -(define-public (set-owner (owner principal)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set contract-owner owner)) - ) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply yield-wbtc-40555))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-40555") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-40555") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance yield-wbtc-40555 account))) -) - -(define-public (set-token-uri (value (string-utf8 256))) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ok (var-set token-uri value)) - ) -) - -(define-read-only (get-token-uri) - (ok (some (var-get token-uri))) -) - -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (begin - (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) - (match (ft-transfer? yield-wbtc-40555 (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? yield-wbtc-40555 (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? yield-wbtc-40555 (fixed-to-decimals amount) sender) - ) -) - -(define-public (get-token) - (ok (var-get underlying-token)) -) - -(define-public (get-expiry) - (ok (var-get token-expiry)) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? yield-wbtc-40555 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file From b7e7bb088565b0c3b47e9194374ddd329f6e5dc0 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 27 Oct 2021 23:59:17 +0800 Subject: [PATCH 14/16] arb ready --- init-js-tool/index.js | 237 ++++++++++++-------------------------- init-js-tool/pools-crp.js | 7 +- init-js-tool/pools-fwp.js | 11 +- init-js-tool/pools-ytp.js | 11 +- 4 files changed, 95 insertions(+), 171 deletions(-) diff --git a/init-js-tool/index.js b/init-js-tool/index.js index f86886a3..a5a391ed 100644 --- a/init-js-tool/index.js +++ b/init-js-tool/index.js @@ -62,175 +62,90 @@ const { const _deploy = { 0: {token: 'token-wbtc', collateral: 'token-usda', - yield_token: 'yield-wbtc-40555', - key_token: 'key-wbtc-40555-usda', - pool_token: 'ytp-yield-wbtc-40555-wbtc', - multisig_ytp: 'multisig-ytp-yield-wbtc-40555-wbtc', - multisig_crp: 'multisig-crp-wbtc-40555-usda', + yield_token: 'yield-wbtc-11520', + key_token: 'key-wbtc-11520-usda', + pool_token: 'ytp-yield-wbtc-11520-wbtc', + multisig_ytp: 'multisig-ytp-yield-wbtc-11520-wbtc', + multisig_crp: 'multisig-crp-wbtc-11520-usda', liquidity_ytp: 100e+8, collateral_crp: 1500000e+8, ltv_0: 0.7e+8, bs_vol: 0.8e+8, - target_apy: 0.08354, - expiry: 40555e+8, + target_apy: 0.06354, + expiry: 11520e+8, }, 1: {token: 'token-usda', collateral: 'token-wbtc', - yield_token: 'yield-usda-40555', - key_token: 'key-usda-40555-wbtc', - pool_token: 'ytp-yield-usda-40555-usda', - multisig_ytp: 'multisig-ytp-yield-usda-40555-usda', - multisig_crp: 'multisig-crp-usda-40555-wbtc', + yield_token: 'yield-usda-11520', + key_token: 'key-usda-11520-wbtc', + pool_token: 'ytp-yield-usda-11520-usda', + multisig_ytp: 'multisig-ytp-yield-usda-11520-usda', + multisig_crp: 'multisig-crp-usda-11520-wbtc', liquidity_ytp: 6000000e+8, collateral_crp: 25e+8, ltv_0: 0.7e+8, bs_vol: 0.8e+8, - target_apy: 0.136475, - expiry: 40555e+8, + target_apy: 0.086475, + expiry: 11520e+8, }, 2: { token: 'token-wbtc', collateral: 'token-usda', - yield_token: 'yield-wbtc-80875', - key_token: 'key-wbtc-80875-usda', - pool_token: 'ytp-yield-wbtc-80875-wbtc', - multisig_ytp: 'multisig-ytp-yield-wbtc-80875-wbtc', - multisig_crp: 'multisig-crp-wbtc-80875-usda', + yield_token: 'yield-wbtc-51840', + key_token: 'key-wbtc-51840-usda', + pool_token: 'ytp-yield-wbtc-51840-wbtc', + multisig_ytp: 'multisig-ytp-yield-wbtc-51840-wbtc', + multisig_crp: 'multisig-crp-wbtc-51840-usda', liquidity_ytp: 100e+8, collateral_crp: 1500000e+8, ltv_0: 0.7e+8, bs_vol: 0.8e+8, - target_apy: 0.113486, - expiry: 80875e+8, + target_apy: 0.093486, + expiry: 51840e+8, }, 3: { token: 'token-usda', collateral: 'token-wbtc', - yield_token: 'yield-usda-80875', - key_token: 'key-usda-80875-wbtc', - pool_token: 'ytp-yield-usda-80875-usda', - multisig_ytp: 'multisig-ytp-yield-usda-80875-usda', - multisig_crp: 'multisig-crp-usda-80875-wbtc', + yield_token: 'yield-usda-51840', + key_token: 'key-usda-51840-wbtc', + pool_token: 'ytp-yield-usda-51840-usda', + multisig_ytp: 'multisig-ytp-yield-usda-51840-usda', + multisig_crp: 'multisig-crp-usda-51840-wbtc', liquidity_ytp: 6000000e+8, collateral_crp: 25e+8, ltv_0: 0.7e+8, bs_vol: 0.8e+8, - target_apy: 0.135768, - expiry: 80875e+8, + target_apy: 0.10768, + expiry: 51840e+8, }, 4: {token: 'token-wbtc', collateral: 'token-usda', - yield_token: 'yield-wbtc-121195', - key_token: 'key-wbtc-121195-usda', - pool_token: 'ytp-yield-wbtc-121195-wbtc', - multisig_ytp: 'multisig-ytp-yield-wbtc-121195-wbtc', - multisig_crp: 'multisig-crp-wbtc-121195-usda', + yield_token: 'yield-wbtc-92160', + key_token: 'key-wbtc-92160-usda', + pool_token: 'ytp-yield-wbtc-92160-wbtc', + multisig_ytp: 'multisig-ytp-yield-wbtc-92160-wbtc', + multisig_crp: 'multisig-crp-wbtc-92160-usda', liquidity_ytp: 100e+8, collateral_crp: 1500000e+8, ltv_0: 0.7e+8, bs_vol: 0.8e+8, target_apy: 0.18469, - expiry: 121195e+8, + expiry: 92160e+8, }, 5: {token: 'token-usda', collateral: 'token-wbtc', - yield_token: 'yield-usda-121195', - key_token: 'key-usda-121195-wbtc', - pool_token: 'ytp-yield-usda-121195-usda', - multisig_ytp: 'multisig-ytp-yield-usda-121195-usda', - multisig_crp: 'multisig-crp-usda-121195-wbtc', + yield_token: 'yield-usda-92160', + key_token: 'key-usda-92160-wbtc', + pool_token: 'ytp-yield-usda-92160-usda', + multisig_ytp: 'multisig-ytp-yield-usda-92160-usda', + multisig_crp: 'multisig-crp-usda-92160-wbtc', liquidity_ytp: 6000000e+8, collateral_crp: 25e+8, ltv_0: 0.7e+8, bs_vol: 0.8e+8, target_apy: 0.16950, - expiry: 121195e+8, - }, - 6: {token: 'token-wbtc', - collateral: 'token-usda', - yield_token: 'yield-wbtc-161515', - key_token: 'key-wbtc-161515-usda', - pool_token: 'ytp-yield-wbtc-161515-wbtc', - multisig_ytp: 'multisig-ytp-yield-wbtc-161515-wbtc', - multisig_crp: 'multisig-crp-wbtc-161515-usda', - liquidity_ytp: 100e+8, - collateral_crp: 1500000e+8, - ltv_0: 0.7e+8, - bs_vol: 0.8e+8, - target_apy: 0.24469, - expiry: 161515e+8, - }, - 7: {token: 'token-usda', - collateral: 'token-wbtc', - yield_token: 'yield-usda-161515', - key_token: 'key-usda-161515-wbtc', - pool_token: 'ytp-yield-usda-161515-usda', - multisig_ytp: 'multisig-ytp-yield-usda-161515-usda', - multisig_crp: 'multisig-crp-usda-161515-wbtc', - liquidity_ytp: 6000000e+8, - collateral_crp: 25e+8, - ltv_0: 0.7e+8, - bs_vol: 0.8e+8, - target_apy: 0.22950, - expiry: 161515e+8, - }, - 8: {token: 'token-wbtc', - collateral: 'token-usda', - yield_token: 'yield-wbtc-200335', - key_token: 'key-wbtc-200335-usda', - pool_token: 'ytp-yield-wbtc-200335-wbtc', - multisig_ytp: 'multisig-ytp-yield-wbtc-200335-wbtc', - multisig_crp: 'multisig-crp-wbtc-200335-usda', - liquidity_ytp: 100e+8, - collateral_crp: 1500000e+8, - ltv_0: 0.7e+8, - bs_vol: 0.8e+8, - target_apy: 0.08469, - expiry: 200335e+8, - }, - 9: {token: 'token-usda', - collateral: 'token-wbtc', - yield_token: 'yield-usda-200335', - key_token: 'key-usda-200335-wbtc', - pool_token: 'ytp-yield-usda-200335-usda', - multisig_ytp: 'multisig-ytp-yield-usda-200335-usda', - multisig_crp: 'multisig-crp-usda-200335-wbtc', - liquidity_ytp: 6000000e+8, - collateral_crp: 25e+8, - ltv_0: 0.7e+8, - bs_vol: 0.8e+8, - target_apy: 0.10950, - expiry: 200335e+8, - }, - 10: {token: 'token-wbtc', - collateral: 'token-usda', - yield_token: 'yield-wbtc-240655', - key_token: 'key-wbtc-240655-usda', - pool_token: 'ytp-yield-wbtc-240655-wbtc', - multisig_ytp: 'multisig-ytp-yield-wbtc-240655-wbtc', - multisig_crp: 'multisig-crp-wbtc-240655-usda', - liquidity_ytp: 100e+8, - collateral_crp: 1500000e+8, - ltv_0: 0.7e+8, - bs_vol: 0.8e+8, - target_apy: 0.08469, - expiry: 240655e+8, - }, - 11: {token: 'token-usda', - collateral: 'token-wbtc', - yield_token: 'yield-usda-240655', - key_token: 'key-usda-240655-wbtc', - pool_token: 'ytp-yield-usda-240655-usda', - multisig_ytp: 'multisig-ytp-yield-usda-240655-usda', - multisig_crp: 'multisig-crp-usda-240655-wbtc', - liquidity_ytp: 6000000e+8, - collateral_crp: 25e+8, - ltv_0: 0.7e+8, - bs_vol: 0.8e+8, - target_apy: 0.10950, - expiry: 240655e+8, - }, - + expiry: 92160e+8, + }, } const ONE_8 = 100000000 @@ -383,14 +298,14 @@ async function arbitrage_fwp(dry_run = true) { if (dx.type === 7 && dx.value.value > 0n) { let dy = await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dx.value.value); if (dy.type == 7) { - await fwpSwapXforY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dx.value.value); + await fwpSwapXforY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dx.value.value, 0); } else { console.log('error: ', dy.value.value); let dx_i = Math.round(Number(dx.value.value) / 4); for (let i = 0; i < 4; i++) { let dy_i = await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dx_i); if (dy_i.type == 7) { - await fwpSwapXforY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dx_i); + await fwpSwapXforY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dx_i, 0); } } } @@ -403,14 +318,14 @@ async function arbitrage_fwp(dry_run = true) { if (dy.type === 7 && dy.value.value > 0n) { let dx = await fwpGetXgivenY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dy.value.value); if (dx.type == 7) { - await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dy.value.value); + await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dy.value.value, 0); } else { console.log('error: ', dx.value.value); let dy_i = Math.round(Number(dy.value.value) / 4); for (let i = 0; i < 4; i++) { let dx_i = await fwpGetXgivenY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dy_i); if (dx_i.type == 7) { - await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dy_i); + await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, dy_i, 0); } } } @@ -465,14 +380,14 @@ async function arbitrage_crp(dry_run = true, _subset=_deploy) { if (dx.type === 7 && dx.value.value > 0n) { let dy = await crpGetYgivenX(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dx.value.value); if (dy.type == 7) { - await crpSwapXforY(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dx.value.value); + await crpSwapXforY(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dx.value.value, 0); } else { console.log('error: ', dy.value.value); let dx_i = Math.round(Number(dx.value.value) / 4); for (let i = 0; i < 4; i++) { let dy_i = await crpGetYgivenX(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dx_i); if (dy_i.type == 7) { - await crpSwapXforY(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dx_i); + await crpSwapXforY(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dx_i, 0); } else { console.log('error: ', dy_i.value.value); break; @@ -488,14 +403,14 @@ async function arbitrage_crp(dry_run = true, _subset=_deploy) { if (dy.type === 7 && dy.value.value > 0n) { let dx = await crpGetXgivenY(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dy.value.value); if (dx.type == 7) { - await crpSwapYforX(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dy.value.value); + await crpSwapYforX(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dy.value.value, 0); } else { console.log('error: ', dx.value.value); let dy_i = Math.round(Number(dy.value.value) / 4); for (let i = 0; i < 4; i++) { let dx_i = await crpGetXgivenY(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dy_i); if (dx_i.type == 7) { - await crpSwapYforX(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dy_i); + await crpSwapYforX(_subset[key]['token'], _subset[key]['collateral'], _subset[key]['expiry'], dy_i, 0); } else { console.log('error: ', dx_i.value.value); break; @@ -546,14 +461,14 @@ async function arbitrage_ytp(dry_run = true, _subset=_deploy) { if (dx.type === 7 && dx.value.value > 0n) { let dy = await ytpGetYgivenX(_subset[key]['yield_token'], dx.value.value); if (dy.type == 7) { - await ytpSwapXforY(_subset[key]['yield_token'], _subset[key]['token'], dx.value.value); + await ytpSwapXforY(_subset[key]['yield_token'], _subset[key]['token'], dx.value.value, 0); } else { console.log('error: ', dy.value.value); dx_i = Math.round(Number(dx.value.value) / 10); for (let i = 0; i < 10; i++) { let dy_i = await ytpGetYgivenX(_subset[key]['yield_token'], dx_i); if (dy_i.type == 7) { - await ytpSwapXforY(_subset[key]['yield_token'], _subset[key]['token'], dx_i); + await ytpSwapXforY(_subset[key]['yield_token'], _subset[key]['token'], dx_i, 0); } else { console.log('error: ', dy_i.value.value); break; @@ -625,13 +540,13 @@ async function test_spot_trading() { let from_amount = ONE_8; let to_amount = parseInt((await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, from_amount)).value.value); let exchange_rate = parseInt((await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, ONE_8))); - await fwpSwapXforY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, from_amount); + await fwpSwapXforY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, from_amount, 0); from_amount = Number(wbtcPrice); to_amount = (await fwpGetXgivenY('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, from_amount)); exchange_rate = parseInt((await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, ONE_8))); if (to_amount.type === 7) { - await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, from_amount); + await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, from_amount, 0); } else { console.log('error: ', to_amount.value.value); } @@ -643,35 +558,35 @@ async function test_margin_trading() { let wbtcPrice = (await getOpenOracle('coingecko', 'WBTC')).value.value; let usdaPrice = (await getOpenOracle('coingecko', 'USDA')).value.value; - let expiry_0 = 40555e+8 + let expiry_0 = 11520e+8 let amount = 1 * ONE_8; //gross exposure of 1 BTC let trade_price = Number((await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, amount)).value.value); // in USD let trade_amount = amount; // in BTC let ltv = Number((await crpGetLtv('token-usda', 'token-wbtc', expiry_0)).value.value); - ltv /= Number((await ytpGetPrice("yield-usda-40555")).value.value); + ltv /= Number((await ytpGetPrice("yield-usda-11520")).value.value); let margin = Math.round(amount * (1 - ltv)); // in BTC let leverage = 1 / (1 - ltv); console.log("ltv: ", format_number(ltv, 2), "; amount (BTC): ", format_number(amount, 8), "; margin (BTC): ", format_number(margin, 8)); console.log("leverage: ", format_number(leverage, 2), "; trade_price (USD): ", format_number(trade_price, 2)); - await flashloan('flash-loan-user-margin-wbtc-usda-40555', 'token-wbtc', (amount - margin)); + await flashloan('flash-loan-user-margin-wbtc-usda-11520', 'token-wbtc', (amount - margin)); console.log("------ Testing Margin Trading (Short BTC vs USD) ------"); console.log(timestamp()); - expiry_0 = 40555e+8 + expiry_0 = 11520e+8 amount = 1 * ONE_8; //gross exposure of 1 BTC trade_price = Number((await fwpGetYgivenX('token-wbtc', 'token-usda', 0.5e+8, 0.5e+8, amount)).value.value); // in USD trade_amount = amount; // in BTC ltv = Number((await crpGetLtv('token-wbtc', 'token-usda', expiry_0)).value.value); - ltv /= Number((await ytpGetPrice("yield-wbtc-40555")).value.value); + ltv /= Number((await ytpGetPrice("yield-wbtc-11520")).value.value); margin = Math.round(amount * (1 - ltv) * Number(wbtcPrice) / ONE_8); // in USD leverage = 1 / (1 - ltv); console.log("ltv: ", format_number(ltv, 2), "; amount (BTC): ", format_number(amount, 8), "; margin (USD): ", format_number(margin, 2)); console.log("leverage: ", format_number(leverage, 2), "; trade_price (USD): ", format_number(trade_price, 2)) - await flashloan('flash-loan-user-margin-usda-wbtc-40555', 'token-usda', (trade_price - margin)); + await flashloan('flash-loan-user-margin-usda-wbtc-11520', 'token-usda', (trade_price - margin)); } function format_number(number, fixed = 2) { @@ -809,19 +724,19 @@ async function run() { // await mint_some_tokens(process.env.USER_ACCOUNT_ADDRESS); // await get_some_token(process.env.USER_ACCOUNT_ADDRESS); - const _pools = { 0:_deploy[2], - 1:_deploy[3], - 2:_deploy[4], - 3:_deploy[5], - 4:_deploy[6], - 5:_deploy[7], - 6:_deploy[8], - 7:_deploy[9], - 8:_deploy[10], - 9:_deploy[11] - }; - // const _pools = { 0:_deploy[10], 1:_deploy[11] }; - // const _pools = _deploy; + // const _pools = { 0:_deploy[2], + // 1:_deploy[3], + // 2:_deploy[4], + // 3:_deploy[5], + // 4:_deploy[6], + // 5:_deploy[7], + // 6:_deploy[8], + // 7:_deploy[9], + // 8:_deploy[10], + // 9:_deploy[11] + // }; + // const _pools = { 0:_deploy[4], 1:_deploy[5] }; + const _pools = _deploy; // await create_fwp(add_only=false); // await create_ytp(add_only=false, _pools); @@ -829,8 +744,8 @@ async function run() { await arbitrage_fwp(dry_run = false); await arbitrage_crp(dry_run = false, _pools); - // await arbitrage_ytp(dry_run = false, _pools); - // await arbitrage_fwp(dry_run = false); + await arbitrage_ytp(dry_run = false, _pools); + await arbitrage_fwp(dry_run = false); // await test_spot_trading(); // await test_margin_trading(); @@ -865,12 +780,12 @@ async function run() { // await burn('token-wbtc', 'STZP1114C4EA044RE54M6G5ZC2NYK9SAHB5QVE1', 9995719169074); // await burn('token-usda', 'STZP1114C4EA044RE54M6G5ZC2NYK9SAHB5QVE1', 399709145833000000); - // result = await ytpGetYgivenX('yield-wbtc-80875', 1e8); + // result = await ytpGetYgivenX('yield-wbtc-51840', 1e8); // console.log(result); // result = await fwpGetXgivenY('token-wbtc', 'token-usda', 0.5e8, 0.5e8, 500000000e8); // console.log(result); - // await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e8, 0.5e8, 500000000e8); + // await fwpSwapYforX('token-wbtc', 'token-usda', 0.5e8, 0.5e8, 500000000e8, 0); // await arbitrage_fwp(dry_run = false); // await mint_some_wbtc(process.env.USER_ACCOUNT_ADDRESS); // await see_balance(process.env.USER_ACCOUNT_ADDRESS); diff --git a/init-js-tool/pools-crp.js b/init-js-tool/pools-crp.js index 614f6677..525392e3 100644 --- a/init-js-tool/pools-crp.js +++ b/init-js-tool/pools-crp.js @@ -7,6 +7,7 @@ const { AnchorMode, PostConditionMode, uintCV, + someCV, contractPrincipalCV, broadcastTransaction, ClarityType @@ -173,7 +174,7 @@ const {wait_until_confirmation } = require('./utils'); } } - const crpSwapXforY = async (token, collateral, expiry, dx) => { + const crpSwapXforY = async (token, collateral, expiry, dx, min_dy) => { console.log('--------------------------------------------------------------------------'); console.log('[CRP] swap-x-for-y...', token, collateral, expiry, dx); const privateKey = await getUserPK(); @@ -186,6 +187,7 @@ const {wait_until_confirmation } = require('./utils'); contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, collateral), uintCV(expiry), uintCV(dx), + someCV(uintCV(min_dy)) ], senderKey: privateKey, validateWithAbi: true, @@ -203,7 +205,7 @@ const {wait_until_confirmation } = require('./utils'); } } - const crpSwapYforX = async (token, collateral, expiry, dy) => { + const crpSwapYforX = async (token, collateral, expiry, dy, min_dx) => { console.log('--------------------------------------------------------------------------'); console.log('[CRP] swap-y-for-x...', token, collateral, expiry, dy); const privateKey = await getUserPK(); @@ -216,6 +218,7 @@ const {wait_until_confirmation } = require('./utils'); contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, collateral), uintCV(expiry), uintCV(dy), + someCV(uintCV(min_dx)) ], senderKey: privateKey, validateWithAbi: true, diff --git a/init-js-tool/pools-fwp.js b/init-js-tool/pools-fwp.js index e38ce96b..6d9bc9f5 100644 --- a/init-js-tool/pools-fwp.js +++ b/init-js-tool/pools-fwp.js @@ -7,6 +7,7 @@ const { AnchorMode, PostConditionMode, uintCV, + someCV, contractPrincipalCV, broadcastTransaction, ClarityType @@ -178,7 +179,7 @@ const fwpGetYgivenX = async (tokenX, tokenY, weightX, weightY, dx) => { } }; -const fwpSwapXforY = async (tokenX, tokenY, weightX, weightY, dx) => { +const fwpSwapXforY = async (tokenX, tokenY, weightX, weightY, dx, min_dy) => { console.log('--------------------------------------------------------------------------'); console.log('[FWP] swap-x-for-y...', tokenX, tokenY, weightX, weightY, dx); const privateKey = await getUserPK(); @@ -191,7 +192,8 @@ const fwpSwapXforY = async (tokenX, tokenY, weightX, weightY, dx) => { contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, tokenY), uintCV(weightX), uintCV(weightY), - uintCV(dx) + uintCV(dx), + someCV(uintCV(min_dy)) ], senderKey: privateKey, validateWithAbi: true, @@ -210,7 +212,7 @@ const fwpSwapXforY = async (tokenX, tokenY, weightX, weightY, dx) => { } } -const fwpSwapYforX = async (tokenX, tokenY, weightX, weightY, dy) => { +const fwpSwapYforX = async (tokenX, tokenY, weightX, weightY, dy, min_dx) => { console.log('--------------------------------------------------------------------------'); console.log('[FWP] swap-y-for-x...', tokenX, tokenY, weightX, weightY, dy); const privateKey = await getUserPK(); @@ -223,7 +225,8 @@ const fwpSwapYforX = async (tokenX, tokenY, weightX, weightY, dy) => { contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, tokenY), uintCV(weightX), uintCV(weightY), - uintCV(dy) + uintCV(dy), + someCV(uintCV(min_dx)) ], senderKey: privateKey, validateWithAbi: true, diff --git a/init-js-tool/pools-ytp.js b/init-js-tool/pools-ytp.js index 0738143b..1f085ef6 100644 --- a/init-js-tool/pools-ytp.js +++ b/init-js-tool/pools-ytp.js @@ -7,6 +7,7 @@ const { AnchorMode, PostConditionMode, uintCV, + someCV, contractPrincipalCV, broadcastTransaction, ClarityType @@ -150,7 +151,7 @@ const { } } - const ytpSwapXforY = async (yiedToken, token, dx) => { + const ytpSwapXforY = async (yiedToken, token, dx, min_dy) => { console.log('--------------------------------------------------------------------------'); console.log('[YTP] swap-x-for-y...', yiedToken, token, dx); const privateKey = await getUserPK(); @@ -161,7 +162,8 @@ const { functionArgs: [ contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, yiedToken), contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, token), - uintCV(dx) + uintCV(dx), + someCV(uintCV(min_dy)) ], senderKey: privateKey, validateWithAbi: true, @@ -179,7 +181,7 @@ const { } } - const ytpSwapYforX = async (yiedToken, token, dy) => { + const ytpSwapYforX = async (yiedToken, token, dy, min_dx) => { console.log('--------------------------------------------------------------------------'); console.log('[YTP] swap-y-for-x...', yiedToken, token, dy); const privateKey = await getUserPK(); @@ -190,7 +192,8 @@ const { functionArgs: [ contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, yiedToken), contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, token), - uintCV(dy) + uintCV(dy), + someCV(uintCV(min_dx)) ], senderKey: privateKey, validateWithAbi: true, From c5a279d779671001f15c50fa54b3c19ed43343cc Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 27 Oct 2021 23:59:46 +0800 Subject: [PATCH 15/16] clean up --- ...lash-loan-user-margin-usda-wbtc-11520.clar | 29 ++ ...lash-loan-user-margin-usda-wbtc-23040.clar | 29 ++ ...lash-loan-user-margin-usda-wbtc-51840.clar | 29 ++ ...lash-loan-user-margin-usda-wbtc-92160.clar | 29 ++ ...lash-loan-user-margin-wbtc-usda-11520.clar | 29 ++ ...lash-loan-user-margin-wbtc-usda-23040.clar | 29 ++ ...lash-loan-user-margin-wbtc-usda-51840.clar | 29 ++ ...lash-loan-user-margin-wbtc-usda-92160.clar | 29 ++ .../key-token/key-usda-11520-wbtc.clar | 107 +++++++ .../key-token/key-usda-23040-wbtc.clar | 107 +++++++ .../key-token/key-usda-51840-wbtc.clar | 107 +++++++ .../key-token/key-usda-92160-wbtc.clar | 107 +++++++ .../key-token/key-wbtc-11520-usda.clar | 107 +++++++ .../key-token/key-wbtc-23040-usda.clar | 107 +++++++ .../key-token/key-wbtc-51840-usda.clar | 107 +++++++ .../key-token/key-wbtc-92160-usda.clar | 107 +++++++ .../multisig-crp-usda-11520-wbtc.clar | 277 ++++++++++++++++++ .../multisig-crp-usda-23040-wbtc.clar | 277 ++++++++++++++++++ .../multisig-crp-usda-51840-wbtc.clar | 277 ++++++++++++++++++ .../multisig-crp-usda-92160-wbtc.clar | 277 ++++++++++++++++++ .../multisig-crp-wbtc-11520-usda.clar | 277 ++++++++++++++++++ .../multisig-crp-wbtc-23040-usda.clar | 277 ++++++++++++++++++ .../multisig-crp-wbtc-51840-usda.clar | 277 ++++++++++++++++++ .../multisig-crp-wbtc-92160-usda.clar | 277 ++++++++++++++++++ .../multisig-ytp-yield-usda-11520-usda.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-usda-23040-usda.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-usda-51840-usda.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-usda-92160-usda.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-wbtc-11520-wbtc.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-wbtc-23040-wbtc.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-wbtc-51840-wbtc.clar | 272 +++++++++++++++++ .../multisig-ytp-yield-wbtc-92160-wbtc.clar | 272 +++++++++++++++++ .../pool-token/ytp-yield-usda-11520-usda.clar | 97 ++++++ .../pool-token/ytp-yield-usda-23040-usda.clar | 97 ++++++ .../pool-token/ytp-yield-usda-51840-usda.clar | 97 ++++++ .../pool-token/ytp-yield-usda-92160-usda.clar | 97 ++++++ .../pool-token/ytp-yield-wbtc-11520-wbtc.clar | 97 ++++++ .../pool-token/ytp-yield-wbtc-23040-wbtc.clar | 97 ++++++ .../pool-token/ytp-yield-wbtc-51840-wbtc.clar | 97 ++++++ .../pool-token/ytp-yield-wbtc-92160-wbtc.clar | 97 ++++++ .../yield-token/yield-usda-11520.clar | 112 +++++++ .../yield-token/yield-usda-23040.clar | 112 +++++++ .../yield-token/yield-usda-51840.clar | 112 +++++++ .../yield-token/yield-usda-92160.clar | 112 +++++++ .../yield-token/yield-wbtc-11520.clar | 112 +++++++ .../yield-token/yield-wbtc-23040.clar | 112 +++++++ .../yield-token/yield-wbtc-51840.clar | 112 +++++++ .../yield-token/yield-wbtc-92160.clar | 112 +++++++ init-js-tool/contract-records.json | 1 + 49 files changed, 7153 insertions(+) create mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar create mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar create mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-51840.clar create mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-92160.clar create mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-11520.clar create mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar create mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-51840.clar create mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-92160.clar create mode 100644 clarity/contracts/key-token/key-usda-11520-wbtc.clar create mode 100644 clarity/contracts/key-token/key-usda-23040-wbtc.clar create mode 100644 clarity/contracts/key-token/key-usda-51840-wbtc.clar create mode 100644 clarity/contracts/key-token/key-usda-92160-wbtc.clar create mode 100644 clarity/contracts/key-token/key-wbtc-11520-usda.clar create mode 100644 clarity/contracts/key-token/key-wbtc-23040-usda.clar create mode 100644 clarity/contracts/key-token/key-wbtc-51840-usda.clar create mode 100644 clarity/contracts/key-token/key-wbtc-92160-usda.clar create mode 100644 clarity/contracts/multisig/multisig-crp-usda-11520-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-crp-usda-51840-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-crp-usda-92160-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-11520-usda.clar create mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar create mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-51840-usda.clar create mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-92160-usda.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-51840-usda.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-92160-usda.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-11520-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-51840-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-92160-wbtc.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-usda-51840-usda.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-usda-92160-usda.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-51840-wbtc.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-92160-wbtc.clar create mode 100644 clarity/contracts/yield-token/yield-usda-11520.clar create mode 100644 clarity/contracts/yield-token/yield-usda-23040.clar create mode 100644 clarity/contracts/yield-token/yield-usda-51840.clar create mode 100644 clarity/contracts/yield-token/yield-usda-92160.clar create mode 100644 clarity/contracts/yield-token/yield-wbtc-11520.clar create mode 100644 clarity/contracts/yield-token/yield-wbtc-23040.clar create mode 100644 clarity/contracts/yield-token/yield-wbtc-51840.clar create mode 100644 clarity/contracts/yield-token/yield-wbtc-92160.clar create mode 100644 init-js-tool/contract-records.json diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar new file mode 100644 index 00000000..94bec467 --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-wbtc-11520 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-11520))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-11520 .key-wbtc-11520-usda gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-11520", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar new file mode 100644 index 00000000..d3e1127e --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-wbtc-23040 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-23040))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-23040 .key-wbtc-23040-usda gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-23040", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-51840.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-51840.clar new file mode 100644 index 00000000..2b1c4a7b --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-51840.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-wbtc-51840 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-51840))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-51840 .key-wbtc-51840-usda gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-51840", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-92160.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-92160.clar new file mode 100644 index 00000000..7f3bea33 --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc-92160.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-wbtc-92160 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-wbtc .token-usda expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-wbtc-92160))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-92160 .key-wbtc-92160-usda gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-92160", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-11520.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-11520.clar new file mode 100644 index 00000000..2f02a734 --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-11520.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-usda-11520 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-usda-11520))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-11520 .key-usda-11520-wbtc gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-11520", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar new file mode 100644 index 00000000..c9b6d214 --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-23040.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-usda-23040 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-usda-23040))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-23040 .key-usda-23040-wbtc gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-23040", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-51840.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-51840.clar new file mode 100644 index 00000000..923f2059 --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-51840.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-usda-51840 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-usda-51840))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-51840 .key-usda-51840-wbtc gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-51840", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-92160.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-92160.clar new file mode 100644 index 00000000..d0a10f53 --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda-92160.clar @@ -0,0 +1,29 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant math-call-err (err u2010)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (token ) (amount uint)) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (expiry (unwrap! (contract-call? .yield-usda-92160 get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc expiry))) + (price (try! (contract-call? .yield-token-pool get-price .yield-usda-92160))) + (gross-amount (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-92160 .key-usda-92160-wbtc gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (if (is-some (contract-call? .fixed-weight-pool get-pool-exists .token-wbtc .token-usda u50000000 u50000000)) + (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token none)) + ) + + (print { object: "flash-loan-user-margin-usda-wbtc-92160", action: "execute", data: gross-amount }) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-11520-wbtc.clar b/clarity/contracts/key-token/key-usda-11520-wbtc.clar new file mode 100644 index 00000000..7c5bd59b --- /dev/null +++ b/clarity/contracts/key-token/key-usda-11520-wbtc.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-usda-11520-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u1152000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-usda-11520-wbtc))) +) + +(define-read-only (get-name) + (ok "key-usda-11520-wbtc") +) + +(define-read-only (get-symbol) + (ok "key-usda-11520-wbtc") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-usda-11520-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-usda-11520-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-usda-11520-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-usda-11520-wbtc (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-23040-wbtc.clar b/clarity/contracts/key-token/key-usda-23040-wbtc.clar new file mode 100644 index 00000000..f47610c4 --- /dev/null +++ b/clarity/contracts/key-token/key-usda-23040-wbtc.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-usda-23040-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u2304000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-usda-23040-wbtc))) +) + +(define-read-only (get-name) + (ok "key-usda-23040-wbtc") +) + +(define-read-only (get-symbol) + (ok "key-usda-23040-wbtc") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-usda-23040-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-usda-23040-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-usda-23040-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-usda-23040-wbtc (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-51840-wbtc.clar b/clarity/contracts/key-token/key-usda-51840-wbtc.clar new file mode 100644 index 00000000..f3df36ba --- /dev/null +++ b/clarity/contracts/key-token/key-usda-51840-wbtc.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-usda-51840-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u5184000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-usda-51840-wbtc))) +) + +(define-read-only (get-name) + (ok "key-usda-51840-wbtc") +) + +(define-read-only (get-symbol) + (ok "key-usda-51840-wbtc") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-usda-51840-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-usda-51840-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-usda-51840-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-usda-51840-wbtc (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-usda-92160-wbtc.clar b/clarity/contracts/key-token/key-usda-92160-wbtc.clar new file mode 100644 index 00000000..6a62781e --- /dev/null +++ b/clarity/contracts/key-token/key-usda-92160-wbtc.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-usda-92160-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u9216000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-usda-92160-wbtc))) +) + +(define-read-only (get-name) + (ok "key-usda-92160-wbtc") +) + +(define-read-only (get-symbol) + (ok "key-usda-92160-wbtc") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-usda-92160-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-usda-92160-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-usda-92160-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-usda-92160-wbtc (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-11520-usda.clar b/clarity/contracts/key-token/key-wbtc-11520-usda.clar new file mode 100644 index 00000000..8978a862 --- /dev/null +++ b/clarity/contracts/key-token/key-wbtc-11520-usda.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-wbtc-11520-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u1152000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-wbtc-11520-usda))) +) + +(define-read-only (get-name) + (ok "key-wbtc-11520-usda") +) + +(define-read-only (get-symbol) + (ok "key-wbtc-11520-usda") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-wbtc-11520-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-wbtc-11520-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-wbtc-11520-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-wbtc-11520-usda (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-23040-usda.clar b/clarity/contracts/key-token/key-wbtc-23040-usda.clar new file mode 100644 index 00000000..d2f07647 --- /dev/null +++ b/clarity/contracts/key-token/key-wbtc-23040-usda.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-wbtc-23040-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u2304000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-wbtc-23040-usda))) +) + +(define-read-only (get-name) + (ok "key-wbtc-23040-usda") +) + +(define-read-only (get-symbol) + (ok "key-wbtc-23040-usda") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-wbtc-23040-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-wbtc-23040-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-wbtc-23040-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-wbtc-23040-usda (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-51840-usda.clar b/clarity/contracts/key-token/key-wbtc-51840-usda.clar new file mode 100644 index 00000000..b64da3ef --- /dev/null +++ b/clarity/contracts/key-token/key-wbtc-51840-usda.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-wbtc-51840-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u5184000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-wbtc-51840-usda))) +) + +(define-read-only (get-name) + (ok "key-wbtc-51840-usda") +) + +(define-read-only (get-symbol) + (ok "key-wbtc-51840-usda") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-wbtc-51840-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-wbtc-51840-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-wbtc-51840-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-wbtc-51840-usda (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-92160-usda.clar b/clarity/contracts/key-token/key-wbtc-92160-usda.clar new file mode 100644 index 00000000..b670a5a3 --- /dev/null +++ b/clarity/contracts/key-token/key-wbtc-92160-usda.clar @@ -0,0 +1,107 @@ +(impl-trait .trait-yield-token.yield-token-trait) +(impl-trait .trait-ownable.ownable-trait) + +(define-fungible-token key-wbtc-92160-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u9216000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply key-wbtc-92160-usda))) +) + +(define-read-only (get-name) + (ok "key-wbtc-92160-usda") +) + +(define-read-only (get-symbol) + (ok "key-wbtc-92160-usda") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance key-wbtc-92160-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? key-wbtc-92160-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? key-wbtc-92160-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? key-wbtc-92160-usda (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-usda-11520-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-11520-wbtc.clar new file mode 100644 index 00000000..2c10a530 --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-usda-11520-wbtc.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-usda-11520) (is-eq (contract-of token) .key-usda-11520-wbtc)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-11520 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-11520-wbtc get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-11520 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-11520-wbtc get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-11520 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-11520-wbtc get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u1152000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u1152000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar new file mode 100644 index 00000000..dd1f503d --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-usda-23040) (is-eq (contract-of token) .key-usda-23040-wbtc)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-23040 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-23040-wbtc get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-23040 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-23040-wbtc get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-23040 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-23040-wbtc get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u2304000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u2304000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-usda-51840-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-51840-wbtc.clar new file mode 100644 index 00000000..6f856cbd --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-usda-51840-wbtc.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-usda-51840) (is-eq (contract-of token) .key-usda-51840-wbtc)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-51840 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-51840-wbtc get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-51840 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-51840-wbtc get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-51840 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-51840-wbtc get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u5184000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u5184000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-usda-92160-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-92160-wbtc.clar new file mode 100644 index 00000000..f163b5da --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-usda-92160-wbtc.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-usda-92160) (is-eq (contract-of token) .key-usda-92160-wbtc)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda-92160 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-92160-wbtc get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-92160 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-92160-wbtc get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda-92160 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-92160-wbtc get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc u9216000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u9216000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-11520-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-11520-usda.clar new file mode 100644 index 00000000..fe576151 --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-wbtc-11520-usda.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-wbtc-11520) (is-eq (contract-of token) .key-wbtc-11520-usda)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-11520 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-11520-usda get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-11520 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-11520-usda get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-11520 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-11520-usda get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u1152000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u1152000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar new file mode 100644 index 00000000..cea5fa52 --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-wbtc-23040) (is-eq (contract-of token) .key-wbtc-23040-usda)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-23040 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-23040-usda get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-23040 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-23040-usda get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-23040 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-23040-usda get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u2304000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u2304000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-51840-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-51840-usda.clar new file mode 100644 index 00000000..e89b42cb --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-wbtc-51840-usda.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-wbtc-51840) (is-eq (contract-of token) .key-wbtc-51840-usda)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-51840 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-51840-usda get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-51840 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-51840-usda get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-51840 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-51840-usda get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u5184000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u5184000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-92160-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-92160-usda.clar new file mode 100644 index 00000000..25a90fef --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-wbtc-92160-usda.clar @@ -0,0 +1,277 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-x: uint, + new-fee-rate-y: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-wbtc-92160) (is-eq (contract-of token) .key-wbtc-92160-usda)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-x uint) + (new-fee-rate-y uint) + ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-wbtc-92160 get-balance tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-92160-usda get-balance tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-92160 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-92160-usda get-total-supply))) + (total-supply (+ total-yield-supply total-key-supply)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-x: new-fee-rate-x, + new-fee-rate-y: new-fee-rate-y + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-92160 get-total-supply))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-92160-usda get-total-supply))) + (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-x (get new-fee-rate-x proposal)) + (new-fee-rate-y (get new-fee-rate-y proposal)) + ) + + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-wbtc .token-usda u9216000000000 new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u9216000000000 new-fee-rate-y)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar new file mode 100644 index 00000000..ba05dfce --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-usda-11520-usda) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-11520-usda get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-11520-usda get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-11520-usda get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-11520 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-11520 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar new file mode 100644 index 00000000..1408cf3f --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda-23040-usda.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-usda-23040-usda) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-23040-usda get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-23040-usda get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-23040-usda get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-23040 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-23040 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-51840-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-51840-usda.clar new file mode 100644 index 00000000..a49052ff --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda-51840-usda.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-usda-51840-usda) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-51840-usda get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-51840-usda get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-51840-usda get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-51840 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-51840 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-92160-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-92160-usda.clar new file mode 100644 index 00000000..0e54fc05 --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda-92160-usda.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-usda-92160-usda) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-92160-usda get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-92160-usda get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-92160-usda get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-92160 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-usda-92160 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-11520-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-11520-wbtc.clar new file mode 100644 index 00000000..8c346261 --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-11520-wbtc.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-wbtc-11520-wbtc) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-11520-wbtc get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-11520-wbtc get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-11520-wbtc get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-11520 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-11520 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar new file mode 100644 index 00000000..c749fbab --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-wbtc-23040-wbtc) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23040-wbtc get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23040-wbtc get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23040-wbtc get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-23040 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-23040 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-51840-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-51840-wbtc.clar new file mode 100644 index 00000000..0e172f55 --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-51840-wbtc.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-wbtc-51840-wbtc) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-51840-wbtc get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-51840-wbtc get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-51840-wbtc get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-51840 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-51840 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-92160-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-92160-wbtc.clar new file mode 100644 index 00000000..3f7d0226 --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-92160-wbtc.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + + +;; Alex voting for MultiSig DAO +;; +;; Voting and proposing the proposals +;; A proposal will just update the DAO with new contracts. + +;; Voting can be done by locking up the corresponding pool token. +;; This prototype is for ayusda-usda pool token. +;; Common Trait and for each pool, implementation is required. +;; + +;; Errors +(define-constant ERR-NOT-ENOUGH-BALANCE (err u8000)) +(define-constant ERR-NO-FEE-CHANGE (err u8001)) +(define-constant ERR-INVALID-POOL-TOKEN (err u8002)) +(define-constant ERR-BLOCK-HEIGHT-NOT-REACHED (err u8003)) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-MATH-CALL (err u2010)) + +(define-constant ONE_8 u100000000) + +;; Constants +(define-constant DEFAULT_OWNER tx-sender) + +;; Proposal variables +;; With Vote, we can set : +;; 1. contract to have right to mint/burn token +;; 2. Set Feerate / Fee address / Collect Fees +(define-map proposals + { id: uint } + { + id: uint, + proposer: principal, + title: (string-utf8 256), + url: (string-utf8 256), + is-open: bool, + start-block-height: uint, + end-block-height: uint, + yes-votes: uint, + no-votes: uint, + new-fee-rate-token: uint, + new-fee-rate-aytoken: uint + } +) + +(define-data-var proposal-count uint u0) +(define-data-var proposal-ids (list 100 uint) (list u0)) +(define-data-var threshold uint u75000000) ;; 75% + +(define-data-var total-supply-of-token uint u0) +(define-data-var threshold-percentage uint u0) + +(define-map votes-by-member { proposal-id: uint, member: principal } { vote-count: uint }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal } { amount: uint }) + +;; Get all proposals in detail +(define-read-only (get-proposals) + (ok (map get-proposal-by-id (var-get proposal-ids))) +) + +;; Get all proposal ID in list +(define-read-only (get-proposal-ids) + (ok (var-get proposal-ids)) +) + +;; Get votes for a member on proposal +(define-read-only (get-votes-by-member-by-id (proposal-id uint) (member principal)) + (default-to + { vote-count: u0 } + (map-get? votes-by-member { proposal-id: proposal-id, member: member }) + ) +) + +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + title: u"", + url: u"", + is-open: false, + start-block-height: u0, + end-block-height: u0, + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: u0, ;; Default token feerate + new-fee-rate-aytoken: u0 ;; default aytoken feerate + } + (map-get? proposals { id: proposal-id }) + ) +) + +;; To check which tokens are accepted as votes, Only by staking Pool Token is allowed. +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-wbtc-92160-wbtc) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-aytoken uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-92160-wbtc get-balance tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-92160-wbtc get-total-supply)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) + + ;; Requires 10% of the supply + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + ;; Mutate + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + title: title, + url: url, + is-open: true, + start-block-height: start-block-height, + end-block-height: (+ start-block-height u1440), + yes-votes: u0, + no-votes: u0, + new-fee-rate-token: new-fee-rate-token, + new-fee-rate-aytoken: new-fee-rate-aytoken + } + ) + (var-set proposal-count proposal-id) + (var-set proposal-ids (unwrap-panic (as-max-len? (append (var-get proposal-ids) proposal-id) u100))) + (ok proposal-id) + ) +) + +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + + ) + + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { yes-votes: (+ amount (get yes-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (vote-count (get vote-count (get-votes-by-member-by-id proposal-id tx-sender))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token))) + ) + ;; Can vote with corresponding pool token + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + ;; Proposal should be open for voting + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + ;; Vote should be casted after the start-block-height + (asserts! (>= block-height (get start-block-height proposal)) ERR-NOT-AUTHORIZED) + ;; Voter should stake the corresponding pool token to the vote contract. + (try! (contract-call? token transfer amount tx-sender (as-contract tx-sender) none)) + + ;; Mutate + (map-set proposals + { id: proposal-id } + (merge proposal { no-votes: (+ amount (get no-votes proposal)) })) + (map-set votes-by-member + { proposal-id: proposal-id, member: tx-sender } + { vote-count: (+ amount vote-count) }) + (map-set tokens-by-member + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let ((proposal (get-proposal-by-id proposal-id)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-92160-wbtc get-total-supply)) ONE_8)) + (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (yes-votes (get yes-votes proposal)) + ) + + (asserts! (not (is-eq (get id proposal) u0)) ERR-NOT-AUTHORIZED) ;; Default id + (asserts! (get is-open proposal) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-BLOCK-HEIGHT-NOT-REACHED) + + (map-set proposals + { id: proposal-id } + (merge proposal { is-open: false })) + + ;; Execute the proposal when the yes-vote passes threshold-count. + (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) + (ok true)) +) + +;; Return votes to voter(member) +;; This function needs to be called for all members +(define-public (return-votes-to-member (token ) (proposal-id uint) (member principal)) + (let + ( + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token)) ONE_8)) + (proposal (get-proposal-by-id proposal-id)) + ) + + (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) + (asserts! (not (get is-open proposal)) ERR-NOT-AUTHORIZED) + (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) + + ;; Return the pool token + (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-aytoken (get new-fee-rate-aytoken proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-92160 new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-aytoken .yield-wbtc-92160 new-fee-rate-aytoken)) + + (ok true) + ) +) diff --git a/clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar new file mode 100644 index 00000000..245f2084 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-usda-11520-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-11520-usda))) +) + +(define-read-only (get-name) + (ok "ytp-yield-usda-11520-usda") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-usda-11520-usda") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-11520-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-usda-11520-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-usda-11520-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-usda-11520-usda (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar new file mode 100644 index 00000000..362f3084 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-usda-23040-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-23040-usda))) +) + +(define-read-only (get-name) + (ok "ytp-yield-usda-23040-usda") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-usda-23040-usda") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-23040-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-usda-23040-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-usda-23040-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-usda-23040-usda (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-51840-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-51840-usda.clar new file mode 100644 index 00000000..d8d762f7 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-usda-51840-usda.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-usda-51840-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-51840-usda))) +) + +(define-read-only (get-name) + (ok "ytp-yield-usda-51840-usda") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-usda-51840-usda") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-51840-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-usda-51840-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-usda-51840-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-usda-51840-usda (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-92160-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-92160-usda.clar new file mode 100644 index 00000000..88f2a015 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-usda-92160-usda.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-usda-92160-usda) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda-92160-usda))) +) + +(define-read-only (get-name) + (ok "ytp-yield-usda-92160-usda") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-usda-92160-usda") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda-92160-usda account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-usda-92160-usda (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-usda-92160-usda (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-usda-92160-usda (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar new file mode 100644 index 00000000..a1ab5ea2 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-wbtc-11520-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-11520-wbtc))) +) + +(define-read-only (get-name) + (ok "ytp-yield-wbtc-11520-wbtc") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-wbtc-11520-wbtc") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-11520-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-wbtc-11520-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-wbtc-11520-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-wbtc-11520-wbtc (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar new file mode 100644 index 00000000..69522189 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-wbtc-23040-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-23040-wbtc))) +) + +(define-read-only (get-name) + (ok "ytp-yield-wbtc-23040-wbtc") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-wbtc-23040-wbtc") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-23040-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-wbtc-23040-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-wbtc-23040-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-wbtc-23040-wbtc (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-51840-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-51840-wbtc.clar new file mode 100644 index 00000000..8a6e523e --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-wbtc-51840-wbtc.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-wbtc-51840-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-51840-wbtc))) +) + +(define-read-only (get-name) + (ok "ytp-yield-wbtc-51840-wbtc") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-wbtc-51840-wbtc") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-51840-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-wbtc-51840-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-wbtc-51840-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-wbtc-51840-wbtc (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-92160-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-92160-wbtc.clar new file mode 100644 index 00000000..5df22005 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-wbtc-92160-wbtc.clar @@ -0,0 +1,97 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-pool-token.pool-token-trait) + +(define-fungible-token ytp-yield-wbtc-92160-wbtc) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .yield-token-pool) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc-92160-wbtc))) +) + +(define-read-only (get-name) + (ok "ytp-yield-wbtc-92160-wbtc") +) + +(define-read-only (get-symbol) + (ok "ytp-yield-wbtc-92160-wbtc") +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc-92160-wbtc account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? ytp-yield-wbtc-92160-wbtc (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? ytp-yield-wbtc-92160-wbtc (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? ytp-yield-wbtc-92160-wbtc (fixed-to-decimals amount) sender) + ) +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-11520.clar b/clarity/contracts/yield-token/yield-usda-11520.clar new file mode 100644 index 00000000..359e3b45 --- /dev/null +++ b/clarity/contracts/yield-token/yield-usda-11520.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-usda-11520) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u1152000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-usda-11520))) +) + +(define-read-only (get-name) + (ok "yield-usda-11520") +) + +(define-read-only (get-symbol) + (ok "yield-usda-11520") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-usda-11520 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-usda-11520 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-usda-11520 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-usda-11520 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-usda-11520 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-23040.clar b/clarity/contracts/yield-token/yield-usda-23040.clar new file mode 100644 index 00000000..f6643c65 --- /dev/null +++ b/clarity/contracts/yield-token/yield-usda-23040.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-usda-23040) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u2304000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-usda-23040))) +) + +(define-read-only (get-name) + (ok "yield-usda-23040") +) + +(define-read-only (get-symbol) + (ok "yield-usda-23040") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-usda-23040 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-usda-23040 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-usda-23040 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-usda-23040 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-usda-23040 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-51840.clar b/clarity/contracts/yield-token/yield-usda-51840.clar new file mode 100644 index 00000000..c6089134 --- /dev/null +++ b/clarity/contracts/yield-token/yield-usda-51840.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-usda-51840) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u5184000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-usda-51840))) +) + +(define-read-only (get-name) + (ok "yield-usda-51840") +) + +(define-read-only (get-symbol) + (ok "yield-usda-51840") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-usda-51840 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-usda-51840 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-usda-51840 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-usda-51840 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-usda-51840 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-92160.clar b/clarity/contracts/yield-token/yield-usda-92160.clar new file mode 100644 index 00000000..f59d1c54 --- /dev/null +++ b/clarity/contracts/yield-token/yield-usda-92160.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-usda-92160) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u9216000000000) +(define-data-var underlying-token principal .token-usda) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-usda-92160))) +) + +(define-read-only (get-name) + (ok "yield-usda-92160") +) + +(define-read-only (get-symbol) + (ok "yield-usda-92160") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-usda get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-usda-92160 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-usda-92160 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-usda-92160 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-usda-92160 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-usda-92160 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-11520.clar b/clarity/contracts/yield-token/yield-wbtc-11520.clar new file mode 100644 index 00000000..66bed7a0 --- /dev/null +++ b/clarity/contracts/yield-token/yield-wbtc-11520.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-wbtc-11520) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u1152000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-wbtc-11520))) +) + +(define-read-only (get-name) + (ok "yield-wbtc-11520") +) + +(define-read-only (get-symbol) + (ok "yield-wbtc-11520") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-wbtc-11520 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-wbtc-11520 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-wbtc-11520 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-wbtc-11520 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-wbtc-11520 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-23040.clar b/clarity/contracts/yield-token/yield-wbtc-23040.clar new file mode 100644 index 00000000..6335989d --- /dev/null +++ b/clarity/contracts/yield-token/yield-wbtc-23040.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-wbtc-23040) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u2304000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-wbtc-23040))) +) + +(define-read-only (get-name) + (ok "yield-wbtc-23040") +) + +(define-read-only (get-symbol) + (ok "yield-wbtc-23040") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-wbtc-23040 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-wbtc-23040 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-wbtc-23040 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-wbtc-23040 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-wbtc-23040 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-51840.clar b/clarity/contracts/yield-token/yield-wbtc-51840.clar new file mode 100644 index 00000000..9e4a2336 --- /dev/null +++ b/clarity/contracts/yield-token/yield-wbtc-51840.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-wbtc-51840) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u5184000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-wbtc-51840))) +) + +(define-read-only (get-name) + (ok "yield-wbtc-51840") +) + +(define-read-only (get-symbol) + (ok "yield-wbtc-51840") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-wbtc-51840 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-wbtc-51840 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-wbtc-51840 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-wbtc-51840 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-wbtc-51840 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-92160.clar b/clarity/contracts/yield-token/yield-wbtc-92160.clar new file mode 100644 index 00000000..eada6850 --- /dev/null +++ b/clarity/contracts/yield-token/yield-wbtc-92160.clar @@ -0,0 +1,112 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-yield-token.yield-token-trait) + +(define-fungible-token yield-wbtc-92160) + +(define-data-var token-uri (string-utf8 256) u"") +(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var token-expiry uint u9216000000000) +(define-data-var underlying-token principal .token-wbtc) + +;; errors +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 (pow u10 u8)) + +(define-private (pow-decimals) + (pow u10 (unwrap-panic (get-decimals))) +) + +(define-read-only (fixed-to-decimals (amount uint)) + (/ (* amount (pow-decimals)) ONE_8) +) + +(define-private (decimals-to-fixed (amount uint)) + (/ (* amount ONE_8) (pow-decimals)) +) + +(define-read-only (get-owner) + (ok (var-get contract-owner)) +) + +(define-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set contract-owner owner)) + ) +) + +;; --------------------------------------------------------- +;; SIP-10 Functions +;; --------------------------------------------------------- + +(define-read-only (get-total-supply) + (ok (decimals-to-fixed (ft-get-supply yield-wbtc-92160))) +) + +(define-read-only (get-name) + (ok "yield-wbtc-92160") +) + +(define-read-only (get-symbol) + (ok "yield-wbtc-92160") +) + +(define-read-only (get-decimals) + (ok (unwrap-panic (contract-call? .token-wbtc get-decimals))) +) + +(define-read-only (get-balance (account principal)) + (ok (decimals-to-fixed (ft-get-balance yield-wbtc-92160 account))) +) + +(define-public (set-token-uri (value (string-utf8 256))) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ok (var-set token-uri value)) + ) +) + +(define-read-only (get-token-uri) + (ok (some (var-get token-uri))) +) + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (begin + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) + (match (ft-transfer? yield-wbtc-92160 (fixed-to-decimals amount) sender recipient) + response (begin + (print memo) + (ok response) + ) + error (err error) + ) + ) +) + +(define-public (mint (recipient principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-mint? yield-wbtc-92160 (fixed-to-decimals amount) recipient) + ) +) + +(define-public (burn (sender principal) (amount uint)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (ft-burn? yield-wbtc-92160 (fixed-to-decimals amount) sender) + ) +) + +(define-public (get-token) + (ok (var-get underlying-token)) +) + +(define-public (get-expiry) + (ok (var-get token-expiry)) +) + +;; Initialize the contract for Testing. +(begin + (try! (ft-mint? yield-wbtc-92160 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 +) \ No newline at end of file diff --git a/init-js-tool/contract-records.json b/init-js-tool/contract-records.json new file mode 100644 index 00000000..b78cefde --- /dev/null +++ b/init-js-tool/contract-records.json @@ -0,0 +1 @@ +{"Contracts":[{"name":"math-log-exp","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"math-fixed-point","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-sip-010","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-flash-loan-user","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-oracle","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-pool-token","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-yield-token","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-ownable","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-vault","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-multisig-vote","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"weighted-equation","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-token-equation","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"token-alex","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"token-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"token-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"alex-vault","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"open-oracle","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"alex-reserve-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"fixed-weight-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"liquidity-bootstrapping-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-token-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"collateral-rebalancing-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"faucet","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"fwp-wbtc-usda-50-50","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-fwp-wbtc-usda-50-50","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-wbtc-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-usda-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-usda-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-wbtc-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-wbtc-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-usda-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-wbtc-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-usda-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-wbtc-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-usda-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-usda-wbtc-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-wbtc-usda-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-wbtc-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-usda-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-usda-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-wbtc-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-wbtc-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-usda-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-wbtc-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-usda-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-wbtc-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-usda-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-usda-wbtc-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-wbtc-usda-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-wbtc-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-usda-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-usda-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-wbtc-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-wbtc-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-usda-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-wbtc-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-usda-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-wbtc-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-usda-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-usda-wbtc-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-wbtc-usda-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"}]} \ No newline at end of file From 42ced44233ce948213de00c7ee45c9e53d18b4c0 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Thu, 28 Oct 2021 00:00:09 +0800 Subject: [PATCH 16/16] minor clean up --- init-js-tool/contract-records.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 init-js-tool/contract-records.json diff --git a/init-js-tool/contract-records.json b/init-js-tool/contract-records.json deleted file mode 100644 index b78cefde..00000000 --- a/init-js-tool/contract-records.json +++ /dev/null @@ -1 +0,0 @@ -{"Contracts":[{"name":"math-log-exp","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"math-fixed-point","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-sip-010","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-flash-loan-user","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-oracle","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-pool-token","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-yield-token","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-ownable","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-vault","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"trait-multisig-vote","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"weighted-equation","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-token-equation","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"token-alex","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"token-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"token-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"alex-vault","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"open-oracle","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"alex-reserve-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"fixed-weight-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"liquidity-bootstrapping-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-token-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"collateral-rebalancing-pool","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"faucet","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"fwp-wbtc-usda-50-50","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-fwp-wbtc-usda-50-50","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-wbtc-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-usda-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-usda-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-wbtc-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-wbtc-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-usda-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-wbtc-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-usda-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-wbtc-11520-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-usda-11520-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-usda-wbtc-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-wbtc-usda-11520","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-wbtc-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-usda-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-usda-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-wbtc-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-wbtc-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-usda-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-wbtc-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-usda-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-wbtc-51840-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-usda-51840-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-usda-wbtc-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-wbtc-usda-51840","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-wbtc-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"yield-usda-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-usda-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"key-wbtc-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-wbtc-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"ytp-yield-usda-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-wbtc-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-crp-usda-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-wbtc-92160-wbtc","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"multisig-ytp-yield-usda-92160-usda","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-usda-wbtc-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"},{"name":"flash-loan-user-margin-wbtc-usda-92160","version":"1","deployer":"ST1J2JTYXGRMZYNKE40GM87ZCACSPSSEEQVSNB7DC"}]} \ No newline at end of file