Is there a way to implement a factory pattern contracts with rust on solana? Is it similar to the ethereum way? Please show an example.
Went through discord and docs, probably missed it, but couldn't find an answer.
What is the workaround, if missing?
Right now i think that it will look smth like this:
Compile another smart contract, cargo build-bpf. I used the most basic for testing.
Put it into buffer: solana write-buffer -> it returns a Buffer:some string. I am not sure what that is but i included that into program to test ( down below). My guesses that it might be the compiled data, that's how i used it.
bincode it, create, write and finalize a transaction. Right now (line 3) gives an error. Not sure why, will fix.
src/lib.rs
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{system_instruction, loader_instruction} ;
use anchor_lang::solana_program::signature::{Signer, keypair} ;
use bincode;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod test {
use super::*;
pub fn initialize(ctx: Context<Factory>) -> Result<()> {
let program_keypair = keypair::Keypair::new();
let program_pubkey = program_keypair.pubkey();
let target = Some("8pLNgkLvgFGA2NnUb1i1w7GVEoS48XGYQRWiZ9c1Jei5".to_string());
let program: Vec<u8> = bincode::serialize(&target).unwrap();
let instruction = system_instruction::create_account(&ctx.accounts.authority.key(),
&program_pubkey, 6000, 2000, &ctx.accounts.authority.key());
let chunk_size: usize = 512;
let mut offset = 0;
for chunk in program.chunks(chunk_size) {
let instruction =
loader_instruction::write(&program_pubkey, &ctx.accounts.authority.key(), offset, chunk.to_vec());
offset += chunk_size as u32;
}
let instruction = loader_instruction::finalize(&program_pubkey, &ctx.accounts.authority.key());
Ok(())
}
}
#[derive(Accounts)]
pub struct Factory<'info> {
authority: Signer<'info>,
}