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

automatically lookup errors for batch action commands #298

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
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
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.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ spl-token = "3.5.0"
structopt = "0.3.26"
thiserror = "1.0.40"
tokio = "1.14.1"
regex = "1.8.3"
once_cell = "1.18.0"

[features]

Expand Down
19 changes: 17 additions & 2 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use anyhow::{anyhow, Result as AnyResult};
use async_trait::async_trait;
use indexmap::IndexMap;
use log::info;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use solana_client::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;

use std::{
collections::HashMap,
fs::{File, OpenOptions},
Expand All @@ -16,7 +19,7 @@ use std::{

use crate::{
constants::NANO_SECONDS_IN_SECOND, errors::ActionError,
limiter::create_rate_limiter_with_capacity, spinner::create_progress_bar,
limiter::create_rate_limiter_with_capacity, spinner::create_progress_bar, utils::find_tm_error,
};

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -60,11 +63,23 @@ impl Cache {
// Clear out old errors.
self.clear();

//Regex to find hex codes in error
static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r" 0x[0-9a-fA-F]+").expect("Failed to create regex"));

for error in errors {
match error {
ActionError::ActionFailed(mint_address, _) => {
// Find hex codes in error message.
let error_message = if let Some(mat) = RE.find(&error.to_string()) {
find_tm_error(&mat.as_str().trim_start().replace("0x", ""))
.unwrap_or_else(|| error.to_string())
} else {
error.to_string()
};

let item = CacheItem {
error: Some(error.to_string()),
error: Some(error_message),
};

self.insert(mint_address.to_string(), item);
Expand Down
6 changes: 6 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ pub fn find_errors(hex_code: &str) -> Vec<FoundError> {
found_errors
}

pub fn find_tm_error(hex_code: &str) -> Option<String> {
let hex_code = hex_code.to_uppercase();

METADATA_ERROR.get(&hex_code).map(|e| e.to_string())
}

pub fn clone_keypair(keypair: &Keypair) -> Keypair {
Keypair::from_bytes(&keypair.to_bytes()).unwrap()
}
Expand Down
Loading