-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add approve-all command to batch approvals (#51)
* improvements on logging and code quality * feat: add approve-all command to batch approvals
- Loading branch information
Showing
13 changed files
with
98 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use clap::{ArgMatches, Command}; | ||
|
||
pub fn generate() -> Command { | ||
Command::new("approve-all") | ||
.about("Approve all configured token spending (needed to swap tokens)") | ||
.arg( | ||
clap::arg!(-w --"wallet" <WALLET_NAME> "Wallet id from config file") | ||
.required(true), | ||
) | ||
.arg( | ||
clap::arg!(-n --"network" <bsc> "Network to run all approvals") | ||
.required(true), | ||
) | ||
.arg( | ||
clap::arg!(-a --"amount" <VALUE> "Amount to allow spending: default is the current balance") | ||
.required(false) | ||
.value_parser(clap::value_parser!(f64)), | ||
) | ||
} | ||
|
||
#[tracing::instrument(name = "approve_all call command", level = "debug")] | ||
pub async fn call_sub_commands(args: &ArgMatches) -> Result<(), anyhow::Error> { | ||
super::run(args).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::{cmd::helpers, config::Config}; | ||
use clap::ArgMatches; | ||
|
||
pub mod cmd; | ||
|
||
#[tracing::instrument(name = "run approve-all", skip(args))] | ||
async fn run(args: &ArgMatches) -> Result<(), anyhow::Error> { | ||
let wallet = helpers::get_wallet(args)?; | ||
let network = helpers::get_network(args)?; | ||
let config = Config::global(); | ||
|
||
let assets = config.assets.assets_by_network(network)?; | ||
let exchanges = network.get_exchanges(); | ||
|
||
for asset in assets { | ||
let asset_decimals = asset.decimals().await.unwrap(); | ||
let amount = helpers::get_amount(args, asset_decimals).unwrap_or(asset.balance_of(wallet.address()).await?); | ||
tracing::debug!("amount: {:?}", amount); | ||
|
||
for exchange in exchanges.iter() { | ||
let allowance_amount = asset.allowance(wallet.address(), exchange.as_router_address().unwrap()).await; | ||
if allowance_amount >= amount { | ||
tracing::info!("current amount allowed is greater or equal --amount, to {} on {} for {} with spender {}", asset.name(), exchange.name(), amount, wallet.address()); | ||
continue; | ||
} | ||
|
||
tracing::info!("running approve_spender to {} on {} for {} with spender {}", asset.name(), exchange.name(), amount, wallet.address()); | ||
asset | ||
.approve_spender(wallet, exchange.as_router_address().unwrap(), amount) | ||
.await | ||
.unwrap(); | ||
|
||
let remaning = asset | ||
.allowance(wallet.address(), exchange.as_router_address().unwrap()) | ||
.await; | ||
tracing::info!( | ||
"approved_spender allowance remaning on {}, to spend: {:?}, asset_decimals: {}", | ||
asset.name(), | ||
remaning, | ||
asset_decimals | ||
); | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +0,0 @@ | ||
use crate::utils::{self, password::encrypt_private_key_to_base64}; | ||
use clap::{ArgMatches, Command}; | ||
|
||
pub const COMMAND: &str = "enc"; | ||
|
||
pub fn generate_cmd<'a>() -> Command { | ||
Command::new(COMMAND).about("Encrypt data with a password") | ||
} | ||
|
||
pub async fn call_sub_commands(_: &ArgMatches) { | ||
let password = utils::password::prompt_password("Type a password: ").unwrap_or_else(|e| { | ||
tracing::error!(error = %e); | ||
panic!() | ||
}); | ||
|
||
let private_key = utils::password::prompt_password("Paste the wallet private key: ") | ||
.unwrap_or_else(|e| { | ||
tracing::error!(error = %e); | ||
panic!() | ||
}); | ||
|
||
let base64 = encrypt_private_key_to_base64(password, private_key); | ||
|
||
println!("Encrypted key as base64: {}", base64); | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters