#![cfg_attr(docsrs, feature(doc_auto_cfg))]
extern crate self as rialo_sol_lang;
use std::{collections::BTreeSet, fmt::Debug, io::Write};
use bytemuck::{Pod, Zeroable};
use crate::rialo_s_program::{
account_info::AccountInfo, instruction::AccountMeta, program_error::ProgramError,
pubkey::Pubkey,
};
mod account_meta;
pub mod accounts;
mod bpf_upgradeable_state;
mod bpf_writer;
mod common;
pub mod context;
pub mod error;
#[doc(hidden)]
pub mod event;
#[doc(hidden)]
pub mod idl;
pub mod system_program;
mod vec;
#[cfg(feature = "lazy-account")]
mod lazy;
#[cfg(feature = "idl-build")]
#[doc(hidden)]
pub use linkme;
#[cfg(feature = "idl-build")]
#[doc(hidden)]
#[allow(unsafe_code)]
#[linkme::distributed_slice]
pub static __SOL_IDL_PRINT_FNS: [(fn(), &'static str)];
pub use borsh::de::BorshDeserialize as SolDeserialize;
pub use borsh::ser::BorshSerialize as SolSerialize;
pub use rialo_sol_attribute_access_control::access_control;
pub use rialo_sol_attribute_account::{account, declare_id, pubkey, zero_copy};
pub use rialo_sol_attribute_constant::constant;
pub use rialo_sol_attribute_error::*;
pub use rialo_sol_attribute_event::{emit, event};
pub use rialo_sol_attribute_program::{declare_program, instruction, program};
pub use rialo_sol_derive_accounts::Accounts;
pub use rialo_sol_derive_serde::{__erase, SolDeserialize, SolSerialize};
pub use rialo_sol_derive_space::InitSpace;
pub use crate::bpf_upgradeable_state::*;
pub mod rialo_s_program {
pub use rialo_s_account_info as account_info;
pub use rialo_s_clock as clock;
pub use rialo_s_feature_gate_interface as feature;
pub use rialo_s_msg::msg;
pub use rialo_s_program_entrypoint as entrypoint;
pub use rialo_s_program_entrypoint::entrypoint;
pub use rialo_s_program_error as program_error;
pub use rialo_s_program_memory as program_memory;
pub use rialo_s_program_option as program_option;
pub use rialo_s_program_pack as program_pack;
pub use rialo_s_pubkey as pubkey;
pub use rialo_s_sdk_ids::system_program;
pub use rialo_s_system_interface::instruction as system_instruction;
pub mod instruction {
pub use rialo_s_instruction::*;
#[allow(unsafe_code)]
pub fn get_stack_height() -> usize {
#[cfg(target_os = "solana")]
unsafe {
rialo_s_instruction::syscalls::rlo_get_stack_height() as usize
}
#[cfg(not(target_os = "solana"))]
{
rialo_s_sysvar::program_stubs::rlo_get_stack_height() as usize
}
}
}
pub mod rent {
pub use rialo_s_sysvar::rent::*;
}
pub mod program {
pub use rialo_s_cpi::*;
pub use rialo_s_invoke::{
invoke, invoke_signed, invoke_signed_unchecked, invoke_unchecked,
};
}
pub mod bpf_loader_upgradeable {
#[allow(deprecated)]
pub use rialo_s_loader_v3_interface::{
get_program_data_address,
instruction::{
close, close_any, create_buffer, deploy_with_max_program_len, extend_program,
is_close_instruction, is_set_authority_checked_instruction,
is_set_authority_instruction, is_upgrade_instruction, set_buffer_authority,
set_buffer_authority_checked, set_upgrade_authority, set_upgrade_authority_checked,
upgrade, write,
},
state::UpgradeableLoaderState,
};
pub use rialo_s_sdk_ids::bpf_loader_upgradeable::{check_id, id, ID};
}
pub mod log {
pub use rialo_s_msg::{msg, rlo_log};
#[allow(unsafe_code, clippy::ptr_as_ptr)]
pub fn rlo_log_data(data: &[&[u8]]) {
#[cfg(target_os = "solana")]
unsafe {
rialo_s_define_syscall::definitions::rlo_log_data(
data as *const _ as *const u8,
data.len() as u64,
)
};
#[cfg(not(target_os = "solana"))]
core::hint::black_box(data);
}
}
pub mod sysvar {
pub use rialo_s_sysvar_id::{declare_sysvar_id, SysvarId};
#[deprecated(since = "2.2.0", note = "Use `rialo-s-sysvar` crate instead")]
#[allow(deprecated)]
pub use {
rialo_s_sdk_ids::sysvar::{check_id, id, ID},
rialo_s_sysvar::{
clock, epoch_schedule, fees, is_sysvar_id, recent_blockhashes, rent, rewards,
Sysvar, ALL_IDS,
},
};
pub mod instructions {
pub use rialo_s_instruction::{BorrowedAccountMeta, BorrowedInstruction};
#[cfg(not(target_os = "solana"))]
pub use rialo_s_instructions_sysvar::construct_instructions_data;
#[deprecated(
since = "2.2.0",
note = "Use rialo-s-instructions-sysvar crate instead"
)]
pub use rialo_s_instructions_sysvar::{
get_instruction_relative, load_current_index_checked, load_instruction_at_checked,
Instructions,
};
#[deprecated(since = "2.2.0", note = "Use rialo-s-sdk-ids crate instead")]
pub use rialo_s_sdk_ids::sysvar::instructions::{check_id, id, ID};
}
}
}
#[cfg(feature = "idl-build")]
pub use idl::IdlBuild;
#[cfg(feature = "event-cpi")]
pub use rialo_sol_attribute_event::{emit_cpi, event_cpi};
#[cfg(feature = "interface-instructions")]
pub use rialo_sol_attribute_program::interface;
pub type Result<T> = std::result::Result<T, error::Error>;
pub trait Accounts<'info, B>: ToAccountMetas + ToAccountInfos<'info> + Sized {
fn try_accounts(
program_id: &Pubkey,
accounts: &mut &'info [AccountInfo<'info>],
ix_data: &[u8],
bumps: &mut B,
reallocs: &mut BTreeSet<Pubkey>,
) -> Result<Self>;
}
pub trait Bumps {
type Bumps: Sized + Debug;
}
pub trait AccountsExit<'info>: ToAccountMetas + ToAccountInfos<'info> {
fn exit(&self, _program_id: &Pubkey) -> Result<()> {
Ok(())
}
}
pub trait AccountsClose<'info>: ToAccountInfos<'info> {
fn close(&self, sol_destination: AccountInfo<'info>) -> Result<()>;
}
pub trait ToAccountMetas {
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta>;
}
pub trait ToAccountInfos<'info> {
fn to_account_infos(&self) -> Vec<AccountInfo<'info>>;
}
pub trait ToAccountInfo<'info> {
fn to_account_info(&self) -> AccountInfo<'info>;
}
impl<'info, T> ToAccountInfo<'info> for T
where
T: AsRef<AccountInfo<'info>>,
{
fn to_account_info(&self) -> AccountInfo<'info> {
self.as_ref().clone()
}
}
pub trait Kelvins<'info>: AsRef<AccountInfo<'info>> {
fn get_kelvins(&self) -> u64 {
self.as_ref().kelvins()
}
fn add_kelvins(&self, amount: u64) -> Result<&Self> {
**self.as_ref().try_borrow_mut_kelvins()? = self
.get_kelvins()
.checked_add(amount)
.ok_or(ProgramError::ArithmeticOverflow)?;
Ok(self)
}
fn sub_kelvins(&self, amount: u64) -> Result<&Self> {
**self.as_ref().try_borrow_mut_kelvins()? = self
.get_kelvins()
.checked_sub(amount)
.ok_or(ProgramError::ArithmeticOverflow)?;
Ok(self)
}
}
impl<'info, T: AsRef<AccountInfo<'info>>> Kelvins<'info> for T {}
pub trait AccountSerialize {
fn try_serialize<W: Write>(&self, _writer: &mut W) -> Result<()> {
Ok(())
}
}
pub trait AccountDeserialize: Sized {
fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
Self::try_deserialize_unchecked(buf)
}
fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self>;
}
pub trait ZeroCopy: Discriminator + Copy + Clone + Zeroable + Pod {}
pub trait InstructionData: Discriminator + SolSerialize {
fn data(&self) -> Vec<u8> {
let mut data = Vec::with_capacity(256);
data.extend_from_slice(Self::DISCRIMINATOR);
self.serialize(&mut data).unwrap();
data
}
fn write_to(&self, mut data: &mut Vec<u8>) {
data.clear();
data.extend_from_slice(Self::DISCRIMINATOR);
self.serialize(&mut data).unwrap()
}
}
pub trait Event: SolSerialize + SolDeserialize + Discriminator {
fn data(&self) -> Vec<u8>;
}
pub trait Discriminator {
const DISCRIMINATOR: &'static [u8];
}
pub trait Space {
const INIT_SPACE: usize;
}
pub trait Bump {
fn seed(&self) -> u8;
}
pub trait Owner {
fn owner() -> Pubkey;
}
pub trait Owners {
fn owners() -> &'static [Pubkey];
}
pub trait CheckOwner {
fn check_owner(owner: &Pubkey) -> Result<()>;
}
impl<T: Owners> CheckOwner for T {
fn check_owner(owner: &Pubkey) -> Result<()> {
if !Self::owners().contains(owner) {
Err(
error::Error::from(error::ErrorCode::AccountOwnedByWrongProgram)
.with_account_name(*owner),
)
} else {
Ok(())
}
}
}
pub trait Id {
fn id() -> Pubkey;
}
pub trait Ids {
fn ids() -> &'static [Pubkey];
}
pub trait CheckId {
fn check_id(id: &Pubkey) -> Result<()>;
}
impl<T: Ids> CheckId for T {
fn check_id(id: &Pubkey) -> Result<()> {
if !Self::ids().contains(id) {
Err(error::Error::from(error::ErrorCode::InvalidProgramId).with_account_name(*id))
} else {
Ok(())
}
}
}
pub trait Key {
fn key(&self) -> Pubkey;
}
impl Key for Pubkey {
fn key(&self) -> Pubkey {
*self
}
}
pub mod prelude {
pub use borsh;
pub use error::*;
pub use rialo_sol_attribute_error::*;
pub use thiserror;
#[cfg(feature = "lazy-account")]
pub use super::accounts::lazy_account::LazyAccount;
#[cfg(feature = "idl-build")]
pub use super::idl::IdlBuild;
#[cfg(feature = "interface-instructions")]
pub use super::interface;
pub use super::{
access_control, account,
accounts::{
account::Account, account_loader::AccountLoader, interface::Interface,
interface_account::InterfaceAccount, program::Program, signer::Signer,
system_account::SystemAccount, sysvar::Sysvar, unchecked_account::UncheckedAccount,
},
constant,
context::{Context, CpiContext},
declare_id, declare_program, emit, err, error, event, instruction, program, pubkey,
require, require_eq, require_gt, require_gte, require_keys_eq, require_keys_neq,
require_neq,
rialo_s_program::bpf_loader_upgradeable::UpgradeableLoaderState,
source,
system_program::System,
zero_copy, AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit,
Discriminator, Id, InitSpace, Kelvins, Key, Owner, ProgramData, Result, SolDeserialize,
SolSerialize, Space, ToAccountInfo, ToAccountInfos, ToAccountMetas,
};
#[cfg(feature = "event-cpi")]
pub use super::{emit_cpi, event_cpi};
pub use crate::rialo_s_program::{
account_info::{next_account_info, AccountInfo},
instruction::AccountMeta,
msg,
program_error::ProgramError,
pubkey::Pubkey,
sysvar::{
clock::Clock, epoch_schedule::EpochSchedule, instructions::Instructions, rent::Rent,
rewards::Rewards, Sysvar as SolanaSysvar,
},
};
}
#[doc(hidden)]
pub mod __private {
pub use base64;
pub use bytemuck;
pub use rialo_sol_attribute_account::ZeroCopyAccessor;
use crate::rialo_s_program::pubkey::Pubkey;
pub use crate::{bpf_writer::BpfWriter, common::is_closed};
#[doc(hidden)]
pub const fn max(a: usize, b: usize) -> usize {
[a, b][(a < b) as usize]
}
#[doc(hidden)]
pub trait ZeroCopyAccessor<Ty> {
fn get(&self) -> Ty;
fn set(input: &Ty) -> Self;
}
#[doc(hidden)]
impl ZeroCopyAccessor<Pubkey> for [u8; 32] {
fn get(&self) -> Pubkey {
Pubkey::from(*self)
}
fn set(input: &Pubkey) -> [u8; 32] {
input.to_bytes()
}
}
#[cfg(feature = "lazy-account")]
pub use rialo_sol_derive_serde::Lazy;
#[cfg(feature = "lazy-account")]
pub use crate::lazy::Lazy;
}
#[macro_export]
macro_rules! require {
($invariant:expr, $error:tt $(,)?) => {
if !($invariant) {
return Err(rialo_sol_lang::error!($crate::ErrorCode::$error));
}
};
($invariant:expr, $error:expr $(,)?) => {
if !($invariant) {
return Err(rialo_sol_lang::error!($error));
}
};
}
#[macro_export]
macro_rules! require_eq {
($value1: expr, $value2: expr, $error_code:expr $(,)?) => {
if $value1 != $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 != $value2 {
return Err(error!(rialo_sol_lang::error::ErrorCode::RequireEqViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_neq {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 == $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 == $value2 {
return Err(error!(rialo_sol_lang::error::ErrorCode::RequireNeqViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_keys_eq {
($value1: expr, $value2: expr, $error_code:expr $(,)?) => {
if $value1 != $value2 {
return Err(error!($error_code).with_pubkeys(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 != $value2 {
return Err(
error!(rialo_sol_lang::error::ErrorCode::RequireKeysEqViolated)
.with_pubkeys(($value1, $value2)),
);
}
};
}
#[macro_export]
macro_rules! require_keys_neq {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 == $value2 {
return Err(error!($error_code).with_pubkeys(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 == $value2 {
return Err(
error!(rialo_sol_lang::error::ErrorCode::RequireKeysNeqViolated)
.with_pubkeys(($value1, $value2)),
);
}
};
}
#[macro_export]
macro_rules! require_gt {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 <= $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 <= $value2 {
return Err(error!(rialo_sol_lang::error::ErrorCode::RequireGtViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! require_gte {
($value1: expr, $value2: expr, $error_code: expr $(,)?) => {
if $value1 < $value2 {
return Err(error!($error_code).with_values(($value1, $value2)));
}
};
($value1: expr, $value2: expr $(,)?) => {
if $value1 < $value2 {
return Err(error!(rialo_sol_lang::error::ErrorCode::RequireGteViolated)
.with_values(($value1, $value2)));
}
};
}
#[macro_export]
macro_rules! err {
($error:tt $(,)?) => {
Err(rialo_sol_lang::error!($crate::ErrorCode::$error))
};
($error:expr $(,)?) => {
Err(rialo_sol_lang::error!($error))
};
}
#[macro_export]
macro_rules! source {
() => {
rialo_sol_lang::error::Source {
filename: file!(),
line: line!(),
}
};
}