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

[tools] Improve airdrop tools #3532

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Changes from all commits
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
58 changes: 55 additions & 3 deletions cmd/airdrop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,77 @@ async fn main() -> Result<()> {
.try_into()?;
let is_stc = stc_type_tag().eq(&TypeTag::Struct(token_type.clone()));

let mut total_amount = 0u128;
let airdrop_infos: Vec<AirdropInfo> = {
let mut csv_reader = csv::ReaderBuilder::default()
.has_headers(false)
.from_path(airdrop_file.as_path())?;
let mut leafs = Vec::with_capacity(4096);
for record in csv_reader.deserialize() {
let data: AirdropInfo = record?;
for (idx, record) in csv_reader.records().enumerate() {
let record = record?;
if record.iter().all(|part| part.trim().is_empty()) {
//just skip empty line
continue;
}
match record.len().cmp(&2) {
std::cmp::Ordering::Equal => {}
std::cmp::Ordering::Greater => {
println!(
"[WARN] invalid record: line {}, {:?}, ignore extra field.",
idx, record
);
}
std::cmp::Ordering::Less => {
println!("[WARN] invalid record: line {}, {:?}, skip.", idx, record);
continue;
}
}
let address = match AccountAddress::from_str(record[0].trim()) {
Ok(address) => address,
Err(err) => {
if idx == 0 {
println!("[INFO] skip header line: {}", record.as_slice());
} else {
println!(
"[WARN] invalid record: line {}, {:?}, skip. {}",
idx, record, err
);
}
continue;
}
};
let amount = match u128::from_str(record[1].trim()) {
Ok(amount) => amount,
Err(err) => {
println!(
"[WARN] invalid record: line {}, {:?}, skip. {}",
idx, record, err
);
continue;
}
};
let data = AirdropInfo { address, amount };
if !is_stc && !is_accept_token(data.address, token_type.clone(), &state_client).await? {
println!(
"{} does not accepted the token {}, skip.",
data.address, token_type
);
continue;
}
total_amount += data.amount;
leafs.push(data);
}
leafs
};

println!(
"airdrop {} token {} to {} addresses, total amount: {}",
token_type,
batch_size,
airdrop_infos.len(),
total_amount
);

let private_key: AccountPrivateKey = {
let pass = rpassword::prompt_password_stdout("Please Input Private Key: ")?;
AccountPrivateKey::from_encoded_string(pass.trim())?
Expand Down Expand Up @@ -239,7 +291,7 @@ async fn main() -> Result<()> {
}
};
if txn_info.status != TransactionStatusView::Executed {
eprintln!(
println!(
"txn {:?} error: {:?}, please resume from user: {}",
txn_hash,
txn_info,
Expand Down