#alloy #serialization #type #deserialize #default #override #string

alloy_serde_macro

在与原生 sol 类型一起工作的同时,确保它们被反序列化为或序列化出非十六进制类型

3 个版本

0.1.2 2024 年 1 月 24 日
0.1.1 2024 年 1 月 24 日
0.1.0 2024 年 1 月 24 日

#14#override

Download history 196/week @ 2024-03-13 102/week @ 2024-03-20 74/week @ 2024-03-27 53/week @ 2024-04-03 51/week @ 2024-04-10 51/week @ 2024-04-17 53/week @ 2024-04-24 63/week @ 2024-05-01 81/week @ 2024-05-08 208/week @ 2024-05-15 179/week @ 2024-05-22 229/week @ 2024-05-29 100/week @ 2024-06-05 31/week @ 2024-06-12 63/week @ 2024-06-19 61/week @ 2024-06-26

344 每月下载
aori-rs 中使用

MIT 许可证

15KB
216 代码行

CI Crates.io

Alloy-Serde (反)序列化宏

这个包是关于什么的?

Alloy 类型很棒,但有时实现需要将类型作为整数或字符串发送,因为 alloy 默认序列化为十六进制,所以在这种情况下覆盖默认序列化是一个好主意。

这就是这个包存在的原因。

它允许在与原生 sol 类型一起工作的同时,确保它们被反序列化为或序列化出非十六进制类型。

使用示例

在这种情况下,后端 json rpc 服务器接受一些值作为整数,但默认情况下,它们将被序列化为十六进制。此包包含一个宏,该宏生成序列化器或反序列化器,格式为 "$target_type_as_$source_type" 用于序列化,以及 "$target_type_from_$source_type" 用于反序列化。

以下示例来自出色的 aori sdk。

use alloy_sol_types::sol;
use serde::{Deserialize, Serialize};
use alloy_serde_macro::*;

sol!(
    #[derive(Default, Debug, Deserialize, Serialize)]
    struct AoriOrder {
        address offerer;
        // input
        address inputToken;
        #[serde(serialize_with = "U256_as_String", deserialize_with = "U256_from_String")]
        uint256 inputAmount;
        #[serde(serialize_with = "U256_as_u32", deserialize_with = "U256_from_u32")]
        uint256 inputChainId;
        address inputZone;
        // output
        address outputToken;
        #[serde(serialize_with = "U256_as_String", deserialize_with = "U256_from_String")]
        uint256 outputAmount;
        #[serde(serialize_with = "U256_as_u32", deserialize_with = "U256_from_u32")]
        uint256 outputChainId;
        address outputZone;
        // other
        #[serde(serialize_with = "U256_as_String", deserialize_with = "U256_from_String")]
        uint256 startTime;
        #[serde(serialize_with = "U256_as_String", deserialize_with = "U256_from_String")]
        uint256 endTime;
        uint256 salt;
        #[serde(serialize_with = "U256_as_u32", deserialize_with = "U256_from_u32")]
        uint256 counter;
        bool toWithdraw;
    }
);

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json;
    use alloy_sol_types::SolValue;
    use alloy_primitives::U256;
    use alloy_primitives::Address;
    use alloy_primitives::keccak256;
    #[test]
    fn aori_order() {
        let order = AoriOrder {
            offerer: "0x0000000000000000000000000000000000000001"
                .parse::<Address>()
                .unwrap(),
            inputToken: "0x0000000000000000000000000000000000000002"
                .parse::<Address>()
                .unwrap(),
            inputAmount: U256::from(1000000000000000000_u64),
            inputChainId: U256::from(1),
            inputZone: "0x0000000000000000000000000000000000000003"
                .parse::<Address>()
                .unwrap(),
            outputToken: "0x0000000000000000000000000000000000000004"
                .parse::<Address>()
                .unwrap(),
            outputAmount: U256::from(2000000000000000000_u64),
            outputChainId: U256::from(1),
            outputZone: "0x0000000000000000000000000000000000000000"
                .parse::<Address>()
                .unwrap(),
            startTime: U256::from(1619827200),
            endTime: U256::from(1622428800),
            salt: U256::from(1),
            counter: U256::from(1),
            toWithdraw: false,
        };
        let packed = order.abi_encode_packed();
        let hash = keccak256(packed);
        println!("rust order hash {}", hash);
    }

    #[test]
    fn serialize_aori_order() {
        let order = AoriOrder {
            offerer: "0x0000000000000000000000000000000000000001".parse::<Address>().unwrap(),
            inputToken: "0x0000000000000000000000000000000000000002".parse::<Address>().unwrap(),
            inputAmount: U256::from(1000000000000000000_u64),
            inputChainId: U256::from(1),
            inputZone: "0x0000000000000000000000000000000000000003".parse::<Address>().unwrap(),
            outputToken: "0x0000000000000000000000000000000000000004".parse::<Address>().unwrap(),
            outputAmount: U256::from(2000000000000000000_u64),
            outputChainId: U256::from(1),
            outputZone: "0x0000000000000000000000000000000000000000".parse::<Address>().unwrap(),
            startTime: U256::from(1619827200),
            endTime: U256::from(1622428800),
            salt: U256::from(1),
            counter: U256::from(1),
            toWithdraw: false,
        };

        let serialized = serde_json::to_string(&order).unwrap();
        println!("Serialized AoriOrder: {}", serialized);

        // Deserialize the order
        let deserialized: AoriOrder = serde_json::from_str(&serialized).unwrap();
        println!("Deserialized AoriOrder: {:?}", deserialized);
    }
}

依赖关系

~14MB
~323K SLoC