cairo_lang_sierra/extensions/modules/starknet/
getter.rsuse std::marker::PhantomData;
use super::interoperability::ContractAddressType;
use super::syscalls::SyscallGenericLibfunc;
use super::{felt252_span_ty, span_ty};
use crate::extensions::boxing::box_ty;
use crate::extensions::felt252::Felt252Type;
use crate::extensions::int::unsigned::{Uint32Type, Uint64Type};
use crate::extensions::int::unsigned128::Uint128Type;
use crate::extensions::lib_func::SignatureSpecializationContext;
use crate::extensions::structure::StructType;
use crate::extensions::{NamedType, NoGenericArgsGenericType, SpecializationError};
use crate::ids::{ConcreteTypeId, UserTypeId};
use crate::program::GenericArg;
pub trait GetterTraits: Default {
const STR_ID: &'static str;
type InfoType: NoGenericArgsGenericType;
}
pub trait GetterTraitsEx: Default {
const STR_ID: &'static str;
fn info_type_id(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError>;
}
impl<TGetterTraits: GetterTraits> GetterTraitsEx for TGetterTraits {
const STR_ID: &'static str = TGetterTraits::STR_ID;
fn info_type_id(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
context.get_concrete_type(TGetterTraits::InfoType::id(), &[])
}
}
#[derive(Default)]
pub struct GetterLibfunc<TGetterTraitsEx: GetterTraitsEx> {
_phantom: PhantomData<TGetterTraitsEx>,
}
impl<TGetterTraitsEx: GetterTraitsEx> SyscallGenericLibfunc for GetterLibfunc<TGetterTraitsEx> {
const STR_ID: &'static str = TGetterTraitsEx::STR_ID;
fn input_tys(
_context: &dyn SignatureSpecializationContext,
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> {
Ok(vec![])
}
fn success_output_tys(
context: &dyn SignatureSpecializationContext,
) -> Result<Vec<crate::ids::ConcreteTypeId>, SpecializationError> {
Ok(vec![TGetterTraitsEx::info_type_id(context)?])
}
}
fn get_execution_info_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
context.get_concrete_type(
StructType::id(),
&[
GenericArg::UserType(UserTypeId::from_string("core::starknet::info::ExecutionInfo")),
GenericArg::Type(box_ty(context, get_block_info_type(context)?)?),
GenericArg::Type(box_ty(context, get_tx_info_type(context)?)?),
GenericArg::Type(contract_address_ty.clone()),
GenericArg::Type(contract_address_ty),
GenericArg::Type(felt252_ty),
],
)
}
fn get_execution_info_v2_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
context.get_concrete_type(
StructType::id(),
&[
GenericArg::UserType(UserTypeId::from_string(
"core::starknet::info::v2::ExecutionInfo",
)),
GenericArg::Type(box_ty(context, get_block_info_type(context)?)?),
GenericArg::Type(box_ty(context, get_tx_info_v2_type(context)?)?),
GenericArg::Type(contract_address_ty.clone()),
GenericArg::Type(contract_address_ty),
GenericArg::Type(felt252_ty),
],
)
}
fn get_block_info_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
let u64_ty = context.get_concrete_type(Uint64Type::id(), &[])?;
context.get_concrete_type(
StructType::id(),
&[
GenericArg::UserType(UserTypeId::from_string("core::starknet::info::BlockInfo")),
GenericArg::Type(u64_ty.clone()),
GenericArg::Type(u64_ty),
GenericArg::Type(contract_address_ty),
],
)
}
fn get_tx_info_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
let u128_ty = context.get_concrete_type(Uint128Type::id(), &[])?;
context.get_concrete_type(
StructType::id(),
&[
GenericArg::UserType(UserTypeId::from_string("core::starknet::info::TxInfo")),
GenericArg::Type(felt252_ty.clone()),
GenericArg::Type(contract_address_ty),
GenericArg::Type(u128_ty),
GenericArg::Type(felt252_span_ty(context)?),
GenericArg::Type(felt252_ty.clone()),
GenericArg::Type(felt252_ty.clone()),
GenericArg::Type(felt252_ty),
],
)
}
const RESOURCE_BOUNDS_USER_TYPE_ID: &str = "core::starknet::info::v2::ResourceBounds";
fn resource_bounds_span_ty(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
span_ty(context, get_resource_bounds_type(context)?, RESOURCE_BOUNDS_USER_TYPE_ID)
}
fn get_resource_bounds_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
let u64_ty = context.get_concrete_type(Uint64Type::id(), &[])?;
let u128_ty = context.get_concrete_type(Uint128Type::id(), &[])?;
context.get_concrete_type(
StructType::id(),
&[
GenericArg::UserType(UserTypeId::from_string(RESOURCE_BOUNDS_USER_TYPE_ID)),
GenericArg::Type(felt252_ty),
GenericArg::Type(u64_ty),
GenericArg::Type(u128_ty),
],
)
}
fn get_tx_info_v2_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
let felt252_ty = context.get_concrete_type(Felt252Type::id(), &[])?;
let felt252_span_ty = felt252_span_ty(context)?;
let contract_address_ty = context.get_concrete_type(ContractAddressType::id(), &[])?;
let u32_ty = context.get_concrete_type(Uint32Type::id(), &[])?;
let u128_ty = context.get_concrete_type(Uint128Type::id(), &[])?;
context.get_concrete_type(
StructType::id(),
&[
GenericArg::UserType(UserTypeId::from_string("core::starknet::info::v2::TxInfo")),
GenericArg::Type(felt252_ty.clone()),
GenericArg::Type(contract_address_ty),
GenericArg::Type(u128_ty.clone()),
GenericArg::Type(felt252_span_ty.clone()),
GenericArg::Type(felt252_ty.clone()),
GenericArg::Type(felt252_ty.clone()),
GenericArg::Type(felt252_ty),
GenericArg::Type(resource_bounds_span_ty(context)?),
GenericArg::Type(u128_ty),
GenericArg::Type(felt252_span_ty.clone()),
GenericArg::Type(u32_ty.clone()),
GenericArg::Type(u32_ty),
GenericArg::Type(felt252_span_ty),
],
)
}
#[derive(Default)]
pub struct GetExecutionInfoTrait {}
impl GetterTraitsEx for GetExecutionInfoTrait {
const STR_ID: &'static str = "get_execution_info_syscall";
fn info_type_id(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
box_ty(context, get_execution_info_type(context)?)
}
}
#[derive(Default)]
pub struct GetExecutionInfoV2Trait {}
impl GetterTraitsEx for GetExecutionInfoV2Trait {
const STR_ID: &'static str = "get_execution_info_v2_syscall";
fn info_type_id(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
box_ty(context, get_execution_info_v2_type(context)?)
}
}