Derive Macro ethers_contract::EthAbiCodec

source ·
#[derive(EthAbiCodec)]
Available on crate feature abigen only.
Expand description

Derives the AbiEncode and AbiDecode traits for the labeled type.

This is separate from other derive macros because this derives a generic codec implementation for structs, while EthEvent and others derive a specialized implementation.

Note that this macro requires the EthAbiType macro to be derived or for the type to implement AbiType and Tokenizable. The type returned by the AbiType implementation must be a Token::Tuple, otherwise this macro’s implementation of AbiDecode will panic at runtime.

§Examples

use ethers_contract_derive::{EthAbiCodec, EthAbiType};
use ethers_core::types::Address;
use ethers_core::abi::{AbiDecode, AbiEncode};

#[derive(Clone, Debug, Default, PartialEq, EthAbiType, EthAbiCodec)]
struct MyStruct {
    addr: Address,
    old_value: String,
    new_value: String,
}

let value = MyStruct::default();
let encoded = value.clone().encode();
let decoded = MyStruct::decode(&encoded).unwrap();
assert_eq!(decoded, value);