Merge pull request #5 from alexgo-io/feat/rebase-swap-pool

feat: rebase-swap-pool
This commit is contained in:
fiftyeightandeight
2024-02-13 18:28:18 +04:00
committed by GitHub
4 changed files with 73 additions and 31 deletions

View File

@@ -53,7 +53,7 @@
(define-read-only (validate-burn-request (request-id uint))
(let (
(request-details (try! (contract-call? .lqstx-mint-registry get-burn-request-or-fail request-id)))
(vaulted-amount (contract-call? .token-wlqstx convert-to-tokens (get amount request-details)))
(vaulted-amount (unwrap-panic (contract-call? .token-wlqstx get-shares-to-tokens (get amount request-details))))
(balance (stx-account .vault)))
(asserts! (>= (* (get unlocked balance) u100) vaulted-amount) err-request-pending)
(asserts! (is-eq (get-pending) (get status request-details)) err-request-finalized-or-revoked)
@@ -125,7 +125,7 @@
(asserts! (is-eq (get-pending) (get status request-details)) err-request-finalized-or-revoked)
(asserts! (is-eq tx-sender (get requested-by request-details)) err-unauthorised)
(as-contract (try! (contract-call? .token-wlqstx burn-fixed (get amount request-details) tx-sender)))
(as-contract (contract-call? .token-lqstx transfer-fixed (contract-call? .token-wlqstx convert-to-tokens (get amount request-details)) tx-sender (get requested-by request-details) none))))
(as-contract (contract-call? .token-lqstx transfer-fixed (unwrap-panic (contract-call? .token-wlqstx get-shares-to-tokens (get amount request-details))) tx-sender (get requested-by request-details) none))))
(define-public (callback (extension principal) (payload (buff 34)))
(ok true))

View File

@@ -53,22 +53,22 @@
(define-public (dao-mint (amount uint) (recipient principal))
(begin
(try! (is-dao-or-extension))
(ft-mint? lqstx (convert-to-shares amount) recipient)))
(ft-mint? lqstx (unwrap-panic (get-tokens-to-shares amount)) recipient)))
(define-public (dao-mint-fixed (amount uint) (recipient principal))
(begin
(try! (is-dao-or-extension))
(ft-mint? lqstx (fixed-to-decimals (convert-to-shares amount)) recipient)))
(ft-mint? lqstx (fixed-to-decimals (unwrap-panic (get-tokens-to-shares amount))) recipient)))
(define-public (dao-burn-fixed (amount uint) (sender principal))
(begin
(try! (is-dao-or-extension))
(ft-burn? lqstx (fixed-to-decimals (convert-to-shares amount)) sender)))
(ft-burn? lqstx (fixed-to-decimals (unwrap-panic (get-tokens-to-shares amount))) sender)))
(define-public (dao-burn (amount uint) (sender principal))
(begin
(try! (is-dao-or-extension))
(ft-burn? lqstx (convert-to-shares amount) sender)))
(ft-burn? lqstx (unwrap-panic (get-tokens-to-shares amount)) sender)))
(define-public (dao-burn-fixed-many (senders (list 200 {amount: uint, sender: principal})))
(begin
@@ -91,10 +91,10 @@
(ok (var-get token-decimals)))
(define-read-only (get-balance (who principal))
(ok (convert-to-tokens (get-shares who))))
(get-shares-to-tokens (get-shares who)))
(define-read-only (get-total-supply)
(ok (convert-to-tokens (get-total-shares))))
(get-shares-to-tokens (get-total-shares)))
(define-read-only (get-token-uri)
(ok (var-get token-uri)))
@@ -105,14 +105,14 @@
(define-read-only (get-total-shares)
(ft-get-supply lqstx))
(define-read-only (convert-to-shares (amount uint))
(div-down amount (var-get reward-multiplier)))
(define-read-only (get-tokens-to-shares (amount uint))
(ok (div-down amount (var-get reward-multiplier))))
(define-read-only (convert-to-tokens (shares uint))
(mul-down shares (var-get reward-multiplier)))
(define-read-only (get-shares-to-tokens (shares uint))
(ok (mul-down shares (var-get reward-multiplier))))
(define-read-only (get-reward-multiplier)
(var-get reward-multiplier))
(ok (var-get reward-multiplier)))
(define-read-only (get-total-supply-fixed)
(ok (decimals-to-fixed (unwrap-panic (get-total-supply)))))
@@ -128,10 +128,10 @@
(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
(begin
(asserts! (is-eq sender tx-sender) err-unauthorised)
(try! (ft-transfer? lqstx (fixed-to-decimals (convert-to-shares amount)) sender recipient))
(try! (ft-transfer? lqstx (fixed-to-decimals (unwrap-panic (get-tokens-to-shares amount))) sender recipient))
(match memo to-print (print to-print) 0x)
(ok true)))
(ok true)))
;; private functions
(define-private (dao-burn-fixed-many-iter (item {amount: uint, sender: principal}))

View File

@@ -3,7 +3,8 @@
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)
(define-constant err-unauthorised (err u3000))
(define-constant ONE_8 u100000000)
(define-constant one-8 u100000000)
(define-constant base-token .token-lqstx)
(define-fungible-token wlqstx)
@@ -52,7 +53,7 @@
(define-public (mint-fixed (amount uint) (recipient principal))
(let
(
(shares (convert-to-shares amount))
(shares (unwrap-panic (get-tokens-to-shares amount)))
)
(asserts! (is-eq recipient tx-sender) err-unauthorised)
(try! (contract-call? .token-lqstx transfer-fixed amount recipient (as-contract tx-sender) none))
@@ -64,7 +65,7 @@
(define-public (burn-fixed (amount uint) (sender principal))
(let
(
(vaulted-amount (convert-to-tokens amount))
(vaulted-amount (unwrap-panic (get-shares-to-tokens amount)))
)
(asserts! (is-eq sender tx-sender) err-unauthorised)
(try! (ft-burn? wlqstx (fixed-to-decimals amount) sender))
@@ -104,34 +105,37 @@
(ok (var-get token-uri)))
(define-read-only (get-vaulted-balance-fixed (who principal))
(ok (convert-to-tokens (unwrap-panic (get-balance-fixed who)))))
(get-shares-to-tokens (unwrap-panic (get-balance-fixed who))))
(define-read-only (get-total-vaulted-balance-fixed)
(ok (unwrap-panic (contract-call? .token-lqstx get-balance-fixed (as-contract tx-sender)))))
(define-read-only (convert-to-shares (amount uint))
(define-read-only (get-tokens-to-shares (amount uint))
(let
(
(total-supply (unwrap-panic (get-total-supply-fixed)))
)
(if (is-eq total-supply u0)
(ok (if (is-eq total-supply u0)
amount
(div-down (mul-down amount total-supply) (unwrap-panic (get-total-vaulted-balance-fixed)))
)
))
)
)
(define-read-only (convert-to-tokens (shares uint))
(define-read-only (get-shares-to-tokens (shares uint))
(let
(
(total-supply (unwrap-panic (get-total-supply-fixed)))
)
(if (is-eq total-supply u0)
(ok (if (is-eq total-supply u0)
shares
(div-down (mul-down shares (unwrap-panic (get-total-vaulted-balance-fixed))) total-supply)
)
))
)
)
)
(define-read-only (get-reward-multiplier)
(contract-call? .token-lqstx get-reward-multiplier))
;; private functions
@@ -139,15 +143,15 @@
(pow u10 (unwrap-panic (get-decimals))))
(define-private (decimals-to-fixed (amount uint))
(/ (* amount ONE_8) (pow-decimals)))
(/ (* amount one-8) (pow-decimals)))
(define-private (fixed-to-decimals (amount uint))
(/ (* amount (pow-decimals)) ONE_8))
(/ (* amount (pow-decimals)) one-8))
(define-private (mul-down (a uint) (b uint))
(/ (* a b) ONE_8))
(/ (* a b) one-8))
(define-private (div-down (a uint) (b uint))
(if (is-eq a u0)
u0
(/ (* a ONE_8) b)))
(/ (* a one-8) b)))

View File

@@ -0,0 +1,38 @@
(define-trait rebase-sip-010-trait
(
;; Transfer from the caller to a new principal
(transfer (uint principal principal (optional (buff 34))) (response bool uint))
;; the human readable name of the token
(get-name () (response (string-ascii 32) uint))
;; the ticker symbol, or empty if none
(get-symbol () (response (string-ascii 32) uint))
;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token
(get-decimals () (response uint uint))
;; the balance of the passed principal
(get-balance (principal) (response uint uint))
;; the current total supply (which does not need to be a constant)
(get-total-supply () (response uint uint))
;; an optional URI that represents metadata of this token
(get-token-uri () (response (optional (string-utf8 256)) uint))
;; helper functions for 8-digit fixed notation
(transfer-fixed (uint principal principal (optional (buff 34))) (response bool uint))
(get-balance-fixed (principal) (response uint uint))
(get-total-supply-fixed () (response uint uint))
(mint (uint principal) (response bool uint))
(burn (uint principal) (response bool uint))
(mint-fixed (uint principal) (response bool uint))
(burn-fixed (uint principal) (response bool uint))
(get-reward-multiplier () (response uint uint))
(get-tokens-to-shares (uint) (response uint uint))
(get-shares-to-tokens (uint) (response uint uint))
)
)