Derive Macro ethers_contract::Eip712

source ·
#[derive(Eip712)]
{
    // Attributes available to this derive:
    #[eip712]
}
Available on crate feature abigen only.
Expand description

Derives the Eip712 trait for the labeled type.

Encodes a Rust struct into a payload hash, according to eip-712.

The following traits are required to be implemented for the struct:

§Attribute parameters

Required:

  • name = "...": The name of the EIP712 domain separator.
  • version = "...": The version of the EIP712 domain separator.
  • chain_id = ...: The chain id of the EIP712 domain separator.
  • verifying_contract = "...": The verifying contract’s address of the EIP712 domain separator.

Optional:

  • salt = "..." or raw_salt = "...": The salt of the EIP712 domain separator;
    • salt is interpreted as UTF-8 bytes and hashed, while raw_salt is interpreted as a hex string.

§Examples

use ethers_contract_derive::{EthAbiType, Eip712};
use ethers_core::types::{transaction::eip712::Eip712, H160};

#[derive(Clone, Default, EthAbiType, Eip712)]
#[eip712(
    name = "Radicle",
    version = "1",
    chain_id = 1,
    verifying_contract = "0x0000000000000000000000000000000000000000",
    // salt/raw_salt are optional parameters
    salt = "my-unique-spice",
)]
pub struct Puzzle {
    pub organization: H160,
    pub contributor: H160,
    pub commit: String,
    pub project: String,
}

let puzzle = Puzzle::default();
let hash = puzzle.encode_eip712().unwrap();

§Limitations

At the moment, the derive macro does not recursively encode nested Eip712 structs.

There is an Inner helper attribute #[eip712] for fields that will eventually be used to determine if there is a nested eip712 struct. However, this work is not yet complete.