multiversx_sc/types/interaction/
tx_from.rs

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
use crate::types::{heap::Address, ManagedAddress};

use super::{AnnotatedValue, TxEnv};

/// Marks the sender of any transaction.
pub trait TxFrom<Env>
where
    Env: TxEnv,
{
    fn resolve_address(&self, env: &Env) -> ManagedAddress<Env::Api>;
}

/// Marks the non-empty sender of a transaction.
///
/// Enforces the reciipent to be explicitly specified.
#[diagnostic::on_unimplemented(
    message = "Type `{Self}` cannot be used as a sender value (does not implement `TxFromSpecified<{Env}>`)",
    label = "sender needs to be explicit",
    note = "there are multiple ways to specify the sender value for a transaction, but `{Self}` is not one of them"
)]
pub trait TxFromSpecified<Env>:
    TxFrom<Env> + AnnotatedValue<Env, ManagedAddress<Env::Api>>
where
    Env: TxEnv,
{
}

impl<Env> TxFrom<Env> for ()
where
    Env: TxEnv,
{
    fn resolve_address(&self, env: &Env) -> ManagedAddress<Env::Api> {
        env.resolve_sender_address()
    }
}

impl<Env> TxFrom<Env> for ManagedAddress<Env::Api>
where
    Env: TxEnv,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        self.clone()
    }
}
impl<Env> TxFromSpecified<Env> for ManagedAddress<Env::Api> where Env: TxEnv {}

impl<Env> TxFrom<Env> for &ManagedAddress<Env::Api>
where
    Env: TxEnv,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        (*self).clone()
    }
}
impl<Env> TxFromSpecified<Env> for &ManagedAddress<Env::Api> where Env: TxEnv {}

impl<Env> TxFrom<Env> for Address
where
    Env: TxEnv,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        self.into()
    }
}

impl<Env> TxFromSpecified<Env> for Address where Env: TxEnv {}

impl<Env> TxFrom<Env> for &Address
where
    Env: TxEnv,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        ManagedAddress::from_address(self)
    }
}

impl<Env> TxFromSpecified<Env> for &Address where Env: TxEnv {}