(define-fungible-token auto-alex-v3-wrapped)

(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-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))))

(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 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 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 (var-get token-decimals)))

(define-read-only (get-balance (who principal))
	(ok (ft-get-balance auto-alex-v3-wrapped who)))

(define-read-only (get-total-supply)
	(ok (ft-get-supply auto-alex-v3-wrapped)))

(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 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)))
