Message Fields
Message fields define the parameters that callers pass when invoking a message variant.
Field Syntax
Section titled “Field Syntax”Fields are defined inside curly braces, similar to struct fields:
#[selector = 0xa9059cbb] Transfer { to: u256, amount: u256 } -> bool,Each field has:
- A name (
to,amount) - A type (
u256)
Multiple fields are separated by commas.
Supported Types
Section titled “Supported Types”Message fields support all Fe types:
Primitive Types
Section titled “Primitive Types”msg Example { #[selector = 0x00000001] WithPrimitives { flag: bool, count: u256, signed_value: i128, },}Compound Types
Section titled “Compound Types”msg Example { #[selector = 0x00000002] WithTuple { coords: (u256, u256) } -> bool,
}No Fields
Section titled “No Fields”Variants can have no fields:
msg Query { #[selector = 0x18160ddd] TotalSupply -> u256,
#[selector = 0x06fdde03] Name -> String,}Field Order Matters
Section titled “Field Order Matters”Field order is significant for ABI encoding. The order in which fields are defined determines how calldata is decoded:
// These are different!msg Example1 { #[selector = 0xa9059cbb] Transfer { to: u256, amount: u256 },}
msg Example2 { #[selector = 0xa9059cbb] Transfer { amount: u256, to: u256 },}When implementing standard interfaces like ERC20, ensure field order matches the specification.
Accessing Fields in Handlers
Section titled “Accessing Fields in Handlers”In recv blocks, destructure fields to access their values:
recv TokenMsg { Transfer { to, amount } -> bool { // 'to' and 'amount' are available here true } }You can also rename fields during destructuring:
recv TokenMsg { Transfer { to: recipient, amount: value } -> bool { // Use 'recipient' and 'value' instead true } }Ignoring Fields
Section titled “Ignoring Fields”Use _ to ignore fields you don’t need:
recv TokenMsg { Transfer { to, amount: _ } -> bool { // Only use 'to', ignore amount true } }Use .. to ignore remaining fields:
recv TokenMsg { TransferFrom { from, .. } -> bool { // Only use 'from', ignore to and amount true } }Summary
Section titled “Summary”| Pattern | Meaning |
|---|---|
{ field: Type } | Named field with type |
{ a, b, c } | Multiple fields |
| (no braces) | No parameters |
{ field } | Destructure keeping name |
{ field: name } | Destructure with rename |
{ field: _ } | Ignore specific field |
{ field, .. } | Ignore remaining fields |