How to require a true value in Clarity smart contract?

89 views Asked by At

In my smart contract, I want to check whether a boolean value is true, if not the smart contract should abort or throw an error like

(begin
   (require-true value)
   ...continue
)

How can I do that?

2

There are 2 answers

0
criadoperez On

asserts! will return true and continue the execution only if the boolean expression it evaluates is true. If not it will return the thrown-value and exit the current control flow.

Signature is: (asserts! boolean-expression thrown-value)

Example that will return true and continue execution:

(asserts! (is-eq 1 1) (err 1))

0
friedger On

You can use unwrap-panic in a function, e.g.

(define-private (require-true (value bool))
  (unwrap-panic (if value (some true) none))
)