Skip to main content

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