Skip to content

Commit

Permalink
Fix balances command (solana-labs#17)
Browse files Browse the repository at this point in the history
* Fix balances command

* Fix readme
  • Loading branch information
garious authored May 5, 2020
1 parent e766d88 commit a737e9e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
6 changes: 3 additions & 3 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> --input_csv <BIDS_CSV> <TRANSACTION_LOG> --fee-payer <KEYPAIR>
solana-tokens distribute-tokens --from <KEYPAIR> --dollars-per-sol <NUMBER> --input-csv <BIDS_CSV> <TRANSACTION_LOG> --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> --input_csv <BIDS_CSV> <TRANSACTION_LOG> --fee-payer <KEYPAIR>
solana-tokens distribute-tokens --from <KEYPAIR> --dollars-per-sol <NUMBER> --input-csv <BIDS_CSV> <TRANSACTION_LOG> --fee-payer <KEYPAIR>
```

Example output:
Expand Down Expand Up @@ -60,7 +60,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 --input_csv <BIDS_CSV> <TRANSACTION_LOG>
solana-tokens distribute-tokens --dollars-per-sol <NUMBER> --dry-run --input-csv <BIDS_CSV> <TRANSACTION_LOG>
```

Example bids.csv:
Expand Down
13 changes: 9 additions & 4 deletions src/arg_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
.required(true)
.takes_value(true)
.value_name("NUMBER")
.help("Dollars per SOL, if input CSV contains bids")
.help("Dollars per SOL, if input CSV contains bids"),
)
.arg(
Arg::with_name("dry_run")
Expand Down Expand Up @@ -155,16 +155,20 @@ where
.about("Balance of each account")
.arg(
Arg::with_name("input_csv")
.long("input_csv")
.long("input-csv")
.required(true)
.takes_value(true)
.value_name("FILE")
.help("Bids CSV file"),
)
.arg(
Arg::with_name("from_bids")
.long("from-bids")
.help("Input CSV contains bids in dollars, not allocations in SOL"),
)
.arg(
Arg::with_name("dollars_per_sol")
.long("dollars-per-sol")
.required(true)
.takes_value(true)
.value_name("NUMBER")
.help("Dollars per SOL"),
Expand Down Expand Up @@ -201,7 +205,8 @@ fn parse_distribute_stake_args(matches: &ArgMatches<'_>) -> DistributeStakeArgs<
fn parse_balances_args(matches: &ArgMatches<'_>) -> BalancesArgs {
BalancesArgs {
input_csv: value_t_or_exit!(matches, "input_csv", String),
dollars_per_sol: value_t_or_exit!(matches, "dollars_per_sol", f64),
from_bids: matches.is_present("from_bids"),
dollars_per_sol: value_t!(matches, "dollars_per_sol", f64).ok(),
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub struct DistributeStakeArgs<P, K> {

pub struct BalancesArgs {
pub input_csv: String,
pub dollars_per_sol: f64,
pub from_bids: bool,
pub dollars_per_sol: Option<f64>,
}

pub enum Command<P, K> {
Expand Down
38 changes: 19 additions & 19 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,27 +273,30 @@ fn set_transaction_info(
}

fn read_allocations(
args: &DistributeTokensArgs<Box<dyn Signer>>
input_csv: &str,
from_bids: bool,
dollars_per_sol: Option<f64>,
) -> Vec<Allocation> {
let rdr = ReaderBuilder::new()
.trim(Trim::All)
.from_path(&args.input_csv);
if args.from_bids {
let rdr = ReaderBuilder::new().trim(Trim::All).from_path(input_csv);
if from_bids {
let bids: Vec<Bid> = rdr.unwrap().deserialize().map(|bid| bid.unwrap()).collect();
bids
.into_iter()
.map(|bid| create_allocation(&bid, args.dollars_per_sol.unwrap()))
bids.into_iter()
.map(|bid| create_allocation(&bid, dollars_per_sol.unwrap()))
.collect()
} else {
rdr.unwrap().deserialize().map(|entry| entry.unwrap()).collect()
rdr.unwrap()
.deserialize()
.map(|entry| entry.unwrap())
.collect()
}
}

pub fn process_distribute_tokens<T: Client>(
client: &ThinClient<T>,
args: &DistributeTokensArgs<Box<dyn Signer>>,
) -> Result<(), Error> {
let mut allocations: Vec<Allocation> = read_allocations(&args);
let mut allocations: Vec<Allocation> =
read_allocations(&args.input_csv, args.from_bids, args.dollars_per_sol);

let starting_total_tokens: f64 = allocations.iter().map(|x| x.amount).sum();
println!(
Expand Down Expand Up @@ -423,14 +426,8 @@ pub fn process_balances<T: Client>(
client: &ThinClient<T>,
args: &BalancesArgs,
) -> Result<(), csv::Error> {
let mut rdr = ReaderBuilder::new()
.trim(Trim::All)
.from_path(&args.input_csv)?;
let bids: Vec<Bid> = rdr.deserialize().map(|bid| bid.unwrap()).collect();
let allocations: Vec<Allocation> = bids
.into_iter()
.map(|bid| create_allocation(&bid, args.dollars_per_sol))
.collect();
let allocations: Vec<Allocation> =
read_allocations(&args.input_csv, args.from_bids, args.dollars_per_sol);
let allocations = merge_allocations(&allocations);

println!(
Expand Down Expand Up @@ -521,7 +518,10 @@ pub fn test_process_distribute_bids_with_client<C: Client>(client: C, sender_key
);
}

pub fn test_process_distribute_allocations_with_client<C: Client>(client: C, sender_keypair: Keypair) {
pub fn test_process_distribute_allocations_with_client<C: Client>(
client: C,
sender_keypair: Keypair,
) {
let thin_client = ThinClient(client);
let fee_payer = Keypair::new();
thin_client
Expand Down

0 comments on commit a737e9e

Please sign in to comment.