Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Constants

Constants in Ursus are compile-time values that don't change during contract execution. There are two types: global constants (outside contracts) and contract constants (inside contracts).

Contract Constants

Contract constants are defined within the Contract declaration and are accessible to all contract functions.

Syntax

Constants
  Definition CONSTANT_NAME : type := value
  Definition ANOTHER_CONSTANT : type := value
;

Important: The semicolon (;) must appear after the last constant definition.

Examples

Integer constants:

Constants
  Definition MAX_SUPPLY : uint256 := {1000000}
  Definition MIN_BALANCE : uint256 := {100}
  Definition DECIMALS : uint8 := {18}
;

Error codes:

Constants
  Definition ERR_ADDR_ZERO : uint16 := {104}
  Definition ERR_LOW_AMOUNT : uint16 := {105}
  Definition ERR_LOW_BALANCE : uint16 := {106}
  Definition ERR_NOT_SELF : uint16 := {107}
;

Boolean constants:

Constants
  Definition DEFAULT_ACTIVE : boolean := @true
  Definition ALLOW_TRANSFERS : boolean := @false
;

Address constants:

Constants
  Definition ZERO_ADDRESS : address := {0x0000000000000000000000000000000000000000}
;

Using Constants

Constants can be used anywhere in contract functions:

Ursus Definition mint (amount: uint256): UExpression PhantomType true.
{
    ::// require_ (totalSupply + amount <= MAX_SUPPLY) .
    ::// totalSupply := totalSupply + amount |.
}
return.
Defined.

Global Constants

Global constants are defined outside the contract, typically in separate modules or at the file level.

Syntax

Definition GLOBAL_CONSTANT : type := value.

Examples

Module-level constants:

Require Import UrsusEnvironment.Solidity.current.Environment.

Definition SECONDS_PER_DAY : uint256 := {86400}.
Definition DAYS_PER_YEAR : uint256 := {365}.

Module MyContract.
  (* Contract definition *)
End MyContract.

Shared constants:

(* Constants.v *)
Definition MAX_UINT256 : uint256 := {115792089237316195423570985008687907853269984665640564039457584007913129639935}.
Definition ZERO : uint256 := {0}.

Constant Expressions

Constants can be computed from other constants:

Constants
  Definition BASE_FEE : uint256 := {100}
  Definition FEE_MULTIPLIER : uint256 := {3}
  Definition TOTAL_FEE : uint256 := {300}  (* BASE_FEE * FEE_MULTIPLIER *)
;

Note: Ursus doesn't evaluate constant expressions automatically. You must compute the value manually.

Best Practices

1. Use Descriptive Names

Good:

Constants
  Definition MAX_SUPPLY : uint256 := {1000000}
  Definition MIN_TRANSFER_AMOUNT : uint256 := {1}
;

Avoid:

Constants
  Definition X : uint256 := {1000000}
  Definition Y : uint256 := {1}
;

Good:

Constants
  (* Supply limits *)
  Definition MAX_SUPPLY : uint256 := {1000000}
  Definition MIN_SUPPLY : uint256 := {0}

  (* Fee configuration *)
  Definition BASE_FEE : uint256 := {100}
  Definition MAX_FEE : uint256 := {1000}

  (* Error codes *)
  Definition ERR_INSUFFICIENT_BALANCE : uint16 := {100}
  Definition ERR_INVALID_ADDRESS : uint16 := {101}
;

3. Use Appropriate Types

Good:

Constants
  Definition DECIMALS : uint8 := {18}        (* Small number *)
  Definition MAX_SUPPLY : uint256 := {1000000}  (* Large number *)
;

Avoid:

Constants
  Definition DECIMALS : uint256 := {18}      (* Wasteful *)
;

4. Document Magic Numbers

Good:

Constants
  (* 86400 seconds = 1 day *)
  Definition SECONDS_PER_DAY : uint256 := {86400}

  (* 10^18 for 18 decimal places *)
  Definition DECIMALS_MULTIPLIER : uint256 := {1000000000000000000}
;

Common Patterns

Error Codes

Constants
  Definition ERR_NOT_OWNER : uint16 := {100}
  Definition ERR_PAUSED : uint16 := {101}
  Definition ERR_INSUFFICIENT_BALANCE : uint16 := {102}
  Definition ERR_INVALID_AMOUNT : uint16 := {103}
;

Usage:

::// require_ (msg->sender == owner) or_throw ERR_NOT_OWNER .

Configuration Values

Constants
  Definition LOCK_PERIOD : uint256 := {86400}  (* 1 day *)
  Definition MIN_STAKE : uint256 := {1000}
  Definition REWARD_RATE : uint256 := {5}  (* 5% *)
;

Flags and Masks

Constants
  Definition FLAG_SEND_ALL : uint8 := {128}
  Definition FLAG_IGNORE_ERRORS : uint8 := {2}
  Definition FLAG_BOUNCE : uint8 := {64}
;

Complete Example

#[translation = on]
#[language = solidity]
Contract TokenSale ;
Sends To IERC20 ;

Types ;

Constants
  (* Token configuration *)
  Definition TOKEN_PRICE : uint256 := {1000}
  Definition MIN_PURCHASE : uint256 := {100}
  Definition MAX_PURCHASE : uint256 := {10000}

  (* Sale phases *)
  Definition PHASE_PRESALE : uint8 := {0}
  Definition PHASE_PUBLIC : uint8 := {1}
  Definition PHASE_ENDED : uint8 := {2}

  (* Error codes *)
  Definition ERR_SALE_NOT_ACTIVE : uint16 := {100}
  Definition ERR_AMOUNT_TOO_LOW : uint16 := {101}
  Definition ERR_AMOUNT_TOO_HIGH : uint16 := {102}
;

Record Contract := {
    token: address;
    current_phase: uint8;
    total_sold: uint256
}.

See Also