From d76c9e6dbb904a37ce210a493ba3ce4f8d07cf33 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sun, 21 Apr 2024 16:00:30 +0800 Subject: [PATCH 01/12] fix: remove duplicate conversion --- .../auto-token/auto-alex-v3-1-endpoint.clar | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 150a4c39..e0df4f7c 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -102,9 +102,6 @@ (define-read-only (get-shares-to-tokens (dx uint)) (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) -(define-read-only (get-tokens-to-shares (dx uint)) - (contract-call? .auto-alex-v3-1 get-tokens-to-shares dx)) - ;; governance calls (define-public (set-contract-owner (owner principal)) @@ -145,7 +142,6 @@ (define-public (add-to-position (dx uint)) (let ( (current-cycle (try! (rebase))) - (new-supply (get-tokens-to-shares dx)) (sender tx-sender)) (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) @@ -155,8 +151,8 @@ (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: dx }))) ;; mint pool token and send to tx-sender - (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed new-supply sender))) - (print { notification: "position-added", payload: { new-supply: new-supply } }) + (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed dx sender))) + (print { notification: "position-added", payload: { new-supply: dx } }) (rebase))) (define-public (upgrade (dx uint)) @@ -164,7 +160,6 @@ (end-cycle-v2 (contract-call? .auto-alex-v2 get-end-cycle)) (current-cycle (try! (rebase))) (intrinsic-dx (mul-down dx (try! (contract-call? .auto-alex-v2 get-intrinsic)))) - (new-supply (get-tokens-to-shares intrinsic-dx)) (sender tx-sender)) (asserts! (> intrinsic-dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) @@ -175,8 +170,8 @@ (and (< end-cycle-v2 current-cycle) (begin (as-contract (try! (reduce-position-v2))) true)) ;; mint pool token and send to tx-sender - (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed new-supply sender))) - (print { notification: "upgrade-position-added", payload: { new-supply: new-supply } }) + (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed intrinsic-dx sender))) + (print { notification: "upgrade-position-added", payload: { new-supply: intrinsic-dx } }) (rebase))) ;; claims alex for the reward-cycles and mint auto-alex-v3 From ae1eb4cc8b698e131893f03990ec328f70f2df5d Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Sun, 21 Apr 2024 17:38:38 +0800 Subject: [PATCH 02/12] chore: get-end-cycle-v2 --- clarity/Clarinet.toml | 14 +++++++------- .../auto-token/auto-alex-v3-1-endpoint.clar | 3 +-- .../wrapped-token/token-ssl-wsbtc-08JSD.clar | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index 87df7bc6..f1ba9c0c 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -50,8 +50,8 @@ contract_id = "SP3951VNPC55BMS9RCF6SKRZP4K3Q2PQ2RSM1DD1V.fast" contract_id = "SPPK49DG7WR1J5D50GZ4W7DYYWM5MAXSX0ZA9VEJ.FrodoSaylorKeanuPepe10Inu-token-v69" [[project.requirements]] contract_id = "SP3WPNAEBYMX06RQNNYTH5PTJ1FRGX5A13ZZMZ01D.dogwifhat-token" -# [[project.requirements]] -# contract_id = "SP3R4MHK99JAAM0N3T0CBZRZVSVQFG1N75DQ08JSD.Wsbtc" +[[project.requirements]] +contract_id = "SP3R4MHK99JAAM0N3T0CBZRZVSVQFG1N75DQ08JSD.Wsbtc" [[project.requirements]] contract_id = "SP3SJSNPJXXHY7RJM48ZT6G1BXYZADAC5YAXZA3ZC.ODINToken" [[project.requirements]] @@ -1596,11 +1596,11 @@ epoch = 2.4 path = "contracts/bridge/bridged-tokens/token-ssko.clar" depends_on = ["trait-ownable", "trait-sip-010"] -# [contracts.token-ssl-wsbtc-08JSD] -# path = "contracts/wrapped-token/token-ssl-wsbtc-08JSD.clar" -# depends_on = [ ] -# clarity_version = 2 -# epoch = 2.4 +[contracts.token-ssl-wsbtc-08JSD] +path = "contracts/wrapped-token/token-ssl-wsbtc-08JSD.clar" +depends_on = [ ] +clarity_version = 2 +epoch = 2.4 [contracts.token-ssl-odin-ZA3ZC] path = "contracts/wrapped-token/token-ssl-odin-ZA3ZC.clar" diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index e0df4f7c..6e56f2d0 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -98,7 +98,6 @@ (define-read-only (get-intrinsic) (contract-call? .auto-alex-v3-1 get-shares-to-tokens ONE_8)) -;; @dev need to save the historical ratio. (define-read-only (get-shares-to-tokens (dx uint)) (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) @@ -157,7 +156,7 @@ (define-public (upgrade (dx uint)) (let ( - (end-cycle-v2 (contract-call? .auto-alex-v2 get-end-cycle)) + (end-cycle-v2 (get-end-cycle-v2)) (current-cycle (try! (rebase))) (intrinsic-dx (mul-down dx (try! (contract-call? .auto-alex-v2 get-intrinsic)))) (sender tx-sender)) diff --git a/clarity/contracts/wrapped-token/token-ssl-wsbtc-08JSD.clar b/clarity/contracts/wrapped-token/token-ssl-wsbtc-08JSD.clar index a8054cc7..84c72e8b 100644 --- a/clarity/contracts/wrapped-token/token-ssl-wsbtc-08JSD.clar +++ b/clarity/contracts/wrapped-token/token-ssl-wsbtc-08JSD.clar @@ -4,7 +4,7 @@ (define-fungible-token wsbtc-08JSD) (define-data-var token-name (string-ascii 32) "wsbtc-08JSD (SSL)") -(define-data-var token-symbol (string-ascii 10) "wsbtc-08JSD") +(define-data-var token-symbol (string-ascii 10) "wsbtc") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/token-ssl-wsbtc-08JSD.json")) (define-data-var token-decimals uint u8) From 9090b8f19d8871bee027e825d537828aa31c93d0 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Wed, 1 May 2024 21:05:44 +0800 Subject: [PATCH 03/12] working --- .../auto-token/auto-alex-v3-1-endpoint.clar | 25 ++++++++++--------- .../auto-token/auto-alex-v3-1-registry.clar | 13 ++-------- .../auto-token/auto-alex-v3-1-wrapped.clar | 22 ++++++++-------- .../contracts/auto-token/auto-alex-v3-1.clar | 18 ++++--------- 4 files changed, 31 insertions(+), 47 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 6e56f2d0..9520574b 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -62,9 +62,6 @@ (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry get-redeem-shares-per-cycle-or-default reward-cycle)) -(define-read-only (get-redeem-tokens-per-cycle-or-default (reward-cycle uint)) - (contract-call? .auto-alex-v3-1-registry get-redeem-tokens-per-cycle-or-default reward-cycle)) - (define-read-only (get-redeem-request-or-fail (request-id uint)) (contract-call? .auto-alex-v3-1-registry get-redeem-request-or-fail request-id)) @@ -99,7 +96,7 @@ (contract-call? .auto-alex-v3-1 get-shares-to-tokens ONE_8)) (define-read-only (get-shares-to-tokens (dx uint)) - (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) + (contract-call? .auto-alex-v3-1-wrapped get-shares-to-tokens dx)) ;; governance calls @@ -198,17 +195,18 @@ (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle-shares-to-tokens reward-cycle (get-shares-to-tokens ONE_8)))) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) (print { notification: "claim-and-stake", payload: { redeeming: redeeming }}) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-tokens-per-cycle reward-cycle redeeming))) (ok true))) (define-public (request-redeem (amount uint)) (let ( (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) (redeem-cycle (+ current-cycle redeem-delay-cycles)) - (request-details { requested-by: tx-sender, shares: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) + (shares (contract-call? .auto-alex-v3-1-wrapped get-tokens-to-shares amount)) + (request-details { requested-by: tx-sender, amount: amount, shares: shares, redeem-cycle: redeem-cycle, status: (get-pending) })) (asserts! (not (is-redeem-paused)) ERR-PAUSED) - (try! (contract-call? .auto-alex-v3-1 transfer-fixed amount tx-sender .auto-alex-v3-1 none)) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (+ (get-redeem-shares-per-cycle-or-default redeem-cycle) amount)))) + (try! (contract-call? .auto-alex-v3-1-wrapped mint-fixed amount tx-sender)) + (try! (contract-call? .auto-alex-v3-1-wrapped transfer-fixed shares tx-sender .auto-alex-v3-1 none)) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (+ (get-redeem-shares-per-cycle-or-default redeem-cycle) shares)))) (print { notification: "redeem-request", payload: request-details }) (as-contract (contract-call? .auto-alex-v3-1-registry set-redeem-request u0 request-details)))) @@ -218,13 +216,15 @@ (redeem-cycle (get redeem-cycle request-details)) (check-claim-and-stake (and (not (is-cycle-staked redeem-cycle)) (try! (claim-and-stake redeem-cycle)))) (current-cycle (try! (rebase))) - (redeem-tokens (div-down (mul-down (get shares request-details) (get-redeem-tokens-per-cycle-or-default redeem-cycle)) (get-redeem-shares-per-cycle-or-default redeem-cycle))) + (tokens (get-shares-to-tokens (get shares request-details))) (updated-request-details (merge request-details { status: (get-finalized) }))) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) - (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .age000-governance-token redeem-tokens (get requested-by request-details)))) - (as-contract (try! (contract-call? .auto-alex-v3-1 burn-fixed (get shares request-details) .auto-alex-v3-1))) + (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1-wrapped (get shares request-details) tx-sender))) + (as-contract (try! (contract-call? .auto-alex-v3-1-wrapped burn-fixed (get shares request-details) tx-sender))) + (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .age000-governance-token tokens (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-1 burn-fixed tokens tx-sender))) (print { notification: "finalize-redeem", payload: updated-request-details }) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details))) (rebase))) @@ -237,7 +237,8 @@ (asserts! (is-eq tx-sender (get requested-by request-details)) ERR-NOT-AUTHORIZED) (asserts! (not (is-cycle-staked redeem-cycle)) ERR-REWARD-CYCLE-NOT-COMPLETED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) - (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1 (get shares request-details) (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1-wrapped (get shares request-details) (get requested-by request-details)))) + (try! (contract-call? .auto-alex-v3-1-wrapped burn-fixed (get shares request-details) tx-sender)) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (- (get-redeem-shares-per-cycle-or-default redeem-cycle) (get shares request-details))))) (print { notification: "revoke-redeem", payload: updated-request-details }) (as-contract (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details)))) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar index 15bc5089..ed674c15 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar @@ -21,9 +21,8 @@ (define-map staked-cycle uint bool) (define-map staked-cycle-shares-to-token uint uint) -(define-map redeem-requests uint { requested-by: principal, shares: uint, redeem-cycle: uint, status: (buff 1) }) +(define-map redeem-requests uint { requested-by: principal, amount: uint, shares: uint, redeem-cycle: uint, status: (buff 1) }) (define-map redeem-shares-per-cycle uint uint) -(define-map redeem-tokens-per-cycle uint uint) ;; read-only calls @@ -51,9 +50,6 @@ (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (default-to u0 (map-get? redeem-shares-per-cycle reward-cycle))) -(define-read-only (get-redeem-tokens-per-cycle-or-default (reward-cycle uint)) - (default-to u0 (map-get? redeem-tokens-per-cycle reward-cycle))) - (define-read-only (get-redeem-request-or-fail (request-id uint)) (ok (unwrap! (map-get? redeem-requests request-id) ERR-REQUEST-ID-NOT-FOUND))) @@ -90,7 +86,7 @@ (asserts! (is-cycle-staked cycle) ERR-CYCLE-NOT-STAKED) (ok (map-set staked-cycle-shares-to-token cycle shares-to-tokens)))) -(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, shares: uint, redeem-cycle: uint, status: (buff 1) })) +(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, amount: uint, shares: uint, redeem-cycle: uint, status: (buff 1) })) (let ( (next-nonce (+ (var-get redeem-request-nonce) u1)) (id (if (> request-id u0) request-id (begin (var-set redeem-request-nonce next-nonce) next-nonce)))) @@ -103,11 +99,6 @@ (try! (check-is-approved)) (ok (map-set redeem-shares-per-cycle reward-cycle shares)))) -(define-public (set-redeem-tokens-per-cycle (reward-cycle uint) (tokens uint)) - (begin - (try! (check-is-approved)) - (ok (map-set redeem-tokens-per-cycle reward-cycle tokens)))) - ;; private functions ;; diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar b/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar index 51970b13..081149ec 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar @@ -3,6 +3,7 @@ (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ONE_8 u100000000) +(define-constant token-decimals u8) (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) @@ -11,14 +12,12 @@ (define-data-var token-symbol (string-ascii 10) "watALEXv3") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3-wrapped.json")) -(define-data-var token-decimals uint u8) - ;; governance functions (define-public (set-contract-owner (owner principal)) - (begin - (try! (check-is-owner)) - (ok (var-set contract-owner owner)))) + (begin + (try! (check-is-owner)) + (ok (var-set contract-owner owner)))) (define-public (set-approved-contract (owner principal) (approved bool)) (begin @@ -35,11 +34,6 @@ (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) -(define-public (set-decimals (new-decimals uint)) - (begin - (try! (check-is-owner)) - (ok (var-set token-decimals new-decimals)))) - (define-public (set-token-uri (new-uri (optional (string-utf8 256)))) (begin (try! (check-is-owner)) @@ -90,14 +84,20 @@ (ok (var-get token-uri))) (define-read-only (get-decimals) - (ok (var-get token-decimals))) + (ok token-decimals)) (define-read-only (get-balance (who principal)) (ok (ft-get-balance auto-alex-v3-wrapped who))) +(define-read-only (get-balance-fixed (who principal)) + (get-balance who)) + (define-read-only (get-total-supply) (ok (ft-get-supply auto-alex-v3-wrapped))) +(define-read-only (get-total-supply-fixed) + (get-total-supply)) + (define-read-only (get-share (who principal)) (ok (get-shares-to-tokens (unwrap-panic (get-balance who))))) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1.clar b/clarity/contracts/auto-token/auto-alex-v3-1.clar index 793c35ae..e3f977a4 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1.clar @@ -5,6 +5,8 @@ (define-constant ONE_8 u100000000) +(define-constant token-decimals u8) + (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) @@ -12,8 +14,6 @@ (define-data-var token-symbol (string-ascii 10) "atALEXv3") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3.json")) -(define-data-var token-decimals uint u8) - (define-data-var reserve uint u0) ;; governance functions @@ -38,11 +38,6 @@ (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) -(define-public (set-decimals (new-decimals uint)) - (begin - (try! (check-is-owner)) - (ok (var-set token-decimals new-decimals)))) - (define-public (set-token-uri (new-uri (optional (string-utf8 256)))) (begin (try! (check-is-owner)) @@ -102,7 +97,7 @@ (ok (var-get token-uri))) (define-read-only (get-decimals) - (ok (var-get token-decimals))) + (ok token-decimals)) (define-read-only (get-balance (who principal)) (ok (get-shares-to-tokens (unwrap-panic (get-share who))))) @@ -131,9 +126,6 @@ (define-read-only (get-reserve) (ok (var-get reserve))) -(define-read-only (get-reserve-fixed) - (get-reserve)) - (define-read-only (get-tokens-to-shares (amount uint)) (if (is-eq (get-reserve) (ok u0)) amount @@ -167,10 +159,10 @@ (match prior ok-value result err-value (err err-value))) (define-private (check-is-owner) - (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) (define-private (check-is-approved) - (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) + (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) ;; staking related fuctions From 8a376409aa7a2f4728c80a2dfffcff3bc3ded6ee Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 11:24:40 +0800 Subject: [PATCH 04/12] working --- .../auto-token/auto-alex-v3-1-endpoint.clar | 14 +++----------- clarity/tests/auto-alex-v3_test.ts | 16 ++++++++++------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 3aab9411..0808745b 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -121,9 +121,9 @@ (let ( (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) (start-cycle (get-start-cycle)) - (check-start-cycle (asserts! (<= start-cycle current-cycle) ERR-NOT-ACTIVATED)) - (auto-alex-v2-bal (unwrap-panic (contract-call? .auto-alex-v2 get-balance-fixed .auto-alex-v3-1))) - (auto-alex-v3-wrapped-bal (unwrap-panic (contract-call? .auto-alex-v3-1-wrapped get-balance-fixed .auto-alex-v3-1)))) + (check-start-cycle (asserts! (<= start-cycle current-cycle) ERR-NOT-ACTIVATED))) + ;; (auto-alex-v2-bal (unwrap-panic (contract-call? .auto-alex-v2 get-balance-fixed .auto-alex-v3-1))) + ;; (auto-alex-v3-wrapped-bal (unwrap-panic (contract-call? .auto-alex-v3-1-wrapped get-balance-fixed .auto-alex-v3-1)))) (and (> current-cycle start-cycle) (not (is-cycle-staked (- current-cycle u1))) (try! (claim-and-stake (- current-cycle u1)))) ;; (print { notification: "next-base", payload: { ;; ;; amount-staked: (get amount-staked (as-contract (get-staker-at-cycle (+ current-cycle u1)))), @@ -178,12 +178,8 @@ (sender tx-sender)) (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) - - ;; transfer dx to contract to stake for max cycles (try! (contract-call? .age000-governance-token transfer-fixed dx sender .auto-alex-v3-1 none)) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: dx }))) - - ;; mint pool token and send to tx-sender (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed dx sender))) (print { notification: "position-added", payload: { new-supply: dx } }) (rebase))) @@ -197,12 +193,8 @@ (asserts! (> intrinsic-dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) (asserts! (< end-cycle-v2 (+ current-cycle u32)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly - - ;; transfer dx to contract to stake for max cycles (try! (contract-call? .auto-alex-v2 transfer-fixed dx sender .auto-alex-v3-1 none)) (and (< end-cycle-v2 current-cycle) (begin (as-contract (try! (reduce-position-v2))) true)) - - ;; mint pool token and send to tx-sender (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed intrinsic-dx sender))) (print { notification: "upgrade-position-added", payload: { new-supply: intrinsic-dx } }) (rebase))) diff --git a/clarity/tests/auto-alex-v3_test.ts b/clarity/tests/auto-alex-v3_test.ts index 542e0a8e..0d1eca27 100644 --- a/clarity/tests/auto-alex-v3_test.ts +++ b/clarity/tests/auto-alex-v3_test.ts @@ -33,6 +33,8 @@ Clarinet.test({ result.expectOk(); result = alexToken.mintFixed(deployer, wallet_3.address, dx); result.expectOk(); + result = alexToken.mintFixed(deployer, wallet_4.address, dx); + result.expectOk(); let block = chain.mineBlock([ Tx.contractCall( @@ -50,11 +52,11 @@ Clarinet.test({ reservePool.setCoinbaseAmount( deployer, alexTokenAddress, - 0, - 0, - 0, - 0, - 0 + 1e8, + 1e8, + 1e8, + 1e8, + 1e8 ), yieldVault.setStartCycle(deployer, 0), yieldVault.setBountyInFixed(deployer, 1), @@ -123,7 +125,7 @@ Clarinet.test({ block = chain.mineBlock([ Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'revoke-redeem', [types.uint(3)], wallet_3.address), + Tx.contractCall('auto-alex-v3-1-endpoint', 'revoke-redeem', [types.uint(3)], wallet_3.address), ]); console.log(block.receipts[0].events); block.receipts[0].result.expectOk(); @@ -131,9 +133,11 @@ Clarinet.test({ console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-1')], wallet_1.address)); console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(2)], wallet_1.address).result); + console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(3)], wallet_1.address).result); console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-intrinsic', [], wallet_1.address)); block = chain.mineBlock([ + Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(3)], wallet_3.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); block.receipts.forEach(e => { e.result.expectOk() }); From 492b7fa590faef83f0eeb2895d4bf2457964ac76 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 12:09:00 +0800 Subject: [PATCH 05/12] working --- .../auto-token/auto-alex-v3-1-endpoint.clar | 12 ++++++------ .../auto-token/auto-alex-v3-1-registry.clar | 6 ++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 0808745b..12f068a9 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -53,8 +53,8 @@ (define-read-only (is-cycle-staked (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry is-cycle-staked reward-cycle)) -(define-read-only (get-staked-cycle-shares-to-tokens-or-fail (reward-cycle uint)) - (contract-call? .auto-alex-v3-1-registry get-staked-cycle-shares-to-tokens-or-fail reward-cycle)) +(define-read-only (get-staked-cycle-shares-to-tokens-or-default (reward-cycle uint)) + (contract-call? .auto-alex-v3-1-registry get-staked-cycle-shares-to-tokens-or-default reward-cycle)) (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry get-redeem-shares-per-cycle-or-default reward-cycle)) @@ -151,13 +151,13 @@ (claimed (as-contract (try! (claim-staking-reward reward-cycle)))) (claimed-v2 (if (< end-cycle-v2 current-cycle) (as-contract (try! (reduce-position-v2))) (begin (try! (claim-and-stake-v2 reward-cycle)) u0))) (tokens (+ (get to-return claimed) (get entitled-token claimed) claimed-v2)) - (previous-shares-to-tokens (try! (get-staked-cycle-shares-to-tokens-or-fail (- reward-cycle u1)))) + (previous-shares-to-tokens (get-staked-cycle-shares-to-tokens-or-default reward-cycle)) (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) - (intrinsic (get-intrinsic))) + (intrinsic (get-shares-to-tokens ONE_8))) (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) (asserts! (>= tokens redeeming) ERR-REDEEM-IMBALANCE) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle reward-cycle true))) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle-shares-to-tokens reward-cycle intrinsic))) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle-shares-to-tokens (+ reward-cycle u1) intrinsic))) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) (print { notification: "claim-and-stake", payload: { redeeming: redeeming, tokens: tokens, intrinsic: intrinsic }}) (ok true))) @@ -302,7 +302,7 @@ (contract-call? .auto-alex-v3-1 reduce-position-v2)) (define-private (get-shares-to-tokens (dx uint)) - (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) + (contract-call? .auto-alex-v3-1-wrapped get-shares-to-tokens dx)) (define-private (claim-and-stake-v2 (reward-cycle uint)) (contract-call? .auto-alex-v2 claim-and-stake reward-cycle)) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar index ed674c15..9e1ddc8f 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar @@ -4,7 +4,6 @@ ;; (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-REQUEST-ID-NOT-FOUND (err u10019)) -(define-constant ERR-CYCLE-NOT-STAKED (err u20020)) (define-constant PENDING 0x00) (define-constant FINALIZED 0x01) @@ -44,8 +43,8 @@ (define-read-only (is-cycle-staked (reward-cycle uint)) (is-some (map-get? staked-cycle reward-cycle))) -(define-read-only (get-staked-cycle-shares-to-tokens-or-fail (reward-cycle uint)) - (ok (unwrap! (map-get? staked-cycle-shares-to-token reward-cycle) ERR-CYCLE-NOT-STAKED))) +(define-read-only (get-staked-cycle-shares-to-tokens-or-default (reward-cycle uint)) + (default-to u0 (map-get? staked-cycle-shares-to-token reward-cycle))) (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (default-to u0 (map-get? redeem-shares-per-cycle reward-cycle))) @@ -83,7 +82,6 @@ (define-public (set-staked-cycle-shares-to-tokens (cycle uint) (shares-to-tokens uint)) (begin (try! (check-is-approved)) - (asserts! (is-cycle-staked cycle) ERR-CYCLE-NOT-STAKED) (ok (map-set staked-cycle-shares-to-token cycle shares-to-tokens)))) (define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, amount: uint, shares: uint, redeem-cycle: uint, status: (buff 1) })) From 0d86457dc84e37886980b63bf698f6d2a8072cce Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 13:13:18 +0800 Subject: [PATCH 06/12] working --- .../contracts/auto-token/auto-alex-v3-1-endpoint.clar | 9 +++++---- clarity/tests/auto-alex-v3_test.ts | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 12f068a9..c639ff09 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -151,11 +151,12 @@ (claimed (as-contract (try! (claim-staking-reward reward-cycle)))) (claimed-v2 (if (< end-cycle-v2 current-cycle) (as-contract (try! (reduce-position-v2))) (begin (try! (claim-and-stake-v2 reward-cycle)) u0))) (tokens (+ (get to-return claimed) (get entitled-token claimed) claimed-v2)) - (previous-shares-to-tokens (get-staked-cycle-shares-to-tokens-or-default reward-cycle)) - (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) - (intrinsic (get-shares-to-tokens ONE_8))) + ;; (previous-shares-to-tokens (get-staked-cycle-shares-to-tokens-or-default reward-cycle)) + ;; (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) + (intrinsic (get-shares-to-tokens ONE_8)) + (redeeming (mul-down intrinsic (get-redeem-shares-per-cycle-or-default reward-cycle)))) (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) - (asserts! (>= tokens redeeming) ERR-REDEEM-IMBALANCE) + (asserts! (>= tokens redeeming) (err redeeming));;ERR-REDEEM-IMBALANCE) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle reward-cycle true))) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle-shares-to-tokens (+ reward-cycle u1) intrinsic))) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) diff --git a/clarity/tests/auto-alex-v3_test.ts b/clarity/tests/auto-alex-v3_test.ts index 0d1eca27..42c38bbb 100644 --- a/clarity/tests/auto-alex-v3_test.ts +++ b/clarity/tests/auto-alex-v3_test.ts @@ -85,7 +85,8 @@ Clarinet.test({ } block = chain.mineBlock([ - yieldVault.addToPosition(wallet_1, dx), + // yieldVault.addToPosition(wallet_1, dx), + Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_2.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_3.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_4.address), @@ -97,7 +98,7 @@ Clarinet.test({ block = chain.mineBlock([ Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(1), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), + // Tx.contractCall('auto-alex-v3-1-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_2.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_3.address), From 36474e8d64363d2afa8a4311886b556c6d1ac909 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 15:36:10 +0800 Subject: [PATCH 07/12] no wrap of auto-alex-v3-1 upon redeem request --- .../auto-token/auto-alex-v3-1-endpoint.clar | 41 ++++++++----------- .../auto-token/auto-alex-v3-1-registry.clar | 15 ++++--- clarity/tests/auto-alex-v3_test.ts | 6 +-- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index c639ff09..c34c2449 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -53,8 +53,8 @@ (define-read-only (is-cycle-staked (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry is-cycle-staked reward-cycle)) -(define-read-only (get-staked-cycle-shares-to-tokens-or-default (reward-cycle uint)) - (contract-call? .auto-alex-v3-1-registry get-staked-cycle-shares-to-tokens-or-default reward-cycle)) +(define-read-only (get-shares-to-tokens-per-cycle-or-default (reward-cycle uint)) + (contract-call? .auto-alex-v3-1-registry get-shares-to-tokens-per-cycle-or-default reward-cycle)) (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry get-redeem-shares-per-cycle-or-default reward-cycle)) @@ -151,14 +151,13 @@ (claimed (as-contract (try! (claim-staking-reward reward-cycle)))) (claimed-v2 (if (< end-cycle-v2 current-cycle) (as-contract (try! (reduce-position-v2))) (begin (try! (claim-and-stake-v2 reward-cycle)) u0))) (tokens (+ (get to-return claimed) (get entitled-token claimed) claimed-v2)) - ;; (previous-shares-to-tokens (get-staked-cycle-shares-to-tokens-or-default reward-cycle)) - ;; (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) - (intrinsic (get-shares-to-tokens ONE_8)) - (redeeming (mul-down intrinsic (get-redeem-shares-per-cycle-or-default reward-cycle)))) + (previous-shares-to-tokens (get-shares-to-tokens-per-cycle-or-default (- reward-cycle u1))) + (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) + (intrinsic (get-shares-to-tokens ONE_8))) (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) (asserts! (>= tokens redeeming) (err redeeming));;ERR-REDEEM-IMBALANCE) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle reward-cycle true))) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle-shares-to-tokens (+ reward-cycle u1) intrinsic))) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-shares-to-tokens-per-cycle reward-cycle intrinsic))) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) (print { notification: "claim-and-stake", payload: { redeeming: redeeming, tokens: tokens, intrinsic: intrinsic }}) (ok true))) @@ -204,12 +203,10 @@ (let ( (current-cycle (try! (rebase))) (redeem-cycle (+ current-cycle redeem-delay-cycles)) - (shares (contract-call? .auto-alex-v3-1-wrapped get-tokens-to-shares amount)) - (request-details { requested-by: tx-sender, amount: amount, shares: shares, redeem-cycle: redeem-cycle, status: (get-pending) })) + (request-details { requested-by: tx-sender, amount: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) (asserts! (not (is-redeem-paused)) ERR-PAUSED) - (try! (contract-call? .auto-alex-v3-1-wrapped mint-fixed amount tx-sender)) - (try! (contract-call? .auto-alex-v3-1-wrapped transfer-fixed shares tx-sender .auto-alex-v3-1 none)) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (+ (get-redeem-shares-per-cycle-or-default redeem-cycle) shares)))) + (try! (contract-call? .auto-alex-v3-1 transfer-fixed amount tx-sender .auto-alex-v3-1 none)) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (+ (get-redeem-shares-per-cycle-or-default redeem-cycle) amount)))) (print { notification: "redeem-request", payload: request-details }) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request u0 request-details))) (rebase))) @@ -220,15 +217,13 @@ (redeem-cycle (get redeem-cycle request-details)) (check-claim-and-stake (and (not (is-cycle-staked redeem-cycle)) (try! (claim-and-stake redeem-cycle)))) (current-cycle (try! (rebase))) - (tokens (get-shares-to-tokens (get shares request-details))) + (tokens (mul-down (get-shares-to-tokens-per-cycle-or-default (- redeem-cycle u1)) (get amount request-details))) (updated-request-details (merge request-details { status: (get-finalized) }))) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) - - (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1-wrapped (get shares request-details) tx-sender))) - (as-contract (try! (contract-call? .auto-alex-v3-1-wrapped burn-fixed (get shares request-details) tx-sender))) + + (as-contract (try! (contract-call? .auto-alex-v3-1 burn-fixed (get amount request-details) .auto-alex-v3-1))) (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .age000-governance-token tokens (get requested-by request-details)))) - (as-contract (try! (contract-call? .auto-alex-v3-1 burn-fixed tokens tx-sender))) (print { notification: "finalize-redeem", payload: updated-request-details }) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details))) (rebase))) @@ -238,13 +233,13 @@ (request-details (try! (get-redeem-request-or-fail request-id))) (current-cycle (try! (rebase))) (redeem-cycle (get redeem-cycle request-details)) + (check-cycle (asserts! (> redeem-cycle current-cycle) ERR-NO-REDEEM-REVOKE)) + (tokens (mul-down (get-shares-to-tokens-per-cycle-or-default (- current-cycle u1)) (get amount request-details))) (updated-request-details (merge request-details { status: (get-revoked) }))) - (asserts! (is-eq tx-sender (get requested-by request-details)) ERR-NOT-AUTHORIZED) - (asserts! (> redeem-cycle current-cycle) ERR-NO-REDEEM-REVOKE) + (asserts! (is-eq tx-sender (get requested-by request-details)) ERR-NOT-AUTHORIZED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) - (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1-wrapped (get shares request-details) (get requested-by request-details)))) - (try! (contract-call? .auto-alex-v3-1-wrapped burn-fixed (get shares request-details) tx-sender)) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (- (get-redeem-shares-per-cycle-or-default redeem-cycle) (get shares request-details))))) + (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1 tokens (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (- (get-redeem-shares-per-cycle-or-default redeem-cycle) (get amount request-details))))) (print { notification: "revoke-redeem", payload: updated-request-details }) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details))) (rebase))) @@ -303,7 +298,7 @@ (contract-call? .auto-alex-v3-1 reduce-position-v2)) (define-private (get-shares-to-tokens (dx uint)) - (contract-call? .auto-alex-v3-1-wrapped get-shares-to-tokens dx)) + (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) (define-private (claim-and-stake-v2 (reward-cycle uint)) (contract-call? .auto-alex-v2 claim-and-stake reward-cycle)) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar index 9e1ddc8f..67219b66 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar @@ -19,9 +19,9 @@ (define-data-var redeem-request-nonce uint u0) (define-map staked-cycle uint bool) -(define-map staked-cycle-shares-to-token uint uint) -(define-map redeem-requests uint { requested-by: principal, amount: uint, shares: uint, redeem-cycle: uint, status: (buff 1) }) +(define-map redeem-requests uint { requested-by: principal, amount: uint, redeem-cycle: uint, status: (buff 1) }) (define-map redeem-shares-per-cycle uint uint) +(define-map shares-to-token-per-cycle uint uint) ;; read-only calls @@ -43,8 +43,8 @@ (define-read-only (is-cycle-staked (reward-cycle uint)) (is-some (map-get? staked-cycle reward-cycle))) -(define-read-only (get-staked-cycle-shares-to-tokens-or-default (reward-cycle uint)) - (default-to u0 (map-get? staked-cycle-shares-to-token reward-cycle))) +(define-read-only (get-shares-to-tokens-per-cycle-or-default (reward-cycle uint)) + (default-to u0 (map-get? shares-to-token-per-cycle reward-cycle))) (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (default-to u0 (map-get? redeem-shares-per-cycle reward-cycle))) @@ -68,7 +68,6 @@ (begin (try! (check-is-owner)) (map-set staked-cycle new-start-cycle true) - (map-set staked-cycle-shares-to-token new-start-cycle u0) (ok (var-set start-cycle new-start-cycle)))) ;; privileged functions @@ -79,12 +78,12 @@ (try! (check-is-approved)) (ok (map-set staked-cycle cycle staked)))) -(define-public (set-staked-cycle-shares-to-tokens (cycle uint) (shares-to-tokens uint)) +(define-public (set-shares-to-tokens-per-cycle (cycle uint) (shares-to-tokens uint)) (begin (try! (check-is-approved)) - (ok (map-set staked-cycle-shares-to-token cycle shares-to-tokens)))) + (ok (map-set shares-to-token-per-cycle cycle shares-to-tokens)))) -(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, amount: uint, shares: uint, redeem-cycle: uint, status: (buff 1) })) +(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, amount: uint, redeem-cycle: uint, status: (buff 1) })) (let ( (next-nonce (+ (var-get redeem-request-nonce) u1)) (id (if (> request-id u0) request-id (begin (var-set redeem-request-nonce next-nonce) next-nonce)))) diff --git a/clarity/tests/auto-alex-v3_test.ts b/clarity/tests/auto-alex-v3_test.ts index 42c38bbb..9e56c4cf 100644 --- a/clarity/tests/auto-alex-v3_test.ts +++ b/clarity/tests/auto-alex-v3_test.ts @@ -85,8 +85,8 @@ Clarinet.test({ } block = chain.mineBlock([ - // yieldVault.addToPosition(wallet_1, dx), - Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_1.address), + yieldVault.addToPosition(wallet_1, dx), + // Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_2.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_3.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_4.address), @@ -98,7 +98,7 @@ Clarinet.test({ block = chain.mineBlock([ Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(1), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), - // Tx.contractCall('auto-alex-v3-1-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), + Tx.contractCall('auto-alex-v3-1-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_2.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_3.address), From e2b6d0e45b72244ad2134b82926c7df8ea68459c Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 17:18:03 +0800 Subject: [PATCH 08/12] test updated --- .../auto-token/auto-alex-v3-1-endpoint.clar | 18 ++---------------- clarity/tests/auto-alex-v3_test.ts | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index c34c2449..573e9115 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -122,21 +122,7 @@ (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) (start-cycle (get-start-cycle)) (check-start-cycle (asserts! (<= start-cycle current-cycle) ERR-NOT-ACTIVATED))) - ;; (auto-alex-v2-bal (unwrap-panic (contract-call? .auto-alex-v2 get-balance-fixed .auto-alex-v3-1))) - ;; (auto-alex-v3-wrapped-bal (unwrap-panic (contract-call? .auto-alex-v3-1-wrapped get-balance-fixed .auto-alex-v3-1)))) (and (> current-cycle start-cycle) (not (is-cycle-staked (- current-cycle u1))) (try! (claim-and-stake (- current-cycle u1)))) - ;; (print { notification: "next-base", payload: { - ;; ;; amount-staked: (get amount-staked (as-contract (get-staker-at-cycle (+ current-cycle u1)))), - ;; ;; to-return: (get to-return (as-contract (get-staker-at-cycle current-cycle))), - ;; ;; staking-reward: (as-contract (get-staking-reward current-cycle)), - ;; ;; alex-balance: (unwrap-panic (contract-call? .age000-governance-token get-balance-fixed .auto-alex-v3-1)), - ;; ;; v2-balance: (if (is-eq auto-alex-v2-bal u0) u0 (mul-down auto-alex-v2-bal (try! (contract-call? .auto-alex-v2 get-intrinsic)))), - ;; ;; v2-balance: auto-alex-v2-bal, - ;; ;; v2-intrinsic: (try! (contract-call? .auto-alex-v2 get-intrinsic)) - ;; ;; v3-balance: (if (is-eq auto-alex-v3-wrapped-bal u0) u0 (contract-call? .auto-alex-v3-1-wrapped get-shares-to-tokens auto-alex-v3-wrapped-bal)) - ;; v3-balance: auto-alex-v3-wrapped-bal, - ;; v3-intrinsic: (contract-call? .auto-alex-v3-1-wrapped get-shares-to-tokens ONE_8) - ;; }}) (as-contract (try! (contract-call? .auto-alex-v3-1 set-reserve (try! (get-next-base))))) (ok current-cycle))) @@ -155,11 +141,11 @@ (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) (intrinsic (get-shares-to-tokens ONE_8))) (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) - (asserts! (>= tokens redeeming) (err redeeming));;ERR-REDEEM-IMBALANCE) + (asserts! (>= tokens redeeming) ERR-REDEEM-IMBALANCE) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle reward-cycle true))) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-shares-to-tokens-per-cycle reward-cycle intrinsic))) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) - (print { notification: "claim-and-stake", payload: { redeeming: redeeming, tokens: tokens, intrinsic: intrinsic }}) + (print { notification: "claim-and-stake", payload: { redeeming: redeeming, tokens: tokens }}) (ok true))) ;; claims alex for the reward-cycles and mint auto-alex-v3 diff --git a/clarity/tests/auto-alex-v3_test.ts b/clarity/tests/auto-alex-v3_test.ts index 9e56c4cf..42f8ddaf 100644 --- a/clarity/tests/auto-alex-v3_test.ts +++ b/clarity/tests/auto-alex-v3_test.ts @@ -105,7 +105,7 @@ Clarinet.test({ Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_4.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'revoke-redeem', [types.uint(4)], wallet_4.address), ]); - console.log(block.receipts[3].events); + // console.log(block.receipts[3].events); block.receipts.forEach(e => { e.result.expectOk() }); const redeem_cycle = end_cycle + 2; @@ -124,6 +124,10 @@ Clarinet.test({ chain.mineEmptyBlockUntil(ACTIVATION_BLOCK + (redeem_cycle + 1) * 525); + console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-1')], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-intrinsic', [], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-shares-to-tokens-per-cycle-or-default', [types.uint(redeem_cycle - 1)], wallet_1.address)); + block = chain.mineBlock([ Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'revoke-redeem', [types.uint(3)], wallet_3.address), @@ -132,15 +136,18 @@ Clarinet.test({ block.receipts[0].result.expectOk(); block.receipts[1].result.expectErr(10019); - console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-1')], wallet_1.address)); console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(2)], wallet_1.address).result); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(3)], wallet_1.address).result); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-intrinsic', [], wallet_1.address)); - + console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(3)], wallet_1.address).result); + console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-1')], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-intrinsic', [], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-shares-to-tokens-per-cycle-or-default', [types.uint(redeem_cycle)], wallet_1.address)); + block = chain.mineBlock([ Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(3)], wallet_3.address), Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); + console.log(block.receipts[0].events); + console.log(block.receipts[1].events); block.receipts.forEach(e => { e.result.expectOk() }); block = chain.mineBlock([ @@ -152,7 +159,6 @@ Clarinet.test({ block = chain.mineBlock([ Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); - console.log(block.receipts[0].events); block.receipts[0].result.expectErr(10020); }, }); \ No newline at end of file From 919c01e0b5922247e59c6b55a392b1ed02a9b0da Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 17:52:10 +0800 Subject: [PATCH 09/12] v3-2 --- clarity/Clarinet.toml | 16 +- .../auto-token/auto-alex-v3-1-endpoint.clar | 204 ++++-------- .../auto-token/auto-alex-v3-1-registry.clar | 65 ++-- .../auto-token/auto-alex-v3-1-wrapped.clar | 56 +--- .../contracts/auto-token/auto-alex-v3-1.clar | 73 +---- .../auto-token/auto-alex-v3-2-endpoint.clar | 300 ++++++++++++++++++ .../auto-token/auto-alex-v3-2-registry.clar | 106 +++++++ .../auto-token/auto-alex-v3-2-wrapped.clar | 125 ++++++++ .../contracts/auto-token/auto-alex-v3-2.clar | 195 ++++++++++++ clarity/tests/auto-alex-v3_test.ts | 64 ++-- 10 files changed, 867 insertions(+), 337 deletions(-) create mode 100644 clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar create mode 100644 clarity/contracts/auto-token/auto-alex-v3-2-registry.clar create mode 100644 clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar create mode 100644 clarity/contracts/auto-token/auto-alex-v3-2.clar diff --git a/clarity/Clarinet.toml b/clarity/Clarinet.toml index 7267cdc2..3f7c7c99 100644 --- a/clarity/Clarinet.toml +++ b/clarity/Clarinet.toml @@ -1451,20 +1451,20 @@ path = "contracts/pool/rebase-swap-pool.clar" clarity_version = 2 epoch = 2.4 -[contracts.auto-alex-v3-1] -path = "contracts/auto-token/auto-alex-v3-1.clar" +[contracts.auto-alex-v3-2] +path = "contracts/auto-token/auto-alex-v3-2.clar" depends_on = [ "trait-sip-010", "trait-ownable" ] -[contracts.auto-alex-v3-1-wrapped] -path = "contracts/auto-token/auto-alex-v3-1-wrapped.clar" +[contracts.auto-alex-v3-2-wrapped] +path = "contracts/auto-token/auto-alex-v3-2-wrapped.clar" depends_on = [ "trait-sip-010", "trait-ownable" ] -[contracts.auto-alex-v3-1-registry] -path = "contracts/auto-token/auto-alex-v3-1-registry.clar" +[contracts.auto-alex-v3-2-registry] +path = "contracts/auto-token/auto-alex-v3-2-registry.clar" depends_on = [ ] -[contracts.auto-alex-v3-1-endpoint] -path = "contracts/auto-token/auto-alex-v3-1-endpoint.clar" +[contracts.auto-alex-v3-2-endpoint] +path = "contracts/auto-token/auto-alex-v3-2-endpoint.clar" depends_on = [ "trait-sip-010", "age000-governance-token", "alex-reserve-pool" ] [contracts.stx20-bridge-endpoint-v1-01] diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 573e9115..92d43ccf 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -1,249 +1,189 @@ -;; -- autoALEX creation/staking/redemption - -;; constants -;; (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-INVALID-LIQUIDITY (err u2003)) (define-constant ERR-NOT-ACTIVATED (err u2043)) +(define-constant ERR-ACTIVATED (err u2044)) (define-constant ERR-PAUSED (err u2046)) +(define-constant ERR-INVALID-PERCENT (err u5000)) +(define-constant ERR-GET-BALANCE-FIXED-FAIL (err u6001)) +(define-constant ERR-USER-ID-NOT-FOUND (err u10003)) (define-constant ERR-STAKING-NOT-AVAILABLE (err u10015)) (define-constant ERR-REWARD-CYCLE-NOT-COMPLETED (err u10017)) (define-constant ERR-CLAIM-AND-STAKE (err u10018)) -(define-constant ERR-NO-REDEEM-REVOKE (err u10019)) +(define-constant ERR-REQUEST-ID-NOT-FOUND (err u10019)) (define-constant ERR-REQUEST-FINALIZED-OR-REVOKED (err u10020)) -(define-constant ERR-REDEEM-IMBALANCE (err u10021)) +(define-constant ERR-REDEMPTION-TOO-HIGH (err u10021)) (define-constant ERR-END-CYCLE-V2 (err u10022)) - - (define-constant ONE_8 u100000000) - (define-constant REWARD-CYCLE-INDEXES (list 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 u32)) - -;; data maps and vars -;; - (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) - (define-data-var create-paused bool true) (define-data-var redeem-paused bool true) - -;; __IF_MAINNET__ (define-constant redeem-delay-cycles u32) -;; (define-constant redeem-delay-cycles u2) -;; __ENDIF__ - -;; read-only calls - (define-read-only (get-contract-owner) (var-get contract-owner)) - (define-read-only (get-pending) (contract-call? .auto-alex-v3-1-registry get-pending)) - (define-read-only (get-finalized) (contract-call? .auto-alex-v3-1-registry get-finalized)) - (define-read-only (get-revoked) (contract-call? .auto-alex-v3-1-registry get-revoked)) - (define-read-only (get-start-cycle) (contract-call? .auto-alex-v3-1-registry get-start-cycle)) - (define-read-only (is-cycle-staked (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry is-cycle-staked reward-cycle)) - -(define-read-only (get-shares-to-tokens-per-cycle-or-default (reward-cycle uint)) - (contract-call? .auto-alex-v3-1-registry get-shares-to-tokens-per-cycle-or-default reward-cycle)) - +(define-read-only (get-staked-cycle-shares-to-tokens-or-fail (reward-cycle uint)) + (contract-call? .auto-alex-v3-1-registry get-staked-cycle-shares-to-tokens-or-fail reward-cycle)) (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (contract-call? .auto-alex-v3-1-registry get-redeem-shares-per-cycle-or-default reward-cycle)) - +(define-read-only (get-redeem-tokens-per-cycle-or-default (reward-cycle uint)) + (contract-call? .auto-alex-v3-1-registry get-redeem-tokens-per-cycle-or-default reward-cycle)) (define-read-only (get-redeem-request-or-fail (request-id uint)) (contract-call? .auto-alex-v3-1-registry get-redeem-request-or-fail request-id)) - (define-read-only (is-create-paused) (var-get create-paused)) - (define-read-only (is-redeem-paused) (var-get redeem-paused)) - -;; @desc get the next capital base of the vault -;; @desc next-base = principal to be staked at the next cycle -;; @desc + principal to be claimed at the next cycle and staked for the following cycle -;; @desc + reward to be claimed at the next cycle and staked for the following cycle -;; @desc + balance of ALEX in the contract -;; @desc + intrinsic of autoALEXv2 in the contract (define-read-only (get-next-base) (let ( (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) - (auto-alex-v2-bal (unwrap-panic (contract-call? .auto-alex-v2 get-balance-fixed .auto-alex-v3-1)))) + (auto-alex-v2-bal (unwrap! (contract-call? .auto-alex-v2 get-balance-fixed .auto-alex-v3-1) ERR-GET-BALANCE-FIXED-FAIL))) (asserts! (or (is-eq current-cycle (get-start-cycle)) (is-cycle-staked (- current-cycle u1))) ERR-CLAIM-AND-STAKE) (ok (+ (get amount-staked (as-contract (get-staker-at-cycle (+ current-cycle u1)))) (get to-return (as-contract (get-staker-at-cycle current-cycle))) (as-contract (get-staking-reward current-cycle)) - (unwrap-panic (contract-call? .age000-governance-token get-balance-fixed .auto-alex-v3-1)) + (unwrap! (contract-call? .age000-governance-token get-balance-fixed .auto-alex-v3-1) ERR-GET-BALANCE-FIXED-FAIL) (if (is-eq auto-alex-v2-bal u0) u0 (mul-down auto-alex-v2-bal (try! (contract-call? .auto-alex-v2 get-intrinsic)))))))) - -;; @desc get the intrinsic value of auto-alex-v3 -;; @desc intrinsic = next capital base of the vault / total supply of auto-alex-v3 (define-read-only (get-intrinsic) - (get-shares-to-tokens ONE_8)) - -;; governance calls - + (contract-call? .auto-alex-v3-1 get-shares-to-tokens ONE_8)) +(define-read-only (get-shares-to-tokens (dx uint)) + (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) +(define-read-only (get-tokens-to-shares (dx uint)) + (contract-call? .auto-alex-v3-1 get-tokens-to-shares dx)) (define-public (set-contract-owner (owner principal)) (begin (try! (check-is-owner)) (ok (var-set contract-owner owner)))) - (define-public (set-approved-contract (owner principal) (approved bool)) (begin (try! (check-is-owner)) (ok (map-set approved-contracts owner approved)))) - (define-public (pause-create (pause bool)) (begin (try! (check-is-owner)) (ok (var-set create-paused pause)))) - (define-public (pause-redeem (pause bool)) (begin (try! (check-is-owner)) (ok (var-set redeem-paused pause)))) - -;; public functions -;; - (define-public (rebase) (let ( (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) (start-cycle (get-start-cycle)) (check-start-cycle (asserts! (<= start-cycle current-cycle) ERR-NOT-ACTIVATED))) (and (> current-cycle start-cycle) (not (is-cycle-staked (- current-cycle u1))) (try! (claim-and-stake (- current-cycle u1)))) - (as-contract (try! (contract-call? .auto-alex-v3-1 set-reserve (try! (get-next-base))))) + (as-contract (try! (contract-call? .auto-alex-v3-1 set-reserve (try! (get-next-base))))) (ok current-cycle))) - -;; @desc triggers external event that claims all that's available and stake for another 32 cycles -;; @desc this can be triggered by anyone -;; @param reward-cycle the target cycle to claim (and stake for current cycle + 32 cycles). reward-cycle must be < current cycle. -(define-public (claim-and-stake (reward-cycle uint)) - (let ( - (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) - (end-cycle-v2 (get-end-cycle-v2)) - ;; claim all that's available to claim for the reward-cycle - (claimed (as-contract (try! (claim-staking-reward reward-cycle)))) - (claimed-v2 (if (< end-cycle-v2 current-cycle) (as-contract (try! (reduce-position-v2))) (begin (try! (claim-and-stake-v2 reward-cycle)) u0))) - (tokens (+ (get to-return claimed) (get entitled-token claimed) claimed-v2)) - (previous-shares-to-tokens (get-shares-to-tokens-per-cycle-or-default (- reward-cycle u1))) - (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) - (intrinsic (get-shares-to-tokens ONE_8))) - (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) - (asserts! (>= tokens redeeming) ERR-REDEEM-IMBALANCE) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle reward-cycle true))) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-shares-to-tokens-per-cycle reward-cycle intrinsic))) - (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) - (print { notification: "claim-and-stake", payload: { redeeming: redeeming, tokens: tokens }}) - (ok true))) - -;; claims alex for the reward-cycles and mint auto-alex-v3 -(define-public (claim-and-mint (reward-cycles (list 200 uint))) - (let ( - (claimed (unwrap-panic (contract-call? .staking-helper claim-staking-reward .age000-governance-token reward-cycles)))) - (try! (add-to-position (fold sum-claimed claimed u0))) - (ok claimed))) - -;; @desc add to position -;; @desc transfers dx to vault, stake them for 32 cycles and mints auto-alex-v3, the number of which is determined as % of total supply / next base -;; @param dx the number of $ALEX in 8-digit fixed point notation (define-public (add-to-position (dx uint)) (let ( (current-cycle (try! (rebase))) + (new-supply (get-tokens-to-shares dx)) (sender tx-sender)) (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) + ;; transfer dx to contract to stake for max cycles (try! (contract-call? .age000-governance-token transfer-fixed dx sender .auto-alex-v3-1 none)) (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: dx }))) - (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed dx sender))) - (print { notification: "position-added", payload: { new-supply: dx } }) + ;; mint pool token and send to tx-sender + (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed new-supply sender))) + (print { notification: "position-added", payload: { new-supply: new-supply } }) (rebase))) - (define-public (upgrade (dx uint)) (let ( - (end-cycle-v2 (get-end-cycle-v2)) + (end-cycle-v2 (contract-call? .auto-alex-v2 get-end-cycle)) (current-cycle (try! (rebase))) (intrinsic-dx (mul-down dx (try! (contract-call? .auto-alex-v2 get-intrinsic)))) + (new-supply (get-tokens-to-shares intrinsic-dx)) (sender tx-sender)) (asserts! (> intrinsic-dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) (asserts! (< end-cycle-v2 (+ current-cycle u32)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly + ;; transfer dx to contract to stake for max cycles (try! (contract-call? .auto-alex-v2 transfer-fixed dx sender .auto-alex-v3-1 none)) (and (< end-cycle-v2 current-cycle) (begin (as-contract (try! (reduce-position-v2))) true)) - (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed intrinsic-dx sender))) - (print { notification: "upgrade-position-added", payload: { new-supply: intrinsic-dx } }) + ;; mint pool token and send to tx-sender + (as-contract (try! (contract-call? .auto-alex-v3-1 mint-fixed new-supply sender))) + (print { notification: "upgrade-position-added", payload: { new-supply: new-supply } }) (rebase))) - +(define-public (claim-and-mint (reward-cycles (list 200 uint))) + (let ( + (claimed (unwrap-panic (contract-call? .staking-helper claim-staking-reward .age000-governance-token reward-cycles)))) + (try! (add-to-position (fold sum-claimed claimed u0))) + (ok claimed))) +(define-public (claim-and-stake (reward-cycle uint)) + (let ( + (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) + (end-cycle-v2 (get-end-cycle-v2)) + ;; claim all that's available to claim for the reward-cycle + (claimed (as-contract (try! (claim-staking-reward reward-cycle)))) + (claimed-v2 (if (< end-cycle-v2 current-cycle) (as-contract (try! (reduce-position-v2))) (begin (try! (claim-and-stake-v2 reward-cycle)) u0))) + (tokens (+ (get to-return claimed) (get entitled-token claimed) claimed-v2)) + (previous-shares-to-tokens (try! (get-staked-cycle-shares-to-tokens-or-fail (- reward-cycle u1)))) + (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle)))) + (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle reward-cycle true))) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-staked-cycle-shares-to-tokens reward-cycle (get-shares-to-tokens ONE_8)))) + (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) + (print { notification: "claim-and-stake", payload: { redeeming: redeeming }}) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-tokens-per-cycle reward-cycle redeeming))) + (ok true))) (define-public (request-redeem (amount uint)) (let ( - (current-cycle (try! (rebase))) + (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) (redeem-cycle (+ current-cycle redeem-delay-cycles)) - (request-details { requested-by: tx-sender, amount: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) + (request-details { requested-by: tx-sender, shares: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (try! (contract-call? .auto-alex-v3-1 transfer-fixed amount tx-sender .auto-alex-v3-1 none)) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (+ (get-redeem-shares-per-cycle-or-default redeem-cycle) amount)))) (print { notification: "redeem-request", payload: request-details }) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request u0 request-details))) - (rebase))) - + (as-contract (contract-call? .auto-alex-v3-1-registry set-redeem-request u0 request-details)))) (define-public (finalize-redeem (request-id uint)) (let ( (request-details (try! (get-redeem-request-or-fail request-id))) (redeem-cycle (get redeem-cycle request-details)) (check-claim-and-stake (and (not (is-cycle-staked redeem-cycle)) (try! (claim-and-stake redeem-cycle)))) (current-cycle (try! (rebase))) - (tokens (mul-down (get-shares-to-tokens-per-cycle-or-default (- redeem-cycle u1)) (get amount request-details))) + (redeem-tokens (div-down (mul-down (get shares request-details) (get-redeem-tokens-per-cycle-or-default redeem-cycle)) (get-redeem-shares-per-cycle-or-default redeem-cycle))) (updated-request-details (merge request-details { status: (get-finalized) }))) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) - - (as-contract (try! (contract-call? .auto-alex-v3-1 burn-fixed (get amount request-details) .auto-alex-v3-1))) - (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .age000-governance-token tokens (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .age000-governance-token redeem-tokens (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-1 burn-fixed (get shares request-details) .auto-alex-v3-1))) (print { notification: "finalize-redeem", payload: updated-request-details }) (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details))) (rebase))) - (define-public (revoke-redeem (request-id uint)) (let ( (request-details (try! (get-redeem-request-or-fail request-id))) - (current-cycle (try! (rebase))) (redeem-cycle (get redeem-cycle request-details)) - (check-cycle (asserts! (> redeem-cycle current-cycle) ERR-NO-REDEEM-REVOKE)) - (tokens (mul-down (get-shares-to-tokens-per-cycle-or-default (- current-cycle u1)) (get amount request-details))) - (updated-request-details (merge request-details { status: (get-revoked) }))) - (asserts! (is-eq tx-sender (get requested-by request-details)) ERR-NOT-AUTHORIZED) + (updated-request-details (merge request-details { status: (get-revoked) }))) + (asserts! (is-eq tx-sender (get requested-by request-details)) ERR-NOT-AUTHORIZED) + (asserts! (not (is-cycle-staked redeem-cycle)) ERR-REWARD-CYCLE-NOT-COMPLETED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) - (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1 tokens (get requested-by request-details)))) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (- (get-redeem-shares-per-cycle-or-default redeem-cycle) (get amount request-details))))) + (as-contract (try! (contract-call? .auto-alex-v3-1 transfer-token .auto-alex-v3-1 (get shares request-details) (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-shares-per-cycle redeem-cycle (- (get-redeem-shares-per-cycle-or-default redeem-cycle) (get shares request-details))))) (print { notification: "revoke-redeem", payload: updated-request-details }) - (as-contract (try! (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details))) - (rebase))) - -;; private functions -;; - + (as-contract (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details)))) (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) - (define-private (check-is-approved) (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) - (define-private (sum-claimed (claimed-response (response (tuple (entitled-token uint) (to-return uint)) uint)) (sum-so-far uint)) (match claimed-response claimed (+ sum-so-far (get to-return claimed) (get entitled-token claimed)) err sum-so-far)) - (define-private (stake-tokens-iter (cycles-to-stake uint) (previous-response (response { current-cycle: uint, remaining: uint } uint))) (match previous-response ok-value @@ -261,41 +201,25 @@ (and (> staking u0) (as-contract (try! (stake-tokens staking cycles-to-stake)))) (ok { current-cycle: (get current-cycle ok-value), remaining: (- (get remaining ok-value) staking) })) err-value previous-response)) - (define-private (get-reward-cycle (stack-height uint)) (contract-call? .alex-reserve-pool get-reward-cycle .age000-governance-token stack-height)) - (define-private (get-staking-reward (reward-cycle uint)) (contract-call? .alex-reserve-pool get-staking-reward .age000-governance-token (get-user-id) reward-cycle)) - (define-private (get-staker-at-cycle (reward-cycle uint)) (contract-call? .alex-reserve-pool get-staker-at-cycle-or-default .age000-governance-token reward-cycle (get-user-id))) - (define-private (get-user-id) (default-to u0 (contract-call? .alex-reserve-pool get-user-id .age000-governance-token .auto-alex-v3-1))) - (define-private (stake-tokens (amount-tokens uint) (lock-period uint)) (contract-call? .auto-alex-v3-1 stake-tokens amount-tokens lock-period)) - (define-private (claim-staking-reward (reward-cycle uint)) (contract-call? .auto-alex-v3-1 claim-staking-reward reward-cycle)) - (define-private (reduce-position-v2) (contract-call? .auto-alex-v3-1 reduce-position-v2)) - -(define-private (get-shares-to-tokens (dx uint)) - (contract-call? .auto-alex-v3-1 get-shares-to-tokens dx)) - (define-private (claim-and-stake-v2 (reward-cycle uint)) (contract-call? .auto-alex-v2 claim-and-stake reward-cycle)) - (define-private (get-end-cycle-v2) (contract-call? .auto-alex-v2 get-end-cycle)) - (define-private (mul-down (a uint) (b uint)) (/ (* a b) ONE_8)) - (define-private (div-down (a uint) (b uint)) - (if (is-eq a u0) u0 (/ (* a ONE_8) b))) - -;; contract initialisation + (if (is-eq a u0) u0 (/ (* a ONE_8) b))) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar index 67219b66..7653e1e9 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-registry.clar @@ -1,106 +1,77 @@ -;; -- autoALEX creation/staking/redemption - -;; constants -;; (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-REQUEST-ID-NOT-FOUND (err u10019)) - +(define-constant ERR-CYCLE-NOT-STAKED (err u20020)) (define-constant PENDING 0x00) (define-constant FINALIZED 0x01) (define-constant REVOKED 0x02) - -;; data maps and vars -;; - (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) - (define-data-var start-cycle uint u340282366920938463463374607431768211455) (define-data-var redeem-request-nonce uint u0) - (define-map staked-cycle uint bool) -(define-map redeem-requests uint { requested-by: principal, amount: uint, redeem-cycle: uint, status: (buff 1) }) +(define-map staked-cycle-shares-to-token uint uint) +(define-map redeem-requests uint { requested-by: principal, shares: uint, redeem-cycle: uint, status: (buff 1) }) (define-map redeem-shares-per-cycle uint uint) -(define-map shares-to-token-per-cycle uint uint) - -;; read-only calls - +(define-map redeem-tokens-per-cycle uint uint) (define-read-only (get-pending) PENDING) - (define-read-only (get-finalized) FINALIZED) - (define-read-only (get-revoked) REVOKED) - (define-read-only (get-contract-owner) (var-get contract-owner)) - (define-read-only (get-start-cycle) (var-get start-cycle)) - (define-read-only (is-cycle-staked (reward-cycle uint)) (is-some (map-get? staked-cycle reward-cycle))) - -(define-read-only (get-shares-to-tokens-per-cycle-or-default (reward-cycle uint)) - (default-to u0 (map-get? shares-to-token-per-cycle reward-cycle))) - +(define-read-only (get-staked-cycle-shares-to-tokens-or-fail (reward-cycle uint)) + (ok (unwrap! (map-get? staked-cycle-shares-to-token reward-cycle) ERR-CYCLE-NOT-STAKED))) (define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) (default-to u0 (map-get? redeem-shares-per-cycle reward-cycle))) - +(define-read-only (get-redeem-tokens-per-cycle-or-default (reward-cycle uint)) + (default-to u0 (map-get? redeem-tokens-per-cycle reward-cycle))) (define-read-only (get-redeem-request-or-fail (request-id uint)) (ok (unwrap! (map-get? redeem-requests request-id) ERR-REQUEST-ID-NOT-FOUND))) - -;; governance calls - (define-public (set-contract-owner (owner principal)) (begin (try! (check-is-owner)) (ok (var-set contract-owner owner)))) - (define-public (set-approved-contract (owner principal) (approved bool)) (begin (try! (check-is-owner)) (ok (map-set approved-contracts owner approved)))) - (define-public (set-start-cycle (new-start-cycle uint)) (begin (try! (check-is-owner)) (map-set staked-cycle new-start-cycle true) + (map-set staked-cycle-shares-to-token new-start-cycle u0) (ok (var-set start-cycle new-start-cycle)))) - -;; privileged functions -;; - (define-public (set-staked-cycle (cycle uint) (staked bool)) (begin (try! (check-is-approved)) (ok (map-set staked-cycle cycle staked)))) - -(define-public (set-shares-to-tokens-per-cycle (cycle uint) (shares-to-tokens uint)) +(define-public (set-staked-cycle-shares-to-tokens (cycle uint) (shares-to-tokens uint)) (begin (try! (check-is-approved)) - (ok (map-set shares-to-token-per-cycle cycle shares-to-tokens)))) - -(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, amount: uint, redeem-cycle: uint, status: (buff 1) })) + (asserts! (is-cycle-staked cycle) ERR-CYCLE-NOT-STAKED) + (ok (map-set staked-cycle-shares-to-token cycle shares-to-tokens)))) +(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, shares: uint, redeem-cycle: uint, status: (buff 1) })) (let ( (next-nonce (+ (var-get redeem-request-nonce) u1)) (id (if (> request-id u0) request-id (begin (var-set redeem-request-nonce next-nonce) next-nonce)))) (try! (check-is-approved)) (map-set redeem-requests id request-details) (ok id))) - (define-public (set-redeem-shares-per-cycle (reward-cycle uint) (shares uint)) (begin (try! (check-is-approved)) (ok (map-set redeem-shares-per-cycle reward-cycle shares)))) - -;; private functions -;; - +(define-public (set-redeem-tokens-per-cycle (reward-cycle uint) (tokens uint)) + (begin + (try! (check-is-approved)) + (ok (map-set redeem-tokens-per-cycle reward-cycle tokens)))) (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) - (define-private (check-is-approved) - (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) + (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar b/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar index 081149ec..bb912898 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar @@ -1,125 +1,87 @@ (define-fungible-token auto-alex-v3-wrapped) - (define-constant ERR-NOT-AUTHORIZED (err u1000)) - (define-constant ONE_8 u100000000) -(define-constant token-decimals u8) - (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) - (define-data-var token-name (string-ascii 32) "Wrapped Auto ALEX v3") (define-data-var token-symbol (string-ascii 10) "watALEXv3") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3-wrapped.json")) - -;; governance functions - +(define-data-var token-decimals uint u8) (define-public (set-contract-owner (owner principal)) - (begin - (try! (check-is-owner)) - (ok (var-set contract-owner owner)))) - + (begin + (try! (check-is-owner)) + (ok (var-set contract-owner owner)))) (define-public (set-approved-contract (owner principal) (approved bool)) (begin (try! (check-is-owner)) (ok (map-set approved-contracts owner approved)))) - (define-public (set-name (new-name (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-name new-name)))) - (define-public (set-symbol (new-symbol (string-ascii 10))) (begin (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) - +(define-public (set-decimals (new-decimals uint)) + (begin + (try! (check-is-owner)) + (ok (var-set token-decimals new-decimals)))) (define-public (set-token-uri (new-uri (optional (string-utf8 256)))) (begin (try! (check-is-owner)) (ok (var-set token-uri new-uri)))) - -;; public functions - (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) (begin (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) (try! (ft-transfer? auto-alex-v3-wrapped amount sender recipient)) (print { type: "transfer", amount: amount, sender: sender, recipient: recipient, memo: memo }) (ok true))) - (define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) (transfer amount sender recipient memo)) - (define-public (mint (amount uint) (recipient principal)) (begin (asserts! (or (is-eq tx-sender recipient) (is-eq contract-caller recipient)) ERR-NOT-AUTHORIZED) (try! (ft-mint? auto-alex-v3-wrapped (get-tokens-to-shares amount) recipient)) (contract-call? .auto-alex-v3-1 transfer amount recipient (as-contract tx-sender) none))) - (define-public (mint-fixed (amount uint) (recipient principal)) (mint amount recipient)) - (define-public (burn (amount uint) (sender principal)) (begin (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) (as-contract (try! (contract-call? .auto-alex-v3-1 transfer (get-shares-to-tokens amount) tx-sender sender none))) (ft-burn? auto-alex-v3-wrapped amount sender))) - (define-public (burn-fixed (amount uint) (sender principal)) (burn amount sender)) - -;; read-only functions - (define-read-only (get-contract-owner) (var-get contract-owner)) (define-read-only (get-name) (ok (var-get token-name))) - (define-read-only (get-symbol) (ok (var-get token-symbol))) - (define-read-only (get-token-uri) (ok (var-get token-uri))) - (define-read-only (get-decimals) - (ok token-decimals)) - + (ok (var-get token-decimals))) (define-read-only (get-balance (who principal)) (ok (ft-get-balance auto-alex-v3-wrapped who))) - -(define-read-only (get-balance-fixed (who principal)) - (get-balance who)) - (define-read-only (get-total-supply) (ok (ft-get-supply auto-alex-v3-wrapped))) - -(define-read-only (get-total-supply-fixed) - (get-total-supply)) - (define-read-only (get-share (who principal)) (ok (get-shares-to-tokens (unwrap-panic (get-balance who))))) - (define-read-only (get-total-shares) (contract-call? .auto-alex-v3-1 get-balance (as-contract tx-sender))) - (define-read-only (get-tokens-to-shares (amount uint)) (if (is-eq (get-total-supply) (ok u0)) amount (/ (* amount (unwrap-panic (get-total-supply))) (unwrap-panic (get-total-shares))))) - (define-read-only (get-shares-to-tokens (shares uint)) (if (is-eq (get-total-supply) (ok u0)) shares (/ (* shares (unwrap-panic (get-total-shares))) (unwrap-panic (get-total-supply))))) - -;; private functions - (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) - (define-private (check-is-approved) (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) - (contract-call? .alex-vault-v1-1 set-approved-token .auto-alex-v3-1-wrapped true) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-1.clar b/clarity/contracts/auto-token/auto-alex-v3-1.clar index e3f977a4..a8c5fab2 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1.clar @@ -1,73 +1,56 @@ (define-fungible-token auto-alex-v3) - (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ERR-INVALID-AMOUNT (err u1001)) - (define-constant ONE_8 u100000000) - -(define-constant token-decimals u8) - (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) - (define-data-var token-name (string-ascii 32) "Auto ALEX v3") (define-data-var token-symbol (string-ascii 10) "atALEXv3") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3.json")) - +(define-data-var token-decimals uint u8) (define-data-var reserve uint u0) - -;; governance functions - (define-public (set-contract-owner (owner principal)) (begin (try! (check-is-owner)) (ok (var-set contract-owner owner)))) - (define-public (set-approved-contract (owner principal) (approved bool)) (begin (try! (check-is-owner)) (ok (map-set approved-contracts owner approved)))) - (define-public (set-name (new-name (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-name new-name)))) - (define-public (set-symbol (new-symbol (string-ascii 10))) (begin (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) - +(define-public (set-decimals (new-decimals uint)) + (begin + (try! (check-is-owner)) + (ok (var-set token-decimals new-decimals)))) (define-public (set-token-uri (new-uri (optional (string-utf8 256)))) (begin (try! (check-is-owner)) (ok (var-set token-uri new-uri)))) - -;; privileged calls - (define-public (set-reserve (new-reserve uint)) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) (var-set reserve new-reserve) (print {notification: "rebase", payload: {reserve: (var-get reserve), total-shares: (ft-get-supply auto-alex-v3)}}) (ok true))) - (define-public (add-reserve (increment uint)) (set-reserve (+ (var-get reserve) increment))) - (define-public (remove-reserve (decrement uint)) (begin (asserts! (<= decrement (var-get reserve)) ERR-INVALID-AMOUNT) (set-reserve (- (var-get reserve) decrement)))) - (define-public (mint (amount uint) (recipient principal)) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) (ft-mint? auto-alex-v3 (get-tokens-to-shares amount) recipient))) - (define-public (mint-fixed (amount uint) (recipient principal)) (mint amount recipient)) - (define-public (burn (amount uint) (sender principal)) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) @@ -75,69 +58,48 @@ (define-public (burn-fixed (amount uint) (sender principal)) (burn amount sender)) - (define-public (burn-many (senders (list 200 {amount: uint, sender: principal}))) (fold check-err (map burn-many-iter senders) (ok true))) - (define-public (burn-fixed-many (senders (list 200 {amount: uint, sender: principal}))) (burn-many senders)) - -;; read-only functions - (define-read-only (get-contract-owner) (var-get contract-owner)) - (define-read-only (get-name) (ok (var-get token-name))) - (define-read-only (get-symbol) (ok (var-get token-symbol))) - (define-read-only (get-token-uri) (ok (var-get token-uri))) - (define-read-only (get-decimals) - (ok token-decimals)) - + (ok (var-get token-decimals))) (define-read-only (get-balance (who principal)) (ok (get-shares-to-tokens (unwrap-panic (get-share who))))) - (define-read-only (get-balance-fixed (who principal)) (get-balance who)) - (define-read-only (get-total-supply) (get-reserve)) - (define-read-only (get-total-supply-fixed) (get-total-supply)) - (define-read-only (get-share (who principal)) (ok (ft-get-balance auto-alex-v3 who))) - (define-read-only (get-share-fixed (who principal)) (get-share who)) - (define-read-only (get-total-shares) (ok (ft-get-supply auto-alex-v3))) - (define-read-only (get-total-shares-fixed) (get-total-shares)) - (define-read-only (get-reserve) (ok (var-get reserve))) - +(define-read-only (get-reserve-fixed) + (get-reserve)) (define-read-only (get-tokens-to-shares (amount uint)) (if (is-eq (get-reserve) (ok u0)) amount (/ (* amount (unwrap-panic (get-total-shares))) (unwrap-panic (get-reserve))))) - (define-read-only (get-shares-to-tokens (shares uint)) (if (is-eq (get-total-shares) (ok u0)) shares (/ (* shares (unwrap-panic (get-reserve))) (unwrap-panic (get-total-shares))))) - -;; public calls - (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) (let ( (shares (get-tokens-to-shares amount))) @@ -146,50 +108,35 @@ (match memo to-print (print to-print) 0x) (print { notification: "transfer", payload: { amount: amount, shares: shares, sender: sender, recipient: recipient } }) (ok true))) - (define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) (transfer amount sender recipient memo)) - -;; private functions - (define-private (burn-many-iter (item {amount: uint, sender: principal})) (burn (get amount item) (get sender item))) - (define-private (check-err (result (response bool uint)) (prior (response bool uint))) (match prior ok-value result err-value (err err-value))) - (define-private (check-is-owner) - (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) - + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) (define-private (check-is-approved) - (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) - -;; staking related fuctions - + (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) (use-trait ft-trait .trait-sip-010.sip-010-trait) - (define-public (stake-tokens (amount-tokens uint) (lock-period uint)) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) (as-contract (contract-call? .alex-reserve-pool stake-tokens .age000-governance-token amount-tokens lock-period)))) - (define-public (transfer-token (token-trait ) (amount uint) (recipient principal)) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) (if (is-eq (contract-of token-trait) (as-contract tx-sender)) (as-contract (transfer-fixed amount tx-sender recipient none)) (as-contract (contract-call? token-trait transfer-fixed amount tx-sender recipient none))))) - (define-public (claim-staking-reward (reward-cycle uint)) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) (as-contract (contract-call? .alex-reserve-pool claim-staking-reward .age000-governance-token reward-cycle)))) - (define-public (reduce-position-v2) (begin (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) (if (is-eq u0 (unwrap-panic (contract-call? .auto-alex-v2 get-balance (as-contract tx-sender)))) (ok u0) (as-contract (contract-call? .auto-alex-v2 reduce-position ONE_8))))) - (contract-call? .alex-vault-v1-1 set-approved-token .auto-alex-v3-1 true) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar new file mode 100644 index 00000000..977e7e90 --- /dev/null +++ b/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar @@ -0,0 +1,300 @@ +;; -- autoALEX creation/staking/redemption + +;; constants +;; +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-INVALID-LIQUIDITY (err u2003)) +(define-constant ERR-NOT-ACTIVATED (err u2043)) +(define-constant ERR-PAUSED (err u2046)) +(define-constant ERR-STAKING-NOT-AVAILABLE (err u10015)) +(define-constant ERR-REWARD-CYCLE-NOT-COMPLETED (err u10017)) +(define-constant ERR-CLAIM-AND-STAKE (err u10018)) +(define-constant ERR-NO-REDEEM-REVOKE (err u10019)) +(define-constant ERR-REQUEST-FINALIZED-OR-REVOKED (err u10020)) +(define-constant ERR-REDEEM-IMBALANCE (err u10021)) +(define-constant ERR-END-CYCLE-V2 (err u10022)) + +(define-constant ONE_8 u100000000) + +(define-constant REWARD-CYCLE-INDEXES (list 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 u32)) + +;; data maps and vars +;; + +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) + +(define-data-var create-paused bool true) +(define-data-var redeem-paused bool true) + +;; __IF_MAINNET__ +(define-constant redeem-delay-cycles u32) +;; (define-constant redeem-delay-cycles u2) +;; __ENDIF__ + +;; read-only calls + +(define-read-only (get-contract-owner) + (var-get contract-owner)) + +(define-read-only (get-pending) + (contract-call? .auto-alex-v3-2-registry get-pending)) + +(define-read-only (get-finalized) + (contract-call? .auto-alex-v3-2-registry get-finalized)) + +(define-read-only (get-revoked) + (contract-call? .auto-alex-v3-2-registry get-revoked)) + +(define-read-only (get-start-cycle) + (contract-call? .auto-alex-v3-2-registry get-start-cycle)) + +(define-read-only (is-cycle-staked (reward-cycle uint)) + (contract-call? .auto-alex-v3-2-registry is-cycle-staked reward-cycle)) + +(define-read-only (get-shares-to-tokens-per-cycle-or-default (reward-cycle uint)) + (contract-call? .auto-alex-v3-2-registry get-shares-to-tokens-per-cycle-or-default reward-cycle)) + +(define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) + (contract-call? .auto-alex-v3-2-registry get-redeem-shares-per-cycle-or-default reward-cycle)) + +(define-read-only (get-redeem-request-or-fail (request-id uint)) + (contract-call? .auto-alex-v3-2-registry get-redeem-request-or-fail request-id)) + +(define-read-only (is-create-paused) + (var-get create-paused)) + +(define-read-only (is-redeem-paused) + (var-get redeem-paused)) + +;; @desc get the next capital base of the vault +;; @desc next-base = principal to be staked at the next cycle +;; @desc + principal to be claimed at the next cycle and staked for the following cycle +;; @desc + reward to be claimed at the next cycle and staked for the following cycle +;; @desc + balance of ALEX in the contract +;; @desc + intrinsic of autoALEXv2 in the contract +(define-read-only (get-next-base) + (let ( + (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) + (auto-alex-v2-bal (unwrap-panic (contract-call? .auto-alex-v2 get-balance-fixed .auto-alex-v3-2)))) + (asserts! (or (is-eq current-cycle (get-start-cycle)) (is-cycle-staked (- current-cycle u1))) ERR-CLAIM-AND-STAKE) + (ok + (+ + (get amount-staked (as-contract (get-staker-at-cycle (+ current-cycle u1)))) + (get to-return (as-contract (get-staker-at-cycle current-cycle))) + (as-contract (get-staking-reward current-cycle)) + (unwrap-panic (contract-call? .age000-governance-token get-balance-fixed .auto-alex-v3-2)) + (if (is-eq auto-alex-v2-bal u0) u0 (mul-down auto-alex-v2-bal (try! (contract-call? .auto-alex-v2 get-intrinsic)))))))) + +;; @desc get the intrinsic value of auto-alex-v3 +;; @desc intrinsic = next capital base of the vault / total supply of auto-alex-v3 +(define-read-only (get-intrinsic) + (get-shares-to-tokens ONE_8)) + +;; governance calls + +(define-public (set-contract-owner (owner principal)) + (begin + (try! (check-is-owner)) + (ok (var-set contract-owner owner)))) + +(define-public (set-approved-contract (owner principal) (approved bool)) + (begin + (try! (check-is-owner)) + (ok (map-set approved-contracts owner approved)))) + +(define-public (pause-create (pause bool)) + (begin + (try! (check-is-owner)) + (ok (var-set create-paused pause)))) + +(define-public (pause-redeem (pause bool)) + (begin + (try! (check-is-owner)) + (ok (var-set redeem-paused pause)))) + +;; public functions +;; + +(define-public (rebase) + (let ( + (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) + (start-cycle (get-start-cycle)) + (check-start-cycle (asserts! (<= start-cycle current-cycle) ERR-NOT-ACTIVATED))) + (and (> current-cycle start-cycle) (not (is-cycle-staked (- current-cycle u1))) (try! (claim-and-stake (- current-cycle u1)))) + (as-contract (try! (contract-call? .auto-alex-v3-2 set-reserve (try! (get-next-base))))) + (ok current-cycle))) + +;; @desc triggers external event that claims all that's available and stake for another 32 cycles +;; @desc this can be triggered by anyone +;; @param reward-cycle the target cycle to claim (and stake for current cycle + 32 cycles). reward-cycle must be < current cycle. +(define-public (claim-and-stake (reward-cycle uint)) + (let ( + (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) + (end-cycle-v2 (get-end-cycle-v2)) + ;; claim all that's available to claim for the reward-cycle + (claimed (as-contract (try! (claim-staking-reward reward-cycle)))) + (claimed-v2 (if (< end-cycle-v2 current-cycle) (as-contract (try! (reduce-position-v2))) (begin (try! (claim-and-stake-v2 reward-cycle)) u0))) + (tokens (+ (get to-return claimed) (get entitled-token claimed) claimed-v2)) + (previous-shares-to-tokens (get-shares-to-tokens-per-cycle-or-default (- reward-cycle u1))) + (redeeming (mul-down previous-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) + (intrinsic (get-shares-to-tokens ONE_8))) + (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) + (asserts! (>= tokens redeeming) ERR-REDEEM-IMBALANCE) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-staked-cycle reward-cycle true))) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-shares-to-tokens-per-cycle reward-cycle intrinsic))) + (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: (- tokens redeeming) }))) + (print { notification: "claim-and-stake", payload: { redeeming: redeeming, tokens: tokens }}) + (ok true))) + +;; claims alex for the reward-cycles and mint auto-alex-v3 +(define-public (claim-and-mint (reward-cycles (list 200 uint))) + (let ( + (claimed (unwrap-panic (contract-call? .staking-helper claim-staking-reward .age000-governance-token reward-cycles)))) + (try! (add-to-position (fold sum-claimed claimed u0))) + (ok claimed))) + +;; @desc add to position +;; @desc transfers dx to vault, stake them for 32 cycles and mints auto-alex-v3, the number of which is determined as % of total supply / next base +;; @param dx the number of $ALEX in 8-digit fixed point notation +(define-public (add-to-position (dx uint)) + (let ( + (current-cycle (try! (rebase))) + (sender tx-sender)) + (asserts! (> dx u0) ERR-INVALID-LIQUIDITY) + (asserts! (not (is-create-paused)) ERR-PAUSED) + (try! (contract-call? .age000-governance-token transfer-fixed dx sender .auto-alex-v3-2 none)) + (try! (fold stake-tokens-iter REWARD-CYCLE-INDEXES (ok { current-cycle: current-cycle, remaining: dx }))) + (as-contract (try! (contract-call? .auto-alex-v3-2 mint-fixed dx sender))) + (print { notification: "position-added", payload: { new-supply: dx } }) + (rebase))) + +(define-public (upgrade (dx uint)) + (let ( + (end-cycle-v2 (get-end-cycle-v2)) + (current-cycle (try! (rebase))) + (intrinsic-dx (mul-down dx (try! (contract-call? .auto-alex-v2 get-intrinsic)))) + (sender tx-sender)) + (asserts! (> intrinsic-dx u0) ERR-INVALID-LIQUIDITY) + (asserts! (not (is-create-paused)) ERR-PAUSED) + (asserts! (< end-cycle-v2 (+ current-cycle u32)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly + (try! (contract-call? .auto-alex-v2 transfer-fixed dx sender .auto-alex-v3-2 none)) + (and (< end-cycle-v2 current-cycle) (begin (as-contract (try! (reduce-position-v2))) true)) + (as-contract (try! (contract-call? .auto-alex-v3-2 mint-fixed intrinsic-dx sender))) + (print { notification: "upgrade-position-added", payload: { new-supply: intrinsic-dx } }) + (rebase))) + +(define-public (request-redeem (amount uint)) + (let ( + (current-cycle (try! (rebase))) + (redeem-cycle (+ current-cycle redeem-delay-cycles)) + (request-details { requested-by: tx-sender, amount: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) + (asserts! (not (is-redeem-paused)) ERR-PAUSED) + (try! (contract-call? .auto-alex-v3-2 transfer-fixed amount tx-sender .auto-alex-v3-2 none)) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-redeem-shares-per-cycle redeem-cycle (+ (get-redeem-shares-per-cycle-or-default redeem-cycle) amount)))) + (print { notification: "redeem-request", payload: request-details }) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-redeem-request u0 request-details))) + (rebase))) + +(define-public (finalize-redeem (request-id uint)) + (let ( + (request-details (try! (get-redeem-request-or-fail request-id))) + (redeem-cycle (get redeem-cycle request-details)) + (check-claim-and-stake (and (not (is-cycle-staked redeem-cycle)) (try! (claim-and-stake redeem-cycle)))) + (current-cycle (try! (rebase))) + (tokens (mul-down (get-shares-to-tokens-per-cycle-or-default (- redeem-cycle u1)) (get amount request-details))) + (updated-request-details (merge request-details { status: (get-finalized) }))) + (asserts! (not (is-redeem-paused)) ERR-PAUSED) + (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) + + (as-contract (try! (contract-call? .auto-alex-v3-2 burn-fixed (get amount request-details) .auto-alex-v3-2))) + (as-contract (try! (contract-call? .auto-alex-v3-2 transfer-token .age000-governance-token tokens (get requested-by request-details)))) + (print { notification: "finalize-redeem", payload: updated-request-details }) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-redeem-request request-id updated-request-details))) + (rebase))) + +(define-public (revoke-redeem (request-id uint)) + (let ( + (request-details (try! (get-redeem-request-or-fail request-id))) + (current-cycle (try! (rebase))) + (redeem-cycle (get redeem-cycle request-details)) + (check-cycle (asserts! (> redeem-cycle current-cycle) ERR-NO-REDEEM-REVOKE)) + (tokens (mul-down (get-shares-to-tokens-per-cycle-or-default (- current-cycle u1)) (get amount request-details))) + (updated-request-details (merge request-details { status: (get-revoked) }))) + (asserts! (is-eq tx-sender (get requested-by request-details)) ERR-NOT-AUTHORIZED) + (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) + (as-contract (try! (contract-call? .auto-alex-v3-2 transfer-token .auto-alex-v3-2 tokens (get requested-by request-details)))) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-redeem-shares-per-cycle redeem-cycle (- (get-redeem-shares-per-cycle-or-default redeem-cycle) (get amount request-details))))) + (print { notification: "revoke-redeem", payload: updated-request-details }) + (as-contract (try! (contract-call? .auto-alex-v3-2-registry set-redeem-request request-id updated-request-details))) + (rebase))) + +;; private functions +;; + +(define-private (check-is-owner) + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) + +(define-private (check-is-approved) + (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) + +(define-private (sum-claimed (claimed-response (response (tuple (entitled-token uint) (to-return uint)) uint)) (sum-so-far uint)) + (match claimed-response + claimed (+ sum-so-far (get to-return claimed) (get entitled-token claimed)) + err sum-so-far)) + +(define-private (stake-tokens-iter (cycles-to-stake uint) (previous-response (response { current-cycle: uint, remaining: uint } uint))) + (match previous-response + ok-value + (let ( + (reward-cycle (+ (get current-cycle ok-value) cycles-to-stake)) + (redeeming (get-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) + (returning (get to-return (get-staker-at-cycle reward-cycle))) + (staking (if (is-eq cycles-to-stake u32) + (get remaining ok-value) + (if (> returning redeeming) + u0 + (if (> (get remaining ok-value) (- redeeming returning)) + (- redeeming returning) + (get remaining ok-value)))))) + (and (> staking u0) (as-contract (try! (stake-tokens staking cycles-to-stake)))) + (ok { current-cycle: (get current-cycle ok-value), remaining: (- (get remaining ok-value) staking) })) + err-value previous-response)) + +(define-private (get-reward-cycle (stack-height uint)) + (contract-call? .alex-reserve-pool get-reward-cycle .age000-governance-token stack-height)) + +(define-private (get-staking-reward (reward-cycle uint)) + (contract-call? .alex-reserve-pool get-staking-reward .age000-governance-token (get-user-id) reward-cycle)) + +(define-private (get-staker-at-cycle (reward-cycle uint)) + (contract-call? .alex-reserve-pool get-staker-at-cycle-or-default .age000-governance-token reward-cycle (get-user-id))) + +(define-private (get-user-id) + (default-to u0 (contract-call? .alex-reserve-pool get-user-id .age000-governance-token .auto-alex-v3-2))) + +(define-private (stake-tokens (amount-tokens uint) (lock-period uint)) + (contract-call? .auto-alex-v3-2 stake-tokens amount-tokens lock-period)) + +(define-private (claim-staking-reward (reward-cycle uint)) + (contract-call? .auto-alex-v3-2 claim-staking-reward reward-cycle)) + +(define-private (reduce-position-v2) + (contract-call? .auto-alex-v3-2 reduce-position-v2)) + +(define-private (get-shares-to-tokens (dx uint)) + (contract-call? .auto-alex-v3-2 get-shares-to-tokens dx)) + +(define-private (claim-and-stake-v2 (reward-cycle uint)) + (contract-call? .auto-alex-v2 claim-and-stake reward-cycle)) + +(define-private (get-end-cycle-v2) + (contract-call? .auto-alex-v2 get-end-cycle)) + +(define-private (mul-down (a uint) (b uint)) + (/ (* a b) ONE_8)) + +(define-private (div-down (a uint) (b uint)) + (if (is-eq a u0) u0 (/ (* a ONE_8) b))) + +;; contract initialisation diff --git a/clarity/contracts/auto-token/auto-alex-v3-2-registry.clar b/clarity/contracts/auto-token/auto-alex-v3-2-registry.clar new file mode 100644 index 00000000..67219b66 --- /dev/null +++ b/clarity/contracts/auto-token/auto-alex-v3-2-registry.clar @@ -0,0 +1,106 @@ +;; -- autoALEX creation/staking/redemption + +;; constants +;; +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-REQUEST-ID-NOT-FOUND (err u10019)) + +(define-constant PENDING 0x00) +(define-constant FINALIZED 0x01) +(define-constant REVOKED 0x02) + +;; data maps and vars +;; + +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) + +(define-data-var start-cycle uint u340282366920938463463374607431768211455) +(define-data-var redeem-request-nonce uint u0) + +(define-map staked-cycle uint bool) +(define-map redeem-requests uint { requested-by: principal, amount: uint, redeem-cycle: uint, status: (buff 1) }) +(define-map redeem-shares-per-cycle uint uint) +(define-map shares-to-token-per-cycle uint uint) + +;; read-only calls + +(define-read-only (get-pending) + PENDING) + +(define-read-only (get-finalized) + FINALIZED) + +(define-read-only (get-revoked) + REVOKED) + +(define-read-only (get-contract-owner) + (var-get contract-owner)) + +(define-read-only (get-start-cycle) + (var-get start-cycle)) + +(define-read-only (is-cycle-staked (reward-cycle uint)) + (is-some (map-get? staked-cycle reward-cycle))) + +(define-read-only (get-shares-to-tokens-per-cycle-or-default (reward-cycle uint)) + (default-to u0 (map-get? shares-to-token-per-cycle reward-cycle))) + +(define-read-only (get-redeem-shares-per-cycle-or-default (reward-cycle uint)) + (default-to u0 (map-get? redeem-shares-per-cycle reward-cycle))) + +(define-read-only (get-redeem-request-or-fail (request-id uint)) + (ok (unwrap! (map-get? redeem-requests request-id) ERR-REQUEST-ID-NOT-FOUND))) + +;; governance calls + +(define-public (set-contract-owner (owner principal)) + (begin + (try! (check-is-owner)) + (ok (var-set contract-owner owner)))) + +(define-public (set-approved-contract (owner principal) (approved bool)) + (begin + (try! (check-is-owner)) + (ok (map-set approved-contracts owner approved)))) + +(define-public (set-start-cycle (new-start-cycle uint)) + (begin + (try! (check-is-owner)) + (map-set staked-cycle new-start-cycle true) + (ok (var-set start-cycle new-start-cycle)))) + +;; privileged functions +;; + +(define-public (set-staked-cycle (cycle uint) (staked bool)) + (begin + (try! (check-is-approved)) + (ok (map-set staked-cycle cycle staked)))) + +(define-public (set-shares-to-tokens-per-cycle (cycle uint) (shares-to-tokens uint)) + (begin + (try! (check-is-approved)) + (ok (map-set shares-to-token-per-cycle cycle shares-to-tokens)))) + +(define-public (set-redeem-request (request-id uint) (request-details { requested-by: principal, amount: uint, redeem-cycle: uint, status: (buff 1) })) + (let ( + (next-nonce (+ (var-get redeem-request-nonce) u1)) + (id (if (> request-id u0) request-id (begin (var-set redeem-request-nonce next-nonce) next-nonce)))) + (try! (check-is-approved)) + (map-set redeem-requests id request-details) + (ok id))) + +(define-public (set-redeem-shares-per-cycle (reward-cycle uint) (shares uint)) + (begin + (try! (check-is-approved)) + (ok (map-set redeem-shares-per-cycle reward-cycle shares)))) + +;; private functions +;; + +(define-private (check-is-owner) + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) + +(define-private (check-is-approved) + (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) diff --git a/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar b/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar new file mode 100644 index 00000000..79273b6f --- /dev/null +++ b/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar @@ -0,0 +1,125 @@ +(define-fungible-token auto-alex-v3-wrapped) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) + +(define-constant ONE_8 u100000000) +(define-constant token-decimals u8) + +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) + +(define-data-var token-name (string-ascii 32) "vLiALEX") +(define-data-var token-symbol (string-ascii 10) "vLiALEX") +(define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3-wrapped.json")) + +;; governance functions + +(define-public (set-contract-owner (owner principal)) + (begin + (try! (check-is-owner)) + (ok (var-set contract-owner owner)))) + +(define-public (set-approved-contract (owner principal) (approved bool)) + (begin + (try! (check-is-owner)) + (ok (map-set approved-contracts owner approved)))) + +(define-public (set-name (new-name (string-ascii 32))) + (begin + (try! (check-is-owner)) + (ok (var-set token-name new-name)))) + +(define-public (set-symbol (new-symbol (string-ascii 10))) + (begin + (try! (check-is-owner)) + (ok (var-set token-symbol new-symbol)))) + +(define-public (set-token-uri (new-uri (optional (string-utf8 256)))) + (begin + (try! (check-is-owner)) + (ok (var-set token-uri new-uri)))) + +;; public functions + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) + (begin + (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) + (try! (ft-transfer? auto-alex-v3-wrapped amount sender recipient)) + (print { type: "transfer", amount: amount, sender: sender, recipient: recipient, memo: memo }) + (ok true))) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) + (transfer amount sender recipient memo)) + +(define-public (mint (amount uint) (recipient principal)) + (begin + (asserts! (or (is-eq tx-sender recipient) (is-eq contract-caller recipient)) ERR-NOT-AUTHORIZED) + (try! (ft-mint? auto-alex-v3-wrapped (get-tokens-to-shares amount) recipient)) + (contract-call? .auto-alex-v3-2 transfer amount recipient (as-contract tx-sender) none))) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint amount recipient)) + +(define-public (burn (amount uint) (sender principal)) + (begin + (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) + (as-contract (try! (contract-call? .auto-alex-v3-2 transfer (get-shares-to-tokens amount) tx-sender sender none))) + (ft-burn? auto-alex-v3-wrapped amount sender))) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn amount sender)) + +;; read-only functions + +(define-read-only (get-contract-owner) + (var-get contract-owner)) + +(define-read-only (get-name) + (ok (var-get token-name))) + +(define-read-only (get-symbol) + (ok (var-get token-symbol))) + +(define-read-only (get-token-uri) + (ok (var-get token-uri))) + +(define-read-only (get-decimals) + (ok token-decimals)) + +(define-read-only (get-balance (who principal)) + (ok (ft-get-balance auto-alex-v3-wrapped who))) + +(define-read-only (get-balance-fixed (who principal)) + (get-balance who)) + +(define-read-only (get-total-supply) + (ok (ft-get-supply auto-alex-v3-wrapped))) + +(define-read-only (get-total-supply-fixed) + (get-total-supply)) + +(define-read-only (get-share (who principal)) + (ok (get-shares-to-tokens (unwrap-panic (get-balance who))))) + +(define-read-only (get-total-shares) + (contract-call? .auto-alex-v3-2 get-balance (as-contract tx-sender))) + +(define-read-only (get-tokens-to-shares (amount uint)) + (if (is-eq (get-total-supply) (ok u0)) + amount + (/ (* amount (unwrap-panic (get-total-supply))) (unwrap-panic (get-total-shares))))) + +(define-read-only (get-shares-to-tokens (shares uint)) + (if (is-eq (get-total-supply) (ok u0)) + shares + (/ (* shares (unwrap-panic (get-total-shares))) (unwrap-panic (get-total-supply))))) + +;; private functions + +(define-private (check-is-owner) + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) + +(define-private (check-is-approved) + (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) + +(contract-call? .alex-vault-v1-1 set-approved-token .auto-alex-v3-2-wrapped true) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-2.clar b/clarity/contracts/auto-token/auto-alex-v3-2.clar new file mode 100644 index 00000000..6eb6ff76 --- /dev/null +++ b/clarity/contracts/auto-token/auto-alex-v3-2.clar @@ -0,0 +1,195 @@ +(define-fungible-token auto-alex-v3) + +(define-constant ERR-NOT-AUTHORIZED (err u1000)) +(define-constant ERR-INVALID-AMOUNT (err u1001)) + +(define-constant ONE_8 u100000000) + +(define-constant token-decimals u8) + +(define-data-var contract-owner principal tx-sender) +(define-map approved-contracts principal bool) + +(define-data-var token-name (string-ascii 32) "LiALEX") +(define-data-var token-symbol (string-ascii 10) "LiALEX") +(define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3.json")) + +(define-data-var reserve uint u0) + +;; governance functions + +(define-public (set-contract-owner (owner principal)) + (begin + (try! (check-is-owner)) + (ok (var-set contract-owner owner)))) + +(define-public (set-approved-contract (owner principal) (approved bool)) + (begin + (try! (check-is-owner)) + (ok (map-set approved-contracts owner approved)))) + +(define-public (set-name (new-name (string-ascii 32))) + (begin + (try! (check-is-owner)) + (ok (var-set token-name new-name)))) + +(define-public (set-symbol (new-symbol (string-ascii 10))) + (begin + (try! (check-is-owner)) + (ok (var-set token-symbol new-symbol)))) + +(define-public (set-token-uri (new-uri (optional (string-utf8 256)))) + (begin + (try! (check-is-owner)) + (ok (var-set token-uri new-uri)))) + +;; privileged calls + +(define-public (set-reserve (new-reserve uint)) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (var-set reserve new-reserve) + (print {notification: "rebase", payload: {reserve: (var-get reserve), total-shares: (ft-get-supply auto-alex-v3)}}) + (ok true))) + +(define-public (add-reserve (increment uint)) + (set-reserve (+ (var-get reserve) increment))) + +(define-public (remove-reserve (decrement uint)) + (begin + (asserts! (<= decrement (var-get reserve)) ERR-INVALID-AMOUNT) + (set-reserve (- (var-get reserve) decrement)))) + +(define-public (mint (amount uint) (recipient principal)) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (ft-mint? auto-alex-v3 (get-tokens-to-shares amount) recipient))) + +(define-public (mint-fixed (amount uint) (recipient principal)) + (mint amount recipient)) + +(define-public (burn (amount uint) (sender principal)) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (ft-burn? auto-alex-v3 (get-tokens-to-shares amount) sender))) + +(define-public (burn-fixed (amount uint) (sender principal)) + (burn amount sender)) + +(define-public (burn-many (senders (list 200 {amount: uint, sender: principal}))) + (fold check-err (map burn-many-iter senders) (ok true))) + +(define-public (burn-fixed-many (senders (list 200 {amount: uint, sender: principal}))) + (burn-many senders)) + +;; read-only functions + +(define-read-only (get-contract-owner) + (var-get contract-owner)) + +(define-read-only (get-name) + (ok (var-get token-name))) + +(define-read-only (get-symbol) + (ok (var-get token-symbol))) + +(define-read-only (get-token-uri) + (ok (var-get token-uri))) + +(define-read-only (get-decimals) + (ok token-decimals)) + +(define-read-only (get-balance (who principal)) + (ok (get-shares-to-tokens (unwrap-panic (get-share who))))) + +(define-read-only (get-balance-fixed (who principal)) + (get-balance who)) + +(define-read-only (get-total-supply) + (get-reserve)) + +(define-read-only (get-total-supply-fixed) + (get-total-supply)) + +(define-read-only (get-share (who principal)) + (ok (ft-get-balance auto-alex-v3 who))) + +(define-read-only (get-share-fixed (who principal)) + (get-share who)) + +(define-read-only (get-total-shares) + (ok (ft-get-supply auto-alex-v3))) + +(define-read-only (get-total-shares-fixed) + (get-total-shares)) + +(define-read-only (get-reserve) + (ok (var-get reserve))) + +(define-read-only (get-tokens-to-shares (amount uint)) + (if (is-eq (get-reserve) (ok u0)) + amount + (/ (* amount (unwrap-panic (get-total-shares))) (unwrap-panic (get-reserve))))) + +(define-read-only (get-shares-to-tokens (shares uint)) + (if (is-eq (get-total-shares) (ok u0)) + shares + (/ (* shares (unwrap-panic (get-reserve))) (unwrap-panic (get-total-shares))))) + +;; public calls + +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) + (let ( + (shares (get-tokens-to-shares amount))) + (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) + (try! (ft-transfer? auto-alex-v3 shares sender recipient)) + (match memo to-print (print to-print) 0x) + (print { notification: "transfer", payload: { amount: amount, shares: shares, sender: sender, recipient: recipient } }) + (ok true))) + +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) + (transfer amount sender recipient memo)) + +;; private functions + +(define-private (burn-many-iter (item {amount: uint, sender: principal})) + (burn (get amount item) (get sender item))) + +(define-private (check-err (result (response bool uint)) (prior (response bool uint))) + (match prior ok-value result err-value (err err-value))) + +(define-private (check-is-owner) + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) + +(define-private (check-is-approved) + (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) + +;; staking related fuctions + +(use-trait ft-trait .trait-sip-010.sip-010-trait) + +(define-public (stake-tokens (amount-tokens uint) (lock-period uint)) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (as-contract (contract-call? .alex-reserve-pool stake-tokens .age000-governance-token amount-tokens lock-period)))) + +(define-public (transfer-token (token-trait ) (amount uint) (recipient principal)) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (if (is-eq (contract-of token-trait) (as-contract tx-sender)) + (as-contract (transfer-fixed amount tx-sender recipient none)) + (as-contract (contract-call? token-trait transfer-fixed amount tx-sender recipient none))))) + +(define-public (claim-staking-reward (reward-cycle uint)) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (as-contract (contract-call? .alex-reserve-pool claim-staking-reward .age000-governance-token reward-cycle)))) + +(define-public (reduce-position-v2) + (begin + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) + (if (is-eq u0 (unwrap-panic (contract-call? .auto-alex-v2 get-balance (as-contract tx-sender)))) + (ok u0) + (as-contract (contract-call? .auto-alex-v2 reduce-position ONE_8))))) + +(contract-call? .alex-vault-v1-1 set-approved-token .auto-alex-v3-2 true) \ No newline at end of file diff --git a/clarity/tests/auto-alex-v3_test.ts b/clarity/tests/auto-alex-v3_test.ts index 42f8ddaf..a9ab0a94 100644 --- a/clarity/tests/auto-alex-v3_test.ts +++ b/clarity/tests/auto-alex-v3_test.ts @@ -68,11 +68,11 @@ Clarinet.test({ block = chain.mineBlock([ yieldVault.setEndCycle(wallet_1, end_cycle), yieldVault.setEndCycle(deployer, end_cycle), - Tx.contractCall('auto-alex-v3-1', 'set-approved-contract', [types.principal(deployer.address + '.auto-alex-v3-1-endpoint'), types.bool(true)], deployer.address), - Tx.contractCall('auto-alex-v3-1-registry', 'set-approved-contract', [types.principal(deployer.address + '.auto-alex-v3-1-endpoint'), types.bool(true)], deployer.address), - Tx.contractCall('auto-alex-v3-1-registry', 'set-start-cycle', [types.uint(0)], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'pause-create', [types.bool(false)], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'pause-redeem', [types.bool(false)], deployer.address) + Tx.contractCall('auto-alex-v3-2', 'set-approved-contract', [types.principal(deployer.address + '.auto-alex-v3-2-endpoint'), types.bool(true)], deployer.address), + Tx.contractCall('auto-alex-v3-2-registry', 'set-approved-contract', [types.principal(deployer.address + '.auto-alex-v3-2-endpoint'), types.bool(true)], deployer.address), + Tx.contractCall('auto-alex-v3-2-registry', 'set-start-cycle', [types.uint(0)], deployer.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'pause-create', [types.bool(false)], deployer.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'pause-redeem', [types.bool(false)], deployer.address) ]); block.receipts[0].result.expectErr().expectUint(1000); block.receipts[1].result.expectOk(); @@ -86,10 +86,10 @@ Clarinet.test({ block = chain.mineBlock([ yieldVault.addToPosition(wallet_1, dx), - // Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_1.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_2.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_3.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'add-to-position', [types.uint(dx)], wallet_4.address), + // Tx.contractCall('auto-alex-v3-2-endpoint', 'add-to-position', [types.uint(dx)], wallet_1.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'add-to-position', [types.uint(dx)], wallet_2.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'add-to-position', [types.uint(dx)], wallet_3.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'add-to-position', [types.uint(dx)], wallet_4.address), ]); block.receipts.forEach((e) => { e.result.expectOk() }); @@ -97,13 +97,13 @@ Clarinet.test({ block = chain.mineBlock([ Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(1), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_1.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_2.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_3.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'request-redeem', [types.uint(dx)], wallet_4.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'revoke-redeem', [types.uint(4)], wallet_4.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'rebase', [], deployer.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'request-redeem', [types.uint(dx)], wallet_1.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'request-redeem', [types.uint(dx)], wallet_2.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'request-redeem', [types.uint(dx)], wallet_3.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'request-redeem', [types.uint(dx)], wallet_4.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'revoke-redeem', [types.uint(4)], wallet_4.address), ]); // console.log(block.receipts[3].events); block.receipts.forEach(e => { e.result.expectOk() }); @@ -114,8 +114,8 @@ Clarinet.test({ chain.mineEmptyBlockUntil(ACTIVATION_BLOCK + (cycle + 1) * 525); block = chain.mineBlock([ Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(1), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'rebase', [], deployer.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); console.log(block.receipts[1].events); block.receipts[1].result.expectOk(); @@ -124,40 +124,40 @@ Clarinet.test({ chain.mineEmptyBlockUntil(ACTIVATION_BLOCK + (redeem_cycle + 1) * 525); - console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-1')], wallet_1.address)); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-intrinsic', [], wallet_1.address)); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-shares-to-tokens-per-cycle-or-default', [types.uint(redeem_cycle - 1)], wallet_1.address)); + console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-2')], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-2-endpoint', 'get-intrinsic', [], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-2-endpoint', 'get-shares-to-tokens-per-cycle-or-default', [types.uint(redeem_cycle - 1)], wallet_1.address)); block = chain.mineBlock([ - Tx.contractCall('auto-alex-v3-1-endpoint', 'rebase', [], deployer.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'revoke-redeem', [types.uint(3)], wallet_3.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'rebase', [], deployer.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'revoke-redeem', [types.uint(3)], wallet_3.address), ]); console.log(block.receipts[0].events); block.receipts[0].result.expectOk(); block.receipts[1].result.expectErr(10019); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(2)], wallet_1.address).result); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-redeem-request-or-fail', [types.uint(3)], wallet_1.address).result); - console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-1')], wallet_1.address)); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-intrinsic', [], wallet_1.address)); - console.log(chain.callReadOnlyFn('auto-alex-v3-1-endpoint', 'get-shares-to-tokens-per-cycle-or-default', [types.uint(redeem_cycle)], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-2-endpoint', 'get-redeem-request-or-fail', [types.uint(2)], wallet_1.address).result); + console.log(chain.callReadOnlyFn('auto-alex-v3-2-endpoint', 'get-redeem-request-or-fail', [types.uint(3)], wallet_1.address).result); + console.log(chain.callReadOnlyFn('age000-governance-token', 'get-balance-fixed', [types.principal(deployer.address + '.auto-alex-v3-2')], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-2-endpoint', 'get-intrinsic', [], wallet_1.address)); + console.log(chain.callReadOnlyFn('auto-alex-v3-2-endpoint', 'get-shares-to-tokens-per-cycle-or-default', [types.uint(redeem_cycle)], wallet_1.address)); block = chain.mineBlock([ - Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(3)], wallet_3.address), - Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'finalize-redeem', [types.uint(3)], wallet_3.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); console.log(block.receipts[0].events); console.log(block.receipts[1].events); block.receipts.forEach(e => { e.result.expectOk() }); block = chain.mineBlock([ - Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(1)], wallet_1.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'finalize-redeem', [types.uint(1)], wallet_1.address), ]); console.log(block.receipts[0].events); block.receipts.forEach(e => { e.result.expectOk() }); block = chain.mineBlock([ - Tx.contractCall('auto-alex-v3-1-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), + Tx.contractCall('auto-alex-v3-2-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); block.receipts[0].result.expectErr(10020); }, From b409b00783a04b67ede9579986472fc1c11cc771 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 17:59:20 +0800 Subject: [PATCH 10/12] auto-alex-v2 updated --- clarity/contracts/auto-token/auto-alex-v2.clar | 2 +- clarity/tests/auto-alex-v3_test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v2.clar b/clarity/contracts/auto-token/auto-alex-v2.clar index 49c761d7..e1265738 100644 --- a/clarity/contracts/auto-token/auto-alex-v2.clar +++ b/clarity/contracts/auto-token/auto-alex-v2.clar @@ -423,7 +423,7 @@ (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) ) (asserts! (> current-cycle reward-cycle) ERR-REWARD-CYCLE-NOT-COMPLETED) - (asserts! (>= balance bounty) ERR-INSUFFICIENT-BALANCE) + (asserts! (> balance bounty) ERR-INSUFFICIENT-BALANCE) (asserts! (>= (var-get end-cycle) current-cycle) ERR-STAKING-NOT-AVAILABLE) (let diff --git a/clarity/tests/auto-alex-v3_test.ts b/clarity/tests/auto-alex-v3_test.ts index a9ab0a94..e23811e1 100644 --- a/clarity/tests/auto-alex-v3_test.ts +++ b/clarity/tests/auto-alex-v3_test.ts @@ -96,7 +96,7 @@ Clarinet.test({ chain.mineEmptyBlockUntil(ACTIVATION_BLOCK + 2 * 525); block = chain.mineBlock([ - Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(1), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), + Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(3), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), Tx.contractCall('auto-alex-v3-2-endpoint', 'rebase', [], deployer.address), Tx.contractCall('auto-alex-v3-2-endpoint', 'upgrade', [types.uint(dx)], wallet_1.address), Tx.contractCall('auto-alex-v3-2-endpoint', 'request-redeem', [types.uint(dx)], wallet_1.address), @@ -113,7 +113,7 @@ Clarinet.test({ for (let cycle = 2; cycle < redeem_cycle; cycle++) { chain.mineEmptyBlockUntil(ACTIVATION_BLOCK + (cycle + 1) * 525); block = chain.mineBlock([ - Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(1), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), + Tx.contractCall('age000-governance-token', 'mint-fixed', [types.uint(3), types.principal(deployer.address + ".auto-alex-v2")], deployer.address), Tx.contractCall('auto-alex-v3-2-endpoint', 'rebase', [], deployer.address), Tx.contractCall('auto-alex-v3-2-endpoint', 'finalize-redeem', [types.uint(2)], wallet_2.address), ]); From 57cf8da6278ca2a746074ad825452637578aad41 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 18:29:32 +0800 Subject: [PATCH 11/12] fix: coinfabrik-audit-auto-alex-v3 --- .../auto-token/auto-alex-v3-1-endpoint.clar | 27 +++++++---------- .../auto-token/auto-alex-v3-1-wrapped.clar | 17 ++++------- .../contracts/auto-token/auto-alex-v3-1.clar | 8 ++--- .../auto-token/auto-alex-v3-2-endpoint.clar | 29 +++++++------------ .../auto-token/auto-alex-v3-2-wrapped.clar | 19 ++++-------- .../contracts/auto-token/auto-alex-v3-2.clar | 8 ++--- 6 files changed, 38 insertions(+), 70 deletions(-) diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar index 92d43ccf..dc54e31f 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-endpoint.clar @@ -16,10 +16,9 @@ (define-constant ONE_8 u100000000) (define-constant REWARD-CYCLE-INDEXES (list 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 u32)) (define-data-var contract-owner principal tx-sender) -(define-map approved-contracts principal bool) (define-data-var create-paused bool true) (define-data-var redeem-paused bool true) -(define-constant redeem-delay-cycles u32) +(define-constant max-cycles u32) (define-read-only (get-contract-owner) (var-get contract-owner)) (define-read-only (get-pending) @@ -66,10 +65,6 @@ (begin (try! (check-is-owner)) (ok (var-set contract-owner owner)))) -(define-public (set-approved-contract (owner principal) (approved bool)) - (begin - (try! (check-is-owner)) - (ok (map-set approved-contracts owner approved)))) (define-public (pause-create (pause bool)) (begin (try! (check-is-owner)) @@ -109,7 +104,7 @@ (sender tx-sender)) (asserts! (> intrinsic-dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) - (asserts! (< end-cycle-v2 (+ current-cycle u32)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly + (asserts! (< end-cycle-v2 (+ current-cycle max-cycles)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly ;; transfer dx to contract to stake for max cycles (try! (contract-call? .auto-alex-v2 transfer-fixed dx sender .auto-alex-v3-1 none)) (and (< end-cycle-v2 current-cycle) (begin (as-contract (try! (reduce-position-v2))) true)) @@ -120,7 +115,7 @@ (define-public (claim-and-mint (reward-cycles (list 200 uint))) (let ( (claimed (unwrap-panic (contract-call? .staking-helper claim-staking-reward .age000-governance-token reward-cycles)))) - (try! (add-to-position (fold sum-claimed claimed u0))) + (try! (add-to-position (try! (fold sum-claimed claimed (ok u0))))) (ok claimed))) (define-public (claim-and-stake (reward-cycle uint)) (let ( @@ -142,7 +137,7 @@ (define-public (request-redeem (amount uint)) (let ( (current-cycle (unwrap! (get-reward-cycle block-height) ERR-STAKING-NOT-AVAILABLE)) - (redeem-cycle (+ current-cycle redeem-delay-cycles)) + (redeem-cycle (+ current-cycle max-cycles)) (request-details { requested-by: tx-sender, shares: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (try! (contract-call? .auto-alex-v3-1 transfer-fixed amount tx-sender .auto-alex-v3-1 none)) @@ -155,7 +150,7 @@ (redeem-cycle (get redeem-cycle request-details)) (check-claim-and-stake (and (not (is-cycle-staked redeem-cycle)) (try! (claim-and-stake redeem-cycle)))) (current-cycle (try! (rebase))) - (redeem-tokens (div-down (mul-down (get shares request-details) (get-redeem-tokens-per-cycle-or-default redeem-cycle)) (get-redeem-shares-per-cycle-or-default redeem-cycle))) + (redeem-tokens (/ (* (get shares request-details) (get-redeem-tokens-per-cycle-or-default redeem-cycle)) (get-redeem-shares-per-cycle-or-default redeem-cycle))) (updated-request-details (merge request-details { status: (get-finalized) }))) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (asserts! (is-eq (get-pending) (get status request-details)) ERR-REQUEST-FINALIZED-OR-REVOKED) @@ -178,12 +173,10 @@ (as-contract (contract-call? .auto-alex-v3-1-registry set-redeem-request request-id updated-request-details)))) (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) -(define-private (check-is-approved) - (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) -(define-private (sum-claimed (claimed-response (response (tuple (entitled-token uint) (to-return uint)) uint)) (sum-so-far uint)) - (match claimed-response - claimed (+ sum-so-far (get to-return claimed) (get entitled-token claimed)) - err sum-so-far)) +(define-private (sum-claimed (claimed-response (response (tuple (entitled-token uint) (to-return uint)) uint)) (prior (response uint uint))) + (match prior + ok-value (match claimed-response claimed (ok (+ ok-value (get to-return claimed) (get entitled-token claimed))) err (err err)) + err-value (err err-value))) (define-private (stake-tokens-iter (cycles-to-stake uint) (previous-response (response { current-cycle: uint, remaining: uint } uint))) (match previous-response ok-value @@ -191,7 +184,7 @@ (reward-cycle (+ (get current-cycle ok-value) cycles-to-stake)) (redeeming (get-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) (returning (get to-return (get-staker-at-cycle reward-cycle))) - (staking (if (is-eq cycles-to-stake u32) + (staking (if (is-eq cycles-to-stake max-cycles) (get remaining ok-value) (if (> returning redeeming) u0 diff --git a/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar b/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar index bb912898..6cbbdd2a 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1-wrapped.clar @@ -2,24 +2,19 @@ (define-constant ERR-NOT-AUTHORIZED (err u1000)) (define-constant ONE_8 u100000000) (define-data-var contract-owner principal tx-sender) -(define-map approved-contracts principal bool) (define-data-var token-name (string-ascii 32) "Wrapped Auto ALEX v3") -(define-data-var token-symbol (string-ascii 10) "watALEXv3") +(define-data-var token-symbol (string-ascii 32) "watALEXv3") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3-wrapped.json")) (define-data-var token-decimals uint u8) (define-public (set-contract-owner (owner principal)) (begin (try! (check-is-owner)) (ok (var-set contract-owner owner)))) -(define-public (set-approved-contract (owner principal) (approved bool)) - (begin - (try! (check-is-owner)) - (ok (map-set approved-contracts owner approved)))) (define-public (set-name (new-name (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-name new-name)))) -(define-public (set-symbol (new-symbol (string-ascii 10))) +(define-public (set-symbol (new-symbol (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) @@ -31,13 +26,13 @@ (begin (try! (check-is-owner)) (ok (var-set token-uri new-uri)))) -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (begin (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) (try! (ft-transfer? auto-alex-v3-wrapped amount sender recipient)) (print { type: "transfer", amount: amount, sender: sender, recipient: recipient, memo: memo }) (ok true))) -(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (transfer amount sender recipient memo)) (define-public (mint (amount uint) (recipient principal)) (begin @@ -73,7 +68,7 @@ (define-read-only (get-total-shares) (contract-call? .auto-alex-v3-1 get-balance (as-contract tx-sender))) (define-read-only (get-tokens-to-shares (amount uint)) - (if (is-eq (get-total-supply) (ok u0)) + (if (is-eq (get-total-shares) (ok u0)) amount (/ (* amount (unwrap-panic (get-total-supply))) (unwrap-panic (get-total-shares))))) (define-read-only (get-shares-to-tokens (shares uint)) @@ -82,6 +77,4 @@ (/ (* shares (unwrap-panic (get-total-shares))) (unwrap-panic (get-total-supply))))) (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) -(define-private (check-is-approved) - (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) (contract-call? .alex-vault-v1-1 set-approved-token .auto-alex-v3-1-wrapped true) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-1.clar b/clarity/contracts/auto-token/auto-alex-v3-1.clar index a8c5fab2..9ddad616 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-1.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-1.clar @@ -5,7 +5,7 @@ (define-data-var contract-owner principal tx-sender) (define-map approved-contracts principal bool) (define-data-var token-name (string-ascii 32) "Auto ALEX v3") -(define-data-var token-symbol (string-ascii 10) "atALEXv3") +(define-data-var token-symbol (string-ascii 32) "atALEXv3") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3.json")) (define-data-var token-decimals uint u8) (define-data-var reserve uint u0) @@ -21,7 +21,7 @@ (begin (try! (check-is-owner)) (ok (var-set token-name new-name)))) -(define-public (set-symbol (new-symbol (string-ascii 10))) +(define-public (set-symbol (new-symbol (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) @@ -100,7 +100,7 @@ (if (is-eq (get-total-shares) (ok u0)) shares (/ (* shares (unwrap-panic (get-reserve))) (unwrap-panic (get-total-shares))))) -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (shares (get-tokens-to-shares amount))) (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) @@ -108,7 +108,7 @@ (match memo to-print (print to-print) 0x) (print { notification: "transfer", payload: { amount: amount, shares: shares, sender: sender, recipient: recipient } }) (ok true))) -(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (transfer amount sender recipient memo)) (define-private (burn-many-iter (item {amount: uint, sender: principal})) (burn (get amount item) (get sender item))) diff --git a/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar b/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar index 977e7e90..67478755 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-2-endpoint.clar @@ -22,14 +22,13 @@ ;; (define-data-var contract-owner principal tx-sender) -(define-map approved-contracts principal bool) (define-data-var create-paused bool true) (define-data-var redeem-paused bool true) ;; __IF_MAINNET__ -(define-constant redeem-delay-cycles u32) -;; (define-constant redeem-delay-cycles u2) +(define-constant max-cycles u32) +;; (define-constant max-cycles u2) ;; __ENDIF__ ;; read-only calls @@ -98,11 +97,6 @@ (try! (check-is-owner)) (ok (var-set contract-owner owner)))) -(define-public (set-approved-contract (owner principal) (approved bool)) - (begin - (try! (check-is-owner)) - (ok (map-set approved-contracts owner approved)))) - (define-public (pause-create (pause bool)) (begin (try! (check-is-owner)) @@ -151,7 +145,7 @@ (define-public (claim-and-mint (reward-cycles (list 200 uint))) (let ( (claimed (unwrap-panic (contract-call? .staking-helper claim-staking-reward .age000-governance-token reward-cycles)))) - (try! (add-to-position (fold sum-claimed claimed u0))) + (try! (add-to-position (try! (fold sum-claimed claimed (ok u0))))) (ok claimed))) ;; @desc add to position @@ -177,7 +171,7 @@ (sender tx-sender)) (asserts! (> intrinsic-dx u0) ERR-INVALID-LIQUIDITY) (asserts! (not (is-create-paused)) ERR-PAUSED) - (asserts! (< end-cycle-v2 (+ current-cycle u32)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly + (asserts! (< end-cycle-v2 (+ current-cycle max-cycles)) ERR-END-CYCLE-V2) ;; auto-alex-v2 is not configured correctly (try! (contract-call? .auto-alex-v2 transfer-fixed dx sender .auto-alex-v3-2 none)) (and (< end-cycle-v2 current-cycle) (begin (as-contract (try! (reduce-position-v2))) true)) (as-contract (try! (contract-call? .auto-alex-v3-2 mint-fixed intrinsic-dx sender))) @@ -187,7 +181,7 @@ (define-public (request-redeem (amount uint)) (let ( (current-cycle (try! (rebase))) - (redeem-cycle (+ current-cycle redeem-delay-cycles)) + (redeem-cycle (+ current-cycle max-cycles)) (request-details { requested-by: tx-sender, amount: amount, redeem-cycle: redeem-cycle, status: (get-pending) })) (asserts! (not (is-redeem-paused)) ERR-PAUSED) (try! (contract-call? .auto-alex-v3-2 transfer-fixed amount tx-sender .auto-alex-v3-2 none)) @@ -235,13 +229,10 @@ (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) -(define-private (check-is-approved) - (ok (asserts! (or (default-to false (map-get? approved-contracts tx-sender)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED))) - -(define-private (sum-claimed (claimed-response (response (tuple (entitled-token uint) (to-return uint)) uint)) (sum-so-far uint)) - (match claimed-response - claimed (+ sum-so-far (get to-return claimed) (get entitled-token claimed)) - err sum-so-far)) +(define-private (sum-claimed (claimed-response (response (tuple (entitled-token uint) (to-return uint)) uint)) (prior (response uint uint))) + (match prior + ok-value (match claimed-response claimed (ok (+ ok-value (get to-return claimed) (get entitled-token claimed))) err (err err)) + err-value (err err-value))) (define-private (stake-tokens-iter (cycles-to-stake uint) (previous-response (response { current-cycle: uint, remaining: uint } uint))) (match previous-response @@ -250,7 +241,7 @@ (reward-cycle (+ (get current-cycle ok-value) cycles-to-stake)) (redeeming (get-shares-to-tokens (get-redeem-shares-per-cycle-or-default reward-cycle))) (returning (get to-return (get-staker-at-cycle reward-cycle))) - (staking (if (is-eq cycles-to-stake u32) + (staking (if (is-eq cycles-to-stake max-cycles) (get remaining ok-value) (if (> returning redeeming) u0 diff --git a/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar b/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar index 79273b6f..c20450a1 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-2-wrapped.clar @@ -6,10 +6,9 @@ (define-constant token-decimals u8) (define-data-var contract-owner principal tx-sender) -(define-map approved-contracts principal bool) (define-data-var token-name (string-ascii 32) "vLiALEX") -(define-data-var token-symbol (string-ascii 10) "vLiALEX") +(define-data-var token-symbol (string-ascii 32) "vLiALEX") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3-wrapped.json")) ;; governance functions @@ -19,17 +18,12 @@ (try! (check-is-owner)) (ok (var-set contract-owner owner)))) -(define-public (set-approved-contract (owner principal) (approved bool)) - (begin - (try! (check-is-owner)) - (ok (map-set approved-contracts owner approved)))) - (define-public (set-name (new-name (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-name new-name)))) -(define-public (set-symbol (new-symbol (string-ascii 10))) +(define-public (set-symbol (new-symbol (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) @@ -41,14 +35,14 @@ ;; public functions -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (begin (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) (try! (ft-transfer? auto-alex-v3-wrapped amount sender recipient)) (print { type: "transfer", amount: amount, sender: sender, recipient: recipient, memo: memo }) (ok true))) -(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (transfer amount sender recipient memo)) (define-public (mint (amount uint) (recipient principal)) @@ -105,7 +99,7 @@ (contract-call? .auto-alex-v3-2 get-balance (as-contract tx-sender))) (define-read-only (get-tokens-to-shares (amount uint)) - (if (is-eq (get-total-supply) (ok u0)) + (if (is-eq (get-total-shares) (ok u0)) amount (/ (* amount (unwrap-panic (get-total-supply))) (unwrap-panic (get-total-shares))))) @@ -119,7 +113,4 @@ (define-private (check-is-owner) (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED))) -(define-private (check-is-approved) - (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED))) - (contract-call? .alex-vault-v1-1 set-approved-token .auto-alex-v3-2-wrapped true) \ No newline at end of file diff --git a/clarity/contracts/auto-token/auto-alex-v3-2.clar b/clarity/contracts/auto-token/auto-alex-v3-2.clar index 6eb6ff76..73d1b9c2 100644 --- a/clarity/contracts/auto-token/auto-alex-v3-2.clar +++ b/clarity/contracts/auto-token/auto-alex-v3-2.clar @@ -11,7 +11,7 @@ (define-map approved-contracts principal bool) (define-data-var token-name (string-ascii 32) "LiALEX") -(define-data-var token-symbol (string-ascii 10) "LiALEX") +(define-data-var token-symbol (string-ascii 32) "LiALEX") (define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/auto-alex-v3.json")) (define-data-var reserve uint u0) @@ -33,7 +33,7 @@ (try! (check-is-owner)) (ok (var-set token-name new-name)))) -(define-public (set-symbol (new-symbol (string-ascii 10))) +(define-public (set-symbol (new-symbol (string-ascii 32))) (begin (try! (check-is-owner)) (ok (var-set token-symbol new-symbol)))) @@ -138,7 +138,7 @@ ;; public calls -(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (let ( (shares (get-tokens-to-shares amount))) (asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR-NOT-AUTHORIZED) @@ -147,7 +147,7 @@ (print { notification: "transfer", payload: { amount: amount, shares: shares, sender: sender, recipient: recipient } }) (ok true))) -(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 2048)))) +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (transfer amount sender recipient memo)) ;; private functions From 7143851fb230a750992fbf2f79863235edc42f83 Mon Sep 17 00:00:00 2001 From: fiftyeightandeight <443225+fiftyeightandeight@users.noreply.github.com> Date: Mon, 6 May 2024 18:33:46 +0800 Subject: [PATCH 12/12] test updated --- clarity/tests/alex-launchpad-v1-1_test.ts | 6 +++++- clarity/tests/alex-launchpad-v1-2_test.ts | 6 +++++- clarity/tests/alex-launchpad-v1-4_test.ts | 6 +++++- clarity/tests/alex-launchpad-v1-5_test.ts | 10 ++++++++-- clarity/tests/alex-launchpad-v1-6_test.ts | 10 ++++++++-- clarity/tests/alex-launchpad-v1-7_test.ts | 10 ++++++++-- clarity/tests/alex-lottery_test.ts | 2 ++ clarity/tests/launchpad-with-pool-lock-v1-4_test.ts | 2 ++ clarity/tests/launchpad-with-pool-lock-v1-6_test.ts | 2 ++ 9 files changed, 45 insertions(+), 9 deletions(-) diff --git a/clarity/tests/alex-launchpad-v1-1_test.ts b/clarity/tests/alex-launchpad-v1-1_test.ts index f4db3b62..0fc59adf 100644 --- a/clarity/tests/alex-launchpad-v1-1_test.ts +++ b/clarity/tests/alex-launchpad-v1-1_test.ts @@ -511,7 +511,9 @@ Clarinet.test({ const { idoId } = preparation; - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -742,7 +744,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/alex-launchpad-v1-2_test.ts b/clarity/tests/alex-launchpad-v1-2_test.ts index 5a34d748..4fd237a9 100644 --- a/clarity/tests/alex-launchpad-v1-2_test.ts +++ b/clarity/tests/alex-launchpad-v1-2_test.ts @@ -514,7 +514,9 @@ Clarinet.test({ const { idoId } = preparation; - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -745,7 +747,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/alex-launchpad-v1-4_test.ts b/clarity/tests/alex-launchpad-v1-4_test.ts index 5ef180f1..430f8338 100644 --- a/clarity/tests/alex-launchpad-v1-4_test.ts +++ b/clarity/tests/alex-launchpad-v1-4_test.ts @@ -515,7 +515,9 @@ Clarinet.test({ const { idoId } = preparation; - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -746,7 +748,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/alex-launchpad-v1-5_test.ts b/clarity/tests/alex-launchpad-v1-5_test.ts index 34651f92..ab459cf4 100644 --- a/clarity/tests/alex-launchpad-v1-5_test.ts +++ b/clarity/tests/alex-launchpad-v1-5_test.ts @@ -515,7 +515,9 @@ Clarinet.test({ const { idoId } = preparation; - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -765,7 +767,9 @@ Clarinet.test({ ]); whitelisted.receipts.map(({ result }) => result.expectOk()); - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -863,7 +867,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/alex-launchpad-v1-6_test.ts b/clarity/tests/alex-launchpad-v1-6_test.ts index e31e3172..d25e57d3 100644 --- a/clarity/tests/alex-launchpad-v1-6_test.ts +++ b/clarity/tests/alex-launchpad-v1-6_test.ts @@ -515,7 +515,9 @@ Clarinet.test({ const { idoId } = preparation; - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -765,7 +767,9 @@ Clarinet.test({ ]); whitelisted.receipts.map(({ result }) => result.expectOk()); - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -863,7 +867,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/alex-launchpad-v1-7_test.ts b/clarity/tests/alex-launchpad-v1-7_test.ts index 7c7aa0b2..c04bfdbd 100644 --- a/clarity/tests/alex-launchpad-v1-7_test.ts +++ b/clarity/tests/alex-launchpad-v1-7_test.ts @@ -518,7 +518,9 @@ Clarinet.test({ const { idoId } = preparation; - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -768,7 +770,9 @@ Clarinet.test({ ]); whitelisted.receipts.map(({ result }) => result.expectOk()); - chain.mineEmptyBlockUntil(registrationStartHeight); + if(chain.blockHeight < registrationStartHeight){ + chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( @@ -866,7 +870,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/alex-lottery_test.ts b/clarity/tests/alex-lottery_test.ts index dfbd9129..d1587353 100644 --- a/clarity/tests/alex-lottery_test.ts +++ b/clarity/tests/alex-lottery_test.ts @@ -101,7 +101,9 @@ Clarinet.test({ const { lotteryId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/launchpad-with-pool-lock-v1-4_test.ts b/clarity/tests/launchpad-with-pool-lock-v1-4_test.ts index 532546f4..c41f6781 100644 --- a/clarity/tests/launchpad-with-pool-lock-v1-4_test.ts +++ b/clarity/tests/launchpad-with-pool-lock-v1-4_test.ts @@ -117,7 +117,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall( diff --git a/clarity/tests/launchpad-with-pool-lock-v1-6_test.ts b/clarity/tests/launchpad-with-pool-lock-v1-6_test.ts index d51fa2bb..cc26b104 100644 --- a/clarity/tests/launchpad-with-pool-lock-v1-6_test.ts +++ b/clarity/tests/launchpad-with-pool-lock-v1-6_test.ts @@ -117,7 +117,9 @@ Clarinet.test({ const { idoId } = preparation; + if(chain.blockHeight < registrationStartHeight){ chain.mineEmptyBlockUntil(registrationStartHeight); + } const registrations = chain.mineBlock( ticketRecipients.map((entry) => Tx.contractCall(