Source Code

(use-trait extension-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.extension-trait.extension-trait)
(use-trait gas-trait 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.gas-station-trait.gas-station-trait)
(use-trait dual-stacking-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.xbtc-sbtc-swap-v2.enroll-trait)

(use-trait sip-010-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)
(use-trait sip-009-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)
(use-trait pool-trait 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.dexterity-traits-v0.liquidity-pool-trait)
(use-trait dex-trait 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.faktory-dex-trait-v2.dex-trait)
(use-trait pre-trait 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.prelaunch-faktory-trait-v1.prelaunch-trait)
(use-trait token-trait 'SP3XXMS38VTAWTVPE5682XSBFXPTH7XCPEBTX8AN2.faktory-trait-v1.sip-010-trait)
(use-trait nftmarket-trait 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-nftmarket-trait.nftmarket-trait)

(define-constant err-unauthorised (err u4001))
(define-constant err-invalid-signature (err u4002))
(define-constant err-forbidden (err u4003))
(define-constant err-unregistered-pubkey (err u4004))
(define-constant err-not-admin-pubkey (err u4005))
(define-constant err-signature-replay (err u4006))
(define-constant err-no-auth-id (err u4007))
(define-constant err-no-message-hash (err u4008))
(define-constant err-inactive-required (err u4009))
(define-constant err-no-pending-recovery (err u4010))
(define-constant err-not-whitelisted (err u4011))
(define-constant err-in-cooldown (err u4012))
(define-constant err-invalid-operation (err u4013))
(define-constant err-already-executed (err u4014))
(define-constant err-vetoed (err u4015))
(define-constant err-not-signaled (err u4016))
(define-constant err-cooldown-not-passed (err u4017))
(define-constant err-threshold-exceeded (err u4018))
(define-constant err-cooldown-too-long (err u4019))
(define-constant err-no-pending-transfer (err u4020))
(define-constant err-no-pending-pubkey (err u4021))
(define-constant err-already-initialized (err u4022))
(define-constant err-token-locked (err u4023))
(define-constant err-fatal-owner-not-admin (err u9999))

(define-constant INACTIVITY-PERIOD u52560) 
(define-constant MAX-CONFIG-COOLDOWN u4032) 
(define-constant DEPLOYED-BURNT-BLOCK burn-block-height)
(define-constant SBTC-CONTRACT 'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token)
(define-constant ZSBTC-CONTRACT 'SP2VCQJGH7PHP2DJK7Z0V48AGBHQAW3R3ZW1QF4N.zsbtc-token)
(define-constant FAKFUN-DEPLOYER 'SP1G655MB1JVQ5FBE2JJ3E01HEA6KBM4H39F5EW63)
(define-constant PUBK 0x000000000000000000000000000000000000000000000000000000000000000000)

(define-constant OPCODE-BUY 0x00)
(define-constant OPCODE-SELL 0x01)
(define-constant OPCODE-BUY-SEATS 0x02)
(define-constant OPCODE-REFUND 0x03)

(define-constant BOB-CONTRACT 'SP2VG7S0R4Z8PYNYCAQ04HCBX1MH75VT11VXCWQ6G.built-on-bitcoin-stxcity)
(define-constant BOB-BURN-AMOUNT u1000000)

(define-constant EXECUTE-OP-BUY 0x00)
(define-constant EXECUTE-OP-SELL 0x01)
(define-constant EXECUTE-OP-ADD-LIQ 0x02)
(define-constant EXECUTE-OP-REMOVE-LIQ 0x03)

(define-constant NFT-OP-LIST 0x00)
(define-constant NFT-OP-BUY 0x01)
(define-constant NFT-OP-UNLIST 0x02)
(define-constant NFT-OP-UPDATE-PRICE 0x03)
(define-constant NFT-OP-UPDATE-FT 0x04)

(define-data-var last-activity-block uint burn-block-height)
(define-data-var recovery-address principal 'SP000000000000000000002Q6VF78)
(define-data-var initial-pubkey (buff 33) PUBK)
(define-data-var is-initialized bool false)
(define-data-var pubkey-initialized bool false)

(define-data-var pending-pubkey {
  pubkey: (buff 33),
  proposed-at: uint,
} {
  pubkey: (var-get initial-pubkey),
  proposed-at: u0,
})

(define-data-var pending-pubkey-cooldown {
  new-period: uint,
  proposed-at: uint,
} {
  new-period: u0,
  proposed-at: u0,
})

(define-data-var owner principal 'SP000000000000000000002Q6VF78)
(define-data-var pending-recovery principal 'SP000000000000000000002Q6VF78)
(define-data-var pending-transfer principal 'SP000000000000000000002Q6VF78)

(define-fungible-token ect)

(define-map used-pubkey-authorizations
  (buff 32) 
  (buff 33)
)


(define-data-var wallet-config {
  stx-threshold: uint,
  sbtc-threshold: uint,
  zsbtc-threshold: uint,
  cooldown-period: uint,
  config-signaled-at: (optional uint),
} {
  stx-threshold: u100000000,
  sbtc-threshold: u100000,
  zsbtc-threshold: u100000,
  cooldown-period: u144,
  config-signaled-at: none,
})

(define-data-var pubkey-cooldown-period uint u432)
(define-data-var max-gas-amount uint u1000)

(define-data-var token-lock-enabled bool false)

(define-data-var spent-this-period {
  stx: uint,
  sbtc: uint,
  zsbtc: uint,
  period-start: uint,
} {
  stx: u0,
  sbtc: u0,
  zsbtc: u0,
  period-start: DEPLOYED-BURNT-BLOCK,
})

(define-private (get-current-spent)
  (let (
      (spent (var-get spent-this-period))
      (config (var-get wallet-config))
      (period-expired (> burn-block-height (+ (get period-start spent) (get cooldown-period config))))
    )
    (if period-expired
      { stx: u0, sbtc: u0, zsbtc: u0, period-start: burn-block-height }
      spent
    )
  )
)

(define-private (add-spent-stx (amount uint))
  (let ((current (get-current-spent)))
    (var-set spent-this-period (merge current { stx: (+ (get stx current) amount) }))
  )
)

(define-private (add-spent-sbtc (amount uint))
  (let ((current (get-current-spent)))
    (var-set spent-this-period (merge current { sbtc: (+ (get sbtc current) amount) }))
  )
)

(define-private (add-spent-zsbtc (amount uint))
  (let ((current (get-current-spent)))
    (var-set spent-this-period (merge current { zsbtc: (+ (get zsbtc current) amount) }))
  )
)

(define-map whitelisted-extensions principal bool)

(define-map pending-operations
  uint 
  {
    op-type: (string-ascii 20),
    amount: uint,
    recipient: principal,
    token: (optional principal),  
    extension: (optional principal),
    payload: (optional (buff 2048)),
    execute-after: uint,
    executed: bool,
    vetoed: bool,
  }
)

(define-data-var operation-nonce uint u0)

(define-public (set-max-gas-amount (amount uint))
  (begin
    (try! (is-admin-calling tx-sender))
    (var-set max-gas-amount amount)
    (ok true)
  )
)

(define-read-only (get-token-lock-enabled)
  (var-get token-lock-enabled)
)

(define-public (toggle-token-lock
    (enabled bool)
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (if enabled
      (match sig-auth
        sig-auth-details (begin
          (try! (is-authorized (some {
            message-hash: (contract-call?
              'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v6
              build-toggle-token-lock-hash {
              auth-id: (get auth-id sig-auth-details),
              enabled: enabled,
            }),
            signature: (get signature sig-auth-details),
            pubkey: (get pubkey sig-auth-details),
          })))
          (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
        )
        (try! (is-authorized none))
      )
      (try! (is-admin-calling tx-sender))
    )
    (var-set token-lock-enabled enabled)
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-token-lock-toggled enabled))
    (ok true)
  )
)

(define-public (signal-config-change)
  (let ((config (var-get wallet-config)))
    (try! (is-authorized none))
    (var-set wallet-config (merge config { config-signaled-at: (some burn-block-height) }))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-signal-config-change))
    (ok true)
  )
)

(define-public (set-wallet-config
    (new-stx-threshold uint)
    (new-sbtc-threshold uint)
    (new-zsbtc-threshold uint)
    (new-cooldown-period uint)
  )
  (let (
      (config (var-get wallet-config))
      (signaled-at (default-to u0 (get config-signaled-at config)))
      (wallet-cooldown (get cooldown-period config))
      (effective-config-cooldown (if (> wallet-cooldown MAX-CONFIG-COOLDOWN)
        MAX-CONFIG-COOLDOWN
        wallet-cooldown
      ))
    )
    (try! (is-authorized none))
    (asserts! (not (is-eq signaled-at u0)) err-not-signaled)
    (asserts! (>= burn-block-height (+ signaled-at effective-config-cooldown)) err-in-cooldown)
    (var-set wallet-config {
      stx-threshold: new-stx-threshold,
      sbtc-threshold: new-sbtc-threshold,
      zsbtc-threshold: new-zsbtc-threshold,
      cooldown-period: new-cooldown-period,
      config-signaled-at: none,
    })
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-wallet-config-set new-stx-threshold new-sbtc-threshold new-zsbtc-threshold new-cooldown-period))
    (ok true)
  )
)

(define-private (create-pending-operation
    (op-type (string-ascii 20))
    (amount uint)
    (recipient principal)
    (token (optional principal))
    (extension (optional principal))
    (payload (optional (buff 2048)))
  )
  (let (
      (config (var-get wallet-config))
      (op-id (var-get operation-nonce))
    )
    (map-set pending-operations op-id {
      op-type: op-type,
      amount: amount,
      recipient: recipient,
      token: token,
      extension: extension,
      payload: payload,
      execute-after: (+ burn-block-height (get cooldown-period config)),
      executed: false,
      vetoed: false,
    })
    (var-set operation-nonce (+ op-id u1))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-pending-operation op-id op-type amount recipient token extension payload (+ burn-block-height (get cooldown-period config))))
    (ok op-id)
  )
)

(define-public (veto-operation
    (op-id uint)
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (let ((op (unwrap! (map-get? pending-operations op-id) err-invalid-operation)))
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-veto-operation-hash {
            auth-id: (get auth-id sig-auth-details),
            op-id: op-id,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (asserts! (not (get executed op)) err-already-executed)
    (map-set pending-operations op-id (merge op { vetoed: true }))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-operation-vetoed op-id))
    (ok true)
  )
)

(define-read-only (get-pending-operation (op-id uint))
  (map-get? pending-operations op-id)
)

(define-private (would-exceed-stx-threshold (amount uint))
  (let (
      (config (var-get wallet-config))
      (spent (get-current-spent))
    )
    (> (+ (get stx spent) amount) (get stx-threshold config))
  )
)

(define-private (would-exceed-sbtc-threshold (amount uint))
  (let (
      (config (var-get wallet-config))
      (spent (get-current-spent))
    )
    (> (+ (get sbtc spent) amount) (get sbtc-threshold config))
  )
)

(define-private (would-exceed-zsbtc-threshold (amount uint))
  (let (
      (config (var-get wallet-config))
      (spent (get-current-spent))
    )
    (> (+ (get zsbtc spent) amount) (get zsbtc-threshold config))
  )
)

(define-private (is-authorized (sig-message-auth (optional {
  message-hash: (buff 32),
  signature: (buff 65),
  pubkey: (buff 33),
})))
  (match sig-message-auth
    sig-message-details (consume-signature (get message-hash sig-message-details)
      (get signature sig-message-details) (get pubkey sig-message-details)
    )
    (is-admin-calling tx-sender)
  )
)

(define-read-only (is-admin-calling (caller principal))
  (ok (asserts! (is-some (map-get? admins caller)) err-unauthorised))
)

(define-public (whitelist-extension (extension principal))
  (begin
    (try! (is-admin-calling tx-sender))
    (create-pending-operation "whitelist-ext" u0 extension none (some extension) none)
  )
)

(define-public (execute-pending-whitelist
    (op-id uint)
    (sig-auth {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    })
    (gas (optional <gas-trait>))
  )
  (let ((op (unwrap! (map-get? pending-operations op-id) err-invalid-operation)))
    (asserts! (is-eq (get op-type op) "whitelist-ext") err-invalid-operation)
    (asserts! (not (get executed op)) err-already-executed)
    (asserts! (not (get vetoed op)) err-vetoed)
    (asserts! (>= burn-block-height (get execute-after op)) err-cooldown-not-passed)
    (try! (is-authorized (some {
      message-hash: (contract-call?
        'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
        build-whitelist-extension-hash {
        auth-id: (get auth-id sig-auth),
        op-id: op-id,
        extension: (unwrap! (get extension op) err-invalid-operation),
      }),
      signature: (get signature sig-auth),
      pubkey: (get pubkey sig-auth),
    })))
    (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
    (map-set pending-operations op-id (merge op { executed: true }))
    (map-set whitelisted-extensions (unwrap! (get extension op) err-invalid-operation) true)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-extension-whitelisted (unwrap-panic (get extension op))))
    (ok true)
  )
)

(define-public (remove-extension-whitelist
    (extension principal)
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-remove-extension-whitelist-hash {
            auth-id: (get auth-id sig-auth-details),
            extension: extension,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-extension-removed extension))
    (ok (map-delete whitelisted-extensions extension))
  )
)

(define-read-only (is-extension-whitelisted (extension principal))
  (default-to false (map-get? whitelisted-extensions extension))
)

(define-public (stx-transfer
    (amount uint)
    (recipient principal)
    (memo (optional (buff 34)))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (asserts! (not (var-get token-lock-enabled)) err-token-locked)
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-stx-transfer-hash {
            auth-id: (get auth-id sig-auth-details),
            amount: amount,
            recipient: recipient,
            memo: memo,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (if (would-exceed-stx-threshold amount)
      (begin
        (unwrap-panic (create-pending-operation "stx-transfer" amount recipient none none none))
        (ok true)
      )
      (begin
        (add-spent-stx amount)
        (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-stx-transfer amount recipient memo))
        (as-contract? ((with-stx amount))
          (match memo
            to-print (try! (stx-transfer-memo? amount tx-sender recipient to-print))
            (try! (stx-transfer? amount tx-sender recipient))
          ))
      )
    )
  )
)

(define-public (execute-pending-stx-transfer 
    (op-id uint)
    (memo (optional (buff 34)))
  )
  (let ((op (unwrap! (map-get? pending-operations op-id) err-invalid-operation)))
    (asserts! (is-eq (get op-type op) "stx-transfer") err-invalid-operation)
    (asserts! (not (get executed op)) err-already-executed)
    (asserts! (not (get vetoed op)) err-vetoed)
    (asserts! (>= burn-block-height (get execute-after op)) err-cooldown-not-passed)
    (try! (is-authorized none))
    (map-set pending-operations op-id (merge op { executed: true }))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-stx-transfer (get amount op) (get recipient op) memo))
    (as-contract? ((with-stx (get amount op)))
      (match memo
        to-print (try! (stx-transfer-memo? (get amount op) tx-sender (get recipient op) to-print))
        (try! (stx-transfer? (get amount op) tx-sender (get recipient op)))
      ))
  )
)

(define-public (extension-call
    (extension <extension-trait>)
    (payload (buff 2048))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (asserts! (is-extension-whitelisted (contract-of extension)) err-not-whitelisted)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-extension-call-hash {
            auth-id: (get auth-id sig-auth-details),
            extension: (contract-of extension),
            payload: payload,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (try! (ft-mint? ect u1 current-contract))
    (try! (ft-burn? ect u1 current-contract))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-extension-call (contract-of extension) payload))
    (as-contract? ((with-all-assets-unsafe))
      (try! (contract-call? extension call payload))
    )
  )
)

(define-public (sip010-transfer
    (amount uint)
    (recipient principal)
    (memo (optional (buff 34)))
    (sip010 <sip-010-trait>)
    (token-name (string-ascii 128))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (asserts! (not (var-get token-lock-enabled)) err-token-locked)
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-sip010-transfer-hash {
            auth-id: (get auth-id sig-auth-details),
            amount: amount,
            recipient: recipient,
            memo: memo,
            sip010: (contract-of sip010),
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (if (and (is-eq (contract-of sip010) SBTC-CONTRACT) (would-exceed-sbtc-threshold amount))
      (begin
        (unwrap-panic (create-pending-operation "sbtc-transfer" amount recipient (some SBTC-CONTRACT) none none))
        (ok true)
      )
      (if (and (is-eq (contract-of sip010) ZSBTC-CONTRACT) (would-exceed-zsbtc-threshold amount))
      (begin
        (unwrap-panic (create-pending-operation "zsbtc-transfer" amount recipient (some ZSBTC-CONTRACT) none none))
        (ok true)
      )
      (begin
        (if (is-eq (contract-of sip010) SBTC-CONTRACT)
          (add-spent-sbtc amount)
          (if (is-eq (contract-of sip010) ZSBTC-CONTRACT)
            (add-spent-zsbtc amount)
            true
          )
        )
        (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-sip010-transfer (contract-of sip010) amount recipient memo))
        (as-contract? ((with-ft (contract-of sip010) token-name amount))
          (try! (contract-call? sip010 transfer amount current-contract recipient memo))
        )
      ))
    )
  )
)

(define-public (execute-pending-sbtc-transfer
    (op-id uint)
    (memo (optional (buff 34)))
  )
  (let ((op (unwrap! (map-get? pending-operations op-id) err-invalid-operation)))
    (asserts! (is-eq (get op-type op) "sbtc-transfer") err-invalid-operation)
    (asserts! (not (get executed op)) err-already-executed)
    (asserts! (not (get vetoed op)) err-vetoed)
    (asserts! (>= burn-block-height (get execute-after op)) err-cooldown-not-passed)
    (try! (is-authorized none))
    (map-set pending-operations op-id (merge op { executed: true }))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-sip010-transfer SBTC-CONTRACT (get amount op) (get recipient op) memo))
    (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (get amount op)))
      (try! (contract-call? 'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token transfer
        (get amount op) current-contract (get recipient op) memo))
    )
  )
)

(define-public (execute-pending-zsbtc-transfer
    (op-id uint)
    (memo (optional (buff 34)))
  )
  (let ((op (unwrap! (map-get? pending-operations op-id) err-invalid-operation)))
    (asserts! (is-eq (get op-type op) "zsbtc-transfer") err-invalid-operation)
    (asserts! (not (get executed op)) err-already-executed)
    (asserts! (not (get vetoed op)) err-vetoed)
    (asserts! (>= burn-block-height (get execute-after op)) err-cooldown-not-passed)
    (try! (is-authorized none))
    (map-set pending-operations op-id (merge op { executed: true }))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-sip010-transfer ZSBTC-CONTRACT (get amount op) (get recipient op) memo))
    (as-contract? ((with-ft ZSBTC-CONTRACT "zsbtc" (get amount op)))
      (try! (contract-call? 'SP2VCQJGH7PHP2DJK7Z0V48AGBHQAW3R3ZW1QF4N.zsbtc-token transfer
        (get amount op) current-contract (get recipient op) memo))
    )
  )
)

(define-public (sip009-transfer
    (nft-id uint)
    (recipient principal)
    (sip009 <sip-009-trait>)
    (token-name (string-ascii 128))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (asserts! (not (var-get token-lock-enabled)) err-token-locked)
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-sip009-transfer-hash {
            auth-id: (get auth-id sig-auth-details),
            nft-id: nft-id,
            recipient: recipient,
            sip009: (contract-of sip009),
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-sip009-transfer nft-id recipient (contract-of sip009)))
    (as-contract? ((with-nft (contract-of sip009) token-name (list nft-id)))
      (try! (contract-call? sip009 transfer nft-id current-contract recipient))
    )
  )
)

(define-public (faktory-execute
    (pool <pool-trait>)
    (amount uint)
    (opcode (optional (buff 16)))
    (sip010 <sip-010-trait>)
    (sip010-name (string-ascii 128))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v4
            build-faktory-execute-hash {
            auth-id: (get auth-id sig-auth-details),
            pool: (contract-of pool),
            amount: amount,
            opcode: opcode,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (let ((op (get-byte (default-to 0x00 opcode) u0)))
          (if (or (is-eq op EXECUTE-OP-BUY) (is-eq op EXECUTE-OP-SELL) (is-eq op EXECUTE-OP-REMOVE-LIQ))
               (as-contract? ((with-ft (contract-of sip010) sip010-name amount))
                  (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2 execute pool amount opcode)))
              (if (is-eq op EXECUTE-OP-ADD-LIQ)
                  (let ((liq-quote (unwrap! (contract-call? pool quote amount (some 0x02)) err-invalid-operation)))
                  (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (get dx liq-quote))
                    (with-ft (contract-of sip010) sip010-name (get dy liq-quote)))
                    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2 execute pool amount opcode))
                  ))
                  err-invalid-operation)
            )
    )
  )
)

(define-private (get-byte
    (opcode (buff 16))
    (position uint))
    (default-to 0x00 (element-at? opcode position)))

(define-public (faktory-place-order
    (dex <dex-trait>)
    (token <token-trait>)
    (token-name (string-ascii 128))
    (amount uint)
    (opcode (optional (buff 16)))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v4
            build-faktory-place-order-hash {
            auth-id: (get auth-id sig-auth-details),
            dex: (contract-of dex),
            amount: amount,
            opcode: opcode,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (let ((op (get-byte (default-to 0x00 opcode) u0)))
        (if (is-eq op OPCODE-BUY)
            (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" amount))
                      (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2
                                 place-order dex token amount opcode)))
            (if (is-eq op OPCODE-SELL)
                (as-contract? ((with-ft (contract-of token) token-name amount))
                  (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2 
                            place-order dex token amount opcode)))
            err-invalid-operation)))
  )
)

(define-public (faktory-process
    (pre <pre-trait>)
    (seat-count uint)
    (opcode (optional (buff 16)))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v4
            build-faktory-process-hash {
            auth-id: (get auth-id sig-auth-details),
            pre: (contract-of pre),
            seat-count: seat-count,
            opcode: opcode,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (let ((operation (get-byte (default-to 0x02 opcode) u0)))
      (if (is-eq operation OPCODE-BUY-SEATS)
        (let ((seat-price (try! (contract-call? pre get-seat-price))))
          (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (* seat-count seat-price)))
            (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2
                       process pre seat-count (some current-contract) opcode))))
        (if (is-eq operation OPCODE-REFUND)
          (as-contract? ()
            (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2
                        process pre seat-count (some current-contract) opcode)))
          err-invalid-operation
        )
      )
    )
  )
)

(define-public (faktory-process-claim
    (pre <pre-trait>)
    (token <token-trait>)
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v4
            build-faktory-process-claim-hash {
            auth-id: (get auth-id sig-auth-details),
            pre: (contract-of pre),
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (as-contract? ()
      (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2 process-claim pre token (some current-contract)))
    )
  )
)

(define-public (faktory-fee-airdrop
    (pre <pre-trait>)
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v4
            build-faktory-fee-airdrop-hash {
            auth-id: (get auth-id sig-auth-details),
            pre: (contract-of pre),
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (as-contract? ()
      (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-core-v2 process-fee-airdrop pre))
    )
  )
)

(define-public (faktory-burn-bob
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v5
            build-faktory-burn-bob-hash {
            auth-id: (get auth-id sig-auth-details),
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (as-contract? ((with-ft BOB-CONTRACT "BOB" BOB-BURN-AMOUNT))
      (try! (contract-call? 'SP29D6YMDNAKN1P045T6Z817RTE1AC0JAA99WAX2B.burn-bob-faktory daily-burn))
    )
  )
)

(define-public (faktory-nft-execute
    (marketplace <nftmarket-trait>)
    (token-id uint)
    (nft-contract <sip-009-trait>)
    (nft-name (string-ascii 128))
    (ft-contract <sip-010-trait>)
    (ft-name (string-ascii 128))
    (price uint)
    (opcode (optional (buff 16)))
    (sig-auth (optional {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v5
            build-faktory-nft-execute-hash {
            auth-id: (get auth-id sig-auth-details),
            marketplace: (contract-of marketplace),
            token-id: token-id,
            ft-contract: (contract-of ft-contract),
            price: price,
            opcode: opcode,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )
    (let ((op (get-byte (default-to 0x00 opcode) u0)))
      (if (is-eq op NFT-OP-LIST)
        (as-contract? ((with-nft (contract-of nft-contract) nft-name (list token-id)))
          (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-nfts-core list-nft marketplace token-id nft-contract ft-contract price))
        )
        (if (is-eq op NFT-OP-BUY)
          (as-contract? ((with-ft (contract-of ft-contract) ft-name price))
            (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-nfts-core buy-nft marketplace token-id nft-contract ft-contract))
          )
          (if (is-eq op NFT-OP-UNLIST)
            (as-contract? ()
              (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-nfts-core unlist-nft marketplace token-id nft-contract))
            )
            (if (is-eq op NFT-OP-UPDATE-PRICE)
              (as-contract? ()
                (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-nfts-core update-price marketplace token-id price))
              )
              (if (is-eq op NFT-OP-UPDATE-FT)
                (as-contract? ()
                  (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-nfts-core update-listing-ft marketplace token-id ft-contract price))
                )
                err-invalid-operation
              )
            )
          )
        )
      )
    )
  )
)

(define-map admins
  principal
  bool
)

(define-map pubkey-to-admin
  (buff 33) 
  principal
)

(define-read-only (is-admin-pubkey (pubkey (buff 33)))
  (let ((user-opt (map-get? pubkey-to-admin pubkey)))
    (match user-opt
      user (ok (unwrap! (is-admin-calling user) err-not-admin-pubkey))
      err-unregistered-pubkey
    )
  )
)

(define-public (propose-transfer-wallet (new-admin principal))
  (begin
    (try! (is-admin-calling tx-sender))
    (asserts! (not (is-eq new-admin tx-sender)) err-forbidden)
    (var-set pending-transfer new-admin)
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-propose-transfer-wallet new-admin))
    (ok true)
  )
)

(define-public (confirm-transfer-wallet
    (sig-auth {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    })
    (gas (optional <gas-trait>))
  )
  (let ((pending (var-get pending-transfer)))
    (asserts! (not (is-eq pending 'SP000000000000000000002Q6VF78)) err-no-pending-transfer)
    (try! (is-authorized (some {
      message-hash: (contract-call?
        'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
        build-confirm-transfer-hash {
        auth-id: (get auth-id sig-auth),
        new-admin: pending,
      }),
      signature: (get signature sig-auth),
      pubkey: (get pubkey sig-auth),
    })))
    (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
    (try! (ft-mint? ect u1 current-contract))
    (try! (ft-burn? ect u1 current-contract))
    (map-set admins pending true)
    (map-delete admins (var-get owner))
    (var-set owner pending)
    (var-set pending-transfer 'SP000000000000000000002Q6VF78)
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-wallet-transferred pending))
    (ok true)
  )
)

(define-public (propose-admin-pubkey (pubkey (buff 33)))
  (begin
    (try! (is-admin-calling tx-sender))
    (var-set pending-pubkey {
      pubkey: pubkey,
      proposed-at: burn-block-height,
    })
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-propose-admin-pubkey pubkey))
    (ok true)
  )
)

(define-public (confirm-admin-pubkey)
  (let (
      (pending (var-get pending-pubkey))
      (pubk (get pubkey pending))
    )
    (asserts! (not (is-eq (get proposed-at pending) u0)) err-no-pending-pubkey)
    (asserts! (>= burn-block-height (+ (get proposed-at pending) (var-get pubkey-cooldown-period))) err-in-cooldown)
    (try! (is-admin-calling tx-sender))
    (map-set pubkey-to-admin pubk tx-sender)
    (var-set pending-pubkey {
      pubkey: 0x000000000000000000000000000000000000000000000000000000000000000000,
      proposed-at: u0,
    })
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-confirm-admin-pubkey pubk tx-sender))
    (ok true)
  )
)

(define-public (signal-pubkey-cooldown-change (new-period uint))
  (begin
    (try! (is-authorized none))
    (var-set pending-pubkey-cooldown {
      new-period: new-period,
      proposed-at: burn-block-height,
    })
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-signal-pubkey-cooldown-change new-period))
    (ok true)
  )
)

(define-public (confirm-pubkey-cooldown-change)
  (let (
      (pending (var-get pending-pubkey-cooldown))
      (new-cooldown (get new-period pending))
      (effective-cooldown (if (> new-cooldown MAX-CONFIG-COOLDOWN)
        MAX-CONFIG-COOLDOWN
        new-cooldown
      ))
      (current-period (var-get pubkey-cooldown-period))
    )
    (try! (is-authorized none))
    (asserts! (not (is-eq (get proposed-at pending) u0)) err-not-signaled)
    (asserts! (>= burn-block-height (+ (get proposed-at pending) current-period)) err-in-cooldown)
    (var-set pubkey-cooldown-period effective-cooldown)
    (var-set pending-pubkey-cooldown {
      new-period: u0,
      proposed-at: u0,
    })
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-confirm-pubkey-cooldown-change effective-cooldown))
    (ok true)
  )
)

(define-public (remove-admin-pubkey (pubkey (buff 33)))
  (begin
    (try! (is-authorized none))
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-remove-admin-pubkey pubkey))
    (ok (map-delete pubkey-to-admin pubkey))
  )
)

(define-read-only (verify-signature
    (message-hash (buff 32))
    (signature (buff 65))
    (pubkey (buff 33))
  )
  (begin
    (try! (is-admin-pubkey pubkey))
    (ok (asserts! (is-eq (secp256k1-recover? message-hash signature) (ok pubkey))
      err-invalid-signature
    ))
  )
)

(define-private (consume-signature
    (message-hash (buff 32))
    (signature (buff 65))
    (pubkey (buff 33))
  )
  (begin
    (try! (verify-signature message-hash signature pubkey))
    (asserts! (is-none (map-get? used-pubkey-authorizations message-hash))
      err-signature-replay
    )
    (map-set used-pubkey-authorizations message-hash pubkey)
    (ok true)
  )
)

(define-read-only (get-owner)
  (ok (var-get owner))
)

(define-read-only (is-inactive)
  (> burn-block-height (+ INACTIVITY-PERIOD (var-get last-activity-block)))
)

(define-private (update-activity)
  (var-set last-activity-block burn-block-height)
)

(define-public (add-admin-with-signature
    (new-admin principal)
    (sig-auth {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    })
    (gas (optional <gas-trait>))
  )
  (begin
    (asserts! (not (var-get is-initialized)) err-already-initialized)
    (try! (is-authorized (some {
      message-hash: (contract-call?
        'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
        build-add-admin-hash {
        auth-id: (get auth-id sig-auth),
        new-admin: new-admin,
      }),
      signature: (get signature sig-auth),
      pubkey: (get pubkey sig-auth),
    })))
    (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
    (map-delete admins 'SP000000000000000000002Q6VF78)
    (map-set admins new-admin true)
    (map-set pubkey-to-admin (get pubkey sig-auth) new-admin)
    (var-set owner new-admin)
    (update-activity)
    (var-set is-initialized true)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-admin-added new-admin))
    (ok true)
  )
)

(define-public (propose-recovery
    (new-recovery principal)
    (sig-auth {
      auth-id: uint,
      signature: (buff 65),
      pubkey: (buff 33),
    })
    (gas (optional <gas-trait>))
  )
  (begin
    (try! (is-authorized (some {
      message-hash: (contract-call?
        'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
        build-propose-recovery-hash {
        auth-id: (get auth-id sig-auth),
        new-recovery: new-recovery,
      }),
      signature: (get signature sig-auth),
      pubkey: (get pubkey sig-auth),
    })))
    (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
    (var-set pending-recovery new-recovery)
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-propose-recovery new-recovery))
    (ok true)
  )
)

(define-public (confirm-recovery)
  (let ((pending (var-get pending-recovery)))
    (asserts! (not (is-eq pending 'SP000000000000000000002Q6VF78)) err-no-pending-recovery)
    (try! (is-admin-calling tx-sender))
    (var-set recovery-address pending)
    (var-set pending-recovery 'SP000000000000000000002Q6VF78)
    (update-activity)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-confirm-recovery pending))
    (ok true)
  )
)

(define-public (recover-inactive-wallet (new-admin principal))
  (begin
    (asserts! (is-inactive) err-inactive-required)
    (asserts! (is-eq tx-sender (var-get recovery-address)) err-unauthorised)
    (map-delete admins (var-get owner))
    (map-set admins new-admin true)
    (var-set owner new-admin)
    (var-set last-activity-block burn-block-height)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-recover-inactive-wallet new-admin tx-sender))
    (ok true)
  )
)

(define-public (enroll-dual-stacking
    (dual-stacking <dual-stacking-trait>)
    (sig-auth (optional { auth-id: uint, signature: (buff 65), pubkey: (buff 33) }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (try! (is-authorized (some {
        message-hash: (contract-call?
          'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
          build-enroll-dual-stacking-hash {
          auth-id: (get auth-id sig-auth-details),
        }),
        signature: (get signature sig-auth-details),
        pubkey: (get pubkey sig-auth-details),
      })))
      (if (is-eq tx-sender FAKFUN-DEPLOYER) true (try! (is-authorized none)))
    )
    (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-enroll-dual-stacking (contract-of dual-stacking)))
    (as-contract? ()
      (try! (contract-call? dual-stacking enroll none))
    )
  )
)

(define-public (stack-stx-fast-pool
    (amount-ustx uint)
    (sig-auth (optional { auth-id: uint, signature: (buff 65), pubkey: (buff 33) }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-stack-stx-fast-pool-hash {
            auth-id: (get auth-id sig-auth-details),
            amount-ustx: amount-ustx,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
          (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )

    (try! (as-contract? ((with-all-assets-unsafe))
      (try! (match (contract-call?
        'SP000000000000000000002Q6VF78.pox-4
        allow-contract-caller
        'SP21YTSM60CAY6D011EZVEVNKXVW8FVZE198XEFFP.pox4-fast-pool-v3
        none)
        success (ok success)
        error (err (to-uint error))))))

    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-stack-stx-fast-pool amount-ustx))

    (as-contract? ((with-all-assets-unsafe))
      (try! (match (contract-call?
        'SP21YTSM60CAY6D011EZVEVNKXVW8FVZE198XEFFP.pox4-fast-pool-v3
        delegate-stx
        amount-ustx)
        success (ok true)
        error (err error))))
  )
)

(define-public (revoke-fast-pool
    (sig-auth (optional { auth-id: uint, signature: (buff 65), pubkey: (buff 33) }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-revoke-fast-pool-hash {
            auth-id: (get auth-id sig-auth-details),
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )

    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-revoke-fast-pool))

    (as-contract? ((with-all-assets-unsafe))
      (try! (match (contract-call?
        'SP000000000000000000002Q6VF78.pox-4
        revoke-delegate-stx)
        success (ok true)
        error (err (to-uint error)))))
  )
)

(define-public (stake-stx-stacking-dao
    (stx-amount uint)
    (sig-auth (optional { auth-id: uint, signature: (buff 65), pubkey: (buff 33) }))
    (gas (optional <gas-trait>))
  )
  (begin
    (update-activity)
    (match sig-auth
      sig-auth-details (begin
        (try! (is-authorized (some {
          message-hash: (contract-call?
            'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.smart-wallet-standard-auth-helpers-v3
            build-stake-stx-stacking-dao-hash {
            auth-id: (get auth-id sig-auth-details),
            stx-amount: stx-amount,
          }),
          signature: (get signature sig-auth-details),
          pubkey: (get pubkey sig-auth-details),
        })))
        (match gas g (try! (as-contract? ((with-ft SBTC-CONTRACT "sbtc-token" (var-get max-gas-amount))) (try! (contract-call? g pay-gas)))) true)
      )
      (try! (is-authorized none))
    )

    (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-stake-stx-stacking-dao stx-amount))

    (as-contract? ((with-all-assets-unsafe))
      (try! (match (contract-call?
        'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.stacking-dao-core-v6
        deposit
        'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.reserve-v1
        'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.commission-v2
        'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.staking-v0
        'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.direct-helpers-v4
        stx-amount
        (some 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22) 
        none) 
        success (ok true)
        error (err error))))
  )
)

(map-set admins 'SP000000000000000000002Q6VF78 true)

(define-public (onboard (pubkey (buff 33)))
    (begin
      (asserts! (is-eq tx-sender FAKFUN-DEPLOYER) err-unauthorised)
      (asserts! (not (var-get pubkey-initialized)) err-unauthorised)
      (var-set initial-pubkey pubkey)
      (map-set pubkey-to-admin pubkey 'SP000000000000000000002Q6VF78)
      (var-set pubkey-initialized true)
      (try! (as-contract? ()
      (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core register-wallet 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet))))
      (try! (contract-call? 'SPV9K21TBFAK4KNRJXF5DFP8N7W46G4V9RCJDC22.fakfun-wallet-core log-wallet-initialized pubkey))
      (ok true)
    )
  )

Functions (42)

FunctionAccessArgs
get-current-spentprivate
add-spent-stxprivateamount: uint
add-spent-sbtcprivateamount: uint
add-spent-zsbtcprivateamount: uint
set-max-gas-amountpublicamount: uint
get-token-lock-enabledread-only
signal-config-changepublic
create-pending-operationprivateop-type: (string-ascii 20
get-pending-operationread-onlyop-id: uint
would-exceed-stx-thresholdprivateamount: uint
would-exceed-sbtc-thresholdprivateamount: uint
would-exceed-zsbtc-thresholdprivateamount: uint
is-admin-callingread-onlycaller: principal
whitelist-extensionpublicextension: principal
is-extension-whitelistedread-onlyextension: principal
stx-transferpublicamount: uint, recipient: principal, memo: (optional (buff 34
execute-pending-stx-transferpublicop-id: uint, memo: (optional (buff 34
extension-callpublicextension: <extension-trait>, payload: (buff 2048
sip010-transferpublicamount: uint, recipient: principal, memo: (optional (buff 34
execute-pending-sbtc-transferpublicop-id: uint, memo: (optional (buff 34
execute-pending-zsbtc-transferpublicop-id: uint, memo: (optional (buff 34
sip009-transferpublicnft-id: uint, recipient: principal, sip009: <sip-009-trait>, token-name: (string-ascii 128
faktory-executepublicpool: <pool-trait>, amount: uint, opcode: (optional (buff 16
get-byteprivateopcode: (buff 16
faktory-place-orderpublicdex: <dex-trait>, token: <token-trait>, token-name: (string-ascii 128
faktory-processpublicpre: <pre-trait>, seat-count: uint, opcode: (optional (buff 16
faktory-nft-executepublicmarketplace: <nftmarket-trait>, token-id: uint, nft-contract: <sip-009-trait>, nft-name: (string-ascii 128
is-admin-pubkeyread-onlypubkey: (buff 33
propose-transfer-walletpublicnew-admin: principal
propose-admin-pubkeypublicpubkey: (buff 33
confirm-admin-pubkeypublic
signal-pubkey-cooldown-changepublicnew-period: uint
confirm-pubkey-cooldown-changepublic
remove-admin-pubkeypublicpubkey: (buff 33
verify-signatureread-onlymessage-hash: (buff 32
consume-signatureprivatemessage-hash: (buff 32
get-ownerread-only
is-inactiveread-only
update-activityprivate
confirm-recoverypublic
recover-inactive-walletpublicnew-admin: principal
onboardpublicpubkey: (buff 33