stacks-network / stacks-network/docs

Improve as-contract? documentation for Clarity 4

Open
#1,840 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
185
Forks
273
Avg merge
4d 13h
Merged PRs (30d)
4

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the as-contract? entry on the Clarity functions documentation and compare its current signatures with SIP-033. Review the referenced sBTC lending example, especially lines 119-122, to verify the allowance syntax and usage. Done means the page explains all with-ft parameters, shows a complete example, documents return values, and links to SIP-033.

Written by the indexing model from the issue text.

Assessment

Domain
blockchain, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.