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 = sol("transfer(address,uint256)")] 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”use std::abi::sol
msg Example { #[selector = sol("withPrimitives(bool,uint256,int128)")] WithPrimitives { flag: bool, count: u256, signed_value: i128, },}Compound Types
Section titled “Compound Types”use std::abi::sol
msg Example { #[selector = sol("withTuple(uint256,uint256)")] WithTuple { coords: (u256, u256) } -> bool,
}No Fields
Section titled “No Fields”Variants can have no fields:
use std::abi::sol
msg Query { #[selector = sol("totalSupply()")] TotalSupply -> u256,
#[selector = sol("name()")] Name -> String<256>,}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 = sol("transfer(address,uint256)")] Transfer { to: u256, amount: u256 },}
msg Example2 { #[selector = sol("transfer(address,uint256)")] 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 |