From 931927c34fde75e304899da7fdc09a9dbd73b42c Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 8 Nov 2021 13:14:40 +0800 Subject: [PATCH 01/25] stacked-poxl-pool added (#332) --- clarity/contracts/pool/stacked-poxl-pool.clar | 198 ++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 clarity/contracts/pool/stacked-poxl-pool.clar diff --git a/clarity/contracts/pool/stacked-poxl-pool.clar b/clarity/contracts/pool/stacked-poxl-pool.clar new file mode 100644 index 00000000..d5b9988c --- /dev/null +++ b/clarity/contracts/pool/stacked-poxl-pool.clar @@ -0,0 +1,198 @@ +(impl-trait .trait-ownable.ownable-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) +(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) + +;; stacked-poxl-pool +;; + +;; constants +;; +(define-constant ONE_8 u100000000) ;; 8 decimal places + +(define-constant invalid-pool-err (err u2001)) +(define-constant no-liquidity-err (err u2002)) +(define-constant invalid-liquidity-err (err u2003)) +(define-constant transfer-x-failed-err (err u3001)) +(define-constant transfer-y-failed-err (err u3002)) +(define-constant pool-already-exists-err (err u2000)) +(define-constant too-many-pools-err (err u2004)) +(define-constant percent-greater-than-one (err u5000)) +(define-constant no-fee-x-err (err u2005)) +(define-constant no-fee-y-err (err u2006)) +(define-constant already-expiry-err (err u2010)) +(define-constant weighted-equation-call-err (err u2009)) +(define-constant math-call-err (err u2010)) +(define-constant internal-function-call-err (err u1001)) +(define-constant internal-get-weight-err (err u2012)) +(define-constant stacking-in-progress-err (err u2018)) + +(define-constant BLOCK-PER-CYCLE u2100) + +(define-data-var CONTRACT-OWNER principal tx-sender) + +(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)) + ) +) + +;; data maps and vars +;; +(define-map pools-map + { pool-id: uint } + { + poxl-token: principal, ;; token to be stacked + reward-token: principal, ;; reward token + start-cycle: uint + } +) + +(define-map pools-data-map + { + poxl-token: principal, + reward-token: principal, + start-cycle: uint + } + { + total-supply: uint, + fee-to-address: principal, + pool-token: principal, + reward-cycles: (list 32 uint) + } +) + +(define-data-var pool-count uint u0) +(define-data-var pools-list (list 2000 uint) (list)) + +;; private functions +;; +(define-private (sum-stacking-reward (reward-cycle uint) (sum-so-far uint)) + (+ sum-so-far (get-stacking-reward (get-user-id) reward-cycle)) +) + +;; to be replaced by proper calls to CityCoins +(define-private (get-stacking-reward (reward-cycle uint)) u1) +(define-private (register-user) true) +(define-private (get-user-id) u1) +(define-private (get-reward-cycle (stack-height uint)) u0) +(define-private (stack-tokens (amount-tokens uint) (lock-period uint)) true) +(define-private (get-first-stacks-block-in-reward-cycle (reward-cycle uint)) u1) +(define-private (claim-stacking-reward (reward-cycle uint)) true) + +;; public functions +;; +(define-read-only (get-pool-count) + (ok (var-get pool-count)) +) + +(define-read-only (get-pool-contracts (pool-id uint)) + (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) invalid-pool-err)) +) + +(define-read-only (get-pools) + (ok (map get-pool-contracts (var-get pools-list))) +) + +(define-read-only (get-pool-details (poxl-token-trait ) (reward-token-trait ) (start-cycle uint)) + (ok (unwrap! (map-get? pools-data-map { poxl-token: (contract-of poxl-token-trait), reward-token: (contract-of reward-token-trait), start-cycle: start-cycle }) invalid-pool-err)) +) + +(define-read-only (get-balance (poxl-token-trait ) (reward-token-trait ) (start-cycle uint)) + (ok (get total-supply (unwrap! (map-get? pools-data-map { poxl-token: (contract-of poxl-token-trait), reward-token: (contract-of reward-token-trait), start-cycle: start-cycle }) invalid-pool-err))) +) + +(define-public (create-pool (poxl-token-trait ) (reward-token-trait ) (reward-cycles (list 32 uint)) (yield-token ) (multisig )) + (let + ( + (pool-id (+ (var-get pool-count) u1)) + (poxl-token (contract-of poxl-token-trait)) + (reward-token (contract-of reward-token-trait)) + (pool-data { + total-supply: u0, + fee-to-address: (contract-of multisig), + pool-token: (contract-of yield-token), + reward-cycles: reward-cycles + }) + (start-cycle (default-to u0 (element-at reward-cycles u0))) + ) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + + ;; register if not registered + (as-contract (register-user)) + + (asserts! (is-none (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle })) pool-already-exists-err) + + (map-set pools-map { pool-id: pool-id } { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) + (map-set pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle } pool-data) + + (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) too-many-pools-err)) + (var-set pool-count pool-id) + (print { object: "pool", action: "created", pool-data: pool-data }) + (ok true) + ) +) + +(define-public (add-to-position (poxl-token-trait ) (reward-token-trait ) (start-cycle uint) (yield-token ) (dx uint)) + (let + ( + (poxl-token (contract-of poxl-token-trait)) + (reward-token (contract-of reward-token-trait)) + (pool (unwrap! (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) invalid-pool-err)) + (total-supply (get total-supply pool)) + (pool-updated (merge pool { + total-supply: (+ dx total-supply) + })) + ) + ;; check if stacking already started + (asserts! (is-eq start-cycle (get-reward-cycle block-height)) stacking-in-progress-err) + + ;; transfer dx to contract and send to stack + (try! (contract-call? poxl-token-trait transfer dx tx-sender (as-contract tx-sender) none)) + (as-contract (stack-tokens dx u32)) + + ;; mint pool token and send to tx-sender + (map-set pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle } pool-updated) + (try! (contract-call? yield-token mint tx-sender dx)) + (print { object: "pool", action: "liquidity-added", data: pool-updated }) + (ok true) + ) +) + +(define-public (reduce-position (poxl-token-trait ) (reward-token-trait ) (start-cycle uint) (yield-token ) (percent uint)) + (let + ( + (poxl-token (contract-of poxl-token-trait)) + (reward-token (contract-of reward-token-trait)) + (pool (unwrap! (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) invalid-pool-err)) + (shares (mul-down (unwrap-panic (contract-call? yield-token get-balance tx-sender)) percent)) + (total-supply (get total-supply pool)) + (pool-updated (merge pool { + total-supply: (- total-supply shares) + }) + ) + (reward-cycles (get reward-cycles pool)) + (shares-to-supply (div-down shares total-supply)) + (total-rewards (fold sum-stacking-reward reward-cycles u0)) + (portioned-rewards (unwrap! (contract-call? .math-fixed-point mul-down total-rewards shares-to-supply) math-call-err)) + ) + + (asserts! (> block-height (+ (get-first-stacks-block-in-reward-cycle (+ start-cycle u32)) BLOCK-PER-CYCLE)) stacking-in-progress-err) + + ;; the first call claims rewards + (as-contract (map claim-stacking-reward reward-cycles)) + + (try! (contract-call? poxl-token-trait transfer shares (as-contract tx-sender) tx-sender none)) + (try! (contract-call? reward-token-trait transfer portioned-rewards (as-contract tx-sender) tx-sender none)) + + (map-set pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle } pool-updated) + (try! (contract-call? yield-token burn tx-sender shares)) + (print { object: "pool", action: "liquidity-removed", data: pool-updated }) + (ok {poxl-token: shares, reward-token: portioned-rewards}) + ) +) \ No newline at end of file From 2683bf701ad471a88045de6383cbb904891b4c98 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 8 Nov 2021 13:37:35 +0800 Subject: [PATCH 02/25] stacking-pool connected to alex-staking --- ...cked-poxl-pool.clar => stacking-pool.clar} | 72 ++++++++++--------- init-js-tool/deploy.js | 2 +- 2 files changed, 38 insertions(+), 36 deletions(-) rename clarity/contracts/pool/{stacked-poxl-pool.clar => stacking-pool.clar} (79%) diff --git a/clarity/contracts/pool/stacked-poxl-pool.clar b/clarity/contracts/pool/stacking-pool.clar similarity index 79% rename from clarity/contracts/pool/stacked-poxl-pool.clar rename to clarity/contracts/pool/stacking-pool.clar index d5b9988c..4ae52400 100644 --- a/clarity/contracts/pool/stacked-poxl-pool.clar +++ b/clarity/contracts/pool/stacking-pool.clar @@ -10,22 +10,10 @@ ;; (define-constant ONE_8 u100000000) ;; 8 decimal places -(define-constant invalid-pool-err (err u2001)) -(define-constant no-liquidity-err (err u2002)) -(define-constant invalid-liquidity-err (err u2003)) -(define-constant transfer-x-failed-err (err u3001)) -(define-constant transfer-y-failed-err (err u3002)) -(define-constant pool-already-exists-err (err u2000)) -(define-constant too-many-pools-err (err u2004)) -(define-constant percent-greater-than-one (err u5000)) -(define-constant no-fee-x-err (err u2005)) -(define-constant no-fee-y-err (err u2006)) -(define-constant already-expiry-err (err u2010)) -(define-constant weighted-equation-call-err (err u2009)) -(define-constant math-call-err (err u2010)) -(define-constant internal-function-call-err (err u1001)) -(define-constant internal-get-weight-err (err u2012)) -(define-constant stacking-in-progress-err (err u2018)) +(define-constant ERR-INVALID-POOL (err u2001)) +(define-constant ERR-POOL-ALREADY-EXISTS (err u2000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-STACKING-IN-PROGRESS (err u2018)) (define-constant BLOCK-PER-CYCLE u2100) @@ -77,13 +65,27 @@ ) ;; to be replaced by proper calls to CityCoins -(define-private (get-stacking-reward (reward-cycle uint)) u1) -(define-private (register-user) true) -(define-private (get-user-id) u1) -(define-private (get-reward-cycle (stack-height uint)) u0) -(define-private (stack-tokens (amount-tokens uint) (lock-period uint)) true) -(define-private (get-first-stacks-block-in-reward-cycle (reward-cycle uint)) u1) -(define-private (claim-stacking-reward (reward-cycle uint)) true) +(define-private (get-stacking-reward (reward-cycle uint)) + (contract-call? .alex-reserve-pool get-staking-reward reward-cycle) +) +(define-private (register-user) + (as-contract (contract-call? .alex-reserve-pool register-user none)) +) +(define-private (get-user-id) + (default-to u0 (contract-call? .alex-reserve-pool get-user-id (as-contract tx-sender))) +) +(define-private (get-reward-cycle (stack-height uint)) + (contract-call? .alex-reserve-pool get-reward-cycle stack-height) +) +(define-private (stack-tokens (amount-tokens uint) (lock-period uint)) + (as-contract (contract-call? .alex-reserve-pool stake-tokens amount-tokens lock-period)) +) +(define-private (get-first-stacks-block-in-reward-cycle (reward-cycle uint)) + (contract-call? .alex-reserve-pool get-first-stacks-block-in-reward-cycle reward-cycle) +) +(define-private (claim-stacking-reward (reward-cycle uint)) + (as-contract (contract-call? .alex-reserve-pool claim-staking-reward reward-cycle)) +) ;; public functions ;; @@ -92,7 +94,7 @@ ) (define-read-only (get-pool-contracts (pool-id uint)) - (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) invalid-pool-err)) + (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) ERR-INVALID-POOL)) ) (define-read-only (get-pools) @@ -100,11 +102,11 @@ ) (define-read-only (get-pool-details (poxl-token-trait ) (reward-token-trait ) (start-cycle uint)) - (ok (unwrap! (map-get? pools-data-map { poxl-token: (contract-of poxl-token-trait), reward-token: (contract-of reward-token-trait), start-cycle: start-cycle }) invalid-pool-err)) + (ok (unwrap! (map-get? pools-data-map { poxl-token: (contract-of poxl-token-trait), reward-token: (contract-of reward-token-trait), start-cycle: start-cycle }) ERR-INVALID-POOL)) ) (define-read-only (get-balance (poxl-token-trait ) (reward-token-trait ) (start-cycle uint)) - (ok (get total-supply (unwrap! (map-get? pools-data-map { poxl-token: (contract-of poxl-token-trait), reward-token: (contract-of reward-token-trait), start-cycle: start-cycle }) invalid-pool-err))) + (ok (get total-supply (unwrap! (map-get? pools-data-map { poxl-token: (contract-of poxl-token-trait), reward-token: (contract-of reward-token-trait), start-cycle: start-cycle }) ERR-INVALID-POOL))) ) (define-public (create-pool (poxl-token-trait ) (reward-token-trait ) (reward-cycles (list 32 uint)) (yield-token ) (multisig )) @@ -124,14 +126,14 @@ (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) ;; register if not registered - (as-contract (register-user)) + (register-user) - (asserts! (is-none (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle })) pool-already-exists-err) + (asserts! (is-none (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle })) ERR-POOL-ALREADY-EXISTS) (map-set pools-map { pool-id: pool-id } { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) (map-set pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle } pool-data) - (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) too-many-pools-err)) + (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) (var-set pool-count pool-id) (print { object: "pool", action: "created", pool-data: pool-data }) (ok true) @@ -143,18 +145,18 @@ ( (poxl-token (contract-of poxl-token-trait)) (reward-token (contract-of reward-token-trait)) - (pool (unwrap! (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) invalid-pool-err)) + (pool (unwrap! (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) ERR-INVALID-POOL)) (total-supply (get total-supply pool)) (pool-updated (merge pool { total-supply: (+ dx total-supply) })) ) ;; check if stacking already started - (asserts! (is-eq start-cycle (get-reward-cycle block-height)) stacking-in-progress-err) + (asserts! (is-eq start-cycle (get-reward-cycle block-height)) ERR-STACKING-IN-PROGRESS) ;; transfer dx to contract and send to stack (try! (contract-call? poxl-token-trait transfer dx tx-sender (as-contract tx-sender) none)) - (as-contract (stack-tokens dx u32)) + (stack-tokens dx u32) ;; mint pool token and send to tx-sender (map-set pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle } pool-updated) @@ -169,7 +171,7 @@ ( (poxl-token (contract-of poxl-token-trait)) (reward-token (contract-of reward-token-trait)) - (pool (unwrap! (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) invalid-pool-err)) + (pool (unwrap! (map-get? pools-data-map { poxl-token: poxl-token, reward-token: reward-token, start-cycle: start-cycle }) ERR-INVALID-POOL)) (shares (mul-down (unwrap-panic (contract-call? yield-token get-balance tx-sender)) percent)) (total-supply (get total-supply pool)) (pool-updated (merge pool { @@ -182,10 +184,10 @@ (portioned-rewards (unwrap! (contract-call? .math-fixed-point mul-down total-rewards shares-to-supply) math-call-err)) ) - (asserts! (> block-height (+ (get-first-stacks-block-in-reward-cycle (+ start-cycle u32)) BLOCK-PER-CYCLE)) stacking-in-progress-err) + (asserts! (> block-height (+ (get-first-stacks-block-in-reward-cycle (+ start-cycle u32)) BLOCK-PER-CYCLE)) ERR-STACKING-IN-PROGRESS) ;; the first call claims rewards - (as-contract (map claim-stacking-reward reward-cycles)) + (map claim-stacking-reward reward-cycles) (try! (contract-call? poxl-token-trait transfer shares (as-contract tx-sender) tx-sender none)) (try! (contract-call? reward-token-trait transfer portioned-rewards (as-contract tx-sender) tx-sender none)) diff --git a/init-js-tool/deploy.js b/init-js-tool/deploy.js index 32e692da..0abbe411 100644 --- a/init-js-tool/deploy.js +++ b/init-js-tool/deploy.js @@ -89,7 +89,7 @@ let contract_paths = [ // "multisig/multisig-ytp-yield-usda-132481-usda.clar", // "flash-loan-user-margin-usda-wbtc-132481.clar", // "flash-loan-user-margin-wbtc-usda-132481.clar" - "faucet-v2.clar" + "faucet-v3.clar" ] async function get_version(){ From e128464cb596690765487afaa745665d1d158821 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Fri, 12 Nov 2021 11:08:09 +0800 Subject: [PATCH 03/25] working --- .../multisig/multisig-ytp-yield-usda.clar | 272 ++++++++++++++++++ .../contracts/pool-token/ytp-yield-usda.clar | 125 ++++++++ clarity/contracts/pool/yield-token-pool.clar | 182 ++++++------ .../traits/trait-semi-fungible-token.clar | 33 +++ clarity/contracts/traits/trait-sip-010.clar | 2 +- .../contracts/traits/trait-yield-token.clar | 6 +- clarity/contracts/yield-token/yield-usda.clar | 125 ++++++++ init-js-tool/faucet.js | 10 +- init-js-tool/index.js | 4 +- 9 files changed, 653 insertions(+), 106 deletions(-) create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-usda.clar create mode 100644 clarity/contracts/traits/trait-semi-fungible-token.clar create mode 100644 clarity/contracts/yield-token/yield-usda.clar diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar new file mode 100644 index 00000000..6015e1b5 --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -0,0 +1,272 @@ +(impl-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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, + expiry: uint, + 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-yield-token: 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, expiry: uint } { 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token + } + ) + (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-usda-11520 new-fee-rate-yield-token)) + + (ok true) + ) +) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar new file mode 100644 index 00000000..39235125 --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -0,0 +1,125 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-fungible-token ytp-yield-usda) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal .yield-token-pool) + +(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)) + ) +) + +(define-constant err-owner-only (err u100)) +(define-constant err-insufficient-balance (err u1)) +(define-constant err-invalid-sender (err u4)) + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance ytp-yield-usda who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply ytp-yield-usda)) +) + +(define-read-only (get-decimals (token-id uint)) + (ok u0) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) err-invalid-sender) + (asserts! (<= amount sender-balance) err-insufficient-balance) + (try! (ft-transfer? semi-fungible-token amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) + (begin + (try! (transfer token-id amount sender recipient)) + (print memo) + (ok true) + ) +) + +(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) +) + +(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) + (fold transfer-many-iter transfers (ok true)) +) + +(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) +) + +(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) + (fold transfer-many-memo-iter transfers (ok true)) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (try! (ft-mint? ytp-yield-usda amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (try! (ft-burn? ytp-yield-usda amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) \ No newline at end of file diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 17d8577a..c3d86b55 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -1,8 +1,7 @@ (impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait pool-token-trait .trait-pool-token.pool-token-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) (use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) ;; yield-token-pool (define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places @@ -51,12 +50,14 @@ { pool-id: uint } { yield-token: principal, ;; yield-token, dy + expiry: uint } ) (define-map pools-data-map { yield-token: principal + expiry: uint } { total-supply: uint, @@ -68,7 +69,6 @@ fee-rate-token: uint, fee-rate-yield-token: uint, fee-rebate: uint, - expiry: uint, listed: uint, oracle-enabled: bool, oracle-average: uint, @@ -141,20 +141,19 @@ ;; @desc get-pool-details ;; @param the-yield-token; yield-token ;; @returns (response (tuple) uint) -(define-read-only (get-pool-details (the-yield-token )) - (ok (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) +(define-read-only (get-pool-details (the-yield-token ) (expiry uint)) + (ok (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) ;; @desc get-yield ;; @desc note yield is not annualised ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-yield (the-yield-token )) +(define-read-only (get-yield (the-yield-token ) (expiry uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry}) ERR-INVALID-POOL-ERR)) (listed (get listed pool)) (balance-token (get balance-token pool)) (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) @@ -167,12 +166,11 @@ ;; @desc get-price ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-price (the-yield-token )) +(define-read-only (get-price (the-yield-token ) (expiry uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (listed (get listed pool)) (balance-token (get balance-token pool)) (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) @@ -185,8 +183,8 @@ ;; @desc get-oracle-enabled ;; @param the-yield-token; yield-token ;; @returns (response bool uint) -(define-read-only (get-oracle-enabled (the-yield-token )) - (ok (get oracle-enabled (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR))) +(define-read-only (get-oracle-enabled (the-yield-token ) (expiry uint)) + (ok (get oracle-enabled (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) ;; @desc set-oracle-enabled @@ -194,15 +192,15 @@ ;; @restricted CONTRACT-OWNER ;; @param the-yield-token; yield-token ;; @returns (response bool uint) -(define-public (set-oracle-enabled (the-yield-token )) +(define-public (set-oracle-enabled (the-yield-token ) (expiry uint)) (let ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) 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 { yield-token: (contract-of the-yield-token) } pool-updated) + (map-set pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry } pool-updated) (ok true) ) ) @@ -211,18 +209,18 @@ ;; @desc returns the moving average used to determine oracle price ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-oracle-average (the-yield-token )) - (ok (get oracle-average (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR))) +(define-read-only (get-oracle-average (the-yield-token ) (expiry uint)) + (ok (get oracle-average (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) ;; @desc set-oracle-average ;; @restricted CONTRACT-OWNER ;; @param the-yield-token; yield-token ;; @returns (response bool uint) -(define-public (set-oracle-average (the-yield-token ) (new-oracle-average uint)) +(define-public (set-oracle-average (the-yield-token ) (expiry uint) (new-oracle-average uint)) (let ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) (pool-updated (merge pool { oracle-average: new-oracle-average, oracle-resilient: (try! (get-oracle-instant the-yield-token)) @@ -231,7 +229,7 @@ (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 { yield-token: (contract-of the-yield-token) } pool-updated) + (map-set pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry } pool-updated) (ok true) ) ) @@ -240,10 +238,10 @@ ;; @desc price-oracle that is less up to date but more resilient to manipulation ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-oracle-resilient (the-yield-token )) +(define-read-only (get-oracle-resilient (the-yield-token ) (expiry uint)) (let ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) (ok (+ (mul-down (- ONE_8 (get oracle-average pool)) (try! (get-oracle-instant the-yield-token))) @@ -255,8 +253,8 @@ ;; @desc price-oracle that is more up to date but less resilient to manipulation ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-oracle-instant (the-yield-token )) - (ok (div-down ONE_8 (try! (get-price the-yield-token)))) +(define-read-only (get-oracle-instant (the-yield-token ) (expiry uint)) + (ok (div-down ONE_8 (try! (get-price the-yield-token expiry)))) ) ;; @desc create-pool @@ -268,17 +266,16 @@ ;; @param dx; amount of token added ;; @param dy; amount of yield-token added ;; @returns (response bool uint) -(define-public (create-pool (the-yield-token ) (the-token ) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) +(define-public (create-pool (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) (begin (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - ;; create pool only if the correct pair - (asserts! (is-eq (try! (contract-call? the-yield-token get-token)) (contract-of the-token)) ERR-INVALID-POOL-ERR) - (asserts! (is-none (map-get? pools-data-map { yield-token: (contract-of the-yield-token) })) ERR-POOL-ALREADY-EXISTS) + ;; ;; create pool only if the correct pair + ;; (asserts! (is-eq (try! (contract-call? the-yield-token get-token)) (contract-of the-token)) ERR-INVALID-POOL-ERR) + (asserts! (is-none (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry })) ERR-POOL-ALREADY-EXISTS) (let ( (yield-token (contract-of the-yield-token)) (pool-id (+ (var-get pool-count) u1)) - (expiry (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) (pool-data { total-supply: u0, balance-token: u0, @@ -289,7 +286,6 @@ fee-rate-yield-token: u0, fee-rate-token: u0, fee-rebate: u0, - expiry: (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR), listed: (* block-height ONE_8), oracle-enabled: false, oracle-average: u0, @@ -297,15 +293,15 @@ }) ) - (map-set pools-map { pool-id: pool-id } { yield-token: yield-token }) - (map-set pools-data-map { yield-token: yield-token } pool-data) + (map-set pools-map { pool-id: pool-id } { yield-token: yield-token, expiry: expiry }) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-data) (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) (var-set pool-count pool-id) ;; ;; if yield-token added has a longer expiry than current max-expiry, update max-expiry (to expiry + one block). ;; (var-set max-expiry (if (< (var-get max-expiry) expiry) (+ expiry ONE_8) (var-get max-expiry))) - (try! (add-to-position the-yield-token the-token the-pool-token dx)) + (try! (add-to-position the-yield-token expiry the-token the-pool-token dx)) (print { object: "pool", action: "created", data: pool-data }) (ok true) @@ -321,15 +317,15 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param dx; amount of token added (part of which will be used to buy yield-token) ;; @returns (response (tuple uint uint uint uint) uint) -(define-public (buy-and-add-to-position (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) +(define-public (buy-and-add-to-position (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (dx uint)) (let ( - (dy-act (get dy-act (try! (get-token-given-position the-yield-token dx)))) - (dx-adjusted (- dx (div-down dx (+ dx (try! (get-x-given-y the-yield-token dy-act)))))) + (dy-act (get dy-act (try! (get-token-given-position the-yield-token expiry dx)))) + (dx-adjusted (- dx (div-down dx (+ dx (try! (get-x-given-y the-yield-token expiry dy-act)))))) (dx-to-buy-dy-adjusted (- dx dx-adjusted)) ) - (and (> dy-act u0) (is-ok (swap-x-for-y the-yield-token the-token dx-to-buy-dy-adjusted none))) - (add-to-position the-yield-token the-token the-pool-token dx-adjusted) + (and (> dy-act u0) (is-ok (swap-x-for-y the-yield-token expiry the-token dx-to-buy-dy-adjusted none))) + (add-to-position the-yield-token expiry the-token the-pool-token dx-adjusted) ) ) @@ -340,14 +336,14 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param dx; amount of token added ;; @returns (response (tuple uint uint uint uint) uint) -(define-public (add-to-position (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) +(define-public (add-to-position (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (dx uint)) (begin ;; dx must be greater than zero (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-token (get balance-token pool)) (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) @@ -371,10 +367,10 @@ ;; send x to vault (unwrap! (contract-call? the-token transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) ;; send y to vault - (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer expiry new-dy-act tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) ;; mint pool token and send to tx-sender - (map-set pools-data-map { yield-token: yield-token } pool-updated) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) (try! (contract-call? the-pool-token mint tx-sender new-supply)) (print { object: "pool", action: "liquidity-added", data: pool-updated }) (ok {supply: new-supply, balance-token: dx, balance-yield-token: new-dy-act, balance-virtual: new-dy-vir}) @@ -389,13 +385,13 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param percent; percentage of pool token held to reduce ;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position (the-yield-token ) (the-token ) (the-pool-token ) (percent uint)) +(define-public (reduce-position (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (percent uint)) (begin (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-token (get balance-token pool)) (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) @@ -418,9 +414,9 @@ (asserts! (is-eq (get pool-token pool) (contract-of the-pool-token)) ERR-INVALID-POOL-TOKEN) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-yield the-yield-token dy-act tx-sender))) + (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-yield the-yield-token expiry dy-act tx-sender))) - (map-set pools-data-map { yield-token: yield-token } pool-updated) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) (try! (contract-call? the-pool-token burn tx-sender shares)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: dx, dy: dy-act}) @@ -438,14 +434,14 @@ ;; @param the-pool-token-to-roll; pool token representing ownership of the pool to roll to ;; @returns (response (tuple uint uint) uint) (define-public (roll-position - (the-yield-token ) (the-token ) (the-pool-token ) (percent uint) - (the-yield-token-to-roll ) (the-pool-token-to-roll )) + (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (percent uint) + (expiry-to-roll uint) (the-pool-token-to-roll )) (let ( - (reduce-data (unwrap! (reduce-position the-yield-token the-token the-pool-token percent) (err u11111))) - (dy-to-dx (get dx (unwrap! (swap-y-for-x the-yield-token the-token (get dy reduce-data) none) (err u22222)))) + (reduce-data (unwrap! (reduce-position the-yield-token expiry the-token the-pool-token percent) (err u11111))) + (dy-to-dx (get dx (unwrap! (swap-y-for-x the-yield-token expiry the-token (get dy reduce-data) none) (err u22222)))) ) - (buy-and-add-to-position the-yield-token-to-roll the-token the-pool-token-to-roll (+ (get dx reduce-data) dy-to-dx)) + (buy-and-add-to-position the-yield-token expiry-to-roll the-token the-pool-token-to-roll (+ (get dx reduce-data) dy-to-dx)) ) ) @@ -455,13 +451,13 @@ ;; @param dx; amount of token to swap ;; @param min-dy; optional, min amount of yield-token to receive ;; @returns (response (tuple uint uint) uint) -(define-public (swap-x-for-y (the-yield-token ) (the-token ) (dx uint) (min-dy (optional uint))) +(define-public (swap-x-for-y (the-yield-token ) (the-token ) (dx uint) (min-dy (optional uint))) (begin (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-token (get balance-token pool)) (balance-yield-token (get balance-yield-token pool)) @@ -493,7 +489,7 @@ (try! (contract-call? .alex-reserve-pool add-to-balance (contract-of the-token) (- fee fee-rebate))) ;; post setting - (map-set pools-data-map { yield-token: yield-token } pool-updated) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) (print { object: "pool", action: "swap-x-for-y", data: pool-updated }) (ok {dx: dx-net-fees, dy: dy}) ) @@ -506,13 +502,13 @@ ;; @param dy; amount of yield token to swap ;; @param min-dx; optional, min amount of token to receive ;; @returns (response (tuple uint uint) uint) -(define-public (swap-y-for-x (the-yield-token ) (the-token ) (dy uint) (min-dx (optional uint))) +(define-public (swap-y-for-x (the-yield-token ) (the-token ) (dy uint) (min-dx (optional uint))) (begin (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (balance-token (get balance-token pool)) (balance-yield-token (get balance-yield-token pool)) @@ -543,7 +539,7 @@ (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) ;; post setting - (map-set pools-data-map { yield-token: yield-token } pool-updated) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) (print { object: "pool", action: "swap-y-for-x", data: pool-updated }) (ok {dx: dx, dy: dy-net-fees}) ) @@ -553,8 +549,8 @@ ;; @desc get-fee-rebate ;; @param the-yield-token; yield token ;; @returns (response uint uint) -(define-read-only (get-fee-rebate (the-yield-token )) - (ok (get fee-rebate (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR))) +(define-read-only (get-fee-rebate (the-yield-token ) (expiry uint)) + (ok (get fee-rebate (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) ;; @desc set-fee-rebate @@ -562,15 +558,15 @@ ;; @param the-yield-token; yield token ;; @param fee-rebate; new fee-rebate ;; @returns (response bool uint) -(define-public (set-fee-rebate (the-yield-token ) (fee-rebate uint)) +(define-public (set-fee-rebate (the-yield-token ) (fee-rebate uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - (map-set pools-data-map { yield-token: yield-token } (merge pool { fee-rebate: fee-rebate })) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rebate: fee-rebate })) (ok true) ) ) @@ -578,11 +574,11 @@ ;; @desc get-fee-rate-yield-token ;; @param the-yield-token; yield token ;; @returns (response uint uint) -(define-read-only (get-fee-rate-yield-token (the-yield-token )) +(define-read-only (get-fee-rate-yield-token (the-yield-token ) (expiry uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (ok (get fee-rate-yield-token pool)) ) @@ -591,11 +587,11 @@ ;; @desc get-fee-rate-token ;; @param the-yield-token; yield token ;; @returns (response uint uint) -(define-read-only (get-fee-rate-token (the-yield-token )) +(define-read-only (get-fee-rate-token (the-yield-token ) (expiry uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (ok (get fee-rate-token pool)) ) @@ -606,15 +602,15 @@ ;; @param the-yield-token; yield token ;; @param fee-rate-yield-token; new fee-rate-yield-token ;; @returns (response bool uint) -(define-public (set-fee-rate-yield-token (the-yield-token ) (fee-rate-yield-token uint)) +(define-public (set-fee-rate-yield-token (the-yield-token ) (fee-rate-yield-token uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - (map-set pools-data-map { yield-token: yield-token } (merge pool { fee-rate-yield-token: fee-rate-yield-token })) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rate-yield-token: fee-rate-yield-token })) (ok true) ) @@ -625,15 +621,15 @@ ;; @param the-yield-token; yield token ;; @param fee-rate-token; new fee-rate-token ;; @returns (response bool uint) -(define-public (set-fee-rate-token (the-yield-token ) (fee-rate-token uint)) +(define-public (set-fee-rate-token (the-yield-token ) (fee-rate-token uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - (map-set pools-data-map { yield-token: yield-token } (merge pool { fee-rate-token: fee-rate-token })) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rate-token: fee-rate-token })) (ok true) ) ) @@ -641,11 +637,11 @@ ;; @desc get-fee-to-address ;; @param the-yield-token; yield token ;; @returns (response principal uint) -(define-read-only (get-fee-to-address (the-yield-token )) +(define-read-only (get-fee-to-address (the-yield-token ) (expiry uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (ok (get fee-to-address pool)) ) @@ -655,10 +651,10 @@ ;; @param the-yield-token; yield token ;; @param dx; amount of token being added ;; @returns (response uint uint) -(define-read-only (get-y-given-x (the-yield-token ) (dx uint)) +(define-read-only (get-y-given-x (the-yield-token ) (dx uint)) (let ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) (normalized-expiry (try! (get-t (get expiry pool) (get listed pool)))) (dy (try! (contract-call? .yield-token-equation get-y-given-x (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dx))) ) @@ -671,11 +667,11 @@ ;; @param the-yield-token; yield token ;; @param dy; amount of yield token being added ;; @returns (response uint uint) -(define-read-only (get-x-given-y (the-yield-token ) (dy uint)) +(define-read-only (get-x-given-y (the-yield-token ) (dy uint)) (let ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) (normalized-expiry (try! (get-t (get expiry pool) (get listed pool)))) ) (contract-call? .yield-token-equation get-x-given-y (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dy) @@ -686,12 +682,12 @@ ;; @param the-yield-token; yield token ;; @param price; target price ;; @returns (response uint uint) -(define-read-only (get-x-given-price (the-yield-token ) (price uint)) +(define-read-only (get-x-given-price (the-yield-token ) (price uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token) }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) @@ -706,12 +702,12 @@ ;; @param the-yield-token; yield token ;; @param price; target price ;; @returns (response uint uint) -(define-read-only (get-y-given-price (the-yield-token ) (price uint)) +(define-read-only (get-y-given-price (the-yield-token ) (price uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) @@ -726,12 +722,12 @@ ;; @param the-yield-token; yield token ;; @param yield; target yield ;; @returns (response uint uint) -(define-read-only (get-x-given-yield (the-yield-token ) (yield uint)) +(define-read-only (get-x-given-yield (the-yield-token ) (yield uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) @@ -746,12 +742,12 @@ ;; @param the-yield-token; yield token ;; @param yield; target yield ;; @returns (response uint uint) -(define-read-only (get-y-given-yield (the-yield-token ) (yield uint)) +(define-read-only (get-y-given-yield (the-yield-token ) (yield uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) @@ -766,12 +762,12 @@ ;; @param the-yield-token; yield token ;; @param dx; amount of token added ;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-token-given-position (the-yield-token ) (dx uint)) +(define-read-only (get-token-given-position (the-yield-token ) (dx uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) @@ -796,12 +792,12 @@ ;; @param the-yield-token; yield token ;; @param token; units of pool token to be minted ;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-mint (the-yield-token ) (token uint)) +(define-read-only (get-position-given-mint (the-yield-token ) (token uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) @@ -825,12 +821,12 @@ ;; @param the-yield-token; yield token ;; @param token; units of pool token to be burnt ;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-burn (the-yield-token ) (token uint)) +(define-read-only (get-position-given-burn (the-yield-token ) (token uint)) (let ( (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token }) ERR-INVALID-POOL-ERR)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar new file mode 100644 index 00000000..468c0db7 --- /dev/null +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -0,0 +1,33 @@ +(define-trait semi-fungible-token-trait + ( + ;; Get a token type balance of the passed principal. + (get-balance (uint principal) (response uint uint)) + + ;; Get the total SFT balance of the passed principal. + (get-overall-balance (principal) (response uint uint)) + + ;; Get the current total supply of a token type. + (get-total-supply (uint) (response uint uint)) + + ;; Get the overall SFT supply. + (get-overall-supply () (response uint uint)) + + ;; Get the number of decimal places of a token type. + (get-decimals (uint) (response uint uint)) + + ;; Get an optional token URI that represents metadata for a specific token. + (get-token-uri (uint) (response (optional (string-utf8 256)) uint)) + + ;; Transfer from one principal to another. + (transfer (uint uint principal principal) (response bool uint)) + + ;; Transfer from one principal to another with a memo. + (transfer-memo (uint uint principal principal (buff 34)) (response bool uint)) + + ;; Transfer many tokens at once. + (transfer-many ((list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal})) (response bool uint)) + + ;; Transfer many tokens at once with memos. + (transfer-many-memo ((list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)})) (response bool uint)) + ) +) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-sip-010.clar b/clarity/contracts/traits/trait-sip-010.clar index cc331ee8..b6436a11 100644 --- a/clarity/contracts/traits/trait-sip-010.clar +++ b/clarity/contracts/traits/trait-sip-010.clar @@ -2,7 +2,7 @@ ;; trait-sip-010 ;; -;; sip-010-trait must implement fixed point - see get-decimals() +;; sip-010-trait (define-trait sip-010-trait ( ;; Transfer from the caller to a new principal diff --git a/clarity/contracts/traits/trait-yield-token.clar b/clarity/contracts/traits/trait-yield-token.clar index 6917a389..5716a8ec 100644 --- a/clarity/contracts/traits/trait-yield-token.clar +++ b/clarity/contracts/traits/trait-yield-token.clar @@ -1,9 +1,5 @@ - - (define-trait yield-token-trait ( - ;; sip-010 - ;; Transfer from the caller to a new principal (transfer (uint principal principal (optional (buff 34))) (response bool uint)) @@ -37,6 +33,6 @@ (get-token () (response principal uint)) ;; the expiry - (get-expiry () (response uint uint)) + (get-expiry () (response uint uint)) ) ) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar new file mode 100644 index 00000000..9fbeaf2a --- /dev/null +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -0,0 +1,125 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-fungible-token yield-usda) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal .collateral-rebalancing-pool) + +(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)) + ) +) + +(define-constant err-owner-only (err u100)) +(define-constant err-insufficient-balance (err u1)) +(define-constant err-invalid-sender (err u4)) + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance yield-usda who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply yield-usda)) +) + +(define-read-only (get-decimals (token-id uint)) + (ok u0) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) err-invalid-sender) + (asserts! (<= amount sender-balance) err-insufficient-balance) + (try! (ft-transfer? semi-fungible-token amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) + (begin + (try! (transfer token-id amount sender recipient)) + (print memo) + (ok true) + ) +) + +(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) +) + +(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) + (fold transfer-many-iter transfers (ok true)) +) + +(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) +) + +(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) + (fold transfer-many-memo-iter transfers (ok true)) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (try! (ft-mint? yield-usda amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (try! (ft-burn? yield-usda amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) \ No newline at end of file diff --git a/init-js-tool/faucet.js b/init-js-tool/faucet.js index 72d3e7df..872ea3b7 100644 --- a/init-js-tool/faucet.js +++ b/init-js-tool/faucet.js @@ -20,7 +20,7 @@ const { const privateKey = await getDeployerPK(); const txOptions = { contractAddress: process.env.DEPLOYER_ACCOUNT_ADDRESS, - contractName: 'faucet-v2', + contractName: 'faucet-v5', functionName: 'set-usda-amount', functionArgs: [ uintCV(amount) @@ -46,7 +46,7 @@ const { const privateKey = await getDeployerPK(); const txOptions = { contractAddress: process.env.DEPLOYER_ACCOUNT_ADDRESS, - contractName: 'faucet-v2', + contractName: 'faucet-v5', functionName: 'set-wbtc-amount', functionArgs: [ uintCV(amount) @@ -72,7 +72,7 @@ const { const privateKey = await getDeployerPK(); const txOptions = { contractAddress: process.env.DEPLOYER_ACCOUNT_ADDRESS, - contractName: 'faucet-v2', + contractName: 'faucet-v5', functionName: 'set-stx-amount', functionArgs: [ uintCV(amount) @@ -98,7 +98,7 @@ const { const privateKey = await getDeployerPK(); const txOptions = { contractAddress: process.env.DEPLOYER_ACCOUNT_ADDRESS, - contractName: 'faucet-v2', + contractName: 'faucet-v5', functionName: 'set-alex-amount', functionArgs: [ uintCV(amount) @@ -124,7 +124,7 @@ const { const privateKey = await getDeployerPK(); const txOptions = { contractAddress: process.env.DEPLOYER_ACCOUNT_ADDRESS, - contractName: 'faucet-v2', + contractName: 'faucet-v5', functionName: 'get-some-tokens', functionArgs: [ principalCV(recipient) diff --git a/init-js-tool/index.js b/init-js-tool/index.js index e60527c3..40748868 100644 --- a/init-js-tool/index.js +++ b/init-js-tool/index.js @@ -290,7 +290,7 @@ async function set_faucet_amounts() { await setUsdaAmount(500000e+8); await setWbtcAmount(5e+8); await setStxAmount(250e+8); - await setAlexAmount(10e+8) + await setAlexAmount(0); } async function get_some_token(recipient) { @@ -752,7 +752,7 @@ async function run() { // await mint_some_tokens(process.env.DEPLOYER_ACCOUNT_ADDRESS); // await mint_some_usda(process.env.DEPLOYER_ACCOUNT_ADDRESS + '.alex-reserve-pool'); // await mint_some_tokens(process.env.USER_ACCOUNT_ADDRESS); - // await get_some_token(process.env.USER_ACCOUNT_ADDRESS); + await get_some_token(process.env.USER_ACCOUNT_ADDRESS); // const _pools = { 0:_deploy[2], // 1:_deploy[3], From 39790a6d9f2a7451282f93c823d8ff983847b4f7 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Fri, 12 Nov 2021 13:28:15 +0800 Subject: [PATCH 04/25] working --- .../multisig/multisig-ytp-yield-usda.clar | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index 6015e1b5..fd1e44c9 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -75,10 +75,10 @@ ) ) -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token ) (expiry uint)) (default-to { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) ) ) @@ -88,6 +88,7 @@ { id: u0, proposer: DEFAULT_OWNER, + expiry: u0, title: u"", url: u"", is-open: false, @@ -103,8 +104,8 @@ ) ;; 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) +(define-read-only (is-token-accepted (token )) + (is-eq (contract-of token) .ytp-yield-usda) ) @@ -112,6 +113,7 @@ ;; Requires 10% of the supply in your wallet ;; Default voting period is 10 days (144 * 10 blocks) (define-public (propose + (expiry uint) (start-block-height uint) (title (string-utf8 256)) (url (string-utf8 256)) @@ -119,8 +121,8 @@ (new-fee-rate-yield-token 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)) + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda get-balance expiry tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) ONE_8)) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -132,6 +134,7 @@ { id: proposal-id, proposer: tx-sender, + expiry: expiry, title: title, url: url, is-open: true, From 971e98049502bd04af54f0183d5caae82521a507 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sat, 13 Nov 2021 01:02:52 +0800 Subject: [PATCH 05/25] initial coding done - test cases to be added --- clarity/Clarinet.toml | 148 ++-------- clarity/contracts/alex-vault.clar | 21 +- ...lash-loan-user-margin-usda-wbtc-11520.clar | 29 -- ...ash-loan-user-margin-usda-wbtc-121195.clar | 29 -- ...ash-loan-user-margin-usda-wbtc-132481.clar | 29 -- ...ash-loan-user-margin-usda-wbtc-161515.clar | 29 -- ...ash-loan-user-margin-usda-wbtc-200335.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-23040.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-23670.clar | 29 -- ...ash-loan-user-margin-usda-wbtc-240655.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-51840.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-59760.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-80875.clar | 29 -- ...lash-loan-user-margin-usda-wbtc-92160.clar | 29 -- ...ash-loan-user-margin-wbtc-usda-121195.clar | 29 -- ...ash-loan-user-margin-wbtc-usda-132481.clar | 29 -- ...ash-loan-user-margin-wbtc-usda-161515.clar | 29 -- ...ash-loan-user-margin-wbtc-usda-200335.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-23040.clar | 29 -- ...ash-loan-user-margin-wbtc-usda-240655.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-51840.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-80875.clar | 29 -- ...lash-loan-user-margin-wbtc-usda-92160.clar | 29 -- ... => flash-loan-user-margin-wbtc-usda.clar} | 8 +- .../key-token/key-usda-11520-wbtc.clar | 107 ------- .../key-token/key-usda-121195-wbtc.clar | 107 ------- .../key-token/key-usda-132481-wbtc.clar | 107 ------- .../key-token/key-usda-161515-wbtc.clar | 107 ------- .../key-token/key-usda-200335-wbtc.clar | 107 ------- .../key-token/key-usda-23040-wbtc.clar | 107 ------- .../key-usda-23040-yield-usda-74880.clar | 107 ------- .../key-token/key-usda-240655-wbtc.clar | 107 ------- .../key-token/key-usda-51840-wbtc.clar | 107 ------- .../key-token/key-usda-80875-wbtc.clar | 107 ------- .../key-token/key-usda-92160-wbtc.clar | 107 ------- .../contracts/key-token/key-usda-wbtc.clar | 127 ++++++++ .../key-token/key-wbtc-11520-usda.clar | 107 ------- .../key-token/key-wbtc-121195-usda.clar | 107 ------- .../key-token/key-wbtc-132481-usda.clar | 107 ------- .../key-token/key-wbtc-161515-usda.clar | 107 ------- .../key-token/key-wbtc-200335-usda.clar | 107 ------- .../key-token/key-wbtc-23040-usda.clar | 107 ------- .../key-token/key-wbtc-23670-usda.clar | 107 ------- .../key-token/key-wbtc-240655-usda.clar | 107 ------- .../key-token/key-wbtc-51840-usda.clar | 107 ------- .../key-token/key-wbtc-59760-usda.clar | 107 ------- .../key-token/key-wbtc-59760-wbtc.clar | 93 ------ .../key-token/key-wbtc-79760-usda.clar | 107 ------- .../key-token/key-wbtc-80875-usda.clar | 107 ------- .../key-token/key-wbtc-92160-usda.clar | 107 ------- clarity/contracts/margin-helper.clar | 8 +- .../multisig-crp-usda-121195-wbtc.clar | 277 ----------------- .../multisig-crp-usda-132481-wbtc.clar | 277 ----------------- .../multisig-crp-usda-161515-wbtc.clar | 277 ----------------- .../multisig-crp-usda-200335-wbtc.clar | 277 ----------------- .../multisig-crp-usda-23040-wbtc.clar | 277 ----------------- ...tisig-crp-usda-23040-yield-usda-74880.clar | 277 ----------------- .../multisig-crp-usda-240655-wbtc.clar | 277 ----------------- .../multisig-crp-usda-51840-wbtc.clar | 277 ----------------- .../multisig-crp-usda-80875-wbtc.clar | 277 ----------------- .../multisig-crp-usda-92160-wbtc.clar | 277 ----------------- ...-wbtc.clar => multisig-crp-usda-wbtc.clar} | 81 ++--- .../multisig-crp-wbtc-11520-usda.clar | 277 ----------------- .../multisig-crp-wbtc-121195-usda.clar | 277 ----------------- .../multisig-crp-wbtc-132481-usda.clar | 277 ----------------- .../multisig-crp-wbtc-161515-usda.clar | 277 ----------------- .../multisig-crp-wbtc-200335-usda.clar | 277 ----------------- .../multisig-crp-wbtc-23040-usda.clar | 277 ----------------- .../multisig-crp-wbtc-23670-usda.clar | 277 ----------------- .../multisig-crp-wbtc-240655-usda.clar | 277 ----------------- .../multisig-crp-wbtc-51840-usda.clar | 277 ----------------- .../multisig-crp-wbtc-59760-usda.clar | 277 ----------------- .../multisig-crp-wbtc-59760-wbtc.clar | 277 ----------------- .../multisig-crp-wbtc-79760-usda.clar | 279 ------------------ .../multisig-crp-wbtc-80875-usda.clar | 277 ----------------- .../multisig-crp-wbtc-92160-usda.clar | 277 ----------------- ...tisig-fwp-usda-23040-usda-74880-50-50.clar | 269 ----------------- .../multisig-lbp-alex-usda-90-10.clar | 2 +- .../multisig-ytp-yield-usda-11520-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-121195-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-132481-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-161515-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-200335-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-23040-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-240655-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-51840-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-80875-usda.clar | 272 ----------------- .../multisig-ytp-yield-usda-92160-usda.clar | 272 ----------------- .../multisig/multisig-ytp-yield-usda.clar | 48 +-- .../multisig-ytp-yield-wbtc-11520-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-121195-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-132481-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-161515-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-200335-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-23040-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-23670-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-240655-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-51840-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-59760-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-79760-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-80875-wbtc.clar | 272 ----------------- .../multisig-ytp-yield-wbtc-92160-wbtc.clar | 272 ----------------- .../fwp-usda-23040-usda-74880-50-50.clar | 97 ------ .../pool-token/ytp-yield-usda-11520-usda.clar | 97 ------ .../ytp-yield-usda-121195-usda.clar | 97 ------ .../ytp-yield-usda-132481-usda.clar | 97 ------ .../ytp-yield-usda-161515-usda.clar | 97 ------ .../ytp-yield-usda-200335-usda.clar | 97 ------ .../pool-token/ytp-yield-usda-23040-usda.clar | 97 ------ .../ytp-yield-usda-240655-usda.clar | 97 ------ .../pool-token/ytp-yield-usda-51840-usda.clar | 97 ------ .../pool-token/ytp-yield-usda-80875-usda.clar | 97 ------ .../pool-token/ytp-yield-usda-92160-usda.clar | 97 ------ .../contracts/pool-token/ytp-yield-usda.clar | 18 +- .../pool-token/ytp-yield-wbtc-11520-wbtc.clar | 97 ------ .../ytp-yield-wbtc-121195-wbtc.clar | 97 ------ .../ytp-yield-wbtc-132481-wbtc.clar | 97 ------ .../ytp-yield-wbtc-161515-wbtc.clar | 97 ------ .../ytp-yield-wbtc-200335-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-23040-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-23670-wbtc.clar | 97 ------ .../ytp-yield-wbtc-240655-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-51840-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-59760-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-79760-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-80875-wbtc.clar | 97 ------ .../pool-token/ytp-yield-wbtc-92160-wbtc.clar | 97 ------ clarity/contracts/pool/alex-reserve-pool.clar | 1 - .../pool/collateral-rebalancing-pool.clar | 46 ++- clarity/contracts/pool/yield-token-pool.clar | 135 ++++----- .../traits/trait-flash-loan-user.clar | 2 +- .../contracts/traits/trait-multisig-vote.clar | 17 +- .../traits/trait-semi-fungible-token.clar | 4 + clarity/contracts/traits/trait-vault.clar | 2 +- .../yield-token/yield-usda-11520.clar | 112 ------- .../yield-token/yield-usda-121195.clar | 112 ------- .../yield-token/yield-usda-132481.clar | 112 ------- .../yield-token/yield-usda-161515.clar | 112 ------- .../yield-token/yield-usda-200335.clar | 112 ------- .../yield-token/yield-usda-23040.clar | 112 ------- .../yield-token/yield-usda-240655.clar | 112 ------- .../yield-token/yield-usda-51840.clar | 112 ------- .../yield-token/yield-usda-59760.clar | 112 ------- .../yield-token/yield-usda-80875.clar | 112 ------- .../yield-token/yield-usda-92160.clar | 112 ------- clarity/contracts/yield-token/yield-usda.clar | 18 +- .../yield-token/yield-wbtc-11520.clar | 112 ------- .../yield-token/yield-wbtc-121195.clar | 112 ------- .../yield-token/yield-wbtc-132481.clar | 112 ------- .../yield-token/yield-wbtc-161515.clar | 112 ------- .../yield-token/yield-wbtc-200335.clar | 112 ------- .../yield-token/yield-wbtc-23040.clar | 112 ------- .../yield-token/yield-wbtc-23670.clar | 112 ------- .../yield-token/yield-wbtc-240655.clar | 112 ------- .../yield-token/yield-wbtc-51840.clar | 112 ------- .../yield-token/yield-wbtc-59760.clar | 112 ------- .../yield-token/yield-wbtc-79760.clar | 112 ------- .../yield-token/yield-wbtc-80875.clar | 113 ------- .../yield-token/yield-wbtc-92160.clar | 112 ------- init-js-tool/index.js | 14 +- 160 files changed, 370 insertions(+), 21792 deletions(-) delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-121195.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-132481.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-161515.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-200335.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-23040.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-240655.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-51840.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-80875.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc-92160.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-121195.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-132481.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-161515.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-200335.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-240655.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-51840.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-80875.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda-92160.clar rename clarity/contracts/{flash-loan-user-margin-wbtc-usda-11520.clar => flash-loan-user-margin-wbtc-usda.clar} (81%) delete mode 100644 clarity/contracts/key-token/key-usda-11520-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-121195-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-132481-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-161515-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-200335-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-23040-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-23040-yield-usda-74880.clar delete mode 100644 clarity/contracts/key-token/key-usda-240655-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-51840-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-80875-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-usda-92160-wbtc.clar create mode 100644 clarity/contracts/key-token/key-usda-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-11520-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-121195-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-132481-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-161515-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-200335-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-23040-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-23670-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-240655-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-51840-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-59760-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-59760-wbtc.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-79760-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-80875-usda.clar delete mode 100644 clarity/contracts/key-token/key-wbtc-92160-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-121195-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-132481-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-161515-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-200335-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-23040-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-23040-yield-usda-74880.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-240655-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-51840-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-80875-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-usda-92160-wbtc.clar rename clarity/contracts/multisig/{multisig-crp-usda-11520-wbtc.clar => multisig-crp-usda-wbtc.clar} (77%) delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-11520-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-121195-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-132481-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-161515-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-200335-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-23040-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-23670-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-240655-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-51840-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-59760-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-59760-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-79760-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-80875-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-92160-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-fwp-usda-23040-usda-74880-50-50.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-121195-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-132481-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-161515-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-200335-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-240655-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-51840-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-80875-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-usda-92160-usda.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-11520-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-121195-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-132481-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-161515-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-200335-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-23040-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-23670-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-240655-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-51840-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-59760-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-79760-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-80875-wbtc.clar delete mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc-92160-wbtc.clar delete mode 100644 clarity/contracts/pool-token/fwp-usda-23040-usda-74880-50-50.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-121195-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-132481-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-161515-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-200335-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-23040-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-240655-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-51840-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-80875-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-usda-92160-usda.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-121195-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-132481-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-161515-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-200335-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-23040-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-23670-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-240655-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-51840-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-59760-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-79760-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-80875-wbtc.clar delete mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc-92160-wbtc.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-11520.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-121195.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-132481.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-161515.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-200335.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-23040.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-240655.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-51840.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-59760.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-80875.clar delete mode 100644 clarity/contracts/yield-token/yield-usda-92160.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-11520.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-121195.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-132481.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-161515.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-200335.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-23040.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-23670.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-240655.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-51840.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-59760.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-79760.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-80875.clar delete mode 100644 clarity/contracts/yield-token/yield-wbtc-92160.clar diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index 4cf73249..bfdc770f 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -2,6 +2,10 @@ name = "alex-v1" requirements = [] +[contracts.trait-semi-fungible-token] +path = "contracts/traits/trait-semi-fungible-token.clar" +depends_on = [] + [contracts.margin-helper] path = "contracts/margin-helper.clar" depends_on = ["trait-sip-010", "trait-yield-token", "trait-flash-loan-user"] @@ -12,7 +16,7 @@ depends_on = ["token-alex", "token-usda", "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", "trait-yield-token"] +depends_on = ["trait-vault", "trait-sip-010", "trait-flash-loan-user", "math-fixed-point", "trait-ownable", "trait-semi-fungible-token"] [contracts.collateral-rebalancing-pool] path = "contracts/pool/collateral-rebalancing-pool.clar" @@ -26,10 +30,6 @@ depends_on = ["token-wbtc", "token-usda", "token-t-alex", "token-wstx"] path = "contracts/pool/fixed-weight-pool.clar" depends_on = ["trait-sip-010", "trait-pool-token", "trait-vault", "math-fixed-point", "weighted-equation", "token-alex", "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", "alex-reserve-pool", "token-usda", "fixed-weight-pool", "trait-multisig-vote"] @@ -50,10 +50,6 @@ depends_on = ["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.token-alex] path = "contracts/token/token-alex.clar" depends_on = ["trait-pool-token", "trait-ownable"] @@ -116,128 +112,40 @@ 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", "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.yield-usda-59760] -path = "contracts/yield-token/yield-usda-59760.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-usda", "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-79760-wbtc.clar" -depends_on = ["ytp-yield-wbtc-79760-wbtc", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-wbtc-79760"] +depends_on = ["trait-sip-010", "trait-vault", "trait-flash-loan-user", "math-fixed-point", "yield-token-equation", "trait-semi-fungible-token", "token-alex", "token-usda", "fixed-weight-pool", "alex-reserve-pool", "trait-multisig-vote"] -[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-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" +[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.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.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.ytp-yield-usda] +path = "contracts/pool-token/ytp-yield-usda.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] + +[contracts.multisig-fwp-wbtc-usda-50-50] +path = "contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar" +depends_on = [] + [contracts.multisig-lbp-alex-usda-90-10] path = "contracts/multisig/multisig-lbp-alex-usda-90-10.clar" depends_on = [] -[contracts.yield-wbtc-80875] -path = "contracts/yield-token/yield-wbtc-80875.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.multisig-ytp-yield-wbtc-80875-wbtc] -path = "contracts/multisig/multisig-ytp-yield-wbtc-80875-wbtc.clar" -depends_on = ["ytp-yield-wbtc-80875-wbtc", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-wbtc-80875"] +[contracts.multisig-crp-usda-wbtc] +path = "contracts/multisig/multisig-crp-usda-wbtc.clar" +depends_on = ["yield-usda", "key-usda-wbtc"] -[contracts.ytp-yield-wbtc-80875-wbtc] -path = "contracts/pool-token/ytp-yield-wbtc-80875-wbtc.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] - -[contracts.yield-usda-51840] -path = "contracts/yield-token/yield-usda-51840.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-usda", "trait-ownable"] - -[contracts.flash-loan-user-margin-wbtc-usda-51840] -path = "contracts/flash-loan-user-margin-wbtc-usda-51840.clar" -depends_on = ["token-usda", "token-wbtc", "yield-usda-51840", "key-usda-51840-wbtc", "trait-sip-010"] - -[contracts.key-usda-51840-wbtc] -path = "contracts/key-token/key-usda-51840-wbtc.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "token-wbtc", "trait-ownable"] - -[contracts.ytp-yield-usda-51840-usda] -path = "contracts/pool-token/ytp-yield-usda-51840-usda.clar" -depends_on = ["trait-sip-010", "trait-pool-token", "trait-ownable"] +[contracts.multisig-ytp-yield-usda] +path = "contracts/multisig/multisig-ytp-yield-usda.clar" +depends_on = ["ytp-yield-usda"] -[contracts.multisig-ytp-yield-usda-51840-usda] -path = "contracts/multisig/multisig-ytp-yield-usda-51840-usda.clar" -depends_on = ["ytp-yield-usda-51840-usda", "trait-yield-token", "trait-sip-010", "yield-token-pool", "yield-usda-51840"] +[contracts.key-usda-wbtc] +path = "contracts/key-token/key-usda-wbtc.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] -[contracts.multisig-crp-usda-51840-wbtc] -path = "contracts/multisig/multisig-crp-usda-51840-wbtc.clar" -depends_on = ["yield-usda-51840", "key-usda-51840-wbtc", "token-wbtc", "token-usda", "trait-sip-010"] +[contracts.yield-usda] +path = "contracts/yield-token/yield-usda.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] \ No newline at end of file diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index 2afc5ec6..5fb16b72 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -1,8 +1,7 @@ (impl-trait .trait-ownable.ownable-trait) (impl-trait .trait-vault.vault-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait pool-token-trait .trait-pool-token.pool-token-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) (use-trait flash-loan-user-trait .trait-flash-loan-user.flash-loan-user-trait) (define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places @@ -75,25 +74,16 @@ ) ) -(define-public (transfer-yield (token ) (amount uint) (recipient principal)) +(define-public (transfer-sft (token ) (token-id uint) (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (as-contract (unwrap! (contract-call? token transfer amount tx-sender recipient none) ERR-TRANSFER-FAILED)) - (ok true) - ) -) - -(define-public (transfer-pool (token ) (amount uint) (recipient principal)) - (begin - (try! (check-is-approved contract-caller)) - (as-contract (unwrap! (contract-call? token transfer amount tx-sender recipient none) ERR-TRANSFER-FAILED)) + (as-contract (unwrap! (contract-call? token transfer token-id amount tx-sender recipient) ERR-TRANSFER-FAILED)) (ok true) ) ) ;; perform flash loan -;; (define-public (flash-loan (flash-loan-user ) (token ) (amount uint) (memo (optional (string-utf8 256)))) -(define-public (flash-loan (flash-loan-user ) (token ) (amount uint)) +(define-public (flash-loan (flash-loan-user ) (token ) (amount uint) (memo (optional uint))) (let ( (pre-bal (unwrap! (get-balance token) ERR-INVALID-FLASH-LOAN)) @@ -109,8 +99,7 @@ (as-contract (unwrap! (contract-call? token transfer amount tx-sender recipient none) ERR-LOAN-TRANSFER-FAILED)) ;; flash-loan-user executes with loan received - ;; (try! (contract-call? flash-loan-user execute token amount memo)) - (try! (contract-call? flash-loan-user execute token amount)) + (try! (contract-call? flash-loan-user execute token amount memo)) ;; return the loan + fee (unwrap! (contract-call? token transfer amount-with-fee tx-sender (as-contract tx-sender) none) ERR-POST-LOAN-TRANSFER-FAILED) diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.clar deleted file mode 100644 index 94bec467..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-11520.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-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-121195.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-121195.clar deleted file mode 100644 index 096cac2c..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-121195.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-121195 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-121195))) - (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-121195 .key-wbtc-121195-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-121195", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-132481.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-132481.clar deleted file mode 100644 index 00cdd2b0..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-132481.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-132481 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-132481))) - (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-132481 .key-wbtc-132481-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-132481", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-161515.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-161515.clar deleted file mode 100644 index afbf6cb5..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-161515.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-161515 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-161515))) - (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-161515 .key-wbtc-161515-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-161515", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-200335.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-200335.clar deleted file mode 100644 index 3a6a48da..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-200335.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-200335 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-200335))) - (gross-amount (unwrap! (contract-call? .math-fixed-point mul-up amount (unwrap! (contract-call? .math-fixed-point div-down price ltv) math-call-err)) math-call-err)) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-200335 .key-wbtc-200335-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)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-200335", 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 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-23670.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.clar deleted file mode 100644 index 6cd967ee..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-23670.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-59760 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-59760))) - (gross-amount (unwrap! (contract-call? .math-fixed-point mul-up amount (contract-call? .math-fixed-point div-down price ltv) math-call-err))) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-59760 .key-wbtc-59760-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-59760", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-240655.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-240655.clar deleted file mode 100644 index 206e17c6..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-240655.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-240655 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-240655))) - (gross-amount (unwrap! (contract-call? .math-fixed-point mul-up amount (unwrap! (contract-call? .math-fixed-point div-down price ltv) math-call-err)) math-call-err)) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-wbtc .token-usda .yield-wbtc-240655 .key-wbtc-240655-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)) - (try! (contract-call? .fixed-weight-pool swap-y-for-x .token-usda .token-wbtc u50000000 u50000000 swapped-token)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-240655", 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 deleted file mode 100644 index 2b1c4a7b..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-51840.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-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-59760.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.clar deleted file mode 100644 index b2459e92..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-59760.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-59760 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-59760))) - (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-59760 .key-wbtc-59760-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-59760", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc-80875.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc-80875.clar deleted file mode 100644 index e5a05c08..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-80875.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-80875 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-80875))) - (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-80875 .key-wbtc-80875-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-80875", 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 deleted file mode 100644 index 7f3bea33..00000000 --- a/clarity/contracts/flash-loan-user-margin-usda-wbtc-92160.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-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-121195.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-121195.clar deleted file mode 100644 index 748e8160..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-121195.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-121195 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-121195))) - (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-121195 .key-usda-121195-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-121195", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-132481.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-132481.clar deleted file mode 100644 index 0f5a881f..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-132481.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-132481 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-132481))) - (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-132481 .key-usda-132481-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-132481", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-161515.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-161515.clar deleted file mode 100644 index 42a6620a..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-161515.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-161515 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-161515))) - (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-161515 .key-usda-161515-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-161515", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda-200335.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-200335.clar deleted file mode 100644 index 6c007c09..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-200335.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-200335 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-200335))) - (gross-amount (unwrap! (contract-call? .math-fixed-point mul-up amount (unwrap! (contract-call? .math-fixed-point div-down price ltv) math-call-err)) math-call-err)) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-200335 .key-usda-200335-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)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-200335", 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-240655.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-240655.clar deleted file mode 100644 index 02f44a00..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-240655.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-240655 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-240655))) - (gross-amount (unwrap! (contract-call? .math-fixed-point mul-up amount (unwrap! (contract-call? .math-fixed-point div-down price ltv) math-call-err)) math-call-err)) - (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc .yield-usda-240655 .key-usda-240655-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)) - (try! (contract-call? .fixed-weight-pool swap-x-for-y .token-usda .token-wbtc u50000000 u50000000 swapped-token)) - ) - - (print { object: "flash-loan-user-margin-usda-wbtc-240655", 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 deleted file mode 100644 index 923f2059..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-51840.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-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-80875.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda-80875.clar deleted file mode 100644 index 1ed8ea08..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-80875.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-80875 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-80875))) - (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-80875 .key-usda-80875-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-80875", 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 deleted file mode 100644 index d0a10f53..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-92160.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-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/flash-loan-user-margin-wbtc-usda-11520.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar similarity index 81% rename from clarity/contracts/flash-loan-user-margin-wbtc-usda-11520.clar rename to clarity/contracts/flash-loan-user-margin-wbtc-usda.clar index 2f02a734..a23b55c8 100644 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda-11520.clar +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar @@ -6,16 +6,16 @@ (define-constant ONE_8 (pow u10 u8)) -(define-public (execute (token ) (amount uint)) +(define-public (execute (token ) (amount uint) (memo (optional 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))) + (price (try! (contract-call? .yield-token-pool get-price .yield-usda (default-to u0 memo)))) (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)))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc (default-to u0 memo) .yield-usda .key-usda-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)) @@ -23,7 +23,7 @@ (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 }) + (print { object: "flash-loan-user-margin-usda-wbtc", 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 deleted file mode 100644 index 7c5bd59b..00000000 --- a/clarity/contracts/key-token/key-usda-11520-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-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-121195-wbtc.clar b/clarity/contracts/key-token/key-usda-121195-wbtc.clar deleted file mode 100644 index fa5e81ef..00000000 --- a/clarity/contracts/key-token/key-usda-121195-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-121195-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 u12119500000000) -(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-121195-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-121195-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-121195-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-121195-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-121195-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-121195-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-121195-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-132481-wbtc.clar b/clarity/contracts/key-token/key-usda-132481-wbtc.clar deleted file mode 100644 index 5eb6d88d..00000000 --- a/clarity/contracts/key-token/key-usda-132481-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-132481-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 u13248100000000) -(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-132481-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-132481-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-132481-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-132481-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-132481-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-132481-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-132481-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-161515-wbtc.clar b/clarity/contracts/key-token/key-usda-161515-wbtc.clar deleted file mode 100644 index 03ffecd6..00000000 --- a/clarity/contracts/key-token/key-usda-161515-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-161515-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 u16151500000000) -(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-161515-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-161515-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-161515-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-161515-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-161515-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-161515-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-161515-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-200335-wbtc.clar b/clarity/contracts/key-token/key-usda-200335-wbtc.clar deleted file mode 100644 index 12d7bd1d..00000000 --- a/clarity/contracts/key-token/key-usda-200335-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-200335-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 u20033500000000) -(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-200335-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-200335-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-200335-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-200335-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-200335-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-200335-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-200335-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 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-23040-yield-usda-74880.clar b/clarity/contracts/key-token/key-usda-23040-yield-usda-74880.clar deleted file mode 100644 index da5d39e4..00000000 --- a/clarity/contracts/key-token/key-usda-23040-yield-usda-74880.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-yield-usda-74880) - -(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-yield-usda-74880))) -) - -(define-read-only (get-name) - (ok "key-usda-23040-yield-usda-74880") -) - -(define-read-only (get-symbol) - (ok "key-usda-23040-yield-usda-74880") -) - -(define-read-only (get-decimals) - (ok (unwrap-panic (contract-call? .yield-usda-23040 get-decimals))) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance key-usda-23040-yield-usda-74880 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-yield-usda-74880 (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-yield-usda-74880 (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-yield-usda-74880 (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-240655-wbtc.clar b/clarity/contracts/key-token/key-usda-240655-wbtc.clar deleted file mode 100644 index 3d76cda8..00000000 --- a/clarity/contracts/key-token/key-usda-240655-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-240655-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 u24065500000000) -(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-240655-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-240655-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-240655-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-240655-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-240655-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-240655-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-240655-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 deleted file mode 100644 index f3df36ba..00000000 --- a/clarity/contracts/key-token/key-usda-51840-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-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-80875-wbtc.clar b/clarity/contracts/key-token/key-usda-80875-wbtc.clar deleted file mode 100644 index 24e25976..00000000 --- a/clarity/contracts/key-token/key-usda-80875-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-80875-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 u8087500000000) -(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-80875-wbtc))) -) - -(define-read-only (get-name) - (ok "key-usda-80875-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-usda-80875-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-80875-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-80875-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-80875-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-80875-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 deleted file mode 100644 index 6a62781e..00000000 --- a/clarity/contracts/key-token/key-usda-92160-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-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-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar new file mode 100644 index 00000000..0e8c2247 --- /dev/null +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -0,0 +1,127 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + +(define-fungible-token key-usda-wbtc) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal .collateral-rebalancing-pool) + +(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)) + ) +) + + + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance key-usda-wbtc who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply key-usda-wbtc)) +) + +(define-read-only (get-decimals (token-id uint)) + (ok u0) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? key-usda-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) + (begin + (try! (transfer token-id amount sender recipient)) + (print memo) + (ok true) + ) +) + +(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) +) + +(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) + (fold transfer-many-iter transfers (ok true)) +) + +(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) +) + +(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) + (fold transfer-many-memo-iter transfers (ok true)) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (ft-mint? key-usda-wbtc amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (ft-burn? key-usda-wbtc amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) \ 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 deleted file mode 100644 index 8978a862..00000000 --- a/clarity/contracts/key-token/key-wbtc-11520-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-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-121195-usda.clar b/clarity/contracts/key-token/key-wbtc-121195-usda.clar deleted file mode 100644 index be64f2d7..00000000 --- a/clarity/contracts/key-token/key-wbtc-121195-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-121195-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 u12119500000000) -(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-121195-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-121195-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-121195-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-121195-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-121195-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-121195-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-121195-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-132481-usda.clar b/clarity/contracts/key-token/key-wbtc-132481-usda.clar deleted file mode 100644 index f0f0cfcd..00000000 --- a/clarity/contracts/key-token/key-wbtc-132481-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-132481-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 u13248100000000) -(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-132481-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-132481-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-132481-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-132481-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-132481-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-132481-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-132481-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-161515-usda.clar b/clarity/contracts/key-token/key-wbtc-161515-usda.clar deleted file mode 100644 index 8eeda19d..00000000 --- a/clarity/contracts/key-token/key-wbtc-161515-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-161515-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 u16151500000000) -(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-161515-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-161515-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-161515-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-161515-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-161515-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-161515-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-161515-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-200335-usda.clar b/clarity/contracts/key-token/key-wbtc-200335-usda.clar deleted file mode 100644 index dbcd8b41..00000000 --- a/clarity/contracts/key-token/key-wbtc-200335-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-200335-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 u20033500000000) -(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-200335-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-200335-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-200335-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-200335-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-200335-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-200335-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-200335-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 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-23670-usda.clar b/clarity/contracts/key-token/key-wbtc-23670-usda.clar deleted file mode 100644 index d92f6873..00000000 --- a/clarity/contracts/key-token/key-wbtc-23670-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-23670-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 u2367000000000) -(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-23670-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-23670-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-23670-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-23670-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-23670-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-23670-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-23670-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-240655-usda.clar b/clarity/contracts/key-token/key-wbtc-240655-usda.clar deleted file mode 100644 index f7ba940a..00000000 --- a/clarity/contracts/key-token/key-wbtc-240655-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-240655-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 u24065500000000) -(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-240655-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-240655-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-240655-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-240655-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-240655-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-240655-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-240655-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 deleted file mode 100644 index b64da3ef..00000000 --- a/clarity/contracts/key-token/key-wbtc-51840-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-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-59760-usda.clar b/clarity/contracts/key-token/key-wbtc-59760-usda.clar deleted file mode 100644 index 1abac997..00000000 --- a/clarity/contracts/key-token/key-wbtc-59760-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-59760-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 u5976000000000) -(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-59760-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-59760-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-59760-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-59760-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-59760-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-59760-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-59760-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-59760-wbtc.clar b/clarity/contracts/key-token/key-wbtc-59760-wbtc.clar deleted file mode 100644 index 303d549e..00000000 --- a/clarity/contracts/key-token/key-wbtc-59760-wbtc.clar +++ /dev/null @@ -1,93 +0,0 @@ -(impl-trait .trait-yield-token.yield-token-trait) -(impl-trait .trait-ownable.ownable-trait) - -(define-fungible-token key-wbtc-59760-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 u5976000000000) -(define-data-var underlying-token principal .token-key-wbtc-59760-wbtc) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(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 (ft-get-supply key-wbtc-59760-wbtc)) -) - -(define-read-only (get-name) - (ok "key-wbtc-59760-wbtc") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-59760-wbtc") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (ft-get-balance key-wbtc-59760-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-wbtc-59760-wbtc 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-59760-wbtc 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-59760-wbtc 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-79760-usda.clar b/clarity/contracts/key-token/key-wbtc-79760-usda.clar deleted file mode 100644 index 478159f2..00000000 --- a/clarity/contracts/key-token/key-wbtc-79760-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-79760-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 u7976000000000) -(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-79760-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-79760-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-79760-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-79760-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-79760-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-79760-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-79760-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-80875-usda.clar b/clarity/contracts/key-token/key-wbtc-80875-usda.clar deleted file mode 100644 index 6e239c9a..00000000 --- a/clarity/contracts/key-token/key-wbtc-80875-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-80875-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 u8087500000000) -(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-80875-usda))) -) - -(define-read-only (get-name) - (ok "key-wbtc-80875-usda") -) - -(define-read-only (get-symbol) - (ok "key-wbtc-80875-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-80875-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-80875-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-80875-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-80875-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 deleted file mode 100644 index b670a5a3..00000000 --- a/clarity/contracts/key-token/key-wbtc-92160-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-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/margin-helper.clar b/clarity/contracts/margin-helper.clar index 31d04f87..c95f99ad 100644 --- a/clarity/contracts/margin-helper.clar +++ b/clarity/contracts/margin-helper.clar @@ -1,13 +1,13 @@ (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) (use-trait flash-loan-user-trait .trait-flash-loan-user.flash-loan-user-trait) (define-constant ONE_8 (pow u10 u8)) -(define-public (roll-position (token ) (collateral ) (the-key-token ) (next-flash-loan-user )) +(define-public (roll-position (token ) (collateral ) (expiry uint) (the-key-token ) (flash-loan-user ) (expiry-to-roll uint)) (let ( - (reduce-data (try! (contract-call? .collateral-rebalancing-pool reduce-position-key token collateral the-key-token ONE_8))) + (reduce-data (try! (contract-call? .collateral-rebalancing-pool reduce-position-key token collateral expiry the-key-token ONE_8))) (collateral-amount (get dx reduce-data)) (token-amount (get dy reduce-data)) (token-to-collateral @@ -17,6 +17,6 @@ ) ) ) - (contract-call? .alex-vault flash-loan next-flash-loan-user collateral (+ collateral-amount token-to-collateral)) + (contract-call? .alex-vault flash-loan flash-loan-user collateral (+ collateral-amount token-to-collateral) (some expiry)) ) ) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-usda-121195-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-121195-wbtc.clar deleted file mode 100644 index 55093188..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-121195-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 yield-token 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-121195) (is-eq (contract-of token) .key-usda-121195-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-121195 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-121195-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-121195 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-121195-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-121195 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-121195-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 u12119500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u12119500000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-usda-132481-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-132481-wbtc.clar deleted file mode 100644 index af40bc0e..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-132481-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 yield-token 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-132481) (is-eq (contract-of token) .key-usda-132481-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-132481 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-132481-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-132481 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-132481-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-132481 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-132481-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 u13248100000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u13248100000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-usda-161515-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-161515-wbtc.clar deleted file mode 100644 index 42448dbc..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-161515-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 yield-token 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-161515) (is-eq (contract-of token) .key-usda-161515-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-161515 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-161515-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-161515 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-161515-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-161515 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-161515-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 u16151500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u16151500000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-usda-200335-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-200335-wbtc.clar deleted file mode 100644 index d3fc0dac..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-200335-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 yield-token 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-200335) (is-eq (contract-of token) .key-usda-200335-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-200335 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-200335-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-200335 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-200335-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-200335 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-200335-wbtc get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (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) - ) -) - -;; 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 u20033500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u20033500000000 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 deleted file mode 100644 index e4f467e2..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 yield-token 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-23040-yield-usda-74880.clar b/clarity/contracts/multisig/multisig-crp-usda-23040-yield-usda-74880.clar deleted file mode 100644 index 7303fe69..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-23040-yield-usda-74880.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 ayyield-usda-74880-yield-usda-74880 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 yield-token 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-yield-usda-74880)) -) - - -;; 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-yield-usda-74880 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-yield-usda-74880 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-yield-usda-74880 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 .yield-usda-74880 u2304000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .yield-usda-74880 u2304000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-usda-240655-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-240655-wbtc.clar deleted file mode 100644 index 863cfdcf..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-240655-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 yield-token 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-240655) (is-eq (contract-of token) .key-usda-240655-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-240655 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-240655-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-240655 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-240655-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-240655 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-240655-wbtc get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (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) - ) -) - -;; 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 u24065500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u24065500000000 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 deleted file mode 100644 index 1ae24ffb..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-51840-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 yield-token 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-80875-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-80875-wbtc.clar deleted file mode 100644 index 0dec17b1..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-80875-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 yield-token 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-80875) (is-eq (contract-of token) .key-usda-80875-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-80875 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-80875-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda-80875 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-80875-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-80875 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-80875-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 u8087500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc u8087500000000 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 deleted file mode 100644 index 0d5fce30..00000000 --- a/clarity/contracts/multisig/multisig-crp-usda-92160-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 yield-token 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-usda-11520-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar similarity index 77% rename from clarity/contracts/multisig/multisig-crp-usda-11520-wbtc.clar rename to clarity/contracts/multisig/multisig-crp-usda-wbtc.clar index 08ddb288..d19d1818 100644 --- a/clarity/contracts/multisig/multisig-crp-usda-11520-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar @@ -1,5 +1,5 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) ;; Alex voting for MultiSig DAO @@ -33,6 +33,7 @@ { id: uint, proposer: principal, + expiry: uint, title: (string-utf8 256), url: (string-utf8 256), is-open: bool, @@ -53,7 +54,7 @@ (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 }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal, expiry: uint } { amount: uint }) ;; Get all proposals in detail (define-read-only (get-proposals) @@ -73,10 +74,10 @@ ) ) -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token ) (expiry uint)) (default-to { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) ) ) @@ -86,6 +87,7 @@ { id: u0, proposer: DEFAULT_OWNER, + expiry: u0, title: u"", url: u"", is-open: false, @@ -101,15 +103,16 @@ ) ;; 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)) +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-usda) (is-eq (contract-of token) .key-usda-wbtc)) ) ;; Start a proposal ;; Requires 10% of the supply in your wallet ;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose +(define-public (propose + (expiry uint) (start-block-height uint) (title (string-utf8 256)) (url (string-utf8 256)) @@ -118,11 +121,11 @@ ) (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-yield-balance (unwrap-panic (contract-call? .yield-usda get-balance expiry tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance expiry 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-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) (total-supply (+ total-yield-supply total-key-supply)) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -135,6 +138,7 @@ { id: proposal-id, proposer: tx-sender, + expiry: expiry, title: title, url: url, is-open: true, @@ -152,12 +156,12 @@ ) ) -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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))) - + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token expiry))) ) ;; Can vote with corresponding pool token @@ -168,7 +172,7 @@ (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)) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -177,7 +181,7 @@ { 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) } + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token), expiry: expiry } { amount: (+ amount token-count)}) (ok amount) @@ -185,14 +189,12 @@ ) ) - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token expiry))) ) ;; Can vote with corresponding pool token (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) @@ -201,7 +203,7 @@ ;; 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)) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -211,7 +213,7 @@ { 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) } + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) , expiry: expiry } { amount: (+ amount token-count)}) (ok amount) ) @@ -219,14 +221,17 @@ ) (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)) - ) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) + (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) @@ -243,11 +248,12 @@ ;; 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)) +(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)) + (expiry (get expiry proposal)) + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) ) (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) @@ -255,7 +261,7 @@ (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))) + (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) (ok true) ) ) @@ -265,12 +271,13 @@ (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-x .token-usda .token-wbtc expiry new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc expiry 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 deleted file mode 100644 index 03cd0876..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-11520-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 yield-token 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-121195-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-121195-usda.clar deleted file mode 100644 index d1998dc6..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-121195-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 yield-token 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-121195) (is-eq (contract-of token) .key-wbtc-121195-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-121195 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-121195-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-121195 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-121195-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-121195 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-121195-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 u12119500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u12119500000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-132481-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-132481-usda.clar deleted file mode 100644 index 8a67d8b0..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-132481-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 yield-token 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-132481) (is-eq (contract-of token) .key-wbtc-132481-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-132481 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-132481-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-132481 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-132481-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-132481 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-132481-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 u13248100000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u13248100000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-161515-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-161515-usda.clar deleted file mode 100644 index a92e04f9..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-161515-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 yield-token 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-161515) (is-eq (contract-of token) .key-wbtc-161515-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-161515 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-161515-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-161515 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-161515-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-161515 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-161515-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 u16151500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u16151500000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-200335-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-200335-usda.clar deleted file mode 100644 index cf2a33a1..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-200335-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 yield-token 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-200335) (is-eq (contract-of token) .key-wbtc-200335-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-200335 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-200335-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-200335 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-200335-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-200335 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-200335-usda get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (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) - ) -) - -;; 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 u20033500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u20033500000000 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 2f622c2b..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 yield-token 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-23670-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-23670-usda.clar deleted file mode 100644 index 02183bce..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-23670-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 yield-token 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-23670) (is-eq (contract-of token) .key-wbtc-23670-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-23670 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-23670-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-23670 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-23670-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-23670 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-23670-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 u2367000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u2367000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-240655-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-240655-usda.clar deleted file mode 100644 index fbd233bd..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-240655-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 yield-token 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-240655) (is-eq (contract-of token) .key-wbtc-240655-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-240655 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-240655-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-240655 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-240655-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-240655 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-240655-usda get-total-supply))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (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) - ) -) - -;; 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 u24065500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u24065500000000 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 deleted file mode 100644 index c16852dd..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-51840-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 yield-token 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-59760-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-59760-usda.clar deleted file mode 100644 index 1287c127..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-59760-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 yield-token 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-59760) (is-eq (contract-of token) .key-wbtc-59760-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-59760 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-59760-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-59760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-59760-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-59760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-59760-usda get-total-supply))) - (total-supply (+ total-yield-supply total-key-supply)) - (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 u5976000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u5976000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-59760-wbtc.clar b/clarity/contracts/multisig/multisig-crp-wbtc-59760-wbtc.clar deleted file mode 100644 index e26b545e..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-59760-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 aywbtc-wbtc 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 yield-token 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-59760) (is-eq (contract-of token) .key-wbtc-59760-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-wbtc-59760 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-59760-wbtc get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-59760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-59760-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-wbtc-59760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-59760-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-wbtc .token-wbtc u5976000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-wbtc u5976000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-79760-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-79760-usda.clar deleted file mode 100644 index 8316e165..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-79760-usda.clar +++ /dev/null @@ -1,279 +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-CONTRACT-CHANGES (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 status-ok u10000) -(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 yield-token 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-79760) (is-eq (contract-of token) .key-wbtc-79760-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-79760 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-79760-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-79760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-79760-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-79760 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-79760-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 u5976000000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u5976000000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-80875-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-80875-usda.clar deleted file mode 100644 index 3a409098..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-80875-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 yield-token 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-80875) (is-eq (contract-of token) .key-wbtc-80875-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-80875 get-balance tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-80875-usda get-balance tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc-80875 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-80875-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-80875 get-total-supply))) - (total-key-supply (unwrap-panic (contract-call? .key-wbtc-80875-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 u8087500000000 new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda u8087500000000 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 deleted file mode 100644 index 9a3c2c99..00000000 --- a/clarity/contracts/multisig/multisig-crp-wbtc-92160-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 yield-token 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-fwp-usda-23040-usda-74880-50-50.clar b/clarity/contracts/multisig/multisig-fwp-usda-23040-usda-74880-50-50.clar deleted file mode 100644 index 9f58666f..00000000 --- a/clarity/contracts/multisig/multisig-fwp-usda-23040-usda-74880-50-50.clar +++ /dev/null @@ -1,269 +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, - 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) .fwp-usda-23040-usda-74880-50-50) -) - - -;; 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? .fwp-usda-23040-usda-74880-50-50 get-balance tx-sender))) - (total-supply (unwrap-panic (contract-call? .fwp-usda-23040-usda-74880-50-50 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: 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-supply (unwrap-panic (contract-call? .fwp-usda-23040-usda-74880-50-50 get-total-supply))) - (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)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .fixed-weight-pool set-fee-rate-x .yield-usda-23040 .yield-usda-74880 u50000000 u50000000 new-fee-rate-x)) - (try! (contract-call? .fixed-weight-pool set-fee-rate-y .yield-usda-23040 .yield-usda-74880 u50000000 u50000000 new-fee-rate-y)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar index 54d2dd4f..ba77462a 100644 --- a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar +++ b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar @@ -114,7 +114,7 @@ (title (string-utf8 256)) (url (string-utf8 256)) (new-fee-rate-x uint) - (new-fee-rate-y uint) + (new-fee-rate-y uint) ) (let ( (proposer-balance (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-balance tx-sender))) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-11520-usda.clar deleted file mode 100644 index 810f371a..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-11520-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-usda-11520 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-121195-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-121195-usda.clar deleted file mode 100644 index cfd6cc4b..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-121195-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-121195-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-121195-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-121195-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-yield-token: new-fee-rate-yield-token - } - ) - (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-121195-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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-121195 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-usda-121195 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-132481-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-132481-usda.clar deleted file mode 100644 index cad91b0a..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-132481-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-132481-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-132481-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-132481-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-yield-token: new-fee-rate-yield-token - } - ) - (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-132481-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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-132481 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-usda-132481 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-161515-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-161515-usda.clar deleted file mode 100644 index eb6ebd79..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-161515-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-161515-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-161515-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-161515-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-yield-token: new-fee-rate-yield-token - } - ) - (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-161515-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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-161515 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-usda-161515 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-200335-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-200335-usda.clar deleted file mode 100644 index 31768a4d..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-200335-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-200335-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-200335-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-200335-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-yield-token: new-fee-rate-yield-token - } - ) - (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-200335-usda get-total-supply)) ONE_8)) - (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)) 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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-200335 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-usda-200335 new-fee-rate-yield-token)) - - (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 91d2bdc3..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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-usda-23040 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-240655-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-240655-usda.clar deleted file mode 100644 index ad2dd4e2..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-240655-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-240655-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-240655-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-240655-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-yield-token: new-fee-rate-yield-token - } - ) - (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-240655-usda get-total-supply)) ONE_8)) - (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)) 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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-240655 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-usda-240655 new-fee-rate-yield-token)) - - (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 deleted file mode 100644 index fa8e6eb1..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-51840-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-usda-51840 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda-80875-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda-80875-usda.clar deleted file mode 100644 index 351d8ad6..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-80875-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-80875-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda-80875-usda get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda-80875-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-yield-token: new-fee-rate-yield-token - } - ) - (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-80875-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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-usda-80875 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-usda-80875 new-fee-rate-yield-token)) - - (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 deleted file mode 100644 index 5e59a8a5..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda-92160-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-usda-92160 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index fd1e44c9..339bf1d6 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -1,4 +1,4 @@ -(impl-trait .trait-multisig-vote.multisig-vote-trait) +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) (use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) @@ -152,12 +152,12 @@ ) ) -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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))) - + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token expiry))) ) ;; Can vote with corresponding pool token @@ -168,7 +168,7 @@ (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)) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -177,7 +177,7 @@ { 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) } + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token), expiry: expiry } { amount: (+ amount token-count)}) (ok amount) @@ -188,11 +188,12 @@ -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token expiry))) ) ;; Can vote with corresponding pool token (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) @@ -201,7 +202,7 @@ ;; 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)) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -211,7 +212,7 @@ { 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) } + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token), expiry: expiry } { amount: (+ amount token-count)}) (ok amount) ) @@ -219,12 +220,15 @@ ) (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)) - ) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) 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) @@ -241,11 +245,12 @@ ;; 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)) +(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)) + (expiry (get expiry proposal)) + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token expiry)) ONE_8)) ) (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) @@ -253,7 +258,7 @@ (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))) + (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) (ok true) ) ) @@ -262,13 +267,14 @@ (define-private (execute-proposal (proposal-id uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (new-fee-rate-token (get new-fee-rate-token proposal)) (new-fee-rate-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-usda-11520 new-fee-rate-yield-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-usda new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-usda new-fee-rate-yield-token)) (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 deleted file mode 100644 index 154f9eba..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-11520-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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))) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-wbtc-11520 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-121195-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-121195-wbtc.clar deleted file mode 100644 index bda22c9a..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-121195-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-121195-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-121195-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-121195-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-yield-token: new-fee-rate-yield-token - } - ) - (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-121195-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-121195 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-121195 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-132481-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-132481-wbtc.clar deleted file mode 100644 index 0e1b6b4f..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-132481-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-132481-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-132481-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-132481-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-yield-token: new-fee-rate-yield-token - } - ) - (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-132481-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-132481 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-132481 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-161515-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-161515-wbtc.clar deleted file mode 100644 index 24ba8039..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-161515-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-161515-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-161515-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-161515-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-yield-token: new-fee-rate-yield-token - } - ) - (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-161515-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-161515 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-161515 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-200335-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-200335-wbtc.clar deleted file mode 100644 index 2af5505e..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-200335-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-200335-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-200335-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-200335-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-yield-token: new-fee-rate-yield-token - } - ) - (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-200335-wbtc 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)) 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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-200335 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-200335 new-fee-rate-yield-token)) - - (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 9e39887b..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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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))) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-wbtc-23040 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23670-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23670-wbtc.clar deleted file mode 100644 index f9c47551..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-23670-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-23670-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23670-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-23670-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-yield-token: new-fee-rate-yield-token - } - ) - (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-23670-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-23670 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-23670 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-240655-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-240655-wbtc.clar deleted file mode 100644 index 8efd78d5..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-240655-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-240655-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-240655-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-240655-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-yield-token: new-fee-rate-yield-token - } - ) - (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-240655-wbtc 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)) 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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-240655 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-240655 new-fee-rate-yield-token)) - - (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 deleted file mode 100644 index 9939b8eb..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-51840-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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))) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-wbtc-51840 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-59760-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-59760-wbtc.clar deleted file mode 100644 index 3cbf8071..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-59760-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-59760-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-59760-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-59760-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-yield-token: new-fee-rate-yield-token - } - ) - (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-59760-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-59760 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-59760 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-79760-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-79760-wbtc.clar deleted file mode 100644 index fef14ef5..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-79760-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-79760-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-79760-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-79760-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-yield-token: new-fee-rate-yield-token - } - ) - (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-79760-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-79760 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-79760 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-80875-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc-80875-wbtc.clar deleted file mode 100644 index e6d96fdf..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-80875-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-80875-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-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc-80875-wbtc get-balance tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc-80875-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-yield-token: new-fee-rate-yield-token - } - ) - (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-80875-wbtc get-total-supply))) - (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-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token .yield-wbtc-80875 new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token .yield-wbtc-80875 new-fee-rate-yield-token)) - - (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 deleted file mode 100644 index c1189f10..00000000 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc-92160-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-yield-token: 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-yield-token: u0 ;; default yield-token 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-yield-token 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-yield-token: new-fee-rate-yield-token - } - ) - (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))) - (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-yield-token (get new-fee-rate-yield-token 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-yield-token .yield-wbtc-92160 new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/contracts/pool-token/fwp-usda-23040-usda-74880-50-50.clar b/clarity/contracts/pool-token/fwp-usda-23040-usda-74880-50-50.clar deleted file mode 100644 index f113e104..00000000 --- a/clarity/contracts/pool-token/fwp-usda-23040-usda-74880-50-50.clar +++ /dev/null @@ -1,97 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token fwp-usda-23040-usda-74880) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .fixed-weight-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 fwp-usda-23040-usda-74880))) -) - -(define-read-only (get-name) - (ok "fwp-usda-23040-usda-74880") -) - -(define-read-only (get-symbol) - (ok "fwp-usda-23040-usda-74880") -) - -(define-read-only (get-decimals) - (ok u8) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance fwp-usda-23040-usda-74880 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? fwp-usda-23040-usda-74880 (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? fwp-usda-23040-usda-74880 (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? fwp-usda-23040-usda-74880 (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-11520-usda.clar deleted file mode 100644 index 245f2084..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-11520-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-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-121195-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-121195-usda.clar deleted file mode 100644 index 4144b946..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-121195-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-121195-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-121195-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-121195-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-121195-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-121195-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-121195-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-121195-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-121195-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-132481-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-132481-usda.clar deleted file mode 100644 index 3e6631dc..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-132481-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-132481-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-132481-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-132481-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-132481-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-132481-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-132481-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-132481-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-132481-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-161515-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-161515-usda.clar deleted file mode 100644 index ba5f83a5..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-161515-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-161515-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-161515-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-161515-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-161515-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-161515-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-161515-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-161515-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-161515-usda (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-usda-200335-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-200335-usda.clar deleted file mode 100644 index 33bb18d3..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-200335-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-200335-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-200335-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-200335-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-200335-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-200335-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-200335-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-200335-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-200335-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 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-240655-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-240655-usda.clar deleted file mode 100644 index f2b3357f..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-240655-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-240655-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-240655-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-240655-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-240655-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-240655-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-240655-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-240655-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-240655-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 deleted file mode 100644 index d8d762f7..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-51840-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-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-80875-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda-80875-usda.clar deleted file mode 100644 index 66356b54..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-80875-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-80875-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-80875-usda))) -) - -(define-read-only (get-name) - (ok "ytp-yield-usda-80875-usda") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-usda-80875-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-80875-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-80875-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-80875-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-80875-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 deleted file mode 100644 index 88f2a015..00000000 --- a/clarity/contracts/pool-token/ytp-yield-usda-92160-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-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-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index 39235125..b2db9035 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -1,6 +1,10 @@ (impl-trait .trait-ownable.ownable-trait) (impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + (define-fungible-token ytp-yield-usda) (define-map token-balances {token-id: uint, owner: principal} uint) (define-map token-supplies uint uint) @@ -19,9 +23,7 @@ ) ) -(define-constant err-owner-only (err u100)) -(define-constant err-insufficient-balance (err u1)) -(define-constant err-invalid-sender (err u4)) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) @@ -68,9 +70,9 @@ ( (sender-balance (get-balance-or-default token-id sender)) ) - (asserts! (is-eq tx-sender sender) err-invalid-sender) - (asserts! (<= amount sender-balance) err-insufficient-balance) - (try! (ft-transfer? semi-fungible-token amount sender recipient)) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? ytp-yield-usda amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) @@ -104,7 +106,7 @@ (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) (try! (ft-mint? ytp-yield-usda amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +117,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) (try! (ft-burn? ytp-yield-usda amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-11520-wbtc.clar deleted file mode 100644 index a1ab5ea2..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-11520-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-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-121195-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-121195-wbtc.clar deleted file mode 100644 index 7c7aa88d..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-121195-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-121195-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-121195-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-121195-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-121195-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-121195-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-121195-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-121195-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-121195-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-132481-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-132481-wbtc.clar deleted file mode 100644 index c756650b..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-132481-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-132481-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-132481-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-132481-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-132481-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-132481-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-132481-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-132481-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-132481-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-161515-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-161515-wbtc.clar deleted file mode 100644 index 63200e6f..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-161515-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-161515-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-161515-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-161515-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-161515-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-161515-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-161515-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-161515-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-161515-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-200335-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-200335-wbtc.clar deleted file mode 100644 index d5751aca..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-200335-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-200335-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-200335-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-200335-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-200335-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-200335-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-200335-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-200335-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-200335-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 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-23670-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-23670-wbtc.clar deleted file mode 100644 index 3189e92c..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-23670-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-23670-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-23670-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-23670-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-23670-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-23670-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-23670-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-23670-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-23670-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-240655-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-240655-wbtc.clar deleted file mode 100644 index da6c6d27..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-240655-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-240655-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-240655-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-240655-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-240655-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-240655-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-240655-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-240655-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-240655-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 deleted file mode 100644 index 8a6e523e..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-51840-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-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-59760-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-59760-wbtc.clar deleted file mode 100644 index ecc1b43f..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-59760-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-59760-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-59760-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-59760-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-59760-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-59760-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-59760-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-59760-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-59760-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-79760-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-79760-wbtc.clar deleted file mode 100644 index 3855af07..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-79760-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-79760-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-79760-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-79760-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-79760-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-79760-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-79760-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-79760-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-79760-wbtc (fixed-to-decimals amount) sender) - ) -) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc-80875-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc-80875-wbtc.clar deleted file mode 100644 index ae7fd6b2..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-80875-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-80875-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-80875-wbtc))) -) - -(define-read-only (get-name) - (ok "ytp-yield-wbtc-80875-wbtc") -) - -(define-read-only (get-symbol) - (ok "ytp-yield-wbtc-80875-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-80875-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-80875-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-80875-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-80875-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 deleted file mode 100644 index 5df22005..00000000 --- a/clarity/contracts/pool-token/ytp-yield-wbtc-92160-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-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/pool/alex-reserve-pool.clar b/clarity/contracts/pool/alex-reserve-pool.clar index d785b9e6..8d739bb2 100644 --- a/clarity/contracts/pool/alex-reserve-pool.clar +++ b/clarity/contracts/pool/alex-reserve-pool.clar @@ -1,6 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) ;; alex-reserve-pool diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index e5cf288b..b56e014c 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -1,7 +1,7 @@ (impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait yield-token-trait .trait-yield-token.yield-token-trait) -(use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(use-trait multisig-trait .trait-multisig-vote.multisig-vote-sft-trait) ;; collateral-rebalancing-pool ;; @@ -283,11 +283,11 @@ ;; @param moving-average; weighting smoothing factor ;; @param dx; amount of collateral token being added ;; @returns (response bool uint) -(define-public (create-pool (token ) (collateral ) (the-yield-token ) (the-key-token ) (multisig-vote ) (ltv-0 uint) (conversion-ltv uint) (bs-vol uint) (moving-average uint) (dx uint)) +(define-public (create-pool (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (multisig-vote ) (ltv-0 uint) (conversion-ltv uint) (bs-vol uint) (moving-average uint) (dx uint)) (begin (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) (asserts! - (is-none (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR) })) + (is-none (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry })) ERR-POOL-ALREADY-EXISTS ) (let @@ -295,7 +295,6 @@ (pool-id (+ (var-get pool-count) u1)) (token-x (contract-of collateral)) (token-y (contract-of token)) - (expiry (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) (now (* block-height ONE_8)) ;; assume 10mins per block @@ -343,7 +342,7 @@ (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) (var-set pool-count pool-id) - (try! (add-to-position token collateral the-yield-token the-key-token dx)) + (try! (add-to-position token collateral expiry the-yield-token the-key-token dx)) (print { object: "pool", action: "created", data: pool-data }) (ok true) ) @@ -360,12 +359,12 @@ ;; @post yield-token; sender transfers > 0 to alex-vault ;; @post token; alex-vault transfers >0 to sender ;; @returns (response (tuple uint uint) uint) -(define-public (add-to-position-and-switch (token ) (collateral ) (the-yield-token ) (the-key-token ) (dx uint)) +(define-public (add-to-position-and-switch (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (dx uint)) (let ( - (minted-yield-token (get yield-token (try! (add-to-position token collateral the-yield-token the-key-token dx)))) + (minted-yield-token (get yield-token (try! (add-to-position token collateral expiry the-yield-token the-key-token dx)))) ) - (contract-call? .yield-token-pool swap-y-for-x the-yield-token token minted-yield-token none) + (contract-call? .yield-token-pool swap-y-for-x expiry the-yield-token token minted-yield-token none) ) ) @@ -377,13 +376,12 @@ ;; @param dx; amount of collateral added ;; @post collateral; sender transfer exactly dx to alex-vault ;; @returns (response (tuple uint uint) uint) -(define-public (add-to-position (token ) (collateral ) (the-yield-token ) (the-key-token ) (dx uint)) +(define-public (add-to-position (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (dx uint)) (let ( (token-x (contract-of collateral)) - (token-y (contract-of token)) - (expiry (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) - (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (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)) (conversion-ltv (get conversion-ltv pool)) (ltv (try! (get-ltv token collateral expiry))) ) @@ -425,8 +423,8 @@ (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) ;; mint pool token and send to tx-sender - (try! (contract-call? the-yield-token mint tx-sender yield-new-supply)) - (try! (contract-call? the-key-token mint tx-sender key-new-supply)) + (try! (contract-call? the-yield-token mint expiry yield-new-supply tx-sender)) + (try! (contract-call? the-key-token mint expiry key-new-supply tx-sender)) (print { object: "pool", action: "liquidity-added", data: pool-updated }) (ok {yield-token: yield-new-supply, key-token: key-new-supply}) ) @@ -440,22 +438,21 @@ ;; @param percent; % of yield-token held to be burnt ;; @post yield-token; alex-vault transfer exactly uints of token equal to (percent * yield-token held) to sender ;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position-yield (token ) (collateral ) (the-yield-token ) (percent uint)) +(define-public (reduce-position-yield (token ) (collateral ) (expiry uint) (the-yield-token ) (percent uint)) (begin (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) ;; burn supported only at maturity - (asserts! (> (* block-height ONE_8) (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) ERR-EXPIRY) + (asserts! (> (* block-height ONE_8) expiry) ERR-EXPIRY) (let ( (token-x (contract-of collateral)) (token-y (contract-of token)) - (expiry (unwrap! (contract-call? the-yield-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) (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)) (yield-supply (get yield-supply pool)) - (total-shares (unwrap! (contract-call? the-yield-token get-balance tx-sender) ERR-GET-BALANCE-FAIL)) + (total-shares (unwrap! (contract-call? the-yield-token get-balance expiry tx-sender) ERR-GET-BALANCE-FAIL)) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (shares-to-yield (div-down shares yield-supply)) @@ -499,7 +496,7 @@ (try! (contract-call? .alex-vault transfer-ft token shares tx-sender)) (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (try! (contract-call? the-yield-token burn tx-sender shares)) + (try! (contract-call? the-yield-token burn expiry shares tx-sender)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: u0, dy: shares}) @@ -515,21 +512,20 @@ ;; @post token; alex-vault transfers > 0 token to sender ;; @post collateral; alex-vault transfers > 0 collateral to sender ;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position-key (token ) (collateral ) (the-key-token ) (percent uint)) +(define-public (reduce-position-key (token ) (collateral ) (expiry uint) (the-key-token ) (percent uint)) (begin (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) ;; burn supported only at maturity - (asserts! (> (* block-height ONE_8) (unwrap! (contract-call? the-key-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) ERR-EXPIRY) + (asserts! (> (* block-height ONE_8) expiry) ERR-EXPIRY) (let ( (token-x (contract-of collateral)) (token-y (contract-of token)) - (expiry (unwrap! (contract-call? the-key-token get-expiry) ERR-GET-EXPIRY-FAIL-ERR)) (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)) (key-supply (get key-supply pool)) - (total-shares (unwrap! (contract-call? the-key-token get-balance tx-sender) ERR-GET-BALANCE-FAIL)) + (total-shares (unwrap! (contract-call? the-key-token get-balance expiry tx-sender) ERR-GET-BALANCE-FAIL)) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (reduce-data (try! (get-position-given-burn-key token collateral expiry shares))) (dx-weighted (get dx reduce-data)) @@ -549,7 +545,7 @@ (and (> dy-weighted u0) (try! (contract-call? .alex-vault transfer-ft token dy-weighted tx-sender))) (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (try! (contract-call? the-key-token burn tx-sender shares)) + (try! (contract-call? the-key-token burn expiry shares tx-sender)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: dx-weighted, dy: dy-weighted}) ) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index c3d86b55..8683233f 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -1,7 +1,7 @@ (impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) (use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(use-trait multisig-trait .trait-multisig-vote.multisig-vote-sft-trait) ;; yield-token-pool (define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places @@ -22,7 +22,7 @@ (define-constant fixed-point-err (err 5014)) (define-constant ERR-MATH-CALL (err u4003)) (define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) -(define-constant dy-bigger-than-available-err (err u2016)) +(define-constant ERR-DY-BIGGER-THAN-AVAILABLE (err u2016)) (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) (define-constant ERR-GET-SYMBOL-FAIL (err u6000)) @@ -56,7 +56,7 @@ (define-map pools-data-map { - yield-token: principal + yield-token: principal, expiry: uint } { @@ -141,7 +141,7 @@ ;; @desc get-pool-details ;; @param the-yield-token; yield-token ;; @returns (response (tuple) uint) -(define-read-only (get-pool-details (the-yield-token ) (expiry uint)) +(define-read-only (get-pool-details (expiry uint) (the-yield-token )) (ok (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) @@ -149,7 +149,7 @@ ;; @desc note yield is not annualised ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-yield (the-yield-token ) (expiry uint)) +(define-read-only (get-yield (expiry uint) (the-yield-token )) (let ( (yield-token (contract-of the-yield-token)) @@ -166,7 +166,7 @@ ;; @desc get-price ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-price (the-yield-token ) (expiry uint)) +(define-read-only (get-price (expiry uint) (the-yield-token )) (let ( (yield-token (contract-of the-yield-token)) @@ -183,7 +183,7 @@ ;; @desc get-oracle-enabled ;; @param the-yield-token; yield-token ;; @returns (response bool uint) -(define-read-only (get-oracle-enabled (the-yield-token ) (expiry uint)) +(define-read-only (get-oracle-enabled (expiry uint) (the-yield-token )) (ok (get oracle-enabled (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) @@ -192,7 +192,7 @@ ;; @restricted CONTRACT-OWNER ;; @param the-yield-token; yield-token ;; @returns (response bool uint) -(define-public (set-oracle-enabled (the-yield-token ) (expiry uint)) +(define-public (set-oracle-enabled (expiry uint) (the-yield-token )) (let ( (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) @@ -209,7 +209,7 @@ ;; @desc returns the moving average used to determine oracle price ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-oracle-average (the-yield-token ) (expiry uint)) +(define-read-only (get-oracle-average (expiry uint) (the-yield-token )) (ok (get oracle-average (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) @@ -217,13 +217,13 @@ ;; @restricted CONTRACT-OWNER ;; @param the-yield-token; yield-token ;; @returns (response bool uint) -(define-public (set-oracle-average (the-yield-token ) (expiry uint) (new-oracle-average uint)) +(define-public (set-oracle-average (expiry uint) (the-yield-token ) (new-oracle-average uint)) (let ( (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) (pool-updated (merge pool { oracle-average: new-oracle-average, - oracle-resilient: (try! (get-oracle-instant the-yield-token)) + oracle-resilient: (try! (get-oracle-instant expiry the-yield-token)) })) ) (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) @@ -238,13 +238,13 @@ ;; @desc price-oracle that is less up to date but more resilient to manipulation ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-oracle-resilient (the-yield-token ) (expiry uint)) +(define-read-only (get-oracle-resilient (expiry uint) (the-yield-token )) (let ( (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) - (ok (+ (mul-down (- ONE_8 (get oracle-average pool)) (try! (get-oracle-instant the-yield-token))) + (ok (+ (mul-down (- ONE_8 (get oracle-average pool)) (try! (get-oracle-instant expiry the-yield-token))) (mul-down (get oracle-average pool) (get oracle-resilient pool)))) ) ) @@ -253,8 +253,8 @@ ;; @desc price-oracle that is more up to date but less resilient to manipulation ;; @param the-yield-token; yield-token ;; @returns (response uint uint) -(define-read-only (get-oracle-instant (the-yield-token ) (expiry uint)) - (ok (div-down ONE_8 (try! (get-price the-yield-token expiry)))) +(define-read-only (get-oracle-instant (expiry uint) (the-yield-token )) + (ok (div-down ONE_8 (try! (get-price expiry the-yield-token)))) ) ;; @desc create-pool @@ -266,7 +266,7 @@ ;; @param dx; amount of token added ;; @param dy; amount of yield-token added ;; @returns (response bool uint) -(define-public (create-pool (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) +(define-public (create-pool (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) (begin (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) ;; ;; create pool only if the correct pair @@ -301,7 +301,7 @@ ;; ;; if yield-token added has a longer expiry than current max-expiry, update max-expiry (to expiry + one block). ;; (var-set max-expiry (if (< (var-get max-expiry) expiry) (+ expiry ONE_8) (var-get max-expiry))) - (try! (add-to-position the-yield-token expiry the-token the-pool-token dx)) + (try! (add-to-position expiry the-yield-token the-token the-pool-token dx)) (print { object: "pool", action: "created", data: pool-data }) (ok true) @@ -317,15 +317,15 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param dx; amount of token added (part of which will be used to buy yield-token) ;; @returns (response (tuple uint uint uint uint) uint) -(define-public (buy-and-add-to-position (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (dx uint)) +(define-public (buy-and-add-to-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) (let ( - (dy-act (get dy-act (try! (get-token-given-position the-yield-token expiry dx)))) - (dx-adjusted (- dx (div-down dx (+ dx (try! (get-x-given-y the-yield-token expiry dy-act)))))) + (dy-act (get dy-act (try! (get-token-given-position expiry the-yield-token dx)))) + (dx-adjusted (- dx (div-down dx (+ dx (try! (get-x-given-y expiry the-yield-token dy-act)))))) (dx-to-buy-dy-adjusted (- dx dx-adjusted)) ) - (and (> dy-act u0) (is-ok (swap-x-for-y the-yield-token expiry the-token dx-to-buy-dy-adjusted none))) - (add-to-position the-yield-token expiry the-token the-pool-token dx-adjusted) + (and (> dy-act u0) (is-ok (swap-x-for-y expiry the-yield-token the-token dx-to-buy-dy-adjusted none))) + (add-to-position expiry the-yield-token the-token the-pool-token dx-adjusted) ) ) @@ -336,7 +336,7 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param dx; amount of token added ;; @returns (response (tuple uint uint uint uint) uint) -(define-public (add-to-position (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (dx uint)) +(define-public (add-to-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) (begin ;; dx must be greater than zero (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) @@ -348,7 +348,7 @@ (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) (total-supply (get total-supply pool)) - (add-data (try! (get-token-given-position the-yield-token dx))) + (add-data (try! (get-token-given-position expiry the-yield-token dx))) (new-supply (get token add-data)) (new-dy-act (get dy-act add-data)) (new-dy-vir (get dy-vir add-data)) @@ -371,7 +371,7 @@ ;; mint pool token and send to tx-sender (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token mint tx-sender new-supply)) + (try! (contract-call? the-pool-token mint expiry new-supply tx-sender)) (print { object: "pool", action: "liquidity-added", data: pool-updated }) (ok {supply: new-supply, balance-token: dx, balance-yield-token: new-dy-act, balance-virtual: new-dy-vir}) ) @@ -385,7 +385,7 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param percent; percentage of pool token held to reduce ;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (percent uint)) +(define-public (reduce-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (percent uint)) (begin (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) (let @@ -396,9 +396,9 @@ (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) (total-supply (get total-supply pool)) - (total-shares (unwrap-panic (contract-call? the-pool-token get-balance tx-sender))) + (total-shares (unwrap-panic (contract-call? the-pool-token get-balance expiry tx-sender))) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) - (reduce-data (try! (get-position-given-burn the-yield-token shares))) + (reduce-data (try! (get-position-given-burn expiry the-yield-token shares))) (dx (get dx reduce-data)) (dy-act (get dy-act reduce-data)) (dy-vir (get dy-vir reduce-data)) @@ -414,10 +414,10 @@ (asserts! (is-eq (get pool-token pool) (contract-of the-pool-token)) ERR-INVALID-POOL-TOKEN) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-yield the-yield-token expiry dy-act tx-sender))) + (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy-act tx-sender))) (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token burn tx-sender shares)) + (try! (contract-call? the-pool-token burn expiry shares tx-sender)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: dx, dy: dy-act}) ) @@ -434,14 +434,14 @@ ;; @param the-pool-token-to-roll; pool token representing ownership of the pool to roll to ;; @returns (response (tuple uint uint) uint) (define-public (roll-position - (the-yield-token ) (expiry uint) (the-token ) (the-pool-token ) (percent uint) - (expiry-to-roll uint) (the-pool-token-to-roll )) + (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (percent uint) + (expiry-to-roll uint)) (let ( - (reduce-data (unwrap! (reduce-position the-yield-token expiry the-token the-pool-token percent) (err u11111))) - (dy-to-dx (get dx (unwrap! (swap-y-for-x the-yield-token expiry the-token (get dy reduce-data) none) (err u22222)))) + (reduce-data (unwrap! (reduce-position expiry the-yield-token the-token the-pool-token percent) (err u11111))) + (dy-to-dx (get dx (unwrap! (swap-y-for-x expiry the-yield-token the-token (get dy reduce-data) none) (err u22222)))) ) - (buy-and-add-to-position the-yield-token expiry-to-roll the-token the-pool-token-to-roll (+ (get dx reduce-data) dy-to-dx)) + (buy-and-add-to-position expiry-to-roll the-yield-token the-token the-pool-token (+ (get dx reduce-data) dy-to-dx)) ) ) @@ -451,7 +451,7 @@ ;; @param dx; amount of token to swap ;; @param min-dy; optional, min amount of yield-token to receive ;; @returns (response (tuple uint uint) uint) -(define-public (swap-x-for-y (the-yield-token ) (the-token ) (dx uint) (min-dy (optional uint))) +(define-public (swap-x-for-y (expiry uint) (the-yield-token ) (the-token ) (dx uint) (min-dy (optional uint))) (begin (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (let @@ -462,21 +462,21 @@ (balance-yield-token (get balance-yield-token pool)) ;; lambda ~= 1 - fee-rate-yield-token * yield - (yield (try! (get-yield the-yield-token))) + (yield (try! (get-yield expiry the-yield-token))) (fee-yield (mul-down yield (get fee-rate-yield-token pool))) (lambda (if (<= ONE_8 fee-yield) u0 (- ONE_8 fee-yield))) (dx-net-fees (mul-down dx lambda)) (fee (if (<= dx dx-net-fees) u0 (- dx dx-net-fees))) (fee-rebate (mul-down fee (get fee-rebate pool))) - (dy (try! (get-y-given-x the-yield-token dx-net-fees))) + (dy (try! (get-y-given-x expiry the-yield-token dx-net-fees))) (pool-updated (merge pool { balance-token: (+ balance-token dx-net-fees fee-rebate), balance-yield-token: (if (<= balance-yield-token dy) u0 (- balance-yield-token dy)), - oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient the-yield-token)) u0) + oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient expiry the-yield-token)) u0) } ) ) @@ -485,7 +485,7 @@ (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-yield-token dy tx-sender))) + (and (> dy u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy tx-sender))) (try! (contract-call? .alex-reserve-pool add-to-balance (contract-of the-token) (- fee fee-rebate))) ;; post setting @@ -502,7 +502,7 @@ ;; @param dy; amount of yield token to swap ;; @param min-dx; optional, min amount of token to receive ;; @returns (response (tuple uint uint) uint) -(define-public (swap-y-for-x (the-yield-token ) (the-token ) (dy uint) (min-dx (optional uint))) +(define-public (swap-y-for-x (expiry uint) (the-yield-token ) (the-token ) (dy uint) (min-dx (optional uint))) (begin (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) (let @@ -513,21 +513,21 @@ (balance-yield-token (get balance-yield-token pool)) ;; lambda ~= 1 - fee-rate-token * yield - (yield (try! (get-yield the-yield-token))) + (yield (try! (get-yield expiry the-yield-token))) (fee-yield (mul-down yield (get fee-rate-token pool))) (lambda (if (<= ONE_8 fee-yield) u0 (- ONE_8 fee-yield))) (dy-net-fees (mul-down dy lambda)) (fee (if (<= dy dy-net-fees) u0 (- dy dy-net-fees))) (fee-rebate (mul-down fee (get fee-rebate pool))) - (dx (try! (get-x-given-y the-yield-token dy-net-fees))) + (dx (try! (get-x-given-y expiry the-yield-token dy-net-fees))) (pool-updated (merge pool { balance-token: (if (<= balance-token dx) u0 (- balance-token dx)), balance-yield-token: (+ balance-yield-token dy-net-fees fee-rebate), - oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient the-yield-token)) u0) + oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient expiry the-yield-token)) u0) } ) ) @@ -535,7 +535,7 @@ (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) ;; post setting @@ -549,7 +549,7 @@ ;; @desc get-fee-rebate ;; @param the-yield-token; yield token ;; @returns (response uint uint) -(define-read-only (get-fee-rebate (the-yield-token ) (expiry uint)) +(define-read-only (get-fee-rebate (expiry uint) (the-yield-token )) (ok (get fee-rebate (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) @@ -558,7 +558,7 @@ ;; @param the-yield-token; yield token ;; @param fee-rebate; new fee-rebate ;; @returns (response bool uint) -(define-public (set-fee-rebate (the-yield-token ) (fee-rebate uint)) +(define-public (set-fee-rebate (expiry uint) (the-yield-token ) (fee-rebate uint)) (let ( (yield-token (contract-of the-yield-token)) @@ -574,7 +574,7 @@ ;; @desc get-fee-rate-yield-token ;; @param the-yield-token; yield token ;; @returns (response uint uint) -(define-read-only (get-fee-rate-yield-token (the-yield-token ) (expiry uint)) +(define-read-only (get-fee-rate-yield-token (expiry uint) (the-yield-token )) (let ( (yield-token (contract-of the-yield-token)) @@ -587,7 +587,7 @@ ;; @desc get-fee-rate-token ;; @param the-yield-token; yield token ;; @returns (response uint uint) -(define-read-only (get-fee-rate-token (the-yield-token ) (expiry uint)) +(define-read-only (get-fee-rate-token (expiry uint) (the-yield-token )) (let ( (yield-token (contract-of the-yield-token)) @@ -602,7 +602,7 @@ ;; @param the-yield-token; yield token ;; @param fee-rate-yield-token; new fee-rate-yield-token ;; @returns (response bool uint) -(define-public (set-fee-rate-yield-token (the-yield-token ) (fee-rate-yield-token uint)) +(define-public (set-fee-rate-yield-token (expiry uint) (the-yield-token ) (fee-rate-yield-token uint)) (let ( (yield-token (contract-of the-yield-token)) @@ -621,7 +621,7 @@ ;; @param the-yield-token; yield token ;; @param fee-rate-token; new fee-rate-token ;; @returns (response bool uint) -(define-public (set-fee-rate-token (the-yield-token ) (fee-rate-token uint)) +(define-public (set-fee-rate-token (expiry uint) (the-yield-token ) (fee-rate-token uint)) (let ( (yield-token (contract-of the-yield-token)) @@ -637,7 +637,7 @@ ;; @desc get-fee-to-address ;; @param the-yield-token; yield token ;; @returns (response principal uint) -(define-read-only (get-fee-to-address (the-yield-token ) (expiry uint)) +(define-read-only (get-fee-to-address (expiry uint) (the-yield-token )) (let ( (yield-token (contract-of the-yield-token)) @@ -651,14 +651,14 @@ ;; @param the-yield-token; yield token ;; @param dx; amount of token being added ;; @returns (response uint uint) -(define-read-only (get-y-given-x (the-yield-token ) (dx uint)) +(define-read-only (get-y-given-x (expiry uint) (the-yield-token ) (dx uint)) (let ( (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (normalized-expiry (try! (get-t (get expiry pool) (get listed pool)))) + (normalized-expiry (try! (get-t expiry (get listed pool)))) (dy (try! (contract-call? .yield-token-equation get-y-given-x (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dx))) ) - (asserts! (> (get balance-yield-token pool) dy) dy-bigger-than-available-err) + (asserts! (> (get balance-yield-token pool) dy) ERR-DY-BIGGER-THAN-AVAILABLE) (ok dy) ) ) @@ -667,12 +667,12 @@ ;; @param the-yield-token; yield token ;; @param dy; amount of yield token being added ;; @returns (response uint uint) -(define-read-only (get-x-given-y (the-yield-token ) (dy uint)) +(define-read-only (get-x-given-y (expiry uint) (the-yield-token ) (dy uint)) (let ( (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (normalized-expiry (try! (get-t (get expiry pool) (get listed pool)))) + (normalized-expiry (try! (get-t expiry (get listed pool)))) ) (contract-call? .yield-token-equation get-x-given-y (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dy) ) @@ -682,13 +682,12 @@ ;; @param the-yield-token; yield token ;; @param price; target price ;; @returns (response uint uint) -(define-read-only (get-x-given-price (the-yield-token ) (price uint)) +(define-read-only (get-x-given-price (expiry uint) (the-yield-token ) (price uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) @@ -702,13 +701,12 @@ ;; @param the-yield-token; yield token ;; @param price; target price ;; @returns (response uint uint) -(define-read-only (get-y-given-price (the-yield-token ) (price uint)) +(define-read-only (get-y-given-price (expiry uint) (the-yield-token ) (price uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) @@ -722,13 +720,12 @@ ;; @param the-yield-token; yield token ;; @param yield; target yield ;; @returns (response uint uint) -(define-read-only (get-x-given-yield (the-yield-token ) (yield uint)) +(define-read-only (get-x-given-yield (expiry uint) (the-yield-token ) (yield uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) @@ -742,13 +739,12 @@ ;; @param the-yield-token; yield token ;; @param yield; target yield ;; @returns (response uint uint) -(define-read-only (get-y-given-yield (the-yield-token ) (yield uint)) +(define-read-only (get-y-given-yield (expiry uint) (the-yield-token ) (yield uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) @@ -762,13 +758,12 @@ ;; @param the-yield-token; yield token ;; @param dx; amount of token added ;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-token-given-position (the-yield-token ) (dx uint)) +(define-read-only (get-token-given-position (expiry uint) (the-yield-token ) (dx uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-actual (get balance-yield-token pool)) @@ -792,13 +787,12 @@ ;; @param the-yield-token; yield token ;; @param token; units of pool token to be minted ;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-mint (the-yield-token ) (token uint)) +(define-read-only (get-position-given-mint (expiry uint) (the-yield-token ) (token uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-actual (get balance-yield-token pool)) @@ -821,13 +815,12 @@ ;; @param the-yield-token; yield token ;; @param token; units of pool token to be burnt ;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-burn (the-yield-token ) (token uint)) +(define-read-only (get-position-given-burn (expiry uint) (the-yield-token ) (token uint)) (let ( (yield-token (contract-of the-yield-token)) (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (expiry (get expiry pool)) (listed (get listed pool)) (normalized-expiry (try! (get-t expiry listed))) (balance-actual (get balance-yield-token pool)) diff --git a/clarity/contracts/traits/trait-flash-loan-user.clar b/clarity/contracts/traits/trait-flash-loan-user.clar index bef19611..b2de11ff 100644 --- a/clarity/contracts/traits/trait-flash-loan-user.clar +++ b/clarity/contracts/traits/trait-flash-loan-user.clar @@ -5,6 +5,6 @@ ;; no need for params, as whoever implements this trait should know what he/she is doing ;; see test-flash-loan-user ;; (execute ( uint) (response bool uint) (optional (string-utf8 256))) - (execute ( uint) (response bool uint)) + (execute ( uint (optional uint)) (response bool uint)) ) ) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-multisig-vote.clar b/clarity/contracts/traits/trait-multisig-vote.clar index c11249f1..397f86c5 100644 --- a/clarity/contracts/traits/trait-multisig-vote.clar +++ b/clarity/contracts/traits/trait-multisig-vote.clar @@ -1,15 +1,22 @@ (use-trait ft-trait .trait-sip-010.sip-010-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) (define-trait multisig-vote-trait ( (propose (uint (string-utf8 256) (string-utf8 256) uint uint) (response uint uint)) - (vote-for ( uint uint) (response uint uint)) - (vote-against ( uint uint) (response uint uint)) - (end-proposal (uint) (response bool uint)) - (return-votes-to-member ( uint principal) (response bool uint)) + ) +) -)) \ No newline at end of file +(define-trait multisig-vote-sft-trait + ( + (propose (uint uint (string-utf8 256) (string-utf8 256) uint uint) (response uint uint)) + (vote-for ( uint uint) (response uint uint)) + (vote-against ( uint uint) (response uint uint)) + (end-proposal (uint) (response bool uint)) + (return-votes-to-member ( uint principal) (response bool uint)) + ) +) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar index 468c0db7..ca88a95a 100644 --- a/clarity/contracts/traits/trait-semi-fungible-token.clar +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -29,5 +29,9 @@ ;; Transfer many tokens at once with memos. (transfer-many-memo ((list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)})) (response bool uint)) + + (mint (uint uint principal) (response bool uint)) + + (burn (uint uint principal) (response bool uint)) ) ) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-vault.clar b/clarity/contracts/traits/trait-vault.clar index abaa2736..d67e6658 100644 --- a/clarity/contracts/traits/trait-vault.clar +++ b/clarity/contracts/traits/trait-vault.clar @@ -8,7 +8,7 @@ (get-balance () (response uint uint)) ;; flash loan currently supports single token loan - (flash-loan ( uint) (response uint uint)) + (flash-loan ( uint (optional uint)) (response uint uint)) ) ) diff --git a/clarity/contracts/yield-token/yield-usda-11520.clar b/clarity/contracts/yield-token/yield-usda-11520.clar deleted file mode 100644 index 359e3b45..00000000 --- a/clarity/contracts/yield-token/yield-usda-11520.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-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-121195.clar b/clarity/contracts/yield-token/yield-usda-121195.clar deleted file mode 100644 index 205a6b85..00000000 --- a/clarity/contracts/yield-token/yield-usda-121195.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-121195) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u12119500000000) -(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-121195))) -) - -(define-read-only (get-name) - (ok "yield-usda-121195") -) - -(define-read-only (get-symbol) - (ok "yield-usda-121195") -) - -(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-121195 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-121195 (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-121195 (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-121195 (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-121195 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-132481.clar b/clarity/contracts/yield-token/yield-usda-132481.clar deleted file mode 100644 index ecf1d92b..00000000 --- a/clarity/contracts/yield-token/yield-usda-132481.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-132481) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u13248100000000) -(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-132481))) -) - -(define-read-only (get-name) - (ok "yield-usda-132481") -) - -(define-read-only (get-symbol) - (ok "yield-usda-132481") -) - -(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-132481 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-132481 (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-132481 (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-132481 (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-132481 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-161515.clar b/clarity/contracts/yield-token/yield-usda-161515.clar deleted file mode 100644 index 193bd68e..00000000 --- a/clarity/contracts/yield-token/yield-usda-161515.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-161515) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u16151500000000) -(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-161515))) -) - -(define-read-only (get-name) - (ok "yield-usda-161515") -) - -(define-read-only (get-symbol) - (ok "yield-usda-161515") -) - -(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-161515 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-161515 (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-161515 (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-161515 (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-161515 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-200335.clar b/clarity/contracts/yield-token/yield-usda-200335.clar deleted file mode 100644 index abfddc1c..00000000 --- a/clarity/contracts/yield-token/yield-usda-200335.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-200335) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u20033500000000) -(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-200335))) -) - -(define-read-only (get-name) - (ok "yield-usda-200335") -) - -(define-read-only (get-symbol) - (ok "yield-usda-200335") -) - -(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-200335 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-200335 (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-200335 (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-200335 (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-200335 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 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-240655.clar b/clarity/contracts/yield-token/yield-usda-240655.clar deleted file mode 100644 index fe429da0..00000000 --- a/clarity/contracts/yield-token/yield-usda-240655.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-240655) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u24065500000000) -(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-240655))) -) - -(define-read-only (get-name) - (ok "yield-usda-240655") -) - -(define-read-only (get-symbol) - (ok "yield-usda-240655") -) - -(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-240655 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-240655 (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-240655 (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-240655 (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-240655 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 deleted file mode 100644 index c6089134..00000000 --- a/clarity/contracts/yield-token/yield-usda-51840.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-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-59760.clar b/clarity/contracts/yield-token/yield-usda-59760.clar deleted file mode 100644 index 76b5f7de..00000000 --- a/clarity/contracts/yield-token/yield-usda-59760.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-59760) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u5976000000000) -(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-59760))) -) - -(define-read-only (get-name) - (ok "yield-usda-59760") -) - -(define-read-only (get-symbol) - (ok "yield-usda-59760") -) - -(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-59760 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-59760 (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-59760 (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-59760 (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-59760 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda-80875.clar b/clarity/contracts/yield-token/yield-usda-80875.clar deleted file mode 100644 index 562cf448..00000000 --- a/clarity/contracts/yield-token/yield-usda-80875.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-80875) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u8087500000000) -(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-80875))) -) - -(define-read-only (get-name) - (ok "yield-usda-80875") -) - -(define-read-only (get-symbol) - (ok "yield-usda-80875") -) - -(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-80875 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-80875 (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-80875 (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-80875 (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-80875 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 deleted file mode 100644 index f59d1c54..00000000 --- a/clarity/contracts/yield-token/yield-usda-92160.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-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-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index 9fbeaf2a..899efa97 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -1,6 +1,10 @@ (impl-trait .trait-ownable.ownable-trait) (impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + (define-fungible-token yield-usda) (define-map token-balances {token-id: uint, owner: principal} uint) (define-map token-supplies uint uint) @@ -19,9 +23,7 @@ ) ) -(define-constant err-owner-only (err u100)) -(define-constant err-insufficient-balance (err u1)) -(define-constant err-invalid-sender (err u4)) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) @@ -68,9 +70,9 @@ ( (sender-balance (get-balance-or-default token-id sender)) ) - (asserts! (is-eq tx-sender sender) err-invalid-sender) - (asserts! (<= amount sender-balance) err-insufficient-balance) - (try! (ft-transfer? semi-fungible-token amount sender recipient)) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? yield-usda amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) @@ -104,7 +106,7 @@ (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) (try! (ft-mint? yield-usda amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +117,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller contract-owner) err-owner-only) + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) (try! (ft-burn? yield-usda amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) diff --git a/clarity/contracts/yield-token/yield-wbtc-11520.clar b/clarity/contracts/yield-token/yield-wbtc-11520.clar deleted file mode 100644 index 66bed7a0..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-11520.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-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-121195.clar b/clarity/contracts/yield-token/yield-wbtc-121195.clar deleted file mode 100644 index 822f4b7e..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-121195.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-121195) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u12119500000000) -(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-121195))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-121195") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-121195") -) - -(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-121195 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-121195 (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-121195 (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-121195 (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-121195 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-132481.clar b/clarity/contracts/yield-token/yield-wbtc-132481.clar deleted file mode 100644 index baed1517..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-132481.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-132481) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u13248100000000) -(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-132481))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-132481") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-132481") -) - -(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-132481 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-132481 (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-132481 (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-132481 (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-132481 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-161515.clar b/clarity/contracts/yield-token/yield-wbtc-161515.clar deleted file mode 100644 index d45f5b05..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-161515.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-161515) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u16151500000000) -(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-161515))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-161515") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-161515") -) - -(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-161515 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-161515 (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-161515 (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-161515 (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-161515 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-200335.clar b/clarity/contracts/yield-token/yield-wbtc-200335.clar deleted file mode 100644 index 7d39b858..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-200335.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-200335) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u20033500000000) -(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-200335))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-200335") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-200335") -) - -(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-200335 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-200335 (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-200335 (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-200335 (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-200335 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-23670.clar b/clarity/contracts/yield-token/yield-wbtc-23670.clar deleted file mode 100644 index aea75e62..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-23670.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-23670) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u2367000000000) -(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-23670))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-23670") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-23670") -) - -(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-23670 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-23670 (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-23670 (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-23670 (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-23670 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-240655.clar b/clarity/contracts/yield-token/yield-wbtc-240655.clar deleted file mode 100644 index 423ecb92..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-240655.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-240655) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u24065500000000) -(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-240655))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-240655") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-240655") -) - -(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-240655 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-240655 (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-240655 (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-240655 (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-240655 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 deleted file mode 100644 index 9e4a2336..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-51840.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-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-59760.clar b/clarity/contracts/yield-token/yield-wbtc-59760.clar deleted file mode 100644 index 25d04d5e..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-59760.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-59760) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u5976000000000) -(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-59760))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-59760") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-59760") -) - -(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-59760 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-59760 (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-59760 (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-59760 (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-59760 u2000000000000 tx-sender)) -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-79760.clar b/clarity/contracts/yield-token/yield-wbtc-79760.clar deleted file mode 100644 index 503d2c91..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-79760.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-79760) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u7976000000000) -(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-79760))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-79760") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-79760") -) - -(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-79760 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-79760 (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-79760 (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-79760 (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-79760 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc-80875.clar b/clarity/contracts/yield-token/yield-wbtc-80875.clar deleted file mode 100644 index 96e2ccee..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-80875.clar +++ /dev/null @@ -1,113 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-yield-token.yield-token-trait) - -(define-fungible-token yield-wbtc-80875) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .collateral-rebalancing-pool) -(define-data-var token-expiry uint u8087500000000) -(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-80875))) -) - -(define-read-only (get-name) - (ok "yield-wbtc-80875") -) - -(define-read-only (get-symbol) - (ok "yield-wbtc-80875") -) - -(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-80875 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-80875 (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-80875 (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-80875 (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-80875 u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 - (try! (ft-mint? yield-wbtc-80875 u2000000000000 tx-sender)) -) \ 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 deleted file mode 100644 index eada6850..00000000 --- a/clarity/contracts/yield-token/yield-wbtc-92160.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-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/index.js b/init-js-tool/index.js index 40748868..c3a4b448 100644 --- a/init-js-tool/index.js +++ b/init-js-tool/index.js @@ -752,7 +752,7 @@ async function run() { // await mint_some_tokens(process.env.DEPLOYER_ACCOUNT_ADDRESS); // await mint_some_usda(process.env.DEPLOYER_ACCOUNT_ADDRESS + '.alex-reserve-pool'); // await mint_some_tokens(process.env.USER_ACCOUNT_ADDRESS); - await get_some_token(process.env.USER_ACCOUNT_ADDRESS); + // await get_some_token(process.env.USER_ACCOUNT_ADDRESS); // const _pools = { 0:_deploy[2], // 1:_deploy[3], @@ -827,11 +827,11 @@ async function run() { // console.log(result); // await transfer('key-usda-11520-wbtc', 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', 10668690600000); - // _list = ['fwp-wbtc-usda-50-50', 'ytp-yield-wbtc-92160-wbtc', 'ytp-yield-usda-92160-usda'] - // for (let i = 0; i < _list.length; i++){ - // // result = await balance(_list[i], process.env.DEPLOYER_ACCOUNT_ADDRESS); - // // console.log(result); - // await transfer(_list[i], 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', ONE_8, deployer=true); - // } + _list = ['fwp-wbtc-usda-50-50', 'ytp-yield-wbtc-92160-wbtc', 'ytp-yield-usda-92160-usda'] + for (let i = 0; i < _list.length; i++){ + // result = await balance(_list[i], process.env.DEPLOYER_ACCOUNT_ADDRESS); + // console.log(result); + await transfer(_list[i], 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', ONE_8, deployer=true); + } } run(); From de6418ab211dfc65a0affd3ed7ce6edba7b48fc0 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sat, 13 Nov 2021 23:39:37 +0800 Subject: [PATCH 06/25] working --- clarity/Clarinet.toml | 44 ++-- clarity/contracts/alex-vault.clar | 8 +- clarity/contracts/faucet.clar | 10 +- .../contracts/key-token/key-usda-wbtc.clar | 44 +++- .../multisig/multisig-ytp-yield-usda.clar | 2 +- .../pool-token/fwp-wbtc-usda-50-50.clar | 2 +- .../pool-token/lbp-alex-usda-90-10.clar | 2 +- .../contracts/pool-token/ytp-yield-usda.clar | 44 +++- clarity/contracts/pool/alex-reserve-pool.clar | 4 +- .../pool/collateral-rebalancing-pool.clar | 6 +- clarity/contracts/pool/fixed-weight-pool.clar | 17 +- .../pool/liquidity-bootstrapping-pool.clar | 17 +- clarity/contracts/pool/yield-token-pool.clar | 6 +- clarity/contracts/token/token-alex.clar | 64 +++-- clarity/contracts/token/token-t-alex-v2.clar | 114 --------- clarity/contracts/token/token-t-alex.clar | 64 +++-- clarity/contracts/token/token-usda.clar | 72 ++++-- clarity/contracts/token/token-wbtc.clar | 70 ++++-- clarity/contracts/token/token-wstx.clar | 66 +++-- .../contracts/traits/trait-pool-token.clar | 34 --- .../traits/trait-semi-fungible-token.clar | 15 +- clarity/contracts/traits/trait-sip-010.clar | 12 + .../contracts/traits/trait-yield-token.clar | 38 --- clarity/contracts/yield-token/yield-usda.clar | 44 +++- clarity/tests/models/alex-tests-tokens.ts | 22 ++ .../models/alex-tests-yield-token-pool.ts | 87 ++++--- clarity/tests/yield-token-pool_test.ts | 232 +++++++++--------- init-js-tool/index.js | 30 +-- init-js-tool/pools-fwp.js | 27 ++ 29 files changed, 655 insertions(+), 542 deletions(-) delete mode 100644 clarity/contracts/token/token-t-alex-v2.clar delete mode 100644 clarity/contracts/traits/trait-pool-token.clar delete mode 100644 clarity/contracts/traits/trait-yield-token.clar diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index bfdc770f..f2db4adf 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -8,7 +8,7 @@ depends_on = [] [contracts.margin-helper] path = "contracts/margin-helper.clar" -depends_on = ["trait-sip-010", "trait-yield-token", "trait-flash-loan-user"] +depends_on = ["trait-sip-010", "trait-flash-loan-user"] [contracts.alex-reserve-pool] path = "contracts/pool/alex-reserve-pool.clar" @@ -20,7 +20,7 @@ depends_on = ["trait-vault", "trait-sip-010", "trait-flash-loan-user", "math-fix [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", "fixed-weight-pool", "token-usda", "token-alex", "alex-reserve-pool", "yield-token-pool"] +depends_on = ["trait-sip-010", "trait-vault", "math-fixed-point", "weighted-equation", "fixed-weight-pool", "token-usda", "token-alex", "alex-reserve-pool", "yield-token-pool"] [contracts.faucet] path = "contracts/faucet.clar" @@ -28,11 +28,11 @@ depends_on = ["token-wbtc", "token-usda", "token-t-alex", "token-wstx"] [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", "alex-reserve-pool", "token-usda", "trait-multisig-vote"] +depends_on = ["trait-sip-010", "trait-vault", "math-fixed-point", "weighted-equation", "token-alex", "alex-reserve-pool", "token-usda", "trait-multisig-vote"] [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", "alex-reserve-pool", "token-usda", "fixed-weight-pool", "trait-multisig-vote"] +depends_on = ["trait-sip-010", "trait-vault", "math-fixed-point", "weighted-equation", "token-alex", "alex-reserve-pool", "token-usda", "fixed-weight-pool", "trait-multisig-vote"] [contracts.math-fixed-point] path = "contracts/lib/math-fixed-point.clar" @@ -52,23 +52,23 @@ depends_on = [] [contracts.token-alex] path = "contracts/token/token-alex.clar" -depends_on = ["trait-pool-token", "trait-ownable"] +depends_on = ["trait-ownable", "trait-sip-010"] [contracts.token-t-alex] path = "contracts/token/token-t-alex.clar" -depends_on = ["trait-pool-token", "trait-ownable"] +depends_on = ["trait-ownable", "trait-sip-010"] [contracts.token-usda] path = "contracts/token/token-usda.clar" -depends_on = ["trait-pool-token", "trait-ownable"] +depends_on = ["trait-ownable", "trait-sip-010"] [contracts.token-wbtc] path = "contracts/token/token-wbtc.clar" -depends_on = ["trait-pool-token", "trait-ownable"] +depends_on = ["trait-ownable", "trait-sip-010"] [contracts.token-wstx] path = "contracts/token/token-wstx.clar" -depends_on = ["trait-pool-token", "trait-ownable", "alex-vault"] +depends_on = ["trait-ownable", "alex-vault", "trait-sip-010"] [contracts.trait-flash-loan-user] path = "contracts/traits/trait-flash-loan-user.clar" @@ -86,10 +86,6 @@ depends_on = [] 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 = [] @@ -98,10 +94,6 @@ depends_on = [] 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"] @@ -116,11 +108,11 @@ depends_on = ["trait-sip-010", "trait-vault", "trait-flash-loan-user", "math-fix [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"] +depends_on = ["trait-sip-010", "trait-ownable"] [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"] +depends_on = ["trait-sip-010", "trait-ownable"] [contracts.ytp-yield-usda] path = "contracts/pool-token/ytp-yield-usda.clar" @@ -140,7 +132,7 @@ depends_on = ["yield-usda", "key-usda-wbtc"] [contracts.multisig-ytp-yield-usda] path = "contracts/multisig/multisig-ytp-yield-usda.clar" -depends_on = ["ytp-yield-usda"] +depends_on = ["ytp-yield-usda", "yield-usda"] [contracts.key-usda-wbtc] path = "contracts/key-token/key-usda-wbtc.clar" @@ -148,4 +140,16 @@ depends_on = ["trait-ownable", "trait-semi-fungible-token"] [contracts.yield-usda] path = "contracts/yield-token/yield-usda.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] + +[contracts.yield-wbtc] +path = "contracts/yield-token/yield-wbtc.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] + +[contracts.multisig-ytp-yield-wbtc] +path = "contracts/multisig/multisig-ytp-yield-wbtc.clar" +depends_on = ["ytp-yield-wbtc", "yield-wbtc"] + +[contracts.ytp-yield-wbtc] +path = "contracts/pool-token/ytp-yield-wbtc.clar" depends_on = ["trait-ownable", "trait-semi-fungible-token"] \ No newline at end of file diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index 5fb16b72..62492a33 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -54,14 +54,14 @@ ;; return token balance held by vault (define-public (get-balance (token )) - (contract-call? token get-balance (as-contract tx-sender)) + (contract-call? token get-balance-fixed (as-contract tx-sender)) ) ;; if sender is an approved contract, then transfer requested amount :qfrom vault to recipient (define-public (transfer-ft (token ) (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (as-contract (unwrap! (contract-call? token transfer amount tx-sender recipient none) ERR-TRANSFER-FAILED)) + (as-contract (unwrap! (contract-call? token transfer-fixed amount tx-sender recipient none) ERR-TRANSFER-FAILED)) (ok true) ) ) @@ -77,7 +77,7 @@ (define-public (transfer-sft (token ) (token-id uint) (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (as-contract (unwrap! (contract-call? token transfer token-id amount tx-sender recipient) ERR-TRANSFER-FAILED)) + (as-contract (unwrap! (contract-call? token transfer-fixed token-id amount tx-sender recipient) ERR-TRANSFER-FAILED)) (ok true) ) ) @@ -96,7 +96,7 @@ (asserts! (> pre-bal amount) ERR-INSUFFICIENT-FLASH-LOAN-BALANCE) ;; transfer loan to flash-loan-user - (as-contract (unwrap! (contract-call? token transfer amount tx-sender recipient none) ERR-LOAN-TRANSFER-FAILED)) + (as-contract (unwrap! (contract-call? token transfer-fixed amount tx-sender recipient none) ERR-LOAN-TRANSFER-FAILED)) ;; flash-loan-user executes with loan received (try! (contract-call? flash-loan-user execute token amount memo)) diff --git a/clarity/contracts/faucet.clar b/clarity/contracts/faucet.clar index edfbfcbc..66da7363 100644 --- a/clarity/contracts/faucet.clar +++ b/clarity/contracts/faucet.clar @@ -118,16 +118,16 @@ ) (map-set users recipient u1) ) - (and (> (var-get wbtc-amount) u0) (unwrap! (contract-call? .token-wbtc mint recipient (var-get wbtc-amount)) ERR-WBTC-TRANSFER-FAILED)) - (and (> (var-get usda-amount) u0) (unwrap! (contract-call? .token-usda mint recipient (var-get usda-amount)) ERR-USDA-TRANSFER-FAILED)) - (and (> (var-get alex-amount) u0) (unwrap! (contract-call? .token-t-alex mint recipient (var-get alex-amount)) ERR-ALEX-TRANSFER-FAILED)) + (and (> (var-get wbtc-amount) u0) (unwrap! (contract-call? .token-wbtc mint (var-get wbtc-amount) recipient) ERR-WBTC-TRANSFER-FAILED)) + (and (> (var-get usda-amount) u0) (unwrap! (contract-call? .token-usda mint (var-get usda-amount) recipient) ERR-USDA-TRANSFER-FAILED)) + (and (> (var-get alex-amount) u0) (unwrap! (contract-call? .token-t-alex mint (var-get alex-amount) recipient) ERR-ALEX-TRANSFER-FAILED)) (and (> (var-get stx-amount) u0) (unwrap! (stx-transfer? (/ (* (var-get stx-amount) (pow u10 u6)) ONE_8) tx-sender recipient) ERR-STX-TRANSFER-FAILED)) (ok true) ) ) (define-public (get-some-wstx-tokens) - (contract-call? .token-wstx mint tx-sender (var-get wstx-amount)) + (contract-call? .token-wstx mint (var-get wstx-amount) tx-sender) ) ;; SEND-MANY @@ -147,7 +147,7 @@ ) (define-private (mint-alex (recipient { to: principal, amount: uint })) - (ok (and (> (get amount recipient) u0) (unwrap! (contract-call? .token-t-alex mint (get to recipient) (get amount recipient)) ERR-ALEX-TRANSFER-FAILED))) + (ok (and (> (get amount recipient) u0) (unwrap! (contract-call? .token-t-alex mint (get amount recipient) (get to recipient)) ERR-ALEX-TRANSFER-FAILED))) ) (define-public (mint-alex-many (recipients (list 200 { to: principal, amount: uint }))) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index 0e8c2247..e45340f2 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -23,8 +23,6 @@ ) ) - - (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -124,4 +122,46 @@ (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) (ok true) ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply key-usda-wbtc))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance key-usda-wbtc who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) ) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index 339bf1d6..c210b93c 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -8,7 +8,7 @@ ;; 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. +;; This prototype is for aywbtc-wbtc pool token. ;; Common Trait and for each pool, implementation is required. ;; diff --git a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar index 0b9095a9..1cb30e27 100644 --- a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token fwp-wbtc-usda-50-50) diff --git a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar index 4c44de57..5e4317b6 100644 --- a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar +++ b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token lbp-alex-usda-90-10) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index b2db9035..302ddeb6 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -23,8 +23,6 @@ ) ) - - (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -124,4 +122,46 @@ (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) (ok true) ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-usda))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) ) \ No newline at end of file diff --git a/clarity/contracts/pool/alex-reserve-pool.clar b/clarity/contracts/pool/alex-reserve-pool.clar index 8d739bb2..a8795744 100644 --- a/clarity/contracts/pool/alex-reserve-pool.clar +++ b/clarity/contracts/pool/alex-reserve-pool.clar @@ -287,7 +287,7 @@ (asserts! (>= block-height (var-get activation-block)) ERR-CONTRACT-NOT-ACTIVATED) (asserts! (and (> lock-period u0) (<= lock-period MAX-REWARD-CYCLES)) ERR-CANNOT-STAKE) (asserts! (> amount-token u0) ERR-CANNOT-STAKE) - (unwrap! (contract-call? .token-alex transfer amount-token tx-sender .alex-vault none) ERR-TRANSFER-FAILED) + (unwrap! (contract-call? .token-alex transfer-fixed amount-token tx-sender .alex-vault none) ERR-TRANSFER-FAILED) (match (fold stake-tokens-closure REWARD-CYCLE-INDEXES (ok commitment)) ok-value (ok true) err-value (err err-value) @@ -387,7 +387,7 @@ ;; send back tokens if user was eligible (and (> to-return u0) (try! (contract-call? .alex-vault transfer-ft .token-alex to-return user))) ;; send back rewards if user was eligible - (and (> entitled-token u0) (try! (as-contract (contract-call? .token-alex mint user (mul-down entitled-token (get-coinbase-amount target-cycle)))))) + (and (> entitled-token u0) (try! (as-contract (contract-call? .token-alex mint-fixed (mul-down entitled-token (get-coinbase-amount target-cycle)) user)))) (ok true) ) ) diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index b56e014c..ddf74b06 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -28,7 +28,7 @@ (define-constant ERR-GET-SYMBOL-FAIL (err u6000)) (define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) (define-constant ERR-EXPIRY (err u2017)) -(define-constant ERR-GET-BALANCE-FAIL (err u6001)) +(define-constant ERR-get-balance-fixed-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)) @@ -452,7 +452,7 @@ (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (yield-supply (get yield-supply pool)) - (total-shares (unwrap! (contract-call? the-yield-token get-balance expiry tx-sender) ERR-GET-BALANCE-FAIL)) + (total-shares (unwrap! (contract-call? the-yield-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (shares-to-yield (div-down shares yield-supply)) @@ -525,7 +525,7 @@ (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (key-supply (get key-supply pool)) - (total-shares (unwrap! (contract-call? the-key-token get-balance expiry tx-sender) ERR-GET-BALANCE-FAIL)) + (total-shares (unwrap! (contract-call? the-key-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (reduce-data (try! (get-position-given-burn-key token collateral expiry shares))) (dx-weighted (get dx reduce-data)) diff --git a/clarity/contracts/pool/fixed-weight-pool.clar b/clarity/contracts/pool/fixed-weight-pool.clar index 045163b9..6e14e5c2 100644 --- a/clarity/contracts/pool/fixed-weight-pool.clar +++ b/clarity/contracts/pool/fixed-weight-pool.clar @@ -1,6 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait pool-token-trait .trait-pool-token.pool-token-trait) (use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) ;; fixed-weight-pool @@ -119,13 +118,13 @@ (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 }) ) -;; @desc get-balances ({balance-x, balance-y}) +;; @desc get-balance-fixeds ({balance-x, balance-y}) ;; @param token-x-trait; token-x ;; @param token-y-trait; token-y ;; @param weight-x; weight of token-x ;; @param weight-y; weight of token-y ;; @returns (response (tuple uint uint) uint) -(define-read-only (get-balances (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) +(define-read-only (get-balance-fixeds (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint)) (let ( (token-x (contract-of token-x-trait)) @@ -275,7 +274,7 @@ ;; @param dx; amount of token-x added ;; @param dy; amount of token-y added ;; @returns (response bool uint) -(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)) +(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)) (let ( (token-x (contract-of token-x-trait)) @@ -328,7 +327,7 @@ ;; @param dx; amount of token-x added ;; @param dy; amount of token-y added ;; @returns (response (tuple uint uint uint) uint) -(define-public (add-to-position (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (the-pool-token ) (dx uint) (dy uint)) +(define-public (add-to-position (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (the-pool-token ) (dx uint) (dy uint)) (begin (asserts! (and (> dx u0) (> dy u0)) ERR-INVALID-LIQUIDITY) @@ -357,7 +356,7 @@ ;; mint pool token and send to tx-sender (map-set pools-data-map { token-x: token-x, token-y: token-y, weight-x: weight-x, weight-y: weight-y } pool-updated) - (try! (contract-call? the-pool-token mint tx-sender new-supply)) + (try! (contract-call? the-pool-token mint new-supply tx-sender)) (print { object: "pool", action: "liquidity-added", data: pool-updated }) (ok {supply: new-supply, dx: dx, dy: new-dy}) @@ -374,7 +373,7 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param percent; percentage of pool token held to reduce ;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (the-pool-token ) (percent uint)) +(define-public (reduce-position (token-x-trait ) (token-y-trait ) (weight-x uint) (weight-y uint) (the-pool-token ) (percent uint)) (begin (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) (let @@ -384,7 +383,7 @@ (pool (unwrap! (map-get? pools-data-map { token-x: token-x, token-y: token-y, weight-x: weight-x, weight-y: weight-y }) ERR-INVALID-POOL-ERR)) (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) - (total-shares (unwrap-panic (contract-call? the-pool-token get-balance tx-sender))) + (total-shares (unwrap-panic (contract-call? the-pool-token get-balance-fixed tx-sender))) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (total-supply (get total-supply pool)) (reduce-data (try! (get-position-given-burn token-x-trait token-y-trait weight-x weight-y shares))) @@ -405,7 +404,7 @@ (map-set pools-data-map { token-x: token-x, token-y: token-y, weight-x: weight-x, weight-y: weight-y } pool-updated) - (try! (contract-call? the-pool-token burn tx-sender shares)) + (try! (contract-call? the-pool-token burn shares tx-sender)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: dx, dy: dy}) diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index 66864335..0a5c4631 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -1,6 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait pool-token-trait .trait-pool-token.pool-token-trait) (use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) ;; liquidity-bootstrapping-pool @@ -76,7 +75,7 @@ ;; ;; liquidity injection is allowed at the pool creation only -(define-private (add-to-position (token-x-trait ) (token-y-trait ) (expiry uint) (the-pool-token ) (dx uint) (dy uint)) +(define-private (add-to-position (token-x-trait ) (token-y-trait ) (expiry uint) (the-pool-token ) (dx uint) (dy uint)) (begin (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (let @@ -105,7 +104,7 @@ ;; mint pool token-x and send to tx-sender (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token mint tx-sender new-supply)) + (try! (contract-call? the-pool-token mint new-supply tx-sender)) (print { object: "pool", action: "liquidity-added", data: pool-updated }) (ok true) ) @@ -211,12 +210,12 @@ ) ) -;; @desc get-balances ({balance-x, balance-y}) +;; @desc get-balance-fixeds ({balance-x, balance-y}) ;; @param token-x-trait; token-x ;; @param token-y-trait; token-y ;; @param expiry; expiry ;; @returns (response (tuple uint uint) uint) -(define-read-only (get-balances (token-x-trait ) (token-y-trait ) (expiry uint)) +(define-read-only (get-balance-fixeds (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)) @@ -237,7 +236,7 @@ ;; @param dx; amount of token-x added ;; @param dy; amount of token-y added ;; @returns (response bool uint) -(define-public (create-pool (token-x-trait ) (token-y-trait ) (weight-x-0 uint) (weight-x-1 uint) (expiry uint) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) +(define-public (create-pool (token-x-trait ) (token-y-trait ) (weight-x-0 uint) (weight-x-1 uint) (expiry uint) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) (let ( (pool-id (+ (var-get pool-count) u1)) @@ -280,7 +279,7 @@ ;; @param pool-token; pool token representing ownership of the pool ;; @param percent; percentage of pool token held to reduce ;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position (token-x-trait ) (token-y-trait ) (expiry uint) (the-pool-token ) (percent uint)) +(define-public (reduce-position (token-x-trait ) (token-y-trait ) (expiry uint) (the-pool-token ) (percent uint)) (begin (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) (let @@ -290,7 +289,7 @@ (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)) - (total-shares (unwrap-panic (contract-call? the-pool-token get-balance tx-sender))) + (total-shares (unwrap-panic (contract-call? the-pool-token get-balance-fixed tx-sender))) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (total-supply (get total-supply pool)) (reduce-data (try! (get-position-given-burn token-x-trait token-y-trait expiry shares))) @@ -309,7 +308,7 @@ (try! (contract-call? .alex-vault ft-transfer-multi token-x-trait dx token-y-trait dy tx-sender)) (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token burn tx-sender shares)) + (try! (contract-call? the-pool-token burn shares tx-sender)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: dx, dy: dy}) ) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 8683233f..3f068d0a 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -396,7 +396,7 @@ (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) (total-supply (get total-supply pool)) - (total-shares (unwrap-panic (contract-call? the-pool-token get-balance expiry tx-sender))) + (total-shares (unwrap-panic (contract-call? the-pool-token get-balance-fixed expiry tx-sender))) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (reduce-data (try! (get-position-given-burn expiry the-yield-token shares))) (dx (get dx reduce-data)) @@ -484,7 +484,7 @@ (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 (> dx u0) (unwrap! (contract-call? the-token transfer-fixed dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED)) (and (> dy u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy tx-sender))) (try! (contract-call? .alex-reserve-pool add-to-balance (contract-of the-token) (- fee fee-rebate))) @@ -535,7 +535,7 @@ (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) + (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) ;; post setting diff --git a/clarity/contracts/token/token-alex.clar b/clarity/contracts/token/token-alex.clar index 2e2322aa..3f6c9a90 100644 --- a/clarity/contracts/token/token-alex.clar +++ b/clarity/contracts/token/token-alex.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token alex) @@ -30,7 +30,7 @@ ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply alex))) + (ok (ft-get-supply alex)) ) (define-read-only (get-name) @@ -46,7 +46,7 @@ ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance alex account))) + (ok (ft-get-balance alex account)) ) (define-public (set-token-uri (value (string-utf8 256))) @@ -60,24 +60,10 @@ (ok (some (var-get token-uri))) ) -(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-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? alex (fixed-to-decimals amount) sender recipient) + (match (ft-transfer? alex amount sender recipient) response (begin (print memo) (ok response) @@ -87,20 +73,54 @@ ) ) -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (ft-mint? alex (fixed-to-decimals amount) recipient) + (ft-mint? alex amount recipient) ) ) -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (begin (try! (check-is-approved contract-caller)) - (ft-burn? alex (fixed-to-decimals amount) sender) + (ft-burn? alex amount sender) ) ) +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply alex))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance alex account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + (begin (map-set approved-contracts .alex-reserve-pool true) (map-set approved-contracts .faucet true) diff --git a/clarity/contracts/token/token-t-alex-v2.clar b/clarity/contracts/token/token-t-alex-v2.clar deleted file mode 100644 index 71ba9990..00000000 --- a/clarity/contracts/token/token-t-alex-v2.clar +++ /dev/null @@ -1,114 +0,0 @@ -(impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) - -(define-fungible-token t-alex) - -(define-data-var token-uri (string-utf8 256) u"") -(define-data-var CONTRACT-OWNER principal tx-sender) -(define-map approved-contracts principal bool) - -;; errors -(define-constant ERR-NOT-AUTHORIZED (err u1000)) - -(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)) - ) -) - -(define-private (check-is-approved (sender principal)) - (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get CONTRACT-OWNER))) ERR-NOT-AUTHORIZED)) -) - -;; --------------------------------------------------------- -;; SIP-10 Functions -;; --------------------------------------------------------- - -(define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply t-alex))) -) - -(define-read-only (get-name) - (ok "t-alex") -) - -(define-read-only (get-symbol) - (ok "t-alex") -) - -(define-read-only (get-decimals) - (ok u0) -) - -(define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance t-alex 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-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-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? t-alex (fixed-to-decimals amount) sender recipient) - response (begin - (print memo) - (ok response) - ) - error (err error) - ) - ) -) - -(define-public (mint (recipient principal) (amount uint)) - (begin - (try! (check-is-approved contract-caller)) - (ft-mint? t-alex (fixed-to-decimals amount) recipient) - ) -) - -(define-public (burn (sender principal) (amount uint)) - (begin - (try! (check-is-approved contract-caller)) - (ft-burn? t-alex (fixed-to-decimals amount) sender) - ) -) - -(begin - (map-set approved-contracts .alex-reserve-pool true) - (map-set approved-contracts .faucet-v5 true) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? t-alex u1000000000 tx-sender)) -) - - diff --git a/clarity/contracts/token/token-t-alex.clar b/clarity/contracts/token/token-t-alex.clar index 3d789cc5..8f89333f 100644 --- a/clarity/contracts/token/token-t-alex.clar +++ b/clarity/contracts/token/token-t-alex.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token t-alex) @@ -30,7 +30,7 @@ ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply t-alex))) + (ok (ft-get-supply t-alex)) ) (define-read-only (get-name) @@ -46,7 +46,7 @@ ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance t-alex account))) + (ok (ft-get-balance t-alex account)) ) (define-public (set-token-uri (value (string-utf8 256))) @@ -60,24 +60,10 @@ (ok (some (var-get token-uri))) ) -(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-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? t-alex (fixed-to-decimals amount) sender recipient) + (match (ft-transfer? t-alex amount sender recipient) response (begin (print memo) (ok response) @@ -87,20 +73,54 @@ ) ) -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (ft-mint? t-alex (fixed-to-decimals amount) recipient) + (ft-mint? t-alex amount recipient) ) ) -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (begin (try! (check-is-approved contract-caller)) - (ft-burn? t-alex (fixed-to-decimals amount) sender) + (ft-burn? t-alex amount sender) ) ) +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply t-alex))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance t-alex account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + (begin (map-set approved-contracts .alex-reserve-pool true) (map-set approved-contracts .faucet true) diff --git a/clarity/contracts/token/token-usda.clar b/clarity/contracts/token/token-usda.clar index 1945957f..4b10309a 100644 --- a/clarity/contracts/token/token-usda.clar +++ b/clarity/contracts/token/token-usda.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token usda) @@ -30,23 +30,23 @@ ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply usda))) + (ok (ft-get-supply usda)) ) (define-read-only (get-name) - (ok "USDA") + (ok "usda") ) (define-read-only (get-symbol) - (ok "USDA") + (ok "usda") ) (define-read-only (get-decimals) - (ok u2) + (ok u0) ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance usda account))) + (ok (ft-get-balance usda account)) ) (define-public (set-token-uri (value (string-utf8 256))) @@ -60,24 +60,10 @@ (ok (some (var-get token-uri))) ) -(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-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? usda (fixed-to-decimals amount) sender recipient) + (match (ft-transfer? usda amount sender recipient) response (begin (print memo) (ok response) @@ -87,20 +73,54 @@ ) ) -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (ft-mint? usda (fixed-to-decimals amount) recipient) + (ft-mint? usda amount recipient) ) ) -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (begin (try! (check-is-approved contract-caller)) - (ft-burn? usda (fixed-to-decimals amount) sender) + (ft-burn? usda amount sender) ) ) +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply usda))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance usda account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + (begin (map-set approved-contracts .faucet true) ) @@ -108,6 +128,6 @@ ;; Initialize the contract for Testing. (begin (try! (ft-mint? usda u1000000000 tx-sender)) - (try! (ft-mint? usda u10000000 .alex-reserve-pool)) + (try! (ft-mint? usda u10000000 .usda-reserve-pool)) (try! (ft-mint? usda u200000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 ) diff --git a/clarity/contracts/token/token-wbtc.clar b/clarity/contracts/token/token-wbtc.clar index 60b870cc..702d4d7c 100644 --- a/clarity/contracts/token/token-wbtc.clar +++ b/clarity/contracts/token/token-wbtc.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token wbtc) @@ -30,23 +30,23 @@ ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply wbtc))) + (ok (ft-get-supply wbtc)) ) (define-read-only (get-name) - (ok "WBTC") + (ok "wbtc") ) (define-read-only (get-symbol) - (ok "WBTC") + (ok "wbtc") ) (define-read-only (get-decimals) - (ok u8) + (ok u0) ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance wbtc account))) + (ok (ft-get-balance wbtc account)) ) (define-public (set-token-uri (value (string-utf8 256))) @@ -60,24 +60,10 @@ (ok (some (var-get token-uri))) ) -(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-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? wbtc (fixed-to-decimals amount) sender recipient) + (match (ft-transfer? wbtc amount sender recipient) response (begin (print memo) (ok response) @@ -87,20 +73,54 @@ ) ) -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (ft-mint? wbtc (fixed-to-decimals amount) recipient) + (ft-mint? wbtc amount recipient) ) ) -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (begin (try! (check-is-approved contract-caller)) - (ft-burn? wbtc (fixed-to-decimals amount) sender) + (ft-burn? wbtc amount sender) ) ) +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply wbtc))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance wbtc account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + (begin (map-set approved-contracts .faucet true) ) diff --git a/clarity/contracts/token/token-wstx.clar b/clarity/contracts/token/token-wstx.clar index 057187ea..50923a4d 100644 --- a/clarity/contracts/token/token-wstx.clar +++ b/clarity/contracts/token/token-wstx.clar @@ -1,5 +1,5 @@ (impl-trait .trait-ownable.ownable-trait) -(impl-trait .trait-pool-token.pool-token-trait) +(impl-trait .trait-sip-010.sip-010-trait) (define-fungible-token wstx) @@ -26,7 +26,7 @@ ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply wstx))) + (ok (ft-get-supply wstx)) ) (define-read-only (get-name) @@ -42,7 +42,7 @@ ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance wstx account))) + (ok (ft-get-balance wstx account)) ) (define-public (set-token-uri (value (string-utf8 256))) @@ -56,24 +56,10 @@ (ok (some (var-get token-uri))) ) -(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-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? wstx (fixed-to-decimals amount) sender recipient) + (match (ft-transfer? wstx amount sender recipient) response (begin (print memo) (ok response) @@ -84,23 +70,57 @@ ) ;; This can only be called by recipient since stx-transfer is involved ;; tx-sender -> .alex-vault -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (begin (asserts! (is-eq tx-sender recipient) ERR-NOT-TOKEN-OWNER) - (try! (stx-transfer? (/ (* amount (pow u10 u6)) ONE_8) recipient .alex-vault)) - (ft-mint? wstx (fixed-to-decimals amount) recipient) + (try! (stx-transfer? amount recipient .alex-vault)) + (ft-mint? wstx amount recipient) ) ) ;; This can only be called by sender since ft-burn is involved -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (begin (asserts! (is-eq tx-sender sender) ERR-NOT-TOKEN-OWNER) (as-contract (try! (contract-call? .alex-vault transfer-stx amount tx-sender sender))) - (ft-burn? wstx (fixed-to-decimals amount) sender) + (ft-burn? wstx amount sender) ) ) +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply wstx))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance wstx account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + ;; Initialize the contract for Testing. (begin (try! (ft-mint? wstx u2000000000000 tx-sender)) diff --git a/clarity/contracts/traits/trait-pool-token.clar b/clarity/contracts/traits/trait-pool-token.clar deleted file mode 100644 index 0a027f17..00000000 --- a/clarity/contracts/traits/trait-pool-token.clar +++ /dev/null @@ -1,34 +0,0 @@ -(define-trait pool-token-trait - ( - ;; sip-010 - - ;; Transfer from the caller to a new principal - (transfer (uint principal principal (optional (buff 34))) (response bool uint)) - - ;; the human readable name of the token - (get-name () (response (string-ascii 32) uint)) - - ;; the ticker symbol, or empty if none - (get-symbol () (response (string-ascii 32) uint)) - - ;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token - (get-decimals () (response uint uint)) - - ;; the balance of the passed principal - (get-balance (principal) (response uint uint)) - - ;; the current total supply (which does not need to be a constant) - (get-total-supply () (response uint uint)) - - ;; an optional URI that represents metadata of this token - (get-token-uri () (response (optional (string-utf8 256)) uint)) - - ;; additional functions - - ;;mint(principal uint):bool - (mint (principal uint) (response bool uint)) - - ;;burn(principal uint):bool - (burn (principal uint) (response bool uint)) - ) -) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar index ca88a95a..edc0d6a8 100644 --- a/clarity/contracts/traits/trait-semi-fungible-token.clar +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -30,8 +30,17 @@ ;; Transfer many tokens at once with memos. (transfer-many-memo ((list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)})) (response bool uint)) - (mint (uint uint principal) (response bool uint)) - - (burn (uint uint principal) (response bool uint)) + (mint (uint uint principal) (response bool uint)) + (burn (uint uint principal) (response bool uint)) + + ;; helper functions for 8-digit fixed notation + (transfer-fixed (uint uint principal principal) (response bool uint)) + (get-balance-fixed (uint principal) (response uint uint)) + (get-total-supply-fixed (uint) (response uint uint)) + (get-overall-balance-fixed (uint principal) (response uint uint)) + (get-total-supply-fixed (uint uint) (response uint uint)) + (get-overall-supply (uint) (response uint uint)) + (mint-fixed (uint principal) (response bool uint)) + (burn-fixed (uint principal) (response bool uint)) ) ) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-sip-010.clar b/clarity/contracts/traits/trait-sip-010.clar index b6436a11..9e0940c2 100644 --- a/clarity/contracts/traits/trait-sip-010.clar +++ b/clarity/contracts/traits/trait-sip-010.clar @@ -25,5 +25,17 @@ ;; an optional URI that represents metadata of this token (get-token-uri () (response (optional (string-utf8 256)) uint)) + + ;; additional functions + + (mint (uint principal) (response bool uint)) + (burn (uint principal) (response bool uint)) + + ;; helper functions for 8-digit fixed notation + (transfer-fixed (uint principal principal (optional (buff 34))) (response bool uint)) + (get-balance-fixed (principal) (response uint uint)) + (get-total-supply-fixed () (response uint uint)) + (mint-fixed (uint principal) (response bool uint)) + (burn-fixed (uint principal) (response bool uint)) ) ) \ No newline at end of file diff --git a/clarity/contracts/traits/trait-yield-token.clar b/clarity/contracts/traits/trait-yield-token.clar deleted file mode 100644 index 5716a8ec..00000000 --- a/clarity/contracts/traits/trait-yield-token.clar +++ /dev/null @@ -1,38 +0,0 @@ -(define-trait yield-token-trait - ( - ;; Transfer from the caller to a new principal - (transfer (uint principal principal (optional (buff 34))) (response bool uint)) - - ;; the human readable name of the token - (get-name () (response (string-ascii 32) uint)) - - ;; the ticker symbol, or empty if none - (get-symbol () (response (string-ascii 32) uint)) - - ;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token - (get-decimals () (response uint uint)) - - ;; the balance of the passed principal - (get-balance (principal) (response uint uint)) - - ;; the current total supply (which does not need to be a constant) - (get-total-supply () (response uint uint)) - - ;; an optional URI that represents metadata of this token - (get-token-uri () (response (optional (string-utf8 256)) uint)) - - ;; additional functions - - ;; mint(principal uint):bool - (mint (principal uint) (response bool uint)) - - ;; burn(principal uint):bool - (burn (principal uint) (response bool uint)) - - ;; the underlying token - (get-token () (response principal uint)) - - ;; the expiry - (get-expiry () (response uint uint)) - ) -) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index 899efa97..af28ddb6 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -23,8 +23,6 @@ ) ) - - (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -124,4 +122,46 @@ (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) (ok true) ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply yield-usda))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance yield-usda who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) ) \ No newline at end of file diff --git a/clarity/tests/models/alex-tests-tokens.ts b/clarity/tests/models/alex-tests-tokens.ts index 05981538..d7dbf728 100644 --- a/clarity/tests/models/alex-tests-tokens.ts +++ b/clarity/tests/models/alex-tests-tokens.ts @@ -161,3 +161,25 @@ class KEY_WBTC_59760_USDA { } } export { KEY_WBTC_59760_USDA }; + +class YIELD_WBTC { + chain: Chain; + deployer: Account; + + constructor(chain: Chain, deployer: Account) { + this.chain = chain; + this.deployer = deployer; + } + + mint(expiry: number, amount: number, recipient: string) { + let block = this.chain.mineBlock([ + Tx.contractCall("yield-wbtc", "transfer", [ + types.uint(expiry), + types.uint(amount,) + types.principal(recipient) + ], this.deployer.address), + ]); + return block.receipts[0].result; + } +} +export { YIELD_WBTC }; diff --git a/clarity/tests/models/alex-tests-yield-token-pool.ts b/clarity/tests/models/alex-tests-yield-token-pool.ts index 556fd428..bd59b4af 100644 --- a/clarity/tests/models/alex-tests-yield-token-pool.ts +++ b/clarity/tests/models/alex-tests-yield-token-pool.ts @@ -22,27 +22,31 @@ import { ], this.deployer.address); } - getYield(aytoken: string) { + getYield(expiry: number, aytoken: string) { return this.chain.callReadOnlyFn("yield-token-pool", "get-yield", [ + types.uint(expiry), types.principal(aytoken) ], this.deployer.address); } - getPrice(aytoken: string) { + getPrice(expiry: number, aytoken: string) { return this.chain.callReadOnlyFn("yield-token-pool", "get-price", [ + types.uint(expiry), types.principal(aytoken) ], this.deployer.address); } - getPoolDetails(aytoken: string) { + getPoolDetails(expiry: number, aytoken: string) { return this.chain.callReadOnlyFn("yield-token-pool", "get-pool-details", [ + types.uint(expiry), types.principal(aytoken) ], this.deployer.address); } - createPool(user: Account, aytoken: string, token: string, pooltoken: string, multiSig: string, dX: number, dY: number) { + createPool(user: Account, expiry: number, aytoken: string, token: string, pooltoken: string, multiSig: string, dX: number, dY: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "create-pool", [ + types.uint(expiry), types.principal(aytoken), types.principal(token), types.principal(pooltoken), @@ -54,10 +58,11 @@ import { return block.receipts[0].result; } - addToPosition(user: Account, aytoken: string, token: string, pooltoken: string, dX: number) { + addToPosition(user: Account, expiry: number, aytoken: string, token: string, pooltoken: string, dX: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "add-to-position", [ - types.principal(aytoken), + types.uint(expiry), + types.principal(aytoken), types.principal(token), types.principal(pooltoken), types.uint(dX), @@ -66,10 +71,11 @@ import { return block.receipts[0].result; } - buyAndAddToPosition(user: Account, aytoken: string, token: string, pooltoken: string, dX: number) { + buyAndAddToPosition(user: Account, expiry: number, aytoken: string, token: string, pooltoken: string, dX: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "buy-and-add-to-position", [ - types.principal(aytoken), + types.uint(expiry), + types.principal(aytoken), types.principal(token), types.principal(pooltoken), types.uint(dX), @@ -78,24 +84,25 @@ import { return block.receipts[0].result; } - rollPosition(user: Account, aytoken: string, token: string, pooltoken: string, percent: number, aytoken2: string, pooltoken2: string) { + rollPosition(user: Account, expiry: number, aytoken: string, token: string, pooltoken: string, percent: number, expiry_to_roll: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "roll-position", [ - types.principal(aytoken), + types.uint(expiry), + types.principal(aytoken), types.principal(token), types.principal(pooltoken), types.uint(percent), - types.principal(aytoken2), - types.principal(pooltoken2) + types.uint(expiry_to_roll) ], user.address), ]); return block.receipts[0].result; } - reducePosition(user: Account, aytoken: string, token: string, pooltoken: string, percent: number) { + reducePosition(user: Account, expiry: number, aytoken: string, token: string, pooltoken: string, percent: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "reduce-position", [ - types.principal(aytoken), + types.uint(expiry), + types.principal(aytoken), types.principal(token), types.principal(pooltoken), types.uint(percent) @@ -104,9 +111,10 @@ import { return block.receipts[0].result; } - swapXForY(user: Account, aytoken: string, token: string, dX: number, dy_min: number) { + swapXForY(user: Account, expiry: number, aytoken: string, token: string, dX: number, dy_min: number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "swap-x-for-y", [ + types.uint(expiry), types.principal(aytoken), types.principal(token), types.uint(dX), @@ -116,10 +124,11 @@ import { return block.receipts[0].result; } - swapYForX(user: Account, aytoken: string, token: string, dY: number, dx_min: number) { + swapYForX(user: Account, expiry: number, 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.uint(expiry), + types.principal(aytoken), types.principal(token), types.uint(dY), types.some(types.uint(dx_min)) @@ -128,70 +137,79 @@ import { return block.receipts[0].result; } - getYgivenX(aytoken: string, dx: number) { + getYgivenX(expiry: number, aytoken: string, dx: number) { return this.chain.callReadOnlyFn("yield-token-pool", "get-y-given-x", [ + types.uint(expiry), types.principal(aytoken), types.uint(dx) ], this.deployer.address); } - getXgivenY(aytoken: string, dy: number) { + getXgivenY(expiry: number, aytoken: string, dy: number) { return this.chain.callReadOnlyFn("yield-token-pool", "get-x-given-y", [ + types.uint(expiry), types.principal(aytoken), types.uint(dy) ], this.deployer.address); } - getYgivenPrice(aytoken: string, price: number) { + getYgivenPrice(expiry: number, aytoken: string, price: number) { return this.chain.callReadOnlyFn("yield-token-pool", "get-y-given-price", [ + types.uint(expiry), types.principal(aytoken), types.uint(price) ], this.deployer.address); } - getXgivenPrice(aytoken: string, price: number) { + getXgivenPrice(expiry: number, aytoken: string, price: number) { return this.chain.callReadOnlyFn("yield-token-pool", "get-x-given-price", [ + types.uint(expiry), types.principal(aytoken), types.uint(price) ], this.deployer.address); } - getYgivenYield(aytoken: string, yied: number) { + getYgivenYield(expiry: number, aytoken: string, yied: number) { return this.chain.callReadOnlyFn("yield-token-pool", "get-y-given-yield", [ + types.uint(expiry), types.principal(aytoken), types.uint(yied) ], this.deployer.address); } - getXgivenYield(aytoken: string, yied: number) { + getXgivenYield(expiry: number, aytoken: string, yied: number) { return this.chain.callReadOnlyFn("yield-token-pool", "get-x-given-yield", [ + types.uint(expiry), types.principal(aytoken), types.uint(yied) ], this.deployer.address); } - getFeetoAddress(user: Account, aytoken: string) { + getFeetoAddress(user: Account, expiry: number, aytoken: string) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "get-fee-to-address", [ + types.uint(expiry), types.principal(aytoken) ], user.address), ]); return block.receipts[0].result; } - collectFees(user: Account, aytoken: string, token: string) { + collectFees(user: Account, expiry: number, aytoken: string, token: string) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "collect-fees", [ - types.principal(aytoken), + types.uint(expiry), + types.principal(aytoken), types.principal(token), ], user.address), ]); return block.receipts[0].result; } - setFeeRateToken(user: Account, aytoken: string, feerate:number) { + setFeeRateToken(user: Account, expiry: number, aytoken: string, feerate:number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "set-fee-rate-token", [ + types.uint(expiry), types.principal(aytoken), types.uint(feerate) ], user.address), @@ -199,9 +217,10 @@ import { return block.receipts[0].result; } - setFeeRateayToken(user: Account, aytoken: string, feerate:number) { + setFeeRateayToken(user: Account, expiry: number, aytoken: string, feerate:number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "set-fee-rate-aytoken", [ + types.uint(expiry), types.principal(aytoken), types.uint(feerate) ], user.address), @@ -209,27 +228,30 @@ import { return block.receipts[0].result; } - getFeeRateToken(user: Account, aytoken: string) { + getFeeRateToken(user: Account, expiry: number, aytoken: string) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "get-fee-rate-token", [ + types.uint(expiry), types.principal(aytoken) ], user.address), ]); return block.receipts[0].result; } - getFeeRateayToken(user: Account, aytoken: string) { + getFeeRateayToken(user: Account, expiry: number, aytoken: string) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "get-fee-rate-aytoken", [ + types.uint(expiry), types.principal(aytoken) ], user.address), ]); return block.receipts[0].result; } - setFeeRebate(user: Account, aytoken: string, rebate : number) { + setFeeRebate(user: Account, expiry: number, aytoken: string, rebate : number) { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "set-fee-rebate", [ + types.uint(expiry), types.principal(aytoken), types.uint(rebate) ], user.address), @@ -237,8 +259,9 @@ import { return block.receipts[0].result; } - getFeeRebate(aytoken: string) { + getFeeRebate(expiry: number, aytoken: string) { return this.chain.callReadOnlyFn("yield-token-pool", "get-fee-rebate", [ + types.uint(expiry), types.principal(aytoken), ], this.deployer.address); } diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index 5ea3f416..97cf51d8 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -3,33 +3,23 @@ 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 { - YTPTestAgent1, - } from './models/alex-tests-yield-token-pool.ts'; +import { YTPTestAgent1, } from './models/alex-tests-yield-token-pool.ts'; -import { - MS_YTP_WBT_59760, -} from './models/alex-tests-multisigs.ts'; +import { MS_YTP_WBT_59760 } from './models/alex-tests-multisigs.ts'; -import { - USDAToken, - WBTCToken, - POOLTOKEN_YTP_WBTC_WBTC_59760 - } from './models/alex-tests-tokens.ts'; +import { USDAToken, WBTCToken, YIELD_WBTC } from './models/alex-tests-tokens.ts'; // Deployer Address Constants const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" -const yieldwbtc59760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc-59760" -const ytpyieldwbtc59760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc-59760-wbtc" -const multisigytpyieldwbtc59760 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc-59760-wbtc" -const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda-23040-usda" -const yieldwbtc80875Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc-80875" -const ytpyieldwbtc80875Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc-80875-wbtc" -const multisigytpyieldwbtc80875 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc-80875-wbtc" +const yieldwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc" +const ytpyieldwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc" +const multisigytpyieldwbtc = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc" const ONE_8 = 100000000 const expiry = 59760 * ONE_8 +const wrongExpiry = 70000 * ONE_8 +const anotherExpiry = 80875 * ONE_8 /** * Yield Token Pool Test Cases @@ -44,11 +34,11 @@ Clarinet.test({ let YTPTest = new YTPTestAgent1(chain, deployer); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print - let call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); @@ -57,7 +47,7 @@ Clarinet.test({ let listed = 100000000; //Add extra liquidity - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 10*ONE_8); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(10*ONE_8); position['balance-token'].expectUint(10*ONE_8); @@ -65,7 +55,7 @@ Clarinet.test({ position['balance-virtual'].expectUint(10*ONE_8); // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1010*ONE_8); position['balance-token'].expectUint(1010*ONE_8); @@ -73,13 +63,13 @@ Clarinet.test({ position['balance-virtual'].expectUint(1010*ONE_8); // Remove all liquidlity - result = YTPTest.reducePosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, ONE_8); + result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, ONE_8); position =result.expectOk().expectTuple(); position['dx'].expectUint(1010*ONE_8); position['dy'].expectUint(0); // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(0); position['balance-token'].expectUint(0); @@ -87,7 +77,7 @@ Clarinet.test({ position['balance-virtual'].expectUint(0); // Add back some liquidity - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 1000*ONE_8); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(1000*ONE_8); position['balance-token'].expectUint(1000*ONE_8); @@ -102,28 +92,28 @@ Clarinet.test({ call.result.expectOk().expectUint(710557) // zero actual yield-token, so must throw an error - call = await YTPTest.getYgivenX(yieldwbtc59760Address, 1*ONE_8); + call = await YTPTest.getYgivenX(expiry, yieldwbtcAddress, 1*ONE_8); call.result.expectErr().expectUint(2016) // zero actual yield-token, so yield must be zero call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(5) // zero rate environment, so yield-token and token are at parity. - call = await YTPTest.getXgivenY(yieldwbtc59760Address, 2*ONE_8); + call = await YTPTest.getXgivenY(expiry, yieldwbtcAddress, 2*ONE_8); call.result.expectOk().expectUint(199975237) // sell some yield-token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 2*ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 2*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(199975237); position['dy'].expectUint(2*ONE_8); // yield-token now has "actual" balance call = chain.callReadOnlyFn("yield-token-pool", "get-pool-details", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); position = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(99800024763); @@ -132,22 +122,22 @@ Clarinet.test({ // now that yield token supply > token supply, yield is positive. call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(2847); // buy back some yield token - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, ONE_8, 0); position = result.expectOk().expectTuple() position['dx'].expectUint(ONE_8); position['dy'].expectUint(100028858); // 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, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 501*ONE_8, 0); position =result.expectErr().expectUint(4002) call = chain.callReadOnlyFn("yield-token-pool", "get-pool-details", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); position = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(99900024763); @@ -156,19 +146,19 @@ Clarinet.test({ // after buying back some yield token, yield decreases. call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(1426); // we sell close to maximum allowed of yield token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 29*ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 29*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(2900524394); position['dy'].expectUint(29*ONE_8); // which moves yield substantially into the positive territory. call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(42661); @@ -184,65 +174,65 @@ Clarinet.test({ // about half way, so yield should halve, just like zero coupon bond gets closer to par call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(21334); // sell some (a lot of) yield-token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 100*ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 100*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(10001386469); position['dy'].expectUint(100*ONE_8); // and see how it pushes the yield pretty high call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(92959); //buy back some yield token - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 100*ONE_8, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 100*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(100*ONE_8); position['dy'].expectUint(10005727560); - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(100000000000); - call = chain.callReadOnlyFn(ytpyieldwbtc59760Address, "get-balance", + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", [types.principal(deployer.address) ], deployer.address); call.result.expectOk().expectUint(100000000000); // Remove some liquidlity - result = YTPTest.reducePosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 0.5*ONE_8); + result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0.5*ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(48499056950); position['dy'].expectUint(1497121749); - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(50000000000); - call = chain.callReadOnlyFn(ytpyieldwbtc59760Address, "get-balance", + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", [types.principal(deployer.address) ], deployer.address); call.result.expectOk().expectUint(50000000000); // Add back some liquidity - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 1000*ONE_8); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(103094788000); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(3086908988); position['balance-virtual'].expectUint(103094788085); - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(50000000000 + 103094788000); - call = chain.callReadOnlyFn(ytpyieldwbtc59760Address, "get-balance", + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", [types.principal(deployer.address) ], deployer.address); call.result.expectOk().expectUint(50000000000 + 103094788000); @@ -259,19 +249,19 @@ Clarinet.test({ // nearly maturity, so yield should be close to zero. call = chain.callReadOnlyFn("yield-token-pool", "get-yield", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(5); // buy some yield-token - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 19*ONE_8, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 19*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(19*ONE_8); position['dy'].expectUint(1900033901); // on expiry, the prices are back to parity. call = chain.callReadOnlyFn("yield-token-pool", "get-price", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(100000005); // par @@ -280,31 +270,31 @@ Clarinet.test({ // on expiry, the prices are back to parity. call = chain.callReadOnlyFn("yield-token-pool", "get-price", - [types.principal(yieldwbtc59760Address) + [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); call.result.expectOk().expectUint(100000005); // par // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(153094788000); position['balance-token'].expectUint(150399056950); position['balance-yield-token'].expectUint(2683996920); position['balance-virtual'].expectUint(153094788043); - call = chain.callReadOnlyFn(ytpyieldwbtc59760Address, "get-balance", + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", [types.principal(deployer.address) ], deployer.address); call.result.expectOk().expectUint(153094788000); // Remove all liquidlity - result = YTPTest.reducePosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, ONE_8); + result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, ONE_8); position =result.expectOk().expectTuple(); position['dx'].expectUint(150399056950); position['dy'].expectUint(2683996806); // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(0); position['balance-token'].expectUint(0); @@ -322,23 +312,23 @@ Clarinet.test({ let YTPTest = new YTPTestAgent1(chain, deployer); //if non-deployer attempts to create a pool, throw an error. - let result = YTPTest.createPool(wallet_1, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(wallet_1, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectErr().expectUint(1000); //Deployer creating a pool, initial tokens injected to the pool - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); //if wrong pool token is supplied, then throw an error - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, wrongPooltokenAddress, 10*ONE_8); + result = YTPTest.addToPosition(deployer, wrongExpiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); result.expectErr().expectUint(2023); // non-deployer can add liquidity - result = YTPTest.addToPosition(wallet_1, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 10*ONE_8); + result = YTPTest.addToPosition(wallet_1, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); result.expectOk(); //if wrong pool token is supplied, throw an error - result = YTPTest.reducePosition(deployer, yieldwbtc59760Address, wbtcAddress, wrongPooltokenAddress, ONE_8); + result = YTPTest.reducePosition(deployer, wrongExpiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, ONE_8); result.expectErr().expectUint(2023); } @@ -352,45 +342,45 @@ Clarinet.test({ let YTPTest = new YTPTestAgent1(chain, deployer); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print - let call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(1000*ONE_8); - call = await YTPTest.getYield(yieldwbtc59760Address); + call = await YTPTest.getYield(expiry, yieldwbtcAddress); call.result.expectOk().expectUint(5); // if current yield < target yield, then supply of yield-token needs to increase - call = await YTPTest.getXgivenYield(yieldwbtc59760Address, 0.001*ONE_8); + call = await YTPTest.getXgivenYield(expiry, yieldwbtcAddress, 0.001*ONE_8); call.result.expectErr().expectUint(2002); - call = await YTPTest.getYgivenYield(yieldwbtc59760Address, 0.001*ONE_8); + call = await YTPTest.getYgivenYield(expiry, yieldwbtcAddress, 0.001*ONE_8); call.result.expectOk().expectUint(7019668000); - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 7019668000, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 7019668000, 0); position = result.expectOk().expectTuple(); position['dy'].expectUint(7019668000); position['dx'].expectUint(7023140005); - call = await YTPTest.getYield(yieldwbtc59760Address); + call = await YTPTest.getYield(expiry, yieldwbtcAddress); call.result.expectOk().expectUint(100006); // now let's try to reduce the yield - call = await YTPTest.getYgivenYield(yieldwbtc59760Address, 0.0005*ONE_8); + call = await YTPTest.getYgivenYield(expiry, yieldwbtcAddress, 0.0005*ONE_8); call.result.expectErr().expectUint(2002); - call = await YTPTest.getXgivenYield(yieldwbtc59760Address, 0.0005*ONE_8); + call = await YTPTest.getXgivenYield(expiry, yieldwbtcAddress, 0.0005*ONE_8); call.result.expectOk().expectUint(3504368516); - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 3504368516, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 3504368516, 0); position = result.expectOk().expectTuple(); position['dy'].expectUint(3507017732); position['dx'].expectUint(3504368516); - call = await YTPTest.getYield(yieldwbtc59760Address); + call = await YTPTest.getYield(expiry, yieldwbtcAddress); call.result.expectOk().expectUint(50003); }, @@ -418,29 +408,29 @@ Clarinet.test({ money.expectOk() //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print - let call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(1000*ONE_8); - result = YTPTest.addToPosition(wallet_2, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 10*ONE_8); + result = YTPTest.addToPosition(wallet_2, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(10*ONE_8); position['balance-token'].expectUint(10*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(10*ONE_8); - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(99972419); position['dy'].expectUint(ONE_8); - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(100900027581); // u99900045311 position['balance-yield-token'].expectUint(ONE_8); @@ -464,11 +454,11 @@ Clarinet.test({ ROresult.result.expectOk().expectUint(100000000000); // Wallet_2 votes his 90% asset - result = MultiSigTest.voteFor(wallet_2, ytpyieldwbtc59760Address, 1, 1000000000 * 9 / 10 ) + result = MultiSigTest.voteFor(wallet_2, ytpyieldwbtcAddress, 1, 1000000000 * 9 / 10 ) result.expectOk().expectUint(900000000) // 90 % of existing tokens are voted for the proposal - result = MultiSigTest.voteFor(deployer, ytpyieldwbtc59760Address, 1, 100000000000 * 9 / 10 ) + result = MultiSigTest.voteFor(deployer, ytpyieldwbtcAddress, 1, 100000000000 * 9 / 10 ) result.expectOk().expectUint(90000000000) chain.mineEmptyBlock(1440); @@ -478,11 +468,11 @@ Clarinet.test({ result.expectOk().expectBool(true) // Success // deployer (Contract owner) sets rebate rate - result = YTPTest.setFeeRebate(contractOwner, yieldwbtc59760Address, feeRebate); + result = YTPTest.setFeeRebate(contractOwner, expiry, yieldwbtcAddress, feeRebate); result.expectOk().expectBool(true) // Fee checking - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['balance-yield-token'].expectUint(100000000); position['balance-token'].expectUint(100900027581); @@ -491,7 +481,7 @@ Clarinet.test({ position['fee-rate-token'].expectUint(0.1*ONE_8); position['fee-rebate'].expectUint(0.5*ONE_8); - call = await YTPTest.getYield(yieldwbtc59760Address); + call = await YTPTest.getYield(expiry, yieldwbtcAddress); call.result.expectOk().expectUint(1355); // fee-yield = yield * fee-rate-token = 1355 * 0.1*ONE_8 = 135 (round-down) // (contract-call? .math-fixed-point mul-down 1335 u10000000) @@ -501,12 +491,12 @@ Clarinet.test({ // fee-rebate = 270 * 0.5 = 135 // sell some yield-token - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 2*ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 2*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(199967096); position['dy'].expectUint(199999730); - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['balance-yield-token'].expectUint(100000000 + 199999730 + 135); //before + after + fee-rebate position['balance-token'].expectUint(100900027581 - 199967096); @@ -532,42 +522,42 @@ Clarinet.test({ money.expectOk() //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Duplicated Pool - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectErr().expectUint(2000); // Check pool details and print - let call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(1000*ONE_8); // Attempts to inject zero liquidity - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 0); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0); position = result.expectErr().expectUint(2003) //Attempt to add extra liquidity but not enough balance - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 1000000*ONE_8); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000000*ONE_8); position = result.expectErr().expectUint(3001) // Attempts for trivial reducing - result = YTPTest.reducePosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 0); + result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0); position =result.expectErr().expectUint(1) // Attempts for trivial reduce more than 100% - result = YTPTest.reducePosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 101*ONE_8); + result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 101*ONE_8); position =result.expectErr().expectUint(5000) // Another user attempts to reduce liquidity with not enough pool token - result = YTPTest.reducePosition(wallet_2, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 1*ONE_8); + result = YTPTest.reducePosition(wallet_2, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1*ONE_8); position =result.expectErr().expectUint(1) // Deployer adds liquidity - result = YTPTest.addToPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 1000*ONE_8); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(1000*ONE_8); position['balance-token'].expectUint(1000*ONE_8); @@ -575,7 +565,7 @@ Clarinet.test({ position['balance-virtual'].expectUint(1000*ONE_8); // Another User adds liquidity - result = YTPTest.addToPosition(wallet_2, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 10*ONE_8); + result = YTPTest.addToPosition(wallet_2, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(10*ONE_8); position['balance-token'].expectUint(10*ONE_8); @@ -583,39 +573,39 @@ Clarinet.test({ position['balance-virtual'].expectUint(10*ONE_8); // Another user attempts to reduce liquidity with zero value - result = YTPTest.reducePosition(wallet_2, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 0); + result = YTPTest.reducePosition(wallet_2, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0); position =result.expectErr().expectUint(1) // False swap value -- Filter added - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 0, 0); position =result.expectErr().expectUint(2003) // Too small => < max-slippage - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0.0000001 * ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 0.0000001 * ONE_8, 0); position =result.expectErr().expectUint(2020); // Fixed - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 0.001 * ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 0.001 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(36251); position['dy'].expectUint(0.001 * ONE_8); // Attempt for Swapping - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(99944571); position['dy'].expectUint(ONE_8); // Attempts for zero value swapping - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 0, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, 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, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 100*ONE_8, 0); position =result.expectErr().expectUint(2016) // Swap - result = YTPTest.swapXForY(deployer, yieldwbtc59760Address, wbtcAddress, 0.1 * ONE_8, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 0.1 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(0.1 * ONE_8); position['dy'].expectUint(10043969); @@ -632,24 +622,24 @@ Clarinet.test({ let YTPTest = new YTPTestAgent1(chain, deployer); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print - let call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(1000*ONE_8); // inject some yield-token to pool - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 10 * ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 10 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(1000039937); position['dy'].expectUint(10 * ONE_8); // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8); position['balance-token'].expectUint(1000*ONE_8 - 1000039937); @@ -657,13 +647,13 @@ Clarinet.test({ position['balance-virtual'].expectUint(1000*ONE_8); // make sure wallet_1 does not have any yield-token - call = chain.callReadOnlyFn(yieldwbtc59760Address, "get-balance", - [types.principal(wallet_1.address) + call = chain.callReadOnlyFn(yieldwbtcAddress, "get-balance", + [types.uint(expiry), types.principal(wallet_1.address) ], wallet_1.address); call.result.expectOk().expectUint(0); //Add extra liquidity with secondary buying of yield-token - result = YTPTest.buyAndAddToPosition(wallet_1, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 10*ONE_8); + result = YTPTest.buyAndAddToPosition(wallet_1, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); position = result.expectOk().expectTuple(); position['supply'].expectUint(909189000); position['balance-token'].expectUint(900997112); @@ -681,24 +671,24 @@ Clarinet.test({ let YTPTest = new YTPTestAgent1(chain, deployer); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, 1000*ONE_8, 1000*ONE_8); + let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print - let call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(1000*ONE_8); // inject some yield-token to pool - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 10 * ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 10 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(1000039937); position['dy'].expectUint(10 * ONE_8); // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc59760Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8); position['balance-token'].expectUint(1000*ONE_8 - 1000039937); @@ -706,21 +696,21 @@ Clarinet.test({ position['balance-virtual'].expectUint(1000*ONE_8); // make sure wallet_1 does not have any yield-token - call = chain.callReadOnlyFn(yieldwbtc59760Address, "get-balance", - [types.principal(wallet_1.address) + call = chain.callReadOnlyFn(yieldwbtcAddress, "get-balance", + [types.uint(expiry), types.principal(wallet_1.address) ], wallet_1.address); call.result.expectOk().expectUint(0); // create another ytp - result = YTPTest.createPool(deployer, yieldwbtc80875Address, wbtcAddress, ytpyieldwbtc80875Address, multisigytpyieldwbtc80875, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, anotherExpiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // inject some yield-token to pool - result = YTPTest.swapYForX(deployer, yieldwbtc80875Address, wbtcAddress, 10 * ONE_8, 0); + result = YTPTest.swapYForX(deployer, anotherExpiry, yieldwbtcAddress, wbtcAddress, 10 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(1000067035); position['dy'].expectUint(10 * ONE_8); - call = await YTPTest.getPoolDetails(yieldwbtc80875Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8); position['balance-token'].expectUint(1000*ONE_8 - 1000067035); @@ -728,7 +718,7 @@ Clarinet.test({ position['balance-virtual'].expectUint(1000*ONE_8); //Add extra liquidity with secondary buying of yield-token - result = YTPTest.rollPosition(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, 0.5*ONE_8, yieldwbtc80875Address, ytpyieldwbtc80875Address); + result = YTPTest.rollPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0.5*ONE_8, anotherExpiry); position = result.expectOk().expectTuple(); position['supply'].expectUint(50354658000); position['balance-token'].expectUint(49900929799); @@ -736,7 +726,7 @@ Clarinet.test({ position['balance-virtual'].expectUint(50354658105); // Check pool details and print - call = await YTPTest.getPoolDetails(yieldwbtc80875Address); + call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8 + 50354658000); // a bit more than 148900862764 = 1000*ONE_8 - 1000067035 + 49900929799, due to buy-and-add-to-position diff --git a/init-js-tool/index.js b/init-js-tool/index.js index c3a4b448..94581298 100644 --- a/init-js-tool/index.js +++ b/init-js-tool/index.js @@ -1,16 +1,6 @@ require('dotenv').config(); const { ClarityType, getNonce } = require('@stacks/transactions'); const { initCoinPrice, setOpenOracle, getOpenOracle } = require('./oracles').default -const { - flExecuteMarginUsdaWbtc23670, - flExecuteMarginUsdaWbtc59760, - flExecuteMarginUsdaWbtc23040, - flExecuteMarginUsdaWbtc34560, - flExecuteMarginUsdaWbtc74880, - flExecuteMarginWbtcUsda23040, - flExecuteMarginWbtcUsda34560, - flExecuteMarginWbtcUsda74880, -} = require('./flashloan') const { flashloan, getBalance, mint, burn, balance, transfer } = require('./vault') const { setUsdaAmount, setWbtcAmount, setStxAmount, getSomeTokens, setAlexAmount } = require('./faucet') const { @@ -24,6 +14,7 @@ const { fwpSwapXforY, fwpSwapYforX, fwpGetPoolDetails, + fwpGetPositionGivenBurn } = require('./pools-fwp') const { crpCreate, @@ -820,18 +811,21 @@ async function run() { // await arbitrage_fwp(dry_run = false); // await mint_some_wbtc('ST32AK70FP7VNAD68KVDQF3K8XSFG99WKVEHVAPFA'); // await see_balance(process.env.USER_ACCOUNT_ADDRESS); - // result = await ytpGetPositionGivenBurn('yield-wbtc-200335', 625000000000, deployer=true); - // console.log(result); + + result = await fwpGetPositionGivenBurn('token-wbtc', 'token-usda', 0.5e8, 0.5e8, 325.48 * 1e3 * 1e8); + printResult(result); + result = await ytpGetPositionGivenBurn('yield-wbtc-92160', 0.5 * 1e8); + printResult(result); // result = await balance('key-usda-11520-wbtc', process.env.USER_ACCOUNT_ADDRESS); // console.log(result); // await transfer('key-usda-11520-wbtc', 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', 10668690600000); - _list = ['fwp-wbtc-usda-50-50', 'ytp-yield-wbtc-92160-wbtc', 'ytp-yield-usda-92160-usda'] - for (let i = 0; i < _list.length; i++){ - // result = await balance(_list[i], process.env.DEPLOYER_ACCOUNT_ADDRESS); - // console.log(result); - await transfer(_list[i], 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', ONE_8, deployer=true); - } + // _list = ['fwp-wbtc-usda-50-50', 'ytp-yield-wbtc-92160-wbtc', 'ytp-yield-usda-92160-usda'] + // for (let i = 0; i < _list.length; i++){ + // // result = await balance(_list[i], process.env.DEPLOYER_ACCOUNT_ADDRESS); + // // console.log(result); + // await transfer(_list[i], 'STCTK0C1JAFK3JVM95TFV6EB16579WRCEYN10CTQ', ONE_8, deployer=true); + // } } run(); diff --git a/init-js-tool/pools-fwp.js b/init-js-tool/pools-fwp.js index 6d9bc9f5..0f28b690 100644 --- a/init-js-tool/pools-fwp.js +++ b/init-js-tool/pools-fwp.js @@ -295,6 +295,32 @@ const fwpGetYGivenPrice = async (tokenX, tokenY, weightX, weightY, price) => { } }; +const fwpGetPositionGivenBurn = async (tokenX, tokenY, weightX, weightY, token) => { + console.log('--------------------------------------------------------------------------'); + console.log('[FWP] get-position-given-burn...', tokenX, tokenY, weightX, weightY, token); + + const options = { + contractAddress: process.env.DEPLOYER_ACCOUNT_ADDRESS, + contractName: 'fixed-weight-pool', + functionName: 'get-position-given-burn', + functionArgs: [ + contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, tokenX), + contractPrincipalCV(process.env.DEPLOYER_ACCOUNT_ADDRESS, tokenY), + uintCV(weightX), + uintCV(weightY), + uintCV(token) + ], + network: network, + senderAddress: process.env.USER_ACCOUNT_ADDRESS, + }; + try { + return callReadOnlyFunction(options); + + } catch (error) { + console.log(error); + } +}; + const fwpGetPoolDetails = async (tokenX, tokenY, weightX, weightY) => { console.log('--------------------------------------------------------------------------'); console.log('[FWP] get-pool-details...]', tokenX, tokenY, weightX, weightY); @@ -329,3 +355,4 @@ exports.fwpGetYgivenX = fwpGetYgivenX; exports.fwpGetXgivenY = fwpGetXgivenY; exports.fwpSwapXforY = fwpSwapXforY; exports.fwpSwapYforX = fwpSwapYforX; +exports.fwpGetPositionGivenBurn = fwpGetPositionGivenBurn; From 90b800391fb91458abc27d002ace8100cbc418de Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sat, 13 Nov 2021 23:39:54 +0800 Subject: [PATCH 07/25] working --- .../multisig/multisig-ytp-yield-wbtc.clar | 281 ++++ .../contracts/pool-token/ytp-yield-wbtc.clar | 167 ++ clarity/contracts/yield-token/yield-wbtc.clar | 167 ++ clarity/out | 1381 +++++++++++++++++ 4 files changed, 1996 insertions(+) create mode 100644 clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar create mode 100644 clarity/contracts/pool-token/ytp-yield-wbtc.clar create mode 100644 clarity/contracts/yield-token/yield-wbtc.clar create mode 100644 clarity/out diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar new file mode 100644 index 00000000..e615d34b --- /dev/null +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar @@ -0,0 +1,281 @@ +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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 aywbtc-wbtc 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, + expiry: uint, + 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-yield-token: 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, expiry: uint } { 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 ) (expiry uint)) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + expiry: u0, + 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-yield-token: u0 ;; default yield-token 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) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (expiry uint) + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-yield-token uint) + ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance expiry tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) 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, + expiry: expiry, + 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-yield-token: new-fee-rate-yield-token + } + ) + (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)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) 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 + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token expiry)) ONE_8)) + ) + + (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 expiry token-count (as-contract tx-sender) member))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-wbtc new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-wbtc new-fee-rate-yield-token)) + + (ok true) + ) +) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar new file mode 100644 index 00000000..ce3076cb --- /dev/null +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -0,0 +1,167 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + +(define-fungible-token ytp-yield-wbtc) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal .yield-token-pool) + +(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)) + ) +) + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance ytp-yield-wbtc who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply ytp-yield-wbtc)) +) + +(define-read-only (get-decimals (token-id uint)) + (ok u0) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? ytp-yield-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) + (begin + (try! (transfer token-id amount sender recipient)) + (print memo) + (ok true) + ) +) + +(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) +) + +(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) + (fold transfer-many-iter transfers (ok true)) +) + +(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) +) + +(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) + (fold transfer-many-memo-iter transfers (ok true)) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (ft-mint? ytp-yield-wbtc amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (ft-burn? ytp-yield-wbtc amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply ytp-yield-wbtc))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) +) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar new file mode 100644 index 00000000..60c82332 --- /dev/null +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -0,0 +1,167 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + +(define-fungible-token yield-wbtc) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal .collateral-rebalancing-pool) + +(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)) + ) +) + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance yield-wbtc who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply yield-wbtc)) +) + +(define-read-only (get-decimals (token-id uint)) + (ok u0) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? yield-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) + (begin + (try! (transfer token-id amount sender recipient)) + (print memo) + (ok true) + ) +) + +(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) +) + +(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) + (fold transfer-many-iter transfers (ok true)) +) + +(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) + (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) +) + +(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) + (fold transfer-many-memo-iter transfers (ok true)) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (ft-mint? yield-wbtc amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (ft-burn? yield-wbtc amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply yield-wbtc))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance yield-wbtc who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) +) \ No newline at end of file diff --git a/clarity/out b/clarity/out new file mode 100644 index 00000000..37aa1385 --- /dev/null +++ b/clarity/out @@ -0,0 +1,1381 @@ + +Error: Analysis error: invalid signature for method 'burn' regarding trait's specification +Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification +Analysis error: invalid signature for method 'burn' regarding trait's specification +Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda' +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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, + expiry: uint, + 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, expiry: uint } { 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 ) (expiry uint)) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + expiry: u0, + 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 yield-token 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) (is-eq (contract-of token) .key-usda-wbtc)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (expiry uint) + (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 get-balance expiry tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance expiry tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) + (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, + expiry: expiry, + 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)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + + ;; 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) , expiry: expiry } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) + (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 + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) + ) + + (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 expiry token-count (as-contract tx-sender) member))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc expiry new-fee-rate-y)) + + (ok true) + ) +) +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fwp-wbtc-usda-50-50' +(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) .fwp-wbtc-usda-50-50) +) + + +;; 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) + ) + ([1;4;31mlet ( + (proposer-balance (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-balance tx-sender))) + (total-supply (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 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: 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-supply (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-total-supply))) + (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)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .fixed-weight-pool set-fee-rate-x .token-wbtc .token-usda u50000000 u50000000 new-fee-rate-x)) + (try! (contract-call? .fixed-weight-pool set-fee-rate-y .token-wbtc .token-usda u50000000 u50000000 new-fee-rate-y)) + + (ok true) + ) +) +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.lbp-alex-usda-90-10' +(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) + ) + ([1;4;31mlet ( + (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 (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) + ) +) +Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda' +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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 aywbtc-wbtc 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, + expiry: uint, + 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-yield-token: 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, expiry: uint } { 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 ) (expiry uint)) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + expiry: u0, + 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-yield-token: u0 ;; default yield-token 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) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (expiry uint) + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-yield-token uint) + ) + ([1;4;31mlet ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda get-balance expiry tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) 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, + expiry: expiry, + 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-yield-token: new-fee-rate-yield-token + } + ) + (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)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) 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 + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token expiry)) ONE_8)) + ) + + (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 expiry token-count (as-contract tx-sender) member))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-usda new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-usda new-fee-rate-yield-token)) + + (ok true) + ) +) +Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification +Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc' +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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 aywbtc-wbtc 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, + expiry: uint, + 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-yield-token: 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, expiry: uint } { 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 ) (expiry uint)) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + expiry: u0, + 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-yield-token: u0 ;; default yield-token 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) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (expiry uint) + (start-block-height uint) + (title (string-utf8 256)) + (url (string-utf8 256)) + (new-fee-rate-token uint) + (new-fee-rate-yield-token uint) + ) + ([1;4;31mlet ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance expiry tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) 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, + expiry: expiry, + 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-yield-token: new-fee-rate-yield-token + } + ) + (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)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + + + + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + ;; 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 expiry amount tx-sender (as-contract tx-sender))) + + ;; 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), expiry: expiry } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) 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 + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token expiry)) ONE_8)) + ) + + (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 expiry token-count (as-contract tx-sender) member))) + (ok true) + ) +) + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) + ) + + ;; Setting for Yield Token Pool + (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-wbtc new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-wbtc new-fee-rate-yield-token)) + + (ok true) + ) +) From b41b1d65ef622ff122742d69924ff21c10c03cc7 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sun, 14 Nov 2021 22:38:06 +0800 Subject: [PATCH 08/25] working --- .../contracts/key-token/key-usda-wbtc.clar | 49 +- .../multisig/multisig-crp-usda-wbtc.clar | 6 +- .../multisig/multisig-ytp-yield-usda.clar | 6 +- .../multisig/multisig-ytp-yield-wbtc.clar | 6 +- .../pool-token/fwp-wbtc-usda-50-50.clar | 91 +- .../pool-token/lbp-alex-usda-90-10.clar | 91 +- .../contracts/pool-token/ytp-yield-usda.clar | 49 +- .../contracts/pool-token/ytp-yield-wbtc.clar | 49 +- clarity/contracts/pool/yield-token-pool.clar | 4 +- .../traits/trait-semi-fungible-token.clar | 25 +- clarity/contracts/yield-token/yield-usda.clar | 49 +- clarity/contracts/yield-token/yield-wbtc.clar | 49 +- clarity/out | 3152 +++++++++++++---- clarity/tests/models/alex-tests-multisigs.ts | 30 +- clarity/tests/models/alex-tests-tokens.ts | 61 +- .../models/alex-tests-yield-token-pool.ts | 2 +- clarity/tests/yield-token-pool_test.ts | 26 +- 17 files changed, 2840 insertions(+), 905 deletions(-) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index e45340f2..335516b1 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -10,7 +10,8 @@ (define-map token-supplies uint uint) (define-map token-owned principal (list 2000 uint)) -(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) (define-read-only (get-owner) (ok (var-get contract-owner)) @@ -23,6 +24,10 @@ ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -55,7 +60,7 @@ (ok (ft-get-supply key-usda-wbtc)) ) -(define-read-only (get-decimals (token-id uint)) +(define-read-only (get-decimals) (ok u0) ) @@ -63,7 +68,7 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -73,38 +78,14 @@ (try! (ft-transfer? key-usda-wbtc amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) - (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) (ok true) ) ) -(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) - (begin - (try! (transfer token-id amount sender recipient)) - (print memo) - (ok true) - ) -) - -(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) -) - -(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) - (fold transfer-many-iter transfers (ok true)) -) - -(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) -) - -(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) - (fold transfer-many-memo-iter transfers (ok true)) -) - (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-mint? key-usda-wbtc amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +96,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-burn? key-usda-wbtc amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) @@ -154,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance key-usda-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (transfer token-id (fixed-to-decimals amount) sender recipient memo) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) @@ -164,4 +145,8 @@ (define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .collateral-rebalancing-pool true) ) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar index d19d1818..2ea49eff 100644 --- a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar @@ -172,7 +172,7 @@ (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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -203,7 +203,7 @@ ;; 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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -261,7 +261,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) + (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index c210b93c..d1b10c76 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -168,7 +168,7 @@ (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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -202,7 +202,7 @@ ;; 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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -258,7 +258,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) + (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar index e615d34b..ad14884d 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar @@ -168,7 +168,7 @@ (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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -202,7 +202,7 @@ ;; 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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -258,7 +258,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) + (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) diff --git a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar index 1cb30e27..11082d63 100644 --- a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar @@ -4,42 +4,33 @@ (define-fungible-token fwp-wbtc-usda-50-50) (define-data-var token-uri (string-utf8 256) u"") -(define-data-var contract-owner principal .fixed-weight-pool) +(define-data-var CONTRACT-OWNER principal tx-sender) +(define-map approved-contracts principal bool) ;; 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)) + (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)) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + (ok (var-set CONTRACT-OWNER owner)) ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get CONTRACT-OWNER))) ERR-NOT-AUTHORIZED)) +) + ;; --------------------------------------------------------- ;; SIP-10 Functions ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply fwp-wbtc-usda-50-50))) + (ok (ft-get-supply fwp-wbtc-usda-50-50)) ) (define-read-only (get-name) @@ -51,16 +42,16 @@ ) (define-read-only (get-decimals) - (ok u8) + (ok u0) ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance fwp-wbtc-usda-50-50 account))) + (ok (ft-get-balance fwp-wbtc-usda-50-50 account)) ) (define-public (set-token-uri (value (string-utf8 256))) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) (ok (var-set token-uri value)) ) ) @@ -72,7 +63,7 @@ (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? fwp-wbtc-usda-50-50 (fixed-to-decimals amount) sender recipient) + (match (ft-transfer? fwp-wbtc-usda-50-50 amount sender recipient) response (begin (print memo) (ok response) @@ -82,16 +73,56 @@ ) ) -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-mint? fwp-wbtc-usda-50-50 (fixed-to-decimals amount) recipient) + (try! (check-is-approved contract-caller)) + (ft-mint? fwp-wbtc-usda-50-50 amount recipient) ) ) -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) - (ft-burn? fwp-wbtc-usda-50-50 (fixed-to-decimals amount) sender) + (try! (check-is-approved contract-caller)) + (ft-burn? fwp-wbtc-usda-50-50 amount sender) ) -) \ No newline at end of file +) + +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply fwp-wbtc-usda-50-50))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance fwp-wbtc-usda-50-50 account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .fixed-weight-pool 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 index 5e4317b6..b45b4db1 100644 --- a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar +++ b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar @@ -4,42 +4,33 @@ (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) +(define-data-var CONTRACT-OWNER principal tx-sender) +(define-map approved-contracts principal bool) ;; 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)) + (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)) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + (ok (var-set CONTRACT-OWNER owner)) ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get CONTRACT-OWNER))) ERR-NOT-AUTHORIZED)) +) + ;; --------------------------------------------------------- ;; SIP-10 Functions ;; --------------------------------------------------------- (define-read-only (get-total-supply) - (ok (decimals-to-fixed (ft-get-supply lbp-alex-usda-90-10))) + (ok (ft-get-supply lbp-alex-usda-90-10)) ) (define-read-only (get-name) @@ -51,16 +42,16 @@ ) (define-read-only (get-decimals) - (ok u8) + (ok u0) ) (define-read-only (get-balance (account principal)) - (ok (decimals-to-fixed (ft-get-balance lbp-alex-usda-90-10 account))) + (ok (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) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) (ok (var-set token-uri value)) ) ) @@ -72,7 +63,7 @@ (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) + (match (ft-transfer? lbp-alex-usda-90-10 amount sender recipient) response (begin (print memo) (ok response) @@ -82,16 +73,56 @@ ) ) -(define-public (mint (recipient principal) (amount uint)) +(define-public (mint (amount uint) (recipient principal)) (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) + (try! (check-is-approved contract-caller)) + (ft-mint? lbp-alex-usda-90-10 amount recipient) ) ) -(define-public (burn (sender principal) (amount uint)) +(define-public (burn (amount uint) (sender principal)) (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) + (try! (check-is-approved contract-caller)) + (ft-burn? lbp-alex-usda-90-10 amount sender) ) -) \ No newline at end of file +) + +(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-total-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply lbp-alex-usda-90-10))) +) + +(define-read-only (get-balance-fixed (account principal)) + (ok (decimals-to-fixed (ft-get-balance lbp-alex-usda-90-10 account))) +) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .liquidity-bootstrapping-pool true) +) + + diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index 302ddeb6..a43e0e18 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -10,7 +10,8 @@ (define-map token-supplies uint uint) (define-map token-owned principal (list 2000 uint)) -(define-data-var contract-owner principal .yield-token-pool) +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) (define-read-only (get-owner) (ok (var-get contract-owner)) @@ -23,6 +24,10 @@ ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -55,7 +60,7 @@ (ok (ft-get-supply ytp-yield-usda)) ) -(define-read-only (get-decimals (token-id uint)) +(define-read-only (get-decimals) (ok u0) ) @@ -63,7 +68,7 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -73,38 +78,14 @@ (try! (ft-transfer? ytp-yield-usda amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) - (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) (ok true) ) ) -(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) - (begin - (try! (transfer token-id amount sender recipient)) - (print memo) - (ok true) - ) -) - -(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) -) - -(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) - (fold transfer-many-iter transfers (ok true)) -) - -(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) -) - -(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) - (fold transfer-many-memo-iter transfers (ok true)) -) - (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-mint? ytp-yield-usda amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +96,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-burn? ytp-yield-usda amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) @@ -154,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (transfer token-id (fixed-to-decimals amount) sender recipient memo) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) @@ -164,4 +145,8 @@ (define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .yield-token-pool true) ) \ No newline at end of file diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar index ce3076cb..b34b7f2b 100644 --- a/clarity/contracts/pool-token/ytp-yield-wbtc.clar +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -10,7 +10,8 @@ (define-map token-supplies uint uint) (define-map token-owned principal (list 2000 uint)) -(define-data-var contract-owner principal .yield-token-pool) +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) (define-read-only (get-owner) (ok (var-get contract-owner)) @@ -23,6 +24,10 @@ ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -55,7 +60,7 @@ (ok (ft-get-supply ytp-yield-wbtc)) ) -(define-read-only (get-decimals (token-id uint)) +(define-read-only (get-decimals) (ok u0) ) @@ -63,7 +68,7 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -73,38 +78,14 @@ (try! (ft-transfer? ytp-yield-wbtc amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) - (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) (ok true) ) ) -(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) - (begin - (try! (transfer token-id amount sender recipient)) - (print memo) - (ok true) - ) -) - -(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) -) - -(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) - (fold transfer-many-iter transfers (ok true)) -) - -(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) -) - -(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) - (fold transfer-many-memo-iter transfers (ok true)) -) - (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-mint? ytp-yield-wbtc amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +96,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-burn? ytp-yield-wbtc amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) @@ -154,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (transfer token-id (fixed-to-decimals amount) sender recipient memo) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) @@ -164,4 +145,8 @@ (define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .yield-token-pool true) ) \ No newline at end of file diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 3f068d0a..a6b98d29 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -367,7 +367,7 @@ ;; send x to vault (unwrap! (contract-call? the-token transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) ;; send y to vault - (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer expiry new-dy-act tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) + (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer expiry new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) ;; mint pool token and send to tx-sender (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) @@ -396,7 +396,7 @@ (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) (total-supply (get total-supply pool)) - (total-shares (unwrap-panic (contract-call? the-pool-token get-balance-fixed expiry tx-sender))) + (total-shares (unwrap! (contract-call? the-pool-token get-balance-fixed expiry tx-sender) (err u11111))) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (reduce-data (try! (get-position-given-burn expiry the-yield-token shares))) (dx (get dx reduce-data)) diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar index edc0d6a8..f4fa0dea 100644 --- a/clarity/contracts/traits/trait-semi-fungible-token.clar +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -1,5 +1,7 @@ (define-trait semi-fungible-token-trait ( + ;; (get-token-owned (principal) (list)) + ;; Get a token type balance of the passed principal. (get-balance (uint principal) (response uint uint)) @@ -13,22 +15,13 @@ (get-overall-supply () (response uint uint)) ;; Get the number of decimal places of a token type. - (get-decimals (uint) (response uint uint)) + (get-decimals () (response uint uint)) ;; Get an optional token URI that represents metadata for a specific token. (get-token-uri (uint) (response (optional (string-utf8 256)) uint)) ;; Transfer from one principal to another. - (transfer (uint uint principal principal) (response bool uint)) - - ;; Transfer from one principal to another with a memo. - (transfer-memo (uint uint principal principal (buff 34)) (response bool uint)) - - ;; Transfer many tokens at once. - (transfer-many ((list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal})) (response bool uint)) - - ;; Transfer many tokens at once with memos. - (transfer-many-memo ((list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)})) (response bool uint)) + (transfer (uint uint principal principal (optional (buff 34))) (response bool uint)) (mint (uint uint principal) (response bool uint)) (burn (uint uint principal) (response bool uint)) @@ -37,10 +30,10 @@ (transfer-fixed (uint uint principal principal) (response bool uint)) (get-balance-fixed (uint principal) (response uint uint)) (get-total-supply-fixed (uint) (response uint uint)) - (get-overall-balance-fixed (uint principal) (response uint uint)) - (get-total-supply-fixed (uint uint) (response uint uint)) - (get-overall-supply (uint) (response uint uint)) - (mint-fixed (uint principal) (response bool uint)) - (burn-fixed (uint principal) (response bool uint)) + (get-total-supply-fixed (uint) (response uint uint)) + (get-overall-balance-fixed (principal) (response uint uint)) + (get-overall-supply-fixed () (response uint uint)) + (mint-fixed (uint uint principal) (response bool uint)) + (burn-fixed (uint uint principal) (response bool uint)) ) ) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index af28ddb6..b5f90d47 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -10,7 +10,8 @@ (define-map token-supplies uint uint) (define-map token-owned principal (list 2000 uint)) -(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) (define-read-only (get-owner) (ok (var-get contract-owner)) @@ -23,6 +24,10 @@ ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -55,7 +60,7 @@ (ok (ft-get-supply yield-usda)) ) -(define-read-only (get-decimals (token-id uint)) +(define-read-only (get-decimals) (ok u0) ) @@ -63,7 +68,7 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -73,38 +78,14 @@ (try! (ft-transfer? yield-usda amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) - (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) (ok true) ) ) -(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) - (begin - (try! (transfer token-id amount sender recipient)) - (print memo) - (ok true) - ) -) - -(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) -) - -(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) - (fold transfer-many-iter transfers (ok true)) -) - -(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) -) - -(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) - (fold transfer-many-memo-iter transfers (ok true)) -) - (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-mint? yield-usda amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +96,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-burn? yield-usda amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) @@ -154,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance yield-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (transfer token-id (fixed-to-decimals amount) sender recipient memo) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) @@ -164,4 +145,8 @@ (define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .collateral-rebalancing-pool true) ) \ No newline at end of file diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar index 60c82332..e33ff8b8 100644 --- a/clarity/contracts/yield-token/yield-wbtc.clar +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -10,7 +10,8 @@ (define-map token-supplies uint uint) (define-map token-owned principal (list 2000 uint)) -(define-data-var contract-owner principal .collateral-rebalancing-pool) +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) (define-read-only (get-owner) (ok (var-get contract-owner)) @@ -23,6 +24,10 @@ ) ) +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + (define-read-only (get-token-owned (owner principal)) (default-to (list) (map-get? token-owned owner)) ) @@ -55,7 +60,7 @@ (ok (ft-get-supply yield-wbtc)) ) -(define-read-only (get-decimals (token-id uint)) +(define-read-only (get-decimals) (ok u0) ) @@ -63,7 +68,7 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -73,38 +78,14 @@ (try! (ft-transfer? yield-wbtc amount sender recipient)) (try! (set-balance token-id (- sender-balance amount) sender)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) - (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) (ok true) ) ) -(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) - (begin - (try! (transfer token-id amount sender recipient)) - (print memo) - (ok true) - ) -) - -(define-private (transfer-many-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer (get token-id item) (get amount item) (get sender item) (get recipient item)) prev-err previous-response) -) - -(define-public (transfer-many (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal}))) - (fold transfer-many-iter transfers (ok true)) -) - -(define-private (transfer-many-memo-iter (item {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}) (previous-response (response bool uint))) - (match previous-response prev-ok (transfer-memo (get token-id item) (get amount item) (get sender item) (get recipient item) (get memo item)) prev-err previous-response) -) - -(define-public (transfer-many-memo (transfers (list 200 {token-id: uint, amount: uint, sender: principal, recipient: principal, memo: (buff 34)}))) - (fold transfer-many-memo-iter transfers (ok true)) -) - (define-public (mint (token-id uint) (amount uint) (recipient principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-mint? yield-wbtc amount recipient)) (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) @@ -115,7 +96,7 @@ (define-public (burn (token-id uint) (amount uint) (sender principal)) (begin - (asserts! (is-eq contract-caller (var-get contract-owner)) ERR-NOT-AUTHORIZED) + (try! (check-is-approved contract-caller)) (try! (ft-burn? yield-wbtc amount sender)) (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) @@ -154,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance yield-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) - (transfer token-id (fixed-to-decimals amount) sender recipient memo) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) @@ -164,4 +145,8 @@ (define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .collateral-rebalancing-pool true) ) \ No newline at end of file diff --git a/clarity/out b/clarity/out index 37aa1385..da31f850 100644 --- a/clarity/out +++ b/clarity/out @@ -1,566 +1,2504 @@ -Error: Analysis error: invalid signature for method 'burn' regarding trait's specification -Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification -Analysis error: invalid signature for method 'burn' regarding trait's specification -Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda' -(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +Error: Analysis error: detected two execution paths, returning two different expression types (got '(response UnknownType uint)' and '(response UnknownType int)') +(impl-trait .trait-ownable.ownable-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) (use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(use-trait multisig-trait .trait-multisig-vote.multisig-vote-sft-trait) + +;; yield-token-pool +(define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places +(define-constant MAX_T u85000000) + +(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 invalid-token-err (err u2007)) +(define-constant ERR-NO-FEE (err u2005)) +(define-constant ERR-NO-FEE-Y (err u2006)) +(define-constant invalid-ERR-EXPIRY (err u2009)) +(define-constant fixed-point-err (err 5014)) +(define-constant ERR-MATH-CALL (err u4003)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) +(define-constant ERR-DY-BIGGER-THAN-AVAILABLE (err u2016)) +(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)) +(define-constant ERR-INVALID-POOL-TOKEN (err u2023)) +(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-data-var CONTRACT-OWNER principal tx-sender) + +(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)) + ) +) -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. +;; data maps and vars +(define-map pools-map + { pool-id: uint } + { + yield-token: principal, ;; yield-token, dy + expiry: uint + } +) -;; 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. -;; +(define-map pools-data-map + { + yield-token: principal, + expiry: uint + } + { + total-supply: uint, + balance-token: uint, ;; dx + balance-yield-token: uint, ;; dy_actual + balance-virtual: uint, ;; dy_virtual + fee-to-address: principal, + pool-token: principal, + fee-rate-token: uint, + fee-rate-yield-token: uint, + fee-rebate: uint, + listed: uint, + oracle-enabled: bool, + oracle-average: uint, + oracle-resilient: uint + } +) -;; 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-data-var pool-count uint u0) +(define-data-var pools-list (list 2000 uint) (list)) -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) +;; 4 years based on 2102400 blocks per year (i.e. 15 secs per block) +(define-data-var max-expiry uint (scale-up u8409600)) -;; 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, - expiry: uint, - 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 - } +;; @desc get-max-expiry +;; @returns uint +(define-read-only (get-max-expiry) + (var-get max-expiry) ) -(define-data-var proposal-count uint u0) -(define-data-var proposal-ids (list 100 uint) (list u0)) -(define-data-var threshold uint u75000000) ;; 75% +;; @desc set-max-expiry +;; @restricted CONTRACT-OWNER +;; @param new-max-expiry; new max-expiry +;; @returns (response bool uint) +(define-public (set-max-expiry (new-max-expiry uint)) + (begin + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + (ok (var-set max-expiry new-max-expiry)) + ) +) -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) +;; @desc get-t +;; @desc get time-to-maturity as a function of max-expiry +;; @param expiry; when contract expiries +;; @param listed; when contract was listed +;; @returns (response uint uint) +(define-read-only (get-t (expiry uint) (listed uint)) + (begin + (asserts! (> (var-get max-expiry) expiry) invalid-ERR-EXPIRY) + (asserts! (> (var-get max-expiry) (* block-height ONE_8)) invalid-ERR-EXPIRY) + (let + ( + (t (div-down + (if (< expiry (* block-height ONE_8)) u0 (- expiry (* block-height ONE_8))) + (- (var-get max-expiry) listed))) + ) + (ok (if (< t MAX_T) t MAX_T)) ;; to avoid numerical error + ) + ) +) -(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, expiry: uint } { amount: uint }) +;; @desc get-pool-count +;; @returns uint +(define-read-only (get-pool-count) + (var-get pool-count) +) -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) +;; @desc get-pool-contracts +;; @param pool-id; pool-id +;; @returns (response (tutple) uint) +(define-read-only (get-pool-contracts (pool-id uint)) + (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) ERR-INVALID-POOL-ERR)) ) -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) +;; @desc get-pools +;; @returns map of get-pool-contracts +(define-read-only (get-pools) + (ok (map get-pool-contracts (var-get pools-list))) ) -;; 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 }) - ) +;; @desc get-pool-details +;; @param the-yield-token; yield-token +;; @returns (response (tuple) uint) +(define-read-only (get-pool-details (expiry uint) (the-yield-token )) + (ok (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) ) -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token ) (expiry uint)) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) - ) +;; @desc get-yield +;; @desc note yield is not annualised +;; @param the-yield-token; yield-token +;; @returns (response uint uint) +(define-read-only (get-yield (expiry uint) (the-yield-token )) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry}) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (balance-token (get balance-token pool)) + (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) + (t-value (try! (get-t expiry listed))) + ) + (contract-call? .yield-token-equation get-yield balance-token balance-yield-token t-value) + ) ) -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - expiry: u0, - 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 yield-token feerate - } - (map-get? proposals { id: proposal-id }) - ) +;; @desc get-price +;; @param the-yield-token; yield-token +;; @returns (response uint uint) +(define-read-only (get-price (expiry uint) (the-yield-token )) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (balance-token (get balance-token pool)) + (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) + (t-value (try! (get-t expiry listed))) + ) + (contract-call? .yield-token-equation get-price balance-token balance-yield-token t-value) + ) ) -;; 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) (is-eq (contract-of token) .key-usda-wbtc)) +;; @desc get-oracle-enabled +;; @param the-yield-token; yield-token +;; @returns (response bool uint) +(define-read-only (get-oracle-enabled (expiry uint) (the-yield-token )) + (ok (get oracle-enabled (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) ) +;; @desc set-oracle-enabled +;; @desc oracle can only be enabled +;; @restricted CONTRACT-OWNER +;; @param the-yield-token; yield-token +;; @returns (response bool uint) +(define-public (set-oracle-enabled (expiry uint) (the-yield-token )) + (let + ( + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) 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 { yield-token: (contract-of the-yield-token), expiry: expiry } pool-updated) + (ok true) + ) +) -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (expiry uint) - (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 get-balance expiry tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance expiry tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) - (total-supply (+ total-yield-supply total-key-supply)) - (proposal-id (+ u1 (var-get proposal-count))) - ) +;; @desc get-oracle-average +;; @desc returns the moving average used to determine oracle price +;; @param the-yield-token; yield-token +;; @returns (response uint uint) +(define-read-only (get-oracle-average (expiry uint) (the-yield-token )) + (ok (get oracle-average (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) +) - ;; 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, - expiry: expiry, - 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) - ) +;; @desc set-oracle-average +;; @restricted CONTRACT-OWNER +;; @param the-yield-token; yield-token +;; @returns (response bool uint) +(define-public (set-oracle-average (expiry uint) (the-yield-token ) (new-oracle-average uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (pool-updated (merge pool { + oracle-average: new-oracle-average, + oracle-resilient: (try! (get-oracle-instant expiry the-yield-token)) + })) + ) + (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 { yield-token: (contract-of the-yield-token), expiry: expiry } pool-updated) + (ok true) + ) ) -(define-public (vote-for (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (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 expiry))) - ) +;; @desc get-oracle-resilient +;; @desc price-oracle that is less up to date but more resilient to manipulation +;; @param the-yield-token; yield-token +;; @returns (response uint uint) +(define-read-only (get-oracle-resilient (expiry uint) (the-yield-token )) + (let + ( + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) + (ok (+ (mul-down (- ONE_8 (get oracle-average pool)) (try! (get-oracle-instant expiry the-yield-token))) + (mul-down (get oracle-average pool) (get oracle-resilient pool)))) + ) +) - ;; 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) +;; @desc get-oracle-instant +;; @desc price-oracle that is more up to date but less resilient to manipulation +;; @param the-yield-token; yield-token +;; @returns (response uint uint) +(define-read-only (get-oracle-instant (expiry uint) (the-yield-token )) + (ok (div-down ONE_8 (try! (get-price expiry the-yield-token)))) +) + +;; @desc create-pool +;; @restricted CONTRACT-OWNER +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param pool-token; pool token representing ownership of the pool +;; @param multisig-vote; DAO used by pool token holers +;; @param dx; amount of token added +;; @param dy; amount of yield-token added +;; @returns (response bool uint) +(define-public (create-pool (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) + (begin + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + ;; ;; create pool only if the correct pair + ;; (asserts! (is-eq (try! (contract-call? the-yield-token get-token)) (contract-of the-token)) ERR-INVALID-POOL-ERR) + (asserts! (is-none (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry })) ERR-POOL-ALREADY-EXISTS) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool-id (+ (var-get pool-count) u1)) + (pool-data { + total-supply: u0, + balance-token: u0, + balance-yield-token: u0, + balance-virtual: u0, + fee-to-address: (contract-of multisig-vote), + pool-token: (contract-of the-pool-token), + fee-rate-yield-token: u0, + fee-rate-token: u0, + fee-rebate: u0, + listed: (* block-height ONE_8), + oracle-enabled: false, + oracle-average: u0, + oracle-resilient: u0 + }) + ) + + (map-set pools-map { pool-id: pool-id } { yield-token: yield-token, expiry: expiry }) + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-data) + + (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) + (var-set pool-count pool-id) + + ;; ;; if yield-token added has a longer expiry than current max-expiry, update max-expiry (to expiry + one block). + ;; (var-set max-expiry (if (< (var-get max-expiry) expiry) (+ expiry ONE_8) (var-get max-expiry))) + (try! (add-to-position expiry the-yield-token the-token the-pool-token dx)) + + (print { object: "pool", action: "created", data: pool-data }) + (ok true) + ) + ) +) + +;; @desc buy-and-add-to-position +;; @desc helper function to buy required yield-token before adding position +;; @desc returns units of pool tokens minted, dx, dy-actual and dy-virtual added +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param pool-token; pool token representing ownership of the pool +;; @param dx; amount of token added (part of which will be used to buy yield-token) +;; @returns (response (tuple uint uint uint uint) uint) +(define-public (buy-and-add-to-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) + (let + ( + (dy-act (get dy-act (try! (get-token-given-position expiry the-yield-token dx)))) + (dx-adjusted (- dx (div-down dx (+ dx (try! (get-x-given-y expiry the-yield-token dy-act)))))) + (dx-to-buy-dy-adjusted (- dx dx-adjusted)) + ) + (and (> dy-act u0) (is-ok (swap-x-for-y expiry the-yield-token the-token dx-to-buy-dy-adjusted none))) + (add-to-position expiry the-yield-token the-token the-pool-token dx-adjusted) + ) +) + +;; @desc add-to-position +;; @desc returns units of pool tokens minted, dx, dy-actual and dy-virtual added +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param pool-token; pool token representing ownership of the pool +;; @param dx; amount of token added +;; @returns (response (tuple uint uint uint uint) uint) +(define-public (add-to-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) + (begin + ;; dx must be greater than zero + (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-token (get balance-token pool)) + (balance-yield-token (get balance-yield-token pool)) + (balance-virtual (get balance-virtual pool)) + (total-supply (get total-supply pool)) + (add-data (try! (get-token-given-position expiry the-yield-token dx))) + (new-supply (get token add-data)) + (new-dy-act (get dy-act add-data)) + (new-dy-vir (get dy-vir add-data)) + (pool-updated (merge pool { + total-supply: (+ new-supply total-supply), + balance-token: (+ balance-token dx), + balance-yield-token: (+ balance-yield-token new-dy-act), + balance-virtual: (+ balance-virtual new-dy-vir) + })) + ) + + (asserts! (is-eq (get pool-token pool) (contract-of the-pool-token)) ERR-INVALID-POOL-TOKEN) + + ;; at least one of dy must be greater than zero + (asserts! (or (> new-dy-act u0) (> new-dy-vir u0)) ERR-INVALID-LIQUIDITY) + ;; send x to vault + (unwrap! (contract-call? the-token transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) + ;; send y to vault + (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer expiry new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + + ;; mint pool token and send to tx-sender + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) + (try! (contract-call? the-pool-token mint expiry new-supply tx-sender)) + (print { object: "pool", action: "liquidity-added", data: pool-updated }) + (ok {supply: new-supply, balance-token: dx, balance-yield-token: new-dy-act, balance-virtual: new-dy-vir}) + ) + ) +) + +;; @desc reduce-position +;; @desc returns dx and dy-actual due to the position +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param pool-token; pool token representing ownership of the pool +;; @param percent; percentage of pool token held to reduce +;; @returns (response (tuple uint uint) uint) +(define-public (reduce-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (percent uint)) + (begin + (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-token (get balance-token pool)) + (balance-yield-token (get balance-yield-token pool)) + (balance-virtual (get balance-virtual pool)) + (total-supply (get total-supply pool)) + (total-shares (unwrap! (contract-call? the-pool-token get-balance-fixed expiry tx-sender) (err 11111))) + (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) + (reduce-data (try! (get-position-given-burn expiry the-yield-token shares))) + (dx (get dx reduce-data)) + (dy-act (get dy-act reduce-data)) + (dy-vir (get dy-vir reduce-data)) + (pool-updated (merge pool { + total-supply: (if (<= total-supply shares) u0 (- total-supply shares)), + balance-token: (if (<= balance-token dx) u0 (- balance-token dx)), + balance-yield-token: (if (<= balance-yield-token dy-act) u0 (- balance-yield-token dy-act)), + balance-virtual: (if (<= balance-virtual dy-vir) u0 (- balance-virtual dy-vir)) + }) + ) + ) + + (asserts! (is-eq (get pool-token pool) (contract-of the-pool-token)) ERR-INVALID-POOL-TOKEN) + + (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) + (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy-act tx-sender))) + + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) + (try! (contract-call? the-pool-token burn expiry shares tx-sender)) + (print { object: "pool", action: "liquidity-removed", data: pool-updated }) + (ok {dx: dx, dy: dy-act}) + ) + ) +) + +;; @desc roll-position +;; @desc roll given liquidity position to another pool +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param pool-token; pool token representing ownership of the pool +;; @param percent; percentage of pool token held to reduce +;; @param the-yield-token-to-roll; yield token to roll +;; @param the-pool-token-to-roll; pool token representing ownership of the pool to roll to +;; @returns (response (tuple uint uint) uint) +(define-public (roll-position + (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (percent uint) + (expiry-to-roll uint)) + (let + ( + (reduce-data (unwrap! (reduce-position expiry the-yield-token the-token the-pool-token percent) (err u11111))) + (dy-to-dx (get dx (unwrap! (swap-y-for-x expiry the-yield-token the-token (get dy reduce-data) none) (err u22222)))) + ) + (buy-and-add-to-position expiry-to-roll the-yield-token the-token the-pool-token (+ (get dx reduce-data) dy-to-dx)) + ) +) + +;; @desc swap-x-for-y +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param dx; amount of token to swap +;; @param min-dy; optional, min amount of yield-token to receive +;; @returns (response (tuple uint uint) uint) +(define-public (swap-x-for-y (expiry uint) (the-yield-token ) (the-token ) (dx uint) (min-dy (optional uint))) + (begin + (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-token (get balance-token pool)) + (balance-yield-token (get balance-yield-token pool)) + + ;; lambda ~= 1 - fee-rate-yield-token * yield + (yield (try! (get-yield expiry the-yield-token))) + (fee-yield (mul-down yield (get fee-rate-yield-token pool))) + (lambda (if (<= ONE_8 fee-yield) u0 (- ONE_8 fee-yield))) + (dx-net-fees (mul-down dx lambda)) + (fee (if (<= dx dx-net-fees) u0 (- dx dx-net-fees))) + (fee-rebate (mul-down fee (get fee-rebate pool))) + + (dy (try! (get-y-given-x expiry the-yield-token dx-net-fees))) + + (pool-updated + (merge pool + { + balance-token: (+ balance-token dx-net-fees fee-rebate), + balance-yield-token: (if (<= balance-yield-token dy) u0 (- balance-yield-token dy)), + oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient expiry the-yield-token)) u0) + } + ) + ) + ) + + (asserts! (< (default-to u0 min-dy) dy) ERR-EXCEEDS-MAX-SLIPPAGE) + + (and (> dx u0) (unwrap! (contract-call? the-token transfer-fixed dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED)) + (and (> dy u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy tx-sender))) + (try! (contract-call? .alex-reserve-pool add-to-balance (contract-of the-token) (- fee fee-rebate))) + + ;; post setting + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) + (print { object: "pool", action: "swap-x-for-y", data: pool-updated }) + (ok {dx: dx-net-fees, dy: dy}) + ) + ) +) + +;; @desc swap-y-for-x +;; @param the-yield-token; yield token +;; @param the-token; token +;; @param dy; amount of yield token to swap +;; @param min-dx; optional, min amount of token to receive +;; @returns (response (tuple uint uint) uint) +(define-public (swap-y-for-x (expiry uint) (the-yield-token ) (the-token ) (dy uint) (min-dx (optional uint))) + (begin + (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-token (get balance-token pool)) + (balance-yield-token (get balance-yield-token pool)) + + ;; lambda ~= 1 - fee-rate-token * yield + (yield (try! (get-yield expiry the-yield-token))) + (fee-yield (mul-down yield (get fee-rate-token pool))) + (lambda (if (<= ONE_8 fee-yield) u0 (- ONE_8 fee-yield))) + (dy-net-fees (mul-down dy lambda)) + (fee (if (<= dy dy-net-fees) u0 (- dy dy-net-fees))) + (fee-rebate (mul-down fee (get fee-rebate pool))) + + (dx (try! (get-x-given-y expiry the-yield-token dy-net-fees))) + + (pool-updated + (merge pool + { + balance-token: (if (<= balance-token dx) u0 (- balance-token dx)), + balance-yield-token: (+ balance-yield-token dy-net-fees fee-rebate), + oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient expiry the-yield-token)) u0) + } + ) + ) + ) + (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) + + (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) + (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) + (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) + + ;; post setting + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) + (print { object: "pool", action: "swap-y-for-x", data: pool-updated }) + (ok {dx: dx, dy: dy-net-fees}) + ) + ) +) + +;; @desc get-fee-rebate +;; @param the-yield-token; yield token +;; @returns (response uint uint) +(define-read-only (get-fee-rebate (expiry uint) (the-yield-token )) + (ok (get fee-rebate (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) +) + +;; @desc set-fee-rebate +;; @restricted CONTRACT-OWNER +;; @param the-yield-token; yield token +;; @param fee-rebate; new fee-rebate +;; @returns (response bool uint) +(define-public (set-fee-rebate (expiry uint) (the-yield-token ) (fee-rebate uint)) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rebate: fee-rebate })) + (ok true) + ) +) + +;; @desc get-fee-rate-yield-token +;; @param the-yield-token; yield token +;; @returns (response uint uint) +(define-read-only (get-fee-rate-yield-token (expiry uint) (the-yield-token )) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (ok (get fee-rate-yield-token pool)) + ) +) + +;; @desc get-fee-rate-token +;; @param the-yield-token; yield token +;; @returns (response uint uint) +(define-read-only (get-fee-rate-token (expiry uint) (the-yield-token )) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (ok (get fee-rate-token pool)) + ) +) + +;; @desc set-fee-rate-yield-token +;; @restricted fee-to-address +;; @param the-yield-token; yield token +;; @param fee-rate-yield-token; new fee-rate-yield-token +;; @returns (response bool uint) +(define-public (set-fee-rate-yield-token (expiry uint) (the-yield-token ) (fee-rate-yield-token uint)) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) + + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rate-yield-token: fee-rate-yield-token })) + (ok true) - ;; Voter should stake the corresponding pool token to the vote contract. - (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender))) - ;; 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), expiry: expiry } - { amount: (+ amount token-count)}) + ) +) - (ok amount) +;; @desc set-fee-rate-token +;; @restricted fee-to-address +;; @param the-yield-token; yield token +;; @param fee-rate-token; new fee-rate-token +;; @returns (response bool uint) +(define-public (set-fee-rate-token (expiry uint) (the-yield-token ) (fee-rate-token uint)) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) + + (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rate-token: fee-rate-token })) + (ok true) + ) +) + +;; @desc get-fee-to-address +;; @param the-yield-token; yield token +;; @returns (response principal uint) +(define-read-only (get-fee-to-address (expiry uint) (the-yield-token )) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + ) + (ok (get fee-to-address pool)) + ) +) + +;; @desc units of yield token given units of token +;; @param the-yield-token; yield token +;; @param dx; amount of token being added +;; @returns (response uint uint) +(define-read-only (get-y-given-x (expiry uint) (the-yield-token ) (dx uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (normalized-expiry (try! (get-t expiry (get listed pool)))) + (dy (try! (contract-call? .yield-token-equation get-y-given-x (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dx))) + ) + (asserts! (> (get balance-yield-token pool) dy) ERR-DY-BIGGER-THAN-AVAILABLE) + (ok dy) + ) +) + +;; @desc units of token given units of yield token +;; @param the-yield-token; yield token +;; @param dy; amount of yield token being added +;; @returns (response uint uint) +(define-read-only (get-x-given-y (expiry uint) (the-yield-token ) (dy uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (normalized-expiry (try! (get-t expiry (get listed pool)))) + ) + (contract-call? .yield-token-equation get-x-given-y (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dy) ) - ) +) -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - ;; 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 expiry amount tx-sender (as-contract tx-sender))) +;; @desc units of token required for a target price +;; @param the-yield-token; yield token +;; @param price; target price +;; @returns (response uint uint) +(define-read-only (get-x-given-price (expiry uint) (the-yield-token ) (price uint)) + + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) + (balance-token (get balance-token pool)) + ) + (contract-call? .yield-token-equation get-x-given-price balance-token balance-yield-token normalized-expiry price) + ) +) - ;; 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) , expiry: expiry } - { amount: (+ amount token-count)}) - (ok amount) +;; @desc units of yield token required for a target price +;; @param the-yield-token; yield token +;; @param price; target price +;; @returns (response uint uint) +(define-read-only (get-y-given-price (expiry uint) (the-yield-token ) (price uint)) + + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) + (balance-token (get balance-token pool)) + ) + (contract-call? .yield-token-equation get-y-given-price balance-token balance-yield-token normalized-expiry price) ) +) + +;; @desc units of token required for a target yield +;; @param the-yield-token; yield token +;; @param yield; target yield +;; @returns (response uint uint) +(define-read-only (get-x-given-yield (expiry uint) (the-yield-token ) (yield uint)) + + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) + (balance-token (get balance-token pool)) + ) + (contract-call? .yield-token-equation get-x-given-yield balance-token balance-yield-token normalized-expiry yield) + ) +) + +;; @desc units of yield token required for a target yield +;; @param the-yield-token; yield token +;; @param yield; target yield +;; @returns (response uint uint) +(define-read-only (get-y-given-yield (expiry uint) (the-yield-token ) (yield uint)) + + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) + (balance-token (get balance-token pool)) + ) + (contract-call? .yield-token-equation get-y-given-yield balance-token balance-yield-token normalized-expiry yield) + ) +) + +;; @desc units of pool token to be minted, together with break-down of yield-token given amount of token being added +;; @param the-yield-token; yield token +;; @param dx; amount of token added +;; @returns (response (tuple uint uint uint) uint) +(define-read-only (get-token-given-position (expiry uint) (the-yield-token ) (dx uint)) + + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-actual (get balance-yield-token pool)) + (balance-virtual (get balance-virtual pool)) + (balance-yield-token (+ balance-actual balance-virtual)) + (balance-token (get balance-token pool)) + (total-supply (get total-supply pool)) + (data (try! (contract-call? .yield-token-equation get-token-given-position balance-token balance-yield-token normalized-expiry total-supply dx))) + (token (get token data)) + (dy (get dy data)) + (percent-act (if (is-eq balance-yield-token u0) u0 (div-down balance-actual balance-yield-token))) + (dy-act (if (is-eq token dy) u0 (mul-down dy percent-act))) + (dy-vir (if (is-eq token dy) token (if (<= dy dy-act) u0 (- dy dy-act)))) + ) + (ok {token: token, dy-act: dy-act, dy-vir: dy-vir}) + ) + +) + +;; @desc units of token, yield-token and yield-token (virtual) required to mint given units of pool-token +;; @param the-yield-token; yield token +;; @param token; units of pool token to be minted +;; @returns (response (tuple uint uint uint) uint) +(define-read-only (get-position-given-mint (expiry uint) (the-yield-token ) (token uint)) + + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-actual (get balance-yield-token pool)) + (balance-virtual (get balance-virtual pool)) + (balance-yield-token (+ balance-actual balance-virtual)) + (balance-token (get balance-token pool)) + (total-supply (get total-supply pool)) + (data (try! (contract-call? .yield-token-equation get-position-given-mint balance-token balance-yield-token normalized-expiry total-supply token))) + (dx (get dx data)) + (dy (get dy data)) + (percent-act (div-down balance-actual balance-yield-token)) + (dy-act (mul-down dy percent-act)) + (dy-vir (if (<= dy dy-act) u0 (- dy dy-act))) + ) + (ok {dx: dx, dy-act: dy-act, dy-vir: dy-vir}) + ) +) + +;; @desc units of token, yield-token and yield-token (virtual) to be returned after burning given units of pool-token +;; @param the-yield-token; yield token +;; @param token; units of pool token to be burnt +;; @returns (response (tuple uint uint uint) uint) +(define-read-only (get-position-given-burn (expiry uint) (the-yield-token ) (token uint)) + (let + ( + (yield-token (contract-of the-yield-token)) + (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) + (listed (get listed pool)) + (normalized-expiry (try! (get-t expiry listed))) + (balance-actual (get balance-yield-token pool)) + (balance-virtual (get balance-virtual pool)) + (balance-yield-token (+ balance-actual balance-virtual)) + (balance-token (get balance-token pool)) + (total-supply (get total-supply pool)) + (data (try! (contract-call? .yield-token-equation get-position-given-burn balance-token balance-yield-token normalized-expiry total-supply token))) + (dx (get dx data)) + (dy (get dy data)) + (percent-act (div-down balance-actual balance-yield-token)) + (dy-act (mul-down dy percent-act)) + (dy-vir (if (<= dy dy-act) u0 (- dy dy-act))) + ) + (ok {dx: dx, dy-act: dy-act, dy-vir: dy-vir}) ) +) -(define-public (end-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) - (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)) + +;; math-fixed-point +;; Fixed Point Math +;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol + +;; constants +;; +(define-constant SCALE_UP_OVERFLOW (err u5001)) +(define-constant SCALE_DOWN_OVERFLOW (err u5002)) +(define-constant ADD_OVERFLOW (err u5003)) +(define-constant SUB_OVERFLOW (err u5004)) +(define-constant MUL_OVERFLOW (err u5005)) +(define-constant DIV_OVERFLOW (err u5006)) +(define-constant POW_OVERFLOW (err u5007)) + +;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, +;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error +;; (i.e. the last digit of the result may be completely lost to this error). +(define-constant MAX_POW_RELATIVE_ERROR u4) + +;; public functions +;; + +(define-read-only (get_one) + (ok ONE_8) +) + +(define-read-only (scale-up (a uint)) + (* a ONE_8) +) + +(define-read-only (scale-down (a uint)) + (/ a ONE_8) +) + +(define-read-only (mul-down (a uint) (b uint)) + (/ (* a b) ONE_8) +) + + +(define-read-only (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) + +(define-read-only (div-down (a uint) (b uint)) + (if (is-eq a u0) + u0 + (/ (* a ONE_8) b) ) +) - (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) +(define-read-only (div-up (a uint) (b uint)) + (if (is-eq a u0) + u0 + (+ u1 (/ (- (* a ONE_8) u1) b)) + ) +) - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) +(define-read-only (pow-down (a uint) (b uint)) + (let + ( + (raw (unwrap-panic (pow-fixed a b))) + (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) + ) + (if (< raw max-error) + u0 + (- raw max-error) + ) + ) +) - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) +(define-read-only (pow-up (a uint) (b uint)) + (let + ( + (raw (unwrap-panic (pow-fixed a b))) + (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) + ) + (+ raw max-error) + ) ) -;; 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 +;; math-log-exp +;; Exponentiation and logarithm functions for 8 decimal fixed point numbers (both base and exponent/argument). +;; Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural +;; exponentiation and logarithm (where the base is Euler's number). +;; Reference: https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/LogExpMath.sol +;; MODIFIED: because we use only 128 bits instead of 256, we cannot do 20 decimal or 36 decimal accuracy like in Balancer. + +;; constants +;; +;; All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying +;; two numbers, and multiply by ONE when dividing them. +;; All arguments and return values are 8 decimal fixed point numbers. +(define-constant iONE_8 (pow 10 8)) +(define-constant ONE_10 (pow 10 10)) + +;; The domain of natural exponentiation is bound by the word size and number of decimals used. +;; The largest possible result is (2^127 - 1) / 10^8, +;; which makes the largest exponent ln((2^127 - 1) / 10^8) = 69.6090111872. +;; The smallest possible result is 10^(-8), which makes largest negative argument ln(10^(-8)) = -18.420680744. +;; We use 69.0 and -18.0 to have some safety margin. +(define-constant MAX_NATURAL_EXPONENT (* 69 iONE_8)) +(define-constant MIN_NATURAL_EXPONENT (* -18 iONE_8)) + +(define-constant MILD_EXPONENT_BOUND (/ (pow u2 u126) (to-uint iONE_8))) + +;; Because largest exponent is 69, we start from 64 +;; The first several a_n are too large if stored as 8 decimal numbers, and could cause intermediate overflows. +;; Instead we store them as plain integers, with 0 decimals. +(define-constant x_a_list_no_deci (list +{x_pre: 6400000000, a_pre: 6235149080811616882910000000, use_deci: false} ;; x1 = 2^6, a1 = e^(x1) +)) +;; 8 decimal constants +(define-constant x_a_list (list +{x_pre: 3200000000, a_pre: 7896296018268069516100, use_deci: true} ;; x2 = 2^5, a2 = e^(x2) +{x_pre: 1600000000, a_pre: 888611052050787, use_deci: true} ;; x3 = 2^4, a3 = e^(x3) +{x_pre: 800000000, a_pre: 298095798704, use_deci: true} ;; x4 = 2^3, a4 = e^(x4) +{x_pre: 400000000, a_pre: 5459815003, use_deci: true} ;; x5 = 2^2, a5 = e^(x5) +{x_pre: 200000000, a_pre: 738905610, use_deci: true} ;; x6 = 2^1, a6 = e^(x6) +{x_pre: 100000000, a_pre: 271828183, use_deci: true} ;; x7 = 2^0, a7 = e^(x7) +{x_pre: 50000000, a_pre: 164872127, use_deci: true} ;; x8 = 2^-1, a8 = e^(x8) +{x_pre: 25000000, a_pre: 128402542, use_deci: true} ;; x9 = 2^-2, a9 = e^(x9) +{x_pre: 12500000, a_pre: 113314845, use_deci: true} ;; x10 = 2^-3, a10 = e^(x10) +{x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) +)) + +(define-constant X_OUT_OF_BOUNDS (err u5009)) +(define-constant Y_OUT_OF_BOUNDS (err u5010)) +(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant INVALID_EXPONENT (err u5012)) +(define-constant OUT_OF_BOUNDS (err u5013)) + +;; private functions +;; + +;; Internal natural logarithm (ln(a)) with signed 8 decimal fixed point argument. +(define-private (ln-priv (a int)) + (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) - ) + (a_sum_no_deci (fold accumulate_division x_a_list_no_deci {a: a, sum: 0})) + (a_sum (fold accumulate_division x_a_list {a: (get a a_sum_no_deci), sum: (get sum a_sum_no_deci)})) + (out_a (get a a_sum)) + (out_sum (get sum a_sum)) + (z (/ (* (- out_a iONE_8) iONE_8) (+ out_a iONE_8))) + (z_squared (/ (* z z) iONE_8)) + (div_list (list 3 5 7 9 11)) + (num_sum_zsq (fold rolling_sum_div div_list {num: z, seriesSum: z, z_squared: z_squared})) + (seriesSum (get seriesSum num_sum_zsq)) + (r (+ out_sum (* seriesSum 2))) + ) + (ok r) + ) +) - (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) +(define-private (accumulate_division (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_a_sum (tuple (a int) (sum int)))) + (let + ( + (a_pre (get a_pre x_a_pre)) + (x_pre (get x_pre x_a_pre)) + (use_deci (get use_deci x_a_pre)) + (rolling_a (get a rolling_a_sum)) + (rolling_sum (get sum rolling_a_sum)) + ) + (if (>= rolling_a (if use_deci a_pre (* a_pre iONE_8))) + {a: (/ (* rolling_a (if use_deci iONE_8 1)) a_pre), sum: (+ rolling_sum x_pre)} + {a: rolling_a, sum: rolling_sum} + ) + ) +) - ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) - (ok true) +(define-private (rolling_sum_div (n int) (rolling (tuple (num int) (seriesSum int) (z_squared int)))) + (let + ( + (rolling_num (get num rolling)) + (rolling_sum (get seriesSum rolling)) + (z_squared (get z_squared rolling)) + (next_num (/ (* rolling_num z_squared) iONE_8)) + (next_sum (+ rolling_sum (/ next_num n))) + ) + {num: next_num, seriesSum: next_sum, z_squared: z_squared} + ) +) + +;; Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to +;; arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means +;; x^y = exp(y * ln(x)). +;; Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`. +(define-private (pow-priv (x uint) (y uint)) + (let + ( + (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)) + ) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - (let +(define-private (exp-pos (x int)) + (begin + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (let + ( + ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct + ;; it and compute the accumulated product. + (x_product_no_deci (fold accumulate_product x_a_list_no_deci {x: x, product: 1})) + (x_adj (get x x_product_no_deci)) + (firstAN (get product x_product_no_deci)) + (x_product (fold accumulate_product x_a_list {x: x_adj, product: iONE_8})) + (product_out (get product x_product)) + (x_out (get x x_product)) + (seriesSum (+ iONE_8 x_out)) + (div_list (list 2 3 4 5 6 7 8 9 10 11 12)) + (term_sum_x (fold rolling_div_sum div_list {term: x_out, seriesSum: seriesSum, x: x_out})) + (sum (get seriesSum term_sum_x)) + ) + (ok (* (/ (* product_out sum) iONE_8) firstAN)) + ) + ) +) + +(define-private (accumulate_product (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_x_p (tuple (x int) (product int)))) + (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (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 expiry new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc expiry new-fee-rate-y)) - - (ok true) + (x_pre (get x_pre x_a_pre)) + (a_pre (get a_pre x_a_pre)) + (use_deci (get use_deci x_a_pre)) + (rolling_x (get x rolling_x_p)) + (rolling_product (get product rolling_x_p)) + ) + (if (>= rolling_x x_pre) + {x: (- rolling_x x_pre), product: (/ (* rolling_product a_pre) (if use_deci iONE_8 1))} + {x: rolling_x, product: rolling_product} + ) + ) +) + +(define-private (rolling_div_sum (n int) (rolling (tuple (term int) (seriesSum int) (x int)))) + (let + ( + (rolling_term (get term rolling)) + (rolling_sum (get seriesSum rolling)) + (x (get x rolling)) + (next_term (/ (/ (* rolling_term x) iONE_8) n)) + (next_sum (+ rolling_sum next_term)) + ) + {term: next_term, seriesSum: next_sum, x: x} + ) +) + +;; public functions +;; + +(define-read-only (get-exp-bound) + (ok MILD_EXPONENT_BOUND) +) + +;; Exponentiation (x^y) with unsigned 8 decimal fixed point base and exponent. +(define-read-only (pow-fixed (x uint) (y uint)) + (begin + ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. + (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + + ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. + (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + + (if (is-eq y u0) + (ok (to-uint iONE_8)) + (if (is-eq x u0) + (ok u0) + (pow-priv x y) + ) + ) ) ) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fwp-wbtc-usda-50-50' -(impl-trait .trait-multisig-vote.multisig-vote-trait) + +;; Natural exponentiation (e^x) with signed 8 decimal fixed point exponent. +;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. +(define-read-only (exp-fixed (x int)) + (begin + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (if (< x 0) + ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it + ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). + ;; Fixed point division requires multiplying by iONE_8. + (ok (/ (* iONE_8 iONE_8) (unwrap-panic (exp-pos (* -1 x))))) + (exp-pos x) + ) + ) +) + +;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. +(define-read-only (log-fixed (arg int) (base int)) + ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). + (let + ( + (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) + (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) + ) + (ok (/ (* logArg iONE_8) logBase)) + ) +) + +;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. +(define-read-only (ln-fixed (a int)) + (begin + (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (if (< a iONE_8) + ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). + ;; If a is less than one, 1/a will be greater than one. + ;; Fixed point division requires multiplying by iONE_8. + (ok (- 0 (unwrap-panic (ln-priv (/ (* iONE_8 iONE_8) a))))) + (ln-priv a) + ) + ) +) +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-token-pool' +(impl-trait .trait-ownable.ownable-trait) (use-trait ft-trait .trait-sip-010.sip-010-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(use-trait multisig-trait .trait-multisig-vote.multisig-vote-sft-trait) + +;; collateral-rebalancing-pool +;; + +;; constants +;; +(define-constant ONE_8 u100000000) ;; 8 decimal places + +(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 ERR-WEIGHTED-EQUATION-CALL (err u2009)) +(define-constant ERR-INTERNAL-FUNCTION-CALL (err u1001)) +(define-constant ERR-GET-WEIGHT-FAIL (err u2012)) +(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) +(define-constant ERR-GET-PRICE-FAIL (err u2015)) +(define-constant ERR-GET-SYMBOL-FAIL (err u6000)) +(define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) +(define-constant ERR-EXPIRY (err u2017)) +(define-constant ERR-get-balance-fixed-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 ERR-INVALID-POOL-TOKEN (err u2023)) +(define-constant a1 u27839300) +(define-constant a2 u23038900) +(define-constant a3 u97200) +(define-constant a4 u7810800) -;; Alex voting for MultiSig DAO -;; -;; Voting and proposing the proposals -;; A proposal will just update the DAO with new contracts. +(define-data-var CONTRACT-OWNER principal tx-sender) -;; 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. -;; +(define-read-only (get-owner) + (ok (var-get CONTRACT-OWNER)) +) -;; 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-public (set-owner (owner principal)) + (begin + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + (ok (var-set CONTRACT-OWNER owner)) + ) +) -(define-constant ONE_8 u100000000) -;; Constants -(define-constant DEFAULT_OWNER tx-sender) +;; data maps and vars +;; +(define-map pools-map + { pool-id: uint } + { + token-x: principal, ;; collateral + token-y: principal, ;; token + expiry: uint + } +) -;; 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 } +(define-map pools-data-map { - 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 - } + token-x: principal, + token-y: principal, + expiry: uint + } + { + yield-supply: uint, + key-supply: uint, + balance-x: uint, + balance-y: uint, + fee-to-address: principal, + yield-token: principal, + key-token: principal, + strike: uint, + bs-vol: uint, + ltv-0: uint, + fee-rate-x: uint, + fee-rate-y: uint, + fee-rebate: uint, + weight-x: uint, + weight-y: uint, + moving-average: uint, + conversion-ltv: 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 pool-count uint u0) +(define-data-var pools-list (list 2000 uint) (list)) + +;; private functions +;; + +;; Approximation of Error Function using Abramowitz and Stegun +;; https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions +;; Please note erf(x) equals -erf(-x) +(define-private (erf (x uint)) + (let + ( + (a1x (mul-down a1 x)) + (x2 (pow-down x u200000000)) + (a2x (mul-down a2 x2)) + (x3 (pow-down x u300000000)) + (a3x (mul-down a3 x3)) + (x4 (pow-down x u400000000)) + (a4x (mul-down a4 x4)) + (denom (+ ONE_8 a1x)) + (denom1 (+ denom a2x)) + (denom2 (+ denom1 a3x)) + (denom3 (+ denom2 a4x)) + (denom4 (pow-down denom3 u400000000)) + (base (div-down ONE_8 denom4)) + ) + (if (<= ONE_8 base) u0 (- ONE_8 base)) + ) +) + +;; public functions +;; + +;; @desc get-pool-count +;; @returns uint +(define-read-only (get-pool-count) + (var-get pool-count) +) + +;; @desc get-pool-contracts +;; @param pool-id; pool-id +;; @returns (response (tuple) uint) +(define-read-only (get-pool-contracts (pool-id uint)) + (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) ERR-INVALID-POOL-ERR)) +) + +;; @desc get-pools +;; @returns (optional (tuple)) +(define-read-only (get-pools) + (map get-pool-contracts (var-get pools-list)) +) + +;; @desc get-pool-details +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; expiry block-height +;; @returns (response (tuple) uint) +(define-read-only (get-pool-details (token ) (collateral ) (expiry uint)) + (ok (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) +) + +;; @desc get-spot +;; @desc units of token per unit of collateral +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; expiry block-height +;; @returns (response uint uint) +(define-read-only (get-spot (token ) (collateral )) + (if (is-eq token collateral) + (ok ONE_8) + (contract-call? .fixed-weight-pool get-oracle-resilient token collateral u50000000 u50000000) + ) +) + +;; @desc get-pool-value-in-token +;; @desc value of pool in units of borrow token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; expiry block-height +;; @returns (response uint uint) +(define-read-only (get-pool-value-in-token (token ) (collateral ) (expiry uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-y (get balance-y pool)) + (balance-x-in-y (div-down (get balance-x pool) (try! (get-spot token collateral)))) + ) + (ok (+ balance-x-in-y balance-y)) + ) +) + +;; @desc get-pool-value-in-collateral +;; @desc value of pool in units of collateral token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; expiry block-height +;; @returns (response uint uint) +(define-read-only (get-pool-value-in-collateral (token ) (collateral ) (expiry uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (balance-x (get balance-x pool)) + (balance-y-in-x (mul-down (get balance-y pool) (try! (get-spot token collateral)))) + ) + (ok (+ balance-y-in-x balance-x)) + ) +) + +;; @desc get-ltv +;; @desc value of yield-token as % of pool value (i.e. loan-to-value) +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; expiry block-height +;; @returns (response uint uint) +(define-read-only (get-ltv (token ) (collateral ) (expiry uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (yield-supply (get yield-supply pool)) ;; in token + (pool-value (try! (get-pool-value-in-token token collateral expiry))) ;; also in token + ) + ;; if no liquidity in the pool, return ltv-0 + (if (is-eq yield-supply u0) + (ok (get ltv-0 pool)) + (ok (div-down yield-supply pool-value)) + ) + ) +) + +;; @desc get-weight-y +;; @desc delta of borrow token (risky asset) based on reference black-scholes option with expiry/strike/bs-vol +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; expiry block-height +;; @param strike; reference strike price +;; @param bs-vol; reference black-scholes vol +;; @returns (response uint uint) +(define-read-only (get-weight-y (token ) (collateral ) (expiry uint) (strike uint) (bs-vol uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) + (weight-y (get weight-y pool)) + (moving-average (get moving-average pool)) + (conversion-ltv (get conversion-ltv pool)) + (ma-comp (- ONE_8 moving-average)) + + (spot (try! (get-spot token collateral))) + (now (* block-height ONE_8)) + (ltv (try! (get-ltv token collateral expiry))) + ) + (if (or (> ltv conversion-ltv) (>= now expiry)) + (ok u99900000) + (let + ( + ;; assume 15secs per block + (t (div-down + (- expiry now) (* u2102400 ONE_8))) + + ;; we calculate d1 first + (spot-term (div-up spot strike)) + (pow-bs-vol (div-up (pow-down bs-vol u200000000) u200000000)) + (vol-term (mul-up t pow-bs-vol)) + (sqrt-t (pow-down t u50000000)) + (sqrt-2 (pow-down u200000000 u50000000)) + + (denominator (mul-down bs-vol sqrt-t)) + (numerator (+ vol-term (- (if (> spot-term ONE_8) spot-term ONE_8) (if (> spot-term ONE_8) ONE_8 spot-term)))) + (d1 (div-up numerator denominator)) + (erf-term (erf (div-up d1 sqrt-2))) + (complement (if (> spot-term ONE_8) (+ ONE_8 erf-term) (if (<= ONE_8 erf-term) u0 (- ONE_8 erf-term)))) + (weight-t (div-up complement u200000000)) + (weighted (+ (mul-down moving-average weight-y) (mul-down ma-comp weight-t))) + ) + ;; make sure weight-x > 0 so it works with weighted-equation + (ok (if (> weighted u100000) weighted u100000)) + ) + ) + ) +) + +;; @desc create-pool with single sided liquidity +;; @restricted CONTRACT-OWNER +;; @param token; borrow token +;; @param collateral; collateral token +;; @param the-yield-token; yield-token to be minted +;; @param the-key-token; key-token to be minted +;; @param multisig-vote; multisig to govern the pool being created +;; @param ltv-0; initial loan-to-value +;; @param conversion-ltv; loan-to-value at which conversion into borrow token happens +;; @param bs-vol; reference black-scholes vol to use +;; @param moving-average; weighting smoothing factor +;; @param dx; amount of collateral token being added +;; @returns (response bool uint) +(define-public (create-pool (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (multisig-vote ) (ltv-0 uint) (conversion-ltv uint) (bs-vol uint) (moving-average uint) (dx uint)) + (begin + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + (asserts! + (is-none (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry })) + ERR-POOL-ALREADY-EXISTS + ) + (let + ( + (pool-id (+ (var-get pool-count) u1)) + (token-x (contract-of collateral)) + (token-y (contract-of token)) + + (now (* block-height ONE_8)) + ;; assume 10mins per block + (t (div-down + (- expiry now) (* u52560 ONE_8))) + + ;; we calculate d1 first + ;; because we support 'at-the-money' only, we can simplify formula + (sqrt-t (pow-down t u50000000)) + (sqrt-2 (pow-down u200000000 u50000000)) + (pow-bs-vol (div-up (pow-down bs-vol u200000000) u200000000)) + (numerator (mul-up t pow-bs-vol)) + (denominator (mul-down bs-vol sqrt-t)) + (d1 (div-up numerator denominator)) + (erf-term (erf (div-up d1 sqrt-2))) + (complement (if (<= ONE_8 erf-term) u0 (- ONE_8 erf-term))) + (weighted (div-up complement u200000000)) + (weight-y (if (> weighted u100000) weighted u100000)) + + (weight-x (- ONE_8 weight-y)) + + (pool-data { + yield-supply: u0, + key-supply: u0, + balance-x: u0, + balance-y: u0, + fee-to-address: (contract-of multisig-vote), + yield-token: (contract-of the-yield-token), + key-token: (contract-of the-key-token), + strike: (try! (get-spot token collateral)), + bs-vol: bs-vol, + fee-rate-x: u0, + fee-rate-y: u0, + fee-rebate: u0, + ltv-0: ltv-0, + weight-x: weight-x, + weight-y: weight-y, + moving-average: moving-average, + conversion-ltv: conversion-ltv + }) + ) + + (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) + + (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) + (var-set pool-count pool-id) + (try! (add-to-position token collateral expiry the-yield-token the-key-token dx)) + (print { object: "pool", action: "created", data: pool-data }) + (ok true) + ) + ) +) + +;; @desc mint yield-token and key-token, swap minted yield-token with token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param the-yield-token; yield-token to be minted +;; @param the-key-token; key-token to be minted +;; @param dx; amount of collateral added +;; @post collateral; sender transfer exactly dx to alex-vault +;; @post yield-token; sender transfers > 0 to alex-vault +;; @post token; alex-vault transfers >0 to sender +;; @returns (response (tuple uint uint) uint) +(define-public (add-to-position-and-switch (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (dx uint)) + (let + ( + (minted-yield-token (get yield-token (try! (add-to-position token collateral expiry the-yield-token the-key-token dx)))) + ) + (contract-call? .yield-token-pool swap-y-for-x expiry the-yield-token token minted-yield-token none) + ) +) + +;; @desc mint yield-token and key-token, with single-sided liquidity +;; @param token; borrow token +;; @param collateral; collateral token +;; @param the-yield-token; yield-token to be minted +;; @param the-key-token; key-token to be minted +;; @param dx; amount of collateral added +;; @post collateral; sender transfer exactly dx to alex-vault +;; @returns (response (tuple uint uint) uint) +(define-public (add-to-position (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (dx 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)) + (conversion-ltv (get conversion-ltv pool)) + (ltv (try! (get-ltv token collateral expiry))) + ) + (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) + ;; mint is possible only if ltv < 1 + (asserts! (>= conversion-ltv ltv) ERR-LTV-GREATER-THAN-ONE) + (asserts! (and (is-eq (get yield-token pool) (contract-of the-yield-token)) (is-eq (get key-token pool) (contract-of the-key-token))) ERR-INVALID-POOL-TOKEN) + (let + ( + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (yield-supply (get yield-supply pool)) + (key-supply (get key-supply pool)) + (weight-x (get weight-x pool)) + + (new-supply (try! (get-token-given-position token collateral expiry dx))) + (yield-new-supply (get yield-token new-supply)) + (key-new-supply (get key-token new-supply)) + + (dx-weighted (mul-down weight-x dx)) + (dx-to-dy (if (<= dx dx-weighted) u0 (- dx dx-weighted))) + + (dy-weighted (if (is-eq token-x token-y) + dx-to-dy + (try! (contract-call? .fixed-weight-pool swap token collateral u50000000 u50000000 dx-to-dy none)) + ) + ) + + (pool-updated (merge pool { + yield-supply: (+ yield-new-supply yield-supply), + key-supply: (+ key-new-supply key-supply), + balance-x: (+ balance-x dx-weighted), + balance-y: (+ balance-y dy-weighted) + })) + ) + + (unwrap! (contract-call? collateral transfer dx-weighted tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) + (unwrap! (contract-call? token transfer dy-weighted tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) + + (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) + ;; mint pool token and send to tx-sender + (try! (contract-call? the-yield-token mint expiry yield-new-supply tx-sender)) + (try! (contract-call? the-key-token mint expiry key-new-supply tx-sender)) + (print { object: "pool", action: "liquidity-added", data: pool-updated }) + (ok {yield-token: yield-new-supply, key-token: key-new-supply}) + ) + ) +) + +;; @desc burn yield-token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param the-yield-token; yield-token to be burnt +;; @param percent; % of yield-token held to be burnt +;; @post yield-token; alex-vault transfer exactly uints of token equal to (percent * yield-token held) to sender +;; @returns (response (tuple uint uint) uint) +(define-public (reduce-position-yield (token ) (collateral ) (expiry uint) (the-yield-token ) (percent uint)) + (begin + (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) + ;; burn supported only at maturity + (asserts! (> (* block-height ONE_8) expiry) ERR-EXPIRY) + + (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)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (yield-supply (get yield-supply pool)) + (total-shares (unwrap! (contract-call? the-yield-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) + (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) + (shares-to-yield (div-down shares yield-supply)) + + ;; if there are any residual collateral, convert to token + (bal-x-to-y (if (is-eq balance-x u0) + u0 + (if (is-eq token-x token-y) + balance-x + (begin + (as-contract (try! (contract-call? .alex-vault transfer-ft collateral balance-x tx-sender))) + (as-contract (try! (contract-call? .fixed-weight-pool swap token collateral u50000000 u50000000 balance-x none))) + ) + ) + ) + ) + (new-bal-y (+ balance-y bal-x-to-y)) + (dy (mul-down new-bal-y shares-to-yield)) + + (pool-updated (merge pool { + yield-supply: (if (<= yield-supply shares) u0 (- yield-supply shares)), + balance-x: u0, + balance-y: (if (<= new-bal-y dy) u0 (- new-bal-y dy)) + }) + ) + ) + + (asserts! (is-eq (get yield-token pool) (contract-of the-yield-token)) ERR-INVALID-POOL-TOKEN) + + ;; if any conversion happened at contract level, transfer back to vault + (and + (> bal-x-to-y u0) + (not (is-eq token-x token-y)) + (as-contract (unwrap! (contract-call? token transfer bal-x-to-y tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + ) + + ;; if shares > dy, then transfer the shortfall from reserve. + ;; TODO: what if token is exhausted but reserve have others? + (and (< dy shares) (try! (contract-call? .alex-reserve-pool remove-from-balance token-y (- shares dy)))) + + ;; transfer shares of token to tx-sender, ensuring convertability of yield-token + (try! (contract-call? .alex-vault transfer-ft token shares tx-sender)) + + (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) + (try! (contract-call? the-yield-token burn expiry shares tx-sender)) + + (print { object: "pool", action: "liquidity-removed", data: pool-updated }) + (ok {dx: u0, dy: shares}) + ) + ) +) + +;; @desc burn key-token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param the-key-token; key-token to be burnt +;; @param percent; % of key-token held to be burnt +;; @post token; alex-vault transfers > 0 token to sender +;; @post collateral; alex-vault transfers > 0 collateral to sender +;; @returns (response (tuple uint uint) uint) +(define-public (reduce-position-key (token ) (collateral ) (expiry uint) (the-key-token ) (percent uint)) + (begin + (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) + ;; burn supported only at maturity + (asserts! (> (* block-height ONE_8) expiry) ERR-EXPIRY) + (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)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (key-supply (get key-supply pool)) + (total-shares (unwrap! (contract-call? the-key-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) + (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) + (reduce-data (try! (get-position-given-burn-key token collateral expiry shares))) + (dx-weighted (get dx reduce-data)) + (dy-weighted (get dy reduce-data)) + + (pool-updated (merge pool { + key-supply: (if (<= key-supply shares) u0 (- key-supply shares)), + balance-x: (if (<= balance-x dx-weighted) u0 (- balance-x dx-weighted)), + balance-y: (if (<= balance-y dy-weighted) u0 (- balance-y dy-weighted)) + }) + ) + ) + + (asserts! (is-eq (get key-token pool) (contract-of the-key-token)) ERR-INVALID-POOL-TOKEN) + + (and (> dx-weighted u0) (try! (contract-call? .alex-vault transfer-ft collateral dx-weighted tx-sender))) + (and (> dy-weighted u0) (try! (contract-call? .alex-vault transfer-ft token dy-weighted tx-sender))) + + (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) + (try! (contract-call? the-key-token burn expiry shares tx-sender)) + (print { object: "pool", action: "liquidity-removed", data: pool-updated }) + (ok {dx: dx-weighted, dy: dy-weighted}) + ) + ) +) + +;; @desc swap collateral with token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param dx; amount of collateral to be swapped +;; @param min-dy; max slippage +;; @post collateral; sender transfers exactly dx collateral to alex-vault +;; @returns (response (tuple uint uint) uint) +(define-public (swap-x-for-y (token ) (collateral ) (expiry uint) (dx uint) (min-dy (optional uint))) + (begin + (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) + ;; swap is supported only if token /= collateral + (asserts! (not (is-eq token collateral)) ERR-INVALID-POOL-ERR) + (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)) + (strike (get strike pool)) + (bs-vol (get bs-vol pool)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + + ;; every swap call updates the weights + (weight-y (unwrap! (get-weight-y token collateral expiry strike bs-vol) ERR-GET-WEIGHT-FAIL)) + (weight-x (- ONE_8 weight-y)) + + ;; fee = dx * fee-rate-x + (fee (mul-up dx (get fee-rate-x pool))) + (fee-rebate (mul-down fee (get fee-rebate pool))) + (dx-net-fees (if (<= dx fee) u0 (- dx fee))) + (dy (try! (get-y-given-x token collateral expiry dx-net-fees))) + + (pool-updated + (merge pool + { + balance-x: (+ balance-x dx-net-fees fee-rebate), + balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), + weight-x: weight-x, + weight-y: weight-y + } + ) + ) + ) + + (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 tx-sender)) + (try! (contract-call? .alex-reserve-pool add-to-balance token-x (- fee fee-rebate))) + + ;; 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}) + ) + ) +) + +;; @desc swap token with collateral +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param dy; amount of token to be swapped +;; @param min-dx; max slippage +;; @post token; sender transfers exactly dy token to alex-vault +;; @returns (response (tuple uint uint) uint) +(define-public (swap-y-for-x (token ) (collateral ) (expiry uint) (dy uint) (min-dx (optional uint))) + (begin + (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) + ;; swap is supported only if token /= collateral + (asserts! (not (is-eq token collateral)) ERR-INVALID-POOL-ERR) + (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)) + (strike (get strike pool)) + (bs-vol (get bs-vol pool)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + + ;; every swap call updates the weights + (weight-y (unwrap! (get-weight-y token collateral expiry strike bs-vol) ERR-GET-WEIGHT-FAIL)) + (weight-x (- ONE_8 weight-y)) + + ;; fee = dy * fee-rate-y + (fee (mul-up dy (get fee-rate-y pool))) + (fee-rebate (mul-down fee (get fee-rebate pool))) + (dy-net-fees (if (<= dy fee) u0 (- dy fee))) + (dx (try! (get-x-given-y token collateral expiry 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-rebate), + weight-x: weight-x, + weight-y: weight-y + } + ) + ) + ) + + (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) + + (try! (contract-call? .alex-vault transfer-ft collateral dx tx-sender)) + (unwrap! (contract-call? token transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) + (try! (contract-call? .alex-reserve-pool add-to-balance token-y (- fee fee-rebate))) + + ;; 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}) + ) + ) +) + +;; @desc get-fee-rebate +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @returns (response uint uint) +(define-read-only (get-fee-rebate (token ) (collateral ) (expiry uint)) + (ok (get fee-rebate (try! (get-pool-details token collateral expiry)))) +) + +;; @desc set-fee-rebate +;; @restricted CONTRACT-OWNER +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param fee-rebate; new fee-rebate +;; @returns (response bool uint) +(define-public (set-fee-rebate (token ) (collateral ) (expiry uint) (fee-rebate uint)) + (let + ( + (pool (try! (get-pool-details token collateral expiry))) + ) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + + (map-set pools-data-map + { + token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry + } + (merge pool { fee-rebate: fee-rebate }) + ) + (ok true) + ) +) + +;; @desc get-fee-rate-x +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @returns (response uint uint) +(define-read-only (get-fee-rate-x (token ) (collateral ) (expiry uint)) + (ok (get fee-rate-x (try! (get-pool-details token collateral expiry)))) +) + +;; @desc get-fee-rate-y +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @returns (response uint uint) +(define-read-only (get-fee-rate-y (token ) (collateral ) (expiry uint)) + (ok (get fee-rate-y (try! (get-pool-details token collateral expiry)))) +) + +;; @desc set-fee-rate-x +;; @restricted fee-to-address +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param fee-rate-x; new fee-rate-x +;; @returns (response bool uint) +(define-public (set-fee-rate-x (token ) (collateral ) (expiry uint) (fee-rate-x uint)) + (let + ( + (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: (contract-of collateral), token-y: (contract-of token), expiry: expiry + } + (merge pool { fee-rate-x: fee-rate-x }) + ) + (ok true) + ) +) + +;; @desc set-fee-rate-y +;; @restricted fee-to-address +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param fee-rate-y; new fee-rate-y +;; @returns (response bool uint) +(define-public (set-fee-rate-y (token ) (collateral ) (expiry uint) (fee-rate-y uint)) + (let + ( + (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: (contract-of collateral), token-y: (contract-of token), expiry: expiry + } + (merge pool { fee-rate-y: fee-rate-y }) + ) + (ok true) + ) +) + +;; @desc get-fee-to-address (multisig of the pool) +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @returns (response principal uint) +(define-read-only (get-fee-to-address (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-to-address pool)) + ) +) -(define-data-var total-supply-of-token uint u0) -(define-data-var threshold-percentage uint u0) +;; @desc units of token given units of collateral +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param dx; amount of collateral being added +;; @returns (response uint uint) +(define-read-only (get-y-given-x (token ) (collateral ) (expiry uint) (dx uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map + { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) + ERR-INVALID-POOL-ERR) + ) + ) + (contract-call? .weighted-equation get-y-given-x + (get balance-x pool) + (get balance-y pool) + (get weight-x pool) + (get weight-y pool) + dx + ) + ) +) -(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 }) +;; @desc units of collateral given units of token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param dy; amount of token being added +;; @returns (response uint uint) +(define-read-only (get-x-given-y (token ) (collateral ) (expiry uint) (dy uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map + { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) + ERR-INVALID-POOL-ERR) + ) + ) + (contract-call? .weighted-equation get-x-given-y + (get balance-x pool) + (get balance-y pool) + (get weight-x pool) + (get weight-y pool) + dy + ) + ) +) -;; Get all proposals in detail -(define-read-only (get-proposals) - (ok (map get-proposal-by-id (var-get proposal-ids))) +;; @desc units of collateral required for a target price +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param price; target price +;; @returns (response uint uint) +(define-read-only (get-x-given-price (token ) (collateral ) (expiry uint) (price 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)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (weight-x (get weight-x pool)) + (weight-y (get weight-y pool)) + ) + (contract-call? .weighted-equation get-x-given-price balance-x balance-y weight-x weight-y price) + ) ) -;; Get all proposal ID in list -(define-read-only (get-proposal-ids) - (ok (var-get proposal-ids)) +;; @desc units of token required for a target price +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param price; target price +;; @returns (response uint uint) +(define-read-only (get-y-given-price (token ) (collateral ) (expiry uint) (price 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)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (weight-x (get weight-x pool)) + (weight-y (get weight-y pool)) + ) + (contract-call? .weighted-equation get-y-given-price balance-x balance-y weight-x weight-y price) + ) ) -;; 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 }) - ) +;; @desc units of yield-/key-token to be minted given amount of collateral being added (single sided liquidity) +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param dx; amount of collateral being added +;; @returns (response (tuple uint uint) uint) +(define-read-only (get-token-given-position (token ) (collateral ) (expiry uint) (dx uint)) + (begin + (asserts! (< (* block-height ONE_8) expiry) ERR-EXPIRY) + (let + ( + (ltv (try! (get-ltv token collateral expiry))) + (dy (if (is-eq (contract-of token) (contract-of collateral)) + dx + (try! (contract-call? .fixed-weight-pool get-x-y token collateral u50000000 u50000000 dx)) + ) + ) + (ltv-dy (mul-down ltv dy)) + ) + + (ok {yield-token: ltv-dy, key-token: ltv-dy}) + ) + ) ) -(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) }) - ) +;; @desc units of token/collateral required to mint given units of yield-/key-token +;; @desc returns dx (single liquidity) based on dx-weighted and dy-weighted +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param shares; units of yield-/key-token to be minted +;; @returns (response (tuple uint uint uint) uint) +(define-read-only (get-position-given-mint (token ) (collateral ) (expiry uint) (shares uint)) + (begin + (asserts! (< (* block-height ONE_8) expiry) ERR-EXPIRY) ;; mint supported until, but excl., expiry + (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)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (total-supply (get yield-supply pool)) ;; prior to maturity, yield-supply == key-supply, so we use yield-supply + (weight-x (get weight-x pool)) + (weight-y (get weight-y pool)) + + (ltv (try! (get-ltv token collateral expiry))) + + (pos-data (unwrap! (contract-call? .weighted-equation get-position-given-mint balance-x balance-y weight-x weight-y total-supply shares) ERR-WEIGHTED-EQUATION-CALL)) + + (dx-weighted (get dx pos-data)) + (dy-weighted (get dy pos-data)) + + ;; always convert to collateral ccy + (dy-to-dx (if (is-eq token-x token-y) + dy-weighted + (try! (contract-call? .fixed-weight-pool get-x-y token collateral u50000000 u50000000 dy-weighted)) + ) + ) + (dx (+ dx-weighted dy-to-dx)) + ) + (ok {dx: dx, dx-weighted: dx-weighted, dy-weighted: dy-weighted}) + ) + ) ) -;; 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 }) - ) +;; @desc units of token/collateral to be returned after burning given units of yield-/key-token +;; @param token; borrow token +;; @param collateral; collateral token +;; @param expiry; borrow expiry +;; @param shares; units of yield-/key-token to be burnt +;; @returns (response (tuple uint uint) uint) +(define-read-only (get-position-given-burn-key (token ) (collateral ) (expiry uint) (shares uint)) + (begin + (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)) + (balance-x (get balance-x pool)) + (balance-y (get balance-y pool)) + (yield-supply (get yield-supply pool)) + (key-supply (get key-supply pool)) + (weight-x (get weight-x pool)) + (weight-y (get weight-y pool)) + (pool-value-unfloored (try! (get-pool-value-in-token token collateral expiry))) + (pool-value-in-y (if (> yield-supply pool-value-unfloored) yield-supply pool-value-unfloored)) + (key-value-in-y (if (<= pool-value-in-y yield-supply) u0 (- pool-value-in-y yield-supply))) + (key-to-pool (div-down key-value-in-y pool-value-in-y)) + (shares-to-key (div-down shares key-supply)) + (shares-to-pool (mul-down key-to-pool shares-to-key)) + + (dx (mul-down shares-to-pool balance-x)) + (dy (mul-down shares-to-pool balance-y)) + ) + (ok {dx: dx, dy: dy}) + ) + ) ) -;; 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) .fwp-wbtc-usda-50-50) + +;; math-fixed-point +;; Fixed Point Math +;; following https://github.com/balancer-labs/balancer-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)) +(define-constant SCALE_DOWN_OVERFLOW (err u5002)) +(define-constant ADD_OVERFLOW (err u5003)) +(define-constant SUB_OVERFLOW (err u5004)) +(define-constant MUL_OVERFLOW (err u5005)) +(define-constant DIV_OVERFLOW (err u5006)) +(define-constant POW_OVERFLOW (err u5007)) + +;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, +;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error +;; (i.e. the last digit of the result may be completely lost to this error). +(define-constant MAX_POW_RELATIVE_ERROR u4) + +;; public functions +;; + +(define-read-only (get_one) + (ok ONE_8) ) +(define-read-only (scale-up (a uint)) + (* a ONE_8) +) -;; 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) - ) - ([1;4;31mlet ( - (proposer-balance (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-balance tx-sender))) - (total-supply (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-total-supply))) - (proposal-id (+ u1 (var-get proposal-count))) - ) +(define-read-only (scale-down (a uint)) + (/ a ONE_8) +) - ;; 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-read-only (mul-down (a uint) (b uint)) + (/ (* a b) ONE_8) ) -(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)}) +(define-read-only (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) - (ok amount) - +(define-read-only (div-down (a uint) (b uint)) + (if (is-eq a u0) + u0 + (/ (* a ONE_8) b) ) - ) - -(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)}) +(define-read-only (div-up (a uint) (b uint)) + (if (is-eq a u0) + u0 + (+ u1 (/ (- (* a ONE_8) u1) b)) + ) +) - (ok amount) +(define-read-only (pow-down (a uint) (b uint)) + (let + ( + (raw (unwrap-panic (pow-fixed a b))) + (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) + ) + (if (< raw max-error) + u0 + (- raw max-error) + ) ) - +) + +(define-read-only (pow-up (a uint) (b uint)) + (let + ( + (raw (unwrap-panic (pow-fixed a b))) + (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) + ) + (+ raw max-error) ) +) -(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? .fwp-wbtc-usda-50-50 get-total-supply))) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) +;; math-log-exp +;; Exponentiation and logarithm functions for 8 decimal fixed point numbers (both base and exponent/argument). +;; Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural +;; exponentiation and logarithm (where the base is Euler's number). +;; Reference: https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/LogExpMath.sol +;; MODIFIED: because we use only 128 bits instead of 256, we cannot do 20 decimal or 36 decimal accuracy like in Balancer. + +;; constants +;; +;; All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying +;; two numbers, and multiply by ONE when dividing them. +;; All arguments and return values are 8 decimal fixed point numbers. +(define-constant iONE_8 (pow 10 8)) +(define-constant ONE_10 (pow 10 10)) + +;; The domain of natural exponentiation is bound by the word size and number of decimals used. +;; The largest possible result is (2^127 - 1) / 10^8, +;; which makes the largest exponent ln((2^127 - 1) / 10^8) = 69.6090111872. +;; The smallest possible result is 10^(-8), which makes largest negative argument ln(10^(-8)) = -18.420680744. +;; We use 69.0 and -18.0 to have some safety margin. +(define-constant MAX_NATURAL_EXPONENT (* 69 iONE_8)) +(define-constant MIN_NATURAL_EXPONENT (* -18 iONE_8)) + +(define-constant MILD_EXPONENT_BOUND (/ (pow u2 u126) (to-uint iONE_8))) + +;; Because largest exponent is 69, we start from 64 +;; The first several a_n are too large if stored as 8 decimal numbers, and could cause intermediate overflows. +;; Instead we store them as plain integers, with 0 decimals. +(define-constant x_a_list_no_deci (list +{x_pre: 6400000000, a_pre: 6235149080811616882910000000, use_deci: false} ;; x1 = 2^6, a1 = e^(x1) +)) +;; 8 decimal constants +(define-constant x_a_list (list +{x_pre: 3200000000, a_pre: 7896296018268069516100, use_deci: true} ;; x2 = 2^5, a2 = e^(x2) +{x_pre: 1600000000, a_pre: 888611052050787, use_deci: true} ;; x3 = 2^4, a3 = e^(x3) +{x_pre: 800000000, a_pre: 298095798704, use_deci: true} ;; x4 = 2^3, a4 = e^(x4) +{x_pre: 400000000, a_pre: 5459815003, use_deci: true} ;; x5 = 2^2, a5 = e^(x5) +{x_pre: 200000000, a_pre: 738905610, use_deci: true} ;; x6 = 2^1, a6 = e^(x6) +{x_pre: 100000000, a_pre: 271828183, use_deci: true} ;; x7 = 2^0, a7 = e^(x7) +{x_pre: 50000000, a_pre: 164872127, use_deci: true} ;; x8 = 2^-1, a8 = e^(x8) +{x_pre: 25000000, a_pre: 128402542, use_deci: true} ;; x9 = 2^-2, a9 = e^(x9) +{x_pre: 12500000, a_pre: 113314845, use_deci: true} ;; x10 = 2^-3, a10 = e^(x10) +{x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) +)) + +(define-constant X_OUT_OF_BOUNDS (err u5009)) +(define-constant Y_OUT_OF_BOUNDS (err u5010)) +(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant INVALID_EXPONENT (err u5012)) +(define-constant OUT_OF_BOUNDS (err u5013)) + +;; private functions +;; + +;; Internal natural logarithm (ln(a)) with signed 8 decimal fixed point argument. +(define-private (ln-priv (a int)) + (let + ( + (a_sum_no_deci (fold accumulate_division x_a_list_no_deci {a: a, sum: 0})) + (a_sum (fold accumulate_division x_a_list {a: (get a a_sum_no_deci), sum: (get sum a_sum_no_deci)})) + (out_a (get a a_sum)) + (out_sum (get sum a_sum)) + (z (/ (* (- out_a iONE_8) iONE_8) (+ out_a iONE_8))) + (z_squared (/ (* z z) iONE_8)) + (div_list (list 3 5 7 9 11)) + (num_sum_zsq (fold rolling_sum_div div_list {num: z, seriesSum: z, z_squared: z_squared})) + (seriesSum (get seriesSum num_sum_zsq)) + (r (+ out_sum (* seriesSum 2))) + ) + (ok r) + ) +) - (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) +(define-private (accumulate_division (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_a_sum (tuple (a int) (sum int)))) + (let + ( + (a_pre (get a_pre x_a_pre)) + (x_pre (get x_pre x_a_pre)) + (use_deci (get use_deci x_a_pre)) + (rolling_a (get a rolling_a_sum)) + (rolling_sum (get sum rolling_a_sum)) + ) + (if (>= rolling_a (if use_deci a_pre (* a_pre iONE_8))) + {a: (/ (* rolling_a (if use_deci iONE_8 1)) a_pre), sum: (+ rolling_sum x_pre)} + {a: rolling_a, sum: rolling_sum} + ) + ) +) - (map-set proposals - { id: proposal-id } - (merge proposal { is-open: false })) +(define-private (rolling_sum_div (n int) (rolling (tuple (num int) (seriesSum int) (z_squared int)))) + (let + ( + (rolling_num (get num rolling)) + (rolling_sum (get seriesSum rolling)) + (z_squared (get z_squared rolling)) + (next_num (/ (* rolling_num z_squared) iONE_8)) + (next_sum (+ rolling_sum (/ next_num n))) + ) + {num: next_num, seriesSum: next_sum, z_squared: z_squared} + ) +) - ;; Execute the proposal when the yes-vote passes threshold-count. - (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true) +;; Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to +;; arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means +;; x^y = exp(y * ln(x)). +;; Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`. +(define-private (pow-priv (x uint) (y uint)) + (let + ( + (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)) ) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) + ) ) -;; 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 +(define-private (exp-pos (x int)) + (begin + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (let + ( + ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct + ;; it and compute the accumulated product. + (x_product_no_deci (fold accumulate_product x_a_list_no_deci {x: x, product: 1})) + (x_adj (get x x_product_no_deci)) + (firstAN (get product x_product_no_deci)) + (x_product (fold accumulate_product x_a_list {x: x_adj, product: iONE_8})) + (product_out (get product x_product)) + (x_out (get x x_product)) + (seriesSum (+ iONE_8 x_out)) + (div_list (list 2 3 4 5 6 7 8 9 10 11 12)) + (term_sum_x (fold rolling_div_sum div_list {term: x_out, seriesSum: seriesSum, x: x_out})) + (sum (get seriesSum term_sum_x)) + ) + (ok (* (/ (* product_out sum) iONE_8) firstAN)) + ) + ) +) + +(define-private (accumulate_product (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_x_p (tuple (x int) (product int)))) + (let ( - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token))) - (proposal (get-proposal-by-id proposal-id)) - ) + (x_pre (get x_pre x_a_pre)) + (a_pre (get a_pre x_a_pre)) + (use_deci (get use_deci x_a_pre)) + (rolling_x (get x rolling_x_p)) + (rolling_product (get product rolling_x_p)) + ) + (if (>= rolling_x x_pre) + {x: (- rolling_x x_pre), product: (/ (* rolling_product a_pre) (if use_deci iONE_8 1))} + {x: rolling_x, product: rolling_product} + ) + ) +) - (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) +(define-private (rolling_div_sum (n int) (rolling (tuple (term int) (seriesSum int) (x int)))) + (let + ( + (rolling_term (get term rolling)) + (rolling_sum (get seriesSum rolling)) + (x (get x rolling)) + (next_term (/ (/ (* rolling_term x) iONE_8) n)) + (next_sum (+ rolling_sum next_term)) + ) + {term: next_term, seriesSum: next_sum, x: x} + ) +) - ;; Return the pool token - (try! (as-contract (contract-call? token transfer token-count (as-contract tx-sender) member none))) - (ok true) +;; public functions +;; + +(define-read-only (get-exp-bound) + (ok MILD_EXPONENT_BOUND) +) + +;; Exponentiation (x^y) with unsigned 8 decimal fixed point base and exponent. +(define-read-only (pow-fixed (x uint) (y uint)) + (begin + ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. + (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + + ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. + (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + + (if (is-eq y u0) + (ok (to-uint iONE_8)) + (if (is-eq x u0) + (ok u0) + (pow-priv x y) + ) + ) ) ) -;; 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)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .fixed-weight-pool set-fee-rate-x .token-wbtc .token-usda u50000000 u50000000 new-fee-rate-x)) - (try! (contract-call? .fixed-weight-pool set-fee-rate-y .token-wbtc .token-usda u50000000 u50000000 new-fee-rate-y)) - - (ok true) +;; Natural exponentiation (e^x) with signed 8 decimal fixed point exponent. +;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. +(define-read-only (exp-fixed (x int)) + (begin + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (if (< x 0) + ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it + ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). + ;; Fixed point division requires multiplying by iONE_8. + (ok (/ (* iONE_8 iONE_8) (unwrap-panic (exp-pos (* -1 x))))) + (exp-pos x) + ) ) ) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.lbp-alex-usda-90-10' -(impl-trait .trait-multisig-vote.multisig-vote-trait) + +;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. +(define-read-only (log-fixed (arg int) (base int)) + ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). + (let + ( + (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) + (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) + ) + (ok (/ (* logArg iONE_8) logBase)) + ) +) + +;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. +(define-read-only (ln-fixed (a int)) + (begin + (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (if (< a iONE_8) + ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). + ;; If a is less than one, 1/a will be greater than one. + ;; Fixed point division requires multiplying by iONE_8. + (ok (- 0 (unwrap-panic (ln-priv (/ (* iONE_8 iONE_8) a))))) + (ln-priv a) + ) + ) +) +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.collateral-rebalancing-pool' (use-trait ft-trait .trait-sip-010.sip-010-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(use-trait flash-loan-user-trait .trait-flash-loan-user.flash-loan-user-trait) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (roll-position (token ) (collateral ) (expiry uint) (the-key-token ) (flash-loan-user ) (expiry-to-roll uint)) + (let + ( + (reduce-data (try! (contract-call? .collateral-rebalancing-pool reduce-position-key token collateral expiry the-key-token ONE_8))) + (collateral-amount (get dx reduce-data)) + (token-amount (get dy reduce-data)) + (token-to-collateral  + (if (is-eq token-amount u0)  + u0  + (try! (contract-call? .fixed-weight-pool swap collateral token u50000000 u50000000 token-amount none))  + ) + ) + ) + (contract-call? .alex-vault flash-loan flash-loan-user collateral (+ collateral-amount token-to-collateral) (some expiry)) + ) +) +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.collateral-rebalancing-pool' +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) ;; Alex voting for MultiSig DAO @@ -594,6 +2532,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 { id: uint, proposer: principal, + expiry: uint, title: (string-utf8 256), url: (string-utf8 256), is-open: bool, @@ -614,7 +2553,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (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 }) +(define-map tokens-by-member { proposal-id: uint, member: principal, token: principal, expiry: uint } { amount: uint }) ;; Get all proposals in detail (define-read-only (get-proposals) @@ -634,10 +2573,10 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 ) ) -(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token )) +(define-read-only (get-tokens-by-member-by-id (proposal-id uint) (member principal) (token ) (expiry uint)) (default-to { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token) }) + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) ) ) @@ -647,6 +2586,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 { id: u0, proposer: DEFAULT_OWNER, + expiry: u0, title: u"", url: u"", is-open: false, @@ -654,66 +2594,73 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 end-block-height: u0, yes-votes: u0, no-votes: u0, - new-fee-rate-x: u0, - new-fee-rate-y: u0 + new-fee-rate-x: u0, ;; Default token feerate + new-fee-rate-y: u0 ;; default yield-token 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) .lbp-alex-usda-90-10) +(define-read-only (is-token-accepted (token )) + (or (is-eq (contract-of token) .yield-usda) (is-eq (contract-of token) .key-usda-wbtc)) ) ;; Start a proposal ;; Requires 10% of the supply in your wallet ;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose +(define-public (propose + (expiry uint) (start-block-height uint) (title (string-utf8 256)) (url (string-utf8 256)) (new-fee-rate-x uint) (new-fee-rate-y uint) ) - ([1;4;31mlet ( - (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))) - ) + (let + ( + (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda get-balance expiry tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance expiry tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) + (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) + (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) + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + expiry: expiry, + 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)) +(define-public (vote-for (token ) (proposal-id uint) (amount uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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))) - + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token expiry))) ) ;; Can vote with corresponding pool token @@ -724,7 +2671,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (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)) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -733,7 +2680,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 { 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) } + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token), expiry: expiry } { amount: (+ amount token-count)}) (ok amount) @@ -741,11 +2688,12 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 ) ) -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) (let ( (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) (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))) + (token-count (get amount (get-tokens-by-member-by-id proposal-id tx-sender token expiry))) ) ;; Can vote with corresponding pool token (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) @@ -754,7 +2702,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 ;; 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)) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -764,21 +2712,25 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 { 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) } + { proposal-id: proposal-id, member: tx-sender, token: (contract-of token) , expiry: expiry } { 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 (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) - (yes-votes (get yes-votes proposal)) - ) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) + (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) @@ -788,19 +2740,19 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 { 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) - ) + ;; 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)) +(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)) + (expiry (get expiry proposal)) + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) ) (asserts! (is-token-accepted token) ERR-INVALID-POOL-TOKEN) @@ -808,12 +2760,28 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (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))) + (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) -Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda' + +;; Make needed contract changes on DAO +(define-private (execute-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc expiry new-fee-rate-y)) + + (ok true) + ) +) +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-token-pool' (impl-trait .trait-multisig-vote.multisig-vote-sft-trait) (use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) @@ -936,35 +2904,35 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (new-fee-rate-token uint) (new-fee-rate-yield-token uint) ) - ([1;4;31mlet ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda get-balance expiry tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda get-balance expiry tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - expiry: expiry, - 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-yield-token: new-fee-rate-yield-token - } - ) - (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) + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + expiry: expiry, + 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-yield-token: new-fee-rate-yield-token + } + ) + (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) ) ) @@ -984,7 +2952,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -1018,7 +2986,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 ;; 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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -1074,30 +3042,28 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) + (try! (as-contract (contract-call? token transfer expiry 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)) - (expiry (get expiry proposal)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) - ) + ([1;4;31mlet ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) + ) ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-usda new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-usda new-fee-rate-yield-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-usda new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-usda new-fee-rate-yield-token)) - (ok true) + (ok true) ) ) -Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification -Analysis error: invalid signature for method 'burn-fixed' regarding trait's specification -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc' +Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-token-pool' (impl-trait .trait-multisig-vote.multisig-vote-sft-trait) (use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) @@ -1220,35 +3186,35 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (new-fee-rate-token uint) (new-fee-rate-yield-token uint) ) - ([1;4;31mlet ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance expiry tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) ONE_8)) - (proposal-id (+ u1 (var-get proposal-count))) - ) + (let ( + (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance expiry tx-sender)) ONE_8)) + (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) ONE_8)) + (proposal-id (+ u1 (var-get proposal-count))) + ) ;; Requires 10% of the supply - (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) + (asserts! (>= (* proposer-balance u10) total-supply) ERR-NOT-ENOUGH-BALANCE) ;; Mutate - (map-set proposals - { id: proposal-id } - { - id: proposal-id, - proposer: tx-sender, - expiry: expiry, - 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-yield-token: new-fee-rate-yield-token - } - ) - (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) + (map-set proposals + { id: proposal-id } + { + id: proposal-id, + proposer: tx-sender, + expiry: expiry, + 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-yield-token: new-fee-rate-yield-token + } + ) + (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) ) ) @@ -1268,7 +3234,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -1302,7 +3268,7 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 ;; 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 expiry amount tx-sender (as-contract tx-sender))) + (try! (contract-call? token transfer expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -1358,24 +3324,24 @@ Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5 (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member))) + (try! (as-contract (contract-call? token transfer expiry 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)) - (expiry (get expiry proposal)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) - ) + ([1;4;31mlet ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (new-fee-rate-token (get new-fee-rate-token proposal)) + (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) + ) ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-wbtc new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-wbtc new-fee-rate-yield-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-wbtc new-fee-rate-token)) + (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-wbtc new-fee-rate-yield-token)) - (ok true) + (ok true) ) ) diff --git a/clarity/tests/models/alex-tests-multisigs.ts b/clarity/tests/models/alex-tests-multisigs.ts index ded487ab..88e650cf 100644 --- a/clarity/tests/models/alex-tests-multisigs.ts +++ b/clarity/tests/models/alex-tests-multisigs.ts @@ -66,7 +66,7 @@ export { MS_FWP_WBTC_USDA_5050 }; -class MS_YTP_WBT_59760 { +class MS_YTP_YIELD_WBTC { chain: Chain; deployer: Account; @@ -75,9 +75,10 @@ constructor(chain: Chain, deployer: Account) { this.deployer = deployer; } -propose(startBlockHeight: number, proposeTitle: string, proposeURL: string, feeRateX: number, feeRateY: number) { +propose(expiry: number, startBlockHeight: number, proposeTitle: string, proposeURL: string, feeRateX: number, feeRateY: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-ytp-yield-wbtc-59760-wbtc", "propose", [ + Tx.contractCall("multisig-ytp-yield-wbtc", "propose", [ + types.uint(expiry), types.uint(startBlockHeight), types.utf8(proposeTitle), types.utf8(proposeURL), @@ -90,7 +91,7 @@ propose(startBlockHeight: number, proposeTitle: string, proposeURL: string, feeR voteFor(contractCaller: Account, token: string, proposalID: number, amount: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-ytp-yield-wbtc-59760-wbtc", "vote-for", [ + Tx.contractCall("multisig-ytp-yield-wbtc", "vote-for", [ types.principal(token), types.uint(proposalID), types.uint(amount) @@ -101,7 +102,7 @@ voteFor(contractCaller: Account, token: string, proposalID: number, amount: numb voteAgainst(contractCaller: Account, token: string, proposalID: number, amount: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-ytp-yield-wbtc-59760-wbtc", "vote-against", [ + Tx.contractCall("multisig-ytp-yield-wbtc", "vote-against", [ types.principal(token), types.uint(proposalID), types.uint(amount) @@ -112,17 +113,17 @@ voteAgainst(contractCaller: Account, token: string, proposalID: number, amount: endProposal(proposalID: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-ytp-yield-wbtc-59760-wbtc", "end-proposal", [ + Tx.contractCall("multisig-ytp-yield-wbtc", "end-proposal", [ types.uint(proposalID), ], this.deployer.address), ]); return block.receipts[0].result; } } -export { MS_YTP_WBT_59760 }; +export { MS_YTP_YIELD_WBTC }; -class MS_CRP_WBTC_USDA_59760 { +class MS_CRP_USDA_WBTC { chain: Chain; deployer: Account; @@ -131,9 +132,10 @@ constructor(chain: Chain, deployer: Account) { this.deployer = deployer; } -propose(contractCaller: Account, startBlockHeight: number, proposeTitle: string, proposeURL: string, feeRateX: number, feeRateY: number) { +propose(contractCaller: Account, expiry: number, startBlockHeight: number, proposeTitle: string, proposeURL: string, feeRateX: number, feeRateY: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-crp-wbtc-59760-usda", "propose", [ + Tx.contractCall("multisig-crp-usda-wbtc", "propose", [ + types.uint(expiry), types.uint(startBlockHeight), types.utf8(proposeTitle), types.utf8(proposeURL), @@ -146,7 +148,7 @@ propose(contractCaller: Account, startBlockHeight: number, proposeTitle: string, voteFor(contractCaller: Account, token: string, proposalID: number, amount: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-crp-wbtc-59760-usda", "vote-for", [ + Tx.contractCall("multisig-crp-usda-wbtc", "vote-for", [ types.principal(token), types.uint(proposalID), types.uint(amount) @@ -157,7 +159,7 @@ voteFor(contractCaller: Account, token: string, proposalID: number, amount: numb voteAgainst(contractCaller: Account, token: string, proposalID: number, amount: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-crp-wbtc-59760-usda", "vote-against", [ + Tx.contractCall("multisig-crp-usda-wbtc", "vote-against", [ types.principal(token), types.uint(proposalID), types.uint(amount) @@ -168,11 +170,11 @@ voteAgainst(contractCaller: Account, token: string, proposalID: number, amount: endProposal(proposalID: number) { let block = this.chain.mineBlock([ - Tx.contractCall("multisig-crp-wbtc-59760-usda", "end-proposal", [ + Tx.contractCall("multisig-crp-usda-wbtc", "end-proposal", [ types.uint(proposalID), ], this.deployer.address), ]); return block.receipts[0].result; } } -export { MS_CRP_WBTC_USDA_59760 }; \ No newline at end of file +export { MS_CRP_USDA_WBTC }; \ No newline at end of file diff --git a/clarity/tests/models/alex-tests-tokens.ts b/clarity/tests/models/alex-tests-tokens.ts index d7dbf728..49fe0220 100644 --- a/clarity/tests/models/alex-tests-tokens.ts +++ b/clarity/tests/models/alex-tests-tokens.ts @@ -77,7 +77,7 @@ export { WBTCToken }; -class POOLTOKEN_FWP_WBTC_USDA_5050 { +class FWP_WBTC_USDA_5050 { chain: Chain; deployer: Account; @@ -96,9 +96,9 @@ class POOLTOKEN_FWP_WBTC_USDA_5050 { return this.chain.callReadOnlyFn("fwp-wbtc-usda-50-50", "get-total-supply", [], this.deployer.address); } } -export { POOLTOKEN_FWP_WBTC_USDA_5050 }; +export { FWP_WBTC_USDA_5050 }; -class POOLTOKEN_YTP_WBTC_WBTC_59760 { +class YTP_YIELD_WBTC { chain: Chain; deployer: Account; @@ -107,41 +107,21 @@ class POOLTOKEN_YTP_WBTC_WBTC_59760 { this.deployer = deployer; } - balanceOf(wallet: string) { - return this.chain.callReadOnlyFn("ytp-yield-wbtc-59760-wbtc", "get-balance", [ - types.principal(wallet), + balanceOf(expiry: number, wallet: string) { + return this.chain.callReadOnlyFn("ytp-yield-wbtc", "get-balance", [ + types.uint(expiry), types.principal(wallet), ], this.deployer.address); } - totalSupply() { - return this.chain.callReadOnlyFn("ytp-yield-wbtc-59760-wbtc", "get-total-supply", [], this.deployer.address); - } -} -export { POOLTOKEN_YTP_WBTC_WBTC_59760 }; - -class YIELD_WBTC_59760 { - chain: Chain; - deployer: Account; - - constructor(chain: Chain, deployer: Account) { - this.chain = chain; - this.deployer = deployer; - } - - balanceOf(wallet: string) { - return this.chain.callReadOnlyFn("yield-wbtc-59760", "get-balance", [ - types.principal(wallet), + totalSupply(expiry: number) { + return this.chain.callReadOnlyFn("ytp-yield-wbtc", "get-total-supply", [ + types.uint(expiry) ], this.deployer.address); } - - totalSupply() { - return this.chain.callReadOnlyFn("yield-wbtc-59760", "get-total-supply", [], this.deployer.address); - } - } -export { YIELD_WBTC_59760 }; +export { YTP_YIELD_WBTC }; -class KEY_WBTC_59760_USDA { +class KEY_USDA_WBTC { chain: Chain; deployer: Account; @@ -150,17 +130,20 @@ class KEY_WBTC_59760_USDA { this.deployer = deployer; } - balanceOf(wallet: string) { - return this.chain.callReadOnlyFn("key-wbtc-59760-usda", "get-balance", [ + balanceOf(expiry: number, wallet: string) { + return this.chain.callReadOnlyFn("key-usda-wbtc", "get-balance", [ + types.uint(expiry), types.principal(wallet), ], this.deployer.address); } - totalSupply() { - return this.chain.callReadOnlyFn("key-wbtc-59760-usda", "get-total-supply", [], this.deployer.address); + totalSupply(expiry: number) { + return this.chain.callReadOnlyFn("key-usda-wbtc", "get-total-supply", [ + types.uint(expiry) + ], this.deployer.address); } } -export { KEY_WBTC_59760_USDA }; +export { KEY_USDA_WBTC }; class YIELD_WBTC { chain: Chain; @@ -171,11 +154,11 @@ class YIELD_WBTC { this.deployer = deployer; } - mint(expiry: number, amount: number, recipient: string) { + mintFixed(expiry: number, amount: number, recipient: string) { let block = this.chain.mineBlock([ - Tx.contractCall("yield-wbtc", "transfer", [ + Tx.contractCall("yield-wbtc", "mint-fixed", [ types.uint(expiry), - types.uint(amount,) + types.uint(amount), types.principal(recipient) ], this.deployer.address), ]); diff --git a/clarity/tests/models/alex-tests-yield-token-pool.ts b/clarity/tests/models/alex-tests-yield-token-pool.ts index bd59b4af..61eadabd 100644 --- a/clarity/tests/models/alex-tests-yield-token-pool.ts +++ b/clarity/tests/models/alex-tests-yield-token-pool.ts @@ -102,7 +102,7 @@ import { let block = this.chain.mineBlock([ Tx.contractCall("yield-token-pool", "reduce-position", [ types.uint(expiry), - types.principal(aytoken), + types.principal(aytoken), types.principal(token), types.principal(pooltoken), types.uint(percent) diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index 97cf51d8..c89c6d2f 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -5,9 +5,9 @@ import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts'; import { YTPTestAgent1, } from './models/alex-tests-yield-token-pool.ts'; -import { MS_YTP_WBT_59760 } from './models/alex-tests-multisigs.ts'; +import { MS_YTP_YIELD_WBTC } from './models/alex-tests-multisigs.ts'; -import { USDAToken, WBTCToken, YIELD_WBTC } from './models/alex-tests-tokens.ts'; +import { USDAToken, WBTCToken, YIELD_WBTC, YTP_YIELD_WBTC } from './models/alex-tests-tokens.ts'; // Deployer Address Constants @@ -32,9 +32,13 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; let YTPTest = new YTPTestAgent1(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + let result = yieldWBTC.mintFixed(expiry, 100000 * ONE_8, deployer.address); + result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print @@ -394,8 +398,8 @@ Clarinet.test({ let wallet_2 = accounts.get("wallet_2")!; let contractOwner = accounts.get("deployer")!; let YTPTest = new YTPTestAgent1(chain, deployer); - let MultiSigTest = new MS_YTP_WBT_59760(chain, deployer); - let ytpPoolToken = new POOLTOKEN_YTP_WBTC_WBTC_59760(chain, deployer); + let MultiSigTest = new MS_YTP_YIELD_WBTC(chain, deployer); + let ytpPoolToken = new YTP_YIELD_WBTC(chain, deployer); let usdaToken = new USDAToken(chain, deployer); let wbtcToken = new WBTCToken(chain, deployer); const buffer = new ArrayBuffer(34) @@ -436,21 +440,21 @@ Clarinet.test({ position['balance-yield-token'].expectUint(ONE_8); position['balance-virtual'].expectUint(1010*ONE_8); - call = await ytpPoolToken.balanceOf(deployer.address); + call = await ytpPoolToken.balanceOf(expiry, deployer.address); call.result.expectOk().expectUint(100000000000); // u100000000000 - call = await ytpPoolToken.balanceOf(wallet_2.address); + call = await ytpPoolToken.balanceOf(expiry, wallet_2.address); call.result.expectOk().expectUint(1000000000); // Fee rate Setting Proposal of Multisig - result = MultiSigTest.propose(1000, " Fee Rate Setting to 10%", " https://docs.alexgo.io", feeRateX, feeRateY) + result = MultiSigTest.propose(expiry, 1000, " Fee Rate Setting to 10%", " https://docs.alexgo.io", feeRateX, feeRateY) result.expectOk().expectUint(1) // First Proposal // Block 1000 mining chain.mineEmptyBlock(1000); // Deployer has 99 % of pool token - let ROresult:any = ytpPoolToken.balanceOf(deployer.address); + let ROresult:any = ytpPoolToken.balanceOf(expiry, deployer.address); ROresult.result.expectOk().expectUint(100000000000); // Wallet_2 votes his 90% asset @@ -511,8 +515,8 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let wallet_2 = accounts.get("wallet_2")!; let YTPTest = new YTPTestAgent1(chain, deployer); - let MultiSigTest = new MS_YTP_WBT_59760(chain, deployer); - let ytpPoolToken = new POOLTOKEN_YTP_WBTC_WBTC_59760(chain, deployer); + let MultiSigTest = new MS_YTP_YIELD_WBTC(chain, deployer); + let ytpPoolToken = new YTP_YIELD_WBTC(chain, deployer); let usdaToken = new USDAToken(chain, deployer); let wbtcToken = new WBTCToken(chain, deployer); const buffer = new ArrayBuffer(0) // Optional memo From 9591e0eab2e89eb8b3047ab60c7c973bcfb22a14 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:30:22 +0800 Subject: [PATCH 09/25] working --- clarity/contracts/alex-vault.clar | 2 +- .../contracts/key-token/key-usda-wbtc.clar | 4 +- .../pool-token/fwp-wbtc-usda-50-50.clar | 2 +- .../pool-token/lbp-alex-usda-90-10.clar | 2 +- .../contracts/pool-token/ytp-yield-usda.clar | 6 +- .../contracts/pool-token/ytp-yield-wbtc.clar | 6 +- clarity/contracts/pool/yield-token-pool.clar | 18 +- .../traits/trait-semi-fungible-token.clar | 2 +- clarity/contracts/yield-token/yield-usda.clar | 4 +- clarity/contracts/yield-token/yield-wbtc.clar | 4 +- clarity/out | 3347 ----------------- clarity/tests/yield-token-pool_test.ts | 110 +- 12 files changed, 90 insertions(+), 3417 deletions(-) delete mode 100644 clarity/out diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index 62492a33..4d9784a7 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -77,7 +77,7 @@ (define-public (transfer-sft (token ) (token-id uint) (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (as-contract (unwrap! (contract-call? token transfer-fixed token-id amount tx-sender recipient) ERR-TRANSFER-FAILED)) + (as-contract (unwrap! (contract-call? token transfer-fixed token-id amount tx-sender recipient none) ERR-TRANSFER-FAILED)) (ok true) ) ) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index 335516b1..d3460f58 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -135,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance key-usda-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) - (transfer token-id (fixed-to-decimals amount) sender recipient) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) diff --git a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar index 11082d63..78054449 100644 --- a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar index b45b4db1..d4d79354 100644 --- a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar +++ b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index a43e0e18..21c0f6d7 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) @@ -135,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) - (transfer token-id (fixed-to-decimals amount) sender recipient) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar index b34b7f2b..7356e5b7 100644 --- a/clarity/contracts/pool-token/ytp-yield-wbtc.clar +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) @@ -135,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) - (transfer token-id (fixed-to-decimals amount) sender recipient) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index a6b98d29..1b199c51 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -18,7 +18,7 @@ (define-constant invalid-token-err (err u2007)) (define-constant ERR-NO-FEE (err u2005)) (define-constant ERR-NO-FEE-Y (err u2006)) -(define-constant invalid-ERR-EXPIRY (err u2009)) +(define-constant ERR-INVALID-EXPIRY (err u2009)) (define-constant fixed-point-err (err 5014)) (define-constant ERR-MATH-CALL (err u4003)) (define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) @@ -106,8 +106,8 @@ ;; @returns (response uint uint) (define-read-only (get-t (expiry uint) (listed uint)) (begin - (asserts! (> (var-get max-expiry) expiry) invalid-ERR-EXPIRY) - (asserts! (> (var-get max-expiry) (* block-height ONE_8)) invalid-ERR-EXPIRY) + (asserts! (> (var-get max-expiry) expiry) ERR-INVALID-EXPIRY) + (asserts! (> (var-get max-expiry) (* block-height ONE_8)) ERR-INVALID-EXPIRY) (let ( (t (div-down @@ -365,13 +365,13 @@ ;; at least one of dy must be greater than zero (asserts! (or (> new-dy-act u0) (> new-dy-vir u0)) ERR-INVALID-LIQUIDITY) ;; send x to vault - (unwrap! (contract-call? the-token transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) + (unwrap! (contract-call? the-token transfer-fixed dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) ;; send y to vault - (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer expiry new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) ;; mint pool token and send to tx-sender (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token mint expiry new-supply tx-sender)) + (try! (contract-call? the-pool-token mint-fixed expiry new-supply tx-sender)) (print { object: "pool", action: "liquidity-added", data: pool-updated }) (ok {supply: new-supply, balance-token: dx, balance-yield-token: new-dy-act, balance-virtual: new-dy-vir}) ) @@ -396,7 +396,7 @@ (balance-yield-token (get balance-yield-token pool)) (balance-virtual (get balance-virtual pool)) (total-supply (get total-supply pool)) - (total-shares (unwrap! (contract-call? the-pool-token get-balance-fixed expiry tx-sender) (err u11111))) + (total-shares (unwrap-panic (contract-call? the-pool-token get-balance-fixed expiry tx-sender))) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (reduce-data (try! (get-position-given-burn expiry the-yield-token shares))) (dx (get dx reduce-data)) @@ -417,7 +417,7 @@ (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy-act tx-sender))) (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token burn expiry shares tx-sender)) + (try! (contract-call? the-pool-token burn-fixed expiry shares tx-sender)) (print { object: "pool", action: "liquidity-removed", data: pool-updated }) (ok {dx: dx, dy: dy-act}) ) @@ -535,7 +535,7 @@ (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) + (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) ;; post setting diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar index f4fa0dea..4b2e6cff 100644 --- a/clarity/contracts/traits/trait-semi-fungible-token.clar +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -27,7 +27,7 @@ (burn (uint uint principal) (response bool uint)) ;; helper functions for 8-digit fixed notation - (transfer-fixed (uint uint principal principal) (response bool uint)) + (transfer-fixed (uint uint principal principal (optional (buff 34))) (response bool uint)) (get-balance-fixed (uint principal) (response uint uint)) (get-total-supply-fixed (uint) (response uint uint)) (get-total-supply-fixed (uint) (response uint uint)) diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index b5f90d47..123871db 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -135,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance yield-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) - (transfer token-id (fixed-to-decimals amount) sender recipient) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar index e33ff8b8..62aa6da8 100644 --- a/clarity/contracts/yield-token/yield-wbtc.clar +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -135,8 +135,8 @@ (ok (decimals-to-fixed (ft-get-balance yield-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) - (transfer token-id (fixed-to-decimals amount) sender recipient) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) (define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) diff --git a/clarity/out b/clarity/out deleted file mode 100644 index da31f850..00000000 --- a/clarity/out +++ /dev/null @@ -1,3347 +0,0 @@ - -Error: Analysis error: detected two execution paths, returning two different expression types (got '(response UnknownType uint)' and '(response UnknownType int)') -(impl-trait .trait-ownable.ownable-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) -(use-trait multisig-trait .trait-multisig-vote.multisig-vote-sft-trait) - -;; yield-token-pool -(define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places -(define-constant MAX_T u85000000) - -(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 invalid-token-err (err u2007)) -(define-constant ERR-NO-FEE (err u2005)) -(define-constant ERR-NO-FEE-Y (err u2006)) -(define-constant invalid-ERR-EXPIRY (err u2009)) -(define-constant fixed-point-err (err 5014)) -(define-constant ERR-MATH-CALL (err u4003)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) -(define-constant ERR-DY-BIGGER-THAN-AVAILABLE (err u2016)) -(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)) -(define-constant ERR-INVALID-POOL-TOKEN (err u2023)) -(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-data-var CONTRACT-OWNER principal tx-sender) - -(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)) - ) -) - -;; data maps and vars -(define-map pools-map - { pool-id: uint } - { - yield-token: principal, ;; yield-token, dy - expiry: uint - } -) - -(define-map pools-data-map - { - yield-token: principal, - expiry: uint - } - { - total-supply: uint, - balance-token: uint, ;; dx - balance-yield-token: uint, ;; dy_actual - balance-virtual: uint, ;; dy_virtual - fee-to-address: principal, - pool-token: principal, - fee-rate-token: uint, - fee-rate-yield-token: uint, - fee-rebate: uint, - listed: uint, - oracle-enabled: bool, - oracle-average: uint, - oracle-resilient: uint - } -) - -(define-data-var pool-count uint u0) -(define-data-var pools-list (list 2000 uint) (list)) - -;; 4 years based on 2102400 blocks per year (i.e. 15 secs per block) -(define-data-var max-expiry uint (scale-up u8409600)) - -;; @desc get-max-expiry -;; @returns uint -(define-read-only (get-max-expiry) - (var-get max-expiry) -) - -;; @desc set-max-expiry -;; @restricted CONTRACT-OWNER -;; @param new-max-expiry; new max-expiry -;; @returns (response bool uint) -(define-public (set-max-expiry (new-max-expiry uint)) - (begin - (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - (ok (var-set max-expiry new-max-expiry)) - ) -) - -;; @desc get-t -;; @desc get time-to-maturity as a function of max-expiry -;; @param expiry; when contract expiries -;; @param listed; when contract was listed -;; @returns (response uint uint) -(define-read-only (get-t (expiry uint) (listed uint)) - (begin - (asserts! (> (var-get max-expiry) expiry) invalid-ERR-EXPIRY) - (asserts! (> (var-get max-expiry) (* block-height ONE_8)) invalid-ERR-EXPIRY) - (let - ( - (t (div-down - (if (< expiry (* block-height ONE_8)) u0 (- expiry (* block-height ONE_8))) - (- (var-get max-expiry) listed))) - ) - (ok (if (< t MAX_T) t MAX_T)) ;; to avoid numerical error - ) - ) -) - -;; @desc get-pool-count -;; @returns uint -(define-read-only (get-pool-count) - (var-get pool-count) -) - -;; @desc get-pool-contracts -;; @param pool-id; pool-id -;; @returns (response (tutple) uint) -(define-read-only (get-pool-contracts (pool-id uint)) - (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) ERR-INVALID-POOL-ERR)) -) - -;; @desc get-pools -;; @returns map of get-pool-contracts -(define-read-only (get-pools) - (ok (map get-pool-contracts (var-get pools-list))) -) - -;; @desc get-pool-details -;; @param the-yield-token; yield-token -;; @returns (response (tuple) uint) -(define-read-only (get-pool-details (expiry uint) (the-yield-token )) - (ok (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) -) - -;; @desc get-yield -;; @desc note yield is not annualised -;; @param the-yield-token; yield-token -;; @returns (response uint uint) -(define-read-only (get-yield (expiry uint) (the-yield-token )) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry}) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (balance-token (get balance-token pool)) - (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) - (t-value (try! (get-t expiry listed))) - ) - (contract-call? .yield-token-equation get-yield balance-token balance-yield-token t-value) - ) -) - -;; @desc get-price -;; @param the-yield-token; yield-token -;; @returns (response uint uint) -(define-read-only (get-price (expiry uint) (the-yield-token )) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (balance-token (get balance-token pool)) - (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) - (t-value (try! (get-t expiry listed))) - ) - (contract-call? .yield-token-equation get-price balance-token balance-yield-token t-value) - ) -) - -;; @desc get-oracle-enabled -;; @param the-yield-token; yield-token -;; @returns (response bool uint) -(define-read-only (get-oracle-enabled (expiry uint) (the-yield-token )) - (ok (get oracle-enabled (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) -) - -;; @desc set-oracle-enabled -;; @desc oracle can only be enabled -;; @restricted CONTRACT-OWNER -;; @param the-yield-token; yield-token -;; @returns (response bool uint) -(define-public (set-oracle-enabled (expiry uint) (the-yield-token )) - (let - ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) 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 { yield-token: (contract-of the-yield-token), expiry: expiry } pool-updated) - (ok true) - ) -) - -;; @desc get-oracle-average -;; @desc returns the moving average used to determine oracle price -;; @param the-yield-token; yield-token -;; @returns (response uint uint) -(define-read-only (get-oracle-average (expiry uint) (the-yield-token )) - (ok (get oracle-average (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) -) - -;; @desc set-oracle-average -;; @restricted CONTRACT-OWNER -;; @param the-yield-token; yield-token -;; @returns (response bool uint) -(define-public (set-oracle-average (expiry uint) (the-yield-token ) (new-oracle-average uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (pool-updated (merge pool { - oracle-average: new-oracle-average, - oracle-resilient: (try! (get-oracle-instant expiry the-yield-token)) - })) - ) - (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 { yield-token: (contract-of the-yield-token), expiry: expiry } pool-updated) - (ok true) - ) -) - -;; @desc get-oracle-resilient -;; @desc price-oracle that is less up to date but more resilient to manipulation -;; @param the-yield-token; yield-token -;; @returns (response uint uint) -(define-read-only (get-oracle-resilient (expiry uint) (the-yield-token )) - (let - ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (asserts! (get oracle-enabled pool) ERR-ORACLE-NOT-ENABLED) - (ok (+ (mul-down (- ONE_8 (get oracle-average pool)) (try! (get-oracle-instant expiry the-yield-token))) - (mul-down (get oracle-average pool) (get oracle-resilient pool)))) - ) -) - -;; @desc get-oracle-instant -;; @desc price-oracle that is more up to date but less resilient to manipulation -;; @param the-yield-token; yield-token -;; @returns (response uint uint) -(define-read-only (get-oracle-instant (expiry uint) (the-yield-token )) - (ok (div-down ONE_8 (try! (get-price expiry the-yield-token)))) -) - -;; @desc create-pool -;; @restricted CONTRACT-OWNER -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param pool-token; pool token representing ownership of the pool -;; @param multisig-vote; DAO used by pool token holers -;; @param dx; amount of token added -;; @param dy; amount of yield-token added -;; @returns (response bool uint) -(define-public (create-pool (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (multisig-vote ) (dx uint) (dy uint)) - (begin - (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - ;; ;; create pool only if the correct pair - ;; (asserts! (is-eq (try! (contract-call? the-yield-token get-token)) (contract-of the-token)) ERR-INVALID-POOL-ERR) - (asserts! (is-none (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry })) ERR-POOL-ALREADY-EXISTS) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool-id (+ (var-get pool-count) u1)) - (pool-data { - total-supply: u0, - balance-token: u0, - balance-yield-token: u0, - balance-virtual: u0, - fee-to-address: (contract-of multisig-vote), - pool-token: (contract-of the-pool-token), - fee-rate-yield-token: u0, - fee-rate-token: u0, - fee-rebate: u0, - listed: (* block-height ONE_8), - oracle-enabled: false, - oracle-average: u0, - oracle-resilient: u0 - }) - ) - - (map-set pools-map { pool-id: pool-id } { yield-token: yield-token, expiry: expiry }) - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-data) - - (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) - (var-set pool-count pool-id) - - ;; ;; if yield-token added has a longer expiry than current max-expiry, update max-expiry (to expiry + one block). - ;; (var-set max-expiry (if (< (var-get max-expiry) expiry) (+ expiry ONE_8) (var-get max-expiry))) - (try! (add-to-position expiry the-yield-token the-token the-pool-token dx)) - - (print { object: "pool", action: "created", data: pool-data }) - (ok true) - ) - ) -) - -;; @desc buy-and-add-to-position -;; @desc helper function to buy required yield-token before adding position -;; @desc returns units of pool tokens minted, dx, dy-actual and dy-virtual added -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param pool-token; pool token representing ownership of the pool -;; @param dx; amount of token added (part of which will be used to buy yield-token) -;; @returns (response (tuple uint uint uint uint) uint) -(define-public (buy-and-add-to-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) - (let - ( - (dy-act (get dy-act (try! (get-token-given-position expiry the-yield-token dx)))) - (dx-adjusted (- dx (div-down dx (+ dx (try! (get-x-given-y expiry the-yield-token dy-act)))))) - (dx-to-buy-dy-adjusted (- dx dx-adjusted)) - ) - (and (> dy-act u0) (is-ok (swap-x-for-y expiry the-yield-token the-token dx-to-buy-dy-adjusted none))) - (add-to-position expiry the-yield-token the-token the-pool-token dx-adjusted) - ) -) - -;; @desc add-to-position -;; @desc returns units of pool tokens minted, dx, dy-actual and dy-virtual added -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param pool-token; pool token representing ownership of the pool -;; @param dx; amount of token added -;; @returns (response (tuple uint uint uint uint) uint) -(define-public (add-to-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (dx uint)) - (begin - ;; dx must be greater than zero - (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-token (get balance-token pool)) - (balance-yield-token (get balance-yield-token pool)) - (balance-virtual (get balance-virtual pool)) - (total-supply (get total-supply pool)) - (add-data (try! (get-token-given-position expiry the-yield-token dx))) - (new-supply (get token add-data)) - (new-dy-act (get dy-act add-data)) - (new-dy-vir (get dy-vir add-data)) - (pool-updated (merge pool { - total-supply: (+ new-supply total-supply), - balance-token: (+ balance-token dx), - balance-yield-token: (+ balance-yield-token new-dy-act), - balance-virtual: (+ balance-virtual new-dy-vir) - })) - ) - - (asserts! (is-eq (get pool-token pool) (contract-of the-pool-token)) ERR-INVALID-POOL-TOKEN) - - ;; at least one of dy must be greater than zero - (asserts! (or (> new-dy-act u0) (> new-dy-vir u0)) ERR-INVALID-LIQUIDITY) - ;; send x to vault - (unwrap! (contract-call? the-token transfer dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) - ;; send y to vault - (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer expiry new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) - - ;; mint pool token and send to tx-sender - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token mint expiry new-supply tx-sender)) - (print { object: "pool", action: "liquidity-added", data: pool-updated }) - (ok {supply: new-supply, balance-token: dx, balance-yield-token: new-dy-act, balance-virtual: new-dy-vir}) - ) - ) -) - -;; @desc reduce-position -;; @desc returns dx and dy-actual due to the position -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param pool-token; pool token representing ownership of the pool -;; @param percent; percentage of pool token held to reduce -;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (percent uint)) - (begin - (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-token (get balance-token pool)) - (balance-yield-token (get balance-yield-token pool)) - (balance-virtual (get balance-virtual pool)) - (total-supply (get total-supply pool)) - (total-shares (unwrap! (contract-call? the-pool-token get-balance-fixed expiry tx-sender) (err 11111))) - (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) - (reduce-data (try! (get-position-given-burn expiry the-yield-token shares))) - (dx (get dx reduce-data)) - (dy-act (get dy-act reduce-data)) - (dy-vir (get dy-vir reduce-data)) - (pool-updated (merge pool { - total-supply: (if (<= total-supply shares) u0 (- total-supply shares)), - balance-token: (if (<= balance-token dx) u0 (- balance-token dx)), - balance-yield-token: (if (<= balance-yield-token dy-act) u0 (- balance-yield-token dy-act)), - balance-virtual: (if (<= balance-virtual dy-vir) u0 (- balance-virtual dy-vir)) - }) - ) - ) - - (asserts! (is-eq (get pool-token pool) (contract-of the-pool-token)) ERR-INVALID-POOL-TOKEN) - - (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy-act u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy-act tx-sender))) - - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (try! (contract-call? the-pool-token burn expiry shares tx-sender)) - (print { object: "pool", action: "liquidity-removed", data: pool-updated }) - (ok {dx: dx, dy: dy-act}) - ) - ) -) - -;; @desc roll-position -;; @desc roll given liquidity position to another pool -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param pool-token; pool token representing ownership of the pool -;; @param percent; percentage of pool token held to reduce -;; @param the-yield-token-to-roll; yield token to roll -;; @param the-pool-token-to-roll; pool token representing ownership of the pool to roll to -;; @returns (response (tuple uint uint) uint) -(define-public (roll-position - (expiry uint) (the-yield-token ) (the-token ) (the-pool-token ) (percent uint) - (expiry-to-roll uint)) - (let - ( - (reduce-data (unwrap! (reduce-position expiry the-yield-token the-token the-pool-token percent) (err u11111))) - (dy-to-dx (get dx (unwrap! (swap-y-for-x expiry the-yield-token the-token (get dy reduce-data) none) (err u22222)))) - ) - (buy-and-add-to-position expiry-to-roll the-yield-token the-token the-pool-token (+ (get dx reduce-data) dy-to-dx)) - ) -) - -;; @desc swap-x-for-y -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param dx; amount of token to swap -;; @param min-dy; optional, min amount of yield-token to receive -;; @returns (response (tuple uint uint) uint) -(define-public (swap-x-for-y (expiry uint) (the-yield-token ) (the-token ) (dx uint) (min-dy (optional uint))) - (begin - (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-token (get balance-token pool)) - (balance-yield-token (get balance-yield-token pool)) - - ;; lambda ~= 1 - fee-rate-yield-token * yield - (yield (try! (get-yield expiry the-yield-token))) - (fee-yield (mul-down yield (get fee-rate-yield-token pool))) - (lambda (if (<= ONE_8 fee-yield) u0 (- ONE_8 fee-yield))) - (dx-net-fees (mul-down dx lambda)) - (fee (if (<= dx dx-net-fees) u0 (- dx dx-net-fees))) - (fee-rebate (mul-down fee (get fee-rebate pool))) - - (dy (try! (get-y-given-x expiry the-yield-token dx-net-fees))) - - (pool-updated - (merge pool - { - balance-token: (+ balance-token dx-net-fees fee-rebate), - balance-yield-token: (if (<= balance-yield-token dy) u0 (- balance-yield-token dy)), - oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient expiry the-yield-token)) u0) - } - ) - ) - ) - - (asserts! (< (default-to u0 min-dy) dy) ERR-EXCEEDS-MAX-SLIPPAGE) - - (and (> dx u0) (unwrap! (contract-call? the-token transfer-fixed dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED)) - (and (> dy u0) (try! (contract-call? .alex-vault transfer-sft the-yield-token expiry dy tx-sender))) - (try! (contract-call? .alex-reserve-pool add-to-balance (contract-of the-token) (- fee fee-rebate))) - - ;; post setting - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (print { object: "pool", action: "swap-x-for-y", data: pool-updated }) - (ok {dx: dx-net-fees, dy: dy}) - ) - ) -) - -;; @desc swap-y-for-x -;; @param the-yield-token; yield token -;; @param the-token; token -;; @param dy; amount of yield token to swap -;; @param min-dx; optional, min amount of token to receive -;; @returns (response (tuple uint uint) uint) -(define-public (swap-y-for-x (expiry uint) (the-yield-token ) (the-token ) (dy uint) (min-dx (optional uint))) - (begin - (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-token (get balance-token pool)) - (balance-yield-token (get balance-yield-token pool)) - - ;; lambda ~= 1 - fee-rate-token * yield - (yield (try! (get-yield expiry the-yield-token))) - (fee-yield (mul-down yield (get fee-rate-token pool))) - (lambda (if (<= ONE_8 fee-yield) u0 (- ONE_8 fee-yield))) - (dy-net-fees (mul-down dy lambda)) - (fee (if (<= dy dy-net-fees) u0 (- dy dy-net-fees))) - (fee-rebate (mul-down fee (get fee-rebate pool))) - - (dx (try! (get-x-given-y expiry the-yield-token dy-net-fees))) - - (pool-updated - (merge pool - { - balance-token: (if (<= balance-token dx) u0 (- balance-token dx)), - balance-yield-token: (+ balance-yield-token dy-net-fees fee-rebate), - oracle-resilient: (if (get oracle-enabled pool) (try! (get-oracle-resilient expiry the-yield-token)) u0) - } - ) - ) - ) - (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) - - (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) - (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) - - ;; post setting - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) - (print { object: "pool", action: "swap-y-for-x", data: pool-updated }) - (ok {dx: dx, dy: dy-net-fees}) - ) - ) -) - -;; @desc get-fee-rebate -;; @param the-yield-token; yield token -;; @returns (response uint uint) -(define-read-only (get-fee-rebate (expiry uint) (the-yield-token )) - (ok (get fee-rebate (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR))) -) - -;; @desc set-fee-rebate -;; @restricted CONTRACT-OWNER -;; @param the-yield-token; yield token -;; @param fee-rebate; new fee-rebate -;; @returns (response bool uint) -(define-public (set-fee-rebate (expiry uint) (the-yield-token ) (fee-rebate uint)) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rebate: fee-rebate })) - (ok true) - ) -) - -;; @desc get-fee-rate-yield-token -;; @param the-yield-token; yield token -;; @returns (response uint uint) -(define-read-only (get-fee-rate-yield-token (expiry uint) (the-yield-token )) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-rate-yield-token pool)) - ) -) - -;; @desc get-fee-rate-token -;; @param the-yield-token; yield token -;; @returns (response uint uint) -(define-read-only (get-fee-rate-token (expiry uint) (the-yield-token )) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-rate-token pool)) - ) -) - -;; @desc set-fee-rate-yield-token -;; @restricted fee-to-address -;; @param the-yield-token; yield token -;; @param fee-rate-yield-token; new fee-rate-yield-token -;; @returns (response bool uint) -(define-public (set-fee-rate-yield-token (expiry uint) (the-yield-token ) (fee-rate-yield-token uint)) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rate-yield-token: fee-rate-yield-token })) - (ok true) - - ) -) - -;; @desc set-fee-rate-token -;; @restricted fee-to-address -;; @param the-yield-token; yield token -;; @param fee-rate-token; new fee-rate-token -;; @returns (response bool uint) -(define-public (set-fee-rate-token (expiry uint) (the-yield-token ) (fee-rate-token uint)) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (asserts! (is-eq contract-caller (get fee-to-address pool)) ERR-NOT-AUTHORIZED) - - (map-set pools-data-map { yield-token: yield-token, expiry: expiry } (merge pool { fee-rate-token: fee-rate-token })) - (ok true) - ) -) - -;; @desc get-fee-to-address -;; @param the-yield-token; yield token -;; @returns (response principal uint) -(define-read-only (get-fee-to-address (expiry uint) (the-yield-token )) - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - ) - (ok (get fee-to-address pool)) - ) -) - -;; @desc units of yield token given units of token -;; @param the-yield-token; yield token -;; @param dx; amount of token being added -;; @returns (response uint uint) -(define-read-only (get-y-given-x (expiry uint) (the-yield-token ) (dx uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (normalized-expiry (try! (get-t expiry (get listed pool)))) - (dy (try! (contract-call? .yield-token-equation get-y-given-x (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dx))) - ) - (asserts! (> (get balance-yield-token pool) dy) ERR-DY-BIGGER-THAN-AVAILABLE) - (ok dy) - ) -) - -;; @desc units of token given units of yield token -;; @param the-yield-token; yield token -;; @param dy; amount of yield token being added -;; @returns (response uint uint) -(define-read-only (get-x-given-y (expiry uint) (the-yield-token ) (dy uint)) - - (let - ( - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (normalized-expiry (try! (get-t expiry (get listed pool)))) - ) - (contract-call? .yield-token-equation get-x-given-y (get balance-token pool) (+ (get balance-yield-token pool) (get balance-virtual pool)) normalized-expiry dy) - ) -) - -;; @desc units of token required for a target price -;; @param the-yield-token; yield token -;; @param price; target price -;; @returns (response uint uint) -(define-read-only (get-x-given-price (expiry uint) (the-yield-token ) (price uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: (contract-of the-yield-token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) - (balance-token (get balance-token pool)) - ) - (contract-call? .yield-token-equation get-x-given-price balance-token balance-yield-token normalized-expiry price) - ) -) - -;; @desc units of yield token required for a target price -;; @param the-yield-token; yield token -;; @param price; target price -;; @returns (response uint uint) -(define-read-only (get-y-given-price (expiry uint) (the-yield-token ) (price uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) - (balance-token (get balance-token pool)) - ) - (contract-call? .yield-token-equation get-y-given-price balance-token balance-yield-token normalized-expiry price) - ) -) - -;; @desc units of token required for a target yield -;; @param the-yield-token; yield token -;; @param yield; target yield -;; @returns (response uint uint) -(define-read-only (get-x-given-yield (expiry uint) (the-yield-token ) (yield uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) - (balance-token (get balance-token pool)) - ) - (contract-call? .yield-token-equation get-x-given-yield balance-token balance-yield-token normalized-expiry yield) - ) -) - -;; @desc units of yield token required for a target yield -;; @param the-yield-token; yield token -;; @param yield; target yield -;; @returns (response uint uint) -(define-read-only (get-y-given-yield (expiry uint) (the-yield-token ) (yield uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-yield-token (+ (get balance-yield-token pool) (get balance-virtual pool))) - (balance-token (get balance-token pool)) - ) - (contract-call? .yield-token-equation get-y-given-yield balance-token balance-yield-token normalized-expiry yield) - ) -) - -;; @desc units of pool token to be minted, together with break-down of yield-token given amount of token being added -;; @param the-yield-token; yield token -;; @param dx; amount of token added -;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-token-given-position (expiry uint) (the-yield-token ) (dx uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-actual (get balance-yield-token pool)) - (balance-virtual (get balance-virtual pool)) - (balance-yield-token (+ balance-actual balance-virtual)) - (balance-token (get balance-token pool)) - (total-supply (get total-supply pool)) - (data (try! (contract-call? .yield-token-equation get-token-given-position balance-token balance-yield-token normalized-expiry total-supply dx))) - (token (get token data)) - (dy (get dy data)) - (percent-act (if (is-eq balance-yield-token u0) u0 (div-down balance-actual balance-yield-token))) - (dy-act (if (is-eq token dy) u0 (mul-down dy percent-act))) - (dy-vir (if (is-eq token dy) token (if (<= dy dy-act) u0 (- dy dy-act)))) - ) - (ok {token: token, dy-act: dy-act, dy-vir: dy-vir}) - ) - -) - -;; @desc units of token, yield-token and yield-token (virtual) required to mint given units of pool-token -;; @param the-yield-token; yield token -;; @param token; units of pool token to be minted -;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-mint (expiry uint) (the-yield-token ) (token uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-actual (get balance-yield-token pool)) - (balance-virtual (get balance-virtual pool)) - (balance-yield-token (+ balance-actual balance-virtual)) - (balance-token (get balance-token pool)) - (total-supply (get total-supply pool)) - (data (try! (contract-call? .yield-token-equation get-position-given-mint balance-token balance-yield-token normalized-expiry total-supply token))) - (dx (get dx data)) - (dy (get dy data)) - (percent-act (div-down balance-actual balance-yield-token)) - (dy-act (mul-down dy percent-act)) - (dy-vir (if (<= dy dy-act) u0 (- dy dy-act))) - ) - (ok {dx: dx, dy-act: dy-act, dy-vir: dy-vir}) - ) -) - -;; @desc units of token, yield-token and yield-token (virtual) to be returned after burning given units of pool-token -;; @param the-yield-token; yield token -;; @param token; units of pool token to be burnt -;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-burn (expiry uint) (the-yield-token ) (token uint)) - - (let - ( - (yield-token (contract-of the-yield-token)) - (pool (unwrap! (map-get? pools-data-map { yield-token: yield-token, expiry: expiry }) ERR-INVALID-POOL-ERR)) - (listed (get listed pool)) - (normalized-expiry (try! (get-t expiry listed))) - (balance-actual (get balance-yield-token pool)) - (balance-virtual (get balance-virtual pool)) - (balance-yield-token (+ balance-actual balance-virtual)) - (balance-token (get balance-token pool)) - (total-supply (get total-supply pool)) - (data (try! (contract-call? .yield-token-equation get-position-given-burn balance-token balance-yield-token normalized-expiry total-supply token))) - (dx (get dx data)) - (dy (get dy data)) - (percent-act (div-down balance-actual balance-yield-token)) - (dy-act (mul-down dy percent-act)) - (dy-vir (if (<= dy dy-act) u0 (- dy dy-act))) - ) - (ok {dx: dx, dy-act: dy-act, dy-vir: dy-vir}) - ) -) - - -;; math-fixed-point -;; Fixed Point Math -;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol - -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - -;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, -;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error -;; (i.e. the last digit of the result may be completely lost to this error). -(define-constant MAX_POW_RELATIVE_ERROR u4) - -;; public functions -;; - -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - -(define-read-only (mul-down (a uint) (b uint)) - (/ (* a b) ONE_8) -) - - -(define-read-only (mul-up (a uint) (b uint)) - (let - ( - (product (* a b)) - ) - (if (is-eq product u0) - u0 - (+ u1 (/ (- product u1) ONE_8)) - ) - ) -) - -(define-read-only (div-down (a uint) (b uint)) - (if (is-eq a u0) - u0 - (/ (* a ONE_8) b) - ) -) - -(define-read-only (div-up (a uint) (b uint)) - (if (is-eq a u0) - u0 - (+ u1 (/ (- (* a ONE_8) u1) b)) - ) -) - -(define-read-only (pow-down (a uint) (b uint)) - (let - ( - (raw (unwrap-panic (pow-fixed a b))) - (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) - ) - (if (< raw max-error) - u0 - (- raw max-error) - ) - ) -) - -(define-read-only (pow-up (a uint) (b uint)) - (let - ( - (raw (unwrap-panic (pow-fixed a b))) - (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) - ) - (+ raw max-error) - ) -) - -;; math-log-exp -;; Exponentiation and logarithm functions for 8 decimal fixed point numbers (both base and exponent/argument). -;; Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural -;; exponentiation and logarithm (where the base is Euler's number). -;; Reference: https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/LogExpMath.sol -;; MODIFIED: because we use only 128 bits instead of 256, we cannot do 20 decimal or 36 decimal accuracy like in Balancer. - -;; constants -;; -;; All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying -;; two numbers, and multiply by ONE when dividing them. -;; All arguments and return values are 8 decimal fixed point numbers. -(define-constant iONE_8 (pow 10 8)) -(define-constant ONE_10 (pow 10 10)) - -;; The domain of natural exponentiation is bound by the word size and number of decimals used. -;; The largest possible result is (2^127 - 1) / 10^8, -;; which makes the largest exponent ln((2^127 - 1) / 10^8) = 69.6090111872. -;; The smallest possible result is 10^(-8), which makes largest negative argument ln(10^(-8)) = -18.420680744. -;; We use 69.0 and -18.0 to have some safety margin. -(define-constant MAX_NATURAL_EXPONENT (* 69 iONE_8)) -(define-constant MIN_NATURAL_EXPONENT (* -18 iONE_8)) - -(define-constant MILD_EXPONENT_BOUND (/ (pow u2 u126) (to-uint iONE_8))) - -;; Because largest exponent is 69, we start from 64 -;; The first several a_n are too large if stored as 8 decimal numbers, and could cause intermediate overflows. -;; Instead we store them as plain integers, with 0 decimals. -(define-constant x_a_list_no_deci (list -{x_pre: 6400000000, a_pre: 6235149080811616882910000000, use_deci: false} ;; x1 = 2^6, a1 = e^(x1) -)) -;; 8 decimal constants -(define-constant x_a_list (list -{x_pre: 3200000000, a_pre: 7896296018268069516100, use_deci: true} ;; x2 = 2^5, a2 = e^(x2) -{x_pre: 1600000000, a_pre: 888611052050787, use_deci: true} ;; x3 = 2^4, a3 = e^(x3) -{x_pre: 800000000, a_pre: 298095798704, use_deci: true} ;; x4 = 2^3, a4 = e^(x4) -{x_pre: 400000000, a_pre: 5459815003, use_deci: true} ;; x5 = 2^2, a5 = e^(x5) -{x_pre: 200000000, a_pre: 738905610, use_deci: true} ;; x6 = 2^1, a6 = e^(x6) -{x_pre: 100000000, a_pre: 271828183, use_deci: true} ;; x7 = 2^0, a7 = e^(x7) -{x_pre: 50000000, a_pre: 164872127, use_deci: true} ;; x8 = 2^-1, a8 = e^(x8) -{x_pre: 25000000, a_pre: 128402542, use_deci: true} ;; x9 = 2^-2, a9 = e^(x9) -{x_pre: 12500000, a_pre: 113314845, use_deci: true} ;; x10 = 2^-3, a10 = e^(x10) -{x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) -)) - -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) - -;; private functions -;; - -;; Internal natural logarithm (ln(a)) with signed 8 decimal fixed point argument. -(define-private (ln-priv (a int)) - (let - ( - (a_sum_no_deci (fold accumulate_division x_a_list_no_deci {a: a, sum: 0})) - (a_sum (fold accumulate_division x_a_list {a: (get a a_sum_no_deci), sum: (get sum a_sum_no_deci)})) - (out_a (get a a_sum)) - (out_sum (get sum a_sum)) - (z (/ (* (- out_a iONE_8) iONE_8) (+ out_a iONE_8))) - (z_squared (/ (* z z) iONE_8)) - (div_list (list 3 5 7 9 11)) - (num_sum_zsq (fold rolling_sum_div div_list {num: z, seriesSum: z, z_squared: z_squared})) - (seriesSum (get seriesSum num_sum_zsq)) - (r (+ out_sum (* seriesSum 2))) - ) - (ok r) - ) -) - -(define-private (accumulate_division (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_a_sum (tuple (a int) (sum int)))) - (let - ( - (a_pre (get a_pre x_a_pre)) - (x_pre (get x_pre x_a_pre)) - (use_deci (get use_deci x_a_pre)) - (rolling_a (get a rolling_a_sum)) - (rolling_sum (get sum rolling_a_sum)) - ) - (if (>= rolling_a (if use_deci a_pre (* a_pre iONE_8))) - {a: (/ (* rolling_a (if use_deci iONE_8 1)) a_pre), sum: (+ rolling_sum x_pre)} - {a: rolling_a, sum: rolling_sum} - ) - ) -) - -(define-private (rolling_sum_div (n int) (rolling (tuple (num int) (seriesSum int) (z_squared int)))) - (let - ( - (rolling_num (get num rolling)) - (rolling_sum (get seriesSum rolling)) - (z_squared (get z_squared rolling)) - (next_num (/ (* rolling_num z_squared) iONE_8)) - (next_sum (+ rolling_sum (/ next_num n))) - ) - {num: next_num, seriesSum: next_sum, z_squared: z_squared} - ) -) - -;; Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to -;; arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means -;; x^y = exp(y * ln(x)). -;; Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`. -(define-private (pow-priv (x uint) (y uint)) - (let - ( - (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)) - ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) - (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) - ) -) - -(define-private (exp-pos (x int)) - (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) - (let - ( - ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct - ;; it and compute the accumulated product. - (x_product_no_deci (fold accumulate_product x_a_list_no_deci {x: x, product: 1})) - (x_adj (get x x_product_no_deci)) - (firstAN (get product x_product_no_deci)) - (x_product (fold accumulate_product x_a_list {x: x_adj, product: iONE_8})) - (product_out (get product x_product)) - (x_out (get x x_product)) - (seriesSum (+ iONE_8 x_out)) - (div_list (list 2 3 4 5 6 7 8 9 10 11 12)) - (term_sum_x (fold rolling_div_sum div_list {term: x_out, seriesSum: seriesSum, x: x_out})) - (sum (get seriesSum term_sum_x)) - ) - (ok (* (/ (* product_out sum) iONE_8) firstAN)) - ) - ) -) - -(define-private (accumulate_product (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_x_p (tuple (x int) (product int)))) - (let - ( - (x_pre (get x_pre x_a_pre)) - (a_pre (get a_pre x_a_pre)) - (use_deci (get use_deci x_a_pre)) - (rolling_x (get x rolling_x_p)) - (rolling_product (get product rolling_x_p)) - ) - (if (>= rolling_x x_pre) - {x: (- rolling_x x_pre), product: (/ (* rolling_product a_pre) (if use_deci iONE_8 1))} - {x: rolling_x, product: rolling_product} - ) - ) -) - -(define-private (rolling_div_sum (n int) (rolling (tuple (term int) (seriesSum int) (x int)))) - (let - ( - (rolling_term (get term rolling)) - (rolling_sum (get seriesSum rolling)) - (x (get x rolling)) - (next_term (/ (/ (* rolling_term x) iONE_8) n)) - (next_sum (+ rolling_sum next_term)) - ) - {term: next_term, seriesSum: next_sum, x: x} - ) -) - -;; public functions -;; - -(define-read-only (get-exp-bound) - (ok MILD_EXPONENT_BOUND) -) - -;; Exponentiation (x^y) with unsigned 8 decimal fixed point base and exponent. -(define-read-only (pow-fixed (x uint) (y uint)) - (begin - ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) - - ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) - - (if (is-eq y u0) - (ok (to-uint iONE_8)) - (if (is-eq x u0) - (ok u0) - (pow-priv x y) - ) - ) - ) -) - -;; Natural exponentiation (e^x) with signed 8 decimal fixed point exponent. -;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. -(define-read-only (exp-fixed (x int)) - (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) - (if (< x 0) - ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it - ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). - ;; Fixed point division requires multiplying by iONE_8. - (ok (/ (* iONE_8 iONE_8) (unwrap-panic (exp-pos (* -1 x))))) - (exp-pos x) - ) - ) -) - -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - -;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. -(define-read-only (ln-fixed (a int)) - (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) - (if (< a iONE_8) - ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). - ;; If a is less than one, 1/a will be greater than one. - ;; Fixed point division requires multiplying by iONE_8. - (ok (- 0 (unwrap-panic (ln-priv (/ (* iONE_8 iONE_8) a))))) - (ln-priv a) - ) - ) -) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-token-pool' -(impl-trait .trait-ownable.ownable-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) -(use-trait multisig-trait .trait-multisig-vote.multisig-vote-sft-trait) - -;; collateral-rebalancing-pool -;; - -;; constants -;; -(define-constant ONE_8 u100000000) ;; 8 decimal places - -(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 ERR-WEIGHTED-EQUATION-CALL (err u2009)) -(define-constant ERR-INTERNAL-FUNCTION-CALL (err u1001)) -(define-constant ERR-GET-WEIGHT-FAIL (err u2012)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) -(define-constant ERR-GET-PRICE-FAIL (err u2015)) -(define-constant ERR-GET-SYMBOL-FAIL (err u6000)) -(define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) -(define-constant ERR-EXPIRY (err u2017)) -(define-constant ERR-get-balance-fixed-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 ERR-INVALID-POOL-TOKEN (err u2023)) - -(define-constant a1 u27839300) -(define-constant a2 u23038900) -(define-constant a3 u97200) -(define-constant a4 u7810800) - -(define-data-var CONTRACT-OWNER principal tx-sender) - -(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)) - ) -) - -;; data maps and vars -;; -(define-map pools-map - { pool-id: uint } - { - token-x: principal, ;; collateral - token-y: principal, ;; token - expiry: uint - } -) - -(define-map pools-data-map - { - token-x: principal, - token-y: principal, - expiry: uint - } - { - yield-supply: uint, - key-supply: uint, - balance-x: uint, - balance-y: uint, - fee-to-address: principal, - yield-token: principal, - key-token: principal, - strike: uint, - bs-vol: uint, - ltv-0: uint, - fee-rate-x: uint, - fee-rate-y: uint, - fee-rebate: uint, - weight-x: uint, - weight-y: uint, - moving-average: uint, - conversion-ltv: uint - } -) - -(define-data-var pool-count uint u0) -(define-data-var pools-list (list 2000 uint) (list)) - -;; private functions -;; - -;; Approximation of Error Function using Abramowitz and Stegun -;; https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions -;; Please note erf(x) equals -erf(-x) -(define-private (erf (x uint)) - (let - ( - (a1x (mul-down a1 x)) - (x2 (pow-down x u200000000)) - (a2x (mul-down a2 x2)) - (x3 (pow-down x u300000000)) - (a3x (mul-down a3 x3)) - (x4 (pow-down x u400000000)) - (a4x (mul-down a4 x4)) - (denom (+ ONE_8 a1x)) - (denom1 (+ denom a2x)) - (denom2 (+ denom1 a3x)) - (denom3 (+ denom2 a4x)) - (denom4 (pow-down denom3 u400000000)) - (base (div-down ONE_8 denom4)) - ) - (if (<= ONE_8 base) u0 (- ONE_8 base)) - ) -) - -;; public functions -;; - -;; @desc get-pool-count -;; @returns uint -(define-read-only (get-pool-count) - (var-get pool-count) -) - -;; @desc get-pool-contracts -;; @param pool-id; pool-id -;; @returns (response (tuple) uint) -(define-read-only (get-pool-contracts (pool-id uint)) - (ok (unwrap! (map-get? pools-map {pool-id: pool-id}) ERR-INVALID-POOL-ERR)) -) - -;; @desc get-pools -;; @returns (optional (tuple)) -(define-read-only (get-pools) - (map get-pool-contracts (var-get pools-list)) -) - -;; @desc get-pool-details -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; expiry block-height -;; @returns (response (tuple) uint) -(define-read-only (get-pool-details (token ) (collateral ) (expiry uint)) - (ok (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) -) - -;; @desc get-spot -;; @desc units of token per unit of collateral -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; expiry block-height -;; @returns (response uint uint) -(define-read-only (get-spot (token ) (collateral )) - (if (is-eq token collateral) - (ok ONE_8) - (contract-call? .fixed-weight-pool get-oracle-resilient token collateral u50000000 u50000000) - ) -) - -;; @desc get-pool-value-in-token -;; @desc value of pool in units of borrow token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; expiry block-height -;; @returns (response uint uint) -(define-read-only (get-pool-value-in-token (token ) (collateral ) (expiry uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-y (get balance-y pool)) - (balance-x-in-y (div-down (get balance-x pool) (try! (get-spot token collateral)))) - ) - (ok (+ balance-x-in-y balance-y)) - ) -) - -;; @desc get-pool-value-in-collateral -;; @desc value of pool in units of collateral token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; expiry block-height -;; @returns (response uint uint) -(define-read-only (get-pool-value-in-collateral (token ) (collateral ) (expiry uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (balance-x (get balance-x pool)) - (balance-y-in-x (mul-down (get balance-y pool) (try! (get-spot token collateral)))) - ) - (ok (+ balance-y-in-x balance-x)) - ) -) - -;; @desc get-ltv -;; @desc value of yield-token as % of pool value (i.e. loan-to-value) -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; expiry block-height -;; @returns (response uint uint) -(define-read-only (get-ltv (token ) (collateral ) (expiry uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (yield-supply (get yield-supply pool)) ;; in token - (pool-value (try! (get-pool-value-in-token token collateral expiry))) ;; also in token - ) - ;; if no liquidity in the pool, return ltv-0 - (if (is-eq yield-supply u0) - (ok (get ltv-0 pool)) - (ok (div-down yield-supply pool-value)) - ) - ) -) - -;; @desc get-weight-y -;; @desc delta of borrow token (risky asset) based on reference black-scholes option with expiry/strike/bs-vol -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; expiry block-height -;; @param strike; reference strike price -;; @param bs-vol; reference black-scholes vol -;; @returns (response uint uint) -(define-read-only (get-weight-y (token ) (collateral ) (expiry uint) (strike uint) (bs-vol uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) ERR-INVALID-POOL-ERR)) - (weight-y (get weight-y pool)) - (moving-average (get moving-average pool)) - (conversion-ltv (get conversion-ltv pool)) - (ma-comp (- ONE_8 moving-average)) - - (spot (try! (get-spot token collateral))) - (now (* block-height ONE_8)) - (ltv (try! (get-ltv token collateral expiry))) - ) - (if (or (> ltv conversion-ltv) (>= now expiry)) - (ok u99900000) - (let - ( - ;; assume 15secs per block - (t (div-down - (- expiry now) (* u2102400 ONE_8))) - - ;; we calculate d1 first - (spot-term (div-up spot strike)) - (pow-bs-vol (div-up (pow-down bs-vol u200000000) u200000000)) - (vol-term (mul-up t pow-bs-vol)) - (sqrt-t (pow-down t u50000000)) - (sqrt-2 (pow-down u200000000 u50000000)) - - (denominator (mul-down bs-vol sqrt-t)) - (numerator (+ vol-term (- (if (> spot-term ONE_8) spot-term ONE_8) (if (> spot-term ONE_8) ONE_8 spot-term)))) - (d1 (div-up numerator denominator)) - (erf-term (erf (div-up d1 sqrt-2))) - (complement (if (> spot-term ONE_8) (+ ONE_8 erf-term) (if (<= ONE_8 erf-term) u0 (- ONE_8 erf-term)))) - (weight-t (div-up complement u200000000)) - (weighted (+ (mul-down moving-average weight-y) (mul-down ma-comp weight-t))) - ) - ;; make sure weight-x > 0 so it works with weighted-equation - (ok (if (> weighted u100000) weighted u100000)) - ) - ) - ) -) - -;; @desc create-pool with single sided liquidity -;; @restricted CONTRACT-OWNER -;; @param token; borrow token -;; @param collateral; collateral token -;; @param the-yield-token; yield-token to be minted -;; @param the-key-token; key-token to be minted -;; @param multisig-vote; multisig to govern the pool being created -;; @param ltv-0; initial loan-to-value -;; @param conversion-ltv; loan-to-value at which conversion into borrow token happens -;; @param bs-vol; reference black-scholes vol to use -;; @param moving-average; weighting smoothing factor -;; @param dx; amount of collateral token being added -;; @returns (response bool uint) -(define-public (create-pool (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (multisig-vote ) (ltv-0 uint) (conversion-ltv uint) (bs-vol uint) (moving-average uint) (dx uint)) - (begin - (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - (asserts! - (is-none (map-get? pools-data-map { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry })) - ERR-POOL-ALREADY-EXISTS - ) - (let - ( - (pool-id (+ (var-get pool-count) u1)) - (token-x (contract-of collateral)) - (token-y (contract-of token)) - - (now (* block-height ONE_8)) - ;; assume 10mins per block - (t (div-down - (- expiry now) (* u52560 ONE_8))) - - ;; we calculate d1 first - ;; because we support 'at-the-money' only, we can simplify formula - (sqrt-t (pow-down t u50000000)) - (sqrt-2 (pow-down u200000000 u50000000)) - (pow-bs-vol (div-up (pow-down bs-vol u200000000) u200000000)) - (numerator (mul-up t pow-bs-vol)) - (denominator (mul-down bs-vol sqrt-t)) - (d1 (div-up numerator denominator)) - (erf-term (erf (div-up d1 sqrt-2))) - (complement (if (<= ONE_8 erf-term) u0 (- ONE_8 erf-term))) - (weighted (div-up complement u200000000)) - (weight-y (if (> weighted u100000) weighted u100000)) - - (weight-x (- ONE_8 weight-y)) - - (pool-data { - yield-supply: u0, - key-supply: u0, - balance-x: u0, - balance-y: u0, - fee-to-address: (contract-of multisig-vote), - yield-token: (contract-of the-yield-token), - key-token: (contract-of the-key-token), - strike: (try! (get-spot token collateral)), - bs-vol: bs-vol, - fee-rate-x: u0, - fee-rate-y: u0, - fee-rebate: u0, - ltv-0: ltv-0, - weight-x: weight-x, - weight-y: weight-y, - moving-average: moving-average, - conversion-ltv: conversion-ltv - }) - ) - - (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) - - (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) - (var-set pool-count pool-id) - (try! (add-to-position token collateral expiry the-yield-token the-key-token dx)) - (print { object: "pool", action: "created", data: pool-data }) - (ok true) - ) - ) -) - -;; @desc mint yield-token and key-token, swap minted yield-token with token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param the-yield-token; yield-token to be minted -;; @param the-key-token; key-token to be minted -;; @param dx; amount of collateral added -;; @post collateral; sender transfer exactly dx to alex-vault -;; @post yield-token; sender transfers > 0 to alex-vault -;; @post token; alex-vault transfers >0 to sender -;; @returns (response (tuple uint uint) uint) -(define-public (add-to-position-and-switch (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (dx uint)) - (let - ( - (minted-yield-token (get yield-token (try! (add-to-position token collateral expiry the-yield-token the-key-token dx)))) - ) - (contract-call? .yield-token-pool swap-y-for-x expiry the-yield-token token minted-yield-token none) - ) -) - -;; @desc mint yield-token and key-token, with single-sided liquidity -;; @param token; borrow token -;; @param collateral; collateral token -;; @param the-yield-token; yield-token to be minted -;; @param the-key-token; key-token to be minted -;; @param dx; amount of collateral added -;; @post collateral; sender transfer exactly dx to alex-vault -;; @returns (response (tuple uint uint) uint) -(define-public (add-to-position (token ) (collateral ) (expiry uint) (the-yield-token ) (the-key-token ) (dx 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)) - (conversion-ltv (get conversion-ltv pool)) - (ltv (try! (get-ltv token collateral expiry))) - ) - (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) - ;; mint is possible only if ltv < 1 - (asserts! (>= conversion-ltv ltv) ERR-LTV-GREATER-THAN-ONE) - (asserts! (and (is-eq (get yield-token pool) (contract-of the-yield-token)) (is-eq (get key-token pool) (contract-of the-key-token))) ERR-INVALID-POOL-TOKEN) - (let - ( - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (yield-supply (get yield-supply pool)) - (key-supply (get key-supply pool)) - (weight-x (get weight-x pool)) - - (new-supply (try! (get-token-given-position token collateral expiry dx))) - (yield-new-supply (get yield-token new-supply)) - (key-new-supply (get key-token new-supply)) - - (dx-weighted (mul-down weight-x dx)) - (dx-to-dy (if (<= dx dx-weighted) u0 (- dx dx-weighted))) - - (dy-weighted (if (is-eq token-x token-y) - dx-to-dy - (try! (contract-call? .fixed-weight-pool swap token collateral u50000000 u50000000 dx-to-dy none)) - ) - ) - - (pool-updated (merge pool { - yield-supply: (+ yield-new-supply yield-supply), - key-supply: (+ key-new-supply key-supply), - balance-x: (+ balance-x dx-weighted), - balance-y: (+ balance-y dy-weighted) - })) - ) - - (unwrap! (contract-call? collateral transfer dx-weighted tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) - (unwrap! (contract-call? token transfer dy-weighted tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) - - (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - ;; mint pool token and send to tx-sender - (try! (contract-call? the-yield-token mint expiry yield-new-supply tx-sender)) - (try! (contract-call? the-key-token mint expiry key-new-supply tx-sender)) - (print { object: "pool", action: "liquidity-added", data: pool-updated }) - (ok {yield-token: yield-new-supply, key-token: key-new-supply}) - ) - ) -) - -;; @desc burn yield-token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param the-yield-token; yield-token to be burnt -;; @param percent; % of yield-token held to be burnt -;; @post yield-token; alex-vault transfer exactly uints of token equal to (percent * yield-token held) to sender -;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position-yield (token ) (collateral ) (expiry uint) (the-yield-token ) (percent uint)) - (begin - (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) - ;; burn supported only at maturity - (asserts! (> (* block-height ONE_8) expiry) ERR-EXPIRY) - - (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)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (yield-supply (get yield-supply pool)) - (total-shares (unwrap! (contract-call? the-yield-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) - (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) - (shares-to-yield (div-down shares yield-supply)) - - ;; if there are any residual collateral, convert to token - (bal-x-to-y (if (is-eq balance-x u0) - u0 - (if (is-eq token-x token-y) - balance-x - (begin - (as-contract (try! (contract-call? .alex-vault transfer-ft collateral balance-x tx-sender))) - (as-contract (try! (contract-call? .fixed-weight-pool swap token collateral u50000000 u50000000 balance-x none))) - ) - ) - ) - ) - (new-bal-y (+ balance-y bal-x-to-y)) - (dy (mul-down new-bal-y shares-to-yield)) - - (pool-updated (merge pool { - yield-supply: (if (<= yield-supply shares) u0 (- yield-supply shares)), - balance-x: u0, - balance-y: (if (<= new-bal-y dy) u0 (- new-bal-y dy)) - }) - ) - ) - - (asserts! (is-eq (get yield-token pool) (contract-of the-yield-token)) ERR-INVALID-POOL-TOKEN) - - ;; if any conversion happened at contract level, transfer back to vault - (and - (> bal-x-to-y u0) - (not (is-eq token-x token-y)) - (as-contract (unwrap! (contract-call? token transfer bal-x-to-y tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) - ) - - ;; if shares > dy, then transfer the shortfall from reserve. - ;; TODO: what if token is exhausted but reserve have others? - (and (< dy shares) (try! (contract-call? .alex-reserve-pool remove-from-balance token-y (- shares dy)))) - - ;; transfer shares of token to tx-sender, ensuring convertability of yield-token - (try! (contract-call? .alex-vault transfer-ft token shares tx-sender)) - - (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (try! (contract-call? the-yield-token burn expiry shares tx-sender)) - - (print { object: "pool", action: "liquidity-removed", data: pool-updated }) - (ok {dx: u0, dy: shares}) - ) - ) -) - -;; @desc burn key-token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param the-key-token; key-token to be burnt -;; @param percent; % of key-token held to be burnt -;; @post token; alex-vault transfers > 0 token to sender -;; @post collateral; alex-vault transfers > 0 collateral to sender -;; @returns (response (tuple uint uint) uint) -(define-public (reduce-position-key (token ) (collateral ) (expiry uint) (the-key-token ) (percent uint)) - (begin - (asserts! (<= percent ONE_8) ERR-PERCENT_GREATER_THAN_ONE) - ;; burn supported only at maturity - (asserts! (> (* block-height ONE_8) expiry) ERR-EXPIRY) - (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)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (key-supply (get key-supply pool)) - (total-shares (unwrap! (contract-call? the-key-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) - (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) - (reduce-data (try! (get-position-given-burn-key token collateral expiry shares))) - (dx-weighted (get dx reduce-data)) - (dy-weighted (get dy reduce-data)) - - (pool-updated (merge pool { - key-supply: (if (<= key-supply shares) u0 (- key-supply shares)), - balance-x: (if (<= balance-x dx-weighted) u0 (- balance-x dx-weighted)), - balance-y: (if (<= balance-y dy-weighted) u0 (- balance-y dy-weighted)) - }) - ) - ) - - (asserts! (is-eq (get key-token pool) (contract-of the-key-token)) ERR-INVALID-POOL-TOKEN) - - (and (> dx-weighted u0) (try! (contract-call? .alex-vault transfer-ft collateral dx-weighted tx-sender))) - (and (> dy-weighted u0) (try! (contract-call? .alex-vault transfer-ft token dy-weighted tx-sender))) - - (map-set pools-data-map { token-x: token-x, token-y: token-y, expiry: expiry } pool-updated) - (try! (contract-call? the-key-token burn expiry shares tx-sender)) - (print { object: "pool", action: "liquidity-removed", data: pool-updated }) - (ok {dx: dx-weighted, dy: dy-weighted}) - ) - ) -) - -;; @desc swap collateral with token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param dx; amount of collateral to be swapped -;; @param min-dy; max slippage -;; @post collateral; sender transfers exactly dx collateral to alex-vault -;; @returns (response (tuple uint uint) uint) -(define-public (swap-x-for-y (token ) (collateral ) (expiry uint) (dx uint) (min-dy (optional uint))) - (begin - (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) - ;; swap is supported only if token /= collateral - (asserts! (not (is-eq token collateral)) ERR-INVALID-POOL-ERR) - (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)) - (strike (get strike pool)) - (bs-vol (get bs-vol pool)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - - ;; every swap call updates the weights - (weight-y (unwrap! (get-weight-y token collateral expiry strike bs-vol) ERR-GET-WEIGHT-FAIL)) - (weight-x (- ONE_8 weight-y)) - - ;; fee = dx * fee-rate-x - (fee (mul-up dx (get fee-rate-x pool))) - (fee-rebate (mul-down fee (get fee-rebate pool))) - (dx-net-fees (if (<= dx fee) u0 (- dx fee))) - (dy (try! (get-y-given-x token collateral expiry dx-net-fees))) - - (pool-updated - (merge pool - { - balance-x: (+ balance-x dx-net-fees fee-rebate), - balance-y: (if (<= balance-y dy) u0 (- balance-y dy)), - weight-x: weight-x, - weight-y: weight-y - } - ) - ) - ) - - (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 tx-sender)) - (try! (contract-call? .alex-reserve-pool add-to-balance token-x (- fee fee-rebate))) - - ;; 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}) - ) - ) -) - -;; @desc swap token with collateral -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param dy; amount of token to be swapped -;; @param min-dx; max slippage -;; @post token; sender transfers exactly dy token to alex-vault -;; @returns (response (tuple uint uint) uint) -(define-public (swap-y-for-x (token ) (collateral ) (expiry uint) (dy uint) (min-dx (optional uint))) - (begin - (asserts! (> dy u0) ERR-INVALID-LIQUIDITY) - ;; swap is supported only if token /= collateral - (asserts! (not (is-eq token collateral)) ERR-INVALID-POOL-ERR) - (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)) - (strike (get strike pool)) - (bs-vol (get bs-vol pool)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - - ;; every swap call updates the weights - (weight-y (unwrap! (get-weight-y token collateral expiry strike bs-vol) ERR-GET-WEIGHT-FAIL)) - (weight-x (- ONE_8 weight-y)) - - ;; fee = dy * fee-rate-y - (fee (mul-up dy (get fee-rate-y pool))) - (fee-rebate (mul-down fee (get fee-rebate pool))) - (dy-net-fees (if (<= dy fee) u0 (- dy fee))) - (dx (try! (get-x-given-y token collateral expiry 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-rebate), - weight-x: weight-x, - weight-y: weight-y - } - ) - ) - ) - - (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) - - (try! (contract-call? .alex-vault transfer-ft collateral dx tx-sender)) - (unwrap! (contract-call? token transfer dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED) - (try! (contract-call? .alex-reserve-pool add-to-balance token-y (- fee fee-rebate))) - - ;; 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}) - ) - ) -) - -;; @desc get-fee-rebate -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @returns (response uint uint) -(define-read-only (get-fee-rebate (token ) (collateral ) (expiry uint)) - (ok (get fee-rebate (try! (get-pool-details token collateral expiry)))) -) - -;; @desc set-fee-rebate -;; @restricted CONTRACT-OWNER -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param fee-rebate; new fee-rebate -;; @returns (response bool uint) -(define-public (set-fee-rebate (token ) (collateral ) (expiry uint) (fee-rebate uint)) - (let - ( - (pool (try! (get-pool-details token collateral expiry))) - ) - (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) - - (map-set pools-data-map - { - token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry - } - (merge pool { fee-rebate: fee-rebate }) - ) - (ok true) - ) -) - -;; @desc get-fee-rate-x -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @returns (response uint uint) -(define-read-only (get-fee-rate-x (token ) (collateral ) (expiry uint)) - (ok (get fee-rate-x (try! (get-pool-details token collateral expiry)))) -) - -;; @desc get-fee-rate-y -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @returns (response uint uint) -(define-read-only (get-fee-rate-y (token ) (collateral ) (expiry uint)) - (ok (get fee-rate-y (try! (get-pool-details token collateral expiry)))) -) - -;; @desc set-fee-rate-x -;; @restricted fee-to-address -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param fee-rate-x; new fee-rate-x -;; @returns (response bool uint) -(define-public (set-fee-rate-x (token ) (collateral ) (expiry uint) (fee-rate-x uint)) - (let - ( - (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: (contract-of collateral), token-y: (contract-of token), expiry: expiry - } - (merge pool { fee-rate-x: fee-rate-x }) - ) - (ok true) - ) -) - -;; @desc set-fee-rate-y -;; @restricted fee-to-address -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param fee-rate-y; new fee-rate-y -;; @returns (response bool uint) -(define-public (set-fee-rate-y (token ) (collateral ) (expiry uint) (fee-rate-y uint)) - (let - ( - (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: (contract-of collateral), token-y: (contract-of token), expiry: expiry - } - (merge pool { fee-rate-y: fee-rate-y }) - ) - (ok true) - ) -) - -;; @desc get-fee-to-address (multisig of the pool) -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @returns (response principal uint) -(define-read-only (get-fee-to-address (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-to-address pool)) - ) -) - -;; @desc units of token given units of collateral -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param dx; amount of collateral being added -;; @returns (response uint uint) -(define-read-only (get-y-given-x (token ) (collateral ) (expiry uint) (dx uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map - { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) - ERR-INVALID-POOL-ERR) - ) - ) - (contract-call? .weighted-equation get-y-given-x - (get balance-x pool) - (get balance-y pool) - (get weight-x pool) - (get weight-y pool) - dx - ) - ) -) - -;; @desc units of collateral given units of token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param dy; amount of token being added -;; @returns (response uint uint) -(define-read-only (get-x-given-y (token ) (collateral ) (expiry uint) (dy uint)) - (let - ( - (pool (unwrap! (map-get? pools-data-map - { token-x: (contract-of collateral), token-y: (contract-of token), expiry: expiry }) - ERR-INVALID-POOL-ERR) - ) - ) - (contract-call? .weighted-equation get-x-given-y - (get balance-x pool) - (get balance-y pool) - (get weight-x pool) - (get weight-y pool) - dy - ) - ) -) - -;; @desc units of collateral required for a target price -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param price; target price -;; @returns (response uint uint) -(define-read-only (get-x-given-price (token ) (collateral ) (expiry uint) (price 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)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (weight-x (get weight-x pool)) - (weight-y (get weight-y pool)) - ) - (contract-call? .weighted-equation get-x-given-price balance-x balance-y weight-x weight-y price) - ) -) - -;; @desc units of token required for a target price -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param price; target price -;; @returns (response uint uint) -(define-read-only (get-y-given-price (token ) (collateral ) (expiry uint) (price 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)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (weight-x (get weight-x pool)) - (weight-y (get weight-y pool)) - ) - (contract-call? .weighted-equation get-y-given-price balance-x balance-y weight-x weight-y price) - ) -) - -;; @desc units of yield-/key-token to be minted given amount of collateral being added (single sided liquidity) -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param dx; amount of collateral being added -;; @returns (response (tuple uint uint) uint) -(define-read-only (get-token-given-position (token ) (collateral ) (expiry uint) (dx uint)) - (begin - (asserts! (< (* block-height ONE_8) expiry) ERR-EXPIRY) - (let - ( - (ltv (try! (get-ltv token collateral expiry))) - (dy (if (is-eq (contract-of token) (contract-of collateral)) - dx - (try! (contract-call? .fixed-weight-pool get-x-y token collateral u50000000 u50000000 dx)) - ) - ) - (ltv-dy (mul-down ltv dy)) - ) - - (ok {yield-token: ltv-dy, key-token: ltv-dy}) - ) - ) -) - -;; @desc units of token/collateral required to mint given units of yield-/key-token -;; @desc returns dx (single liquidity) based on dx-weighted and dy-weighted -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param shares; units of yield-/key-token to be minted -;; @returns (response (tuple uint uint uint) uint) -(define-read-only (get-position-given-mint (token ) (collateral ) (expiry uint) (shares uint)) - (begin - (asserts! (< (* block-height ONE_8) expiry) ERR-EXPIRY) ;; mint supported until, but excl., expiry - (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)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (total-supply (get yield-supply pool)) ;; prior to maturity, yield-supply == key-supply, so we use yield-supply - (weight-x (get weight-x pool)) - (weight-y (get weight-y pool)) - - (ltv (try! (get-ltv token collateral expiry))) - - (pos-data (unwrap! (contract-call? .weighted-equation get-position-given-mint balance-x balance-y weight-x weight-y total-supply shares) ERR-WEIGHTED-EQUATION-CALL)) - - (dx-weighted (get dx pos-data)) - (dy-weighted (get dy pos-data)) - - ;; always convert to collateral ccy - (dy-to-dx (if (is-eq token-x token-y) - dy-weighted - (try! (contract-call? .fixed-weight-pool get-x-y token collateral u50000000 u50000000 dy-weighted)) - ) - ) - (dx (+ dx-weighted dy-to-dx)) - ) - (ok {dx: dx, dx-weighted: dx-weighted, dy-weighted: dy-weighted}) - ) - ) -) - -;; @desc units of token/collateral to be returned after burning given units of yield-/key-token -;; @param token; borrow token -;; @param collateral; collateral token -;; @param expiry; borrow expiry -;; @param shares; units of yield-/key-token to be burnt -;; @returns (response (tuple uint uint) uint) -(define-read-only (get-position-given-burn-key (token ) (collateral ) (expiry uint) (shares uint)) - (begin - (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)) - (balance-x (get balance-x pool)) - (balance-y (get balance-y pool)) - (yield-supply (get yield-supply pool)) - (key-supply (get key-supply pool)) - (weight-x (get weight-x pool)) - (weight-y (get weight-y pool)) - (pool-value-unfloored (try! (get-pool-value-in-token token collateral expiry))) - (pool-value-in-y (if (> yield-supply pool-value-unfloored) yield-supply pool-value-unfloored)) - (key-value-in-y (if (<= pool-value-in-y yield-supply) u0 (- pool-value-in-y yield-supply))) - (key-to-pool (div-down key-value-in-y pool-value-in-y)) - (shares-to-key (div-down shares key-supply)) - (shares-to-pool (mul-down key-to-pool shares-to-key)) - - (dx (mul-down shares-to-pool balance-x)) - (dy (mul-down shares-to-pool balance-y)) - ) - (ok {dx: dx, dy: dy}) - ) - ) -) - - -;; math-fixed-point -;; Fixed Point Math -;; following https://github.com/balancer-labs/balancer-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)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - -;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, -;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error -;; (i.e. the last digit of the result may be completely lost to this error). -(define-constant MAX_POW_RELATIVE_ERROR u4) - -;; public functions -;; - -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - -(define-read-only (mul-down (a uint) (b uint)) - (/ (* a b) ONE_8) -) - - -(define-read-only (mul-up (a uint) (b uint)) - (let - ( - (product (* a b)) - ) - (if (is-eq product u0) - u0 - (+ u1 (/ (- product u1) ONE_8)) - ) - ) -) - -(define-read-only (div-down (a uint) (b uint)) - (if (is-eq a u0) - u0 - (/ (* a ONE_8) b) - ) -) - -(define-read-only (div-up (a uint) (b uint)) - (if (is-eq a u0) - u0 - (+ u1 (/ (- (* a ONE_8) u1) b)) - ) -) - -(define-read-only (pow-down (a uint) (b uint)) - (let - ( - (raw (unwrap-panic (pow-fixed a b))) - (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) - ) - (if (< raw max-error) - u0 - (- raw max-error) - ) - ) -) - -(define-read-only (pow-up (a uint) (b uint)) - (let - ( - (raw (unwrap-panic (pow-fixed a b))) - (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) - ) - (+ raw max-error) - ) -) - -;; math-log-exp -;; Exponentiation and logarithm functions for 8 decimal fixed point numbers (both base and exponent/argument). -;; Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural -;; exponentiation and logarithm (where the base is Euler's number). -;; Reference: https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/LogExpMath.sol -;; MODIFIED: because we use only 128 bits instead of 256, we cannot do 20 decimal or 36 decimal accuracy like in Balancer. - -;; constants -;; -;; All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying -;; two numbers, and multiply by ONE when dividing them. -;; All arguments and return values are 8 decimal fixed point numbers. -(define-constant iONE_8 (pow 10 8)) -(define-constant ONE_10 (pow 10 10)) - -;; The domain of natural exponentiation is bound by the word size and number of decimals used. -;; The largest possible result is (2^127 - 1) / 10^8, -;; which makes the largest exponent ln((2^127 - 1) / 10^8) = 69.6090111872. -;; The smallest possible result is 10^(-8), which makes largest negative argument ln(10^(-8)) = -18.420680744. -;; We use 69.0 and -18.0 to have some safety margin. -(define-constant MAX_NATURAL_EXPONENT (* 69 iONE_8)) -(define-constant MIN_NATURAL_EXPONENT (* -18 iONE_8)) - -(define-constant MILD_EXPONENT_BOUND (/ (pow u2 u126) (to-uint iONE_8))) - -;; Because largest exponent is 69, we start from 64 -;; The first several a_n are too large if stored as 8 decimal numbers, and could cause intermediate overflows. -;; Instead we store them as plain integers, with 0 decimals. -(define-constant x_a_list_no_deci (list -{x_pre: 6400000000, a_pre: 6235149080811616882910000000, use_deci: false} ;; x1 = 2^6, a1 = e^(x1) -)) -;; 8 decimal constants -(define-constant x_a_list (list -{x_pre: 3200000000, a_pre: 7896296018268069516100, use_deci: true} ;; x2 = 2^5, a2 = e^(x2) -{x_pre: 1600000000, a_pre: 888611052050787, use_deci: true} ;; x3 = 2^4, a3 = e^(x3) -{x_pre: 800000000, a_pre: 298095798704, use_deci: true} ;; x4 = 2^3, a4 = e^(x4) -{x_pre: 400000000, a_pre: 5459815003, use_deci: true} ;; x5 = 2^2, a5 = e^(x5) -{x_pre: 200000000, a_pre: 738905610, use_deci: true} ;; x6 = 2^1, a6 = e^(x6) -{x_pre: 100000000, a_pre: 271828183, use_deci: true} ;; x7 = 2^0, a7 = e^(x7) -{x_pre: 50000000, a_pre: 164872127, use_deci: true} ;; x8 = 2^-1, a8 = e^(x8) -{x_pre: 25000000, a_pre: 128402542, use_deci: true} ;; x9 = 2^-2, a9 = e^(x9) -{x_pre: 12500000, a_pre: 113314845, use_deci: true} ;; x10 = 2^-3, a10 = e^(x10) -{x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) -)) - -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) - -;; private functions -;; - -;; Internal natural logarithm (ln(a)) with signed 8 decimal fixed point argument. -(define-private (ln-priv (a int)) - (let - ( - (a_sum_no_deci (fold accumulate_division x_a_list_no_deci {a: a, sum: 0})) - (a_sum (fold accumulate_division x_a_list {a: (get a a_sum_no_deci), sum: (get sum a_sum_no_deci)})) - (out_a (get a a_sum)) - (out_sum (get sum a_sum)) - (z (/ (* (- out_a iONE_8) iONE_8) (+ out_a iONE_8))) - (z_squared (/ (* z z) iONE_8)) - (div_list (list 3 5 7 9 11)) - (num_sum_zsq (fold rolling_sum_div div_list {num: z, seriesSum: z, z_squared: z_squared})) - (seriesSum (get seriesSum num_sum_zsq)) - (r (+ out_sum (* seriesSum 2))) - ) - (ok r) - ) -) - -(define-private (accumulate_division (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_a_sum (tuple (a int) (sum int)))) - (let - ( - (a_pre (get a_pre x_a_pre)) - (x_pre (get x_pre x_a_pre)) - (use_deci (get use_deci x_a_pre)) - (rolling_a (get a rolling_a_sum)) - (rolling_sum (get sum rolling_a_sum)) - ) - (if (>= rolling_a (if use_deci a_pre (* a_pre iONE_8))) - {a: (/ (* rolling_a (if use_deci iONE_8 1)) a_pre), sum: (+ rolling_sum x_pre)} - {a: rolling_a, sum: rolling_sum} - ) - ) -) - -(define-private (rolling_sum_div (n int) (rolling (tuple (num int) (seriesSum int) (z_squared int)))) - (let - ( - (rolling_num (get num rolling)) - (rolling_sum (get seriesSum rolling)) - (z_squared (get z_squared rolling)) - (next_num (/ (* rolling_num z_squared) iONE_8)) - (next_sum (+ rolling_sum (/ next_num n))) - ) - {num: next_num, seriesSum: next_sum, z_squared: z_squared} - ) -) - -;; Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to -;; arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means -;; x^y = exp(y * ln(x)). -;; Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`. -(define-private (pow-priv (x uint) (y uint)) - (let - ( - (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)) - ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) - (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) - ) -) - -(define-private (exp-pos (x int)) - (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) - (let - ( - ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct - ;; it and compute the accumulated product. - (x_product_no_deci (fold accumulate_product x_a_list_no_deci {x: x, product: 1})) - (x_adj (get x x_product_no_deci)) - (firstAN (get product x_product_no_deci)) - (x_product (fold accumulate_product x_a_list {x: x_adj, product: iONE_8})) - (product_out (get product x_product)) - (x_out (get x x_product)) - (seriesSum (+ iONE_8 x_out)) - (div_list (list 2 3 4 5 6 7 8 9 10 11 12)) - (term_sum_x (fold rolling_div_sum div_list {term: x_out, seriesSum: seriesSum, x: x_out})) - (sum (get seriesSum term_sum_x)) - ) - (ok (* (/ (* product_out sum) iONE_8) firstAN)) - ) - ) -) - -(define-private (accumulate_product (x_a_pre (tuple (x_pre int) (a_pre int) (use_deci bool))) (rolling_x_p (tuple (x int) (product int)))) - (let - ( - (x_pre (get x_pre x_a_pre)) - (a_pre (get a_pre x_a_pre)) - (use_deci (get use_deci x_a_pre)) - (rolling_x (get x rolling_x_p)) - (rolling_product (get product rolling_x_p)) - ) - (if (>= rolling_x x_pre) - {x: (- rolling_x x_pre), product: (/ (* rolling_product a_pre) (if use_deci iONE_8 1))} - {x: rolling_x, product: rolling_product} - ) - ) -) - -(define-private (rolling_div_sum (n int) (rolling (tuple (term int) (seriesSum int) (x int)))) - (let - ( - (rolling_term (get term rolling)) - (rolling_sum (get seriesSum rolling)) - (x (get x rolling)) - (next_term (/ (/ (* rolling_term x) iONE_8) n)) - (next_sum (+ rolling_sum next_term)) - ) - {term: next_term, seriesSum: next_sum, x: x} - ) -) - -;; public functions -;; - -(define-read-only (get-exp-bound) - (ok MILD_EXPONENT_BOUND) -) - -;; Exponentiation (x^y) with unsigned 8 decimal fixed point base and exponent. -(define-read-only (pow-fixed (x uint) (y uint)) - (begin - ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) - - ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) - - (if (is-eq y u0) - (ok (to-uint iONE_8)) - (if (is-eq x u0) - (ok u0) - (pow-priv x y) - ) - ) - ) -) - -;; Natural exponentiation (e^x) with signed 8 decimal fixed point exponent. -;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. -(define-read-only (exp-fixed (x int)) - (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) - (if (< x 0) - ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it - ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). - ;; Fixed point division requires multiplying by iONE_8. - (ok (/ (* iONE_8 iONE_8) (unwrap-panic (exp-pos (* -1 x))))) - (exp-pos x) - ) - ) -) - -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - -;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. -(define-read-only (ln-fixed (a int)) - (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) - (if (< a iONE_8) - ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). - ;; If a is less than one, 1/a will be greater than one. - ;; Fixed point division requires multiplying by iONE_8. - (ok (- 0 (unwrap-panic (ln-priv (/ (* iONE_8 iONE_8) a))))) - (ln-priv a) - ) - ) -) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.collateral-rebalancing-pool' -(use-trait ft-trait .trait-sip-010.sip-010-trait) -(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) -(use-trait flash-loan-user-trait .trait-flash-loan-user.flash-loan-user-trait) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (roll-position (token ) (collateral ) (expiry uint) (the-key-token ) (flash-loan-user ) (expiry-to-roll uint)) - (let - ( - (reduce-data (try! (contract-call? .collateral-rebalancing-pool reduce-position-key token collateral expiry the-key-token ONE_8))) - (collateral-amount (get dx reduce-data)) - (token-amount (get dy reduce-data)) - (token-to-collateral  - (if (is-eq token-amount u0)  - u0  - (try! (contract-call? .fixed-weight-pool swap collateral token u50000000 u50000000 token-amount none))  - ) - ) - ) - (contract-call? .alex-vault flash-loan flash-loan-user collateral (+ collateral-amount token-to-collateral) (some expiry)) - ) -) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.collateral-rebalancing-pool' -(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) -(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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, - expiry: uint, - 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, expiry: uint } { 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 ) (expiry uint)) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - expiry: u0, - 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 yield-token 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) (is-eq (contract-of token) .key-usda-wbtc)) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (expiry uint) - (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 get-balance expiry tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance expiry tx-sender))) - (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) - (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, - expiry: expiry, - 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)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - - ;; 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 expiry 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), expiry: expiry } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - ;; 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 expiry 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) , expiry: expiry } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) - (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 - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) - ) - - (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 expiry 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)) - (expiry (get expiry proposal)) - (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 expiry new-fee-rate-x)) - (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-usda .token-wbtc expiry new-fee-rate-y)) - - (ok true) - ) -) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-token-pool' -(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) -(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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 aywbtc-wbtc 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, - expiry: uint, - 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-yield-token: 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, expiry: uint } { 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 ) (expiry uint)) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - expiry: u0, - 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-yield-token: u0 ;; default yield-token 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) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (expiry uint) - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda get-balance expiry tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) 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, - expiry: expiry, - 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-yield-token: new-fee-rate-yield-token - } - ) - (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)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - - ;; 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 expiry 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), expiry: expiry } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - ;; 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 expiry 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), expiry: expiry } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) 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 - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token expiry)) ONE_8)) - ) - - (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 expiry token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - ([1;4;31mlet ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-usda new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-usda new-fee-rate-yield-token)) - - (ok true) - ) -) -Analysis error: use of unresolved contract 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-token-pool' -(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) -(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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 aywbtc-wbtc 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, - expiry: uint, - 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-yield-token: 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, expiry: uint } { 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 ) (expiry uint)) - (default-to - { amount: u0 } - (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) - ) -) - -;; Get proposal -(define-read-only (get-proposal-by-id (proposal-id uint)) - (default-to - { - id: u0, - proposer: DEFAULT_OWNER, - expiry: u0, - 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-yield-token: u0 ;; default yield-token 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) -) - - -;; Start a proposal -;; Requires 10% of the supply in your wallet -;; Default voting period is 10 days (144 * 10 blocks) -(define-public (propose - (expiry uint) - (start-block-height uint) - (title (string-utf8 256)) - (url (string-utf8 256)) - (new-fee-rate-token uint) - (new-fee-rate-yield-token uint) - ) - (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance expiry tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) 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, - expiry: expiry, - 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-yield-token: new-fee-rate-yield-token - } - ) - (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)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - - ;; 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 expiry 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), expiry: expiry } - { amount: (+ amount token-count)}) - - (ok amount) - - ) - ) - - - - -(define-public (vote-against (token ) (proposal-id uint) (amount uint)) - (let ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (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 expiry))) - ) - ;; 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 expiry 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), expiry: expiry } - { amount: (+ amount token-count)}) - (ok amount) - ) - - ) - -(define-public (end-proposal (proposal-id uint)) - (let - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) 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 - ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (token-count (/ (get amount (get-tokens-by-member-by-id proposal-id member token expiry)) ONE_8)) - ) - - (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 expiry token-count (as-contract tx-sender) member none))) - (ok true) - ) -) - -;; Make needed contract changes on DAO -(define-private (execute-proposal (proposal-id uint)) - ([1;4;31mlet ( - (proposal (get-proposal-by-id proposal-id)) - (expiry (get expiry proposal)) - (new-fee-rate-token (get new-fee-rate-token proposal)) - (new-fee-rate-yield-token (get new-fee-rate-yield-token proposal)) - ) - - ;; Setting for Yield Token Pool - (try! (contract-call? .yield-token-pool set-fee-rate-token expiry .yield-wbtc new-fee-rate-token)) - (try! (contract-call? .yield-token-pool set-fee-rate-yield-token expiry .yield-wbtc new-fee-rate-yield-token)) - - (ok true) - ) -) diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index c89c6d2f..57bec1b7 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -15,6 +15,7 @@ const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" const yieldwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc" const ytpyieldwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc" const multisigytpyieldwbtc = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc" +const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda" const ONE_8 = 100000000 const expiry = 59760 * ONE_8 @@ -34,7 +35,7 @@ Clarinet.test({ let YTPTest = new YTPTestAgent1(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); - let result = yieldWBTC.mintFixed(expiry, 100000 * ONE_8, deployer.address); + let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool @@ -93,7 +94,7 @@ Clarinet.test({ [types.uint(expiry), types.uint(listed) ], deployer.address); - call.result.expectOk().expectUint(710557) + call.result.expectOk().expectUint(710545) // zero actual yield-token, so must throw an error call = await YTPTest.getYgivenX(expiry, yieldwbtcAddress, 1*ONE_8); @@ -107,12 +108,12 @@ Clarinet.test({ // zero rate environment, so yield-token and token are at parity. call = await YTPTest.getXgivenY(expiry, yieldwbtcAddress, 2*ONE_8); - call.result.expectOk().expectUint(199975237) + call.result.expectOk().expectUint(199970398) // sell some yield-token result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 2*ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(199975237); + position['dx'].expectUint(199970398); position['dy'].expectUint(2*ONE_8); // yield-token now has "actual" balance @@ -120,7 +121,7 @@ Clarinet.test({ [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); position = call.result.expectOk().expectTuple(); - position['balance-token'].expectUint(99800024763); + position['balance-token'].expectUint(99800029602); position['balance-yield-token'].expectUint(2*ONE_8); position['balance-virtual'].expectUint(1000*ONE_8); @@ -144,7 +145,7 @@ Clarinet.test({ [types.uint(expiry), types.principal(yieldwbtcAddress) ], deployer.address); position = call.result.expectOk().expectTuple(); - position['balance-token'].expectUint(99900024763); + position['balance-token'].expectUint(99900029602); position['balance-yield-token'].expectUint(99971142); position['balance-virtual'].expectUint(1000*ONE_8); @@ -157,7 +158,7 @@ Clarinet.test({ // we sell close to maximum allowed of yield token result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 29*ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(2900524394); + position['dx'].expectUint(2900525361); position['dy'].expectUint(29*ONE_8); // which moves yield substantially into the positive territory. @@ -185,7 +186,7 @@ Clarinet.test({ // sell some (a lot of) yield-token result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 100*ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(10001386469); + position['dx'].expectUint(10001387748); position['dy'].expectUint(100*ONE_8); // and see how it pushes the yield pretty high @@ -204,42 +205,47 @@ Clarinet.test({ position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(100000000000); - call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", - [types.principal(deployer.address) + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance-fixed", + [types.uint(expiry), types.principal(deployer.address) ], deployer.address); call.result.expectOk().expectUint(100000000000); // Remove some liquidlity result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0.5*ONE_8); position = result.expectOk().expectTuple(); - position['dx'].expectUint(48499056950); + position['dx'].expectUint(48499058246); position['dy'].expectUint(1497121749); call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(50000000000); - call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", - [types.principal(deployer.address) + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance-fixed", + [types.uint(expiry), types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(50000000000); + call.result.expectOk().expectUint(50000000000); + + call = chain.callReadOnlyFn(yieldwbtcAddress, "get-balance-fixed", + [types.uint(expiry), types.principal(deployer.address) + ], deployer.address); + call.result.expectOk().expectUint(998400000000); // Add back some liquidity result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000*ONE_8); position = result.expectOk().expectTuple(); - position['supply'].expectUint(103094788000); + position['supply'].expectUint(103094785000); position['balance-token'].expectUint(1000*ONE_8); - position['balance-yield-token'].expectUint(3086908988); - position['balance-virtual'].expectUint(103094788085); + position['balance-yield-token'].expectUint(3086908898); + position['balance-virtual'].expectUint(103094785085); call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); - position['total-supply'].expectUint(50000000000 + 103094788000); + position['total-supply'].expectUint(50000000000 + 103094785000); - call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", - [types.principal(deployer.address) + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance-fixed", + [types.uint(expiry), types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(50000000000 + 103094788000); + call.result.expectOk().expectUint(50000000000 + 103094785000); // simulate to right before expiry chain.mineEmptyBlockUntil((expiry / ONE_8) - 1); @@ -261,7 +267,7 @@ Clarinet.test({ result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 19*ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(19*ONE_8); - position['dy'].expectUint(1900033901); + position['dy'].expectUint(1900033809); // on expiry, the prices are back to parity. call = chain.callReadOnlyFn("yield-token-pool", "get-price", @@ -281,28 +287,28 @@ Clarinet.test({ // Check pool details and print call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); - position['total-supply'].expectUint(153094788000); - position['balance-token'].expectUint(150399056950); - position['balance-yield-token'].expectUint(2683996920); - position['balance-virtual'].expectUint(153094788043); + position['total-supply'].expectUint(153094785000); + position['balance-token'].expectUint(150399058247); + position['balance-yield-token'].expectUint(2683996922); + position['balance-virtual'].expectUint(153094785043); - call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance", - [types.principal(deployer.address) + call = chain.callReadOnlyFn(ytpyieldwbtcAddress, "get-balance-fixed", + [types.uint(expiry), types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(153094788000); + call.result.expectOk().expectUint(153094785000); // Remove all liquidlity result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, ONE_8); position =result.expectOk().expectTuple(); - position['dx'].expectUint(150399056950); - position['dy'].expectUint(2683996806); + position['dx'].expectUint(150399058247); + position['dy'].expectUint(2683996755); // Check pool details and print call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(0); position['balance-token'].expectUint(0); - position['balance-yield-token'].expectUint(114); + position['balance-yield-token'].expectUint(167); position['balance-virtual'].expectUint(0); }, }); @@ -324,7 +330,7 @@ Clarinet.test({ result.expectOk().expectBool(true); //if wrong pool token is supplied, then throw an error - result = YTPTest.addToPosition(deployer, wrongExpiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 10*ONE_8); + result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, wrongPooltokenAddress, 10*ONE_8); result.expectErr().expectUint(2023); // non-deployer can add liquidity @@ -332,7 +338,7 @@ Clarinet.test({ result.expectOk(); //if wrong pool token is supplied, throw an error - result = YTPTest.reducePosition(deployer, wrongExpiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, ONE_8); + result = YTPTest.reducePosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, wrongPooltokenAddress, ONE_8); result.expectErr().expectUint(2023); } @@ -344,9 +350,13 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; let YTPTest = new YTPTestAgent1(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); + result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print @@ -363,12 +373,12 @@ Clarinet.test({ call = await YTPTest.getXgivenYield(expiry, yieldwbtcAddress, 0.001*ONE_8); call.result.expectErr().expectUint(2002); call = await YTPTest.getYgivenYield(expiry, yieldwbtcAddress, 0.001*ONE_8); - call.result.expectOk().expectUint(7019668000); + call.result.expectOk().expectUint(7019786000); - result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 7019668000, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 7019786000, 0); position = result.expectOk().expectTuple(); - position['dy'].expectUint(7019668000); - position['dx'].expectUint(7023140005); + position['dy'].expectUint(7019786000); + position['dx'].expectUint(7023258477); call = await YTPTest.getYield(expiry, yieldwbtcAddress); call.result.expectOk().expectUint(100006); @@ -377,12 +387,12 @@ Clarinet.test({ call = await YTPTest.getYgivenYield(expiry, yieldwbtcAddress, 0.0005*ONE_8); call.result.expectErr().expectUint(2002); call = await YTPTest.getXgivenYield(expiry, yieldwbtcAddress, 0.0005*ONE_8); - call.result.expectOk().expectUint(3504368516); + call.result.expectOk().expectUint(3504433783); - result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 3504368516, 0); + result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 3504433783, 0); position = result.expectOk().expectTuple(); - position['dy'].expectUint(3507017732); - position['dx'].expectUint(3504368516); + position['dy'].expectUint(3507080102); + position['dx'].expectUint(3504433783); call = await YTPTest.getYield(expiry, yieldwbtcAddress); call.result.expectOk().expectUint(50003); @@ -402,6 +412,11 @@ Clarinet.test({ let ytpPoolToken = new YTP_YIELD_WBTC(chain, deployer); let usdaToken = new USDAToken(chain, deployer); let wbtcToken = new WBTCToken(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); + result.expectOk().expectBool(true); + const buffer = new ArrayBuffer(34) const feeRateX = 0.1*ONE_8; // 10% const feeRateY = 0.1*ONE_8; @@ -412,7 +427,7 @@ Clarinet.test({ money.expectOk() //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print @@ -519,6 +534,11 @@ Clarinet.test({ let ytpPoolToken = new YTP_YIELD_WBTC(chain, deployer); let usdaToken = new USDAToken(chain, deployer); let wbtcToken = new WBTCToken(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); + result.expectOk().expectBool(true); + const buffer = new ArrayBuffer(0) // Optional memo let money = usdaToken.transferToken(10*ONE_8,deployer.address,wallet_2.address, buffer); @@ -526,7 +546,7 @@ Clarinet.test({ money.expectOk() //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Duplicated Pool From 0cda6fce2df8dbacea55126e9695091d1a9adee0 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 15 Nov 2021 14:37:09 +0800 Subject: [PATCH 10/25] yield-token-pool test passes --- .../contracts/key-token/key-usda-wbtc.clar | 2 +- .../multisig/multisig-crp-usda-wbtc.clar | 33 ++++-- .../multisig-fwp-wbtc-usda-50-50.clar | 26 +++-- .../multisig-lbp-alex-usda-90-10.clar | 29 +++-- .../multisig/multisig-ytp-yield-usda.clar | 30 ++++-- .../multisig/multisig-ytp-yield-wbtc.clar | 41 ++++--- .../pool-token/fwp-wbtc-usda-50-50.clar | 2 +- .../pool-token/lbp-alex-usda-90-10.clar | 2 +- .../contracts/pool-token/ytp-yield-usda.clar | 2 +- .../contracts/pool-token/ytp-yield-wbtc.clar | 2 +- clarity/contracts/token/token-alex.clar | 2 +- clarity/contracts/token/token-t-alex.clar | 2 +- clarity/contracts/token/token-usda.clar | 2 +- clarity/contracts/token/token-wbtc.clar | 6 +- clarity/contracts/token/token-wstx.clar | 2 +- clarity/contracts/yield-token/yield-usda.clar | 2 +- clarity/contracts/yield-token/yield-wbtc.clar | 2 +- .../tests/collateral-rebalancing-pool_test.ts | 6 +- clarity/tests/models/alex-tests-tokens.ts | 24 ++--- clarity/tests/yield-token-pool_test.ts | 102 +++++++++++------- 20 files changed, 198 insertions(+), 121 deletions(-) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index d3460f58..77995cbd 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar index 2ea49eff..293042ae 100644 --- a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar @@ -121,11 +121,11 @@ ) (let ( - (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda get-balance expiry tx-sender))) - (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance expiry tx-sender))) + (proposer-yield-balance (unwrap-panic (contract-call? .yield-usda get-balance-fixed expiry tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-usda-wbtc get-balance-fixed expiry tx-sender))) (proposer-balance (+ proposer-yield-balance proposer-key-balance)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply-fixed expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply-fixed expiry))) (total-supply (+ total-yield-supply total-key-supply)) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -172,7 +172,7 @@ (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 expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -203,7 +203,7 @@ ;; 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 expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -226,10 +226,10 @@ (proposal (get-proposal-by-id proposal-id)) (expiry (get expiry proposal)) (threshold-percent (var-get threshold)) - (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply expiry))) - (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply expiry))) - (total-supply (* (+ total-yield-supply total-key-supply) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (total-yield-supply (unwrap-panic (contract-call? .yield-usda get-total-supply-fixed expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-usda-wbtc get-total-supply-fixed expiry))) + (total-supply (+ total-yield-supply total-key-supply)) + (threshold-count (mul-up total-supply threshold-percent)) (yes-votes (get yes-votes proposal)) ) @@ -261,7 +261,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) @@ -282,3 +282,14 @@ (ok true) ) ) +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar b/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar index 55226d7d..5d804c65 100644 --- a/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar @@ -117,8 +117,8 @@ (new-fee-rate-y uint) ) (let ( - (proposer-balance (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-balance tx-sender))) - (total-supply (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-total-supply))) + (proposer-balance (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-balance-fixed tx-sender))) + (total-supply (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-total-supply-fixed))) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -163,7 +163,7 @@ (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)) + (try! (contract-call? token transfer-fixed amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -193,7 +193,7 @@ ;; 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)) + (try! (contract-call? token transfer-fixed amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -214,8 +214,8 @@ (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? .fwp-wbtc-usda-50-50 get-total-supply))) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (total-supply (unwrap-panic (contract-call? .fwp-wbtc-usda-50-50 get-total-supply-fixed))) + (threshold-count (mul-up total-supply threshold-percent)) (yes-votes (get yes-votes proposal)) ) @@ -247,7 +247,7 @@ (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))) + (try! (as-contract (contract-call? token transfer-fixed token-count (as-contract tx-sender) member none))) (ok true) ) ) @@ -267,3 +267,15 @@ (ok true) ) ) + +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) diff --git a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar index ba77462a..4d2c5ce9 100644 --- a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar +++ b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar @@ -117,8 +117,8 @@ (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))) + (proposer-balance (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-balance-fixed tx-sender))) + (total-supply (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-total-supply-fixed))) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -153,8 +153,7 @@ (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 @@ -163,7 +162,7 @@ (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)) + (try! (contract-call? token transfer-fixed amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -193,7 +192,7 @@ ;; 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)) + (try! (contract-call? token transfer-fixed amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -214,8 +213,8 @@ (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 (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (total-supply (unwrap-panic (contract-call? .lbp-alex-usda-90-10 get-total-supply-fixed))) + (threshold-count (mul-up total-supply threshold-percent)) (yes-votes (get yes-votes proposal)) ) @@ -247,7 +246,19 @@ (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))) + (try! (as-contract (contract-call? token transfer-fixed token-count (as-contract tx-sender) member none))) (ok true) ) ) + +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index d1b10c76..a369b882 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -18,7 +18,6 @@ (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) @@ -121,8 +120,8 @@ (new-fee-rate-yield-token uint) ) (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-usda get-balance expiry tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) ONE_8)) + (proposer-balance (unwrap-panic (contract-call? .ytp-yield-usda get-balance-fixed expiry tx-sender))) + (total-supply (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply-fixed expiry))) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -168,7 +167,7 @@ (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 expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -202,7 +201,7 @@ ;; 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 expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -225,8 +224,8 @@ (proposal (get-proposal-by-id proposal-id)) (expiry (get expiry proposal)) (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply expiry)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (total-supply (unwrap-panic (contract-call? .ytp-yield-usda get-total-supply-fixed expiry))) + (threshold-count (mul-up total-supply threshold-percent)) (yes-votes (get yes-votes proposal)) ) @@ -240,7 +239,8 @@ ;; Execute the proposal when the yes-vote passes threshold-count. (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) + (ok true) + ) ) ;; Return votes to voter(member) @@ -258,7 +258,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) @@ -279,3 +279,15 @@ (ok true) ) ) + +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar index ad14884d..ba3ba37d 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar @@ -108,7 +108,6 @@ (is-eq (contract-of token) .ytp-yield-wbtc) ) - ;; Start a proposal ;; Requires 10% of the supply in your wallet ;; Default voting period is 10 days (144 * 10 blocks) @@ -121,8 +120,8 @@ (new-fee-rate-yield-token uint) ) (let ( - (proposer-balance (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance expiry tx-sender)) ONE_8)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) ONE_8)) + (proposer-balance (unwrap-panic (contract-call? .ytp-yield-wbtc get-balance-fixed expiry tx-sender))) + (total-supply (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply-fixed expiry))) (proposal-id (+ u1 (var-get proposal-count))) ) @@ -168,7 +167,7 @@ (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 expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals { id: proposal-id } @@ -181,12 +180,8 @@ { amount: (+ amount token-count)}) (ok amount) - - ) ) - - - +) (define-public (vote-against (token ) (proposal-id uint) (amount uint)) (let ( @@ -202,7 +197,7 @@ ;; 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 expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender) none)) ;; Mutate (map-set proposals @@ -215,9 +210,8 @@ { proposal-id: proposal-id, member: tx-sender, token: (contract-of token), expiry: expiry } { amount: (+ amount token-count)}) (ok amount) - ) - - ) + ) +) (define-public (end-proposal (proposal-id uint)) (let @@ -225,8 +219,8 @@ (proposal (get-proposal-by-id proposal-id)) (expiry (get expiry proposal)) (threshold-percent (var-get threshold)) - (total-supply (* (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply expiry)) ONE_8)) - (threshold-count (contract-call? .math-fixed-point mul-up total-supply threshold-percent)) + (total-supply (unwrap-panic (contract-call? .ytp-yield-wbtc get-total-supply-fixed expiry))) + (threshold-count (mul-up total-supply threshold-percent)) (yes-votes (get yes-votes proposal)) ) @@ -240,7 +234,8 @@ ;; Execute the proposal when the yes-vote passes threshold-count. (and (> yes-votes threshold-count) (try! (execute-proposal proposal-id))) - (ok true)) + (ok true) + ) ) ;; Return votes to voter(member) @@ -258,7 +253,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) (ok true) ) ) @@ -279,3 +274,15 @@ (ok true) ) ) + +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) diff --git a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar index 78054449..c73ccc76 100644 --- a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u8) + (ok u6) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar index d4d79354..2f84ff2f 100644 --- a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar +++ b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u8) + (ok u6) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index 21c0f6d7..6ee9401e 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u8) + (ok u6) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar index 7356e5b7..60ac5621 100644 --- a/clarity/contracts/pool-token/ytp-yield-wbtc.clar +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u8) + (ok u6) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/token/token-alex.clar b/clarity/contracts/token/token-alex.clar index 3f6c9a90..d2bb9f3b 100644 --- a/clarity/contracts/token/token-alex.clar +++ b/clarity/contracts/token/token-alex.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-t-alex.clar b/clarity/contracts/token/token-t-alex.clar index 8f89333f..20ae6fc9 100644 --- a/clarity/contracts/token/token-t-alex.clar +++ b/clarity/contracts/token/token-t-alex.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-usda.clar b/clarity/contracts/token/token-usda.clar index 4b10309a..41ed61bd 100644 --- a/clarity/contracts/token/token-usda.clar +++ b/clarity/contracts/token/token-usda.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-wbtc.clar b/clarity/contracts/token/token-wbtc.clar index 702d4d7c..22c3de8b 100644 --- a/clarity/contracts/token/token-wbtc.clar +++ b/clarity/contracts/token/token-wbtc.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-balance (account principal)) @@ -127,6 +127,6 @@ ;; Initialize the contract for Testing. (begin - (try! (ft-mint? wbtc u2000000000000 tx-sender)) - (try! (ft-mint? wbtc u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 + (try! (ft-mint? wbtc u20000000000 tx-sender)) + (try! (ft-mint? wbtc u20000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 ) diff --git a/clarity/contracts/token/token-wstx.clar b/clarity/contracts/token/token-wstx.clar index 50923a4d..72a7609e 100644 --- a/clarity/contracts/token/token-wstx.clar +++ b/clarity/contracts/token/token-wstx.clar @@ -38,7 +38,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index 123871db..ba35b7bf 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar index 62aa6da8..8ad44caa 100644 --- a/clarity/contracts/yield-token/yield-wbtc.clar +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u0) + (ok u6) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/tests/collateral-rebalancing-pool_test.ts b/clarity/tests/collateral-rebalancing-pool_test.ts index 8e6c2b49..a22cff42 100644 --- a/clarity/tests/collateral-rebalancing-pool_test.ts +++ b/clarity/tests/collateral-rebalancing-pool_test.ts @@ -156,7 +156,7 @@ Clarinet.test({ // take away what was minted for testing to another address let block = chain.mineBlock([ - Tx.contractCall("yield-wbtc-59760", "transfer", [ + Tx.contractCall("yield-wbtc-59760", "transfer-fixed", [ types.uint(2000000000000), types.principal(deployer.address), types.principal(wallet_1.address), @@ -243,7 +243,7 @@ Clarinet.test({ // take away what was minted for testing to another address let block = chain.mineBlock([ - Tx.contractCall("yield-wbtc-59760", "transfer", [ + Tx.contractCall("yield-wbtc-59760", "transfer-fixed", [ types.uint(2000000000000), types.principal(deployer.address), types.principal(wallet_1.address), @@ -567,7 +567,7 @@ Clarinet.test({ // take away what was minted for testing to another address let block = chain.mineBlock([ - Tx.contractCall("yield-wbtc-59760", "transfer", [ + Tx.contractCall("yield-wbtc-59760", "transfer-fixed", [ types.uint(2000000000000), types.principal(deployer.address), types.principal(wallet_1.address), diff --git a/clarity/tests/models/alex-tests-tokens.ts b/clarity/tests/models/alex-tests-tokens.ts index 49fe0220..c67f2c35 100644 --- a/clarity/tests/models/alex-tests-tokens.ts +++ b/clarity/tests/models/alex-tests-tokens.ts @@ -17,14 +17,14 @@ class USDAToken { } balanceOf(wallet: string) { - return this.chain.callReadOnlyFn("token-usda", "get-balance", [ + return this.chain.callReadOnlyFn("token-usda", "get-balance-fixed", [ types.principal(wallet), ], this.deployer.address); } transferToken(amount: number, sender: string, receiver: string, memo:ArrayBuffer) { let block = this.chain.mineBlock([ - Tx.contractCall("token-usda", "transfer", [ + Tx.contractCall("token-usda", "transfer-fixed", [ types.uint(amount), types.principal(sender), types.principal(receiver), @@ -36,7 +36,7 @@ class USDAToken { totalSupply() { - return this.chain.callReadOnlyFn("token-usda", "get-total-supply", [], this.deployer.address); + return this.chain.callReadOnlyFn("token-usda", "get-total-supply-fixed", [], this.deployer.address); } } export { USDAToken }; @@ -52,14 +52,14 @@ class WBTCToken { } balanceOf(wallet: string) { - return this.chain.callReadOnlyFn("token-wbtc", "get-balance", [ + return this.chain.callReadOnlyFn("token-wbtc", "get-balance-fixed", [ types.principal(wallet), ], this.deployer.address); } transferToken(amount: number, sender: string, receiver: string, memo:ArrayBuffer) { let block = this.chain.mineBlock([ - Tx.contractCall("token-wbtc", "transfer", [ + Tx.contractCall("token-wbtc", "transfer-fixed", [ types.uint(amount), types.principal(sender), types.principal(receiver), @@ -70,7 +70,7 @@ class WBTCToken { } totalSupply() { - return this.chain.callReadOnlyFn("token-wbtc", "get-total-supply", [], this.deployer.address); + return this.chain.callReadOnlyFn("token-wbtc", "get-total-supply-fixed", [], this.deployer.address); } } export { WBTCToken }; @@ -87,13 +87,13 @@ class FWP_WBTC_USDA_5050 { } balanceOf(wallet: string) { - return this.chain.callReadOnlyFn("fwp-wbtc-usda-50-50", "get-balance", [ + return this.chain.callReadOnlyFn("fwp-wbtc-usda-50-50", "get-balance-fixed", [ types.principal(wallet), ], this.deployer.address); } totalSupply() { - return this.chain.callReadOnlyFn("fwp-wbtc-usda-50-50", "get-total-supply", [], this.deployer.address); + return this.chain.callReadOnlyFn("fwp-wbtc-usda-50-50", "get-total-supply-fixed", [], this.deployer.address); } } export { FWP_WBTC_USDA_5050 }; @@ -108,13 +108,13 @@ class YTP_YIELD_WBTC { } balanceOf(expiry: number, wallet: string) { - return this.chain.callReadOnlyFn("ytp-yield-wbtc", "get-balance", [ + return this.chain.callReadOnlyFn("ytp-yield-wbtc", "get-balance-fixed", [ types.uint(expiry), types.principal(wallet), ], this.deployer.address); } totalSupply(expiry: number) { - return this.chain.callReadOnlyFn("ytp-yield-wbtc", "get-total-supply", [ + return this.chain.callReadOnlyFn("ytp-yield-wbtc", "get-total-supply-fixed", [ types.uint(expiry) ], this.deployer.address); } @@ -131,14 +131,14 @@ class KEY_USDA_WBTC { } balanceOf(expiry: number, wallet: string) { - return this.chain.callReadOnlyFn("key-usda-wbtc", "get-balance", [ + return this.chain.callReadOnlyFn("key-usda-wbtc", "get-balance-fixed", [ types.uint(expiry), types.principal(wallet), ], this.deployer.address); } totalSupply(expiry: number) { - return this.chain.callReadOnlyFn("key-usda-wbtc", "get-total-supply", [ + return this.chain.callReadOnlyFn("key-usda-wbtc", "get-total-supply-fixed", [ types.uint(expiry) ], this.deployer.address); } diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index 57bec1b7..b5d4b12f 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -28,7 +28,7 @@ const anotherExpiry = 80875 * ONE_8 */ Clarinet.test({ - name: "YTP : Pool creation, adding values and reducing values", + name: "YTP : pool creation, adding values and reducing values", async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; @@ -228,7 +228,7 @@ Clarinet.test({ call = chain.callReadOnlyFn(yieldwbtcAddress, "get-balance-fixed", [types.uint(expiry), types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(998400000000); + call.result.expectOk().expectUint(998502878000); // Add back some liquidity result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000*ONE_8); @@ -314,7 +314,7 @@ Clarinet.test({ }); Clarinet.test({ - name: "YTP : Trait check", + name: "YTP : trait check", async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; @@ -401,7 +401,7 @@ Clarinet.test({ }); Clarinet.test({ - name: "YTP : Fee Setting using Multisig ", + name: "YTP : fee setting using multisig ", async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; @@ -433,6 +433,7 @@ Clarinet.test({ // Check pool details and print let call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); let position:any = call.result.expectOk().expectTuple(); + position['total-supply'].expectUint(1000*ONE_8); position['balance-token'].expectUint(1000*ONE_8); position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(1000*ONE_8); @@ -444,22 +445,35 @@ Clarinet.test({ position['balance-yield-token'].expectUint(0); position['balance-virtual'].expectUint(10*ONE_8); + call = chain.callReadOnlyFn(yieldwbtcAddress, "get-balance-fixed", + [types.uint(expiry), types.principal(deployer.address) + ], deployer.address); + call.result.expectOk().expectUint(1000000000000); + call = chain.callReadOnlyFn(wbtcAddress, "get-balance-fixed", + [types.principal(deployer.address) + ], deployer.address); + call.result.expectOk().expectUint(1899000000000); + call = chain.callReadOnlyFn(wbtcAddress, "get-balance-fixed", + [types.principal(deployer.address + ".alex-vault") + ], deployer.address); + call.result.expectOk().expectUint(101000000000); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(99972419); + position['dx'].expectUint(99970483); position['dy'].expectUint(ONE_8); call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); - position['balance-token'].expectUint(100900027581); // u99900045311 + position['balance-token'].expectUint(100900029517); position['balance-yield-token'].expectUint(ONE_8); position['balance-virtual'].expectUint(1010*ONE_8); call = await ytpPoolToken.balanceOf(expiry, deployer.address); - call.result.expectOk().expectUint(100000000000); // u100000000000 + call.result.expectOk().expectUint(1000*ONE_8); // u100000000000 call = await ytpPoolToken.balanceOf(expiry, wallet_2.address); - call.result.expectOk().expectUint(1000000000); + call.result.expectOk().expectUint(10*ONE_8); // Fee rate Setting Proposal of Multisig result = MultiSigTest.propose(expiry, 1000, " Fee Rate Setting to 10%", " https://docs.alexgo.io", feeRateX, feeRateY) @@ -470,7 +484,7 @@ Clarinet.test({ // Deployer has 99 % of pool token let ROresult:any = ytpPoolToken.balanceOf(expiry, deployer.address); - ROresult.result.expectOk().expectUint(100000000000); + ROresult.result.expectOk().expectUint(1000*ONE_8); // Wallet_2 votes his 90% asset result = MultiSigTest.voteFor(wallet_2, ytpyieldwbtcAddress, 1, 1000000000 * 9 / 10 ) @@ -494,7 +508,7 @@ Clarinet.test({ call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['balance-yield-token'].expectUint(100000000); - position['balance-token'].expectUint(100900027581); + position['balance-token'].expectUint(100900029517); position['balance-virtual'].expectUint(101000000000); position['fee-rate-yield-token'].expectUint(0.1*ONE_8); position['fee-rate-token'].expectUint(0.1*ONE_8); @@ -512,19 +526,19 @@ Clarinet.test({ // sell some yield-token result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 2*ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(199967096); + position['dx'].expectUint(199969031); position['dy'].expectUint(199999730); call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['balance-yield-token'].expectUint(100000000 + 199999730 + 135); //before + after + fee-rebate - position['balance-token'].expectUint(100900027581 - 199967096); + position['balance-token'].expectUint(100900029517 - 199969031); position['balance-virtual'].expectUint(101000000000); }, }); Clarinet.test({ - name: "YTP : Error Test Cases ", + name: "YTP : error test cases ", async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; @@ -536,7 +550,7 @@ Clarinet.test({ let wbtcToken = new WBTCToken(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); - let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); + let result = yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); result.expectOk().expectBool(true); const buffer = new ArrayBuffer(0) // Optional memo @@ -611,13 +625,13 @@ Clarinet.test({ // Fixed result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 0.001 * ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(36251); + position['dx'].expectUint(40101); position['dy'].expectUint(0.001 * ONE_8); // Attempt for Swapping result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(99944571); + position['dx'].expectUint(99938798); position['dy'].expectUint(ONE_8); // Attempts for zero value swapping @@ -632,7 +646,7 @@ Clarinet.test({ result = YTPTest.swapXForY(deployer, expiry, yieldwbtcAddress, wbtcAddress, 0.1 * ONE_8, 0); position =result.expectOk().expectTuple(); position['dx'].expectUint(0.1 * ONE_8); - position['dy'].expectUint(10043969); + position['dy'].expectUint(10047818); }, }); @@ -644,9 +658,13 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + let result = yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); + result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print @@ -659,14 +677,14 @@ Clarinet.test({ // inject some yield-token to pool result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 10 * ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(1000039937); + position['dx'].expectUint(1000040905); position['dy'].expectUint(10 * ONE_8); // Check pool details and print call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8); - position['balance-token'].expectUint(1000*ONE_8 - 1000039937); + position['balance-token'].expectUint(1000*ONE_8 - 1000040905); position['balance-yield-token'].expectUint(10 * ONE_8); position['balance-virtual'].expectUint(1000*ONE_8); @@ -681,8 +699,8 @@ Clarinet.test({ position = result.expectOk().expectTuple(); position['supply'].expectUint(909189000); position['balance-token'].expectUint(900997112); - position['balance-yield-token'].expectUint(8191445); - position['balance-virtual'].expectUint(909189002); + position['balance-yield-token'].expectUint(8191455); + position['balance-virtual'].expectUint(909189001); } }); @@ -693,9 +711,15 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + let result = yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); + result.expectOk().expectBool(true); + result = yieldWBTC.mintFixed(anotherExpiry, 100 * ONE_8, deployer.address); + result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - let result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); result.expectOk().expectBool(true); // Check pool details and print @@ -708,14 +732,14 @@ Clarinet.test({ // inject some yield-token to pool result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 10 * ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(1000039937); + position['dx'].expectUint(1000041873); position['dy'].expectUint(10 * ONE_8); // Check pool details and print call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8); - position['balance-token'].expectUint(1000*ONE_8 - 1000039937); + position['balance-token'].expectUint(1000*ONE_8 - 1000041873); position['balance-yield-token'].expectUint(10 * ONE_8); position['balance-virtual'].expectUint(1000*ONE_8); @@ -731,32 +755,32 @@ Clarinet.test({ // inject some yield-token to pool result = YTPTest.swapYForX(deployer, anotherExpiry, yieldwbtcAddress, wbtcAddress, 10 * ONE_8, 0); position =result.expectOk().expectTuple(); - position['dx'].expectUint(1000067035); + position['dx'].expectUint(1000068003); position['dy'].expectUint(10 * ONE_8); - call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); + call = await YTPTest.getPoolDetails(anotherExpiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(1000*ONE_8); - position['balance-token'].expectUint(1000*ONE_8 - 1000067035); + position['balance-token'].expectUint(1000*ONE_8 - 1000068003); position['balance-yield-token'].expectUint(10 * ONE_8); position['balance-virtual'].expectUint(1000*ONE_8); //Add extra liquidity with secondary buying of yield-token result = YTPTest.rollPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 0.5*ONE_8, anotherExpiry); position = result.expectOk().expectTuple(); - position['supply'].expectUint(50354658000); - position['balance-token'].expectUint(49900929799); - position['balance-yield-token'].expectUint(453675742); - position['balance-virtual'].expectUint(50354658105); + position['supply'].expectUint(50354656000); + position['balance-token'].expectUint(49900926876); + position['balance-yield-token'].expectUint(453674699); + position['balance-virtual'].expectUint(50354656156); // Check pool details and print - call = await YTPTest.getPoolDetails(expiry, yieldwbtcAddress); + call = await YTPTest.getPoolDetails(anotherExpiry, yieldwbtcAddress); position = call.result.expectOk().expectTuple(); - position['total-supply'].expectUint(1000*ONE_8 + 50354658000); - // a bit more than 148900862764 = 1000*ONE_8 - 1000067035 + 49900929799, due to buy-and-add-to-position - position['balance-token'].expectUint(148999862966); - // a bit less then 1453675742 = 0 + 10 * ONE_8 + 453675742, due to buy-and-add-to-position - position['balance-yield-token'].expectUint(1354636777); - position['balance-virtual'].expectUint(1000*ONE_8 + 50354658105); + position['total-supply'].expectUint(1000*ONE_8 + 50354656000); + // a bit more than 148900862764 = 1000*ONE_8 - 1000067035 + 49900926876, due to buy-and-add-to-position + position['balance-token'].expectUint(148999859079); + // a bit less then 1453675742 = 0 + 10 * ONE_8 + 453674699, due to buy-and-add-to-position + position['balance-yield-token'].expectUint(1354633799); + position['balance-virtual'].expectUint(1000*ONE_8 + 50354656156); } }); \ No newline at end of file From b0ffac786054a5ef957343d26fc9912a4cb96148 Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Mon, 15 Nov 2021 13:33:52 +0500 Subject: [PATCH 11/25] Added contract calls in models for minting. - Test case files has bugs in imports - A change in token-wstx and token-usda get-decimals is questionable - Added mintFixed for now --- clarity/contracts/token/token-usda.clar | 7 -- clarity/contracts/token/token-wbtc.clar | 6 -- clarity/contracts/token/token-wstx.clar | 6 -- .../tests/collateral-rebalancing-pool_test.ts | 11 +++ clarity/tests/fixed-weight-pool_test.ts | 16 +++- clarity/tests/flash-loan_test.ts | 19 +++- clarity/tests/models/alex-tests-tokens.ts | 95 +++++++++++++++++++ 7 files changed, 135 insertions(+), 25 deletions(-) diff --git a/clarity/contracts/token/token-usda.clar b/clarity/contracts/token/token-usda.clar index 41ed61bd..3405bc79 100644 --- a/clarity/contracts/token/token-usda.clar +++ b/clarity/contracts/token/token-usda.clar @@ -124,10 +124,3 @@ (begin (map-set approved-contracts .faucet true) ) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? usda u1000000000 tx-sender)) - (try! (ft-mint? usda u10000000 .usda-reserve-pool)) - (try! (ft-mint? usda u200000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) diff --git a/clarity/contracts/token/token-wbtc.clar b/clarity/contracts/token/token-wbtc.clar index 22c3de8b..99041bff 100644 --- a/clarity/contracts/token/token-wbtc.clar +++ b/clarity/contracts/token/token-wbtc.clar @@ -124,9 +124,3 @@ (begin (map-set approved-contracts .faucet true) ) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? wbtc u20000000000 tx-sender)) - (try! (ft-mint? wbtc u20000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 -) diff --git a/clarity/contracts/token/token-wstx.clar b/clarity/contracts/token/token-wstx.clar index 72a7609e..59972ad2 100644 --- a/clarity/contracts/token/token-wstx.clar +++ b/clarity/contracts/token/token-wstx.clar @@ -119,10 +119,4 @@ (define-public (burn-fixed (amount uint) (sender principal)) (burn (fixed-to-decimals amount) sender) -) - -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? wstx u2000000000000 tx-sender)) - (try! (ft-mint? wstx u2000000000000 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK)) ;;wallet_1 ) \ No newline at end of file diff --git a/clarity/tests/collateral-rebalancing-pool_test.ts b/clarity/tests/collateral-rebalancing-pool_test.ts index a22cff42..77da627e 100644 --- a/clarity/tests/collateral-rebalancing-pool_test.ts +++ b/clarity/tests/collateral-rebalancing-pool_test.ts @@ -29,6 +29,7 @@ const reserveAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-p const keywbtc59760wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-wbtc-59760-wbtc" const multisigncrpwbtc59760wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-wbtc-59760-wbtc" const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda-59760" +const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" const ONE_8 = 100000000 const expiry = 59760 * ONE_8 @@ -60,6 +61,16 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000); + usdaToken.mintFixed(alexReservePoolAddress, 10000000); + usdaToken.mintFixed(wallet_1.address, 200000); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); diff --git a/clarity/tests/fixed-weight-pool_test.ts b/clarity/tests/fixed-weight-pool_test.ts index 0ff1d418..29a9a308 100644 --- a/clarity/tests/fixed-weight-pool_test.ts +++ b/clarity/tests/fixed-weight-pool_test.ts @@ -7,7 +7,7 @@ import { MS_FWP_WBTC_USDA_5050 } from './models/alex-tests-multisigs.ts'; import { USDAToken, WBTCToken, - POOLTOKEN_FWP_WBTC_USDA_5050 + FWP_WBTC_USDA_5050 } from './models/alex-tests-tokens.ts'; // Deployer Address Constants @@ -17,6 +17,7 @@ const fwpwbtcusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fwp-wbtc-u const multisigAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-fwp-wbtc-usda-50-50" const fwpAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fixed-weight-pool" const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda-23040-usda" +const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" const ONE_8 = 100000000 @@ -34,7 +35,18 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; + let wallet_1 = accounts.get("wallet_1")!; let FWPTest = new FWPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000); + usdaToken.mintFixed(alexReservePoolAddress, 10000000); + usdaToken.mintFixed(wallet_1.address, 200000); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) // Deployer creating a pool, initial tokens injected to the pool let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigAddress, wbtcQ, wbtcQ*wbtcPrice); @@ -150,7 +162,7 @@ Clarinet.test({ let MultiSigTest = new MS_FWP_WBTC_USDA_5050(chain, deployer); let usdaToken = new USDAToken(chain, deployer); let wbtcToken = new WBTCToken(chain, deployer); - let fwpPoolToken = new POOLTOKEN_FWP_WBTC_USDA_5050(chain, deployer); + let fwpPoolToken = new FWP_WBTC_USDA_5050(chain, deployer); const feeRateX = 0.1*ONE_8; // 10% const feeRateY = 0.1*ONE_8; diff --git a/clarity/tests/flash-loan_test.ts b/clarity/tests/flash-loan_test.ts index ce75d446..910ba0b2 100644 --- a/clarity/tests/flash-loan_test.ts +++ b/clarity/tests/flash-loan_test.ts @@ -7,7 +7,7 @@ import { CRPTestAgent1 } from './models/alex-tests-collateral-rebalancing-pool.t import { FWPTestAgent1 } from './models/alex-tests-fixed-weight-pool.ts'; import { YTPTestAgent1 } from './models/alex-tests-yield-token-pool.ts'; import { FLTestAgent1 } from './models/alex-tests-flash-loan.ts'; -import { WBTCToken } from './models/alex-tests-tokens.ts'; +import { WBTCToken, USDAToken } from './models/alex-tests-tokens.ts'; // Deployer Address Constants const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" @@ -26,6 +26,7 @@ const ytpyieldusda51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp- const multisigncrpusda51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-usda-51840-wbtc" const multisigytpyieldusda51840 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-usda-51840-usda" const loanuser51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.flash-loan-user-margin-wbtc-usda-51840" +const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" const ONE_8 = 100000000 const expiry = 23040e+8 @@ -50,12 +51,22 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; + let wallet_1 = accounts.get("wallet_1")!; let wallet_5 = accounts.get("wallet_5")!; let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); let FLTest = new FLTestAgent1(chain, deployer); - let wbtcToken = new WBTCToken(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000); + usdaToken.mintFixed(alexReservePoolAddress, 10000000); + usdaToken.mintFixed(wallet_1.address, 200000); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) wbtcToken.transferToken(ONE_8,deployer.address, wallet_5.address, new ArrayBuffer(30)); @@ -69,7 +80,7 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, yieldusda23040Address, usdaAddress, ytpyieldusda23040Address, multisigytpyieldusda23040, 500000e+8, 500000e+8); + result = YTPTest.createPool(deployer, expiry, yieldusda23040Address, usdaAddress, ytpyieldusda23040Address, multisigytpyieldusda23040, 500000e+8, 500000e+8); result.expectOk().expectBool(true); result = CRPTest.createPool(deployer, usdaAddress, wbtcAddress, yieldusda23040Address, keyusda23040Address, multisigncrpusda23040Address, ltv_0, conversion_ltv, bs_vol, moving_average, 1e+8); result.expectOk().expectBool(true); @@ -103,7 +114,7 @@ Clarinet.test({ result.expectErr().expectUint(2017); // but let's set up new pools - result = YTPTest.createPool(deployer, yieldusda51840Address, usdaAddress, ytpyieldusda51840Address, multisigytpyieldusda51840, 500000e+8, 500000e+8); + result = YTPTest.createPool(deployer, expiry, yieldusda51840Address, usdaAddress, ytpyieldusda51840Address, multisigytpyieldusda51840, 500000e+8, 500000e+8); result.expectOk().expectBool(true); result = CRPTest.createPool(deployer, usdaAddress, wbtcAddress, yieldusda51840Address, keyusda51840Address, multisigncrpusda51840Address, ltv_0, conversion_ltv, bs_vol, moving_average, 1e+8); result.expectOk().expectBool(true); diff --git a/clarity/tests/models/alex-tests-tokens.ts b/clarity/tests/models/alex-tests-tokens.ts index c67f2c35..8d8c870a 100644 --- a/clarity/tests/models/alex-tests-tokens.ts +++ b/clarity/tests/models/alex-tests-tokens.ts @@ -22,6 +22,27 @@ class USDAToken { ], this.deployer.address); } + getBalance(account: string) { + return this.chain.callReadOnlyFn("token-usda", "get-balance", [ + types.principal(account), + ], this.deployer.address); + } + + // Always need to called by deployer + mint(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-usda", "mint", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + + mintFixed(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-usda", "mint-fixed", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + transferToken(amount: number, sender: string, receiver: string, memo:ArrayBuffer) { let block = this.chain.mineBlock([ Tx.contractCall("token-usda", "transfer-fixed", [ @@ -56,6 +77,27 @@ class WBTCToken { types.principal(wallet), ], this.deployer.address); } + + getBalance(account: string) { + return this.chain.callReadOnlyFn("token-wbtc", "get-balance", [ + types.principal(account), + ], this.deployer.address); + } + + // Always need to called by deployer + mint(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-wbtc", "mint", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + + mintFixed(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-wbtc", "mint-fixed", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } transferToken(amount: number, sender: string, receiver: string, memo:ArrayBuffer) { let block = this.chain.mineBlock([ @@ -75,7 +117,60 @@ class WBTCToken { } export { WBTCToken }; +class WSTXToken { + chain: Chain; + deployer: Account; + + constructor(chain: Chain, deployer: Account) { + this.chain = chain; + this.deployer = deployer; + } + + balanceOf(wallet: string) { + return this.chain.callReadOnlyFn("token-wstx", "get-balance-fixed", [ + types.principal(wallet), + ], this.deployer.address); + } + + getBalance(account: string) { + return this.chain.callReadOnlyFn("token-wstx", "get-balance", [ + types.principal(account), + ], this.deployer.address); + } + + // Always need to called by deployer + mint(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-wstx", "mint", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + mintFixed(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-wstx", "mint-fixed", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + + transferToken(amount: number, sender: string, receiver: string, memo:ArrayBuffer) { + let block = this.chain.mineBlock([ + Tx.contractCall("token-wstx", "transfer-fixed", [ + types.uint(amount), + types.principal(sender), + types.principal(receiver), + types.some(types.buff(memo)) + ], this.deployer.address), + ]); + return block.receipts[0].result; + } + + + totalSupply() { + return this.chain.callReadOnlyFn("token-wstx", "get-total-supply-fixed", [], this.deployer.address); + } +} +export { WSTXToken }; class FWP_WBTC_USDA_5050 { chain: Chain; From b5de0c3f4294b383cc18956aab9dbd0473518470 Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Mon, 15 Nov 2021 16:53:23 +0500 Subject: [PATCH 12/25] Fixed the bugs in test files --- .../tests/collateral-rebalancing-pool_test.ts | 44 ++++++++++--------- clarity/tests/flash-loan_test.ts | 4 +- clarity/tests/models/alex-tests-tokens.ts | 12 +++++ clarity/tests/yield-token-pool_test.ts | 32 +++++++++++++- 4 files changed, 69 insertions(+), 23 deletions(-) diff --git a/clarity/tests/collateral-rebalancing-pool_test.ts b/clarity/tests/collateral-rebalancing-pool_test.ts index 77da627e..7ab347c7 100644 --- a/clarity/tests/collateral-rebalancing-pool_test.ts +++ b/clarity/tests/collateral-rebalancing-pool_test.ts @@ -6,8 +6,12 @@ import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts'; import { CRPTestAgent1 } from './models/alex-tests-collateral-rebalancing-pool.ts'; import { FWPTestAgent1 } from './models/alex-tests-fixed-weight-pool.ts'; import { YTPTestAgent1 } from './models/alex-tests-yield-token-pool.ts'; -import { MS_CRP_WBTC_USDA_59760} from './models/alex-tests-multisigs.ts'; -import { USDAToken,WBTCToken,YIELD_WBTC_59760,KEY_WBTC_59760_USDA } from './models/alex-tests-tokens.ts'; +import { MS_CRP_USDA_WBTC} from './models/alex-tests-multisigs.ts'; +import { + USDAToken, + WBTCToken, + YIELD_WBTC, + KEY_USDA_WBTC } from './models/alex-tests-tokens.ts'; // Deployer Address Constants const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" @@ -85,7 +89,7 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool @@ -223,7 +227,7 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); // non-deployer creating a pool will throw an error @@ -292,7 +296,7 @@ Clarinet.test({ position['balance-x'].expectUint(wbtcQ); position['balance-y'].expectUint(Math.round(wbtcQ * wbtcPrice / ONE_8)); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool @@ -302,7 +306,7 @@ Clarinet.test({ // simulate to half way to expiry chain.mineEmptyBlockUntil((expiry / ONE_8) / 2) - result = YTPTest.createPool(deployer, yieldwbtc79760Address, wbtcAddress, ytpyieldwbtc79760Address, multisigytpyieldwbtc79760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc79760Address, wbtcAddress, ytpyieldwbtc79760Address, multisigytpyieldwbtc79760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc79760Address, keywbtc79760Address, multisigncrpwbtc79760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); @@ -333,7 +337,7 @@ Clarinet.test({ position['balance-x'].expectUint(wbtcQ); position['balance-y'].expectUint(Math.round(wbtcQ * wbtcPrice / ONE_8)); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); let moving_average_0 = 0.95e+8 @@ -395,14 +399,14 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ, wbtcQ); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ, wbtcQ); result.expectOk().expectBool(true); // sell some yield-token to create a positive yield - result = YTPTest.swapYForX(deployer, yieldwbtc59760Address, wbtcAddress, 5*ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtc59760Address, wbtcAddress, 5*ONE_8, 0); let position:any = result.expectOk().expectTuple(); - let call = await YTPTest.getPrice(yieldwbtc59760Address); + let call = await YTPTest.getPrice(expiry, yieldwbtc59760Address); call.result.expectOk().expectUint(100071157); let ltv_00 = Math.round(ONE_8 * ONE_8 / 109095981); @@ -461,7 +465,7 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool @@ -547,9 +551,9 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); - let MultiSigTest = new MS_CRP_WBTC_USDA_59760(chain, deployer); - let YieldToken = new YIELD_WBTC_59760(chain, deployer); - let KeyToken = new KEY_WBTC_59760_USDA(chain, deployer); + let MultiSigTest = new MS_CRP_USDA_WBTC(chain, deployer); + let YieldToken = new YIELD_WBTC(chain, deployer); + let KeyToken = new KEY_USDA_WBTC(chain, deployer); const feeRateX = 0.1*ONE_8; // 10% const feeRateY = 0.1*ONE_8; const feeRebate = 0.5*ONE_8; @@ -566,14 +570,14 @@ Clarinet.test({ position['balance-x'].expectUint(wbtcQ); position['balance-y'].expectUint(Math.round(wbtcQ * wbtcPrice / ONE_8)); - result = YTPTest.createPool(deployer, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); - let ROresult:any = YieldToken.totalSupply() + let ROresult:any = YieldToken.totalSupply(expiry) ROresult.result.expectOk().expectUint(2000080807360); // take away what was minted for testing to another address @@ -587,16 +591,16 @@ Clarinet.test({ ]); block.receipts[0].result.expectOk(); - ROresult = YieldToken.balanceOf(deployer.address) + ROresult = YieldToken.balanceOf(expiry, deployer.address) ROresult.result.expectOk().expectUint(80807360); - ROresult = KeyToken.totalSupply() + ROresult = KeyToken.totalSupply(expiry) ROresult.result.expectOk().expectUint(80807360); - ROresult = KeyToken.balanceOf(deployer.address) + ROresult = KeyToken.balanceOf(expiry, deployer.address) ROresult.result.expectOk().expectUint(80807360); // Fee rate Setting Proposal of Multisig - result = MultiSigTest.propose(wallet_1,1000, " Fee Rate Setting to 10%", " https://docs.alexgo.io", feeRateX, feeRateY) + result = MultiSigTest.propose(wallet_1, expiry, 1000, " Fee Rate Setting to 10%", " https://docs.alexgo.io", feeRateX, feeRateY) result.expectOk().expectUint(1) // First Proposal // Block 1000 mining diff --git a/clarity/tests/flash-loan_test.ts b/clarity/tests/flash-loan_test.ts index 910ba0b2..979d9f4f 100644 --- a/clarity/tests/flash-loan_test.ts +++ b/clarity/tests/flash-loan_test.ts @@ -64,14 +64,14 @@ Clarinet.test({ usdaToken.mintFixed(deployer.address, 1000000000); usdaToken.mintFixed(alexReservePoolAddress, 10000000); usdaToken.mintFixed(wallet_1.address, 200000); - + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) wbtcToken.transferToken(ONE_8,deployer.address, wallet_5.address, new ArrayBuffer(30)); let call = await FLTest.getBalance(wbtcAddress, wallet_5.address); - let position:any = call.result.expectOk().expectUint(ONE_8); + let position:any = call.result.expectOk().expectUint(1000000); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, Math.round(500000e+8 * ONE_8 / wbtcPrice), 500000e+8); result.expectOk().expectBool(true); diff --git a/clarity/tests/models/alex-tests-tokens.ts b/clarity/tests/models/alex-tests-tokens.ts index 8d8c870a..a2c1335e 100644 --- a/clarity/tests/models/alex-tests-tokens.ts +++ b/clarity/tests/models/alex-tests-tokens.ts @@ -248,6 +248,18 @@ class YIELD_WBTC { this.chain = chain; this.deployer = deployer; } + + balanceOf(expiry: number, wallet: string) { + return this.chain.callReadOnlyFn("yield-wbtc", "get-balance-fixed", [ + types.uint(expiry), types.principal(wallet), + ], this.deployer.address); + } + + totalSupply(expiry: number) { + return this.chain.callReadOnlyFn("yield-wbtc", "get-total-supply-fixed", [ + types.uint(expiry) + ], this.deployer.address); + } mintFixed(expiry: number, amount: number, recipient: string) { let block = this.chain.mineBlock([ diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index b5d4b12f..5ff15c79 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -32,8 +32,13 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; + let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); result.expectOk().expectBool(true); @@ -320,6 +325,10 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) //if non-deployer attempts to create a pool, throw an error. let result = YTPTest.createPool(wallet_1, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, 1000*ONE_8, 1000*ONE_8); @@ -349,8 +358,13 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; + let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); result.expectOk().expectBool(true); @@ -405,6 +419,7 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; + let wallet_1 = accounts.get("wallet_1")!; let wallet_2 = accounts.get("wallet_2")!; let contractOwner = accounts.get("deployer")!; let YTPTest = new YTPTestAgent1(chain, deployer); @@ -414,6 +429,9 @@ Clarinet.test({ let wbtcToken = new WBTCToken(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) + let result = yieldWBTC.mintFixed(expiry, 10000 * ONE_8, deployer.address); result.expectOk().expectBool(true); @@ -452,7 +470,7 @@ Clarinet.test({ call = chain.callReadOnlyFn(wbtcAddress, "get-balance-fixed", [types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(1899000000000); + call.result.expectOk().expectUint(899000000000); //1899000000000 call = chain.callReadOnlyFn(wbtcAddress, "get-balance-fixed", [types.principal(deployer.address + ".alex-vault") ], deployer.address); @@ -542,6 +560,7 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; + let wallet_1 = accounts.get("wallet_1")!; let wallet_2 = accounts.get("wallet_2")!; let YTPTest = new YTPTestAgent1(chain, deployer); let MultiSigTest = new MS_YTP_YIELD_WBTC(chain, deployer); @@ -550,6 +569,9 @@ Clarinet.test({ let wbtcToken = new WBTCToken(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) + let result = yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); result.expectOk().expectBool(true); @@ -659,6 +681,10 @@ Clarinet.test({ let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) let result = yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); result.expectOk().expectBool(true); @@ -712,7 +738,11 @@ Clarinet.test({ let wallet_1 = accounts.get("wallet_1")!; let YTPTest = new YTPTestAgent1(chain, deployer); let yieldWBTC = new YIELD_WBTC(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + wbtcToken.mintFixed(deployer.address, 10000*ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) + let result = yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); result.expectOk().expectBool(true); result = yieldWBTC.mintFixed(anotherExpiry, 100 * ONE_8, deployer.address); From e021f2246e2de02ab87f556e9d168dfbba8e5c88 Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Mon, 15 Nov 2021 17:18:32 +0500 Subject: [PATCH 13/25] Changed the error code to match gitbook --- clarity/contracts/alex-vault.clar | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index 4d9784a7..a77c6cb6 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -11,7 +11,7 @@ (define-constant ERR-INVALID-POST-LOAN-BALANCE (err u3004)) (define-constant ERR-USER-EXECUTE (err u3005)) (define-constant ERR-TRANSFER-FAILED (err u3000)) -(define-constant ERR-STX-TRANSFER-FAILED (err u3001)) +(define-constant ERR-STX-TRANSFER-FAILED (err u9003)) (define-constant ERR-LOAN-TRANSFER-FAILED (err u3006)) (define-constant ERR-POST-LOAN-TRANSFER-FAILED (err u3007)) (define-constant ERR-INVALID-FLASH-LOAN (err u3008)) From 74bc3475c8be503d5499bde5315323cc9f2fb917 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 15 Nov 2021 22:11:55 +0800 Subject: [PATCH 14/25] fixed-weight-pool test passes --- .../contracts/key-token/key-usda-wbtc.clar | 2 +- .../pool-token/fwp-wbtc-usda-50-50.clar | 2 +- .../pool-token/lbp-alex-usda-90-10.clar | 2 +- .../contracts/pool-token/ytp-yield-usda.clar | 2 +- .../contracts/pool-token/ytp-yield-wbtc.clar | 2 +- clarity/contracts/token/token-alex.clar | 2 +- clarity/contracts/token/token-t-alex.clar | 2 +- clarity/contracts/token/token-usda.clar | 2 +- clarity/contracts/token/token-wbtc.clar | 2 +- clarity/contracts/token/token-wstx.clar | 2 +- clarity/contracts/yield-token/yield-usda.clar | 2 +- clarity/contracts/yield-token/yield-wbtc.clar | 2 +- clarity/tests/alex-reserve-pool_test.ts | 11 ++--- clarity/tests/fixed-weight-pool_test.ts | 47 ++++++++++++++----- clarity/tests/flash-loan_test.ts | 31 +++++------- .../alex-tests-collateral-rebalancing-pool.ts | 2 +- clarity/tests/models/alex-tests-flash-loan.ts | 2 +- clarity/tests/token-alex-src/token-client.ts | 8 ++-- 18 files changed, 69 insertions(+), 56 deletions(-) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index 77995cbd..d663a701 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar index c73ccc76..8bb1af26 100644 --- a/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/pool-token/fwp-wbtc-usda-50-50.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar index 2f84ff2f..31177772 100644 --- a/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar +++ b/clarity/contracts/pool-token/lbp-alex-usda-90-10.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index 6ee9401e..21c0f6d7 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar index 60ac5621..7356e5b7 100644 --- a/clarity/contracts/pool-token/ytp-yield-wbtc.clar +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/token/token-alex.clar b/clarity/contracts/token/token-alex.clar index d2bb9f3b..1f6c2576 100644 --- a/clarity/contracts/token/token-alex.clar +++ b/clarity/contracts/token/token-alex.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-t-alex.clar b/clarity/contracts/token/token-t-alex.clar index 20ae6fc9..bceda2ab 100644 --- a/clarity/contracts/token/token-t-alex.clar +++ b/clarity/contracts/token/token-t-alex.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-usda.clar b/clarity/contracts/token/token-usda.clar index 3405bc79..ae6eafcf 100644 --- a/clarity/contracts/token/token-usda.clar +++ b/clarity/contracts/token/token-usda.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-wbtc.clar b/clarity/contracts/token/token-wbtc.clar index 99041bff..c8a9afff 100644 --- a/clarity/contracts/token/token-wbtc.clar +++ b/clarity/contracts/token/token-wbtc.clar @@ -42,7 +42,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/token/token-wstx.clar b/clarity/contracts/token/token-wstx.clar index 59972ad2..b8026d52 100644 --- a/clarity/contracts/token/token-wstx.clar +++ b/clarity/contracts/token/token-wstx.clar @@ -38,7 +38,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-balance (account principal)) diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index ba35b7bf..474ae8be 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar index 8ad44caa..6d47d925 100644 --- a/clarity/contracts/yield-token/yield-wbtc.clar +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -61,7 +61,7 @@ ) (define-read-only (get-decimals) - (ok u6) + (ok u8) ) (define-read-only (get-token-uri (token-id uint)) diff --git a/clarity/tests/alex-reserve-pool_test.ts b/clarity/tests/alex-reserve-pool_test.ts index 758e47c5..7a2399de 100644 --- a/clarity/tests/alex-reserve-pool_test.ts +++ b/clarity/tests/alex-reserve-pool_test.ts @@ -293,7 +293,6 @@ describe("[ALEX STAKING]", () => { const amountTokens = 20 * ONE_8; const lockPeriod = 5; const block = chain.mineBlock([ - clients.core.setActivationThreshold(deployer, 1), clients.core.registerUser(staker), clients.token.mint(amountTokens, staker, deployer), @@ -309,10 +308,10 @@ describe("[ALEX STAKING]", () => { // assert receipt.result.expectOk().expectBool(true); - + console.log(amountTokens); assertEquals(receipt.events.length, 2); receipt.events.expectFungibleTokenTransferEvent( - amountTokens / ONE_8, + amountTokens, staker.address, clients.core.getVaultAddress(), "alex" @@ -353,7 +352,7 @@ describe("[ALEX STAKING]", () => { assertEquals(receipt.events.length, 2); receipt.events.expectFungibleTokenTransferEvent( - amountTokens / ONE_8, + amountTokens, staker.address, clients.core.getVaultAddress(), "alex" @@ -688,7 +687,7 @@ describe("[ALEX STAKING]", () => { assertEquals(receipt.events.length, 3); receipt.events.expectFungibleTokenTransferEvent( - amountTokens / ONE_8, + amountTokens, clients.core.getVaultAddress(), staker.address, "alex" @@ -786,7 +785,7 @@ describe("[ALEX STAKING]", () => { assertEquals(receipt.events.length, 3); receipt.events.expectFungibleTokenTransferEvent( - toReturn / ONE_8, + toReturn, clients.core.getVaultAddress(), staker.address, "alex" diff --git a/clarity/tests/fixed-weight-pool_test.ts b/clarity/tests/fixed-weight-pool_test.ts index 29a9a308..302bc602 100644 --- a/clarity/tests/fixed-weight-pool_test.ts +++ b/clarity/tests/fixed-weight-pool_test.ts @@ -16,7 +16,7 @@ const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" const fwpwbtcusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fwp-wbtc-usda-50-50" const multisigAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-fwp-wbtc-usda-50-50" const fwpAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fixed-weight-pool" -const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda-23040-usda" +const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.lbp-alex-usda-90-10" const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" const ONE_8 = 100000000 @@ -41,12 +41,10 @@ Clarinet.test({ let wbtcToken = new WBTCToken(chain, deployer); // Deployer minting initial tokens - usdaToken.mintFixed(deployer.address, 1000000000); - usdaToken.mintFixed(alexReservePoolAddress, 10000000); - usdaToken.mintFixed(wallet_1.address, 200000); - - wbtcToken.mintFixed(deployer.address, 10000*ONE_8) - wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + usdaToken.mintFixed(wallet_1.address, 200000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 100000 * ONE_8); + wbtcToken.mintFixed(wallet_1.address, 100000 * ONE_8); // Deployer creating a pool, initial tokens injected to the pool let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigAddress, wbtcQ, wbtcQ*wbtcPrice); @@ -117,6 +115,14 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let wallet_1 = accounts.get("wallet_1")!; let FWPTest = new FWPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + usdaToken.mintFixed(wallet_1.address, 200000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 100000 * ONE_8); + wbtcToken.mintFixed(wallet_1.address, 100000 * ONE_8); // non-deployer attempting to create a pool will throw an error let result = FWPTest.createPool(wallet_1, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigAddress, wbtcQ, wbtcQ*wbtcPrice); @@ -152,16 +158,20 @@ Clarinet.test({ Clarinet.test({ - name: "FWP : fee Setting using multisig ", + name: "FWP : fee setting using multisig ", async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; - let contractOwner = deployer + let contractOwner = deployer; + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 100000 * ONE_8); let FWPTest = new FWPTestAgent1(chain, deployer); let MultiSigTest = new MS_FWP_WBTC_USDA_5050(chain, deployer); - let usdaToken = new USDAToken(chain, deployer); - let wbtcToken = new WBTCToken(chain, deployer); let fwpPoolToken = new FWP_WBTC_USDA_5050(chain, deployer); const feeRateX = 0.1*ONE_8; // 10% @@ -261,11 +271,16 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; - let wallet_1 = accounts.get("wallet_1")!; let FWPTest = new FWPTestAgent1(chain, deployer); let MultiSigTest = new MS_FWP_WBTC_USDA_5050(chain, deployer); const feeRateX = 5000000; // 5% const feeRateY = 5000000; + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 100000 * ONE_8); // Deployer creating a pool, initial tokens injected to the pool let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigAddress, wbtcQ, wbtcQ*wbtcPrice); @@ -352,7 +367,13 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; - let FWPTest = new FWPTestAgent1(chain, deployer); + let FWPTest = new FWPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 100000 * ONE_8); // Deployer creating a pool, initial tokens injected to the pool let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigAddress, wbtcQ, wbtcQ*wbtcPrice); diff --git a/clarity/tests/flash-loan_test.ts b/clarity/tests/flash-loan_test.ts index 910ba0b2..2ff0d5de 100644 --- a/clarity/tests/flash-loan_test.ts +++ b/clarity/tests/flash-loan_test.ts @@ -14,22 +14,17 @@ const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" const fwpwbtcusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fwp-wbtc-usda-50-50" const multisigfwpAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-fwp-wbtc-usda-50-50" -const yieldusda23040Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda-23040" -const keyusda23040Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-usda-23040-wbtc" -const ytpyieldusda23040Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda-23040-usda" -const multisigncrpusda23040Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-usda-23040-wbtc" -const multisigytpyieldusda23040 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-usda-23040-usda" -const loanuser23040Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.flash-loan-user-margin-wbtc-usda-23040" -const yieldusda51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda-51840" -const keyusda51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-usda-51840-wbtc" -const ytpyieldusda51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda-51840-usda" -const multisigncrpusda51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-usda-51840-wbtc" -const multisigytpyieldusda51840 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-usda-51840-usda" -const loanuser51840Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.flash-loan-user-margin-wbtc-usda-51840" +const yieldusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda" +const keyusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-usda-wbtc" +const ytpyieldusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda" +const multisigncrpusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-usda-wbtc" +const multisigytpyieldusda = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-usda" +const loanuserAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.flash-loan-user-margin-wbtc-usda" const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" const ONE_8 = 100000000 const expiry = 23040e+8 +const nextExpiry = 51840e+8 const ltv_0 = 0.8e+8 const conversion_ltv = 0.95e+8 const bs_vol = 0.8e+8 @@ -61,14 +56,12 @@ Clarinet.test({ let wbtcToken = new WBTCToken(chain, deployer); // Deployer minting initial tokens - usdaToken.mintFixed(deployer.address, 1000000000); - usdaToken.mintFixed(alexReservePoolAddress, 10000000); - usdaToken.mintFixed(wallet_1.address, 200000); + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + usdaToken.mintFixed(wallet_1.address, 200000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); + wbtcToken.mintFixed(wallet_1.address, 10000 * ONE_8); - wbtcToken.mintFixed(deployer.address, 10000*ONE_8) - wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) - - wbtcToken.transferToken(ONE_8,deployer.address, wallet_5.address, new ArrayBuffer(30)); + wbtcToken.transferToken(ONE_8, deployer.address, wallet_5.address, new ArrayBuffer(30)); let call = await FLTest.getBalance(wbtcAddress, wallet_5.address); let position:any = call.result.expectOk().expectUint(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 2e9a993e..4e0f294c 100644 --- a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts +++ b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts @@ -278,7 +278,7 @@ import { } transfer(user: Account, token: string, amount: number, sender: string, recipient: string, memo: ArrayBuffer) { - let block = this.chain.mineBlock([Tx.contractCall(token, "transfer", [ + let block = this.chain.mineBlock([Tx.contractCall(token, "transfer-fixed", [ types.uint(amount), types.principal(sender), types.principal(recipient), diff --git a/clarity/tests/models/alex-tests-flash-loan.ts b/clarity/tests/models/alex-tests-flash-loan.ts index f8dd6930..ec574bb4 100644 --- a/clarity/tests/models/alex-tests-flash-loan.ts +++ b/clarity/tests/models/alex-tests-flash-loan.ts @@ -40,7 +40,7 @@ import { } getBalance(token: string, owner: string) { - return this.chain.callReadOnlyFn(token, "get-balance", [ + return this.chain.callReadOnlyFn(token, "get-balance-fixed", [ types.principal(owner) ], this.deployer.address); } diff --git a/clarity/tests/token-alex-src/token-client.ts b/clarity/tests/token-alex-src/token-client.ts index 18b4fbcd..a83f80b3 100644 --- a/clarity/tests/token-alex-src/token-client.ts +++ b/clarity/tests/token-alex-src/token-client.ts @@ -24,7 +24,7 @@ export class TokenClient extends Client { return Tx.contractCall( this.contractName, - "transfer", + "transfer-fixed", [ types.uint(amount), types.principal(from.address), @@ -38,7 +38,7 @@ export class TokenClient extends Client { burn(amount: number, sender: Account): Tx { return Tx.contractCall( this.contractName, - "burn", + "burn-fixed", [types.uint(amount), types.principal(sender.address)], sender.address ); @@ -92,8 +92,8 @@ export class TokenClient extends Client { mint(amount: number, recipient: Account, sender: Account): Tx { return Tx.contractCall( this.contractName, - "mint", - [types.principal(recipient.address), types.uint(amount)], + "mint-fixed", + [types.uint(amount), types.principal(recipient.address)], sender.address ); } From 9b6d487f736d11db0fa98e67ae5945464eb28176 Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Tue, 16 Nov 2021 12:46:48 +0500 Subject: [PATCH 15/25] Fixed the error calls and refactored the names --- clarity/contracts/alex-vault.clar | 1 - clarity/contracts/equations/yield-token-equation.clar | 10 +++++----- .../contracts/flash-loan-user-margin-wbtc-usda.clar | 1 - clarity/contracts/pool/alex-reserve-pool.clar | 2 +- .../contracts/pool/collateral-rebalancing-pool.clar | 8 ++++---- clarity/contracts/pool/fixed-weight-pool.clar | 2 +- clarity/contracts/pool/yield-token-pool.clar | 5 ++--- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index a77c6cb6..fcb380c0 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -15,7 +15,6 @@ (define-constant ERR-LOAN-TRANSFER-FAILED (err u3006)) (define-constant ERR-POST-LOAN-TRANSFER-FAILED (err u3007)) (define-constant ERR-INVALID-FLASH-LOAN (err u3008)) -(define-constant ERR-INVALID-BALANCE (err u3011)) (define-constant ERR-MATH-CALL (err u2010)) (define-constant ERR-INTERNAL-FUNCTION-CALL (err u1001)) diff --git a/clarity/contracts/equations/yield-token-equation.clar b/clarity/contracts/equations/yield-token-equation.clar index 1f492194..08ab67f5 100644 --- a/clarity/contracts/equations/yield-token-equation.clar +++ b/clarity/contracts/equations/yield-token-equation.clar @@ -12,8 +12,8 @@ (define-constant ERR-WEIGHT-SUM (err u4000)) (define-constant ERR-MAX-IN-RATIO (err u4001)) (define-constant ERR-MAX-OUT-RATIO (err u4002)) -(define-constant ERR-INSUFFICIENT-BAL (err u4004)) -(define-constant ERR-INVALID-BAL (err u2008)) +(define-constant ERR-INSUFFICIENT-BALANCE (err u4004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) (define-data-var CONTRACT-OWNER principal tx-sender) @@ -82,7 +82,7 @@ ;; @returns (response uint uint) (define-read-only (get-price (balance-x uint) (balance-y uint) (t uint)) (begin - (asserts! (>= balance-y balance-x) ERR-INVALID-BAL) + (asserts! (>= balance-y balance-x) ERR-INVALID-BALANCE) (ok (pow-up (div-down balance-y balance-x) t)) ) ) @@ -114,7 +114,7 @@ ;; @returns (response uint uint) (define-read-only (get-y-given-x (balance-x uint) (balance-y uint) (t uint) (dx uint)) (begin - (asserts! (>= balance-x dx) ERR-INSUFFICIENT-BAL) + (asserts! (>= balance-x dx) ERR-INSUFFICIENT-BALANCE) (asserts! (< dx (mul-down balance-x (var-get MAX-IN-RATIO))) ERR-MAX-IN-RATIO) (let ( @@ -149,7 +149,7 @@ ;; @returns (response uint uint) (define-read-only (get-x-given-y (balance-x uint) (balance-y uint) (t uint) (dy uint)) (begin - (asserts! (>= balance-y dy) ERR-INSUFFICIENT-BAL) + (asserts! (>= balance-y dy) ERR-INSUFFICIENT-BALANCE) (asserts! (< dy (mul-down balance-y (var-get MAX-OUT-RATIO))) ERR-MAX-OUT-RATIO) (let ( diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar index a23b55c8..cea54ea3 100644 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar +++ b/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar @@ -1,7 +1,6 @@ (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)) diff --git a/clarity/contracts/pool/alex-reserve-pool.clar b/clarity/contracts/pool/alex-reserve-pool.clar index a8795744..33a03853 100644 --- a/clarity/contracts/pool/alex-reserve-pool.clar +++ b/clarity/contracts/pool/alex-reserve-pool.clar @@ -11,7 +11,7 @@ (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-X (err u2005)) (define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) (define-constant ERR-MATH-CALL (err u2010)) diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index ddf74b06..3080d2aa 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -18,7 +18,7 @@ (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-X (err u2005)) (define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) (define-constant ERR-INTERNAL-FUNCTION-CALL (err u1001)) @@ -28,7 +28,7 @@ (define-constant ERR-GET-SYMBOL-FAIL (err u6000)) (define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) (define-constant ERR-EXPIRY (err u2017)) -(define-constant ERR-get-balance-fixed-FAIL (err u6001)) +(define-constant ERR-GET-BALANCE-FIXED-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)) @@ -452,7 +452,7 @@ (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (yield-supply (get yield-supply pool)) - (total-shares (unwrap! (contract-call? the-yield-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) + (total-shares (unwrap! (contract-call? the-yield-token get-balance-fixed expiry tx-sender) ERR-GET-BALANCE-FIXED-FAIL)) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (shares-to-yield (div-down shares yield-supply)) @@ -525,7 +525,7 @@ (balance-x (get balance-x pool)) (balance-y (get balance-y pool)) (key-supply (get key-supply pool)) - (total-shares (unwrap! (contract-call? the-key-token get-balance-fixed expiry tx-sender) ERR-get-balance-fixed-FAIL)) + (total-shares (unwrap! (contract-call? the-key-token get-balance-fixed expiry tx-sender) ERR-GET-BALANCE-FIXED-FAIL)) (shares (if (is-eq percent ONE_8) total-shares (mul-down total-shares percent))) (reduce-data (try! (get-position-given-burn-key token collateral expiry shares))) (dx-weighted (get dx reduce-data)) diff --git a/clarity/contracts/pool/fixed-weight-pool.clar b/clarity/contracts/pool/fixed-weight-pool.clar index 6e14e5c2..3d7b1998 100644 --- a/clarity/contracts/pool/fixed-weight-pool.clar +++ b/clarity/contracts/pool/fixed-weight-pool.clar @@ -19,7 +19,7 @@ (define-constant ERR-PERCENT_GREATER_THAN_ONE (err u5000)) (define-constant ERR-INVALID-BALANCE (err u2008)) (define-constant ERR-INVALID-TOKEN (err u2007)) -(define-constant ERR-NO-FEE (err u2005)) +(define-constant ERR-NO-FEE-X (err u2005)) (define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) (define-constant ERR-MATH-CALL (err u2010)) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 1b199c51..7067b6df 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -15,12 +15,11 @@ (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 invalid-token-err (err u2007)) -(define-constant ERR-NO-FEE (err u2005)) +(define-constant ERR-NO-FEE-X (err u2005)) (define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-INVALID-EXPIRY (err u2009)) (define-constant fixed-point-err (err 5014)) -(define-constant ERR-MATH-CALL (err u4003)) +(define-constant ERR-MATH-CALL (err 2010)) (define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) (define-constant ERR-DY-BIGGER-THAN-AVAILABLE (err u2016)) (define-constant ERR-NOT-AUTHORIZED (err u1000)) From 418056ce9fac5c0352687cf7049fd1c27bdef456 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Tue, 16 Nov 2021 22:39:12 +0800 Subject: [PATCH 16/25] clarinet test passes --- clarity/Clarinet.toml | 22 +- clarity/contracts/margin-helper.clar | 4 +- clarity/contracts/token/token-alex.clar | 5 - .../tests/collateral-rebalancing-pool_test.ts | 228 +++++++++--------- clarity/tests/flash-loan_test.ts | 38 +-- .../liquidity-bootstrapping-pool_test.ts | 18 +- .../alex-tests-collateral-rebalancing-pool.ts | 32 ++- clarity/tests/models/alex-tests-flash-loan.ts | 18 +- clarity/tests/models/alex-tests-multisigs.ts | 58 ++++- clarity/tests/models/alex-tests-tokens.ts | 78 ++++++ clarity/tests/yield-token-pool_test.ts | 2 +- 11 files changed, 342 insertions(+), 161 deletions(-) diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index f2db4adf..ac119aa1 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -130,6 +130,14 @@ depends_on = [] path = "contracts/multisig/multisig-crp-usda-wbtc.clar" depends_on = ["yield-usda", "key-usda-wbtc"] +[contracts.multisig-crp-wbtc-wbtc] +path = "contracts/multisig/multisig-crp-wbtc-wbtc.clar" +depends_on = ["yield-wbtc", "key-wbtc-wbtc"] + +[contracts.multisig-crp-wbtc-usda] +path = "contracts/multisig/multisig-crp-wbtc-usda.clar" +depends_on = ["yield-wbtc", "key-wbtc-usda"] + [contracts.multisig-ytp-yield-usda] path = "contracts/multisig/multisig-ytp-yield-usda.clar" depends_on = ["ytp-yield-usda", "yield-usda"] @@ -138,6 +146,14 @@ depends_on = ["ytp-yield-usda", "yield-usda"] path = "contracts/key-token/key-usda-wbtc.clar" depends_on = ["trait-ownable", "trait-semi-fungible-token"] +[contracts.key-wbtc-usda] +path = "contracts/key-token/key-wbtc-usda.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] + +[contracts.key-wbtc-wbtc] +path = "contracts/key-token/key-wbtc-wbtc.clar" +depends_on = ["trait-ownable", "trait-semi-fungible-token"] + [contracts.yield-usda] path = "contracts/yield-token/yield-usda.clar" depends_on = ["trait-ownable", "trait-semi-fungible-token"] @@ -152,4 +168,8 @@ depends_on = ["ytp-yield-wbtc", "yield-wbtc"] [contracts.ytp-yield-wbtc] path = "contracts/pool-token/ytp-yield-wbtc.clar" -depends_on = ["trait-ownable", "trait-semi-fungible-token"] \ No newline at end of file +depends_on = ["trait-ownable", "trait-semi-fungible-token"] + +[contracts.flash-loan-user-margin-usda-wbtc] +path = "contracts/flash-loan-user-margin-usda-wbtc.clar" +depends_on = ["trait-sip-010", "trait-flash-loan-user", "yield-usda", "key-usda-wbtc"] \ No newline at end of file diff --git a/clarity/contracts/margin-helper.clar b/clarity/contracts/margin-helper.clar index c95f99ad..d09c3f43 100644 --- a/clarity/contracts/margin-helper.clar +++ b/clarity/contracts/margin-helper.clar @@ -4,7 +4,7 @@ (define-constant ONE_8 (pow u10 u8)) -(define-public (roll-position (token ) (collateral ) (expiry uint) (the-key-token ) (flash-loan-user ) (expiry-to-roll uint)) +(define-public (roll-position (token ) (collateral ) (the-key-token ) (flash-loan-user ) (expiry uint) (expiry-to-roll uint)) (let ( (reduce-data (try! (contract-call? .collateral-rebalancing-pool reduce-position-key token collateral expiry the-key-token ONE_8))) @@ -17,6 +17,6 @@ ) ) ) - (contract-call? .alex-vault flash-loan flash-loan-user collateral (+ collateral-amount token-to-collateral) (some expiry)) + (contract-call? .alex-vault flash-loan flash-loan-user collateral (+ collateral-amount token-to-collateral) (some expiry-to-roll)) ) ) \ No newline at end of file diff --git a/clarity/contracts/token/token-alex.clar b/clarity/contracts/token/token-alex.clar index 1f6c2576..09ab397e 100644 --- a/clarity/contracts/token/token-alex.clar +++ b/clarity/contracts/token/token-alex.clar @@ -126,9 +126,4 @@ (map-set approved-contracts .faucet true) ) -;; Initialize the contract for Testing. -(begin - (try! (ft-mint? alex u1000000000 tx-sender)) -) - diff --git a/clarity/tests/collateral-rebalancing-pool_test.ts b/clarity/tests/collateral-rebalancing-pool_test.ts index 7ab347c7..26dc1adc 100644 --- a/clarity/tests/collateral-rebalancing-pool_test.ts +++ b/clarity/tests/collateral-rebalancing-pool_test.ts @@ -6,34 +6,24 @@ import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts'; import { CRPTestAgent1 } from './models/alex-tests-collateral-rebalancing-pool.ts'; import { FWPTestAgent1 } from './models/alex-tests-fixed-weight-pool.ts'; import { YTPTestAgent1 } from './models/alex-tests-yield-token-pool.ts'; -import { MS_CRP_USDA_WBTC} from './models/alex-tests-multisigs.ts'; -import { - USDAToken, - WBTCToken, - YIELD_WBTC, - KEY_USDA_WBTC } from './models/alex-tests-tokens.ts'; +import { MS_CRP_USDA_WBTC, MS_CRP_WBTC_USDA } from './models/alex-tests-multisigs.ts'; +import { USDAToken, WBTCToken, YIELD_WBTC, KEY_WBTC_USDA } from './models/alex-tests-tokens.ts'; // Deployer Address Constants const wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-wbtc" const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" const fwpwbtcusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.fwp-wbtc-usda-50-50" const multisigfwpAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-fwp-wbtc-usda-50-50" -const yieldwbtc59760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc-59760" -const keywbtc59760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-wbtc-59760-usda" -const ytpyieldwbtc59760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc-59760-wbtc" -const multisigncrpwbtc59760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-wbtc-59760-usda" -const multisigytpyieldwbtc59760 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc-59760-wbtc" -const yieldwbtc79760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc-79760" -const keywbtc79760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-wbtc-79760-usda" -const ytpyieldwbtc79760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc-79760-wbtc" -const multisigncrpwbtc79760Address = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-wbtc-79760-usda" -const multisigytpyieldwbtc79760 = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc-79760-wbtc" +const yieldwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-wbtc" +const keywbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-wbtc-usda" +const ytpyieldwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-wbtc" +const multisigncrpwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-wbtc-usda" +const multisigytpyieldwbtc = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-wbtc" const vaultAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-vault" const reserveAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" -const keywbtc59760wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-wbtc-59760-wbtc" -const multisigncrpwbtc59760wbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-wbtc-59760-wbtc" -const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda-59760" -const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" +const keywbtcwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-wbtc-wbtc" +const multisigncrpwbtcwbtcAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-wbtc-wbtc" +const wrongPooltokenAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.yield-usda" const ONE_8 = 100000000 const expiry = 59760 * ONE_8 @@ -69,12 +59,10 @@ Clarinet.test({ let wbtcToken = new WBTCToken(chain, deployer); // Deployer minting initial tokens - usdaToken.mintFixed(deployer.address, 1000000000); - usdaToken.mintFixed(alexReservePoolAddress, 10000000); - usdaToken.mintFixed(wallet_1.address, 200000); - - wbtcToken.mintFixed(deployer.address, 10000*ONE_8) - wbtcToken.mintFixed(wallet_1.address, 10000*ONE_8) + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + usdaToken.mintFixed(wallet_1.address, 200000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000 * ONE_8) let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -89,11 +77,11 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); @@ -132,7 +120,7 @@ Clarinet.test({ // 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); + result = CRPTest.addToPositionAndSwitch(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, 5000 * ONE_8); position = result.expectOk().expectTuple(); position['dy'].expectUint(8046279); position['dx'].expectUint(8046447); @@ -160,7 +148,7 @@ Clarinet.test({ chain.mineEmptyBlockUntil((expiry / ONE_8)) // but lender cannot yet redeem - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, 0.5 * ONE_8); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, 0.5 * ONE_8); result.expectErr().expectUint(2017); // simulate to expiry + 1 @@ -169,19 +157,8 @@ Clarinet.test({ call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); call.result.expectOk().expectUint(109786643); - // take away what was minted for testing to another address - let block = chain.mineBlock([ - Tx.contractCall("yield-wbtc-59760", "transfer-fixed", [ - types.uint(2000000000000), - types.principal(deployer.address), - types.principal(wallet_1.address), - types.some(types.buff(new ArrayBuffer(10))) - ], deployer.address), - ]); - block.receipts[0].result.expectOk(); - // deployer holds less than total supply because he sold some yield-wbtc for wbtc - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, ONE_8); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(0); position['dy'].expectUint(80807360); @@ -195,7 +172,7 @@ Clarinet.test({ position['key-supply'].expectUint(88853639); // also remove all key tokens - result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, keywbtc59760Address, ONE_8); + result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, expiry, keywbtcAddress, ONE_8); position = result.expectOk().expectTuple(); position['dx'].expectUint(0); position['dy'].expectUint(1895950); @@ -218,6 +195,14 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + usdaToken.mintFixed(wallet_1.address, 200000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8) + wbtcToken.mintFixed(wallet_1.address, 10000 * ONE_8) let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -227,50 +212,39 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); // non-deployer creating a pool will throw an error - result = CRPTest.createPool(wallet_1, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(wallet_1, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectErr().expectUint(1000); //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); // supplying a wrong pool-token throws an error - result = CRPTest.addToPositionAndSwitch(wallet_1, wbtcAddress, usdaAddress, wrongPooltokenAddress, keywbtc59760Address, 5000 * ONE_8); + result = CRPTest.addToPositionAndSwitch(wallet_1, wbtcAddress, usdaAddress, expiry, wrongPooltokenAddress, keywbtcAddress, 5000 * ONE_8); result.expectErr().expectUint(2023); // same for key-token - result = CRPTest.addToPositionAndSwitch(wallet_1, wbtcAddress, usdaAddress, yieldwbtc59760Address, wrongPooltokenAddress, 5000 * ONE_8); + result = CRPTest.addToPositionAndSwitch(wallet_1, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, wrongPooltokenAddress, 5000 * ONE_8); result.expectErr().expectUint(2023); // simulate to expiry + 1 chain.mineEmptyBlockUntil((expiry / ONE_8) + 1) // supplying a wrong pool-token throws an error - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, wrongPooltokenAddress, ONE_8); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, wrongPooltokenAddress, ONE_8); result.expectErr().expectUint(2023); // same for key-token - result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, wrongPooltokenAddress, ONE_8); + result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, expiry, wrongPooltokenAddress, ONE_8); result.expectErr().expectUint(2023); - - // take away what was minted for testing to another address - let block = chain.mineBlock([ - Tx.contractCall("yield-wbtc-59760", "transfer-fixed", [ - types.uint(2000000000000), - types.principal(deployer.address), - types.principal(wallet_1.address), - types.some(types.buff(new ArrayBuffer(10))) - ], deployer.address), - ]); - block.receipts[0].result.expectOk(); // remove all liquidity - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, ONE_8); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, ONE_8); result.expectOk(); - result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, keywbtc59760Address, ONE_8); + result = CRPTest.reducePositionKey(deployer, wbtcAddress, usdaAddress, expiry, keywbtcAddress, ONE_8); result.expectOk(); } }); @@ -283,6 +257,12 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -296,20 +276,20 @@ Clarinet.test({ position['balance-x'].expectUint(wbtcQ); position['balance-y'].expectUint(Math.round(wbtcQ * wbtcPrice / ONE_8)); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); // simulate to half way to expiry chain.mineEmptyBlockUntil((expiry / ONE_8) / 2) - result = YTPTest.createPool(deployer, expiry, yieldwbtc79760Address, wbtcAddress, ytpyieldwbtc79760Address, multisigytpyieldwbtc79760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry79760, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc79760Address, keywbtc79760Address, multisigncrpwbtc79760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry79760, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); @@ -324,6 +304,12 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -337,12 +323,12 @@ Clarinet.test({ position['balance-x'].expectUint(wbtcQ); position['balance-y'].expectUint(Math.round(wbtcQ * wbtcPrice / ONE_8)); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); let moving_average_0 = 0.95e+8 //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average_0, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average_0, 50000 * ONE_8); result.expectOk().expectBool(true); call = await CRPTest.getPoolValueInToken(wbtcAddress, usdaAddress, expiry); @@ -391,6 +377,14 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + let yieldWBTC = new YIELD_WBTC(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); + yieldWBTC.mintFixed(expiry, 100 * ONE_8, deployer.address); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -399,23 +393,24 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ, wbtcQ); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ, wbtcQ); result.expectOk().expectBool(true); // sell some yield-token to create a positive yield - result = YTPTest.swapYForX(deployer, expiry, yieldwbtc59760Address, wbtcAddress, 5*ONE_8, 0); + result = YTPTest.swapYForX(deployer, expiry, yieldwbtcAddress, wbtcAddress, 5*ONE_8, 0); let position:any = result.expectOk().expectTuple(); - let call = await YTPTest.getPrice(expiry, yieldwbtc59760Address); - call.result.expectOk().expectUint(100071157); + let call = await YTPTest.getPrice(expiry, yieldwbtcAddress); + call.result.expectOk().expectUint(100071156); let ltv_00 = Math.round(ONE_8 * ONE_8 / 109095981); let conversion_ltv_0 = 0.98e+8; let bs_vol_0 = 0.1e+8; let collateral = ONE_8; let moving_average_0 = 0.95e+8 + //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, wbtcAddress, yieldwbtc59760Address, keywbtc59760wbtcAddress, multisigncrpwbtc59760wbtcAddress, ltv_00, conversion_ltv_0, bs_vol_0, moving_average_0, collateral); + result = CRPTest.createPool(deployer, wbtcAddress, wbtcAddress, expiry, yieldwbtcAddress, keywbtcwbtcAddress, multisigncrpwbtcwbtcAddress, ltv_00, conversion_ltv_0, bs_vol_0, moving_average_0, collateral); result.expectOk().expectBool(true); call = await CRPTest.getPoolValueInToken(wbtcAddress, wbtcAddress, expiry); @@ -430,10 +425,10 @@ Clarinet.test({ position = call.result.expectOk().expectTuple(); position['yield-supply'].expectUint(91662405); //about 1 / 1.09 position['key-supply'].expectUint(91662405); - position['weight-x'].expectUint(52269439); - position['weight-y'].expectUint(ONE_8 - 52269439); - position['balance-x'].expectUint(52269439); - position['balance-y'].expectUint(ONE_8 - 52269439); + position['weight-x'].expectUint(52269424); + position['weight-y'].expectUint(ONE_8 - 52269424); + position['balance-x'].expectUint(52269424); + position['balance-y'].expectUint(ONE_8 - 52269424); position['strike'].expectUint(ONE_8); position['ltv-0'].expectUint(ltv_00); position['bs-vol'].expectUint(bs_vol_0); @@ -442,7 +437,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(9516811587); + call.result.expectOk().expectUint(9516824576); result = CRPTest.swapXForY(deployer, wbtcAddress, wbtcAddress, expiry, 9516811587, 0); position = result.expectErr().expectUint(2001); }, @@ -457,6 +452,12 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -465,17 +466,17 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); - result = CRPTest.addToPositionAndSwitch(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, 0); + result = CRPTest.addToPositionAndSwitch(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, 0); result.expectErr().expectUint(2003) - result = CRPTest.addToPositionAndSwitch(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, ONE_8 * ONE_8); + result = CRPTest.addToPositionAndSwitch(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, ONE_8 * ONE_8); result.expectErr().expectUint(4002) // arbtrageur attepmts to swap zero value @@ -490,15 +491,15 @@ Clarinet.test({ chain.mineEmptyBlockUntil((expiry / ONE_8) + 1) // arbtrageur attepmts to retreive back with zero value - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, 0); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, 0); result.expectErr().expectUint(3000) // arbitrageur attempts to retreuve back with small value - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, 0.001 * ONE_8); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, 0.001 * ONE_8); result.expectOk().expectTuple(); // arbtrageur attepmts to retreive back with full value - result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, 101*ONE_8); + result = CRPTest.reducePositionYield(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, 101*ONE_8); result.expectErr().expectUint(5000) }, }); @@ -509,6 +510,12 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -517,7 +524,7 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); result = await CRPTest.getYgivenX(deployer, wbtcAddress, usdaAddress, expiry, ONE_8); @@ -551,12 +558,22 @@ Clarinet.test({ let CRPTest = new CRPTestAgent1(chain, deployer); let FWPTest = new FWPTestAgent1(chain, deployer); let YTPTest = new YTPTestAgent1(chain, deployer); - let MultiSigTest = new MS_CRP_USDA_WBTC(chain, deployer); + let MultiSigTest = new MS_CRP_WBTC_USDA(chain, deployer); let YieldToken = new YIELD_WBTC(chain, deployer); - let KeyToken = new KEY_USDA_WBTC(chain, deployer); + let KeyToken = new KEY_WBTC_USDA(chain, deployer); const feeRateX = 0.1*ONE_8; // 10% const feeRateY = 0.1*ONE_8; const feeRebate = 0.5*ONE_8; + + let usdaToken = new USDAToken(chain, deployer); + let wbtcToken = new WBTCToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + usdaToken.mintFixed(wallet_1.address, 200000 * ONE_8); + wbtcToken.mintFixed(deployer.address, 10000 * ONE_8); + wbtcToken.mintFixed(wallet_1.address, 10000 * ONE_8); + YieldToken.mintFixed(expiry, 20000 * ONE_8, wallet_1.address); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, wbtcQ, Math.round(wbtcPrice * wbtcQ / ONE_8)); result.expectOk().expectBool(true); @@ -570,27 +587,16 @@ Clarinet.test({ position['balance-x'].expectUint(wbtcQ); position['balance-y'].expectUint(Math.round(wbtcQ * wbtcPrice / ONE_8)); - result = YTPTest.createPool(deployer, expiry, yieldwbtc59760Address, wbtcAddress, ytpyieldwbtc59760Address, multisigytpyieldwbtc59760, wbtcQ / 10, wbtcQ / 10); + result = YTPTest.createPool(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, multisigytpyieldwbtc, wbtcQ / 10, wbtcQ / 10); result.expectOk().expectBool(true); //Deployer creating a pool, initial tokens injected to the pool - result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, yieldwbtc59760Address, keywbtc59760Address, multisigncrpwbtc59760Address, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); + result = CRPTest.createPool(deployer, wbtcAddress, usdaAddress, expiry, yieldwbtcAddress, keywbtcAddress, multisigncrpwbtcAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 50000 * ONE_8); result.expectOk().expectBool(true); let ROresult:any = YieldToken.totalSupply(expiry) ROresult.result.expectOk().expectUint(2000080807360); - // take away what was minted for testing to another address - let block = chain.mineBlock([ - Tx.contractCall("yield-wbtc-59760", "transfer-fixed", [ - types.uint(2000000000000), - types.principal(deployer.address), - types.principal(wallet_1.address), - types.some(types.buff(new ArrayBuffer(10))) - ], deployer.address), - ]); - block.receipts[0].result.expectOk(); - ROresult = YieldToken.balanceOf(expiry, deployer.address) ROresult.result.expectOk().expectUint(80807360); @@ -607,10 +613,10 @@ Clarinet.test({ chain.mineEmptyBlock(1000); // deployer and wallet_1 votes for 90 % of his token - result = MultiSigTest.voteFor(wallet_1, yieldwbtc59760Address, 1, 2000000000000 * 9 / 10 ) + result = MultiSigTest.voteFor(wallet_1, yieldwbtcAddress, 1, 2000000000000 * 9 / 10 ) result.expectOk().expectUint(1800000000000) - result = MultiSigTest.voteFor(deployer, yieldwbtc59760Address, 1, 80807360 * 9 / 10 ) + result = MultiSigTest.voteFor(deployer, yieldwbtcAddress, 1, 80807360 * 9 / 10 ) result.expectOk().expectUint(72726624) // Block 1440 mining for ending proposal @@ -625,8 +631,8 @@ Clarinet.test({ call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); - position['balance-x'].expectUint(3326726300000); - position['balance-y'].expectUint(33576900); + position['balance-x'].expectUint(3326719750000); + position['balance-y'].expectUint(33577100); position['yield-supply'].expectUint(80807360); position['key-supply'].expectUint(80807360); position['fee-rate-x'].expectUint(0.1*ONE_8); @@ -643,17 +649,17 @@ Clarinet.test({ // fee-rebate : 0.5 * ONE_8 call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); - position['balance-x'].expectUint(3336226300000); // 3326726300000 + 0.95 * 100* ONE_8 - position['balance-y'].expectUint(33397031); + position['balance-x'].expectUint(3336219750000); // 3326726300000 + 0.95 * 100* ONE_8 + position['balance-y'].expectUint(33397231); result = CRPTest.swapYForX(deployer, wbtcAddress, usdaAddress, expiry, 0.001 * ONE_8, 0); position = result.expectOk().expectTuple(); - position['dx'].expectUint(9866889282); + position['dx'].expectUint(9866803186); position['dy'].expectUint(0.0009 * ONE_8); call = await CRPTest.getPoolDetails(wbtcAddress, usdaAddress, expiry); position = call.result.expectOk().expectTuple(); - position['balance-x'].expectUint(3326359410718); - position['balance-y'].expectUint(33492031); // 33397031 + 0.95 * 0.001* ONE_8 + position['balance-x'].expectUint(3336219750000 - 9866803186); + position['balance-y'].expectUint(33397231 + 0.95 * 0.001 * ONE_8); // 33397031 + 0.95 * 0.001* ONE_8 } }) \ No newline at end of file diff --git a/clarity/tests/flash-loan_test.ts b/clarity/tests/flash-loan_test.ts index 5cf76ed3..961f4a67 100644 --- a/clarity/tests/flash-loan_test.ts +++ b/clarity/tests/flash-loan_test.ts @@ -19,7 +19,7 @@ const keyusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.key-usda-wbtc" const ytpyieldusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.ytp-yield-usda" const multisigncrpusdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-crp-usda-wbtc" const multisigytpyieldusda = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.multisig-ytp-yield-usda" -const loanuserAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.flash-loan-user-margin-wbtc-usda" +const loanuserAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.flash-loan-user-margin-usda-wbtc" const alexReservePoolAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.alex-reserve-pool" const ONE_8 = 100000000 @@ -64,7 +64,7 @@ Clarinet.test({ wbtcToken.transferToken(ONE_8, deployer.address, wallet_5.address, new ArrayBuffer(30)); let call = await FLTest.getBalance(wbtcAddress, wallet_5.address); - let position:any = call.result.expectOk().expectUint(1000000); + let position:any = call.result.expectOk().expectUint(100000000); let result = FWPTest.createPool(deployer, wbtcAddress, usdaAddress, weightX, weightY, fwpwbtcusdaAddress, multisigfwpAddress, Math.round(500000e+8 * ONE_8 / wbtcPrice), 500000e+8); result.expectOk().expectBool(true); @@ -73,18 +73,18 @@ Clarinet.test({ result = FWPTest.setOracleAverage(deployer, wbtcAddress, usdaAddress, weightX, weightY, 0.95e8); result.expectOk().expectBool(true); - result = YTPTest.createPool(deployer, expiry, yieldusda23040Address, usdaAddress, ytpyieldusda23040Address, multisigytpyieldusda23040, 500000e+8, 500000e+8); + result = YTPTest.createPool(deployer, expiry, yieldusdaAddress, usdaAddress, ytpyieldusdaAddress, multisigytpyieldusda, 500000e+8, 500000e+8); result.expectOk().expectBool(true); - result = CRPTest.createPool(deployer, usdaAddress, wbtcAddress, yieldusda23040Address, keyusda23040Address, multisigncrpusda23040Address, ltv_0, conversion_ltv, bs_vol, moving_average, 1e+8); + result = CRPTest.createPool(deployer, usdaAddress, wbtcAddress, expiry, yieldusdaAddress, keyusdaAddress, multisigncrpusdaAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 1e+8); result.expectOk().expectBool(true); - call = await FLTest.getBalance(keyusda23040Address, wallet_5.address); + call = await FLTest.getBalanceSFT(keyusdaAddress, expiry, wallet_5.address); position = call.result.expectOk().expectUint(0); - call = await FLTest.getBalance(yieldusda23040Address, wallet_5.address); + call = await FLTest.getBalanceSFT(yieldusdaAddress, expiry, wallet_5.address); position = call.result.expectOk().expectUint(0); // Let's borrow 1 BTC to lever up - result = FLTest.flashLoan(wallet_5, loanuser23040Address, wbtcAddress, ONE_8); + result = FLTest.flashLoan(wallet_5, loanuserAddress, wbtcAddress, ONE_8, expiry); result.expectOk(); // spent ~$231 to buy levered position (0.02 uints) @@ -93,37 +93,37 @@ Clarinet.test({ call = await FLTest.getBalance(usdaAddress, wallet_5.address); position = call.result.expectOk().expectUint(0); // should see change in key token - call = await FLTest.getBalance(keyusda23040Address, wallet_5.address); - position = call.result.expectOk().expectUint(4094610000000); + call = await FLTest.getBalanceSFT(keyusdaAddress, expiry, wallet_5.address); + position = call.result.expectOk().expectUint(4094610276984); // but nothing with yield token - call = await FLTest.getBalance(yieldusda23040Address, wallet_5.address); + call = await FLTest.getBalanceSFT(yieldusdaAddress, expiry, wallet_5.address); position = call.result.expectOk().expectUint(0); // let's test roll-position from margin-helper chain.mineEmptyBlockUntil(10000); // trying to roll before maturity throws error - result = FLTest.rollPosition(wallet_5, usdaAddress, wbtcAddress, keyusda23040Address, loanuser51840Address); + result = FLTest.rollPosition(wallet_5, usdaAddress, wbtcAddress, keyusdaAddress, loanuserAddress, expiry, nextExpiry); result.expectErr().expectUint(2017); // but let's set up new pools - result = YTPTest.createPool(deployer, expiry, yieldusda51840Address, usdaAddress, ytpyieldusda51840Address, multisigytpyieldusda51840, 500000e+8, 500000e+8); + result = YTPTest.createPool(deployer, nextExpiry, yieldusdaAddress, usdaAddress, ytpyieldusdaAddress, multisigytpyieldusda, 500000e+8, 500000e+8); result.expectOk().expectBool(true); - result = CRPTest.createPool(deployer, usdaAddress, wbtcAddress, yieldusda51840Address, keyusda51840Address, multisigncrpusda51840Address, ltv_0, conversion_ltv, bs_vol, moving_average, 1e+8); + result = CRPTest.createPool(deployer, usdaAddress, wbtcAddress, nextExpiry, yieldusdaAddress, keyusdaAddress, multisigncrpusdaAddress, ltv_0, conversion_ltv, bs_vol, moving_average, 1e+8); result.expectOk().expectBool(true); chain.mineEmptyBlockUntil((expiry / ONE_8) + 1); // roll right after expiry succeeds. - result = FLTest.rollPosition(wallet_5, usdaAddress, wbtcAddress, keyusda23040Address, loanuser51840Address); + result = FLTest.rollPosition(wallet_5, usdaAddress, wbtcAddress, keyusdaAddress, loanuserAddress, expiry, nextExpiry); result.expectOk(); - // key-usda-23040-wbtc should be zero, with non-zero positions in key-usda-51840 - call = await FLTest.getBalance(keyusda23040Address, wallet_5.address); + // key-usda--wbtc should be zero, with non-zero positions in key-usda-51840 + call = await FLTest.getBalanceSFT(keyusdaAddress, expiry, wallet_5.address); position = call.result.expectOk().expectUint(0); - call = await FLTest.getBalance(keyusda51840Address, wallet_5.address); - position = call.result.expectOk().expectUint(1793499000000); + call = await FLTest.getBalanceSFT(keyusdaAddress, nextExpiry, wallet_5.address); + position = call.result.expectOk().expectUint(1793499970806); // but nothing with yield-usda-51840 - call = await FLTest.getBalance(yieldusda51840Address, wallet_5.address); + call = await FLTest.getBalanceSFT(yieldusdaAddress, nextExpiry, wallet_5.address); position = call.result.expectOk().expectUint(0); }, diff --git a/clarity/tests/liquidity-bootstrapping-pool_test.ts b/clarity/tests/liquidity-bootstrapping-pool_test.ts index 176aa96b..3bdc2a5b 100644 --- a/clarity/tests/liquidity-bootstrapping-pool_test.ts +++ b/clarity/tests/liquidity-bootstrapping-pool_test.ts @@ -1,6 +1,7 @@ import { Clarinet, Tx, Chain, Account, types } from 'https://deno.land/x/clarinet@v0.14.0/index.ts'; import { LBPTestAgent } from './models/alex-tests-liquidity-bootstrapping-pool.ts'; +import { USDAToken, ALEXToken } from './models/alex-tests-tokens.ts'; // Deployer Address Constants const usdaAddress = "ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-usda" @@ -29,18 +30,19 @@ Clarinet.test({ async fn(chain: Chain, accounts: Map) { let deployer = accounts.get("deployer")!; let LBPTest = new LBPTestAgent(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let alexToken = new ALEXToken(chain, deployer); - let call = chain.callReadOnlyFn("token-alex", "get-balance", - [types.principal(deployer.address) - ], deployer.address); - call.result.expectOk().expectUint(100000000000000000); + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + alexToken.mintFixed(deployer.address, 1000000000 * ONE_8); // Deployer creating a pool, initial tokens injected to the pool let result = LBPTest.createPool(deployer, alexAddress, usdaAddress, weightX1, weightX2, expiry, poolTokenAddress, multisigAddress, alexQty, usdaQty); result.expectOk().expectBool(true); // Check pool details and print - call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); + let call = await LBPTest.getPoolDetails(alexAddress, usdaAddress, expiry); let position:any = call.result.expectOk().expectTuple(); position['total-supply'].expectUint(80274141756); position['balance-x'].expectUint(alexQty); @@ -168,6 +170,12 @@ Clarinet.test({ let deployer = accounts.get("deployer")!; let wallet_1 = accounts.get("wallet_1")!; let LBPTest = new LBPTestAgent(chain, deployer); + let usdaToken = new USDAToken(chain, deployer); + let alexToken = new ALEXToken(chain, deployer); + + // Deployer minting initial tokens + usdaToken.mintFixed(deployer.address, 1000000000 * ONE_8); + alexToken.mintFixed(deployer.address, 1000000000 * ONE_8); // non-deployer creating a pool will throw an error let result = LBPTest.createPool(wallet_1, alexAddress, usdaAddress, weightX1, weightX2, expiry, poolTokenAddress, multisigAddress, alexQty, usdaQty); diff --git a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts index 4e0f294c..67b4dc0b 100644 --- a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts +++ b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts @@ -73,11 +73,12 @@ import { ], this.deployer.address); } - createPool(user: Account, token: string, collateral: string, yieldToken: string, keyToken: string, multiSig: string, ltv_0: number, conversion_ltv: number, bs_vol: number, moving_average: number, dX: number) { + createPool(user: Account, token: string, collateral: string, expiry: number, yieldToken: string, keyToken: string, multiSig: string, ltv_0: number, conversion_ltv: number, bs_vol: number, moving_average: number, dX: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "create-pool", [ types.principal(token), types.principal(collateral), + types.uint(expiry), types.principal(yieldToken), types.principal(keyToken), types.principal(multiSig), @@ -91,11 +92,12 @@ import { return block.receipts[0].result; } - addToPosition(user: Account, token: string, collateral: string, yieldToken: string, keyToken:string, dX: number) { + addToPosition(user: Account, token: string, collateral: string, expiry: number, yieldToken: string, keyToken:string, dX: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "add-to-position", [ types.principal(token), types.principal(collateral), + types.uint(expiry), types.principal(yieldToken), types.principal(keyToken), types.uint(dX) @@ -104,11 +106,12 @@ import { return block.receipts[0].result; } - addToPositionAndSwitch(user: Account, token: string, collateral: string, yieldToken: string, keyToken: string, dX: number) { + addToPositionAndSwitch(user: Account, token: string, collateral: string, expiry: number, yieldToken: string, keyToken: string, dX: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "add-to-position-and-switch", [ types.principal(token), types.principal(collateral), + types.uint(expiry), types.principal(yieldToken), types.principal(keyToken), types.uint(dX) @@ -117,11 +120,12 @@ import { return block.receipts[0].result; } - reducePositionYield(user: Account, token: string, collateral: string, yieldToken: string, percent: number) { + reducePositionYield(user: Account, token: string, collateral: string, expiry: number, yieldToken: string, percent: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "reduce-position-yield", [ types.principal(token), types.principal(collateral), + types.uint(expiry), types.principal(yieldToken), types.uint(percent) ], user.address), @@ -129,11 +133,12 @@ import { return block.receipts[0].result; } - reducePositionKey(user: Account, token: string, collateral: string, keyToken: string, percent: number) { + reducePositionKey(user: Account, token: string, collateral: string, expiry: number, keyToken: string, percent: number) { let block = this.chain.mineBlock([ Tx.contractCall("collateral-rebalancing-pool", "reduce-position-key", [ types.principal(token), types.principal(collateral), + types.uint(expiry), types.principal(keyToken), types.uint(percent) ], user.address), @@ -268,17 +273,19 @@ import { ], this.deployer.address); } - burnKeyToken(user: Account, amount: number) { - let block = this.chain.mineBlock([Tx.contractCall("key-wbtc-59760-usda", "burn", [ - types.principal(user.address), - types.uint(amount) + burnKeyToken(user: Account, expiry: number, amount: number) { + let block = this.chain.mineBlock([Tx.contractCall("key-wbtc-usda", "burn-fixed", [ + types.uint(expiry), + types.uint(amount), + types.principal(user.address) ], user.address), ]); return block.receipts[0].result; } - transfer(user: Account, token: string, amount: number, sender: string, recipient: string, memo: ArrayBuffer) { + transfer(user: Account, token: string, expiry: number, amount: number, sender: string, recipient: string, memo: ArrayBuffer) { let block = this.chain.mineBlock([Tx.contractCall(token, "transfer-fixed", [ + types.uint(expiry), types.uint(amount), types.principal(sender), types.principal(recipient), @@ -288,8 +295,9 @@ import { return block.receipts[0].result; } - getBalance(token: string, owner: string) { - return this.chain.callReadOnlyFn(token, "get-balance", [ + getBalance(token: string, expiry: number, owner: string) { + return this.chain.callReadOnlyFn(token, "get-balance-fixed", [ + types.uint(expiry), types.principal(owner) ], this.deployer.address); } diff --git a/clarity/tests/models/alex-tests-flash-loan.ts b/clarity/tests/models/alex-tests-flash-loan.ts index ec574bb4..7cc43415 100644 --- a/clarity/tests/models/alex-tests-flash-loan.ts +++ b/clarity/tests/models/alex-tests-flash-loan.ts @@ -16,24 +16,27 @@ import { this.deployer = deployer; } - flashLoan(user: Account, loanuser:string, token:string, amount: number) { + flashLoan(user: Account, loanuser:string, token:string, amount: number, memo: number) { let block = this.chain.mineBlock([ Tx.contractCall("alex-vault", "flash-loan", [ types.principal(loanuser), types.principal(token), types.uint(amount), + types.some(types.uint(memo)) ], user.address), ]); return block.receipts[0].result; } - rollPosition(user: Account, token: string, collateral: string, keyToken: string, loanuser:string) { + rollPosition(user: Account, token: string, collateral: string, keyToken: string, loanuser:string, expiry: number, expiry_to_roll: number) { let block = this.chain.mineBlock([ Tx.contractCall("margin-helper", "roll-position", [ types.principal(token), types.principal(collateral), types.principal(keyToken), - types.principal(loanuser) + types.principal(loanuser), + types.uint(expiry), + types.uint(expiry_to_roll) ], user.address), ]); return block.receipts[0].result; @@ -43,7 +46,14 @@ import { return this.chain.callReadOnlyFn(token, "get-balance-fixed", [ types.principal(owner) ], this.deployer.address); - } + } + + getBalanceSFT(token: string, expiry: number, owner: string) { + return this.chain.callReadOnlyFn(token, "get-balance-fixed", [ + types.uint(expiry), + types.principal(owner) + ], this.deployer.address); + } } export { FLTestAgent1 }; diff --git a/clarity/tests/models/alex-tests-multisigs.ts b/clarity/tests/models/alex-tests-multisigs.ts index 88e650cf..d722fb8c 100644 --- a/clarity/tests/models/alex-tests-multisigs.ts +++ b/clarity/tests/models/alex-tests-multisigs.ts @@ -177,4 +177,60 @@ endProposal(proposalID: number) { return block.receipts[0].result; } } -export { MS_CRP_USDA_WBTC }; \ No newline at end of file +export { MS_CRP_USDA_WBTC }; + +class MS_CRP_WBTC_USDA { + chain: Chain; + deployer: Account; + + constructor(chain: Chain, deployer: Account) { + this.chain = chain; + this.deployer = deployer; + } + + propose(contractCaller: Account, expiry: number, startBlockHeight: number, proposeTitle: string, proposeURL: string, feeRateX: number, feeRateY: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("multisig-crp-wbtc-usda", "propose", [ + types.uint(expiry), + types.uint(startBlockHeight), + types.utf8(proposeTitle), + types.utf8(proposeURL), + types.uint(feeRateX), + types.uint(feeRateY), + ], contractCaller.address), + ]); + return block.receipts[0].result; + } + + voteFor(contractCaller: Account, token: string, proposalID: number, amount: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("multisig-crp-wbtc-usda", "vote-for", [ + types.principal(token), + types.uint(proposalID), + types.uint(amount) + ], contractCaller.address), + ]); + return block.receipts[0].result; + } + + voteAgainst(contractCaller: Account, token: string, proposalID: number, amount: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("multisig-crp-wbtc-usda", "vote-against", [ + types.principal(token), + types.uint(proposalID), + types.uint(amount) + ], contractCaller.address), + ]); + return block.receipts[0].result; + } + + endProposal(proposalID: number) { + let block = this.chain.mineBlock([ + Tx.contractCall("multisig-crp-wbtc-usda", "end-proposal", [ + types.uint(proposalID), + ], this.deployer.address), + ]); + return block.receipts[0].result; + } + } + export { MS_CRP_WBTC_USDA }; \ No newline at end of file diff --git a/clarity/tests/models/alex-tests-tokens.ts b/clarity/tests/models/alex-tests-tokens.ts index a2c1335e..c3c9d8d0 100644 --- a/clarity/tests/models/alex-tests-tokens.ts +++ b/clarity/tests/models/alex-tests-tokens.ts @@ -7,6 +7,60 @@ import { } from "https://deno.land/x/clarinet@v0.13.0/index.ts"; +class ALEXToken { + chain: Chain; + deployer: Account; + + constructor(chain: Chain, deployer: Account) { + this.chain = chain; + this.deployer = deployer; + } + + balanceOf(wallet: string) { + return this.chain.callReadOnlyFn("token-alex", "get-balance-fixed", [ + types.principal(wallet), + ], this.deployer.address); + } + + getBalance(account: string) { + return this.chain.callReadOnlyFn("token-alex", "get-balance", [ + types.principal(account), + ], this.deployer.address); + } + + // Always need to called by deployer + mint(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-alex", "mint", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + + mintFixed(recipient: string, amount : number) { + return this.chain.callReadOnlyFn("token-alex", "mint-fixed", [ + types.uint(amount), + types.principal(recipient), + ], this.deployer.address); + } + + transferToken(amount: number, sender: string, receiver: string, memo:ArrayBuffer) { + let block = this.chain.mineBlock([ + Tx.contractCall("token-alex", "transfer-fixed", [ + types.uint(amount), + types.principal(sender), + types.principal(receiver), + types.some(types.buff(memo)) + ], this.deployer.address), + ]); + return block.receipts[0].result; + } + + totalSupply() { + return this.chain.callReadOnlyFn("token-alex", "get-total-supply-fixed", [], this.deployer.address); + } +} +export { ALEXToken }; + class USDAToken { chain: Chain; deployer: Account; @@ -240,6 +294,30 @@ class KEY_USDA_WBTC { } export { KEY_USDA_WBTC }; +class KEY_WBTC_USDA { + chain: Chain; + deployer: Account; + + constructor(chain: Chain, deployer: Account) { + this.chain = chain; + this.deployer = deployer; + } + + balanceOf(expiry: number, wallet: string) { + return this.chain.callReadOnlyFn("key-wbtc-usda", "get-balance-fixed", [ + types.uint(expiry), + types.principal(wallet), + ], this.deployer.address); + } + + totalSupply(expiry: number) { + return this.chain.callReadOnlyFn("key-wbtc-usda", "get-total-supply-fixed", [ + types.uint(expiry) + ], this.deployer.address); + } +} +export { KEY_WBTC_USDA }; + class YIELD_WBTC { chain: Chain; deployer: Account; diff --git a/clarity/tests/yield-token-pool_test.ts b/clarity/tests/yield-token-pool_test.ts index 5ff15c79..f253e0e1 100644 --- a/clarity/tests/yield-token-pool_test.ts +++ b/clarity/tests/yield-token-pool_test.ts @@ -233,7 +233,7 @@ Clarinet.test({ call = chain.callReadOnlyFn(yieldwbtcAddress, "get-balance-fixed", [types.uint(expiry), types.principal(deployer.address) ], deployer.address); - call.result.expectOk().expectUint(998502878000); + call.result.expectOk().expectUint(998502878167); // Add back some liquidity result = YTPTest.addToPosition(deployer, expiry, yieldwbtcAddress, wbtcAddress, ytpyieldwbtcAddress, 1000*ONE_8); From f8f41a09f6cce5c24b289ee3d55455246f05ba07 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Tue, 16 Nov 2021 22:40:00 +0800 Subject: [PATCH 17/25] clarinet test passes --- .../flash-loan-user-margin-usda-wbtc.clar | 40 +++ .../flash-loan-user-margin-wbtc-usda.clar | 28 -- .../contracts/key-token/key-wbtc-usda.clar | 152 +++++++++ .../contracts/key-token/key-wbtc-wbtc.clar | 152 +++++++++ .../multisig/multisig-crp-wbtc-usda.clar | 295 ++++++++++++++++++ .../multisig/multisig-crp-wbtc-wbtc.clar | 295 ++++++++++++++++++ 6 files changed, 934 insertions(+), 28 deletions(-) create mode 100644 clarity/contracts/flash-loan-user-margin-usda-wbtc.clar delete mode 100644 clarity/contracts/flash-loan-user-margin-wbtc-usda.clar create mode 100644 clarity/contracts/key-token/key-wbtc-usda.clar create mode 100644 clarity/contracts/key-token/key-wbtc-wbtc.clar create mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-usda.clar create mode 100644 clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar diff --git a/clarity/contracts/flash-loan-user-margin-usda-wbtc.clar b/clarity/contracts/flash-loan-user-margin-usda-wbtc.clar new file mode 100644 index 00000000..bf64b5dd --- /dev/null +++ b/clarity/contracts/flash-loan-user-margin-usda-wbtc.clar @@ -0,0 +1,40 @@ +(impl-trait .trait-flash-loan-user.flash-loan-user-trait) +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-constant ONE_8 (pow u10 u8)) + +(define-public (execute (collateral ) (amount uint) (memo (optional uint))) + (let + ( + ;; gross amount * ltv / price = amount + ;; gross amount = amount * price / ltv + (ltv (try! (contract-call? .collateral-rebalancing-pool get-ltv .token-usda .token-wbtc (default-to u0 memo)))) + (price (try! (contract-call? .yield-token-pool get-price (default-to u0 memo) .yield-usda))) + (gross-amount (mul-up amount (div-down price ltv))) + (swapped-token (get dx (try! (contract-call? .collateral-rebalancing-pool add-to-position-and-switch .token-usda .token-wbtc (default-to u0 memo) .yield-usda .key-usda-wbtc gross-amount)))) + ) + ;; swap token to collateral so we can return flash-loan + (try! (contract-call? .fixed-weight-pool swap .token-wbtc .token-usda u50000000 u50000000 swapped-token none)) + (print { object: "flash-loan-user-margin-usda-wbtc", action: "execute", data: gross-amount }) + (ok true) + ) +) + +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) + +(define-private (div-down (a uint) (b uint)) + (if (is-eq a u0) + u0 + (/ (* a ONE_8) b) + ) +) \ No newline at end of file diff --git a/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar b/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar deleted file mode 100644 index cea54ea3..00000000 --- a/clarity/contracts/flash-loan-user-margin-wbtc-usda.clar +++ /dev/null @@ -1,28 +0,0 @@ -(impl-trait .trait-flash-loan-user.flash-loan-user-trait) -(use-trait ft-trait .trait-sip-010.sip-010-trait) - -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) - -(define-constant ONE_8 (pow u10 u8)) - -(define-public (execute (token ) (amount uint) (memo (optional 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 (default-to u0 memo)))) - (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 (default-to u0 memo) .yield-usda .key-usda-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", action: "execute", data: gross-amount }) - (ok true) - ) -) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-usda.clar b/clarity/contracts/key-token/key-wbtc-usda.clar new file mode 100644 index 00000000..5a6cde2a --- /dev/null +++ b/clarity/contracts/key-token/key-wbtc-usda.clar @@ -0,0 +1,152 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + +(define-fungible-token key-wbtc-usda) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) + +(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)) + ) +) + +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance key-wbtc-usda who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply key-wbtc-usda)) +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? key-wbtc-usda amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) + (ok true) + ) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (try! (check-is-approved contract-caller)) + (try! (ft-mint? key-wbtc-usda amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (try! (check-is-approved contract-caller)) + (try! (ft-burn? key-wbtc-usda amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply key-wbtc-usda))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance key-wbtc-usda who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .collateral-rebalancing-pool true) +) \ No newline at end of file diff --git a/clarity/contracts/key-token/key-wbtc-wbtc.clar b/clarity/contracts/key-token/key-wbtc-wbtc.clar new file mode 100644 index 00000000..42781d77 --- /dev/null +++ b/clarity/contracts/key-token/key-wbtc-wbtc.clar @@ -0,0 +1,152 @@ +(impl-trait .trait-ownable.ownable-trait) +(impl-trait .trait-semi-fungible-token.semi-fungible-token-trait) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-INVALID-BALANCE (err u2008)) + +(define-fungible-token key-wbtc-wbtc) +(define-map token-balances {token-id: uint, owner: principal} uint) +(define-map token-supplies uint uint) +(define-map token-owned principal (list 2000 uint)) + +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) + +(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)) + ) +) + +(define-private (check-is-approved (sender principal)) + (ok (asserts! (or (default-to false (map-get? approved-contracts sender)) (is-eq sender (var-get contract-owner))) ERR-NOT-AUTHORIZED)) +) + +(define-read-only (get-token-owned (owner principal)) + (default-to (list) (map-get? token-owned owner)) +) + +(define-private (set-balance (token-id uint) (balance uint) (owner principal)) + (begin + (map-set token-balances {token-id: token-id, owner: owner} balance) + (map-set token-owned owner (unwrap! (as-max-len? (append (get-token-owned owner) token-id) u2000) ERR-TOO-MANY-POOLS)) + (ok true) + ) +) + +(define-private (get-balance-or-default (token-id uint) (who principal)) + (default-to u0 (map-get? token-balances {token-id: token-id, owner: who})) +) + +(define-read-only (get-balance (token-id uint) (who principal)) + (ok (get-balance-or-default token-id who)) +) + +(define-read-only (get-overall-balance (who principal)) + (ok (ft-get-balance key-wbtc-wbtc who)) +) + +(define-read-only (get-total-supply (token-id uint)) + (ok (default-to u0 (map-get? token-supplies token-id))) +) + +(define-read-only (get-overall-supply) + (ok (ft-get-supply key-wbtc-wbtc)) +) + +(define-read-only (get-decimals) + (ok u8) +) + +(define-read-only (get-token-uri (token-id uint)) + (ok none) +) + +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? key-wbtc-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient, memo: memo}) + (ok true) + ) +) + +(define-public (mint (token-id uint) (amount uint) (recipient principal)) + (begin + (try! (check-is-approved contract-caller)) + (try! (ft-mint? key-wbtc-wbtc amount recipient)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (map-set token-supplies token-id (+ (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_mint_event", token-id: token-id, amount: amount, recipient: recipient}) + (ok true) + ) +) + +(define-public (burn (token-id uint) (amount uint) (sender principal)) + (begin + (try! (check-is-approved contract-caller)) + (try! (ft-burn? key-wbtc-wbtc amount sender)) + (try! (set-balance token-id (- (get-balance-or-default token-id sender) amount) sender)) + (map-set token-supplies token-id (- (unwrap-panic (get-total-supply token-id)) amount)) + (print {type: "sft_burn_event", token-id: token-id, amount: amount, sender: sender}) + (ok true) + ) +) + +(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-total-supply-fixed (token-id uint)) + (ok (decimals-to-fixed (default-to u0 (map-get? token-supplies token-id)))) +) + +(define-read-only (get-balance-fixed (token-id uint) (who principal)) + (ok (decimals-to-fixed (get-balance-or-default token-id who))) +) + +(define-read-only (get-overall-supply-fixed) + (ok (decimals-to-fixed (ft-get-supply key-wbtc-wbtc))) +) + +(define-read-only (get-overall-balance-fixed (who principal)) + (ok (decimals-to-fixed (ft-get-balance key-wbtc-wbtc who))) +) + +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) + (transfer token-id (fixed-to-decimals amount) sender recipient memo) +) + +(define-public (mint-fixed (token-id uint) (amount uint) (recipient principal)) + (mint token-id (fixed-to-decimals amount) recipient) +) + +(define-public (burn-fixed (token-id uint) (amount uint) (sender principal)) + (burn token-id (fixed-to-decimals amount) sender) +) + +(begin + (map-set approved-contracts .collateral-rebalancing-pool true) +) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar new file mode 100644 index 00000000..83c1d745 --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar @@ -0,0 +1,295 @@ +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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, + expiry: uint, + 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, expiry: uint } { 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 ) (expiry uint)) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + expiry: u0, + 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 yield-token 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) (is-eq (contract-of token) .key-wbtc-usda)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (expiry uint) + (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 get-balance-fixed expiry tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-usda get-balance-fixed expiry tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc get-total-supply-fixed expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-usda get-total-supply-fixed expiry))) + (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, + expiry: expiry, + 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)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + + ;; 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-fixed expiry 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), expiry: expiry } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + ;; 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-fixed expiry 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) , expiry: expiry } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc get-total-supply-fixed expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-usda get-total-supply-fixed expiry))) + (total-supply (+ total-yield-supply total-key-supply)) + (threshold-count (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 + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) + ) + + (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-fixed expiry 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)) + (expiry (get expiry proposal)) + (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 expiry new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-usda expiry new-fee-rate-y)) + + (ok true) + ) +) +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) \ No newline at end of file diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar b/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar new file mode 100644 index 00000000..80bc7564 --- /dev/null +++ b/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar @@ -0,0 +1,295 @@ +(impl-trait .trait-multisig-vote.multisig-vote-sft-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-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, + expiry: uint, + 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, expiry: uint } { 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 ) (expiry uint)) + (default-to + { amount: u0 } + (map-get? tokens-by-member { proposal-id: proposal-id, member: member, token: (contract-of token), expiry: expiry }) + ) +) + +;; Get proposal +(define-read-only (get-proposal-by-id (proposal-id uint)) + (default-to + { + id: u0, + proposer: DEFAULT_OWNER, + expiry: u0, + 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 yield-token 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) (is-eq (contract-of token) .key-wbtc-wbtc)) +) + + +;; Start a proposal +;; Requires 10% of the supply in your wallet +;; Default voting period is 10 days (144 * 10 blocks) +(define-public (propose + (expiry uint) + (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 get-balance-fixed expiry tx-sender))) + (proposer-key-balance (unwrap-panic (contract-call? .key-wbtc-wbtc get-balance-fixed expiry tx-sender))) + (proposer-balance (+ proposer-yield-balance proposer-key-balance)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc get-total-supply-fixed expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-wbtc get-total-supply-fixed expiry))) + (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, + expiry: expiry, + 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)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + + ;; 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-fixed expiry 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), expiry: expiry } + { amount: (+ amount token-count)}) + + (ok amount) + + ) + ) + +(define-public (vote-against (token ) (proposal-id uint) (amount uint)) + (let ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (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 expiry))) + ) + ;; 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-fixed expiry 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) , expiry: expiry } + { amount: (+ amount token-count)}) + (ok amount) + ) + + ) + +(define-public (end-proposal (proposal-id uint)) + (let + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (threshold-percent (var-get threshold)) + (total-yield-supply (unwrap-panic (contract-call? .yield-wbtc get-total-supply-fixed expiry))) + (total-key-supply (unwrap-panic (contract-call? .key-wbtc-wbtc get-total-supply-fixed expiry))) + (total-supply (+ total-yield-supply total-key-supply)) + (threshold-count (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 + ( + (proposal (get-proposal-by-id proposal-id)) + (expiry (get expiry proposal)) + (token-count (get amount (get-tokens-by-member-by-id proposal-id member token expiry))) + ) + + (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-fixed expiry 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)) + (expiry (get expiry proposal)) + (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-wbtc expiry new-fee-rate-x)) + (try! (contract-call? .collateral-rebalancing-pool set-fee-rate-y .token-wbtc .token-wbtc expiry new-fee-rate-y)) + + (ok true) + ) +) +(define-private (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) \ No newline at end of file From 758f3f547bd1f7be096cafeced0f120f551b47bf Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Wed, 17 Nov 2021 14:51:18 +0500 Subject: [PATCH 18/25] Implements feature #352 --- clarity/contracts/alex-vault.clar | 2 +- .../contracts/key-token/key-usda-wbtc.clar | 23 +++++++++++++++++-- .../contracts/key-token/key-wbtc-usda.clar | 23 +++++++++++++++++-- .../contracts/key-token/key-wbtc-wbtc.clar | 23 +++++++++++++++++-- .../multisig/multisig-crp-usda-wbtc.clar | 6 ++--- .../multisig/multisig-crp-wbtc-usda.clar | 6 ++--- .../multisig/multisig-crp-wbtc-wbtc.clar | 6 ++--- .../multisig/multisig-ytp-yield-usda.clar | 6 ++--- .../multisig/multisig-ytp-yield-wbtc.clar | 6 ++--- .../contracts/pool-token/ytp-yield-usda.clar | 23 +++++++++++++++++-- .../contracts/pool-token/ytp-yield-wbtc.clar | 23 +++++++++++++++++-- clarity/contracts/pool/yield-token-pool.clar | 4 ++-- .../traits/trait-semi-fungible-token.clar | 9 ++++++-- clarity/contracts/yield-token/yield-usda.clar | 23 +++++++++++++++++-- clarity/contracts/yield-token/yield-wbtc.clar | 23 +++++++++++++++++-- .../alex-tests-collateral-rebalancing-pool.ts | 3 +-- 16 files changed, 173 insertions(+), 36 deletions(-) diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index fcb380c0..55149d64 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -76,7 +76,7 @@ (define-public (transfer-sft (token ) (token-id uint) (amount uint) (recipient principal)) (begin (try! (check-is-approved contract-caller)) - (as-contract (unwrap! (contract-call? token transfer-fixed token-id amount tx-sender recipient none) ERR-TRANSFER-FAILED)) + (as-contract (unwrap! (contract-call? token transfer-fixed token-id amount tx-sender recipient) ERR-TRANSFER-FAILED)) (ok true) ) ) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index d663a701..1acf64fc 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? key-usda-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance key-usda-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/contracts/key-token/key-wbtc-usda.clar b/clarity/contracts/key-token/key-wbtc-usda.clar index 5a6cde2a..2b69e992 100644 --- a/clarity/contracts/key-token/key-wbtc-usda.clar +++ b/clarity/contracts/key-token/key-wbtc-usda.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? key-wbtc-usda amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance key-wbtc-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/contracts/key-token/key-wbtc-wbtc.clar b/clarity/contracts/key-token/key-wbtc-wbtc.clar index 42781d77..eaa9d657 100644 --- a/clarity/contracts/key-token/key-wbtc-wbtc.clar +++ b/clarity/contracts/key-token/key-wbtc-wbtc.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? key-wbtc-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance key-wbtc-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar index 293042ae..a19a2d4e 100644 --- a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar @@ -172,7 +172,7 @@ (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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -203,7 +203,7 @@ ;; 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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -261,7 +261,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member))) (ok true) ) ) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar index 83c1d745..b24f3083 100644 --- a/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar +++ b/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar @@ -172,7 +172,7 @@ (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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -203,7 +203,7 @@ ;; 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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -261,7 +261,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member))) (ok true) ) ) diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar b/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar index 80bc7564..2bc81191 100644 --- a/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar @@ -172,7 +172,7 @@ (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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -203,7 +203,7 @@ ;; 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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -261,7 +261,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member))) (ok true) ) ) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index a369b882..2af3f862 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -167,7 +167,7 @@ (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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -201,7 +201,7 @@ ;; 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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -258,7 +258,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member))) (ok true) ) ) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar index ba3ba37d..71b6a18d 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar @@ -167,7 +167,7 @@ (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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals { id: proposal-id } @@ -197,7 +197,7 @@ ;; 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-fixed expiry amount tx-sender (as-contract tx-sender) none)) + (try! (contract-call? token transfer-fixed expiry amount tx-sender (as-contract tx-sender))) ;; Mutate (map-set proposals @@ -253,7 +253,7 @@ (asserts! (>= block-height (get end-block-height proposal)) ERR-NOT-AUTHORIZED) ;; Return the pool token - (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member none))) + (try! (as-contract (contract-call? token transfer-fixed expiry token-count (as-contract tx-sender) member))) (ok true) ) ) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index 21c0f6d7..9bfc355f 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? ytp-yield-usda amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance ytp-yield-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar index 7356e5b7..5dddcd1f 100644 --- a/clarity/contracts/pool-token/ytp-yield-wbtc.clar +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? ytp-yield-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance ytp-yield-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 7067b6df..6e60eedb 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -366,7 +366,7 @@ ;; send x to vault (unwrap! (contract-call? the-token transfer-fixed dx tx-sender .alex-vault none) ERR-TRANSFER-X-FAILED) ;; send y to vault - (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry new-dy-act tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + (and (> new-dy-act u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry new-dy-act tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) ;; mint pool token and send to tx-sender (map-set pools-data-map { yield-token: yield-token, expiry: expiry } pool-updated) @@ -534,7 +534,7 @@ (asserts! (< (default-to u0 min-dx) dx) ERR-EXCEEDS-MAX-SLIPPAGE) (and (> dx u0) (try! (contract-call? .alex-vault transfer-ft the-token dx tx-sender))) - (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault none) ERR-TRANSFER-Y-FAILED)) + (and (> dy u0) (unwrap! (contract-call? the-yield-token transfer-fixed expiry dy tx-sender .alex-vault) ERR-TRANSFER-Y-FAILED)) (try! (contract-call? .alex-reserve-pool add-to-balance yield-token (- fee fee-rebate))) ;; post setting diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar index 4b2e6cff..b0abd549 100644 --- a/clarity/contracts/traits/trait-semi-fungible-token.clar +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -21,13 +21,18 @@ (get-token-uri (uint) (response (optional (string-utf8 256)) uint)) ;; Transfer from one principal to another. - (transfer (uint uint principal principal (optional (buff 34))) (response bool uint)) + (transfer (uint uint principal principal) (response bool uint)) + ;; Transfer from one principal to another with a memo. + (transfer-memo (uint uint principal principal (buff 34)) (response bool uint)) + + ;; Other functions (mint (uint uint principal) (response bool uint)) (burn (uint uint principal) (response bool uint)) ;; helper functions for 8-digit fixed notation - (transfer-fixed (uint uint principal principal (optional (buff 34))) (response bool uint)) + (transfer-fixed (uint uint principal principal) (response bool uint)) + (transfer-memo-fixed (uint uint principal principal (buff 34)) (response bool uint)) (get-balance-fixed (uint principal) (response uint uint)) (get-total-supply-fixed (uint) (response uint uint)) (get-total-supply-fixed (uint) (response uint uint)) diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index 474ae8be..648b0d9c 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? yield-usda amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance yield-usda who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar index 6d47d925..d56314f5 100644 --- a/clarity/contracts/yield-token/yield-wbtc.clar +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -68,7 +68,22 @@ (ok none) ) -(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer (token-id uint) (amount uint) (sender principal) (recipient principal)) + (let + ( + (sender-balance (get-balance-or-default token-id sender)) + ) + (asserts! (is-eq tx-sender sender) ERR-NOT-AUTHORIZED) + (asserts! (<= amount sender-balance) ERR-INVALID-BALANCE) + (try! (ft-transfer? yield-wbtc amount sender recipient)) + (try! (set-balance token-id (- sender-balance amount) sender)) + (try! (set-balance token-id (+ (get-balance-or-default token-id recipient) amount) recipient)) + (print {type: "sft_transfer_event", token-id: token-id, amount: amount, sender: sender, recipient: recipient}) + (ok true) + ) +) + +(define-public (transfer-memo (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (let ( (sender-balance (get-balance-or-default token-id sender)) @@ -135,7 +150,11 @@ (ok (decimals-to-fixed (ft-get-balance yield-wbtc who))) ) -(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) +(define-public (transfer-fixed (token-id uint) (amount uint) (sender principal) (recipient principal)) + (transfer token-id (fixed-to-decimals amount) sender recipient) +) + +(define-public (transfer-memo-fixed (token-id uint) (amount uint) (sender principal) (recipient principal) (memo (buff 34))) (transfer token-id (fixed-to-decimals amount) sender recipient memo) ) diff --git a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts index 67b4dc0b..bcec48ba 100644 --- a/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts +++ b/clarity/tests/models/alex-tests-collateral-rebalancing-pool.ts @@ -283,13 +283,12 @@ import { return block.receipts[0].result; } - transfer(user: Account, token: string, expiry: number, amount: number, sender: string, recipient: string, memo: ArrayBuffer) { + transfer(user: Account, token: string, expiry: number, amount: number, sender: string, recipient: string) { let block = this.chain.mineBlock([Tx.contractCall(token, "transfer-fixed", [ types.uint(expiry), types.uint(amount), types.principal(sender), types.principal(recipient), - types.some(types.buff(memo)) ], user.address), ]); return block.receipts[0].result; From 1fe128c8347b6ad88f5488d37276af96afd2a331 Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Wed, 17 Nov 2021 15:09:17 +0500 Subject: [PATCH 19/25] Fixes #351 of Remove unnecessary/unused error codes from contracts --- clarity/contracts/alex-vault.clar | 4 ---- .../equations/yield-token-equation.clar | 1 - .../multisig/multisig-crp-usda-wbtc.clar | 2 -- .../multisig/multisig-crp-wbtc-usda.clar | 2 -- .../multisig/multisig-crp-wbtc-wbtc.clar | 2 -- .../multisig-fwp-wbtc-usda-50-50.clar | 2 -- .../multisig-lbp-alex-usda-90-10.clar | 2 -- .../multisig/multisig-ytp-yield-usda.clar | 1 - .../multisig/multisig-ytp-yield-wbtc.clar | 2 -- .../new-lib/math-new-fixed-point.clar | 7 ------ clarity/contracts/pool/alex-reserve-pool.clar | 23 ------------------- .../pool/collateral-rebalancing-pool.clar | 8 ------- clarity/contracts/pool/fixed-weight-pool.clar | 8 ------- .../pool/liquidity-bootstrapping-pool.clar | 1 - clarity/contracts/pool/yield-token-pool.clar | 8 ------- 15 files changed, 73 deletions(-) diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index 55149d64..4ad6a247 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -8,15 +8,11 @@ (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-INSUFFICIENT-FLASH-LOAN-BALANCE (err u3003)) -(define-constant ERR-INVALID-POST-LOAN-BALANCE (err u3004)) -(define-constant ERR-USER-EXECUTE (err u3005)) (define-constant ERR-TRANSFER-FAILED (err u3000)) (define-constant ERR-STX-TRANSFER-FAILED (err u9003)) (define-constant ERR-LOAN-TRANSFER-FAILED (err u3006)) (define-constant ERR-POST-LOAN-TRANSFER-FAILED (err u3007)) (define-constant ERR-INVALID-FLASH-LOAN (err u3008)) -(define-constant ERR-MATH-CALL (err u2010)) -(define-constant ERR-INTERNAL-FUNCTION-CALL (err u1001)) (define-data-var CONTRACT-OWNER principal tx-sender) diff --git a/clarity/contracts/equations/yield-token-equation.clar b/clarity/contracts/equations/yield-token-equation.clar index 08ab67f5..fe3b7819 100644 --- a/clarity/contracts/equations/yield-token-equation.clar +++ b/clarity/contracts/equations/yield-token-equation.clar @@ -9,7 +9,6 @@ (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-NO-LIQUIDITY (err u2002)) -(define-constant ERR-WEIGHT-SUM (err u4000)) (define-constant ERR-MAX-IN-RATIO (err u4001)) (define-constant ERR-MAX-OUT-RATIO (err u4002)) (define-constant ERR-INSUFFICIENT-BALANCE (err u4004)) diff --git a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar index a19a2d4e..e648038f 100644 --- a/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-usda-wbtc.clar @@ -14,11 +14,9 @@ ;; 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 diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar b/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar index b24f3083..bf563f17 100644 --- a/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar +++ b/clarity/contracts/multisig/multisig-crp-wbtc-usda.clar @@ -14,11 +14,9 @@ ;; 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 diff --git a/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar b/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar index 2bc81191..4d6ab64b 100644 --- a/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar +++ b/clarity/contracts/multisig/multisig-crp-wbtc-wbtc.clar @@ -14,11 +14,9 @@ ;; 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 diff --git a/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar b/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar index 5d804c65..d56f8d7c 100644 --- a/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar +++ b/clarity/contracts/multisig/multisig-fwp-wbtc-usda-50-50.clar @@ -14,11 +14,9 @@ ;; 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 diff --git a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar index 4d2c5ce9..1d05f4ac 100644 --- a/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar +++ b/clarity/contracts/multisig/multisig-lbp-alex-usda-90-10.clar @@ -14,11 +14,9 @@ ;; 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 diff --git a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar index 2af3f862..f6b72ab0 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-usda.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-usda.clar @@ -14,7 +14,6 @@ ;; 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)) diff --git a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar index 71b6a18d..a13abf91 100644 --- a/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar +++ b/clarity/contracts/multisig/multisig-ytp-yield-wbtc.clar @@ -14,11 +14,9 @@ ;; 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) diff --git a/clarity/contracts/new-lib/math-new-fixed-point.clar b/clarity/contracts/new-lib/math-new-fixed-point.clar index 4e73cdf4..35e35467 100644 --- a/clarity/contracts/new-lib/math-new-fixed-point.clar +++ b/clarity/contracts/new-lib/math-new-fixed-point.clar @@ -6,13 +6,6 @@ ;; constants ;; (define-constant ONE_10 (pow u10 u10)) ;; 10 decimal places -(define-constant ERR-SCALE-UP-OVERFLOW (err u5001)) -(define-constant ERR-SCALE-DOWN-OVERFLOW (err u5002)) -(define-constant ERR-ADD-OVERFLOW (err u5003)) -(define-constant ERR-SUB-OVERFLOW (err u5004)) -(define-constant ERR-MUL-OVERFLOW (err u5005)) -(define-constant ERR-DIV-OVERFLOW (err u5006)) -(define-constant ERR-POW-OVERFLOW (err u5007)) ;; With 10 fixed digits you would have a maximum error of 0.5 * 10^-10 in each entry, ;; which could aggregate to about 10 x 0.5 * 10^-10 = 4 * 10^-10 relative error diff --git a/clarity/contracts/pool/alex-reserve-pool.clar b/clarity/contracts/pool/alex-reserve-pool.clar index fc3bed00..e95b04a6 100644 --- a/clarity/contracts/pool/alex-reserve-pool.clar +++ b/clarity/contracts/pool/alex-reserve-pool.clar @@ -3,40 +3,17 @@ ;; alex-reserve-pool -(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-X (err u2005)) -(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-INTERNAL-FUNCTION-CALL (err u1001)) -(define-constant ERR-GET-WEIGHT-FAIL (err u2012)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) -(define-constant ERR-GET-PRICE-FAIL (err u2015)) -(define-constant ERR-GET-SYMBOL-FAIL (err u6000)) -(define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) -(define-constant ERR-EXPIRY (err u2017)) -(define-constant ERR-GET-BALANCE-FAIL (err u6001)) (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-TRANSFER-FAILED (err u3000)) (define-constant ERR-USER-ALREADY-REGISTERED (err u10001)) -(define-constant ERR-USER-NOT-FOUND (err u10002)) (define-constant ERR-USER-ID-NOT-FOUND (err u10003)) (define-constant ERR-ACTIVATION-THRESHOLD-REACHED (err u10004)) -(define-constant ERR-UNABLE-TO-SET-THRESHOLD (err u10021)) (define-constant ERR-CONTRACT-NOT-ACTIVATED (err u10005)) (define-constant ERR-STAKING-NOT-AVAILABLE (err u10015)) (define-constant ERR-CANNOT-STAKE (err u10016)) (define-constant ERR-REWARD-CYCLE-NOT-COMPLETED (err u10017)) (define-constant ERR-NOTHING-TO-REDEEM (err u10018)) (define-constant ERR-AMOUNT-EXCEED-RESERVE (err u2024)) -(define-constant ERR-TOO-MANY-TOKENS (err u2025)) (define-constant ERR-INVALID-TOKEN (err u2026)) (define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index 3080d2aa..e8e817a8 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -11,22 +11,14 @@ (define-constant ONE_8 u100000000) ;; 8 decimal places (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-X (err u2005)) -(define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-WEIGHTED-EQUATION-CALL (err u2009)) -(define-constant ERR-INTERNAL-FUNCTION-CALL (err u1001)) (define-constant ERR-GET-WEIGHT-FAIL (err u2012)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) -(define-constant ERR-GET-PRICE-FAIL (err u2015)) -(define-constant ERR-GET-SYMBOL-FAIL (err u6000)) -(define-constant ERR-GET-ORACLE-PRICE-FAIL (err u7000)) (define-constant ERR-EXPIRY (err u2017)) (define-constant ERR-GET-BALANCE-FIXED-FAIL (err u6001)) (define-constant ERR-NOT-AUTHORIZED (err u1000)) diff --git a/clarity/contracts/pool/fixed-weight-pool.clar b/clarity/contracts/pool/fixed-weight-pool.clar index 3d7b1998..c387701e 100644 --- a/clarity/contracts/pool/fixed-weight-pool.clar +++ b/clarity/contracts/pool/fixed-weight-pool.clar @@ -10,20 +10,12 @@ (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-INVALID-BALANCE (err u2008)) -(define-constant ERR-INVALID-TOKEN (err u2007)) -(define-constant ERR-NO-FEE-X (err u2005)) -(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-EXCEEDS-MAX-SLIPPAGE (err u2020)) (define-constant ERR-ORACLE-NOT-ENABLED (err u7002)) (define-constant ERR-ORACLE-ALREADY-ENABLED (err u7003)) diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index 0a5c4631..c2075648 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -17,7 +17,6 @@ (define-constant ERR-TOO-MANY-POOLS (err u2004)) (define-constant ERR-PERCENT_GREATER_THAN_ONE (err u5000)) (define-constant ERR-ALREADY-EXPIRED (err u2011)) -(define-constant ERR-MATH-CALL (err u2010)) (define-constant ERR-EXCEEDS-MAX-SLIPPAGE (err u2020)) (define-constant ERR-PRICE-LOWER-THAN-MIN (err u2021)) (define-constant ERR-PRICE-GREATER-THAN-MAX (err u2022)) diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 6e60eedb..08a07033 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -8,23 +8,15 @@ (define-constant MAX_T u85000000) (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-X (err u2005)) -(define-constant ERR-NO-FEE-Y (err u2006)) (define-constant ERR-INVALID-EXPIRY (err u2009)) -(define-constant fixed-point-err (err 5014)) -(define-constant ERR-MATH-CALL (err 2010)) -(define-constant ERR-GET-EXPIRY-FAIL-ERR (err u2013)) (define-constant ERR-DY-BIGGER-THAN-AVAILABLE (err u2016)) (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)) (define-constant ERR-INVALID-POOL-TOKEN (err u2023)) (define-constant ERR-ORACLE-NOT-ENABLED (err u7002)) From f84ac09d18a165c031f84ff63e4a295fb0669f5a Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Wed, 17 Nov 2021 15:15:36 +0500 Subject: [PATCH 20/25] get-decimals now implements the trait with uint as parameter --- clarity/contracts/key-token/key-usda-wbtc.clar | 4 ++-- clarity/contracts/key-token/key-wbtc-usda.clar | 4 ++-- clarity/contracts/key-token/key-wbtc-wbtc.clar | 4 ++-- clarity/contracts/pool-token/ytp-yield-usda.clar | 4 ++-- clarity/contracts/pool-token/ytp-yield-wbtc.clar | 4 ++-- clarity/contracts/traits/trait-semi-fungible-token.clar | 2 +- clarity/contracts/yield-token/yield-usda.clar | 4 ++-- clarity/contracts/yield-token/yield-wbtc.clar | 4 ++-- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/clarity/contracts/key-token/key-usda-wbtc.clar b/clarity/contracts/key-token/key-usda-wbtc.clar index 1acf64fc..3b9a515d 100644 --- a/clarity/contracts/key-token/key-usda-wbtc.clar +++ b/clarity/contracts/key-token/key-usda-wbtc.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply key-usda-wbtc)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) diff --git a/clarity/contracts/key-token/key-wbtc-usda.clar b/clarity/contracts/key-token/key-wbtc-usda.clar index 2b69e992..4ae2103e 100644 --- a/clarity/contracts/key-token/key-wbtc-usda.clar +++ b/clarity/contracts/key-token/key-wbtc-usda.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply key-wbtc-usda)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) diff --git a/clarity/contracts/key-token/key-wbtc-wbtc.clar b/clarity/contracts/key-token/key-wbtc-wbtc.clar index eaa9d657..5ec1faba 100644 --- a/clarity/contracts/key-token/key-wbtc-wbtc.clar +++ b/clarity/contracts/key-token/key-wbtc-wbtc.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply key-wbtc-wbtc)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) diff --git a/clarity/contracts/pool-token/ytp-yield-usda.clar b/clarity/contracts/pool-token/ytp-yield-usda.clar index 9bfc355f..70fe9997 100644 --- a/clarity/contracts/pool-token/ytp-yield-usda.clar +++ b/clarity/contracts/pool-token/ytp-yield-usda.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply ytp-yield-usda)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) diff --git a/clarity/contracts/pool-token/ytp-yield-wbtc.clar b/clarity/contracts/pool-token/ytp-yield-wbtc.clar index 5dddcd1f..9f20c8aa 100644 --- a/clarity/contracts/pool-token/ytp-yield-wbtc.clar +++ b/clarity/contracts/pool-token/ytp-yield-wbtc.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply ytp-yield-wbtc)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) diff --git a/clarity/contracts/traits/trait-semi-fungible-token.clar b/clarity/contracts/traits/trait-semi-fungible-token.clar index b0abd549..9710357a 100644 --- a/clarity/contracts/traits/trait-semi-fungible-token.clar +++ b/clarity/contracts/traits/trait-semi-fungible-token.clar @@ -15,7 +15,7 @@ (get-overall-supply () (response uint uint)) ;; Get the number of decimal places of a token type. - (get-decimals () (response uint uint)) + (get-decimals (uint) (response uint uint)) ;; Get an optional token URI that represents metadata for a specific token. (get-token-uri (uint) (response (optional (string-utf8 256)) uint)) diff --git a/clarity/contracts/yield-token/yield-usda.clar b/clarity/contracts/yield-token/yield-usda.clar index 648b0d9c..971f6828 100644 --- a/clarity/contracts/yield-token/yield-usda.clar +++ b/clarity/contracts/yield-token/yield-usda.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply yield-usda)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) diff --git a/clarity/contracts/yield-token/yield-wbtc.clar b/clarity/contracts/yield-token/yield-wbtc.clar index d56314f5..93a83ac6 100644 --- a/clarity/contracts/yield-token/yield-wbtc.clar +++ b/clarity/contracts/yield-token/yield-wbtc.clar @@ -60,7 +60,7 @@ (ok (ft-get-supply yield-wbtc)) ) -(define-read-only (get-decimals) +(define-read-only (get-decimals (token-id uint)) (ok u8) ) @@ -123,7 +123,7 @@ (define-constant ONE_8 (pow u10 u8)) (define-private (pow-decimals) - (pow u10 (unwrap-panic (get-decimals))) + (pow u10 (unwrap-panic (get-decimals u0))) ) (define-read-only (fixed-to-decimals (amount uint)) From cedbe749f6f20878d5bd5ec02da0f6a096ff7a4c Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:30:37 +0800 Subject: [PATCH 21/25] initial coding of alex-futures-pool done. test cases to be added --- clarity/Clarinet.toml | 6 +++--- clarity/contracts/pool/alex-reserve-pool.clar | 4 ++++ clarity/contracts/pool/stacking-pool.clar | 9 ++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index 756df7ed..799fb14c 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -14,9 +14,9 @@ depends_on = ["trait-sip-010", "trait-flash-loan-user"] path = "contracts/pool/alex-reserve-pool.clar" depends_on = ["trait-ownable", "trait-sip-010", "alex-vault"] -[contracts.stacking-pool] -path = "contracts/pool/stacking-pool.clar" -depends_on = ["trait-ownable", "trait-sip-010", "trait-semi-fungible-token", "trait-multisig-vote"] +[contracts.alex-futures-pool] +path = "contracts/pool/alex-futures-pool.clar" +depends_on = ["trait-ownable", "token-alex", "trait-semi-fungible-token", "trait-multisig-vote", "alex-reserve-pool"] [contracts.alex-vault] path = "contracts/alex-vault.clar" diff --git a/clarity/contracts/pool/alex-reserve-pool.clar b/clarity/contracts/pool/alex-reserve-pool.clar index 7d48268c..fd93ad1e 100644 --- a/clarity/contracts/pool/alex-reserve-pool.clar +++ b/clarity/contracts/pool/alex-reserve-pool.clar @@ -133,6 +133,10 @@ uint ) +(define-read-only (get-reward-cycle-length) + (var-get reward-cycle-length) +) + (define-read-only (is-token-approved (token principal)) (is-some (map-get? approved-tokens token)) ) diff --git a/clarity/contracts/pool/stacking-pool.clar b/clarity/contracts/pool/stacking-pool.clar index 7cfaaacb..9904870d 100644 --- a/clarity/contracts/pool/stacking-pool.clar +++ b/clarity/contracts/pool/stacking-pool.clar @@ -17,8 +17,6 @@ (define-constant ERR-STACKING-IN-PROGRESS (err u2018)) (define-constant ERR-STACKING-NOT-AVAILABLE (err u2027)) -(define-constant BLOCK-PER-CYCLE u2100) - (define-data-var CONTRACT-OWNER principal tx-sender) (define-read-only (get-owner) @@ -86,7 +84,7 @@ (define-private (get-first-stacks-block-in-reward-cycle (token principal) (reward-cycle uint)) (contract-call? .alex-reserve-pool get-first-stacks-block-in-reward-cycle token reward-cycle) ) -(define-private (claim-stacking-reward (token-trait ) (reward-cycle uint)) +(define-private (claim-stacking-reward (reward-cycle uint) (token-trait )) (as-contract (contract-call? .alex-reserve-pool claim-staking-reward token-trait reward-cycle)) ) @@ -192,10 +190,11 @@ (portioned-rewards (mul-down total-rewards shares-to-supply)) ) - (asserts! (> block-height (+ (get-first-stacks-block-in-reward-cycle poxl-token (+ start-cycle u32)) BLOCK-PER-CYCLE)) ERR-STACKING-IN-PROGRESS) + (asserts! (> block-height (+ (get-first-stacks-block-in-reward-cycle poxl-token (+ start-cycle u32)) (contract-call? .alex-reserve-pool get-reward-cycle-length))) ERR-STACKING-IN-PROGRESS) ;; the first call claims rewards - (map claim-stacking-reward poxl-token-trait reward-cycles) + ;; TODO: how can we pass trait to fold? + (map claim-stacking-reward reward-cycles poxl-token-trait) (try! (contract-call? poxl-token-trait transfer-fixed shares (as-contract tx-sender) tx-sender none)) (try! (contract-call? reward-token-trait transfer-fixed portioned-rewards (as-contract tx-sender) tx-sender none)) From faf197c49dfa2151a5512d1ce03b4ddb1ffe6519 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:48:20 +0800 Subject: [PATCH 22/25] minor updates --- clarity/tests/alex-reserve-pool_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clarity/tests/alex-reserve-pool_test.ts b/clarity/tests/alex-reserve-pool_test.ts index ff7e5474..d9fdb5cf 100644 --- a/clarity/tests/alex-reserve-pool_test.ts +++ b/clarity/tests/alex-reserve-pool_test.ts @@ -5,7 +5,7 @@ import { it } from "./token-alex-src/testutil.ts"; const ONE_8 = 1e8; const token = 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.token-alex'; -describe("[ALEX STAKING]", () => { +describe("STAKING :", () => { ////////////////////////////////////////////////// // REGISTRATION From a25cb84049296a540e30f722c3f58f42559a300e Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Thu, 18 Nov 2021 13:22:04 +0800 Subject: [PATCH 23/25] alex-futures-pool added --- clarity/contracts/pool/alex-futures-pool.clar | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 clarity/contracts/pool/alex-futures-pool.clar diff --git a/clarity/contracts/pool/alex-futures-pool.clar b/clarity/contracts/pool/alex-futures-pool.clar new file mode 100644 index 00000000..7f3246d2 --- /dev/null +++ b/clarity/contracts/pool/alex-futures-pool.clar @@ -0,0 +1,195 @@ +(impl-trait .trait-ownable.ownable-trait) +(use-trait sft-trait .trait-semi-fungible-token.semi-fungible-token-trait) +(use-trait multisig-trait .trait-multisig-vote.multisig-vote-trait) + +;; alex-futures-pool +;; ideally we want to make this a "generic" pool, but there is an issue with iterating over trait (claim-staking-rewards) +;; + +;; constants +;; +(define-constant ONE_8 u100000000) ;; 8 decimal places + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-INVALID-POOL (err u2001)) +(define-constant ERR-POOL-ALREADY-EXISTS (err u2000)) +(define-constant ERR-TOO-MANY-POOLS (err u2004)) +(define-constant ERR-staking-IN-PROGRESS (err u2018)) +(define-constant ERR-staking-NOT-AVAILABLE (err u2027)) + +(define-data-var CONTRACT-OWNER principal tx-sender) + +(define-constant REWARD-CYCLE-INDEXES (list u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 u16 u17 u18 u19 u20 u21 u22 u23 u24 u25 u26 u27 u28 u29 u30 u31)) + +(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)) + ) +) + +;; data maps and vars +;; +;; pools-map = pool-id, start-cycle +(define-map pools-map uint uint) + +(define-map pools-data-map + uint + { + total-supply: uint, + fee-to-address: principal, + pool-token: principal, + reward-cycles: (list 32 uint) + } +) + +(define-data-var pool-count uint u0) +(define-data-var pools-list (list 2000 uint) (list)) + +;; private functions +;; +(define-private (sum-staking-reward (reward-cycle uint) (sum-so-far uint)) + (+ sum-so-far (get-staking-reward reward-cycle)) +) +(define-private (get-staking-reward (reward-cycle uint)) + (contract-call? .alex-reserve-pool get-staking-reward .token-alex (get-user-id) reward-cycle) +) +(define-private (register-user) + (as-contract (contract-call? .alex-reserve-pool register-user .token-alex none)) +) +(define-private (get-user-id) + (default-to u0 (contract-call? .alex-reserve-pool get-user-id .token-alex (as-contract tx-sender))) +) +(define-private (get-reward-cycle (stack-height uint)) + (contract-call? .alex-reserve-pool get-reward-cycle .token-alex stack-height) +) +(define-private (stake-tokens (amount-tokens uint) (lock-period uint)) + (as-contract (contract-call? .alex-reserve-pool stake-tokens .token-alex amount-tokens lock-period)) +) +(define-private (get-first-stacks-block-in-reward-cycle (reward-cycle uint)) + (contract-call? .alex-reserve-pool get-first-stacks-block-in-reward-cycle .token-alex reward-cycle) +) +(define-private (claim-staking-reward (reward-cycle uint)) + (as-contract (contract-call? .alex-reserve-pool claim-staking-reward .token-alex reward-cycle)) +) + +;; public functions +;; +(define-read-only (get-pool-count) + (ok (var-get pool-count)) +) + +(define-read-only (get-pool-contracts (pool-id uint)) + (ok (unwrap! (map-get? pools-map pool-id) ERR-INVALID-POOL)) +) + +(define-read-only (get-pools) + (ok (map get-pool-contracts (var-get pools-list))) +) + +(define-read-only (get-pool-details (start-cycle uint)) + (ok (unwrap! (map-get? pools-data-map start-cycle) ERR-INVALID-POOL)) +) + +(define-read-only (get-balance (start-cycle uint)) + (ok (get total-supply (unwrap! (map-get? pools-data-map start-cycle) ERR-INVALID-POOL))) +) + +(define-public (create-pool (reward-cycles (list 32 uint)) (futures-token ) (multisig )) + (let + ( + (pool-id (+ (var-get pool-count) u1)) + (pool-data { + total-supply: u0, + fee-to-address: (contract-of multisig), + pool-token: (contract-of futures-token), + reward-cycles: reward-cycles + }) + (start-cycle (default-to u0 (element-at reward-cycles u0))) + ) + (asserts! (is-eq contract-caller (var-get CONTRACT-OWNER)) ERR-NOT-AUTHORIZED) + + ;; register if not registered + (try! (register-user)) + + (asserts! (is-none (map-get? pools-data-map start-cycle)) ERR-POOL-ALREADY-EXISTS) + + (map-set pools-map pool-id start-cycle) + (map-set pools-data-map start-cycle pool-data) + + (var-set pools-list (unwrap! (as-max-len? (append (var-get pools-list) pool-id) u2000) ERR-TOO-MANY-POOLS)) + (var-set pool-count pool-id) + (print { object: "pool", action: "created", pool-data: pool-data }) + (ok true) + ) +) + +(define-public (add-to-position (start-cycle uint) (futures-token ) (dx uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map start-cycle) ERR-INVALID-POOL)) + (total-supply (get total-supply pool)) + (pool-updated (merge pool { + total-supply: (+ dx total-supply) + })) + (current-cycle (unwrap! (get-reward-cycle block-height) ERR-staking-NOT-AVAILABLE)) + ) + ;; check if staking already started + (asserts! (> start-cycle current-cycle) ERR-staking-IN-PROGRESS) + + ;; transfer dx to contract and send to stake + (try! (contract-call? .token-alex transfer-fixed dx tx-sender (as-contract tx-sender) none)) + (try! (stake-tokens dx u32)) + + ;; mint pool token and send to tx-sender + (map-set pools-data-map start-cycle pool-updated) + (try! (contract-call? futures-token mint-fixed start-cycle dx tx-sender)) + (print { object: "pool", action: "liquidity-added", data: pool-updated }) + (ok true) + ) +) + +(define-public (reduce-position (start-cycle uint) (futures-token ) (percent uint)) + (let + ( + (pool (unwrap! (map-get? pools-data-map start-cycle) ERR-INVALID-POOL)) + (shares (mul-down (unwrap-panic (contract-call? futures-token get-balance-fixed start-cycle tx-sender)) percent)) + (total-supply (get total-supply pool)) + (pool-updated (merge pool { + total-supply: (- total-supply shares) + }) + ) + (reward-cycles (get reward-cycles pool)) + (shares-to-supply (div-down shares total-supply)) + (total-rewards (fold sum-staking-reward reward-cycles u0)) + (portioned-rewards (mul-down total-rewards shares-to-supply)) + ) + + (asserts! (> block-height (+ (get-first-stacks-block-in-reward-cycle (+ start-cycle u32)) (contract-call? .alex-reserve-pool get-reward-cycle-length))) ERR-staking-IN-PROGRESS) + + ;; the first call claims rewards + (map claim-staking-reward reward-cycles) + + (try! (contract-call? .token-alex transfer-fixed (+ shares portioned-rewards) (as-contract tx-sender) tx-sender none)) + + (map-set pools-data-map start-cycle pool-updated) + (try! (contract-call? futures-token burn-fixed start-cycle shares tx-sender)) + (print { object: "pool", action: "liquidity-removed", data: pool-updated }) + (ok {staked: shares, rewards: portioned-rewards}) + ) +) + +(define-read-only (mul-down (a uint) (b uint)) + (/ (* a b) ONE_8) +) + +(define-read-only (div-down (a uint) (b uint)) + (if (is-eq a u0) + u0 + (/ (* a ONE_8) b) + ) +) \ No newline at end of file From fed0591153eee917cfcdfd52a286c66de49839c5 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Thu, 18 Nov 2021 15:16:20 +0800 Subject: [PATCH 24/25] merged with feat/stacked-poxl-pool --- clarity/tests/alex-reserve-pool_test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/clarity/tests/alex-reserve-pool_test.ts b/clarity/tests/alex-reserve-pool_test.ts index 7697acd4..b8433955 100644 --- a/clarity/tests/alex-reserve-pool_test.ts +++ b/clarity/tests/alex-reserve-pool_test.ts @@ -341,7 +341,6 @@ describe("STAKING :", () => { // assert receipt.result.expectOk().expectBool(true); - console.log(amountTokens); assertEquals(receipt.events.length, 2); receipt.events.expectFungibleTokenTransferEvent( amountTokens, From 9ee5a9b125f91f239970c9b0c78c5a0140978a2e Mon Sep 17 00:00:00 2001 From: SaadTahirTintash Date: Thu, 18 Nov 2021 15:47:01 +0500 Subject: [PATCH 25/25] Removed unused code from contracts --- clarity/contracts/alex-vault.clar | 18 +++- .../equations/weighted-equation.clar | 67 ++------------- .../equations/yield-token-equation.clar | 84 +++---------------- clarity/contracts/lib/math-fixed-point.clar | 7 -- .../pool/collateral-rebalancing-pool.clar | 66 +++------------ clarity/contracts/pool/fixed-weight-pool.clar | 56 +++---------- .../pool/liquidity-bootstrapping-pool.clar | 56 +++---------- clarity/contracts/pool/yield-token-pool.clar | 52 +++--------- 8 files changed, 81 insertions(+), 325 deletions(-) diff --git a/clarity/contracts/alex-vault.clar b/clarity/contracts/alex-vault.clar index 4ad6a247..6aa40ebb 100644 --- a/clarity/contracts/alex-vault.clar +++ b/clarity/contracts/alex-vault.clar @@ -83,7 +83,7 @@ ( (pre-bal (unwrap! (get-balance token) ERR-INVALID-FLASH-LOAN)) (fee-with-principal (+ ONE_8 (var-get flash-loan-fee-rate))) - (amount-with-fee (contract-call? .math-fixed-point mul-up amount fee-with-principal)) + (amount-with-fee (mul-up amount fee-with-principal)) (recipient tx-sender) ) @@ -110,6 +110,22 @@ ) ) +(define-read-only (mul-down (a uint) (b uint)) + (/ (* a b) ONE_8) +) + +(define-read-only (mul-up (a uint) (b uint)) + (let + ( + (product (* a b)) + ) + (if (is-eq product u0) + u0 + (+ u1 (/ (- product u1) ONE_8)) + ) + ) +) + ;; contract initialisation (begin (map-set approved-contracts .alex-reserve-pool true) diff --git a/clarity/contracts/equations/weighted-equation.clar b/clarity/contracts/equations/weighted-equation.clar index d4bfb98e..6f6dbd9c 100644 --- a/clarity/contracts/equations/weighted-equation.clar +++ b/clarity/contracts/equations/weighted-equation.clar @@ -286,16 +286,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error ;; (i.e. the last digit of the result may be completely lost to this error). @@ -304,18 +294,6 @@ ;; public functions ;; -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - (define-read-only (mul-down (a uint) (b uint)) (/ (* a b) ONE_8) ) @@ -415,11 +393,10 @@ {x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) )) -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) +(define-constant ERR_X_OUT_OF_BOUNDS (err u5009)) +(define-constant ERR_Y_OUT_OF_BOUNDS (err u5010)) +(define-constant ERR_PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant ERR_INVALID_EXPONENT (err u5012)) ;; private functions ;; @@ -484,14 +461,14 @@ (lnx (unwrap-panic (ln-priv x-int))) (logx-times-y (/ (* lnx y-int) iONE_8)) ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) ERR_PRODUCT_OUT_OF_BOUNDS) (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) (define-private (exp-pos (x int)) (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (let ( ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct @@ -552,10 +529,10 @@ (define-read-only (pow-fixed (x uint) (y uint)) (begin ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + (asserts! (< x (pow u2 u127)) ERR_X_OUT_OF_BOUNDS) ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + (asserts! (< y MILD_EXPONENT_BOUND) ERR_Y_OUT_OF_BOUNDS) (if (is-eq y u0) (ok (to-uint iONE_8)) @@ -571,7 +548,7 @@ ;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. (define-read-only (exp-fixed (x int)) (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (if (< x 0) ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). @@ -580,30 +557,4 @@ (exp-pos x) ) ) -) - -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - -;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. -(define-read-only (ln-fixed (a int)) - (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) - (if (< a iONE_8) - ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). - ;; If a is less than one, 1/a will be greater than one. - ;; Fixed point division requires multiplying by iONE_8. - (ok (- 0 (unwrap-panic (ln-priv (/ (* iONE_8 iONE_8) a))))) - (ln-priv a) - ) - ) ) \ No newline at end of file diff --git a/clarity/contracts/equations/yield-token-equation.clar b/clarity/contracts/equations/yield-token-equation.clar index fe3b7819..14d617d4 100644 --- a/clarity/contracts/equations/yield-token-equation.clar +++ b/clarity/contracts/equations/yield-token-equation.clar @@ -313,16 +313,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error ;; (i.e. the last digit of the result may be completely lost to this error). @@ -331,18 +321,6 @@ ;; public functions ;; -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - (define-read-only (mul-down (a uint) (b uint)) (/ (* a b) ONE_8) ) @@ -367,13 +345,6 @@ ) ) -(define-read-only (div-up (a uint) (b uint)) - (if (is-eq a u0) - u0 - (+ u1 (/ (- (* a ONE_8) u1) b)) - ) -) - (define-read-only (pow-down (a uint) (b uint)) (let ( @@ -442,11 +413,11 @@ {x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) )) -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) +(define-constant ERR_X_OUT_OF_BOUNDS (err u5009)) +(define-constant ERR_Y_OUT_OF_BOUNDS (err u5010)) +(define-constant ERR_PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant ERR_INVALID_EXPONENT (err u5012)) +(define-constant ERR_OUT_OF_BOUNDS (err u5013)) ;; private functions ;; @@ -511,14 +482,14 @@ (lnx (unwrap-panic (ln-priv x-int))) (logx-times-y (/ (* lnx y-int) iONE_8)) ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) ERR_PRODUCT_OUT_OF_BOUNDS) (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) (define-private (exp-pos (x int)) (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (let ( ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct @@ -579,10 +550,10 @@ (define-read-only (pow-fixed (x uint) (y uint)) (begin ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + (asserts! (< x (pow u2 u127)) ERR_X_OUT_OF_BOUNDS) ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + (asserts! (< y MILD_EXPONENT_BOUND) ERR_Y_OUT_OF_BOUNDS) (if (is-eq y u0) (ok (to-uint iONE_8)) @@ -598,7 +569,7 @@ ;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. (define-read-only (exp-fixed (x int)) (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (if (< x 0) ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). @@ -609,22 +580,10 @@ ) ) -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - ;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. (define-read-only (ln-fixed (a int)) (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (asserts! (> a 0) ERR_OUT_OF_BOUNDS) (if (< a iONE_8) ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). ;; If a is less than one, 1/a will be greater than one. @@ -634,24 +593,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/contracts/lib/math-fixed-point.clar b/clarity/contracts/lib/math-fixed-point.clar index 90bd18c9..ebb1f264 100644 --- a/clarity/contracts/lib/math-fixed-point.clar +++ b/clarity/contracts/lib/math-fixed-point.clar @@ -6,13 +6,6 @@ ;; constants ;; (define-constant ONE_8 (pow u10 u8)) ;; 8 decimal places -(define-constant ERR-SCALE-UP-OVERFLOW (err u5001)) -(define-constant ERR-SCALE-DOWN-OVERFLOW (err u5002)) -(define-constant ERR-ADD-OVERFLOW (err u5003)) -(define-constant ERR-SUB-OVERFLOW (err u5004)) -(define-constant ERR-MUL-OVERFLOW (err u5005)) -(define-constant ERR-DIV-OVERFLOW (err u5006)) -(define-constant ERR-POW-OVERFLOW (err u5007)) ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error diff --git a/clarity/contracts/pool/collateral-rebalancing-pool.clar b/clarity/contracts/pool/collateral-rebalancing-pool.clar index e8e817a8..65963ccc 100644 --- a/clarity/contracts/pool/collateral-rebalancing-pool.clar +++ b/clarity/contracts/pool/collateral-rebalancing-pool.clar @@ -974,16 +974,6 @@ ;; TODO: overflow causes runtime error, should handle before operation rather than after -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error ;; (i.e. the last digit of the result may be completely lost to this error). @@ -992,18 +982,6 @@ ;; public functions ;; -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - (define-read-only (mul-down (a uint) (b uint)) (/ (* a b) ONE_8) ) @@ -1048,16 +1026,6 @@ ) ) -(define-read-only (pow-up (a uint) (b uint)) - (let - ( - (raw (unwrap-panic (pow-fixed a b))) - (max-error (+ u1 (mul-up raw MAX_POW_RELATIVE_ERROR))) - ) - (+ raw max-error) - ) -) - ;; math-log-exp ;; Exponentiation and logarithm functions for 8 decimal fixed point numbers (both base and exponent/argument). ;; Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural @@ -1103,11 +1071,11 @@ {x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) )) -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) +(define-constant ERR_X_OUT_OF_BOUNDS (err u5009)) +(define-constant ERR_Y_OUT_OF_BOUNDS (err u5010)) +(define-constant ERR_PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant ERR_INVALID_EXPONENT (err u5012)) +(define-constant ERR_OUT_OF_BOUNDS (err u5013)) ;; private functions ;; @@ -1172,14 +1140,14 @@ (lnx (unwrap-panic (ln-priv x-int))) (logx-times-y (/ (* lnx y-int) iONE_8)) ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) ERR_PRODUCT_OUT_OF_BOUNDS) (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) (define-private (exp-pos (x int)) (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (let ( ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct @@ -1240,10 +1208,10 @@ (define-read-only (pow-fixed (x uint) (y uint)) (begin ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + (asserts! (< x (pow u2 u127)) ERR_X_OUT_OF_BOUNDS) ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + (asserts! (< y MILD_EXPONENT_BOUND) ERR_Y_OUT_OF_BOUNDS) (if (is-eq y u0) (ok (to-uint iONE_8)) @@ -1259,7 +1227,7 @@ ;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. (define-read-only (exp-fixed (x int)) (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (if (< x 0) ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). @@ -1270,22 +1238,10 @@ ) ) -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - ;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. (define-read-only (ln-fixed (a int)) (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (asserts! (> a 0) ERR_OUT_OF_BOUNDS) (if (< a iONE_8) ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). ;; If a is less than one, 1/a will be greater than one. diff --git a/clarity/contracts/pool/fixed-weight-pool.clar b/clarity/contracts/pool/fixed-weight-pool.clar index c387701e..15121136 100644 --- a/clarity/contracts/pool/fixed-weight-pool.clar +++ b/clarity/contracts/pool/fixed-weight-pool.clar @@ -794,16 +794,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error ;; (i.e. the last digit of the result may be completely lost to this error). @@ -812,18 +802,6 @@ ;; public functions ;; -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - (define-read-only (mul-down (a uint) (b uint)) (/ (* a b) ONE_8) ) @@ -923,11 +901,11 @@ {x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) )) -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) +(define-constant ERR_X_OUT_OF_BOUNDS (err u5009)) +(define-constant ERR_Y_OUT_OF_BOUNDS (err u5010)) +(define-constant ERR_PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant ERR_INVALID_EXPONENT (err u5012)) +(define-constant ERR_OUT_OF_BOUNDS (err u5013)) ;; private functions ;; @@ -992,14 +970,14 @@ (lnx (unwrap-panic (ln-priv x-int))) (logx-times-y (/ (* lnx y-int) iONE_8)) ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) ERR_PRODUCT_OUT_OF_BOUNDS) (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) (define-private (exp-pos (x int)) (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (let ( ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct @@ -1060,10 +1038,10 @@ (define-read-only (pow-fixed (x uint) (y uint)) (begin ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + (asserts! (< x (pow u2 u127)) ERR_X_OUT_OF_BOUNDS) ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + (asserts! (< y MILD_EXPONENT_BOUND) ERR_Y_OUT_OF_BOUNDS) (if (is-eq y u0) (ok (to-uint iONE_8)) @@ -1079,7 +1057,7 @@ ;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. (define-read-only (exp-fixed (x int)) (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (if (< x 0) ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). @@ -1090,22 +1068,10 @@ ) ) -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - ;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. (define-read-only (ln-fixed (a int)) (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (asserts! (> a 0) ERR_OUT_OF_BOUNDS) (if (< a iONE_8) ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). ;; If a is less than one, 1/a will be greater than one. diff --git a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar index c2075648..43984beb 100644 --- a/clarity/contracts/pool/liquidity-bootstrapping-pool.clar +++ b/clarity/contracts/pool/liquidity-bootstrapping-pool.clar @@ -562,16 +562,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error ;; (i.e. the last digit of the result may be completely lost to this error). @@ -580,18 +570,6 @@ ;; public functions ;; -(define-read-only (get_one) - (ok ONE_8) -) - -(define-read-only (scale-up (a uint)) - (* a ONE_8) -) - -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - (define-read-only (mul-down (a uint) (b uint)) (/ (* a b) ONE_8) ) @@ -691,11 +669,11 @@ {x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) )) -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) +(define-constant ERR_X_OUT_OF_BOUNDS (err u5009)) +(define-constant ERR_Y_OUT_OF_BOUNDS (err u5010)) +(define-constant ERR_PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant ERR_INVALID_EXPONENT (err u5012)) +(define-constant ERR_OUT_OF_BOUNDS (err u5013)) ;; private functions ;; @@ -760,14 +738,14 @@ (lnx (unwrap-panic (ln-priv x-int))) (logx-times-y (/ (* lnx y-int) iONE_8)) ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) ERR_PRODUCT_OUT_OF_BOUNDS) (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) (define-private (exp-pos (x int)) (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (let ( ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct @@ -828,10 +806,10 @@ (define-read-only (pow-fixed (x uint) (y uint)) (begin ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + (asserts! (< x (pow u2 u127)) ERR_X_OUT_OF_BOUNDS) ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + (asserts! (< y MILD_EXPONENT_BOUND) ERR_Y_OUT_OF_BOUNDS) (if (is-eq y u0) (ok (to-uint iONE_8)) @@ -847,7 +825,7 @@ ;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. (define-read-only (exp-fixed (x int)) (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (if (< x 0) ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). @@ -858,22 +836,10 @@ ) ) -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - ;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. (define-read-only (ln-fixed (a int)) (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (asserts! (> a 0) ERR_OUT_OF_BOUNDS) (if (< a iONE_8) ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). ;; If a is less than one, 1/a will be greater than one. diff --git a/clarity/contracts/pool/yield-token-pool.clar b/clarity/contracts/pool/yield-token-pool.clar index 08a07033..fbfe3fb1 100644 --- a/clarity/contracts/pool/yield-token-pool.clar +++ b/clarity/contracts/pool/yield-token-pool.clar @@ -835,16 +835,6 @@ ;; Fixed Point Math ;; following https://github.com/balancer-labs/balancer-monorepo/blob/master/pkg/solidity-utils/contracts/math/FixedPoint.sol -;; constants -;; -(define-constant SCALE_UP_OVERFLOW (err u5001)) -(define-constant SCALE_DOWN_OVERFLOW (err u5002)) -(define-constant ADD_OVERFLOW (err u5003)) -(define-constant SUB_OVERFLOW (err u5004)) -(define-constant MUL_OVERFLOW (err u5005)) -(define-constant DIV_OVERFLOW (err u5006)) -(define-constant POW_OVERFLOW (err u5007)) - ;; With 8 fixed digits you would have a maximum error of 0.5 * 10^-8 in each entry, ;; which could aggregate to about 8 x 0.5 * 10^-8 = 4 * 10^-8 relative error ;; (i.e. the last digit of the result may be completely lost to this error). @@ -853,18 +843,10 @@ ;; public functions ;; -(define-read-only (get_one) - (ok ONE_8) -) - (define-read-only (scale-up (a uint)) (* a ONE_8) ) -(define-read-only (scale-down (a uint)) - (/ a ONE_8) -) - (define-read-only (mul-down (a uint) (b uint)) (/ (* a b) ONE_8) ) @@ -964,11 +946,11 @@ {x_pre: 6250000, a_pre: 106449446, use_deci: true} ;; x11 = 2^-4, a11 = e^x(11) )) -(define-constant X_OUT_OF_BOUNDS (err u5009)) -(define-constant Y_OUT_OF_BOUNDS (err u5010)) -(define-constant PRODUCT_OUT_OF_BOUNDS (err u5011)) -(define-constant INVALID_EXPONENT (err u5012)) -(define-constant OUT_OF_BOUNDS (err u5013)) +(define-constant ERR_X_OUT_OF_BOUNDS (err u5009)) +(define-constant ERR_Y_OUT_OF_BOUNDS (err u5010)) +(define-constant ERR_PRODUCT_OUT_OF_BOUNDS (err u5011)) +(define-constant ERR_INVALID_EXPONENT (err u5012)) +(define-constant ERR_OUT_OF_BOUNDS (err u5013)) ;; private functions ;; @@ -1033,14 +1015,14 @@ (lnx (unwrap-panic (ln-priv x-int))) (logx-times-y (/ (* lnx y-int) iONE_8)) ) - (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) PRODUCT_OUT_OF_BOUNDS) + (asserts! (and (<= MIN_NATURAL_EXPONENT logx-times-y) (<= logx-times-y MAX_NATURAL_EXPONENT)) ERR_PRODUCT_OUT_OF_BOUNDS) (ok (to-uint (unwrap-panic (exp-fixed logx-times-y)))) ) ) (define-private (exp-pos (x int)) (begin - (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= 0 x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (let ( ;; For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct @@ -1101,10 +1083,10 @@ (define-read-only (pow-fixed (x uint) (y uint)) (begin ;; The ln function takes a signed value, so we need to make sure x fits in the signed 128 bit range. - (asserts! (< x (pow u2 u127)) X_OUT_OF_BOUNDS) + (asserts! (< x (pow u2 u127)) ERR_X_OUT_OF_BOUNDS) ;; This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 128 bit range. - (asserts! (< y MILD_EXPONENT_BOUND) Y_OUT_OF_BOUNDS) + (asserts! (< y MILD_EXPONENT_BOUND) ERR_Y_OUT_OF_BOUNDS) (if (is-eq y u0) (ok (to-uint iONE_8)) @@ -1120,7 +1102,7 @@ ;; Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`. (define-read-only (exp-fixed (x int)) (begin - (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) (err INVALID_EXPONENT)) + (asserts! (and (<= MIN_NATURAL_EXPONENT x) (<= x MAX_NATURAL_EXPONENT)) ERR_INVALID_EXPONENT) (if (< x 0) ;; We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it ;; fits in the signed 128 bit range (as it is larger than MIN_NATURAL_EXPONENT). @@ -1131,22 +1113,10 @@ ) ) -;; Logarithm (log(arg, base), with signed 8 decimal fixed point base and argument. -(define-read-only (log-fixed (arg int) (base int)) - ;; This performs a simple base change: log(arg, base) = ln(arg) / ln(base). - (let - ( - (logBase (* (unwrap-panic (ln-priv base)) iONE_8)) - (logArg (* (unwrap-panic (ln-priv arg)) iONE_8)) - ) - (ok (/ (* logArg iONE_8) logBase)) - ) -) - ;; Natural logarithm (ln(a)) with signed 8 decimal fixed point argument. (define-read-only (ln-fixed (a int)) (begin - (asserts! (> a 0) (err OUT_OF_BOUNDS)) + (asserts! (> a 0) ERR_OUT_OF_BOUNDS) (if (< a iONE_8) ;; Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). ;; If a is less than one, 1/a will be greater than one.