From e10b8eedd15ddd10ccc899262b76f62881b9bb04 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Mon, 8 Jul 2024 20:49:27 +0000 Subject: [PATCH] move spl cpis to utils --- Cargo.lock | 2 + Cargo.toml | 2 +- program/src/claim.rs | 22 ++---- program/src/initialize.rs | 25 +++---- program/src/stake.rs | 22 ++---- program/src/upgrade.rs | 22 ++---- utils/Cargo.toml | 6 ++ utils/src/lib.rs | 3 + utils/src/spl.rs | 138 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 180 insertions(+), 62 deletions(-) create mode 100644 utils/src/spl.rs diff --git a/Cargo.lock b/Cargo.lock index 3325105..bfe82ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2624,6 +2624,8 @@ version = "2.0.0" dependencies = [ "bytemuck", "solana-program", + "spl-associated-token-account", + "spl-token", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 41d214d..b2fcdb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ drillx = { git = "https://github.com/regolith-labs/drillx", branch = "master", f mpl-token-metadata = "4.1.2" num_enum = "0.7.2" ore-api = { path = "api" } -ore-utils = { path = "utils" } +ore-utils = { path = "utils", features = ["spl"] } shank = "0.3.0" solana-program = "1.18" spl-token = { version = "^4", features = ["no-entrypoint"] } diff --git a/program/src/claim.rs b/program/src/claim.rs index c6a68b9..0bee6fd 100644 --- a/program/src/claim.rs +++ b/program/src/claim.rs @@ -1,4 +1,5 @@ use ore_api::{consts::*, error::OreError, instruction::ClaimArgs, loaders::*, state::Proof}; +use ore_utils::spl::transfer_signed; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, @@ -45,21 +46,12 @@ pub fn process_claim<'a, 'info>( .ok_or(OreError::ClaimTooLarge)?; // Distribute tokens from treasury to beneficiary - solana_program::program::invoke_signed( - &spl_token::instruction::transfer( - &spl_token::id(), - treasury_tokens_info.key, - beneficiary_info.key, - treasury_info.key, - &[treasury_info.key], - amount, - )?, - &[ - token_program.clone(), - treasury_tokens_info.clone(), - beneficiary_info.clone(), - treasury_info.clone(), - ], + transfer_signed( + treasury_info, + treasury_tokens_info, + beneficiary_info, + token_program, + amount, &[&[TREASURY, &[TREASURY_BUMP]]], )?; diff --git a/program/src/initialize.rs b/program/src/initialize.rs index 3679132..1a18dbe 100644 --- a/program/src/initialize.rs +++ b/program/src/initialize.rs @@ -6,6 +6,7 @@ use ore_api::{ loaders::*, state::{Bus, Config, Treasury}, }; +use ore_utils::spl::create_ata; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, @@ -209,22 +210,14 @@ pub fn process_initialize<'a, 'info>( .invoke_signed(&[&[TREASURY, &[args.treasury_bump]]])?; // Initialize treasury token account - solana_program::program::invoke( - &spl_associated_token_account::instruction::create_associated_token_account( - signer.key, - treasury_info.key, - mint_info.key, - &spl_token::id(), - ), - &[ - associated_token_program.clone(), - signer.clone(), - treasury_tokens_info.clone(), - treasury_info.clone(), - mint_info.clone(), - system_program.clone(), - token_program.clone(), - ], + create_ata( + signer, + treasury_info, + treasury_tokens_info, + mint_info, + system_program, + token_program, + associated_token_program, )?; Ok(()) diff --git a/program/src/stake.rs b/program/src/stake.rs index 10f43e0..f3437ff 100644 --- a/program/src/stake.rs +++ b/program/src/stake.rs @@ -1,4 +1,5 @@ use ore_api::{consts::*, instruction::StakeArgs, loaders::*, state::Proof}; +use ore_utils::spl::transfer; use solana_program::{ account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar, @@ -43,21 +44,12 @@ pub fn process_stake<'a, 'info>( proof.last_stake_at = clock.unix_timestamp; // Distribute tokens from signer to treasury - solana_program::program::invoke( - &spl_token::instruction::transfer( - &spl_token::id(), - sender_info.key, - treasury_tokens_info.key, - signer.key, - &[signer.key], - amount, - )?, - &[ - token_program.clone(), - sender_info.clone(), - treasury_tokens_info.clone(), - signer.clone(), - ], + transfer( + signer, + sender_info, + treasury_tokens_info, + token_program, + amount, )?; Ok(()) diff --git a/program/src/upgrade.rs b/program/src/upgrade.rs index b74524b..7ece8b9 100644 --- a/program/src/upgrade.rs +++ b/program/src/upgrade.rs @@ -1,4 +1,5 @@ use ore_api::{consts::*, error::OreError, instruction::StakeArgs, loaders::*}; +use ore_utils::spl::mint_to_signed; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, @@ -65,21 +66,12 @@ pub fn process_upgrade<'a, 'info>( drop(mint_data); // Mint to the beneficiary account - solana_program::program::invoke_signed( - &spl_token::instruction::mint_to( - &spl_token::id(), - mint_info.key, - beneficiary_info.key, - treasury_info.key, - &[treasury_info.key], - amount_to_mint, - )?, - &[ - token_program.clone(), - mint_info.clone(), - beneficiary_info.clone(), - treasury_info.clone(), - ], + mint_to_signed( + mint_info, + beneficiary_info, + treasury_info, + token_program, + amount_to_mint, &[&[TREASURY, &[TREASURY_BUMP]]], )?; diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 3e40a35..2af4092 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -9,6 +9,12 @@ 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 } diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 1694c98..a7088cb 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "spl")] +pub mod spl; + use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, rent::Rent, sysvar::Sysvar, diff --git a/utils/src/spl.rs b/utils/src/spl.rs new file mode 100644 index 0000000..bb60d52 --- /dev/null +++ b/utils/src/spl.rs @@ -0,0 +1,138 @@ +use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult}; + +#[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(), + ], + ) +} + +#[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(), + ], + ) +} + +#[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, + ) +} + +#[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, + ) +} + +#[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(), + ], + ) +}