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