### Install ethabi CLI Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Install the command-line tool using cargo. ```bash cargo install ethabi-cli ``` -------------------------------- ### Use ethabi-cli for encoding and decoding Source: https://context7.com/rust-ethereum/ethabi/llms.txt Perform encoding and decoding operations directly from the command line for testing and scripting. ```bash # Install the CLI cargo install ethabi-cli # Encode simple parameters ethabi encode params -v bool 1 # Output: 0000000000000000000000000000000000000000000000000000000000000001 # Encode multiple parameters ethabi encode params -v bool 1 -v string "hello" -v uint256 1000 # Output: encoded hex string # Encode with lenient parsing (decimal numbers) ethabi encode params -v uint256 1000000000000000000 --lenient ethabi encode params -v uint256 1ether --lenient # Supports ether/gwei/wei units # Encode a tuple ethabi encode params -v '(string,bool,string)' '(test,1,cyborg)' # Encode a function call from ABI file ethabi encode function ./erc20.abi transfer -p 0x1111111111111111111111111111111111111111 -p 1000 # Handle overloaded functions with signature ethabi encode function ./contract.abi bar(bool) -p 1 ethabi encode function ./contract.abi 'bar(string):(uint256)' -p "test" # Decode parameters ethabi decode params -t bool \ 0000000000000000000000000000000000000000000000000000000000000001 # Output: bool true # Decode multiple types ethabi decode params -t bool -t string -t uint256 # Decode function output ethabi decode function ./erc20.abi balanceOf # Decode event log ethabi decode log ./erc20.abi Transfer \ -l -l \ # Decode with event signature for overloaded events ethabi decode log ./contract.abi 'Event(bool,address)' -l ``` -------------------------------- ### ethabi CLI Usage Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Command-line interface reference for encoding and decoding ABI data. ```text Ethereum ABI coder. Copyright 2016-2017 Parity Technologies (UK) Limited Usage: ethabi encode function [-p ]... [-l | --lenient] ethabi encode params [-v ]... [-l | --lenient] ethabi decode function ethabi decode params [-t ]... ethabi decode log [-l ]... ethabi -h | --help Options: -h, --help Display this message and exit. -l, --lenient Allow short representation of input params. Commands: encode Encode ABI call. decode Decode ABI call result. function Load function from json ABI file. params Specify types of input params inline. log Decode event log. ``` -------------------------------- ### Create Ethereum Event Filters with ethabi Source: https://context7.com/rust-ethereum/ethabi/llms.txt Use `Event::filter` to construct topic filters for `eth_getLogs` or `eth_subscribe`. Supports filtering by specific topics or multiple possible values for a topic. ```rust use ethabi::{Event, EventParam, ParamType, RawTopicFilter, Token, Topic}; // Define an Approval event let approval_event = Event { name: "Approval".to_owned(), inputs: vec![ EventParam { name: "owner".to_owned(), kind: ParamType::Address, indexed: true }, EventParam { name: "spender".to_owned(), kind: ParamType::Address, indexed: true }, EventParam { name: "value".to_owned(), kind: ParamType::Uint(256), indexed: false }, ], anonymous: false, }; // Create a filter for approvals from a specific owner let owner_address = Token::Address([0x11u8; 20].into()); let raw_filter = RawTopicFilter { topic0: Topic::This(owner_address), topic1: Topic::Any, // Any spender topic2: Topic::Any, }; let filter = approval_event.filter(raw_filter).expect("Failed to create filter"); println!("Topic 0 (event sig): {:?}", filter.topic0); println!("Topic 1 (owner): {:?}", filter.topic1); println!("Topic 2 (spender): {:?}", filter.topic2); // Filter for multiple possible spenders let spender1 = Token::Address([0x22u8; 20].into()); let spender2 = Token::Address([0x33u8; 20].into()); let multi_filter = RawTopicFilter { topic0: Topic::Any, topic1: Topic::OneOf(vec![spender1, spender2]), topic2: Topic::Any, }; ``` -------------------------------- ### Encode Constructor Arguments for Contract Deployment Source: https://context7.com/rust-ethereum/ethabi/llms.txt Use `Constructor::encode_input` to append encoded constructor arguments to contract bytecode for deployment. Ensure the `hex` crate is available for decoding bytecode. ```rust use ethabi::{Constructor, Param, ParamType, Token}; // Define a constructor that takes an initial supply and owner let constructor = Constructor { inputs: vec![ Param { name: "initialSupply".to_owned(), kind: ParamType::Uint(256), internal_type: None }, Param { name: "owner".to_owned(), kind: ParamType::Address, internal_type: None }, ], }; // Contract bytecode (simplified example) let bytecode = hex::decode("608060405234801561001057600080fd5b50").unwrap(); // Constructor arguments let tokens = vec![ Token::Uint(1_000_000u64.into()), Token::Address([0xABu8; 20].into()), ]; // Encode bytecode + constructor args for deployment let deployment_data = constructor.encode_input(bytecode, &tokens) .expect("Failed to encode constructor"); println!("Deployment data: 0x{}", hex::encode(&deployment_data)); ``` -------------------------------- ### Load a Contract ABI in Rust Source: https://context7.com/rust-ethereum/ethabi/llms.txt Parses a JSON ABI file into a Contract struct to access functions, events, and constructors. ```rust use ethabi::Contract; use std::fs::File; // Load an ERC-20 token contract ABI let file = File::open("eip20.abi").expect("Failed to open ABI file"); let contract = Contract::load(file).expect("Failed to parse ABI"); // Access contract components if let Some(constructor) = contract.constructor() { println!("Constructor has {} inputs", constructor.inputs.len()); } // Get a specific function by name let transfer_fn = contract.function("transfer").expect("Function not found"); println!("Function signature: {}", transfer_fn.signature()); // Iterate over all functions for func in contract.functions() { println!("Function: {} with {} inputs", func.name, func.inputs.len()); } // Get a specific event let transfer_event = contract.event("Transfer").expect("Event not found"); println!("Event signature: {:?}", transfer_event.signature()); ``` -------------------------------- ### Parse Event Logs with ethabi Source: https://context7.com/rust-ethereum/ethabi/llms.txt Decodes raw transaction log data into structured event parameters, supporting both indexed topics and non-indexed data fields. ```rust use ethabi::{Event, EventParam, ParamType, RawLog, Token}; // Define a Transfer event (ERC-20 style) let transfer_event = Event { name: "Transfer".to_owned(), inputs: vec![ EventParam { name: "from".to_owned(), kind: ParamType::Address, indexed: true }, EventParam { name: "to".to_owned(), kind: ParamType::Address, indexed: true }, EventParam { name: "value".to_owned(), kind: ParamType::Uint(256), indexed: false }, ], anonymous: false, }; // Create a raw log from transaction receipt data let raw_log = RawLog { topics: vec![ // Topic 0: Event signature hash transfer_event.signature(), // Topic 1: Indexed 'from' address (padded to 32 bytes) "0x000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .parse().unwrap(), // Topic 2: Indexed 'to' address "0x000000000000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" .parse().unwrap(), ], // Non-indexed data: 'value' as uint256 data: hex::decode( "0000000000000000000000000000000000000000000000000de0b6b3a7640000" ).unwrap(), }; // Parse the log let log = transfer_event.parse_log(raw_log).expect("Failed to parse log"); // Access decoded parameters for param in log.params { println!("{}: {}", param.name, param.value); } // Output: // from: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa // to: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb // value: de0b6b3a7640000 ``` -------------------------------- ### Encode Functions Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Encodes function calls using an ABI JSON file and specific function signatures. ```bash ethabi encode function examples/test.json foo -p 1 ``` ```bash ethabi encode function examples/test.json foo(bool) -p 1 ``` ```bash ethabi encode function examples/test.json bar(bool) -p 1 ``` ```bash ethabi encode function examples/test.json bar(string):(uint256) -p 1 ``` -------------------------------- ### Encode Function Calls in Rust Source: https://context7.com/rust-ethereum/ethabi/llms.txt Encodes function parameters into ABI-compliant bytecode using the Function::encode_input method. ```rust use ethabi::{Function, Param, ParamType, StateMutability, Token}; // Define a transfer function (ERC-20 style) let transfer = Function { name: "transfer".to_owned(), inputs: vec![ Param { name: "_to".to_owned(), kind: ParamType::Address, internal_type: None }, Param { name: "_value".to_owned(), kind: ParamType::Uint(256), internal_type: None }, ], outputs: vec![ Param { name: "success".to_owned(), kind: ParamType::Bool, internal_type: None }, ], constant: None, state_mutability: StateMutability::NonPayable, }; // Prepare the parameters as tokens let recipient = Token::Address([0x11u8; 20].into()); let amount = Token::Uint(1000u64.into()); // Encode the function call let encoded = transfer.encode_input(&[recipient, amount]) .expect("Failed to encode"); // Result: 4-byte selector + 32-byte address + 32-byte uint256 println!("Encoded call: 0x{}", hex::encode(&encoded)); // Output: 0xa9059cbb0000000000000000000000001111111111111111111111111111111111111111 // 00000000000000000000000000000000000000000000000000000000000003e8 ``` -------------------------------- ### Encode Parameters Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Encodes various parameter types into their hex representation. ```bash ethabi encode params -v bool 1 ``` ```bash ethabi encode params -v bool 1 -v string gavofyork -v bool 0 ``` ```bash ethabi encode params -v bool[] [1,0,false] ``` ```bash ethabi encode params -v '(string,bool,string)' '(test,1,cyborg)' ``` -------------------------------- ### Encode Raw Parameters with ethabi Source: https://context7.com/rust-ethereum/ethabi/llms.txt Encodes a slice of tokens into ABI format without a function selector. Useful for constructor arguments or raw data manipulation. ```rust use ethabi::{encode, Token}; // Encode a boolean let encoded_bool = encode(&[Token::Bool(true)]); assert_eq!(hex::encode(&encoded_bool), "0000000000000000000000000000000000000000000000000000000000000001"); // Encode multiple parameters let tokens = vec![ Token::Bool(true), Token::String("gavofyork".to_owned()), Token::Bool(false), ]; let encoded = encode(&tokens); println!("Encoded params: 0x{}", hex::encode(&encoded)); // Encode an array of addresses let addresses = Token::Array(vec![ Token::Address([0x11u8; 20].into()), Token::Address([0x22u8; 20].into()), ]); let encoded_array = encode(&[addresses]); println!("Encoded array: 0x{}", hex::encode(&encoded_array)); // Encode a tuple (struct) let tuple = Token::Tuple(vec![ Token::Uint(42u64.into()), Token::String("hello".to_owned()), Token::Address([0x33u8; 20].into()), ]); let encoded_tuple = encode(&[tuple]); println!("Encoded tuple: 0x{}", hex::encode(&encoded_tuple)); ``` -------------------------------- ### Decode Log Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Decodes event logs using an ABI JSON file and log data. ```bash ethabi decode log ./examples/event.json Event -l 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000004444444444444444444444444444444444444444 ``` -------------------------------- ### Decode Raw Parameters with ethabi Source: https://context7.com/rust-ethereum/ethabi/llms.txt Decodes ABI-encoded bytes into tokens using expected parameter types. Use decode_validate for strict validation to ensure no trailing bytes remain. ```rust use ethabi::{decode, decode_validate, ParamType, Token}; // Decode a boolean let data = hex::decode( "0000000000000000000000000000000000000000000000000000000000000001" ).unwrap(); let tokens = decode(&[ParamType::Bool], &data).unwrap(); assert_eq!(tokens[0], Token::Bool(true)); // Decode multiple parameters (bool, string, bool) let data = hex::decode(concat!( "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000060", "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000009", "6761766f66796f726b0000000000000000000000000000000000000000000000" )).unwrap(); let tokens = decode( &[ParamType::Bool, ParamType::String, ParamType::Bool], &data ).unwrap(); if let Token::String(s) = &tokens[1] { println!("Decoded string: {}", s); // "gavofyork" } // Decode with strict validation (fails if extra bytes present) let result = decode_validate(&[ParamType::Address], &data); assert!(result.is_err()); // Too much data for just an address ``` -------------------------------- ### Parse and validate ParamType in Rust Source: https://context7.com/rust-ethereum/ethabi/llms.txt Use the Reader to parse Solidity type strings into ParamType enums and check for dynamic types. ```rust use ethabi::param_type::{ParamType, Reader}; // Parse type strings let address_type = Reader::read("address").unwrap(); assert_eq!(address_type, ParamType::Address); let uint_type = Reader::read("uint256").unwrap(); assert_eq!(uint_type, ParamType::Uint(256)); let bytes32_type = Reader::read("bytes32").unwrap(); assert_eq!(bytes32_type, ParamType::FixedBytes(32)); // Array types let array_type = Reader::read("uint256[]").unwrap(); assert_eq!(array_type, ParamType::Array(Box::new(ParamType::Uint(256)))); let fixed_array = Reader::read("address[3]").unwrap(); assert_eq!(fixed_array, ParamType::FixedArray(Box::new(ParamType::Address), 3)); // Tuple (struct) types let tuple_type = Reader::read("(address,uint256,bool)").unwrap(); assert_eq!(tuple_type, ParamType::Tuple(vec![ ParamType::Address, ParamType::Uint(256), ParamType::Bool, ])); // Nested tuple with array let complex = Reader::read("(address,(uint256,string)[])").unwrap(); // Check if type is dynamic assert!(!ParamType::Address.is_dynamic()); assert!(ParamType::String.is_dynamic()); assert!(ParamType::Array(Box::new(ParamType::Bool)).is_dynamic()); ``` -------------------------------- ### Decode Parameters Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Decodes hex-encoded data back into human-readable parameters. ```bash ethabi decode params -t bool 0000000000000000000000000000000000000000000000000000000000000001 ``` ```bash ethabi decode params -t bool -t string -t bool 00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096761766f66796f726b0000000000000000000000000000000000000000000000 ``` ```bash ethabi decode params -t bool[] 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ``` ```bash ethabi decode params -t '(string,bool,string)' 00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000673706972616c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000067175617361720000000000000000000000000000000000000000000000000000 ``` -------------------------------- ### Utilize the Token Type for Solidity Data Source: https://context7.com/rust-ethereum/ethabi/llms.txt The `Token` enum represents all Solidity types and is used for encoding parameters and decoding return values. It supports type checking and value extraction. ```rust use ethabi::{ParamType, Token}; // Create various token types let address = Token::Address([0x11u8; 20].into()); let uint = Token::Uint(1000u64.into()); let int = Token::Int((-100i64).into()); // Signed integer let boolean = Token::Bool(true); let string = Token::String("hello world".to_owned()); let bytes = Token::Bytes(vec![0x01, 0x02, 0x03]); let fixed_bytes = Token::FixedBytes(vec![0xab, 0xcd]); // Arrays and tuples let array = Token::Array(vec![Token::Uint(1u64.into()), Token::Uint(2u64.into())]); let fixed_array = Token::FixedArray(vec![Token::Bool(true), Token::Bool(false)]); let tuple = Token::Tuple(vec![ Token::Address([0x22u8; 20].into()), Token::Uint(500u64.into()), ]); // Type checking assert!(address.type_check(&ParamType::Address)); assert!(uint.type_check(&ParamType::Uint(256))); assert!(array.type_check(&ParamType::Array(Box::new(ParamType::Uint(256))))); // Extract values from tokens let address_value = address.into_address().expect("Expected address"); let uint_value = uint.into_uint().expect("Expected uint"); let tuple_values = tuple.into_tuple().expect("Expected tuple"); // Check if dynamic (affects encoding) assert!(!Token::Bool(true).is_dynamic()); assert!(Token::String("test".into()).is_dynamic()); assert!(Token::Array(vec![]).is_dynamic()); ``` -------------------------------- ### Decode Function Source: https://github.com/rust-ethereum/ethabi/blob/master/README.md Decodes function call data using an ABI JSON file. ```bash ethabi decode function ./examples/foo.json bar 0000000000000000000000000000000000000000000000000000000000000001 ``` -------------------------------- ### Decode Function Output in Rust Source: https://context7.com/rust-ethereum/ethabi/llms.txt Decodes raw return data from a contract call into typed tokens using Function::decode_output. ```rust use ethabi::{Function, Param, ParamType, Token}; // Define a balanceOf function let balance_of = Function { name: "balanceOf".to_owned(), inputs: vec![ Param { name: "_owner".to_owned(), kind: ParamType::Address, internal_type: None }, ], outputs: vec![ Param { name: "balance".to_owned(), kind: ParamType::Uint(256), internal_type: None }, ], constant: None, state_mutability: ethabi::StateMutability::View, }; // Raw output from an eth_call (32 bytes representing uint256) let output_data = hex::decode( "0000000000000000000000000000000000000000000000000de0b6b3a7640000" ).unwrap(); // Decode the output let tokens = balance_of.decode_output(&output_data) .expect("Failed to decode output"); // Extract the balance value if let Token::Uint(balance) = &tokens[0] { println!("Balance: {} wei", balance); // 1000000000000000000 wei = 1 ETH } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.