less than

Using the less than function for comparisons in Clarity smart contracts.

The less than function (<) in Clarity compares two values and returns true if the first value is less than the second. It's a fundamental comparison operation used in many smart contract conditions and logic flows.

Function Signature

(< v1 v2)
  • Input: Two values of the same type (int, uint, string-ascii, string-utf8, or buff)
  • Output: A boolean (true or false)

Why it matters

The less than function is crucial for:

  1. Implementing conditional logic in smart contracts.
  2. Comparing numerical values for financial operations.
  3. Ordering and sorting data.
  4. Implementing maximum thresholds or limits in contract logic.

When to use it

Use the less than function when you need to:

  • Compare two numerical values to determine if one is smaller.
  • Implement maximum thresholds for certain operations.
  • Create conditional logic based on numerical comparisons.
  • Sort or order data based on numerical or lexicographical order.

Best Practices

  • Ensure that both inputs are of the same type to avoid runtime errors.
  • Be aware of the differences in comparison between signed (int) and unsigned (uint) integers.
  • When comparing strings or buffers, understand that the comparison is lexicographical.
  • Consider edge cases, especially when dealing with the limits of integer ranges.

Practical Example: Auction Contract with Maximum Bid

Let's implement a simple auction contract that uses the less than function to enforce a maximum bid amount:

;; Define constants
(define-constant MAX_BID u1000000000) ;; 1000 STX
(define-constant AUCTION_END_HEIGHT u100)

;; Define data variables
(define-data-var current-highest-bid uint u0)
(define-data-var highest-bidder (optional principal) none)

;; Function to place a bid
(define-public (place-bid (bid uint))
  (let
    ((current-block-height block-height))
    ;; Check if the auction is still open
    (asserts! (< current-block-height AUCTION_END_HEIGHT) (err u1))
    ;; Check if the bid is less than the maximum allowed bid
    (asserts! (< bid MAX_BID) (err u2))
    ;; Check if the bid is higher than the current highest bid
    (asserts! (> bid (var-get current-highest-bid)) (err u3))
    ;; Place the bid
    (try! (stx-transfer? bid tx-sender (as-contract tx-sender)))
    (var-set current-highest-bid bid)
    (var-set highest-bidder (some tx-sender))
    (ok bid)))

;; Function to check current highest bid
(define-read-only (get-highest-bid)
  (ok (var-get current-highest-bid)))

;; Function to check if auction has ended
(define-read-only (auction-ended)
  (>= block-height AUCTION_END_HEIGHT))

This example demonstrates:

  1. Using < to check if the current block height is less than the auction end height.
  2. Using < to ensure the bid is less than the maximum allowed bid.
  3. Combining the less than check with other contract logic for an auction system.

Common Pitfalls

  1. Comparing values of different types, which will result in a runtime error.
  2. Not considering the inclusive nature of <= vs the exclusive nature of < when setting thresholds.
  3. Overlooking potential integer underflow when working with very small numbers.
  • >: Used for greater than comparisons.
  • <=: Used for less than or equal to comparisons.
  • >=: Used for greater than or equal to comparisons.

Conclusion

The less than function is a fundamental tool for implementing comparison logic in Clarity smart contracts. By understanding its behavior with different types and potential edge cases, developers can create robust conditional logic and enforce important thresholds in their contracts.