Skip to main content

iota_types/
transfer.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_sdk_move_types::iota_framework::object::ID;
6use iota_sdk_types::{ObjectId, Version};
7use move_binary_format::{CompiledModule, file_format::SignatureToken};
8use move_bytecode_utils::resolve_struct;
9use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
10use serde::{Deserialize, Serialize};
11
12use crate::IOTA_FRAMEWORK_ADDRESS;
13
14pub const RESOLVED_RECEIVING_STRUCT: (&AccountAddress, &IdentStr, &IdentStr) = (
15    &IOTA_FRAMEWORK_ADDRESS,
16    ident_str!("transfer"),
17    ident_str!("Receiving"),
18);
19
20/// Rust version of the Move iota::transfer::Receiving type
21#[derive(Clone, Serialize, Deserialize, Debug)]
22pub struct Receiving {
23    pub id: ID,
24    pub version: Version,
25}
26
27impl Receiving {
28    pub fn new(id: ObjectId, version: Version) -> Self {
29        Self {
30            id: ID::new(id),
31            version,
32        }
33    }
34
35    pub fn to_bcs_bytes(&self) -> Vec<u8> {
36        bcs::to_bytes(self).expect("Value representation is owned and should always serialize")
37    }
38
39    pub fn is_receiving(view: &CompiledModule, s: &SignatureToken) -> bool {
40        use SignatureToken as S;
41        match s {
42            S::MutableReference(inner) | S::Reference(inner) => Self::is_receiving(view, inner),
43            S::DatatypeInstantiation(inst) => {
44                let (idx, type_args) = &**inst;
45                let struct_tag = resolve_struct(view, *idx);
46                struct_tag == RESOLVED_RECEIVING_STRUCT && type_args.len() == 1
47            }
48            _ => false,
49        }
50    }
51}