Skip to main content

forest/lotus_json/
message.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::*;
5
6use crate::shim::{
7    address::Address,
8    econ::TokenAmount,
9    message::{Message, Message_v4},
10};
11use fvm_ipld_encoding::RawBytes;
12use ::cid::Cid;
13
14#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
15#[serde(rename_all = "PascalCase")]
16#[schemars(rename = "Message")]
17pub struct MessageLotusJson {
18    #[serde(default)]
19    version: u64,
20    #[schemars(with = "LotusJson<Address>")]
21    #[serde(with = "crate::lotus_json")]
22    to: Address,
23    #[schemars(with = "LotusJson<Address>")]
24    #[serde(with = "crate::lotus_json")]
25    from: Address,
26    #[serde(default)]
27    nonce: u64,
28    #[schemars(with = "LotusJson<TokenAmount>")]
29    #[serde(with = "crate::lotus_json", default)]
30    value: TokenAmount,
31    #[serde(default)]
32    gas_limit: u64,
33    #[schemars(with = "LotusJson<TokenAmount>")]
34    #[serde(with = "crate::lotus_json", default)]
35    gas_fee_cap: TokenAmount,
36    #[schemars(with = "LotusJson<TokenAmount>")]
37    #[serde(with = "crate::lotus_json", default)]
38    gas_premium: TokenAmount,
39    #[serde(default)]
40    method: u64,
41    #[schemars(with = "LotusJson<RawBytes>")]
42    #[serde(with = "crate::lotus_json", default)]
43    params: RawBytes,
44    #[schemars(with = "LotusJson<Option<Cid>>")]
45    #[serde(
46        with = "crate::lotus_json",
47        rename = "CID",
48        default,
49        skip_serializing_if = "Option::is_none"
50    )]
51    cid: Option<Cid>,
52}
53
54impl HasLotusJson for Message {
55    type LotusJson = MessageLotusJson;
56
57    #[cfg(test)]
58    fn snapshots() -> Vec<(serde_json::Value, Self)> {
59        let msg = Message::default();
60        let cid = msg.cid();
61        vec![(
62            json!({
63                "From": "f00",
64                "GasFeeCap": "0",
65                "GasLimit": 0,
66                "GasPremium": "0",
67                "Method": 0,
68                "Nonce": 0,
69                "Params": null,
70                "To": "f00",
71                "Value": "0",
72                "Version": 0,
73                "CID": { "/": cid.to_string() },
74            }),
75            msg,
76        )]
77    }
78
79    fn into_lotus_json(self) -> Self::LotusJson {
80        let cid = Some(self.cid());
81        // The only lossless way to take a `Message` apart by value from outside its module.
82        let Message_v4 {
83            version,
84            from,
85            to,
86            sequence,
87            value,
88            method_num,
89            params,
90            gas_limit,
91            gas_fee_cap,
92            gas_premium,
93        } = self.into();
94        Self::LotusJson {
95            version,
96            to: to.into(),
97            from: from.into(),
98            nonce: sequence,
99            value: value.into(),
100            gas_limit,
101            gas_fee_cap: gas_fee_cap.into(),
102            gas_premium: gas_premium.into(),
103            method: method_num,
104            params,
105            cid,
106        }
107    }
108
109    fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
110        let Self::LotusJson {
111            version,
112            to,
113            from,
114            nonce,
115            value,
116            gas_limit,
117            gas_fee_cap,
118            gas_premium,
119            method,
120            params,
121            cid: _,
122        } = lotus_json;
123        Message::builder()
124            .version(version)
125            .from(from)
126            .to(to)
127            .sequence(nonce)
128            .value(value)
129            .method_num(method)
130            .params(params)
131            .gas_limit(gas_limit)
132            .gas_fee_cap(gas_fee_cap)
133            .gas_premium(gas_premium)
134            .build()
135    }
136}