3 个版本
0.1.2 | 2024 年 1 月 24 日 |
---|---|
0.1.1 | 2024 年 1 月 24 日 |
0.1.0 | 2024 年 1 月 24 日 |
#14 在 #override
344 每月下载
在 aori-rs 中使用
15KB
216 代码行
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