Source Code


;; router-xyk-arkadiko-v-1-2

(use-trait ft-trait 'SP2AKWJYC7BNY18W1XXKPGP0YVEK63QJG4793Z2D4.sip-010-trait-ft-standard.sip-010-trait)
(use-trait xyk-pool-trait 'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.xyk-pool-trait-v-1-1.xyk-pool-trait)
(use-trait share-fee-to-trait 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1.univ2-share-fee-to-trait.share-fee-to-trait)

(define-constant ERR_INVALID_AMOUNT (err u1002))
(define-constant ERR_MINIMUM_RECEIVED (err u4002))
(define-constant ERR_SWAP_A (err u5001))
(define-constant ERR_SWAP_B (err u5002))

(define-public (get-quote-a
    (amount uint) (provider (optional principal))
    (pool-trait <xyk-pool-trait>)
    (xyk-reversed bool)
    (token-x principal) (token-y principal)
    (diko-reversed bool)
  )
  (let (
    (amount-after-aggregator-fees (try! (get-aggregator-fees provider amount)))
    (quote-a (if (is-eq xyk-reversed false)
                 (try! (contract-call? pool-trait get-dy amount-after-aggregator-fees))
                 (try! (contract-call? pool-trait get-dx amount-after-aggregator-fees))))
    (quote-b (if (is-eq diko-reversed false)
                 (unwrap-panic (contract-call?
                               'SPQC38PW542EQJ5M11CR25P7BS1CA6QT4TBXGB3M.arkadiko-swap-quotes-v-1-1 get-dy
                               token-x token-y
                               quote-a))
                 (unwrap-panic (contract-call?
                               'SPQC38PW542EQJ5M11CR25P7BS1CA6QT4TBXGB3M.arkadiko-swap-quotes-v-1-1 get-dx
                               token-x token-y
                               quote-a))))
  )
    (ok quote-b)
  )
)

(define-public (get-quote-b
    (amount uint) (provider (optional principal))
    (pool-trait <xyk-pool-trait>)
    (xyk-reversed bool)
    (token-x principal) (token-y principal)
    (diko-reversed bool)
  )
  (let (
    (amount-after-aggregator-fees (try! (get-aggregator-fees provider amount)))
    (quote-a (if (is-eq diko-reversed false)
                 (unwrap-panic (contract-call?
                               'SPQC38PW542EQJ5M11CR25P7BS1CA6QT4TBXGB3M.arkadiko-swap-quotes-v-1-1 get-dy
                               token-x token-y
                               amount-after-aggregator-fees))
                 (unwrap-panic (contract-call?
                               'SPQC38PW542EQJ5M11CR25P7BS1CA6QT4TBXGB3M.arkadiko-swap-quotes-v-1-1 get-dx
                               token-x token-y
                               amount-after-aggregator-fees))))
    (quote-b (if (is-eq xyk-reversed false)
                 (try! (contract-call? pool-trait get-dy quote-a))
                 (try! (contract-call? pool-trait get-dx quote-a))))
  )
    (ok quote-b)
  )
)

(define-public (swap-helper-a
    (amount uint) (min-received uint) (provider (optional principal))
    (pool-trait <xyk-pool-trait>)
    (x-token-trait <ft-trait>) (y-token-trait <ft-trait>)
    (xyk-reversed bool)
    (token-x-trait <ft-trait>) (token-y-trait <ft-trait>)
    (diko-reversed bool)
  )
  (let (
    (aggregator-fee-token (if (is-eq xyk-reversed false) x-token-trait y-token-trait))
    (amount-after-aggregator-fees (try! (transfer-aggregator-fees aggregator-fee-token provider amount)))
    (swap-a (if (is-eq xyk-reversed false)
                (unwrap! (xyk-a pool-trait x-token-trait y-token-trait amount-after-aggregator-fees) ERR_SWAP_A)
                (unwrap! (xyk-b pool-trait x-token-trait y-token-trait amount-after-aggregator-fees) ERR_SWAP_A)))
    (swap-b (if (is-eq diko-reversed false)
                (unwrap! (arkadiko-a token-x-trait token-y-trait swap-a) ERR_SWAP_B)
                (unwrap! (arkadiko-b token-x-trait token-y-trait swap-a) ERR_SWAP_B)))
    (caller tx-sender)
  )
    (begin
      (asserts! (> amount-after-aggregator-fees u0) ERR_INVALID_AMOUNT)
      (asserts! (>= swap-b min-received) ERR_MINIMUM_RECEIVED)
      (print {
        action: "swap-helper-a",
        caller: caller, 
        data: {
          amount: amount,
          amount-after-aggregator-fees: amount-after-aggregator-fees,
          min-received: min-received,
          received: swap-b,
          provider: provider,
          pool-trait: (contract-of pool-trait),
          x-token-trait: (contract-of x-token-trait),
          y-token-trait: (contract-of y-token-trait),
          xyk-reversed: xyk-reversed,
          token-x-trait: (contract-of token-x-trait),
          token-y-trait: (contract-of token-y-trait),
          diko-reversed: diko-reversed
        }
      })
      (ok swap-b)
    )
  )
)

(define-public (swap-helper-b
    (amount uint) (min-received uint) (provider (optional principal))
    (pool-trait <xyk-pool-trait>)
    (x-token-trait <ft-trait>) (y-token-trait <ft-trait>)
    (xyk-reversed bool)
    (token-x-trait <ft-trait>) (token-y-trait <ft-trait>)
    (diko-reversed bool)
  )
  (let (
    (aggregator-fee-token (if (is-eq diko-reversed false) token-x-trait token-y-trait))
    (amount-after-aggregator-fees (try! (transfer-aggregator-fees aggregator-fee-token provider amount)))
    (swap-a (if (is-eq diko-reversed false)
                (unwrap! (arkadiko-a token-x-trait token-y-trait amount-after-aggregator-fees) ERR_SWAP_A)
                (unwrap! (arkadiko-b token-x-trait token-y-trait amount-after-aggregator-fees) ERR_SWAP_A)))
    (swap-b (if (is-eq xyk-reversed false)
                (unwrap! (xyk-a pool-trait x-token-trait y-token-trait swap-a) ERR_SWAP_B)
                (unwrap! (xyk-b pool-trait x-token-trait y-token-trait swap-a) ERR_SWAP_B)))
    (caller tx-sender)
  )
    (begin
      (asserts! (> amount-after-aggregator-fees u0) ERR_INVALID_AMOUNT)
      (asserts! (>= swap-b min-received) ERR_MINIMUM_RECEIVED)
      (print {
        action: "swap-helper-b",
        caller: caller, 
        data: {
          amount: amount,
          amount-after-aggregator-fees: amount-after-aggregator-fees,
          min-received: min-received,
          received: swap-b,
          provider: provider,
          pool-trait: (contract-of pool-trait),
          x-token-trait: (contract-of x-token-trait),
          y-token-trait: (contract-of y-token-trait),
          xyk-reversed: xyk-reversed,
          token-x-trait: (contract-of token-x-trait),
          token-y-trait: (contract-of token-y-trait),
          diko-reversed: diko-reversed
        }
      })
      (ok swap-b)
    )
  )
)

(define-private (xyk-a
    (pool-trait <xyk-pool-trait>)
    (x-token-trait <ft-trait>) (y-token-trait <ft-trait>)
    (x-amount uint)
  )
  (let (
    (swap-a (try! (contract-call?
                  'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.xyk-core-v-1-1 swap-x-for-y
                  pool-trait
                  x-token-trait y-token-trait
                  x-amount u1)))
  )
    (ok swap-a)
  )
)

(define-private (xyk-b
    (pool-trait <xyk-pool-trait>)
    (x-token-trait <ft-trait>) (y-token-trait <ft-trait>)
    (y-amount uint)
  )
  (let (
    (swap-a (try! (contract-call?
                  'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.xyk-core-v-1-1 swap-y-for-x
                  pool-trait
                  x-token-trait y-token-trait
                  y-amount u1)))
  )
    (ok swap-a)
  )
)

(define-private (arkadiko-a
    (token-x-trait <ft-trait>) (token-y-trait <ft-trait>)
    (dx uint)
  )
  (let (
    (swap-a (try! (contract-call?
                  'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1 swap-x-for-y
                  token-x-trait token-y-trait
                  dx u1)))
  )
    (ok (default-to u0 (element-at? swap-a u1)))
  )
)

(define-private (arkadiko-b
    (token-x-trait <ft-trait>) (token-y-trait <ft-trait>)
    (dy uint)
  )
  (let (
    (swap-a (try! (contract-call?
                  'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1 swap-y-for-x
                  token-x-trait token-y-trait
                  dy u1)))
  )
    (ok (default-to u0 (element-at? swap-a u0)))
  )
)

(define-private (get-aggregator-fees (provider (optional principal)) (amount uint))
  (let (
    (call-a (try! (contract-call?
                  'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.aggregator-core-v-1-1 get-aggregator-fees
                  (as-contract tx-sender) provider amount)))
    (amount-after-fees (- amount (get amount-fees-total call-a)))
  )
    (ok amount-after-fees)
  )
)

(define-private (transfer-aggregator-fees (token <ft-trait>) (provider (optional principal)) (amount uint))
  (let (
    (call-a (try! (contract-call?
                  'SM1793C4R5PZ4NS4VQ4WMP7SKKYVH8JZEWSZ9HCCR.aggregator-core-v-1-1 transfer-aggregator-fees
                  token (as-contract tx-sender) provider amount)))
    (amount-after-fees (- amount (get amount-fees-total call-a)))
  )
    (ok amount-after-fees)
  )
)

Functions (6)

FunctionAccessArgs
transfer-aggregator-feesprivatetoken: <ft-trait>, provider: (optional principal
get-aggregator-feesprivateprovider: (optional principal
get-quote-apublicamount: uint, provider: (optional principal
get-quote-bpublicamount: uint, provider: (optional principal
swap-helper-apublicamount: uint, min-received: uint, provider: (optional principal
swap-helper-bpublicamount: uint, min-received: uint, provider: (optional principal