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 sizesi8
,i16
,i32
,i64
,i128
,i256
- Signed integers of various sizesbigint
- Arbitrary precision integer (use with caution, prefer sized integers)
-
Other Primitives
bool
- Boolean values (True/False)str
- UTF-8 encoded stringbytes
- Raw byte sequencesAddress
- Blockchain address (20 bytes)
Collection Types
- Arrays
DynArray[T]
- Dynamic array of type T (replaces Python'slist[T]
)
- Maps
TreeMap[K, V]
- Ordered map from key type K to value type V (replaces Python'sdict[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
)
- Lists (converted to
Type Restrictions and Best Practices
Storage Type Restrictions
-
No Raw Python Collections
- Use
DynArray[T]
instead oflist[T]
- Use
TreeMap[K, V]
instead ofdict[K, V]
- Use
-
No Raw Integers
- Use sized integers (
u256
,i64
, etc.) instead ofint
- Use
bigint
only when absolutely necessary
- Use sized integers (
-
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