Skip to content

Commit

Permalink
Split instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen committed Apr 25, 2024
1 parent 40e599c commit 05f2682
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 117 deletions.
52 changes: 31 additions & 21 deletions cli/src/processor/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use anchor_spl::{associated_token, associated_token::get_associated_token_addres
use sablier_network_program::state::{
Config, Fee, Penalty, Registry, Snapshot, SnapshotFrame, Worker, WorkerSettings,
};
use solana_sdk::{
instruction::AccountMeta,
signature::{Keypair, Signer},
};
use solana_sdk::signature::{Keypair, Signer};

use crate::{client::Client, errors::CliError};

Expand Down Expand Up @@ -91,29 +88,42 @@ pub fn create(client: &Client, signatory: Keypair, silent: bool) -> Result<(), C
// Build ix
let worker_id = registry.total_workers;
let worker_pubkey = Worker::pubkey(worker_id);
let mut accounts = sablier_network_program::accounts::WorkerCreate {
associated_token_program: associated_token::ID,
authority: client.payer_pubkey(),
config: Config::pubkey(),
fee: Fee::pubkey(worker_pubkey),
mint: config.mint,
registry: Registry::pubkey(),
system_program: system_program::ID,
token_program: token::ID,
worker: worker_pubkey,
worker_tokens: get_associated_token_address(&worker_pubkey, &config.mint),
}
.to_account_metas(Some(false));
accounts.push(AccountMeta::new_readonly(signatory.pubkey(), true));
accounts.push(AccountMeta::new(Penalty::pubkey(worker_pubkey), false));

let ix = Instruction {
program_id: sablier_network_program::ID,
accounts,
accounts: sablier_network_program::accounts::WorkerCreate {
associated_token_program: associated_token::ID,
authority: client.payer_pubkey(),
config: Config::pubkey(),
// fee: Fee::pubkey(worker_pubkey),
signatory: signatory.pubkey(),
mint: config.mint,
registry: Registry::pubkey(),
system_program: system_program::ID,
token_program: token::ID,
worker: worker_pubkey,
worker_tokens: get_associated_token_address(&worker_pubkey, &config.mint),
}
.to_account_metas(Some(false)),
data: sablier_network_program::instruction::WorkerCreate {}.data(),
};

let worker_pubkey = Worker::pubkey(worker_id + 1);
let ix_utils = Instruction {
program_id: sablier_network_program::ID,
accounts: sablier_network_program::accounts::WorkerUtilsCreate {
authority: client.payer_pubkey(),
worker: worker_pubkey,
registry: Registry::pubkey(),
fee: Fee::pubkey(worker_pubkey),
penalty: Penalty::pubkey(worker_pubkey),
system_program: system_program::ID,
}
.to_account_metas(Some(false)),
data: sablier_network_program::instruction::WorkerUtilsCreate {}.data(),
};
client
.send_and_confirm(&[ix], &[client.payer(), &signatory])
.send_and_confirm(&[ix, ix_utils], &[client.payer(), &signatory])
.unwrap();
if !silent {
get(client, worker_id)?;
Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn handler(ctx: Context<Initialize>) -> Result<()> {

// Initialize accounts.
config.init(admin.key(), mint.key())?;
registry.init()?;
registry.init(ctx.bumps.registry)?;
snapshot.init(0)?;

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions programs/network/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod unstake_create;
pub mod worker_claim;
pub mod worker_create;
pub mod worker_update;
pub mod worker_utils_create;

pub use config_update::*;
pub use delegation_claim::*;
Expand All @@ -31,3 +32,4 @@ pub use unstake_create::*;
pub use worker_claim::*;
pub use worker_create::*;
pub use worker_update::*;
pub use worker_utils_create::*;
81 changes: 11 additions & 70 deletions programs/network/src/instructions/worker_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ pub struct WorkerCreate<'info> {
#[account(address = Config::pubkey())]
pub config: AccountLoader<'info, Config>,

#[account(
init,
seeds = [
SEED_FEE,
worker.key().as_ref(),
],
bump,
payer = authority,
space = 8 + Fee::INIT_SPACE,
)]
pub fee: AccountLoader<'info, Fee>,

#[account(address = config.load()?.mint)]
pub mint: Box<Account<'info, Mint>>,

Expand All @@ -38,6 +26,9 @@ pub struct WorkerCreate<'info> {
)]
pub registry: Box<Account<'info, Registry>>,

#[account(constraint = signatory.key() != authority.key() @ SablierError::InvalidSignatory)]
pub signatory: Signer<'info>,

#[account(
init,
seeds = [
Expand Down Expand Up @@ -65,70 +56,20 @@ pub struct WorkerCreate<'info> {
pub token_program: Program<'info, Token>,
}

pub fn handler<'info>(ctx: Context<'_, '_, 'info, 'info, WorkerCreate<'info>>) -> Result<()> {
pub fn handler(ctx: Context<WorkerCreate>) -> Result<()> {
// Get accounts
let authority = &mut ctx.accounts.authority;
let fee = &mut ctx.accounts.fee;
let registry = &mut ctx.accounts.registry;
let worker = &mut ctx.accounts.worker;

let signatory = {
let signatory_info = ctx
.remaining_accounts
.first()
.ok_or(ErrorCode::AccountNotEnoughKeys)?;

if !signatory_info.is_signer {
return Err(ErrorCode::AccountNotSigner.into());
}

if signatory_info.key == authority.key {
return Err(SablierError::InvalidSignatory.into());
}

Signer::try_from(signatory_info)?
};

let mut penalty: Account<Penalty> = {
let penalty_info = ctx
.remaining_accounts
.get(1)
.ok_or(ErrorCode::AccountNotEnoughKeys)?;

if !penalty_info.is_writable {
return Err(ErrorCode::AccountNotMutable.into());
}

let (pda_key, bump) =
Pubkey::find_program_address(&[SEED_PENALTY, worker.key().as_ref()], &crate::ID);

if &pda_key != penalty_info.key {
return Err(ErrorCode::ConstraintSeeds.into());
}

let account_space = 8 + Penalty::INIT_SPACE;
let lamports = Rent::get()?.minimum_balance(account_space);
let cpi_accounts = anchor_lang::system_program::CreateAccount {
from: authority.to_account_info(),
to: penalty_info.to_owned(),
};
let cpi_context = anchor_lang::context::CpiContext::new(
ctx.accounts.system_program.to_account_info(),
cpi_accounts,
);
anchor_lang::system_program::create_account(
cpi_context.with_signer(&[&[SEED_PENALTY, worker.key().as_ref(), &[bump][..]][..]]),
lamports,
account_space as u64,
&crate::ID,
)?;
Account::try_from_unchecked(penalty_info)?
};
let signatory = &mut ctx.accounts.signatory;

// Initialize the worker accounts.
worker.init(authority, registry.total_workers, &signatory)?;
fee.init(worker.key())?;
penalty.init(worker.key())?;
worker.init(
authority,
registry.total_workers,
signatory,
ctx.bumps.worker,
)?;

// Update the registry's worker counter.
registry.total_workers += 1;
Expand Down
64 changes: 64 additions & 0 deletions programs/network/src/instructions/worker_utils_create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use anchor_lang::prelude::*;

use crate::{
constants::{SEED_FEE, SEED_PENALTY, SEED_REGISTRY, SEED_WORKER},
Fee, FeeAccount, Penalty, PenaltyAccount, Registry, Worker,
};

#[derive(Accounts)]
pub struct WorkerUtilsCreate<'info> {
#[account(mut)]
pub authority: Signer<'info>,

#[account(
seeds = [
SEED_WORKER,
registry.total_workers.to_be_bytes().as_ref(),
],
bump = worker.bump,
)]
pub worker: Account<'info, Worker>,

#[account(
seeds = [SEED_REGISTRY],
bump = registry.bump,
)]
pub registry: Account<'info, Registry>,

#[account(
init,
seeds = [
SEED_FEE,
worker.key().as_ref(),
],
bump,
payer = authority,
space = 8 + Fee::INIT_SPACE,
)]
pub fee: Account<'info, Fee>,

#[account(
init,
seeds = [
SEED_PENALTY,
worker.key().as_ref(),
],
bump,
payer = authority,
space = 8 + Penalty::INIT_SPACE,
)]
pub penalty: Account<'info, Penalty>,

pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<WorkerUtilsCreate>) -> Result<()> {
let worker = &mut ctx.accounts.worker;
let fee = &mut ctx.accounts.fee;
let penalty = &mut ctx.accounts.penalty;

fee.init(worker.key(), ctx.bumps.fee)?;
penalty.init(worker.key(), ctx.bumps.penalty)?;

Ok(())
}
7 changes: 3 additions & 4 deletions programs/network/src/jobs/distribute_fees/process_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ pub struct DistributeFeesProcessEntry<'info> {
mut,
seeds = [
SEED_FEE,
fee.load()?.worker.as_ref(),
fee.worker.as_ref(),
],
bump,
has_one = worker,
)]
pub fee: AccountLoader<'info, Fee>,
pub fee: Account<'info, Fee>,

#[account(address = Registry::pubkey())]
pub registry: Account<'info, Registry>,
Expand Down Expand Up @@ -66,7 +66,6 @@ pub fn handler(ctx: Context<DistributeFeesProcessEntry>) -> Result<ThreadRespons
let config = &ctx.accounts.config;
let delegation = &mut ctx.accounts.delegation;
let fee = &mut ctx.accounts.fee;
let fee_data = fee.load()?;
let registry = &ctx.accounts.registry;
let snapshot = &ctx.accounts.snapshot;
let snapshot_entry = &ctx.accounts.snapshot_entry;
Expand All @@ -76,7 +75,7 @@ pub fn handler(ctx: Context<DistributeFeesProcessEntry>) -> Result<ThreadRespons

// Calculate the balance of this particular delegation, based on the weight of its stake with this worker.
let distribution_balance = if snapshot_frame.stake_amount > 0 {
fee_data.distributable_balance * snapshot_entry.stake_amount / snapshot_frame.stake_amount
fee.distributable_balance * snapshot_entry.stake_amount / snapshot_frame.stake_amount
} else {
0
};
Expand Down
7 changes: 3 additions & 4 deletions programs/network/src/jobs/distribute_fees/process_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub struct DistributeFeesProcessFrame<'info> {
mut,
seeds = [
SEED_FEE,
fee.load()?.worker.as_ref(),
fee.worker.as_ref(),
],
bump,
has_one = worker,
)]
pub fee: AccountLoader<'info, Fee>,
pub fee: Account<'info, Fee>,

#[account(address = Registry::pubkey())]
pub registry: Account<'info, Registry>,
Expand Down Expand Up @@ -46,7 +46,6 @@ pub fn handler(ctx: Context<DistributeFeesProcessFrame>) -> Result<ThreadRespons
// Get accounts.
let config = &ctx.accounts.config;
let fee = &mut ctx.accounts.fee;
let fee_data = &mut fee.load_mut()?;
let registry = &ctx.accounts.registry;
let snapshot = &ctx.accounts.snapshot;
let snapshot_frame = &ctx.accounts.snapshot_frame;
Expand All @@ -70,7 +69,7 @@ pub fn handler(ctx: Context<DistributeFeesProcessFrame>) -> Result<ThreadRespons
worker.commission_balance += commission_balance;

// Record the balance that is distributable to delegations.
fee_data.distributable_balance = fee_usable_balance - commission_balance;
fee.distributable_balance = fee_usable_balance - commission_balance;

// Build next instruction for the thread.
let dynamic_instruction = if snapshot_frame.total_entries > 0 {
Expand Down
8 changes: 5 additions & 3 deletions programs/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ pub mod network_program {
worker_claim::handler(ctx, amount)
}

pub fn worker_create<'info>(
ctx: Context<'_, '_, 'info, 'info, WorkerCreate<'info>>,
) -> Result<()> {
pub fn worker_create(ctx: Context<WorkerCreate>) -> Result<()> {
worker_create::handler(ctx)
}

pub fn worker_utils_create(ctx: Context<WorkerUtilsCreate>) -> Result<()> {
worker_utils_create::handler(ctx)
}

pub fn worker_update(ctx: Context<WorkerUpdate>, settings: WorkerSettings) -> Result<()> {
worker_update::handler(ctx, settings)
}
Expand Down
15 changes: 8 additions & 7 deletions programs/network/src/state/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use anchor_lang::prelude::*;
use crate::constants::SEED_FEE;

/// Escrows the lamport balance owed to a particular worker.
#[account(zero_copy)]
#[account]
#[derive(Debug, InitSpace)]
pub struct Fee {
/// The number of lamports that are distributable for this epoch period.
pub distributable_balance: u64,
/// The worker who received the fees.
pub worker: Pubkey,
pub bump: u8,
}

impl Fee {
Expand All @@ -27,14 +28,14 @@ impl Fee {
/// Trait for reading and writing to a fee account.
pub trait FeeAccount {
/// Initialize the account to hold fee object.
fn init(&mut self, worker: Pubkey) -> Result<()>;
fn init(&mut self, worker: Pubkey, bump: u8) -> Result<()>;
}

impl FeeAccount for AccountLoader<'_, Fee> {
fn init(&mut self, worker: Pubkey) -> Result<()> {
let mut fee = self.load_init()?;
fee.distributable_balance = 0;
fee.worker = worker;
impl FeeAccount for Account<'_, Fee> {
fn init(&mut self, worker: Pubkey, bump: u8) -> Result<()> {
self.distributable_balance = 0;
self.worker = worker;
self.bump = bump;
Ok(())
}
}
Loading

0 comments on commit 05f2682

Please sign in to comment.