Constants are data members that cannot be changed once they are defined (hence the name constant). They are useful to define concrete configuration values, error codes, and more. The general form to define a constant looks like this:
(define-constant constant-name expression)
constant-name can be any valid phrase and the
expression any valid
The expression passed into the definition is evaluated at contract launch in the order that it is supplied. If one constant thus depends on another, they need to be defined in the right order.
(define-constant my-constant "This is a constant value") (define-constant my-second-constant (concat my-constant " that depends on another") ) (print my-constant) (print my-second-constant)
A common pattern that you will come across is that of defining a constant to store the principal that deployed the contract:
(define-constant contract-owner tx-sender) (print contract-owner)
Constants are also useful to give return values and errors meaningful names.
(define-constant err-something-failed (err u100)) ;; And then use err-something-failed instead of (err u100) later in the code. (print err-something-failed)
If you are curious about the
... to be a completely separate network and separate block chain, yet share CPU power with Bitcoin`` - Satoshi Nakamoto