Skip to content

Improve as-contract? documentation for Clarity 4 #1840

@ClaraDevRel

Description

@ClaraDevRel

Problem

The current documentation for as-contract? at https://docs.stacks.co/reference/clarity/functions lacks critical details needed to actually use the function. Developers are struggling to implement Clarity 4 contracts because:

  1. Incomplete with-ft syntax: The docs don't clearly show that with-ft takes THREE parameters: (with-ft contract-id token-name amount)
  2. Missing concrete examples: No working code examples showing real-world usage
  3. Unclear allowance syntax: The double parentheses around allowances ((with-ft ...)) vs single (with-ft ...) causes confusion

Current Documentation Gap

The docs show signatures but don't provide complete working examples. For instance:

  • What does "token-name" actually mean? (It's the string from define-fungible-token)
  • How do you structure the allowances list? (Double parens: ((with-ft ...) (with-stx ...)))
  • What's the exact return type when an allowance is violated? (Answered in SIP-033 but not in docs)

Suggested Improvements

1. Add Complete Working Example

;; Transfer FT from contract to user with explicit allowance
(define-public (withdraw-tokens (amount uint) (recipient principal))
  (unwrap! (as-contract? ((with-ft 'ST...token-contract "my-token" amount))
             (unwrap! (contract-call? 'ST...token-contract transfer 
                        amount 
                        tx-sender  ;; Inside as-contract?, this is the contract
                        recipient 
                        none)
                      (err u1)))
           (err u2)))

2. Document with-ft Parameters Clearly

(with-ft contract-id token-name amount)
  • contract-id: Principal - The contract defining the FT ('ST...address.contract-name)
  • token-name: (string-ascii 128) - Name from define-fungible-token call (e.g., "my-token")
  • amount: uint - Maximum amount allowed to transfer

3. Show Return Values

Returns (response A uint):

  • (ok result) - Success, result is the final body expression
  • (err index) - Allowance violated, index shows which one (0-based)
  • (err u128) - Asset transfer with no matching allowance

4. Link to SIP-033

The complete specification is in SIP-033 which has excellent examples. The docs should reference this.

Real-World Impact

I spent hours debugging as-contract? because the docs didn't explain:

  • That with-ft needs the token NAME string (not just contract-id + amount)
  • The allowances need double parentheses: ((with-ft ...) (with-stx ...))
  • The function returns a response, so you need unwrap! or similar

This is blocking Clarity 4 adoption. Better docs = faster ecosystem growth.

Reference Implementation

See a complete working example here: https://github.com/bastiatai/sbtc-lending/blob/main/contracts/sbtc-lending-pool.clar

Lines 119-122 show proper as-contract? with with-ft usage for sBTC transfers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions