1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Coin kinds introduced with the stardust models.
//!
//! We define as coin kinds objects that hold some kind of `Balance`
//! and comply with the `Coin` type layout:
//!
//! ```rust
//! struct T {
//!     id: iota_types::id::UID,
//!     balance: iota_types::balance::Balance,
//!     // ...
//! }
//! ```

use std::mem::size_of;

use crate::{
    balance::Balance,
    gas_coin::GAS,
    object::{ID_END_INDEX, Object},
    stardust::output::{AliasOutput, BasicOutput, NftOutput},
    timelock::timelock::TimeLock,
};

/// Infer whether the object is a kind of gas coin.
pub fn is_gas_coin_kind(object: &Object) -> bool {
    let Some(struct_tag) = object.struct_tag() else {
        return false;
    };
    struct_tag == AliasOutput::tag(GAS::type_tag())
        || struct_tag == BasicOutput::tag(GAS::type_tag())
        || struct_tag == NftOutput::tag(GAS::type_tag())
        || struct_tag == TimeLock::<Balance>::type_(Balance::type_(GAS::type_tag()).into())
        || object.is_gas_coin()
}

/// Return the `Balance` of a gas-coin kind.
///
/// Useful to avoid deserialization of the entire object.
pub fn get_gas_balance_maybe(object: &Object) -> Option<Balance> {
    if !is_gas_coin_kind(object) {
        return None;
    }
    let inner = object.data.try_as_move()?;
    bcs::from_bytes(&inner.contents()[ID_END_INDEX..][..size_of::<Balance>()]).ok()
}

#[cfg(test)]
#[cfg(feature = "test-utils")]
mod tests {

    use iota_protocol_config::ProtocolConfig;

    use crate::{
        balance::Balance,
        base_types::{IotaAddress, ObjectID, TxContext},
        id::UID,
        object::{Object, Owner},
        stardust::{
            coin_kind::{get_gas_balance_maybe, is_gas_coin_kind},
            coin_type::CoinType,
            output::{AliasOutput, BasicOutput, NftOutput},
        },
        timelock::timelock::{TimeLock, to_genesis_object},
    };

    fn nft_output(balance: u64, coin_type: CoinType) -> anyhow::Result<Object> {
        let id = UID::new(ObjectID::random());
        let balance = Balance::new(balance);
        let output = NftOutput {
            id,
            balance,
            native_tokens: Default::default(),
            storage_deposit_return: Default::default(),
            timelock: Default::default(),
            expiration: Default::default(),
        };
        output.to_genesis_object(
            IotaAddress::ZERO,
            &ProtocolConfig::get_for_min_version(),
            &TxContext::random_for_testing_only(),
            1.into(),
            coin_type,
        )
    }

    #[test]
    fn is_coin_kind_nft_output() {
        let object = nft_output(100, CoinType::Iota).unwrap();
        assert!(is_gas_coin_kind(&object));
    }

    #[test]
    fn get_gas_balance_nft_output() {
        let value = 100;
        let object = nft_output(value, CoinType::Iota).unwrap();
        let gas_coin_balance = get_gas_balance_maybe(&object).unwrap();
        assert_eq!(gas_coin_balance.value(), value);
    }

    fn alias_output(balance: u64, coin_type: CoinType) -> anyhow::Result<Object> {
        let id = UID::new(ObjectID::random());
        let balance = Balance::new(balance);
        let output = AliasOutput {
            id,
            balance,
            native_tokens: Default::default(),
        };
        output.to_genesis_object(
            Owner::AddressOwner(IotaAddress::ZERO),
            &ProtocolConfig::get_for_min_version(),
            &TxContext::random_for_testing_only(),
            1.into(),
            coin_type,
        )
    }

    #[test]
    fn is_coin_kind_alias_output() {
        let object = alias_output(100, CoinType::Iota).unwrap();
        assert!(is_gas_coin_kind(&object));
    }

    #[test]
    fn get_gas_balance_alias_output() {
        let value = 100;
        let object = alias_output(value, CoinType::Iota).unwrap();
        let gas_coin_balance = get_gas_balance_maybe(&object).unwrap();
        assert_eq!(gas_coin_balance.value(), value);
    }

    fn basic_output(balance: u64, coin_type: CoinType) -> anyhow::Result<Object> {
        let id = UID::new(ObjectID::random());
        let balance = Balance::new(balance);
        let output = BasicOutput {
            id,
            balance,
            native_tokens: Default::default(),
            storage_deposit_return: Default::default(),
            timelock: Default::default(),
            expiration: Default::default(),
            metadata: Default::default(),
            tag: Default::default(),
            sender: Default::default(),
        };
        output.to_genesis_object(
            IotaAddress::ZERO,
            &ProtocolConfig::get_for_min_version(),
            &TxContext::random_for_testing_only(),
            1.into(),
            &coin_type,
        )
    }

    #[test]
    fn is_coin_kind_basic_output() {
        let object = basic_output(100, CoinType::Iota).unwrap();
        assert!(is_gas_coin_kind(&object));
    }

    #[test]
    fn get_gas_balance_basic_output() {
        let value = 100;
        let object = basic_output(value, CoinType::Iota).unwrap();
        let gas_coin_balance = get_gas_balance_maybe(&object).unwrap();
        assert_eq!(gas_coin_balance.value(), value);
    }

    fn timelock(balance: u64) -> anyhow::Result<Object> {
        let id = UID::new(ObjectID::random());
        let balance = Balance::new(balance);
        let expiration_timestamp_ms = 10;
        let label = None;

        let timelock = TimeLock::new(id, balance, expiration_timestamp_ms, label);
        Ok(to_genesis_object(
            timelock,
            IotaAddress::ZERO,
            &ProtocolConfig::get_for_min_version(),
            &TxContext::random_for_testing_only(),
            1.into(),
        )?)
    }

    #[test]
    fn is_coin_kind_timelock() {
        let object = timelock(100).unwrap();
        assert!(is_gas_coin_kind(&object));
    }

    #[test]
    fn get_gas_balance_timelock() {
        let value = 100;
        let object = timelock(value).unwrap();
        let gas_coin_balance = get_gas_balance_maybe(&object).unwrap();
        assert_eq!(gas_coin_balance.value(), value);
    }
}