Skip to content

Commit

Permalink
feat: Add functions to deploy Solana program (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d authored Jan 12, 2025
1 parent c56c47b commit dfa660b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
34 changes: 33 additions & 1 deletion frame/solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub mod pallet {
use parity_scale_codec::Codec;
use solana_sdk::{
account::Account,
clock,
bpf_loader, clock,
feature_set::FeatureSet,
fee_calculator::FeeCalculator,
hash::Hash,
Expand Down Expand Up @@ -218,6 +218,12 @@ pub mod pallet {
#[pallet::origin]
pub type Origin = RawOrigin;

#[pallet::error]
pub enum Error<T> {
/// Failed to reallocate account data of this length
InvalidRealloc,
}

#[pallet::storage]
#[pallet::getter(fn slot)]
pub type Slot<T: Config> = StorageValue<_, clock::Slot, ValueQuery>;
Expand Down Expand Up @@ -328,6 +334,32 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
pub fn create_account(pubkey: Pubkey, owner: Pubkey, executable: bool) {
let who = T::AccountIdConversion::convert(pubkey);
<frame_system::Pallet<T>>::inc_sufficients(&who);
<AccountMeta<T>>::insert(
who,
AccountMetadata { owner, executable, rent_epoch: u64::MAX },
);
}

pub fn deploy_program(
pubkey: Pubkey,
data: Vec<u8>,
owner: Option<Pubkey>,
) -> Result<(), Error<T>> {
let program_id = T::AccountIdConversion::convert(pubkey);
let owner = owner.unwrap_or(bpf_loader::id());
Self::create_account(pubkey, owner, true);
if !data.is_empty() {
<AccountData<T>>::insert(
program_id,
BoundedVec::try_from(data.to_vec()).map_err(|_| Error::InvalidRealloc)?,
);
}
Ok(())
}

pub fn get_hash_info_if_valid(
hash: &T::Hash,
max_age: BlockNumberFor<T>,
Expand Down
12 changes: 4 additions & 8 deletions frame/solana/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,11 @@ fn process_transaction(bank: &Bank<Test>, tx: Transaction) -> Result<()> {
bank.load_execute_and_commit_sanitized_transaction(sanitized_tx)
}

fn deploy_program(program_id: &Pubkey, data: Vec<u8>) {
fn mock_deploy_program(program_id: &Pubkey, data: Vec<u8>) {
<Pallet<Test>>::deploy_program(*program_id, data, None).unwrap();

let who = AccountIdConversion::convert(*program_id);
System::inc_providers(&who);
Balances::mint_into(&who, sol_into_balances(1)).unwrap();
<AccountMeta<Test>>::insert(
&who,
AccountMetadata { rent_epoch: u64::MAX, owner: bpf_loader::id(), executable: true },
);
<AccountData<Test>>::insert(&who, BoundedVec::try_from(data).expect("account data"));
}

#[test]
Expand Down Expand Up @@ -212,7 +208,7 @@ fn spl_token_program_should_work() {
let program_data =
std::fs::read("tests/example-programs/token/token_program.so").expect("program data");

deploy_program(&token_program_id, program_data);
mock_deploy_program(&token_program_id, program_data);

let create_account = system_instruction::create_account(
&authority.pubkey(),
Expand Down

0 comments on commit dfa660b

Please sign in to comment.