sui_types/
funds_accumulator.rs1use crate::SUI_FRAMEWORK_ADDRESS;
7use crate::base_types::SuiAddress;
8use move_core_types::annotated_value::{MoveFieldLayout, MoveStructLayout, MoveTypeLayout};
9use move_core_types::ident_str;
10use move_core_types::identifier::IdentStr;
11use move_core_types::language_storage::{StructTag, TypeTag};
12use move_core_types::u256::U256;
13use serde::Deserialize;
14use serde::Serialize;
15
16pub const FUNDS_ACCUMULATOR_MODULE_NAME: &IdentStr = ident_str!("funds_accumulator");
17pub const WITHDRAWAL_STRUCT_NAME: &IdentStr = ident_str!("Withdrawal");
18
19#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
21pub struct Withdrawal {
22 pub owner: SuiAddress,
23 pub limit: U256,
28}
29
30impl Withdrawal {
31 pub fn new(owner: SuiAddress, limit: U256) -> Self {
32 Self { owner, limit }
33 }
34
35 pub fn type_(type_arg: TypeTag) -> StructTag {
36 StructTag {
37 address: SUI_FRAMEWORK_ADDRESS,
38 module: FUNDS_ACCUMULATOR_MODULE_NAME.to_owned(),
39 name: WITHDRAWAL_STRUCT_NAME.to_owned(),
40 type_params: vec![type_arg],
41 }
42 }
43
44 pub fn type_tag(type_param: TypeTag) -> TypeTag {
45 TypeTag::Struct(Box::new(Self::type_(type_param)))
46 }
47
48 pub fn is_withdrawal(s: &StructTag) -> bool {
49 s.address == SUI_FRAMEWORK_ADDRESS
50 && s.module.as_ident_str() == FUNDS_ACCUMULATOR_MODULE_NAME
51 && s.name.as_ident_str() == WITHDRAWAL_STRUCT_NAME
52 && s.type_params.len() == 1
53 }
54
55 pub fn is_withdrawal_type(type_param: &TypeTag) -> bool {
56 if let TypeTag::Struct(struct_tag) = type_param {
57 Self::is_withdrawal(struct_tag)
58 } else {
59 false
60 }
61 }
62
63 pub fn owner(&self) -> SuiAddress {
64 self.owner
65 }
66
67 pub fn limit(&self) -> U256 {
68 self.limit
69 }
70
71 pub fn layout(type_param: TypeTag) -> MoveStructLayout {
72 MoveStructLayout {
73 type_: Self::type_(type_param),
74 fields: vec![
75 MoveFieldLayout::new(ident_str!("owner").to_owned(), MoveTypeLayout::Address),
76 MoveFieldLayout::new(ident_str!("limit").to_owned(), MoveTypeLayout::U256),
77 ],
78 }
79 }
80}