;; Title: VELAR-aeUSDC DEX Wrapper
;; Description: Wraps UniV2-style DEX with Dexterity interface
;; Traits
(impl-trait 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.dexterity-traits-v0.liquidity-pool-trait)
(impl-trait 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.charisma-traits-v1.sip010-ft-trait)
;; Constants
(define-constant ERR_INVALID_OPERATION (err u400))
(define-constant ERR_UNAUTHORIZED (err u403))
(define-constant TOKEN-NAME "Swords to Plowshares")
(define-constant TOKEN-SYMBOL "STP")
(define-constant TOKEN-URI (some u"https://charisma.rocks/api/v0/metadata/SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.swords-to-plowshares"))
;; Opcodes
(define-constant OP_SWAP_A_TO_B 0x00) ;; Swap token A for B
(define-constant OP_SWAP_B_TO_A 0x01) ;; Swap token B for A
(define-constant OP_LOOKUP_RESERVES 0x04) ;; Read pool reserves
;; SIP-010 Functions
(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
(begin
(asserts! (is-eq tx-sender sender) ERR_UNAUTHORIZED)
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-usdc
transfer amount sender recipient memo)))
(define-read-only (get-name)
(ok TOKEN-NAME))
(define-read-only (get-symbol)
(ok TOKEN-SYMBOL))
(define-read-only (get-decimals)
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-usdc get-decimals))
(define-read-only (get-balance (who principal))
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-usdc get-balance who))
(define-read-only (get-total-supply)
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-usdc get-total-supply))
(define-read-only (get-token-uri)
(ok TOKEN-URI))
;; Core Functions
(define-public (execute (amount uint) (opcode (optional (buff 16))))
(let (
(operation (get-byte opcode u0)))
(if (is-eq operation OP_SWAP_A_TO_B) (swap-a-to-b amount)
(if (is-eq operation OP_SWAP_B_TO_A) (swap-b-to-a amount)
ERR_INVALID_OPERATION))))
(define-read-only (quote (amount uint) (opcode (optional (buff 16))))
(let (
(operation (get-byte opcode u0)))
(if (is-eq operation OP_SWAP_A_TO_B) (ok (get-swap-quote amount opcode))
(if (is-eq operation OP_SWAP_B_TO_A) (ok (get-swap-quote amount opcode))
(if (is-eq operation OP_LOOKUP_RESERVES) (ok (get-reserves-quote))
ERR_INVALID_OPERATION)))))
;; Execute Functions
(define-private (swap-a-to-b (amount uint))
(let (
(sender tx-sender)
(delta (get-swap-quote amount (some OP_SWAP_A_TO_B))))
(try! (contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-path2 do-swap
amount 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-token
'SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to))
(ok delta)))
(define-private (swap-b-to-a (amount uint))
(let (
(sender tx-sender)
(delta (get-swap-quote amount (some OP_SWAP_B_TO_A))))
(try! (contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-path2 do-swap
amount 'SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-token
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to))
(ok delta)))
;; Helper Functions
(define-private (quote-a-to-b (amount uint))
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-path2 amount-out
amount
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-token
'SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc))
(define-private (quote-b-to-a (amount uint))
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-path2 amount-out
amount
'SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-token))
(define-private (get-byte (opcode (optional (buff 16))) (position uint))
(default-to 0x00 (element-at? (default-to 0x00 opcode) position)))
;; Quote Functions
(define-read-only (get-swap-quote (amount uint) (opcode (optional (buff 16))))
(let (
(operation (get-byte opcode u0))
(amt-out (if (is-eq operation OP_SWAP_A_TO_B)
(quote-a-to-b amount)
(quote-b-to-a amount))))
{
dx: amount,
dy: amt-out,
dk: u0
}))
(define-read-only (get-reserves-quote)
(let (
(res (unwrap-panic
(contract-call? 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-core lookup-pool
'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.velar-token
'SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA4K.token-aeusdc)))
(pool (get pool res)))
{
dx: (get reserve0 pool),
dy: (get reserve1 pool),
dk: (unwrap-panic (get-total-supply))
}))