clarity-lang / reference

The Clarity Reference
149 stars 34 forks source link

read-only functions can not call trait functions even if the implementation uses a read-only function #67

Open obycode opened 1 year ago

obycode commented 1 year ago

This is duplicated from https://github.com/stacks-network/stacks-blockchain/issues/1981:

The following code

(define-read-only (symbol)
  (contract-call? 'ST3J2GVMMM2R07ZFBJDWTYEYAR8FZH5WKDTFJ9AHA.swapr symbol 'ST3J2GVMMM2R07ZFBJDWTYEYAR8FZH5WKDTFJ9AHA.plaid-token 'ST3J2GVMMM2R07ZFBJDWTYEYAR8FZH5WKDTFJ9AHA.stx-token)
)

generates an error expecting read-only statements, detected a writing operation. (i.e. WriteAttemptedInReadOnly) when deploying, even if contract-call? is calling an other define-read-only function.

This should be allowed.

The latest proposal from the issue suggests adding a map to the trait definition with a field indicating if the function must be read-only:

(define-trait src20-trait
  (
    ;; Transfer from the caller to a new principal
    (transfer (principal uint) (response bool uint))

    ;; the human readable name of the token
    (name () (response (string-ascii 32) uint) {read-only: true})

    ;; the ticker symbol, or empty if none
    (symbol () (response (string-ascii 32) uint) {read-only: true})

    ;; the number of decimals used, e.g. 6 would mean 1_000_000 represents 1 token
    (decimals () (response uint uint) {read-only: true})

    ;; the balance of the passed principal
    (balance-of (principal) (response uint uint) {read-only: true})

    ;; the current total supply (which does not need to be a constant)
    (total-supply () (response uint uint) {read-only: true})
  )
)