#abi #primitive #ethereum #byte #type #address #mapping

ethers_primitives

Primitive types for ethereum, including Rlp/ABI encode/decode support

1 unstable release

0.2.2 Feb 24, 2023

#130 in #abi

Download history 51/week @ 2024-07-21 138/week @ 2024-07-28 22/week @ 2024-08-04 34/week @ 2024-08-11 41/week @ 2024-08-18 45/week @ 2024-08-25 46/week @ 2024-09-01 68/week @ 2024-09-08 72/week @ 2024-09-15 72/week @ 2024-09-22 41/week @ 2024-09-29 17/week @ 2024-10-06 22/week @ 2024-10-13 21/week @ 2024-10-20 25/week @ 2024-10-27 74/week @ 2024-11-03

145 downloads per month
Used in 8 crates

MIT license

56KB
1.5K SLoC

Ethereum core types

The crate defines the rust mapping of enthereum core types.

abi rust Status
uint<M>/int<M> crate::IntM support
bytes<M> crate::BytesM support
bytes crate::Bytes support
address crate::Bytes support
bool bool support
<type>[M] [T;N]/&[T;N] support
<type>[] Vec<T>/&[T] support
fixed<M>x<N> working on
(T1,T2,...,Tn) (T1,T2,...,Tn)/struct A(T1,T2,...,Tn)/struct A { t1:T1,t2:T2,...,t3:Tn } support

With serde

This crate using serde framework to handle contract abi serialize/deserilize. Therefore, it is easy to define the structure that supports the Ethereum ABI.


use ethers_types_rs::*;

#[derive(Debug,Serialize,Deserilize)]
struct Transfer {
    to: Address,
    amount: U256,
}

EIP712 support

Again, thanks to the good framework design of serde. this crate define a series of serializers to support eip712 protocol.

let domain = json!({
    "name": "Ether Mail",
    "version": "1",
    "chainId": 1,
    "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
});

let domain: EIP712Domain = serde_json::from_value(domain).unwrap();
assert_eq!(
    eip712_hash_struct(&domain).unwrap().to_eth_hex(),
    "0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f"
);

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Person {
    pub name: String,
    pub wallet: Address,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Mail {
    pub from: Person,
    pub to: Person,
    pub contents: String,
}

let json = json!({
  "from": {
    "name": "Cow",
    "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
  },
  "to": {
    "name": "Bob",
    "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
  },
  "contents": "Hello, Bob!"
});

let mail: Mail = serde_json::from_value(json).expect("parse domain");

assert_eq!(
    "Mail(Person from,Person to,string contents)Person(string name,address wallet)",
    eip712_encode_type(&mail).expect("generate e712 types")
);

assert_eq!(
    eip712_hash_struct(&mail.from).unwrap().to_eth_hex(),
    "0xfc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8"
);

assert_eq!(
    eip712_hash_struct(&mail.to).unwrap().to_eth_hex(),
    "0xcd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1"
);

assert_eq!(
    eip712_hash_struct(&mail).unwrap().to_eth_hex(),
    "0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e"
);

assert_eq!(
    eip712_encode(&domain, &mail).unwrap().to_eth_hex(),
    "0x1901f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090fc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e"
);

let expect_request: TypedData<Mail> =
    serde_json::from_str(include_str!("./eip712.json")).unwrap();

 assert_eq!(eip712_into_request(domain, mail).unwrap(), expect_request);

Dependencies

~4.5–7MB
~118K SLoC