Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add close publisher caps #546

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion staking/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub enum Action {
)]
hermes_url: String,

#[clap(long, default_value = "3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5")]
#[clap(long, default_value = "HDwcJBJXjL9FpJ7UBsYBtaDjsBUhuLCUYoz3zr8SWWaQ")]
wormhole: Pubkey,
},
InitializePoolRewardCustody {},
Expand Down Expand Up @@ -110,6 +110,13 @@ pub enum Action {
publisher_caps: Pubkey,
},
SaveStakeAccountsSnapshot {},
ClaimRewards {
#[clap(long, help = "Minimum staked tokens")]
min_staked: u64,
#[clap(long, help = "Minimum reward tokens per publisher")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment saying this is with full decimals?

min_reward: u64,
},
CloseAllPublisherCaps {},
}

pub enum SignerSource {
Expand Down
201 changes: 197 additions & 4 deletions staking/cli/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use {
TokenAccount,
},
},
base64::Engine,
base64::{
prelude::BASE64_STANDARD,
Engine,
},
integration_tests::{
integrity_pool::pda::{
get_delegation_record_address,
Expand Down Expand Up @@ -86,9 +89,11 @@ use {
global_config::GlobalConfig,
max_voter_weight_record::MAX_VOTER_WEIGHT,
positions::{
DynamicPositionArray,
DynamicPositionArrayAccount,
PositionData,
PositionState,
Target,
},
stake_account::StakeAccountMetadataV2,
},
Expand All @@ -105,9 +110,12 @@ use {
},
mem::size_of,
},
wormhole_core_bridge_solana::sdk::{
WriteEncodedVaaArgs,
VAA_START,
wormhole_core_bridge_solana::{
sdk::{
WriteEncodedVaaArgs,
VAA_START,
},
state::EncodedVaa,
},
wormhole_sdk::vaa::{
Body,
Expand Down Expand Up @@ -870,6 +878,191 @@ pub fn update_y(rpc_client: &RpcClient, signer: &dyn Signer, y: u64) {
process_transaction(rpc_client, &[instruction], &[signer]).unwrap();
}

pub fn close_all_publisher_caps(rpc_client: &RpcClient, signer: &dyn Signer) {
let mut data = EncodedVaa::DISCRIMINATOR.to_vec();
data.extend_from_slice(&[1]);
data.extend_from_slice(&signer.pubkey().to_bytes());

rpc_client
.get_program_accounts_with_config(
&publisher_caps::ID,
RpcProgramAccountsConfig {
filters: Some(vec![RpcFilterType::Memcmp(Memcmp::new(
0,
MemcmpEncodedBytes::Bytes(PublisherCaps::DISCRIMINATOR.to_vec()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should filter on data here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N/A anymore

))]),
account_config: RpcAccountInfoConfig {
encoding: Some(UiAccountEncoding::Base64Zstd),
data_slice: None,
commitment: None,
min_context_slot: None,
},
with_context: None,
},
)
.unwrap()
.into_iter()
.for_each(|(pubkey, _account)| close_publisher_caps(rpc_client, signer, pubkey));
}

pub fn advance_delegation_record(
rpc_client: &RpcClient,
signer: &dyn Signer,
positions: &Pubkey,
min_reward: u64,
) {
let pool_config = get_pool_config_address();

let PoolConfig {
pool_data: pool_data_address,
pyth_token_mint,
..
} = 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();

pool_data
.publishers
.iter()
.enumerate()
.for_each(|(publisher_index, publisher)| {
if *publisher == Pubkey::default() {
return;
}
let publisher_stake_account_positions =
if pool_data.publisher_stake_accounts[publisher_index] == Pubkey::default() {
None
} else {
Some(pool_data.publisher_stake_accounts[publisher_index])
};

let publisher_stake_account_custody =
publisher_stake_account_positions.map(get_stake_account_custody_address);

let accounts = integrity_pool::accounts::AdvanceDelegationRecord {
delegation_record: get_delegation_record_address(*publisher, *positions),
payer: signer.pubkey(),
pool_config,
pool_data: pool_data_address,
pool_reward_custody: get_pool_reward_custody_address(pyth_token_mint),
publisher: *publisher,
publisher_stake_account_positions,
publisher_stake_account_custody,
stake_account_positions: *positions,
stake_account_custody: get_stake_account_custody_address(*positions),
system_program: system_program::ID,
token_program: spl_token::ID,
};

let data = integrity_pool::instruction::AdvanceDelegationRecord {};

let ix = Instruction {
program_id: integrity_pool::ID,
accounts: accounts.to_account_metas(None),
data: data.data(),
};


let mut transaction =
Transaction::new_with_payer(&[ix.clone()], Some(&signer.pubkey()));
transaction.sign(
&[signer],
rpc_client
.get_latest_blockhash_with_commitment(CommitmentConfig::finalized())
.unwrap()
.0,
);
let res = rpc_client.simulate_transaction(&transaction).unwrap();
let reward_amount: u64 = u64::from_le_bytes(
BASE64_STANDARD
.decode(res.value.return_data.unwrap().data.0)
.unwrap()[..8]
.try_into()
.unwrap(),
);

if reward_amount < min_reward {
return;
}

println!(
"Advance delegation record for pubkey: {:?} publisher: {:?} with reward: {:?}",
positions.to_string(),
publisher,
reward_amount,
);

process_transaction(rpc_client, &[ix], &[signer]).unwrap();
keyvankhademi marked this conversation as resolved.
Show resolved Hide resolved
});
}

pub fn claim_rewards(
rpc_client: &RpcClient,
signer: &dyn Signer,
min_staked: u64,
min_reward: u64,
) {
let mut data: Vec<DynamicPositionArrayAccount> = rpc_client
.get_program_accounts_with_config(
&staking::ID,
RpcProgramAccountsConfig {
filters: Some(vec![RpcFilterType::Memcmp(Memcmp::new(
0,
MemcmpEncodedBytes::Bytes(PositionData::discriminator().to_vec()),
))]),
account_config: RpcAccountInfoConfig {
encoding: Some(UiAccountEncoding::Base64Zstd),
data_slice: None,
commitment: None,
min_context_slot: None,
},
with_context: None,
},
)
.unwrap()
.into_iter()
.map(|(pubkey, account)| DynamicPositionArrayAccount {
key: pubkey,
lamports: account.lamports,
data: account.data.clone(),
})
.collect::<Vec<_>>();

let current_epoch = get_current_epoch(rpc_client);

let data: Vec<DynamicPositionArray> = data
.iter_mut()
.filter_map(|positions| {
let acc = positions.to_dynamic_position_array();
let valid = acc
.get_target_exposure(&Target::IntegrityPool, current_epoch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about using get_target_exposure here. get_target_exposure ignores UNLOCKED positions but these can have unclaimed rewards

.unwrap()
>= min_staked;
if valid {
Some(acc)
} else {
None
}
})
.collect();


data.iter().enumerate().for_each(|(i, positions)| {
println!("Claiming rewards for account {} / {}", i, data.len());
advance_delegation_record(rpc_client, signer, positions.acc_info.key, min_reward);
});
}

pub fn save_stake_accounts_snapshot(rpc_client: &RpcClient) {
let data: Vec<(Pubkey, DynamicPositionArrayAccount, Pubkey, Pubkey, Pubkey)> = rpc_client
.get_program_accounts_with_config(
Expand Down
11 changes: 11 additions & 0 deletions staking/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use {
Cli,
},
instructions::{
claim_rewards,
close_all_publisher_caps,
close_publisher_caps,
create_slash_event,
fetch_publisher_caps_and_advance,
Expand Down Expand Up @@ -97,5 +99,14 @@ fn main() {
Action::SaveStakeAccountsSnapshot {} => {
save_stake_accounts_snapshot(&rpc_client);
}
Action::ClaimRewards {
min_staked,
min_reward,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having two parameters here. I feel like we should be disciplined and:

  • either claim all rewards for accounts with min_staked
  • or claim all rewards s.t. reward > min_reward

} => {
claim_rewards(&rpc_client, keypair.as_ref(), min_staked, min_reward);
}
Action::CloseAllPublisherCaps {} => {
close_all_publisher_caps(&rpc_client, keypair.as_ref());
}
}
}
Loading