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

feat: displaying transaction fee #333

Merged
merged 19 commits into from
May 7, 2024
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,30 @@ pub fn handler_invalid_tx_error(
}
}

fn get_near_price() -> color_eyre::eyre::Result<Option<f64>> {
for _ in 0..10 {
let response = reqwest::blocking::get(
"https://api.coingecko.com/api/v3/simple/price?ids=near&vs_currencies=usd",
)?;

let body = response.text()?;
let parsed_body: serde_json::Value = serde_json::from_str(&body)?;
frolvanya marked this conversation as resolved.
Show resolved Hide resolved

let price = parsed_body["near"]["usd"].as_f64();

if let Some(price) = price {
return Ok(Some(price));
}
}

Ok(None)
}

fn convert_near_to_usd(near: crate::types::near_token::NearToken) -> Option<f64> {
let near_price = get_near_price().ok().flatten();
near_price.map(|price| near.0.as_yoctonear() as f64 * price / 10_f64.powf(24_f64))
frolvanya marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn print_transaction_error(
tx_execution_error: &near_primitives::errors::TxExecutionError,
) -> crate::CliResult {
Expand All @@ -1142,14 +1166,37 @@ pub fn print_transaction_status(
network_config: &crate::config::NetworkConfig,
) -> crate::CliResult {
eprintln!("--- Logs ---------------------------");

let mut total_gas_burnt = transaction_info.transaction_outcome.outcome.gas_burnt;
let mut total_tokens_burnt = transaction_info.transaction_outcome.outcome.tokens_burnt;

for receipt in transaction_info.receipts_outcome.iter() {
total_gas_burnt += receipt.outcome.gas_burnt;
total_tokens_burnt += receipt.outcome.tokens_burnt;

if receipt.outcome.logs.is_empty() {
eprintln!("Logs [{}]: No logs", receipt.outcome.executor_id);
} else {
eprintln!("Logs [{}]:", receipt.outcome.executor_id);
eprintln!(" {}", receipt.outcome.logs.join("\n "));
};
}

let total_tokens_burnt_yoctonear =
crate::types::near_token::NearToken::from_yoctonear(total_tokens_burnt);

eprintln!(
"Gas burned: {}",
crate::common::NearGas::from_gas(total_gas_burnt)
);
eprintln!(
"Tokens burned: {} (approximately ${} USD)",
total_tokens_burnt_yoctonear,
convert_near_to_usd(total_tokens_burnt_yoctonear)
.map(|price| format!("{:.8}", price))
.unwrap_or("N/A".to_string())
);

match &transaction_info.status {
near_primitives::views::FinalExecutionStatus::NotStarted
| near_primitives::views::FinalExecutionStatus::Started => unreachable!(),
Expand Down
Loading