use super::{BigUintApi, ErrorApi, StorageReadApi};
use crate::storage;
use crate::types::{Address, BoxedBytes, DctLocalRole, DctTokenData, TokenIdentifier, Vec, H256};
use alloc::boxed::Box;
pub trait BlockchainApi: StorageReadApi + ErrorApi + Clone + Sized + 'static {
type BalanceType: BigUintApi + 'static;
fn get_sc_address(&self) -> Address;
fn get_owner_address(&self) -> Address;
fn check_caller_is_owner(&self) {
if self.get_owner_address() != self.get_caller() {
self.signal_error(b"Endpoint can olny be called by owner");
}
}
fn get_shard_of_address(&self, address: &Address) -> u32;
fn is_smart_contract(&self, address: &Address) -> bool;
fn get_caller(&self) -> Address;
fn get_balance(&self, address: &Address) -> Self::BalanceType;
fn get_sc_balance(&self, token: &TokenIdentifier, nonce: u64) -> Self::BalanceType {
let sc_address = self.get_sc_address();
if token.is_moax() {
self.get_balance(&sc_address)
} else {
self.get_dct_balance(&sc_address, token, nonce)
}
}
fn get_tx_hash(&self) -> H256;
fn get_gas_left(&self) -> u64;
fn get_block_timestamp(&self) -> u64;
fn get_block_nonce(&self) -> u64;
fn get_block_round(&self) -> u64;
fn get_block_epoch(&self) -> u64;
fn get_block_random_seed(&self) -> Box<[u8; 48]>;
fn get_prev_block_timestamp(&self) -> u64;
fn get_prev_block_nonce(&self) -> u64;
fn get_prev_block_round(&self) -> u64;
fn get_prev_block_epoch(&self) -> u64;
fn get_prev_block_random_seed(&self) -> Box<[u8; 48]>;
fn get_current_dct_nft_nonce(&self, address: &Address, token_id: &TokenIdentifier) -> u64;
fn get_dct_balance(
&self,
address: &Address,
token_id: &TokenIdentifier,
nonce: u64,
) -> Self::BalanceType;
fn get_dct_token_data(
&self,
address: &Address,
token_id: &TokenIdentifier,
nonce: u64,
) -> DctTokenData<Self::BalanceType>;
#[inline]
fn get_cumulated_validator_rewards(&self) -> Self::BalanceType {
storage::storage_get(self.clone(), storage::protected_keys::DHARITRI_REWARD_KEY)
}
#[inline]
fn get_dct_local_roles(&self, token_id: &TokenIdentifier) -> Vec<DctLocalRole> {
let mut roles = Vec::new();
let key = [
storage::protected_keys::DHARITRI_DCT_LOCAL_ROLES_KEY,
token_id.as_dct_identifier(),
]
.concat();
let raw_storage = storage::storage_get::<Self, BoxedBytes>(self.clone(), &key);
let raw_storage_bytes = raw_storage.as_slice();
let mut current_index = 0;
while current_index < raw_storage_bytes.len() {
current_index += 1;
let role_len = raw_storage_bytes[current_index];
current_index += 1;
let end_index = current_index + role_len as usize;
let role_name = &raw_storage_bytes[current_index..end_index];
current_index = end_index;
let dct_local_role = DctLocalRole::from(role_name);
roles.push(dct_local_role);
}
roles
}
}