forest/lotus_json/
message.rs

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