forest/lotus_json/
actor_state.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::*;
5use crate::shim::{address::Address, econ::TokenAmount, state_tree::ActorState};
6use ::cid::Cid;
7
8#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
9#[serde(rename_all = "PascalCase")]
10#[schemars(rename = "ActorState")]
11pub struct ActorStateLotusJson {
12    #[schemars(with = "LotusJson<Cid>")]
13    #[serde(with = "crate::lotus_json")]
14    code: Cid,
15    #[schemars(with = "LotusJson<Cid>")]
16    #[serde(with = "crate::lotus_json")]
17    head: Cid,
18    nonce: u64,
19    #[schemars(with = "LotusJson<TokenAmount>")]
20    #[serde(with = "crate::lotus_json")]
21    balance: TokenAmount,
22    #[schemars(with = "LotusJson<Option<Address>>")]
23    #[serde(
24        with = "crate::lotus_json",
25        skip_serializing_if = "Option::is_none",
26        default
27    )]
28    delegated_address: Option<Address>,
29}
30
31impl HasLotusJson for ActorState {
32    type LotusJson = ActorStateLotusJson;
33
34    #[cfg(test)]
35    fn snapshots() -> Vec<(serde_json::Value, Self)> {
36        vec![(
37            json!({
38                "Balance": "0",
39                "Code": {
40                    "/": "baeaaaaa"
41                },
42                "Head": {
43                    "/": "baeaaaaa"
44                },
45                "Nonce": 0,
46            }),
47            Self::new(
48                Cid::default(),
49                Cid::default(),
50                TokenAmount::default(),
51                0,
52                None,
53            ),
54        )]
55    }
56
57    fn into_lotus_json(self) -> Self::LotusJson {
58        let fvm3::state_tree::ActorState {
59            code,
60            state,
61            sequence,
62            balance,
63            delegated_address,
64        } = From::from(self);
65        Self::LotusJson {
66            code,
67            head: state,
68            nonce: sequence,
69            balance: crate::shim::econ::TokenAmount::from(balance),
70            delegated_address: delegated_address.map(crate::shim::address::Address::from),
71        }
72    }
73
74    fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
75        let ActorStateLotusJson {
76            code,
77            head,
78            nonce,
79            balance,
80            delegated_address,
81        } = lotus_json;
82        Self::new(code, head, balance, nonce, delegated_address)
83    }
84}