equanetwork-cli 0.0.4

The Equa Network command line interface
use anyhow::{anyhow, Result};
use equanetwork::{get_permission_address, NetworkInitialize, NetworkInitializeInstructionArgs};
use solana_signer::Signer;

use crate::{
    config::{Command, Config, NetworkCommand},
    log_context,
    transaction::process_instructions,
};

use super::helpers::{addr, network_pda, parse_network_id, pk, require_signer};

pub fn handle(config: &Config) -> Result<()> {
    let Command::Network {
        command: NetworkCommand::Initialize { network_id },
    } = &config.command
    else {
        unreachable!()
    };

    let network_id = parse_network_id(network_id)?;
    let signer = require_signer(config)?;
    let rpc = config.rpc_client();
    let authority = signer.pubkey();
    let (permission, _) = get_permission_address(&addr(authority));
    let network = network_pda(network_id);

    log_context!(
      action => "network_initialize",
      authority => authority.to_string(),
      network => network.to_string(),
      network_id => network_id,
    );

    let ix = NetworkInitialize {
        payer: addr(authority),
        authority: addr(authority),
        authority_permission: permission,
        network,
        system_program: addr(solana_system_interface::program::ID),
    }
    .instruction(NetworkInitializeInstructionArgs { network_id });

    process_instructions(&rpc, &[signer], &[ix])
        .map_err(|e| anyhow!("{e}; network pda {}", pk(network)))
}