sui_types/
funds_accumulator.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Rust bindings for `sui::funds_accumulator`
5
6use crate::SUI_FRAMEWORK_ADDRESS;
7use crate::base_types::SuiAddress;
8use move_core_types::account_address::AccountAddress;
9use move_core_types::annotated_value::{MoveFieldLayout, MoveStructLayout, MoveTypeLayout};
10use move_core_types::ident_str;
11use move_core_types::identifier::IdentStr;
12use move_core_types::language_storage::{StructTag, TypeTag};
13use move_core_types::u256::U256;
14use serde::Deserialize;
15use serde::Serialize;
16
17pub const FUNDS_ACCUMULATOR_MODULE_NAME: &IdentStr = ident_str!("funds_accumulator");
18pub const WITHDRAWAL_STRUCT_NAME: &IdentStr = ident_str!("Withdrawal");
19pub const RESOLVED_WITHDRAWAL_STRUCT: (&AccountAddress, &IdentStr, &IdentStr) = (
20    &SUI_FRAMEWORK_ADDRESS,
21    FUNDS_ACCUMULATOR_MODULE_NAME,
22    WITHDRAWAL_STRUCT_NAME,
23);
24pub const WITHDRAWAL_OWNER_FUNC_NAME: &IdentStr = ident_str!("withdrawal_owner");
25
26/// Rust bindings for the Move struct `sui::funds_accumulator::Withdrawal`.
27#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
28pub struct Withdrawal {
29    pub owner: SuiAddress,
30    /// Note that unlike the `CallArg::FundsWithdrawal` the `limit` here must
31    /// be fully specified, and cannot be `None` (i.e., unlimited).
32    /// As such, it is the responsibility of the PTB runtime to determine
33    /// the maximum limit in such a case, before creating the Move value.
34    pub limit: U256,
35}
36
37impl Withdrawal {
38    pub fn new(owner: SuiAddress, limit: U256) -> Self {
39        Self { owner, limit }
40    }
41
42    pub fn type_(type_arg: TypeTag) -> StructTag {
43        StructTag {
44            address: SUI_FRAMEWORK_ADDRESS,
45            module: FUNDS_ACCUMULATOR_MODULE_NAME.to_owned(),
46            name: WITHDRAWAL_STRUCT_NAME.to_owned(),
47            type_params: vec![type_arg],
48        }
49    }
50
51    pub fn type_tag(type_param: TypeTag) -> TypeTag {
52        TypeTag::Struct(Box::new(Self::type_(type_param)))
53    }
54
55    pub fn is_withdrawal(s: &StructTag) -> bool {
56        s.address == SUI_FRAMEWORK_ADDRESS
57            && s.module.as_ident_str() == FUNDS_ACCUMULATOR_MODULE_NAME
58            && s.name.as_ident_str() == WITHDRAWAL_STRUCT_NAME
59            && s.type_params.len() == 1
60    }
61
62    pub fn is_withdrawal_type(type_param: &TypeTag) -> bool {
63        if let TypeTag::Struct(struct_tag) = type_param {
64            Self::is_withdrawal(struct_tag)
65        } else {
66            false
67        }
68    }
69
70    pub fn owner(&self) -> SuiAddress {
71        self.owner
72    }
73
74    pub fn limit(&self) -> U256 {
75        self.limit
76    }
77
78    pub fn layout(type_param: TypeTag) -> MoveStructLayout {
79        MoveStructLayout {
80            type_: Self::type_(type_param),
81            fields: vec![
82                MoveFieldLayout::new(ident_str!("owner").to_owned(), MoveTypeLayout::Address),
83                MoveFieldLayout::new(ident_str!("limit").to_owned(), MoveTypeLayout::U256),
84            ],
85        }
86    }
87}