Skip to content

Commit

Permalink
Make --transaction-db option implicit (solana-labs#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed May 12, 2020
1 parent 0ee429e commit 8d35db1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ chrono = { version = "0.4", features = ["serde"] }
clap = "2.33.0"
console = "0.10.3"
csv = "1.1.3"
dirs = "2.0.2"
indexmap = "1.3.2"
indicatif = "0.14.0"
itertools = "0.9.0"
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ primary_address,bid_amount_dollars
```

```bash
solana-tokens distribute-tokens --from <KEYPAIR> --dollars-per-sol <NUMBER> --from-bids --input-csv <BIDS_CSV> --transaction-db <FILE> --fee-payer <KEYPAIR>
solana-tokens distribute-tokens --from <KEYPAIR> --dollars-per-sol <NUMBER> --from-bids --input-csv <BIDS_CSV> --fee-payer <KEYPAIR>
```

Example transaction log before:
Expand All @@ -31,7 +31,7 @@ Send tokens to the recipients in `<BIDS_CSV>` if the distribution is
not already recordered in the transaction log.

```bash
solana-tokens distribute-tokens --from <KEYPAIR> --dollars-per-sol <NUMBER> --from-bids --input-csv <BIDS_CSV> --transaction-db <FILE> --fee-payer <KEYPAIR>
solana-tokens distribute-tokens --from <KEYPAIR> --dollars-per-sol <NUMBER> --from-bids --input-csv <BIDS_CSV> --fee-payer <KEYPAIR>
```

Example output:
Expand All @@ -46,6 +46,10 @@ UKUcTXgbeTYh65RaVV5gSf6xBHevqHvAXMo3e8Q6np8k 43

Example transaction log after:

```bash
solana-tokens transaction-log --output-path transactions.csv
```

```text
recipient,amount,signature
6Vo87BaDhp4v4GHwVDhw5huhxVF8CyxSXYtkUwVHbbPv,30,1111111111111111111111111111111111111111111111111111111111111111
Expand All @@ -60,7 +64,7 @@ List the differences between a list of expected distributions and the record of
transactions have already been sent.

```bash
solana-tokens distribute-tokens --dollars-per-sol <NUMBER> --dry-run --from-bids --input-csv <BIDS_CSV> --transaction-db <FILE>
solana-tokens distribute-tokens --dollars-per-sol <NUMBER> --dry-run --from-bids --input-csv <BIDS_CSV>
```

Example bids.csv:
Expand Down Expand Up @@ -91,7 +95,6 @@ the new accounts inherit any lockup or custodian settings of the original.
```bash
solana-tokens distribute-stake --stake-account-address <ACCOUNT_ADDRESS> \
--input-csv <ALLOCATIONS_CSV> \
--transaction-db <FILE> \
--stake-authority <KEYPAIR> --withdraw-authority <KEYPAIR> --fee-payer <KEYPAIR>
```

Expand Down
47 changes: 30 additions & 17 deletions src/arg_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ where
SubCommand::with_name("distribute-tokens")
.about("Distribute tokens")
.arg(
Arg::with_name("transaction_db")
.long("transaction-db")
.required(true)
Arg::with_name("campaign_name")
.long("campaign-name")
.takes_value(true)
.value_name("FILE")
.help("Transaction database file"),
.value_name("NAME")
.help("Campaign name for storing transaction data"),
)
.arg(
Arg::with_name("from_bids")
Expand Down Expand Up @@ -91,12 +90,11 @@ where
SubCommand::with_name("distribute-stake")
.about("Distribute stake accounts")
.arg(
Arg::with_name("transaction_db")
.long("transaction-db")
.required(true)
Arg::with_name("campaign_name")
.long("campaign-name")
.takes_value(true)
.value_name("FILE")
.help("Transaction database file"),
.value_name("NAME")
.help("Campaign name for storing transaction data"),
)
.arg(
Arg::with_name("input_csv")
Expand Down Expand Up @@ -193,12 +191,11 @@ where
SubCommand::with_name("transaction-log")
.about("Print the database to a CSV file")
.arg(
Arg::with_name("transaction_db")
.long("transaction-db")
.required(true)
Arg::with_name("campaign_name")
.long("campaign-name")
.takes_value(true)
.value_name("FILE")
.help("Transactions database file"),
.value_name("NAME")
.help("Campaign name for storing transaction data"),
)
.arg(
Arg::with_name("output_path")
Expand All @@ -212,11 +209,27 @@ where
.get_matches_from(args)
}

fn create_db_path(campaign_name: Option<String>) -> String {
let (prefix, hyphen) = if let Some(name) = campaign_name {
(name, "-")
} else {
("".to_string(), "")
};
let path = dirs::home_dir().unwrap();
let filename = format!("{}{}transactions.db", prefix, hyphen);
path.join(".config")
.join("solana-tokens")
.join(filename)
.to_str()
.unwrap()
.to_string()
}

fn parse_distribute_tokens_args(matches: &ArgMatches<'_>) -> DistributeTokensArgs<String, String> {
DistributeTokensArgs {
input_csv: value_t_or_exit!(matches, "input_csv", String),
from_bids: matches.is_present("from_bids"),
transaction_db: value_t_or_exit!(matches, "transaction_db", String),
transaction_db: create_db_path(value_t!(matches, "campaign_name", String).ok()),
dollars_per_sol: value_t!(matches, "dollars_per_sol", f64).ok(),
dry_run: matches.is_present("dry_run"),
sender_keypair: value_t_or_exit!(matches, "sender_keypair", String),
Expand All @@ -235,7 +248,7 @@ fn parse_distribute_stake_args(matches: &ArgMatches<'_>) -> DistributeTokensArgs
DistributeTokensArgs {
input_csv: value_t_or_exit!(matches, "input_csv", String),
from_bids: false,
transaction_db: value_t_or_exit!(matches, "transaction_db", String),
transaction_db: create_db_path(value_t!(matches, "campaign_name", String).ok()),
dollars_per_sol: None,
dry_run: matches.is_present("dry_run"),
sender_keypair: value_t_or_exit!(matches, "sender_keypair", String),
Expand Down
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use solana_cli_config::Config;
use solana_cli_config::CONFIG_FILE;
use solana_client::rpc_client::RpcClient;
use solana_tokens::{
arg_parser::parse_args,
Expand All @@ -8,10 +9,21 @@ use solana_tokens::{
};
use std::env;
use std::error::Error;
use std::path::Path;
use std::process;

fn main() -> Result<(), Box<dyn Error>> {
let command_args = parse_args(env::args_os());
let config = Config::load(&command_args.config_file)?;
let config = if Path::new(&command_args.config_file).exists() {
Config::load(&command_args.config_file)?
} else {
let default_config_file = CONFIG_FILE.as_ref().unwrap();
if command_args.config_file != *default_config_file {
eprintln!("Error: config file not found");
process::exit(1);
}
Config::default()
};
let json_rpc_url = command_args.url.unwrap_or(config.json_rpc_url);
let client = RpcClient::new(json_rpc_url);

Expand Down
17 changes: 11 additions & 6 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use solana_stake_program::{
use solana_transaction_status::TransactionStatus;
use std::{
cmp::{self, Ordering},
io,
fs, io,
path::Path,
thread::sleep,
time::Duration,
Expand Down Expand Up @@ -220,17 +220,22 @@ fn distribute_tokens<T: Client>(
Ok(())
}

fn open_db(path: &str, dry_run: bool) -> Result<PickleDb, pickledb::error::Error> {
fn open_db(path: &str, dry_run: bool) -> Result<PickleDb, Error> {
let policy = if dry_run {
PickleDbDumpPolicy::NeverDump
} else {
PickleDbDumpPolicy::AutoDump
};
if Path::new(path).exists() {
PickleDb::load_yaml(path, policy)
let path = Path::new(path);
let db = if path.exists() {
PickleDb::load_yaml(path, policy)?
} else {
Ok(PickleDb::new_yaml(path, policy))
}
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
PickleDb::new_yaml(path, policy)
};
Ok(db)
}

fn compare_transaction_infos(a: &TransactionInfo, b: &TransactionInfo) -> Ordering {
Expand Down

0 comments on commit 8d35db1

Please sign in to comment.