Skip to content
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
1 change: 1 addition & 0 deletions test-cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::Cli;
pub mod create_order;
pub mod initialize;
pub mod settle;
pub mod solver;

/// Shared context threaded through every subcommand.
pub struct Context {
Expand Down
91 changes: 91 additions & 0 deletions test-cli/src/cmd/solver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use anyhow::Context as _;
use clap::{Args as ClapArgs, Parser, Subcommand};
use cow_settlement_client::{
cow_settlement_interface::{pda::state::find_state_pda, Pubkey},
instructions::AddSolver,
};
use solana_sdk::{
signature::{read_keypair_file, Signer},
transaction::Transaction,
};

use crate::helpers::print_summary;

use super::Context;

#[derive(Parser)]
pub struct SolverArgs {
#[command(subcommand)]
command: SolverCommand,
}

#[derive(Subcommand)]
enum SolverCommand {
#[command(about = "Authorize a solver to settle orders")]
Add(AddArgs),
}

#[derive(ClapArgs)]
pub struct AddArgs {
/// Address of the solver to authorize
solver: Pubkey,

/// Path to the manager keypair, which authorizes the change and must sign
/// it (defaults to the payer keypair, which always funds the state PDA's
/// growth)
#[arg(long)]
manager: Option<String>,
}

pub fn run(ctx: Context, args: SolverArgs) -> anyhow::Result<()> {
match args.command {
SolverCommand::Add(args) => add(ctx, args),
}
}

fn add(ctx: Context, args: AddArgs) -> anyhow::Result<()> {
let payer = ctx.payer.pubkey();
// Without `--manager` the payer manages the program, so reuse the keypair
// the context already holds instead of reading a file again.
let from_file = args
.manager
.map(|path| {
read_keypair_file(&path)
.map_err(|e| anyhow::anyhow!("failed to read manager keypair from {path}: {e}"))
})
.transpose()?;
let manager = from_file.as_ref().unwrap_or(&ctx.payer);
let manager_pubkey = manager.pubkey();

let ix = AddSolver {
program_id: ctx.program_id,
manager: manager_pubkey,
payer,
solver: args.solver,
};

let blockhash = ctx
.rpc
.get_latest_blockhash()
.context("failed to fetch blockhash")?;
let tx = Transaction::new_signed_with_payer(
&[ix.into()],
Some(&payer),
&[&ctx.payer, manager],
blockhash,
);
let sig = ctx
.rpc
.send_and_confirm_transaction(&tx)
.context("transaction failed")?;

let (state_pda, _) = find_state_pda(&ctx.program_id);
print_summary(&[
("signature", &sig),
("added solver", &args.solver),
("manager", &manager_pubkey),
("statePda", &state_pda),
]);

Ok(())
}
3 changes: 3 additions & 0 deletions test-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ enum Commands {
Buy(cmd::create_order::BuyOrSellArgs),
#[command(about = "Settle one or more orders")]
Settle(cmd::settle::SettleArgs),
#[command(about = "Manage the program's solvers (e.g. `cow solver add <address>`)")]
Solver(cmd::solver::SolverArgs),
}

fn main() -> anyhow::Result<()> {
Expand All @@ -83,5 +85,6 @@ fn main() -> anyhow::Result<()> {
Commands::Sell(args) => cmd::create_order::run_sell(ctx, args),
Commands::Buy(args) => cmd::create_order::run_buy(ctx, args),
Commands::Settle(args) => cmd::settle::run(ctx, args),
Commands::Solver(args) => cmd::solver::run(ctx, args),
}
}