Skip to content

Commit

Permalink
add reclaim rent workflow (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
segfaultdoc committed Mar 28, 2023
1 parent 2a789f4 commit ddfb6d9
Show file tree
Hide file tree
Showing 11 changed files with 583 additions and 177 deletions.
462 changes: 315 additions & 147 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion tip-distributor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description = "Collection of binaries used to distribute MEV rewards to delegato

[dependencies]
anchor-lang = { path = "../anchor/lang" }
clap = { version = "3.2.5", features = ["derive", "env"] }
clap = { version = "4.1.11", features = ["derive", "env"] }
env_logger = "0.9.0"
futures = "0.3.21"
im = "15.1.0"
Expand Down Expand Up @@ -44,3 +44,7 @@ path = "src/bin/merkle-root-uploader.rs"
[[bin]]
name = "solana-claim-mev-tips"
path = "src/bin/claim-mev-tips.rs"

[[bin]]
name = "solana-reclaim-rent"
path = "src/bin/reclaim-rent.rs"
10 changes: 5 additions & 5 deletions tip-distributor/src/bin/claim-mev-tips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ use {
};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to JSON file containing the [GeneratedMerkleTreeCollection] object.
#[clap(long, env)]
#[arg(long, env)]
merkle_trees_path: PathBuf,

/// RPC to send transactions through
#[clap(long, env)]
#[arg(long, env)]
rpc_url: String,

/// Tip distribution program ID
#[clap(long, env)]
#[arg(long, env)]
tip_distribution_program_id: String,

/// Path to keypair
#[clap(long, env)]
#[arg(long, env)]
keypair_path: PathBuf,
}

Expand Down
6 changes: 3 additions & 3 deletions tip-distributor/src/bin/merkle-root-generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use {
};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to JSON file containing the [StakeMetaCollection] object.
#[clap(long, env)]
#[arg(long, env)]
stake_meta_coll_path: PathBuf,

/// Path to JSON file to get populated with tree node data.
#[clap(long, env)]
#[arg(long, env)]
out_path: PathBuf,
}

Expand Down
10 changes: 5 additions & 5 deletions tip-distributor/src/bin/merkle-root-uploader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ use {
};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to JSON file containing the [StakeMetaCollection] object.
#[clap(long, env)]
#[arg(long, env)]
merkle_root_path: PathBuf,

/// The path to the keypair used to sign and pay for the `upload_merkle_root` transactions.
#[clap(long, env)]
#[arg(long, env)]
keypair_path: PathBuf,

/// The RPC to send transactions to.
#[clap(long, env)]
#[arg(long, env)]
rpc_url: String,

/// Tip distribution program ID
#[clap(long, env)]
#[arg(long, env)]
tip_distribution_program_id: String,
}

Expand Down
59 changes: 59 additions & 0 deletions tip-distributor/src/bin/reclaim-rent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Reclaims rent from TDAs and Claim Status accounts.
use {
clap::Parser,
log::*,
solana_client::nonblocking::rpc_client::RpcClient,
solana_sdk::{
commitment_config::CommitmentConfig, pubkey::Pubkey, signature::read_keypair_file,
},
solana_tip_distributor::reclaim_rent_workflow::reclaim_rent,
std::{path::PathBuf, str::FromStr, time::Duration},
tokio::runtime::Runtime,
};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// RPC to send transactions through.
/// NOTE: This script uses getProgramAccounts, make sure you have added an account index
/// for the tip_distribution_program_id on the RPC node.
#[arg(long, env)]
rpc_url: String,

/// Tip distribution program ID.
#[arg(long, env, value_parser = Pubkey::from_str)]
tip_distribution_program_id: Pubkey,

/// The keypair signing and paying for transactions.
#[arg(long, env)]
keypair_path: PathBuf,

/// Specifies whether to reclaim rent on behalf of validators from respective TDAs.
#[arg(long, env)]
should_reclaim_tdas: bool,
}

fn main() {
env_logger::init();

info!("Starting to claim mev tips...");
let args: Args = Args::parse();

let runtime = Runtime::new().unwrap();
if let Err(e) = runtime.block_on(reclaim_rent(
RpcClient::new_with_timeout_and_commitment(
args.rpc_url,
// High timeout b/c of get_program_accounts call
Duration::from_secs(60),
CommitmentConfig::confirmed(),
),
args.tip_distribution_program_id,
read_keypair_file(&args.keypair_path).expect("read keypair file"),
args.should_reclaim_tdas,
)) {
panic!("error reclaiming rent: {e:?}");
}

info!("done reclaiming all rent",);
}
12 changes: 6 additions & 6 deletions tip-distributor/src/bin/stake-meta-generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ use {
};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Ledger path, where you created the snapshot.
#[clap(long, env, parse(try_from_str = Args::ledger_path_parser))]
#[arg(long, env, value_parser = Args::ledger_path_parser)]
ledger_path: PathBuf,

/// The tip-distribution program id.
#[clap(long, env)]
#[arg(long, env)]
tip_distribution_program_id: Pubkey,

/// The tip-payment program id.
#[clap(long, env)]
#[arg(long, env)]
tip_payment_program_id: Pubkey,

/// Path to JSON file populated with the [StakeMetaCollection] object.
#[clap(long, env)]
#[arg(long, env)]
out_path: String,

/// The expected snapshot slot.
#[clap(long, env)]
#[arg(long, env)]
snapshot_slot: Slot,
}

Expand Down
5 changes: 4 additions & 1 deletion tip-distributor/src/claim_mev_workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ pub fn claim_mev_tips(

info!("Sending {} tip claim transactions. {} tried sending zero lamports, {} would be below minimum rent",
&transactions.len(), zero_lamports_count, below_min_rent_count);
send_transactions_with_retry(&rpc_client, &transactions, MAX_RETRY_DURATION).await;
let num_failed_txs = send_transactions_with_retry(&rpc_client, &transactions, MAX_RETRY_DURATION).await;
if num_failed_txs != 0 {
panic!("failed to send {num_failed_txs} transactions");
}
});

Ok(())
Expand Down
11 changes: 3 additions & 8 deletions tip-distributor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod claim_mev_workflow;
pub mod merkle_root_generator_workflow;
pub mod merkle_root_upload_workflow;
pub mod reclaim_rent_workflow;
pub mod stake_meta_generator_workflow;

use {
Expand Down Expand Up @@ -411,7 +412,7 @@ pub async fn send_transactions_with_retry(
rpc_client: &RpcClient,
transactions: &[Transaction],
max_send_duration: Duration,
) {
) -> usize {
let mut transactions_to_send: HashMap<Signature, Transaction> = transactions
.iter()
.map(|tx| (tx.signatures[0], tx.clone()))
Expand Down Expand Up @@ -459,13 +460,7 @@ pub async fn send_transactions_with_retry(
}
}

if !transactions_to_send.is_empty() {
panic!(
"failed to send {} of {} transactions",
transactions_to_send.len(),
transactions.len()
);
}
transactions_to_send.len()
}

mod pubkey_string_conversion {
Expand Down
5 changes: 4 additions & 1 deletion tip-distributor/src/merkle_root_upload_workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ pub fn upload_merkle_root(
)
})
.collect();
send_transactions_with_retry(&rpc_client, &transactions, MAX_RETRY_DURATION).await;
let num_failed_txs = send_transactions_with_retry(&rpc_client, &transactions, MAX_RETRY_DURATION).await;
if num_failed_txs != 0 {
panic!("failed to send {num_failed_txs} transactions");
}
});

Ok(())
Expand Down
Loading

0 comments on commit ddfb6d9

Please sign in to comment.