diem_types/account_config/resources/
balance.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::{
9    access_path::AccessPath,
10    account_config::constants::{
11        xus_tag, ACCOUNT_MODULE_NAME, CORE_CODE_ADDRESS,
12    },
13};
14use move_core_types::{
15    language_storage::{StructTag, TypeTag},
16    move_resource::MoveResource,
17};
18#[cfg(any(test, feature = "fuzzing"))]
19use proptest_derive::Arbitrary;
20use serde::{Deserialize, Serialize};
21
22/// The balance resource held under an account.
23#[derive(Debug, Serialize, Deserialize)]
24#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
25pub struct BalanceResource {
26    coin: u64,
27}
28
29impl BalanceResource {
30    pub fn new(coin: u64) -> Self { Self { coin } }
31
32    pub fn coin(&self) -> u64 { self.coin }
33
34    // TODO/XXX: remove this once the MoveResource trait allows type arguments
35    // to `struct_tag`.
36    pub fn struct_tag_for_currency(currency_typetag: TypeTag) -> StructTag {
37        StructTag {
38            address: CORE_CODE_ADDRESS,
39            name: BalanceResource::struct_identifier(),
40            module: BalanceResource::module_identifier(),
41            type_params: vec![currency_typetag],
42        }
43    }
44
45    // TODO: remove this once the MoveResource trait allows type arguments to
46    // `resource_path`.
47    pub fn access_path_for(currency_typetag: TypeTag) -> Vec<u8> {
48        AccessPath::resource_access_vec(
49            BalanceResource::struct_tag_for_currency(currency_typetag),
50        )
51    }
52}
53
54impl MoveResource for BalanceResource {
55    const MODULE_NAME: &'static str = ACCOUNT_MODULE_NAME;
56    const STRUCT_NAME: &'static str = "Balance";
57
58    fn type_params() -> Vec<TypeTag> { vec![xus_tag()] }
59}