|
| 1 | +use clap::{Parser, Subcommand}; |
| 2 | +use solana_sdk::{instruction::Instruction, pubkey::Pubkey}; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + config::Config, |
| 6 | + types::ChainNameOnAxelar, |
| 7 | + utils::{ |
| 8 | + read_json_file_from_path, write_json_to_file_path, ADDRESS_KEY, CHAINS_KEY, |
| 9 | + CONFIG_ACCOUNT_KEY, CONTRACTS_KEY, GOVERNANCE_ADDRESS_KEY, GOVERNANCE_CHAIN_KEY, |
| 10 | + GOVERNANCE_KEY, UPGRADE_AUTHORITY_KEY, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +#[derive(Subcommand, Debug)] |
| 15 | +pub(crate) enum Commands { |
| 16 | + #[clap(long_about = "Initialize the Gateway program")] |
| 17 | + Init(InitArgs), |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Parser, Debug)] |
| 21 | +pub(crate) struct InitArgs { |
| 22 | + #[clap(short, long)] |
| 23 | + governance_chain: String, |
| 24 | + |
| 25 | + #[clap(short, long)] |
| 26 | + governance_address: String, |
| 27 | + |
| 28 | + #[clap(short, long)] |
| 29 | + minimum_proposal_eta_delay: u32, |
| 30 | + |
| 31 | + #[clap(short, long)] |
| 32 | + operator: Pubkey, |
| 33 | +} |
| 34 | + |
| 35 | +pub(crate) async fn build_instruction( |
| 36 | + fee_payer: &Pubkey, |
| 37 | + command: Commands, |
| 38 | + config: &Config, |
| 39 | +) -> eyre::Result<Instruction> { |
| 40 | + match command { |
| 41 | + Commands::Init(init_args) => init(fee_payer, init_args, config).await, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +async fn init( |
| 46 | + fee_payer: &Pubkey, |
| 47 | + init_args: InitArgs, |
| 48 | + config: &Config, |
| 49 | +) -> eyre::Result<Instruction> { |
| 50 | + let chain_hash = solana_sdk::keccak::hashv(&[init_args.governance_chain.as_bytes()]).0; |
| 51 | + let address_hash = solana_sdk::keccak::hashv(&[init_args.governance_address.as_bytes()]).0; |
| 52 | + let (config_pda, _bump) = axelar_solana_governance::state::GovernanceConfig::pda(); |
| 53 | + |
| 54 | + let governance_config = axelar_solana_governance::state::GovernanceConfig::new( |
| 55 | + chain_hash, |
| 56 | + address_hash, |
| 57 | + init_args.minimum_proposal_eta_delay, |
| 58 | + init_args.operator.to_bytes(), |
| 59 | + ); |
| 60 | + |
| 61 | + let mut chains_info: serde_json::Value = read_json_file_from_path(&config.chains_info_file)?; |
| 62 | + chains_info[CHAINS_KEY][ChainNameOnAxelar::from(config.network_type).0][CONTRACTS_KEY] |
| 63 | + [GOVERNANCE_KEY] = serde_json::json!({ |
| 64 | + ADDRESS_KEY: axelar_solana_gateway::id().to_string(), |
| 65 | + CONFIG_ACCOUNT_KEY: config_pda.to_string(), |
| 66 | + UPGRADE_AUTHORITY_KEY: fee_payer.to_string(), |
| 67 | + GOVERNANCE_ADDRESS_KEY: init_args.governance_address, |
| 68 | + GOVERNANCE_CHAIN_KEY: init_args.governance_chain, |
| 69 | + }); |
| 70 | + |
| 71 | + write_json_to_file_path(&chains_info, &config.chains_info_file)?; |
| 72 | + |
| 73 | + Ok( |
| 74 | + axelar_solana_governance::instructions::builder::IxBuilder::new() |
| 75 | + .initialize_config(fee_payer, &config_pda, governance_config) |
| 76 | + .build(), |
| 77 | + ) |
| 78 | +} |
0 commit comments