;; @contract Wrapped STX SIP-010
;; @version 1

(impl-trait .sip010-ft-trait.sip010-ft-trait)

(define-fungible-token WrappedStacks)
(define-constant CONTRACT_OWNER tx-sender)

;; ------------------------------------------
;; Constants
;; ------------------------------------------

(define-constant  ERR-NOT-AUTHORIZED u1403001)

;; ------------------------------------------
;; Variables
;; ------------------------------------------

(define-data-var token-uri (string-utf8 256) u"")

;; ---------------------------------------------------------
;; SIP-10 Functions
;; ---------------------------------------------------------

(define-read-only (get-total-supply)
  (ok (ft-get-supply WrappedStacks))
)

(define-read-only (get-name)
  (ok "Wrapped Stacks Token")
)

(define-read-only (get-symbol)
  (ok "wSTX")
)

(define-read-only (get-decimals)
  (ok u18)
)

(define-read-only (get-balance (account principal))
  (ok (ft-get-balance WrappedStacks account))
)

(define-public (set-token-uri (value (string-utf8 256)))
  (if (is-eq tx-sender CONTRACT_OWNER)
    (ok (var-set token-uri value))
    (err  ERR-NOT-AUTHORIZED)
  )
)

(define-read-only (get-token-uri)
  (ok (some (var-get token-uri)))
)

(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
  (begin
    (asserts! (is-eq tx-sender sender) (err  ERR-NOT-AUTHORIZED))

    (match (ft-transfer? WrappedStacks amount sender recipient)
      response (begin
        (print memo)
        (ok response)
      )
      error (err error)
    )
  )
)


;; ---------------------------------------------------------
;; Wrap / Unwrap
;; ---------------------------------------------------------

(define-public (wrap (amount uint))
  (begin
    (try! (stx-transfer? amount tx-sender (as-contract tx-sender)))
    (ft-mint? WrappedStacks amount tx-sender)
  )
)

(define-public (unwrap (amount uint))
  (let (
    (recipient tx-sender)
  )
    (try! (as-contract (stx-transfer? amount (as-contract tx-sender) recipient)))
    (ft-burn? WrappedStacks amount recipient)
  )
)

(define-map allowances
  {spender: principal, 
  owner: principal
  }
  {allowance: uint}
)

(define-private (transfer-to (amount uint) (sender principal) (recipient principal) )
  (match (ft-transfer? WrappedStacks amount sender recipient)
    result (ok true)
    error (err false))
)


;; Gets the amount of tokens that an owner allowed to a spender.
(define-private (allowance-of (spender principal) (owner principal))
  (begin
  (default-to u0
    (get allowance
       (map-get? allowances {spender: spender, owner: owner}))))
)

;; Decrease allowance of a specified spender.
(define-private (decrease-allowance (amount uint) (spender principal) (owner principal))
  (let (
      (allowance (allowance-of spender owner)))
    (if (or (> amount allowance) (<= amount u0))
      true
      (begin
        (map-set allowances
          {spender: spender, owner: owner}
          {allowance: (- allowance amount)})
        true))))

;; Internal - Increase allowance of a specified spender.
(define-private (increase-allowance (amount uint) (spender principal) (owner principal))
  (let ((allowance (allowance-of spender owner)))
    (if (<= amount u0)
      false
      (begin
         (map-set allowances
          {spender: spender, owner: owner}
          {allowance: (+ allowance amount)})
        true))))

;; Public functions


;; Transfers tokens to a specified principal, performed by a spender
(define-public (transfer-from (amount uint) (owner principal) (recipient principal) )
  (let ((allowance (allowance-of tx-sender owner)))
    (begin
      (if (or (> amount allowance) (<= amount u0))
        (err false)
        (if (and
            (unwrap! (transfer-to amount owner recipient) (err false))
            (decrease-allowance amount tx-sender owner))
        (ok true)
        (err false)))))
)

;; Update the allowance for a given spender
(define-public (approve (amount uint) (spender principal) )
  (if (and (> amount u0)
           (print (increase-allowance amount spender tx-sender )))
      (ok amount)
      (err false)))

;; Revoke a given spender
(define-public (revoke (spender principal))
  (let ((allowance (allowance-of spender tx-sender)))
    (if (and (> allowance u0)
             (decrease-allowance allowance spender tx-sender))
        (ok 0)
        (err false))))

(define-public (balance-of (owner principal))
  (begin
      (print owner)
      (ok (ft-get-balance WrappedStacks owner))
  )
)

;; (begin (wrap u1000000))