Skip to main content

function_id

Attribute Macro function_id 

Source
#[function_id]
Expand description

Function ID attribute for overriding function selectors in smart contracts.

Specifies a custom 4-byte selector that identifies function calls in the ABI.

§Formats

  • Solidity signature: #[function_id("transfer(address,uint256)")]
  • Hex string: #[function_id("0xa9059cbb")]
  • Byte array: #[function_id([169, 5, 156, 187])]

§Validation

Optional validation ensures the selector matches the function signature:

// Verify hex selector matches the function signature
#[function_id("0xa9059cbb", validate(true))]
fn transfer(&self, to: Address, amount: U256) -> bool { ... }

// Verify function implementation matches the signature
#[function_id("transfer(address,uint256)", validate(true))]
fn transfer(&self, to: Address, amount: U256) -> bool { ... }

Validation is useful when:

  • Ensuring type conversions produce the expected selector
  • Maintaining compatibility with existing contracts
  • During code refactoring to catch selector changes

By default, validation is disabled and selectors are used as-is.

§Example

#[function_id("greeting(string)")]
fn greeting(&self, message: String) -> String {
    message
}