multiversx_sc/types/interaction/markers/
system_sc_address.rs

1use hex_literal::hex;
2use multiversx_chain_core::types::Address;
3use multiversx_sc_codec::{EncodeErrorHandler, TopEncode, TopEncodeOutput};
4
5use crate::{
6    abi::TypeAbiFrom,
7    api::ManagedTypeApi,
8    types::{AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxTo, TxToSpecified},
9};
10
11/// Address of the System Smart Contract.
12const SYSTEM_SC_ADDRESS_BYTES: [u8; 32] =
13    hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
14
15/// Bech32-encoded address of the System Smart Contract.
16const SYSTEM_SC_ADDRESS_BECH32: &str =
17    "erd1lllllllllllllllllllllllllllllllllllllllllllllllllllsckry7t";
18
19/// Annotation for Bech32 format.
20const SYSTEM_SC_ADDRESS_ANNOTATION: &str =
21    "bech32:erd1lllllllllllllllllllllllllllllllllllllllllllllllllllsckry7t";
22
23/// Indicates the System SC address, which is the same on any MultiversX blockchain.
24pub struct SystemSCAddress;
25
26impl SystemSCAddress {
27    pub fn to_managed_address<Api>(self) -> ManagedAddress<Api>
28    where
29        Api: ManagedTypeApi,
30    {
31        ManagedAddress::from(SYSTEM_SC_ADDRESS_BYTES)
32    }
33
34    pub fn to_address(&self) -> Address {
35        SYSTEM_SC_ADDRESS_BYTES.into()
36    }
37
38    pub fn to_bech32_str(&self) -> &str {
39        SYSTEM_SC_ADDRESS_BECH32
40    }
41
42    pub fn to_bech32_string(&self) -> alloc::string::String {
43        SYSTEM_SC_ADDRESS_BECH32.into()
44    }
45}
46
47impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for SystemSCAddress
48where
49    Env: TxEnv,
50{
51    fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
52        ManagedBuffer::from(SYSTEM_SC_ADDRESS_ANNOTATION)
53    }
54
55    fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
56        SystemSCAddress.to_managed_address()
57    }
58}
59
60impl<Env> TxTo<Env> for SystemSCAddress where Env: TxEnv {}
61impl<Env> TxToSpecified<Env> for SystemSCAddress where Env: TxEnv {}
62
63impl TopEncode for SystemSCAddress {
64    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
65    where
66        O: TopEncodeOutput,
67        H: EncodeErrorHandler,
68    {
69        SYSTEM_SC_ADDRESS_BYTES.top_encode_or_handle_err(output, h)
70    }
71}
72
73impl<M> TypeAbiFrom<SystemSCAddress> for ManagedAddress<M> where M: ManagedTypeApi {}
74
75impl core::fmt::Display for SystemSCAddress {
76    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77        f.write_str(SYSTEM_SC_ADDRESS_BECH32)
78    }
79}