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);
24
25/// Rust bindings for the Move struct `sui::funds_accumulator::Withdrawal`.
26#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
27pub struct Withdrawal {
28    pub owner: SuiAddress,
29    /// Note that unlike the `CallArg::FundsWithdrawal` the `limit` here must
30    /// be fully specified, and cannot be `None` (i.e., unlimited).
31    /// As such, it is the responsibility of the PTB runtime to determine
32    /// the maximum limit in such a case, before creating the Move value.
33    pub limit: U256,
34}
35
36impl Withdrawal {
37    pub fn new(owner: SuiAddress, limit: U256) -> Self {
38        Self { owner, limit }
39    }
40
41    pub fn type_(type_arg: TypeTag) -> StructTag {
42        StructTag {
43            address: SUI_FRAMEWORK_ADDRESS,
44            module: FUNDS_ACCUMULATOR_MODULE_NAME.to_owned(),
45            name: WITHDRAWAL_STRUCT_NAME.to_owned(),
46            type_params: vec![type_arg],
47        }
48    }
49
50    pub fn type_tag(type_param: TypeTag) -> TypeTag {
51        TypeTag::Struct(Box::new(Self::type_(type_param)))
52    }
53
54    pub fn is_withdrawal(s: &StructTag) -> bool {
55        s.address == SUI_FRAMEWORK_ADDRESS
56            && s.module.as_ident_str() == FUNDS_ACCUMULATOR_MODULE_NAME
57            && s.name.as_ident_str() == WITHDRAWAL_STRUCT_NAME
58            && s.type_params.len() == 1
59    }
60
61    pub fn is_withdrawal_type(type_param: &TypeTag) -> bool {
62        if let TypeTag::Struct(struct_tag) = type_param {
63            Self::is_withdrawal(struct_tag)
64        } else {
65            false
66        }
67    }
68
69    pub fn owner(&self) -> SuiAddress {
70        self.owner
71    }
72
73    pub fn limit(&self) -> U256 {
74        self.limit
75    }
76
77    pub fn layout(type_param: TypeTag) -> MoveStructLayout {
78        MoveStructLayout {
79            type_: Self::type_(type_param),
80            fields: vec![
81                MoveFieldLayout::new(ident_str!("owner").to_owned(), MoveTypeLayout::Address),
82                MoveFieldLayout::new(ident_str!("limit").to_owned(), MoveTypeLayout::U256),
83            ],
84        }
85    }
86}