remove ore-utils

This commit is contained in:
Hardhat Chad
2024-09-25 06:46:34 +00:00
parent 7814e28847
commit 11d710ab9f
8 changed files with 1 additions and 632 deletions

10
Cargo.lock generated
View File

@@ -1316,16 +1316,6 @@ dependencies = [
"steel",
]
[[package]]
name = "ore-utils"
version = "2.1.9"
dependencies = [
"bytemuck",
"solana-program",
"spl-associated-token-account",
"spl-token",
]
[[package]]
name = "parking_lot"
version = "0.12.3"

View File

@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["api", "program", "utils"]
members = ["api", "program"]
[workspace.package]
version = "2.1.9"

View File

@@ -1,20 +0,0 @@
[package]
name = "ore-utils"
description = "Utils for building ORE programs"
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
keywords.workspace = true
[features]
deafult = []
spl = ["spl-token", "spl-associated-token-account"]
[dependencies]
bytemuck.workspace = true
solana-program.workspace = true
spl-token = { workspace = true, optional = true }
spl-associated-token-account = { workspace = true, optional = true }

View File

@@ -1,214 +0,0 @@
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey, rent::Rent,
sysvar::Sysvar,
};
/// Creates a new pda.
#[inline(always)]
pub fn create_pda<'a, 'info>(
target_account: &'a AccountInfo<'info>,
owner: &Pubkey,
space: usize,
pda_seeds: &[&[u8]],
system_program: &'a AccountInfo<'info>,
payer: &'a AccountInfo<'info>,
) -> ProgramResult {
let rent = Rent::get()?;
if target_account.lamports().eq(&0) {
// If balance is zero, create account
solana_program::program::invoke_signed(
&solana_program::system_instruction::create_account(
payer.key,
target_account.key,
rent.minimum_balance(space),
space as u64,
owner,
),
&[
payer.clone(),
target_account.clone(),
system_program.clone(),
],
&[pda_seeds],
)?;
} else {
// Otherwise, if balance is nonzero:
// 1) transfer sufficient lamports for rent exemption
let rent_exempt_balance = rent
.minimum_balance(space)
.saturating_sub(target_account.lamports());
if rent_exempt_balance.gt(&0) {
solana_program::program::invoke(
&solana_program::system_instruction::transfer(
payer.key,
target_account.key,
rent_exempt_balance,
),
&[
payer.clone(),
target_account.clone(),
system_program.clone(),
],
)?;
}
// 2) allocate space for the account
solana_program::program::invoke_signed(
&solana_program::system_instruction::allocate(target_account.key, space as u64),
&[target_account.clone(), system_program.clone()],
&[pda_seeds],
)?;
// 3) assign our program as the owner
solana_program::program::invoke_signed(
&solana_program::system_instruction::assign(target_account.key, owner),
&[target_account.clone(), system_program.clone()],
&[pda_seeds],
)?;
}
Ok(())
}
#[cfg(feature = "spl")]
#[inline(always)]
pub fn create_ata<'info>(
funder_info: &AccountInfo<'info>,
owner_info: &AccountInfo<'info>,
token_account_info: &AccountInfo<'info>,
mint_info: &AccountInfo<'info>,
system_program: &AccountInfo<'info>,
token_program: &AccountInfo<'info>,
associated_token_program: &AccountInfo<'info>,
) -> ProgramResult {
solana_program::program::invoke(
&spl_associated_token_account::instruction::create_associated_token_account(
funder_info.key,
owner_info.key,
mint_info.key,
&spl_token::id(),
),
&[
funder_info.clone(),
token_account_info.clone(),
owner_info.clone(),
mint_info.clone(),
system_program.clone(),
token_program.clone(),
associated_token_program.clone(),
],
)
}
#[cfg(feature = "spl")]
#[inline(always)]
pub fn transfer<'info>(
authority_info: &AccountInfo<'info>,
from_info: &AccountInfo<'info>,
to_info: &AccountInfo<'info>,
token_program: &AccountInfo<'info>,
amount: u64,
) -> ProgramResult {
solana_program::program::invoke(
&spl_token::instruction::transfer(
&spl_token::id(),
from_info.key,
to_info.key,
authority_info.key,
&[authority_info.key],
amount,
)?,
&[
token_program.clone(),
from_info.clone(),
to_info.clone(),
authority_info.clone(),
],
)
}
#[cfg(feature = "spl")]
#[inline(always)]
pub fn transfer_signed<'info>(
authority_info: &AccountInfo<'info>,
from_info: &AccountInfo<'info>,
to_info: &AccountInfo<'info>,
token_program: &AccountInfo<'info>,
amount: u64,
signer_seeds: &[&[&[u8]]],
) -> ProgramResult {
solana_program::program::invoke_signed(
&spl_token::instruction::transfer(
&spl_token::id(),
from_info.key,
to_info.key,
authority_info.key,
&[authority_info.key],
amount,
)?,
&[
token_program.clone(),
from_info.clone(),
to_info.clone(),
authority_info.clone(),
],
signer_seeds,
)
}
#[cfg(feature = "spl")]
#[inline(always)]
pub fn mint_to_signed<'info>(
mint_info: &AccountInfo<'info>,
to_info: &AccountInfo<'info>,
authority_info: &AccountInfo<'info>,
token_program: &AccountInfo<'info>,
amount: u64,
signer_seeds: &[&[&[u8]]],
) -> ProgramResult {
solana_program::program::invoke_signed(
&spl_token::instruction::mint_to(
&spl_token::id(),
mint_info.key,
to_info.key,
authority_info.key,
&[authority_info.key],
amount,
)?,
&[
token_program.clone(),
mint_info.clone(),
to_info.clone(),
authority_info.clone(),
],
signer_seeds,
)
}
#[cfg(feature = "spl")]
#[inline(always)]
pub fn burn<'info>(
token_account_info: &AccountInfo<'info>,
mint_info: &AccountInfo<'info>,
authority_info: &AccountInfo<'info>,
token_program: &AccountInfo<'info>,
amount: u64,
) -> ProgramResult {
solana_program::program::invoke(
&spl_token::instruction::burn(
&spl_token::id(),
token_account_info.key,
mint_info.key,
authority_info.key,
&[authority_info.key],
amount,
)?,
&[
token_program.clone(),
token_account_info.clone(),
mint_info.clone(),
authority_info.clone(),
],
)
}

View File

@@ -1,8 +0,0 @@
mod cpi;
mod loaders;
pub mod macros;
mod traits;
pub use cpi::*;
pub use loaders::*;
pub use traits::*;

View File

@@ -1,249 +0,0 @@
#[cfg(feature = "spl")]
use solana_program::program_pack::Pack;
use solana_program::{
account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, system_program, sysvar,
};
#[cfg(feature = "spl")]
use spl_token::state::Mint;
/// Errors if:
/// - Account is not a signer.
pub fn load_signer(info: &AccountInfo<'_>) -> Result<(), ProgramError> {
if !info.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
Ok(())
}
/// Errors if:
/// - Address does not match PDA derived from provided seeds.
/// - Cannot load as an uninitialized account.
pub fn load_uninitialized_pda(
info: &AccountInfo<'_>,
seeds: &[&[u8]],
bump: u8,
program_id: &Pubkey,
) -> Result<(), ProgramError> {
let pda = Pubkey::find_program_address(seeds, program_id);
if info.key.ne(&pda.0) {
return Err(ProgramError::InvalidSeeds);
}
if bump.ne(&pda.1) {
return Err(ProgramError::InvalidSeeds);
}
load_system_account(info, true)
}
/// Errors if:
/// - Owner is not the system program.
/// - Data is not empty.
/// - Account is not writable.
pub fn load_system_account(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if info.owner.ne(&system_program::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if !info.data_is_empty() {
return Err(ProgramError::AccountAlreadyInitialized);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not the sysvar address.
/// - Account cannot load with the expected address.
pub fn load_sysvar(info: &AccountInfo<'_>, key: Pubkey) -> Result<(), ProgramError> {
if info.owner.ne(&sysvar::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
load_account(info, key, false)
}
/// Errors if:
/// - Address does not match the expected value.
/// - Expected to be writable, but is not.
pub fn load_account(
info: &AccountInfo<'_>,
key: Pubkey,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.key.ne(&key) {
return Err(ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Address does not match the expected value.
/// - Account is not executable.
pub fn load_program(info: &AccountInfo<'_>, key: Pubkey) -> Result<(), ProgramError> {
if info.key.ne(&key) {
return Err(ProgramError::IncorrectProgramId);
}
if !info.executable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Account is not writable.
pub fn load_any(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not SPL token program.
/// - Address does not match the expected mint address.
/// - Data is empty.
/// - Data cannot deserialize into a mint account.
/// - Expected to be writable, but is not.
#[cfg(feature = "spl")]
pub fn load_mint(
info: &AccountInfo<'_>,
address: Pubkey,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.owner.ne(&spl_token::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.key.ne(&address) {
return Err(ProgramError::InvalidSeeds);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
Mint::unpack(&info.data.borrow())?;
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not SPL token program.
/// - Data is empty.
/// - Data cannot deserialize into a mint account.
/// - Expected to be writable, but is not.
#[cfg(feature = "spl")]
pub fn load_any_mint(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if info.owner.ne(&spl_token::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
Mint::unpack(&info.data.borrow())?;
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not SPL token program.
/// - Data is empty.
/// - Data cannot deserialize into a token account.
/// - Token account owner does not match the expected owner address.
/// - Token account mint does not match the expected mint address.
/// - Expected to be writable, but is not.
#[cfg(feature = "spl")]
pub fn load_token_account(
info: &AccountInfo<'_>,
owner: Option<&Pubkey>,
mint: &Pubkey,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.owner.ne(&spl_token::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
let account_data = info.data.borrow();
let account = spl_token::state::Account::unpack(&account_data)?;
if account.mint.ne(&mint) {
return Err(ProgramError::InvalidAccountData);
}
if let Some(owner) = owner {
if account.owner.ne(owner) {
return Err(ProgramError::InvalidAccountData);
}
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not SPL token program.
/// - Data is empty.
/// - Data cannot deserialize into a token account.
/// - Address does not match the expected associated token address.
/// - Expected to be writable, but is not.
#[cfg(feature = "spl")]
pub fn load_associated_token_account(
info: &AccountInfo<'_>,
owner: &Pubkey,
mint: &Pubkey,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.owner.ne(&spl_token::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
let account_data = info.data.borrow();
let _ = spl_token::state::Account::unpack(&account_data)?;
let address = spl_associated_token_account::get_associated_token_address(owner, mint);
if info.key.ne(&address) {
return Err(ProgramError::InvalidSeeds);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}

View File

@@ -1,120 +0,0 @@
#[macro_export]
macro_rules! impl_to_bytes {
($struct_name:ident) => {
impl $struct_name {
pub fn to_bytes(&self) -> &[u8] {
bytemuck::bytes_of(self)
}
}
};
}
#[macro_export]
macro_rules! impl_from_bytes {
($struct_name:ident) => {
impl $struct_name {
pub fn from_bytes(data: &[u8]) -> &Self {
bytemuck::from_bytes::<Self>(data)
}
}
};
}
#[macro_export]
macro_rules! impl_account_from_bytes {
($struct_name:ident) => {
impl $crate::AccountDeserialize for $struct_name {
fn try_from_bytes(
data: &[u8],
) -> Result<&Self, solana_program::program_error::ProgramError> {
if Self::discriminator().ne(&data[0]) {
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
}
bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
solana_program::program_error::ProgramError::InvalidAccountData,
))
}
fn try_from_bytes_mut(
data: &mut [u8],
) -> Result<&mut Self, solana_program::program_error::ProgramError> {
if Self::discriminator().ne(&data[0]) {
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
}
bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
solana_program::program_error::ProgramError::InvalidAccountData,
))
}
}
};
}
#[macro_export]
macro_rules! impl_instruction_from_bytes {
($struct_name:ident) => {
impl $struct_name {
pub fn try_from_bytes(
data: &[u8],
) -> Result<&Self, solana_program::program_error::ProgramError> {
bytemuck::try_from_bytes::<Self>(data).or(Err(
solana_program::program_error::ProgramError::InvalidInstructionData,
))
}
}
};
}
#[macro_export]
macro_rules! account {
($discriminator_name:ident, $struct_name:ident) => {
$crate::impl_to_bytes!($struct_name);
$crate::impl_account_from_bytes!($struct_name);
impl $crate::Discriminator for $struct_name {
fn discriminator() -> u8 {
$discriminator_name::$struct_name.into()
}
}
};
}
#[macro_export]
macro_rules! error {
($struct_name:ident) => {
impl From<$struct_name> for solana_program::program_error::ProgramError {
fn from(e: $struct_name) -> Self {
solana_program::program_error::ProgramError::Custom(e as u32)
}
}
};
}
#[macro_export]
macro_rules! event {
($struct_name:ident) => {
$crate::impl_to_bytes!($struct_name);
$crate::impl_from_bytes!($struct_name);
};
}
#[macro_export]
macro_rules! instruction {
($discriminator_name:ident, $struct_name:ident) => {
$crate::impl_instruction_from_bytes!($struct_name);
impl $crate::Discriminator for $struct_name {
fn discriminator() -> u8 {
$discriminator_name::$struct_name as u8
}
}
impl $struct_name {
pub fn to_bytes(&self) -> Vec<u8> {
[
[$discriminator_name::$struct_name as u8].to_vec(),
bytemuck::bytes_of(self).to_vec(),
]
.concat()
}
}
};
}

View File

@@ -1,10 +0,0 @@
use solana_program::program_error::ProgramError;
pub trait AccountDeserialize {
fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError>;
fn try_from_bytes_mut(data: &mut [u8]) -> Result<&mut Self, ProgramError>;
}
pub trait Discriminator {
fn discriminator() -> u8;
}