Skip to content

Commit

Permalink
Merge pull request #1832 from multiversx/account-tool-bug
Browse files Browse the repository at this point in the history
account tool bugfix
  • Loading branch information
andrei-marinica authored Oct 25, 2024
2 parents f49da24 + 0080621 commit f88c38e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 79 deletions.
64 changes: 32 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions framework/meta/src/cli/cli_args_standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,6 @@ pub struct AccountArgs {
#[clap(global = true)]
pub api: Option<String>,

/// Provide if the API is a chain simulator or not
#[arg(
long = "chain-simulator",
default_value = "false",
verbatim_doc_comment
)]
pub chain_simulator: Option<bool>,

/// Provide the address you want to retrieve data from
#[arg(long = "address", verbatim_doc_comment)]
pub address: String,
Expand Down
2 changes: 0 additions & 2 deletions framework/meta/src/cmd/retrieve_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use crate::cli::AccountArgs;
/// Interprets arguments and call the account tool from `multiversx_sc_snippets`.
pub async fn retrieve_address(args: &AccountArgs) {
let api_string = args.api.clone().expect("API needs to be specified");
let use_chain_simulator = args.chain_simulator.unwrap_or_default();
account_tool::print_account_as_scenario_set_state(
GatewayHttpProxy::new(api_string),
use_chain_simulator,
args.address.to_string(),
)
.await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ pub struct BytesKey {
pub original: String,
}

impl BytesKey {
pub fn from_hex(hex_value: &str) -> Self {
Self {
value: hex::decode(hex_value).expect("could not decode hex value"),
original: format!("0x{hex_value}"),
}
}
}

impl From<Vec<u8>> for BytesKey {
fn from(v: Vec<u8>) -> Self {
BytesKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ impl BytesValue {
original: ValueSubTree::Str(String::default()),
}
}

pub fn from_hex(hex_value: &str) -> Self {
Self {
value: hex::decode(hex_value).expect("could not decode hex value"),
original: ValueSubTree::Str(format!("0x{hex_value}")),
}
}
}

impl InterpretableFrom<ValueSubTree> for BytesValue {
Expand Down
54 changes: 23 additions & 31 deletions framework/snippets-base/src/account_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ use std::collections::{BTreeMap, HashMap};
/// then formats it as a scenario set state step.
pub async fn print_account_as_scenario_set_state<GatewayProxy: GatewayAsyncService>(
gateway_proxy: GatewayProxy,
use_chain_simulator: bool,
address_bech32_string: String,
) {
// let gateway_proxy = GatewayHttpProxy::new(api_string);
let address = Bech32Address::from_bech32_string(address_bech32_string);
let set_state =
retrieve_account_as_scenario_set_state(&gateway_proxy, use_chain_simulator, &address).await;
let set_state = retrieve_account_as_scenario_set_state(&gateway_proxy, &address).await;
let scenario = build_scenario(set_state);
println!("{}", scenario.into_raw().to_json_string());
}
Expand All @@ -39,36 +36,31 @@ fn build_scenario(set_state: SetStateStep) -> Scenario {

pub async fn retrieve_account_as_scenario_set_state<GatewayProxy: GatewayAsyncService>(
api: &GatewayProxy,
use_chain_simulator: bool,
bech32_address: &Bech32Address,
) -> SetStateStep {
let address = bech32_address.as_address();
let sdk_account = api.request(GetAccountRequest::new(address)).await.unwrap();

let (account_esdt, account_esdt_roles, account_storage) = if use_chain_simulator {
(HashMap::new(), HashMap::new(), HashMap::new())
} else {
let account_esdt = api
.request(GetAccountEsdtTokensRequest::new(address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve ESDT tokens for address {bech32_address}: {err}")
});
let account_esdt_roles = api
.request(GetAccountEsdtRolesRequest::new(address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve ESDT roles for address {bech32_address}: {err}")
});
let account_storage = api
.request(GetAccountStorageRequest::new(address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve storage for address {bech32_address}: {err}")
});

(account_esdt, account_esdt_roles, account_storage)
};
let account_esdt = api
.request(GetAccountEsdtTokensRequest::new(address))
.await
.unwrap_or_else(|err| {
eprintln!("failed to retrieve ESDT tokens for address {bech32_address}: {err}");
HashMap::new()
});
let account_esdt_roles = api
.request(GetAccountEsdtRolesRequest::new(address))
.await
.unwrap_or_else(|err| {
eprintln!("failed to retrieve ESDT roles for address {bech32_address}: {err}");
HashMap::new()
});
let account_storage = api
.request(GetAccountStorageRequest::new(address))
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve storage for address {bech32_address}: {err}")
});

let account_state = set_account(
sdk_account,
Expand All @@ -90,7 +82,7 @@ fn set_account(
let mut account_state = Account::new()
.nonce(account.nonce)
.balance(account.balance.as_str())
.code(account.code);
.code(BytesValue::from_hex(&account.code));
account_state.username = Some(format!("str:{}", account.username.as_str()).into());
account_state.storage = convert_storage(account_storage);

Expand All @@ -112,6 +104,6 @@ fn convert_storage(account_storage: HashMap<String, String>) -> BTreeMap<BytesKe
account_storage
.into_iter()
.filter(|(k, _)| !k.starts_with("454c524f4e44"))
.map(|(k, v)| (BytesKey::from(k.as_str()), BytesValue::from(v)))
.map(|(k, v)| (BytesKey::from_hex(&k), BytesValue::from_hex(&v)))
.collect()
}
7 changes: 1 addition & 6 deletions framework/snippets-base/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ where
}

pub async fn retrieve_account(&mut self, wallet_address: &Bech32Address) {
let set_state = retrieve_account_as_scenario_set_state(
&self.proxy,
self.use_chain_simulator,
wallet_address,
)
.await;
let set_state = retrieve_account_as_scenario_set_state(&self.proxy, wallet_address).await;
self.pre_runners.run_set_state_step(&set_state);
self.post_runners.run_set_state_step(&set_state);
}
Expand Down

0 comments on commit f88c38e

Please sign in to comment.