4

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:

  1. Compile another smart contract, cargo build-bpf. I used the most basic for testing.

  2. 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.

  3. 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>,
}

2 Answers 2

5

This is an anti-pattern on Solana. Program code and data are stored separately, so there is no need to redeploy the code for each instance of the program. It is sufficient to organize program data such that instances/users are segregated as you wish.

For instance a program's code can be deployed once by the developer. It then requires a "config" account, controlled by some instance owner. Each user of an instance then references the corresponding config account along with their own account(s) when interacting with the program. The program logic then needs to enforce tying user accounts to the given config

2
  • is there any resources or codebase that can be referenced? struggling with the similiar issue but having trouble figuring this out. Commented Mar 1 at 15:36
  • the most popular example would probably be spl token, where a "mint" declares a token instance, under which users have accounts
    – trent.sol
    Commented Apr 2 at 8:11
0

AFAIK, you don't deploy new contracts. But you can deploy a new set of storage that is used by the program. I would think of the new set of storage as something like a new instance of a contract. To clarify, you could have different PDAs (program derived accounts) which storage state for different "instances". This can be based off of a user's public key or some other unique seed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.