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?
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?
You can use unwrap-panic
in a function, e.g.
(define-private (require-true (value bool))
(unwrap-panic (if value (some true) none))
)
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))