Intelligent Contracts
Types

Types in Intelligent Contracts

GenVM provides type support for both contract development (storage/state variables) and contract interaction (calldata). Understanding these types is crucial for developing robust Intelligent Contracts.

Contract Development Types

When writing Intelligent Contracts, you can use the following types for state variables and method parameters:

Primitive Types

  • Integers

    • u8, u16, u32, u64, u128, u256 - Unsigned integers of various sizes
    • i8, i16, i32, i64, i128, i256 - Signed integers of various sizes
    • bigint - Arbitrary precision integer (use with caution, prefer sized integers)
  • Other Primitives

    • bool - Boolean values (True/False)
    • str - UTF-8 encoded string
    • bytes - Raw byte sequences
    • Address - Blockchain address (20 bytes)

Collection Types

  • Arrays
    • DynArray[T] - Dynamic array of type T (replaces Python's list[T])
  • Maps
    • TreeMap[K, V] - Ordered map from key type K to value type V (replaces Python's dict[K, V])

Custom Types

You can use custom Python classes in storage, for example:

@dataclass
class User:
  name: str
  balance: u256
 
@gl.contract
class Contract:
  users: DynArray[User]

Calldata Types

When interacting with contract methods, the following types are supported:

  • Basic Types

    • Integers (converted to appropriate sized type)
    • Strings
    • Bytes
    • Booleans
    • None
    • Address (as hex string with '0x' prefix)
  • Collections

    • Lists (converted to DynArray)
    • Dictionaries (must have string keys, converted to TreeMap)

Type Restrictions and Best Practices

Storage Type Restrictions

  1. No Raw Python Collections

    • Use DynArray[T] instead of list[T]
    • Use TreeMap[K, V] instead of dict[K, V]
  2. No Raw Integers

    • Use sized integers (u256, i64, etc.) instead of int
    • Use bigint only when absolutely necessary
  3. Generic Types Must Be Fully Specified

  # ❌ Wrong
  storage: TreeMap  # Not fully specified
  
  # âś… Correct
  storage: TreeMap[str, u256]  # Fully specified

Type Conversion in Calldata

When calling contract methods:

  • String keys are required for dictionary parameters
  • Numbers are automatically converted to appropriate sized integers
  • Addresses should be provided as hex strings with '0x' prefix