cross-peg-out-endpoint-v2-01

SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9

Source Code

(use-trait ft-trait .trait-sip-010.sip-010-trait)
(define-constant ERR-NOT-AUTHORIZED (err u1000))
(define-constant ERR-PAUSED (err u1015))
(define-constant ERR-USER-NOT-WHITELISTED (err u1016))
(define-constant ERR-AMOUNT-LESS-THAN-MIN-FEE (err u1017))
(define-constant ERR-INVALID-AMOUNT (err u1019))
(define-constant MAX_UINT u340282366920938463463374607431768211455)
(define-constant ONE_8 u100000000)
(define-data-var contract-owner principal tx-sender)
(define-data-var is-paused bool true)
(define-data-var use-whitelist bool false)
(define-map whitelisted-users principal bool)
(define-public (transfer-to-unwrap (token-trait <ft-trait>) (amount-in-fixed uint) (dest-chain-id uint) (settle-address (buff 256)))
  (let (
      (sender tx-sender)
      (token (contract-of token-trait))
      (validation-data (try! (validate-transfer-to-unwrap sender token amount-in-fixed dest-chain-id)))
      (chain-details (get chain-details validation-data))
      (token-details (get token-details validation-data))
      (fee (max (mul-down amount-in-fixed (get fee token-details)) (get-min-fee-or-default { token: token, chain-id: dest-chain-id })))
      (net-amount (- amount-in-fixed fee)))
    (if (get burnable token-details)
      (begin
        (as-contract (try! (contract-call? token-trait burn-fixed net-amount sender)))
        (and (> fee u0) (try! (contract-call? token-trait transfer-fixed fee sender .cross-bridge-registry-v2 none))))
      (try! (contract-call? token-trait transfer-fixed amount-in-fixed sender .cross-bridge-registry-v2 none)))
    (as-contract (try! (contract-call? .cross-bridge-registry-v2 add-accrued-fee token fee)))
    (as-contract (try! (contract-call? .cross-bridge-registry-v2 remove-token-reserve { token: token, chain-id: dest-chain-id } amount-in-fixed)))
    (print { object: "cross-bridge-endpoint", action: "transfer-to-unwrap", user: sender, chain: (get name chain-details), dest-chain-id: dest-chain-id, net-amount: net-amount, fee-amount: fee, settle-address: settle-address, token: token })
    (ok true)))
(define-read-only (validate-transfer-to-unwrap (sender principal) (token principal) (amount-in-fixed uint) (dest-chain-id uint))
  (let (
      (chain-details (try! (get-approved-chain-or-fail dest-chain-id)))
      (token-details (try! (get-approved-pair-or-fail { token: token, chain-id: dest-chain-id }))))
    (asserts! (not (get-paused)) ERR-PAUSED)
    (asserts! (or (not (get-use-whitelist)) (is-whitelisted sender)) ERR-USER-NOT-WHITELISTED)
    (asserts! (and (>= amount-in-fixed (get min-amount token-details)) (<= amount-in-fixed (get max-amount token-details)) (<= amount-in-fixed (get-token-reserve-or-default { token: token, chain-id: dest-chain-id }))) ERR-INVALID-AMOUNT)
    (asserts! (> amount-in-fixed (get-min-fee-or-default { token: token, chain-id: dest-chain-id })) ERR-AMOUNT-LESS-THAN-MIN-FEE)
    (ok { amount: amount-in-fixed, chain-details: chain-details, token: token, token-details: token-details })))
(define-read-only (get-use-whitelist)
  (var-get use-whitelist))
(define-read-only (is-whitelisted (user principal))
  (default-to false (map-get? whitelisted-users user)))
(define-read-only (get-paused)
  (var-get is-paused))
(define-read-only (get-contract-owner)
  (var-get contract-owner))
(define-read-only (is-approved-operator-or-default (operator principal))
  (contract-call? .cross-bridge-registry-v2 is-approved-operator-or-default operator))
(define-read-only (get-approved-chain-or-fail (dest-chain-id uint))
  (contract-call? .cross-bridge-registry-v2 get-approved-chain-or-fail dest-chain-id))
(define-read-only (get-token-reserve-or-default (pair { token: principal, chain-id: uint }))
  (contract-call? .cross-bridge-registry-v2 get-token-reserve-or-default pair))
(define-read-only (get-min-fee-or-default (pair { token: principal, chain-id: uint }))
  (contract-call? .cross-bridge-registry-v2 get-min-fee-or-default pair))
(define-read-only (get-approved-pair-or-fail (pair { token: principal, chain-id: uint }))
  (contract-call? .cross-bridge-registry-v2 get-approved-pair-or-fail pair))
(define-public (set-paused (paused bool))
  (begin
    (try! (check-is-owner))
    (ok (var-set is-paused paused))))
(define-public (apply-whitelist (new-use-whitelist bool))
  (begin
    (try! (check-is-owner))
    (ok (var-set use-whitelist new-use-whitelist))))
(define-public (whitelist (user principal) (whitelisted bool))
  (begin
    (try! (check-is-owner))
    (ok (map-set whitelisted-users user whitelisted))))
(define-public (whitelist-many (users (list 2000 principal)) (whitelisted (list 2000 bool)))
  (ok (map whitelist users whitelisted)))
(define-public (set-contract-owner (owner principal))
  (begin
    (try! (check-is-owner))
    (ok (var-set contract-owner owner))))
(define-private (check-is-owner)
  (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)))
(define-private (mul-down (a uint) (b uint))
  (/ (* a b) ONE_8))
(define-private (div-down (a uint) (b uint))
  (if (is-eq a u0) u0 (/ (* a ONE_8) b)))
(define-private (max (a uint) (b uint))
  (if (<= a b) b a))

Functions (20)

FunctionAccessArgs
transfer-to-unwrappublictoken-trait: <ft-trait>, amount-in-fixed: uint, dest-chain-id: uint, settle-address: (buff 256
validate-transfer-to-unwrapread-onlysender: principal, token: principal, amount-in-fixed: uint, dest-chain-id: uint
get-use-whitelistread-only
is-whitelistedread-onlyuser: principal
get-pausedread-only
get-contract-ownerread-only
is-approved-operator-or-defaultread-onlyoperator: principal
get-approved-chain-or-failread-onlydest-chain-id: uint
get-token-reserve-or-defaultread-onlypair: { token: principal, chain-id: uint }
get-min-fee-or-defaultread-onlypair: { token: principal, chain-id: uint }
get-approved-pair-or-failread-onlypair: { token: principal, chain-id: uint }
set-pausedpublicpaused: bool
apply-whitelistpublicnew-use-whitelist: bool
whitelistpublicuser: principal, whitelisted: bool
whitelist-manypublicusers: (list 2000 principal
set-contract-ownerpublicowner: principal
check-is-ownerprivate
mul-downprivatea: uint, b: uint
div-downprivatea: uint, b: uint
maxprivatea: uint, b: uint