Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: Fix the worker creation instruction #22

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
penalty: Penalty::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));

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
6 changes: 3 additions & 3 deletions plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ async-trait.workspace = true
bincode.workspace = true
chrono = { workspace = true, features = ["alloc"] }
sablier-cron.workspace = true
sablier-network-program.workspace = true
sablier-network-program = { workspace = true, features = ["no-entrypoint"] }
sablier-plugin-utils.workspace = true
sablier-relayer-api.workspace = true
sablier-thread-program.workspace = true
sablier-webhook-program.workspace = true
sablier-thread-program = { workspace = true, features = ["no-entrypoint"] }
sablier-webhook-program = { workspace = true, features = ["no-entrypoint"] }
sablier-utils.workspace = true
log.workspace = true
pyth-sdk-solana.workspace = true
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::*;
60 changes: 11 additions & 49 deletions programs/network/src/instructions/worker_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +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: Box<Account<'info, Fee>>,

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

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

Expand All @@ -50,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 @@ -77,37 +56,20 @@ pub struct WorkerCreate<'info> {
pub token_program: Program<'info, Token>,
}

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

let remaining_accounts = &mut ctx.remaining_accounts.iter();

let signatory = {
let signatory_info = next_account_info(remaining_accounts)?;

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 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(())
}
11 changes: 5 additions & 6 deletions programs/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ pub mod network_program {
worker_claim::handler(ctx, amount)
}

pub fn worker_create<'a, 'b, 'c, 'info>(
ctx: Context<'a, 'b, 'c, 'info, WorkerCreate>,
) -> Result<()>
where
'c: 'info,
{
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
8 changes: 5 additions & 3 deletions programs/network/src/state/fee.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anchor_lang::{prelude::*, AnchorDeserialize};
use anchor_lang::prelude::*;

use crate::constants::SEED_FEE;

Expand All @@ -10,6 +10,7 @@ pub struct Fee {
pub distributable_balance: u64,
/// The worker who received the fees.
pub worker: Pubkey,
pub bump: u8,
}

impl Fee {
Expand All @@ -25,17 +26,18 @@ pub trait FeeAccount {
fn pubkey(&self) -> Pubkey;

/// 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 Account<'_, Fee> {
fn pubkey(&self) -> Pubkey {
Fee::pubkey(self.worker)
}

fn init(&mut self, worker: Pubkey) -> Result<()> {
fn init(&mut self, worker: Pubkey, bump: u8) -> Result<()> {
self.distributable_balance = 0;
self.worker = worker;
self.bump = bump;
Ok(())
}
}
6 changes: 4 additions & 2 deletions programs/network/src/state/penalty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::constants::SEED_PENALTY;
pub struct Penalty {
/// The worker who was penalized.
pub worker: Pubkey,
pub bump: u8,
}

impl Penalty {
Expand All @@ -23,16 +24,17 @@ pub trait PenaltyAccount {
fn pubkey(&self) -> Pubkey;

/// Initialize the account to hold penalty object.
fn init(&mut self, worker: Pubkey) -> Result<()>;
fn init(&mut self, worker: Pubkey, bump: u8) -> Result<()>;
}

impl PenaltyAccount for Account<'_, Penalty> {
fn pubkey(&self) -> Pubkey {
Penalty::pubkey(self.worker)
}

fn init(&mut self, worker: Pubkey) -> Result<()> {
fn init(&mut self, worker: Pubkey, bump: u8) -> Result<()> {
self.worker = worker;
self.bump = bump;
Ok(())
}
}
6 changes: 4 additions & 2 deletions programs/network/src/state/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Registry {
pub total_pools: u64,
pub total_unstakes: u64,
pub total_workers: u64,
pub bump: u8,
}

impl Registry {
Expand All @@ -31,16 +32,17 @@ impl Registry {
*/

pub trait RegistryAccount {
fn init(&mut self) -> Result<()>;
fn init(&mut self, bump: u8) -> Result<()>;

fn hash_nonce(&mut self) -> Result<()>;
}

impl RegistryAccount for Account<'_, Registry> {
fn init(&mut self) -> Result<()> {
fn init(&mut self, bump: u8) -> Result<()> {
self.current_epoch = 0;
self.locked = false;
self.total_workers = 0;
self.bump = bump;
Ok(())
}

Expand Down
Loading