Skip to content

Commit

Permalink
Merge pull request #60 from lisalab-io/fix/auto-alex-v3-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftyeightandeight authored Oct 16, 2024
2 parents 31e4efa + a8c27ce commit 0b4c324
Show file tree
Hide file tree
Showing 19 changed files with 1,882 additions and 392 deletions.
22 changes: 21 additions & 1 deletion Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ requirements = [
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.trait-sip-010" },
{ contract_id = "SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.auto-alex-v2" },
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.migrate-legacy-v2-wl" },
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.executor-dao" }
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.executor-dao" },
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-registry" },
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3" },
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-wrapped" },
{ contract_id = "SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-endpoint" }
]

[contracts.lip007]
Expand Down Expand Up @@ -79,6 +83,22 @@ epoch = 2.5
path = "contracts/mocks/tests/alex-boot.clar"
epoch = 2.5

[contracts.lip010]
path = "contracts/proposals/lip010.clar"
epoch = 2.5

[contracts.lip009]
path = "contracts/proposals/lip009.clar"
epoch = 2.5

[contracts.auto-alex-v3-endpoint-v2]
path = "contracts/extensions/auto-alex-v3-endpoint-v2.clar"
epoch = 2.5

[contracts.lip011]
path = "contracts/proposals/lip011.clar"
epoch = 2.5

[contracts.auto-alex-v3-endpoint-v2-01]
path = "contracts/extensions/auto-alex-v3-endpoint-v2-01.clar"
epoch = 2.5
284 changes: 284 additions & 0 deletions contracts/extensions/auto-alex-v3-endpoint-v2-01.clar

Large diffs are not rendered by default.

284 changes: 284 additions & 0 deletions contracts/extensions/auto-alex-v3-endpoint-v2.clar

Large diffs are not rendered by default.

75 changes: 1 addition & 74 deletions contracts/extensions/auto-alex-v3-endpoint.clar
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
;; SPDX-License-Identifier: BUSL-1.1

;; -- autoALEX creation/staking/redemption

;; constants
;;
(define-constant err-unauthorised (err u1000))
(define-constant err-invalid-liquidity (err u2003))
(define-constant err-not-activated (err u2043))
Expand All @@ -15,54 +9,30 @@
(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))

(define-constant PENDING 0x00)
(define-constant FINALIZED 0x01)
(define-constant REVOKED 0x02)

;; data maps and vars
;;

(define-data-var create-paused bool true)
(define-data-var redeem-paused bool true)

(define-constant max-cycles u32)

;; read-only calls

(define-read-only (is-dao-or-extension)
(ok (asserts! (or (is-eq tx-sender 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.lisa-dao) (contract-call? 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.lisa-dao is-extension contract-caller)) err-unauthorised)))

(define-read-only (get-start-cycle)
(contract-call? .auto-alex-v3-registry get-start-cycle))

(define-read-only (is-cycle-staked (reward-cycle uint))
(contract-call? .auto-alex-v3-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-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-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-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))
Expand All @@ -75,27 +45,16 @@
(as-contract (get-staking-reward current-cycle))
(unwrap-panic (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex get-balance .auto-alex-v3))
(if (is-eq auto-alex-v2-bal u0) u0 (mul-down auto-alex-v2-bal (try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.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 (pause-create (pause bool))
(begin
(try! (is-dao-or-extension))
(ok (var-set create-paused pause))))

(define-public (pause-redeem (pause bool))
(begin
(try! (is-dao-or-extension))
(ok (var-set redeem-paused pause))))

;; public functions
;;

(define-public (rebase)
(let (
(current-cycle (unwrap! (get-reward-cycle block-height) err-staking-not-available))
Expand All @@ -104,10 +63,6 @@
(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 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))
Expand All @@ -126,17 +81,11 @@
(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? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.alex-staking-v2 claim-staking-reward-many reward-cycles))))
(try! (add-to-position (try! (fold sum-claimed claimed (ok 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)))
Expand All @@ -149,7 +98,6 @@
(print { notification: "position-added", payload: { new-supply: dx, sender: sender } })
(try! (rebase))
(ok true)))

(define-public (upgrade (dx uint))
(let (
(end-cycle-v2 (get-end-cycle-v2))
Expand All @@ -165,7 +113,6 @@
(print { notification: "upgrade-position-added", payload: { new-supply: intrinsic-dx, sender: sender } })
(try! (rebase))
(ok true)))

(define-public (request-redeem (amount uint))
(let (
(current-cycle (try! (rebase)))
Expand All @@ -178,7 +125,6 @@
(print { notification: "redeem-request", payload: request-details })
(try! (rebase))
(ok request-id)))

(define-public (finalize-redeem (request-id uint))
(let (
(request-details (try! (get-redeem-request-or-fail request-id)))
Expand All @@ -189,14 +135,12 @@
(updated-request-details (merge request-details { status: FINALIZED })))
(asserts! (not (is-redeem-paused)) err-paused)
(asserts! (is-eq PENDING (get status request-details)) err-request-finalized-or-revoked)

(as-contract (try! (contract-call? .auto-alex-v3 burn (get amount request-details) .auto-alex-v3)))
(as-contract (try! (contract-call? .auto-alex-v3 transfer-token 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex tokens (get requested-by request-details))))
(print { notification: "finalize-redeem", payload: updated-request-details })
(as-contract (try! (contract-call? .auto-alex-v3-registry set-redeem-request request-id updated-request-details)))
(try! (rebase))
(ok true)))

(define-public (revoke-redeem (request-id uint))
(let (
(request-details (try! (get-redeem-request-or-fail request-id)))
Expand All @@ -213,15 +157,10 @@
(as-contract (try! (contract-call? .auto-alex-v3-registry set-redeem-request request-id updated-request-details)))
(try! (rebase))
(ok true)))

;; private functions
;;

(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
Expand All @@ -239,39 +178,27 @@
(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 (burn-height uint))
(contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-reserve-pool get-reward-cycle 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token burn-height))

(define-private (get-staking-reward (reward-cycle uint))
(contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.alex-staking-v2 get-staking-reward (get-user-id) reward-cycle))

(define-private (get-staker-at-cycle (reward-cycle uint))
(contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.alex-staking-v2 get-staker-at-cycle-or-default reward-cycle (get-user-id)))

(define-private (get-user-id)
(default-to u0 (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.alex-staking-v2 get-user-id .auto-alex-v3)))

(define-private (stake-tokens (amount-tokens uint) (lock-period uint))
(contract-call? .auto-alex-v3 stake-tokens amount-tokens lock-period))

(define-private (claim-staking-reward (reward-cycle uint))
(contract-call? .auto-alex-v3 claim-staking-reward reward-cycle))

(define-private (reduce-position-v2)
(contract-call? .auto-alex-v3 reduce-position-v2))

(define-private (get-shares-to-tokens (dx uint))
(contract-call? .auto-alex-v3 get-shares-to-tokens dx))

(define-private (claim-and-stake-v2 (reward-cycle uint))
(contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.auto-alex-v2 claim-and-stake reward-cycle))

(define-private (get-end-cycle-v2)
(contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.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)))
(if (is-eq a u0) u0 (/ (* a ONE_8) b)))
3 changes: 2 additions & 1 deletion contracts/mocks/tests/alex-boot.clar
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
{ extension: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.migrate-legacy-v2-wl, enabled: true }
)))

(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.migrate-legacy-v2-wl whitelist (list .auto-alex-v3) (list true)))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.migrate-legacy-v2-wl whitelist (list 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3) (list true)))

(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex mint ONE_8 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex mint ONE_8 'ST2JHG361ZXG51QTKY2NQCVBPPRRE2KZB1HR05NNC))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex mint ONE_8 'ST2NEB84ASENDXKYGJPQW86YXQCEFEX2ZQPG87ND))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex mint ONE_8 'ST2REHHS5J3CERCRBEPMGH7921Q6PYKAADT7JP2VB))
Expand Down
3 changes: 2 additions & 1 deletion contracts/mocks/tests/legacy-boot.clar
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token mint-fixed ONE_8 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-vault add-approved-token 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-reserve-pool add-token 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-reserve-pool set-activation-block 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token u20))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-reserve-pool set-activation-block 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token block-height))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-reserve-pool set-coinbase-amount 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token ONE_8 ONE_8 ONE_8 ONE_8 ONE_8))
;; (try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.alex-reserve-pool set-coinbase-amount 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.age000-governance-token u0 u0 u0 u0 u0))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.auto-alex-v2 set-start-cycle u0))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.auto-alex-v2 set-bounty-in-fixed u1))
(try! (contract-call? 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.auto-alex-v2 pause-create false))
Expand Down
8 changes: 4 additions & 4 deletions contracts/mocks/tests/mock-boot.clar
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
(define-public (execute (sender principal))
(begin
(try! (contract-call? 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.lisa-dao set-extensions (list
{ extension: .auto-alex-v3-endpoint, enabled: true }
{ extension: .auto-alex-v3-endpoint-v2-01, enabled: true }
)))

(try! (contract-call? .auto-alex-v3-registry set-start-cycle u0))
(try! (contract-call? .auto-alex-v3-endpoint pause-create false))
(try! (contract-call? .auto-alex-v3-endpoint pause-redeem false))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-registry set-start-cycle u0))
(try! (contract-call? .auto-alex-v3-endpoint-v2-01 pause-create false))
(try! (contract-call? .auto-alex-v3-endpoint-v2-01 pause-redeem false))

(ok true)
)
Expand Down
11 changes: 11 additions & 0 deletions contracts/proposals/lip010.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

;; SPDX-License-Identifier: BUSL-1.1

(impl-trait 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.proposal-trait.proposal-trait)

(define-public (execute (sender principal))
(begin
(try! (contract-call? 'SM3KNVZS30WM7F89SXKVVFY4SN9RMPZZ9FX929N0V.public-pools-strategy-manager-v2 set-authorised-manager 'SP3TJ5YF08D4FSHM9ZYBBG3X76PW9257YE9SPFWA1 true))
(ok true)
)
)
15 changes: 15 additions & 0 deletions contracts/proposals/lip011.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

;; SPDX-License-Identifier: BUSL-1.1

(impl-trait 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.proposal-trait.proposal-trait)

(define-public (execute (sender principal))
(begin
(try! (contract-call? 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.lisa-dao set-extensions (list
{ extension: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-endpoint, enabled: false }
{ extension: .auto-alex-v3-endpoint-v2, enabled: true } )))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-endpoint pause-create true))
(try! (contract-call? 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.auto-alex-v3-endpoint pause-redeem true))
(try! (contract-call? .auto-alex-v3-endpoint-v2 pause-create false))
(try! (contract-call? .auto-alex-v3-endpoint-v2 pause-redeem false))
(ok true)))
15 changes: 15 additions & 0 deletions contracts/proposals/lip012.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

;; SPDX-License-Identifier: BUSL-1.1

(impl-trait 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.proposal-trait.proposal-trait)

(define-public (execute (sender principal))
(begin
(try! (contract-call? 'SM26NBC8SFHNW4P1Y4DFH27974P56WN86C92HPEHH.lisa-dao set-extensions (list
{ extension: .auto-alex-v3-endpoint-v2, enabled: false }
{ extension: .auto-alex-v3-endpoint-v2-01, enabled: true } )))
(try! (contract-call? .auto-alex-v3-endpoint-v2 pause-create true))
(try! (contract-call? .auto-alex-v3-endpoint-v2 pause-redeem true))
(try! (contract-call? .auto-alex-v3-endpoint-v2-01 pause-create false))
(try! (contract-call? .auto-alex-v3-endpoint-v2-01 pause-redeem false))
(ok true)))
Loading

0 comments on commit 0b4c324

Please sign in to comment.