Skip to content

Commit dfa660b

Browse files
authored
feat: Add functions to deploy Solana program (#121)
1 parent c56c47b commit dfa660b

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

frame/solana/src/lib.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub mod pallet {
113113
use parity_scale_codec::Codec;
114114
use solana_sdk::{
115115
account::Account,
116-
clock,
116+
bpf_loader, clock,
117117
feature_set::FeatureSet,
118118
fee_calculator::FeeCalculator,
119119
hash::Hash,
@@ -218,6 +218,12 @@ pub mod pallet {
218218
#[pallet::origin]
219219
pub type Origin = RawOrigin;
220220

221+
#[pallet::error]
222+
pub enum Error<T> {
223+
/// Failed to reallocate account data of this length
224+
InvalidRealloc,
225+
}
226+
221227
#[pallet::storage]
222228
#[pallet::getter(fn slot)]
223229
pub type Slot<T: Config> = StorageValue<_, clock::Slot, ValueQuery>;
@@ -328,6 +334,32 @@ pub mod pallet {
328334
}
329335

330336
impl<T: Config> Pallet<T> {
337+
pub fn create_account(pubkey: Pubkey, owner: Pubkey, executable: bool) {
338+
let who = T::AccountIdConversion::convert(pubkey);
339+
<frame_system::Pallet<T>>::inc_sufficients(&who);
340+
<AccountMeta<T>>::insert(
341+
who,
342+
AccountMetadata { owner, executable, rent_epoch: u64::MAX },
343+
);
344+
}
345+
346+
pub fn deploy_program(
347+
pubkey: Pubkey,
348+
data: Vec<u8>,
349+
owner: Option<Pubkey>,
350+
) -> Result<(), Error<T>> {
351+
let program_id = T::AccountIdConversion::convert(pubkey);
352+
let owner = owner.unwrap_or(bpf_loader::id());
353+
Self::create_account(pubkey, owner, true);
354+
if !data.is_empty() {
355+
<AccountData<T>>::insert(
356+
program_id,
357+
BoundedVec::try_from(data.to_vec()).map_err(|_| Error::InvalidRealloc)?,
358+
);
359+
}
360+
Ok(())
361+
}
362+
331363
pub fn get_hash_info_if_valid(
332364
hash: &T::Hash,
333365
max_age: BlockNumberFor<T>,

frame/solana/src/tests.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,11 @@ fn process_transaction(bank: &Bank<Test>, tx: Transaction) -> Result<()> {
8585
bank.load_execute_and_commit_sanitized_transaction(sanitized_tx)
8686
}
8787

88-
fn deploy_program(program_id: &Pubkey, data: Vec<u8>) {
88+
fn mock_deploy_program(program_id: &Pubkey, data: Vec<u8>) {
89+
<Pallet<Test>>::deploy_program(*program_id, data, None).unwrap();
90+
8991
let who = AccountIdConversion::convert(*program_id);
90-
System::inc_providers(&who);
9192
Balances::mint_into(&who, sol_into_balances(1)).unwrap();
92-
<AccountMeta<Test>>::insert(
93-
&who,
94-
AccountMetadata { rent_epoch: u64::MAX, owner: bpf_loader::id(), executable: true },
95-
);
96-
<AccountData<Test>>::insert(&who, BoundedVec::try_from(data).expect("account data"));
9793
}
9894

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

215-
deploy_program(&token_program_id, program_data);
211+
mock_deploy_program(&token_program_id, program_data);
216212

217213
let create_account = system_instruction::create_account(
218214
&authority.pubkey(),

0 commit comments

Comments
 (0)