Skip to content

Commit

Permalink
feat: improve advance command (#539)
Browse files Browse the repository at this point in the history
* feat: improve advance command

* go
  • Loading branch information
keyvankhademi authored Sep 19, 2024
1 parent a411e7a commit 45acadf
Showing 1 changed file with 73 additions and 26 deletions.
99 changes: 73 additions & 26 deletions staking/cli/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ use {
get_target_address,
},
},
integrity_pool::state::{
delegation_record::DelegationRecord,
pool::{
PoolConfig,
PoolData,
integrity_pool::{
state::{
delegation_record::DelegationRecord,
pool::{
PoolConfig,
PoolData,
},
},
utils::clock::EPOCH_DURATION,
},
publisher_caps::PublisherCaps,
pythnet_sdk::wire::v1::{
Expand All @@ -49,14 +52,20 @@ use {
instruction::Instruction,
pubkey::Pubkey,
rent::Rent,
signature::Keypair,
signature::{
Keypair,
Signature,
},
signer::Signer,
system_instruction::{
self,
create_account,
},
system_program,
transaction::Transaction,
transaction::{
Transaction,
TransactionError,
},
},
std::{
cmp::min,
Expand Down Expand Up @@ -110,8 +119,8 @@ pub fn init_publisher_caps(rpc_client: &RpcClient, payer: &dyn Signer) -> Pubkey
ComputeBudgetInstruction::set_compute_unit_limit(1_400_000),
],
&[payer, &publisher_caps],
);

)
.unwrap();
publisher_caps.pubkey()
}

Expand All @@ -138,7 +147,7 @@ pub fn write_publisher_caps(
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[payer]);
process_transaction(rpc_client, &[instruction], &[payer]).unwrap();
}

pub fn close_publisher_caps(rpc_client: &RpcClient, payer: &dyn Signer, publisher_caps: Pubkey) {
Expand All @@ -155,7 +164,7 @@ pub fn close_publisher_caps(rpc_client: &RpcClient, payer: &dyn Signer, publishe
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[payer]);
process_transaction(rpc_client, &[instruction], &[payer]).unwrap();
}


Expand Down Expand Up @@ -189,7 +198,8 @@ pub fn verify_publisher_caps(
ComputeBudgetInstruction::set_compute_unit_limit(1_400_000),
],
&[payer],
);
)
.unwrap();
}

pub fn deserialize_accumulator_update_data(
Expand All @@ -207,7 +217,7 @@ pub fn process_transaction(
rpc_client: &RpcClient,
instructions: &[Instruction],
signers: &[&dyn Signer],
) {
) -> Result<Signature, TransactionError> {
let mut transaction = Transaction::new_with_payer(instructions, Some(&signers[0].pubkey()));
transaction.sign(signers, rpc_client.get_latest_blockhash().unwrap());
let transaction_signature_res = rpc_client
Expand All @@ -222,9 +232,11 @@ pub fn process_transaction(
match transaction_signature_res {
Ok(signature) => {
println!("Transaction successful : {signature:?}");
Ok(signature)
}
Err(err) => {
println!("transaction err: {err:?}");
Err(err.get_transaction_error().unwrap())
}
}
}
Expand Down Expand Up @@ -261,7 +273,8 @@ pub fn process_write_encoded_vaa(
rpc_client,
&[create_encoded_vaa, init_encoded_vaa_instruction],
&[payer, &encoded_vaa_keypair],
);
)
.unwrap();

for i in (0..vaa.len()).step_by(1000) {
let chunk = &vaa[i..min(i + 1000, vaa.len())];
Expand Down Expand Up @@ -302,7 +315,8 @@ pub fn process_write_encoded_vaa(
request_compute_units_instruction,
],
&[payer],
);
)
.unwrap();


encoded_vaa_keypair.pubkey()
Expand Down Expand Up @@ -338,7 +352,8 @@ pub fn write_encoded_vaa(
rpc_client,
&[write_encoded_vaa_accounts_instruction],
&[payer],
);
)
.unwrap();
}

pub fn close_encoded_vaa(
Expand All @@ -359,7 +374,7 @@ pub fn close_encoded_vaa(
data: wormhole_core_bridge_solana::instruction::CloseEncodedVaa {}.data(),
};

process_transaction(rpc_client, &[close_encoded_vaa_instruction], &[payer]);
process_transaction(rpc_client, &[close_encoded_vaa_instruction], &[payer]).unwrap();
}

pub fn initialize_reward_custody(rpc_client: &RpcClient, payer: &dyn Signer) {
Expand All @@ -382,7 +397,7 @@ pub fn initialize_reward_custody(rpc_client: &RpcClient, payer: &dyn Signer) {
&spl_token::ID,
);

process_transaction(rpc_client, &[create_ata_ix], &[payer]);
process_transaction(rpc_client, &[create_ata_ix], &[payer]).unwrap();
}

pub fn advance(rpc_client: &RpcClient, payer: &dyn Signer, publisher_caps: Pubkey) {
Expand Down Expand Up @@ -425,7 +440,8 @@ pub fn advance(rpc_client: &RpcClient, payer: &dyn Signer, publisher_caps: Pubke
ComputeBudgetInstruction::set_compute_unit_limit(1_400_000),
],
&[payer],
);
)
.unwrap();
}

pub fn initialize_pool(
Expand Down Expand Up @@ -478,7 +494,14 @@ pub fn initialize_pool(
rpc_client,
&[create_pool_data_acc_ix, initialize_pool_ix],
&[payer, pool_data_keypair],
);
)
.unwrap();
}

pub fn get_current_epoch(rpc_client: &RpcClient) -> u64 {
let slot = rpc_client.get_slot().unwrap();
let blocktime = rpc_client.get_block_time(slot).unwrap();
blocktime as u64 / EPOCH_DURATION
}

pub fn fetch_publisher_caps_and_advance(
Expand All @@ -487,6 +510,30 @@ pub fn fetch_publisher_caps_and_advance(
wormhole: Pubkey,
hermes_url: String,
) {
let pool_config = get_pool_config_address();

let PoolConfig {
pool_data: pool_data_address,
..
} = PoolConfig::try_deserialize(
&mut rpc_client
.get_account_data(&pool_config)
.unwrap()
.as_slice(),
)
.unwrap();

let pool_data = PoolData::try_deserialize(
&mut rpc_client.get_account_data(&pool_data_address).unwrap()[..8 + size_of::<PoolData>()]
.as_ref(),
)
.unwrap();

if pool_data.last_updated_epoch == get_current_epoch(rpc_client) {
println!("Pool data is already updated");
return;
}

let client = Client::new();
let response = client
.get(format!(
Expand Down Expand Up @@ -569,7 +616,7 @@ pub fn update_delegation_fee(rpc_client: &RpcClient, payer: &dyn Signer, delegat
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[payer]);
process_transaction(rpc_client, &[instruction], &[payer]).unwrap();
}

pub fn set_publisher_stake_account(
Expand Down Expand Up @@ -605,7 +652,7 @@ pub fn set_publisher_stake_account(
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[signer]);
process_transaction(rpc_client, &[instruction], &[signer]).unwrap();
}

pub fn create_slash_event(
Expand Down Expand Up @@ -656,7 +703,7 @@ pub fn create_slash_event(
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[signer]);
process_transaction(rpc_client, &[instruction], &[signer]).unwrap();
}

pub fn update_reward_program_authority(
Expand All @@ -682,7 +729,7 @@ pub fn update_reward_program_authority(
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[signer]);
process_transaction(rpc_client, &[instruction], &[signer]).unwrap();
}

pub fn slash(
Expand Down Expand Up @@ -756,7 +803,7 @@ pub fn slash(
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[signer]);
process_transaction(rpc_client, &[instruction], &[signer]).unwrap();
}

pub fn update_y(rpc_client: &RpcClient, signer: &dyn Signer, y: u64) {
Expand All @@ -776,5 +823,5 @@ pub fn update_y(rpc_client: &RpcClient, signer: &dyn Signer, y: u64) {
data: instruction_data.data(),
};

process_transaction(rpc_client, &[instruction], &[signer]);
process_transaction(rpc_client, &[instruction], &[signer]).unwrap();
}

0 comments on commit 45acadf

Please sign in to comment.