Public function smart contract Clarity Stacks

35 views Asked by At

I am writing a public function that only owner of the contract can call. It is a funciton that changes a state variable in a smart conract.

(define-public (changeRatio (newRatio uint))
  (begin
    (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)
    (var-set ratio newRatio)
  )
)

and I get

error: detected two execution paths, returning two different expression types (got '(response UnknownType uint)' and 'bool')

what am I doing wrong here?

1

There are 1 answers

0
Kenny On

Public functions in Clarity have to return a response type at the end. See the Clarity book for more info on this: https://book.clarity-lang.org/ch05-01-public-functions.html

To solve your particular error, you could wrap your var-set in an ok response type like this:

(ok (var-set ratio newRatio))

Note that this will only work if your ERR-NOT-AUTHORIZED is an err and not just a uint, since err and ok are both response types.