Skip to content

Commit c6f3b4a

Browse files
committed
feat(solana): add governance initialization
Signed-off-by: Guilherme Felipe da Silva <[email protected]>
1 parent 4b32795 commit c6f3b4a

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

solana/cli/src/governance.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

solana/cli/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod error;
55
mod gas_service;
66
mod gateway;
77
mod generate;
8+
mod governance;
89
mod its;
910
mod send;
1011
mod sign;
@@ -148,6 +149,9 @@ enum InstructionSubcommand {
148149

149150
#[clap(long_about = "Commands for InterchainTokenService program", subcommand)]
150151
Its(its::Commands),
152+
153+
#[clap(long_about = "Commands for Governance program", subcommand)]
154+
Governance(governance::Commands),
151155
}
152156

153157
#[derive(Parser, Debug)]
@@ -272,6 +276,9 @@ async fn build_instruction(
272276
InstructionSubcommand::Its(command) => {
273277
its::build_instruction(fee_payer, command, config).await?
274278
}
279+
InstructionSubcommand::Governance(commands) => {
280+
governance::build_instruction(fee_payer, commands, config).await?
281+
}
275282
};
276283

277284
Ok(serializable_ix)

solana/cli/src/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ pub(crate) const UPGRADE_AUTHORITY_KEY: &str = "upgradeAuthority";
2828
pub(crate) const OPERATOR_KEY: &str = "operator";
2929
pub(crate) const MINIMUM_ROTATION_DELAY_KEY: &str = "minimumRotationDelay";
3030
pub(crate) const PREVIOUS_SIGNERS_RETENTION_KEY: &str = "previousSignersRetention";
31+
pub(crate) const GOVERNANCE_KEY: &str = "InterchainGovernance";
32+
pub(crate) const MINIMUM_PROPOSAL_ETA_DELAY_KEY: &str = "minimumTimeDelay";
33+
pub(crate) const GOVERNANCE_CHAIN_KEY: &str = "governanceChain";
34+
pub(crate) const GOVERNANCE_ADDRESS_KEY: &str = "governanceAddress";
3135

3236
pub(crate) fn read_json_file<T: DeserializeOwned>(file: &File) -> Result<T> {
3337
let reader = std::io::BufReader::new(file);

0 commit comments

Comments
 (0)