Source Code


;; wrapper-arkadiko-v-1-2

(use-trait ft-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)

(define-constant ERR_PAIR_EXTERNAL (err u201))
(define-constant ERR_BALANCE_X (err u201))
(define-constant ERR_BALANCE_Y (err u201))

(define-read-only (get-dy
    (token-x principal) (token-y principal)
    (dx uint) (provider (optional principal))
  )
  (let (
    (amount-after-aggregator-fees (try! (get-aggregator-fees provider dx)))
    (arkadiko-pool (unwrap! (contract-call?
                            'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1 get-pair-details
                            token-x token-y) ERR_PAIR_EXTERNAL))
    (balance-x (unwrap! (get balance-x arkadiko-pool) ERR_BALANCE_X))
    (balance-y (unwrap! (get balance-y arkadiko-pool) ERR_BALANCE_Y))
    (dx-with-fees (/ (* u997 amount-after-aggregator-fees) u1000))
    (dy (/ (* balance-y dx-with-fees) (+ balance-x dx-with-fees)))
  )
    (ok dy)
  )
)

(define-read-only (get-dx
    (token-x principal) (token-y principal)
    (dy uint) (provider (optional principal))
  )
  (let (
    (amount-after-aggregator-fees (try! (get-aggregator-fees provider dy)))
    (arkadiko-pool (unwrap! (contract-call?
                            'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1 get-pair-details
                            token-x token-y) ERR_PAIR_EXTERNAL))
    (balance-x (unwrap! (get balance-x arkadiko-pool) ERR_BALANCE_X))
    (balance-y (unwrap! (get balance-y arkadiko-pool) ERR_BALANCE_Y))
    (dy-with-fees (/ (* u997 amount-after-aggregator-fees) u1000))
    (dx (/ (* balance-x dy-with-fees) (+ balance-y dy-with-fees)))
  )
    (ok dx)
  )
)

(define-public (swap-x-for-y (token-x-trait <ft-trait>) (token-y-trait <ft-trait>) (dx uint) (min-dy uint) (provider (optional principal)))
  (let (
    (amount-after-aggregator-fees (try! (transfer-aggregator-fees token-x-trait provider dx)))
    (call (try! (contract-call?
                'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1 swap-x-for-y
                token-x-trait
                token-y-trait
                amount-after-aggregator-fees min-dy)))
  )
    (print {
      action: "swap-x-for-y",
      caller: tx-sender,
      data: {
        amount: dx,
        amount-after-aggregator-fees: amount-after-aggregator-fees,
        min-received: min-dy,
        received: call,
        provider: provider,
        token-x-trait: token-x-trait,
        token-y-trait: token-y-trait
      }
    })
    (ok call)
  )
)

(define-public (swap-y-for-x (token-x-trait <ft-trait>) (token-y-trait <ft-trait>) (dy uint) (min-dx uint) (provider (optional principal)))
  (let (
    (amount-after-aggregator-fees (try! (transfer-aggregator-fees token-y-trait provider dy)))
    (call (try! (contract-call?
                'SP2C2YFP12AJZB4MABJBAJ55XECVS7E4PMMZ89YZR.arkadiko-swap-v2-1 swap-y-for-x
                token-x-trait
                token-y-trait
                amount-after-aggregator-fees min-dx)))
  )
    (print {
      action: "swap-y-for-x",
      caller: tx-sender,
      data: {
        amount: dy,
        amount-after-aggregator-fees: amount-after-aggregator-fees,
        min-received: min-dx,
        received: call,
        provider: provider,
        token-x-trait: token-x-trait,
        token-y-trait: token-y-trait
      }
    })
    (ok call)
  )
)

(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
get-dyread-onlytoken-x: principal, token-y: principal, dx: uint, provider: (optional principal
get-dxread-onlytoken-x: principal, token-y: principal, dy: uint, provider: (optional principal
swap-x-for-ypublictoken-x-trait: <ft-trait>, token-y-trait: <ft-trait>, dx: uint, min-dy: uint, provider: (optional principal
swap-y-for-xpublictoken-x-trait: <ft-trait>, token-y-trait: <ft-trait>, dy: uint, min-dx: uint, provider: (optional principal
get-aggregator-feesprivateprovider: (optional principal
transfer-aggregator-feesprivatetoken: <ft-trait>, provider: (optional principal