multiversx_sc/types/interaction/markers/
validator_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 validator system smart contract.
12const VALIDATOR_SC_ADDRESS_BYTES: [u8; 32] =
13    hex!("000000000000000000010000000000000000000000000000000000000001ffff");
14const VALIDATOR_SC_ADDRESS_BECH32: &str =
15    "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqplllst77y4l";
16const VALIDATOR_SC_ADDRESS_ANNOTATION: &str =
17    "bech32:erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqplllst77y4l";
18
19/// Indicates the system SC address, which is the same on any MultiversX blockchain.
20pub struct ValidatorSystemSCAddress;
21
22impl ValidatorSystemSCAddress {
23    pub fn to_managed_address<Api>(self) -> ManagedAddress<Api>
24    where
25        Api: ManagedTypeApi,
26    {
27        ManagedAddress::from(VALIDATOR_SC_ADDRESS_BYTES)
28    }
29
30    pub fn to_address(&self) -> Address {
31        VALIDATOR_SC_ADDRESS_BYTES.into()
32    }
33
34    pub fn to_bech32_str(&self) -> &str {
35        VALIDATOR_SC_ADDRESS_BECH32
36    }
37
38    pub fn to_bech32_string(&self) -> alloc::string::String {
39        VALIDATOR_SC_ADDRESS_BECH32.into()
40    }
41}
42
43impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for ValidatorSystemSCAddress
44where
45    Env: TxEnv,
46{
47    fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
48        ManagedBuffer::from(VALIDATOR_SC_ADDRESS_ANNOTATION)
49    }
50
51    fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
52        ValidatorSystemSCAddress.to_managed_address()
53    }
54}
55
56impl<Env> TxTo<Env> for ValidatorSystemSCAddress where Env: TxEnv {}
57impl<Env> TxToSpecified<Env> for ValidatorSystemSCAddress where Env: TxEnv {}
58
59impl TopEncode for ValidatorSystemSCAddress {
60    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
61    where
62        O: TopEncodeOutput,
63        H: EncodeErrorHandler,
64    {
65        VALIDATOR_SC_ADDRESS_BYTES.top_encode_or_handle_err(output, h)
66    }
67}
68
69impl<M> TypeAbiFrom<ValidatorSystemSCAddress> for ManagedAddress<M> where M: ManagedTypeApi {}
70
71impl core::fmt::Display for ValidatorSystemSCAddress {
72    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73        f.write_str(VALIDATOR_SC_ADDRESS_BECH32)
74    }
75}