forest/lotus_json/
token_amount.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::*;
5use crate::shim::econ::TokenAmount;
6use num::BigInt;
7
8#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
9#[serde(transparent)] // name the field for clarity
10#[schemars(rename = "TokenAmount")]
11pub struct TokenAmountLotusJson {
12    #[schemars(with = "LotusJson<BigInt>")]
13    #[serde(with = "crate::lotus_json")]
14    attos: BigInt,
15}
16
17impl HasLotusJson for TokenAmount {
18    type LotusJson = TokenAmountLotusJson;
19
20    #[cfg(test)]
21    fn snapshots() -> Vec<(serde_json::Value, Self)> {
22        vec![(json!("1"), TokenAmount::from_atto(1))]
23    }
24
25    fn into_lotus_json(self) -> Self::LotusJson {
26        Self::LotusJson {
27            attos: self.atto().clone(),
28        }
29    }
30
31    fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
32        let Self::LotusJson { attos } = lotus_json;
33        Self::from_atto(attos)
34    }
35}