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