drt_sc_snippets/
interactor.rs1use drt_sc_scenario::{
2 imports::{Bech32Address, ScenarioRunner},
3 denali_system::{run_list::ScenarioRunnerList, run_trace::ScenarioTraceFile},
4 drt_sc::types::Address,
5 scenario_model::AddressValue,
6};
7use drt_sdk::{
8 data::{address::Address as MoarsAddress, network_config::NetworkConfig},
9 gateway::GatewayProxy,
10 wallet::Wallet,
11};
12use std::{
13 collections::HashMap,
14 path::{Path, PathBuf},
15 time::Duration,
16};
17
18use crate::{account_tool::retrieve_account_as_scenario_set_state, Sender};
19
20pub const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json";
21
22pub struct Interactor {
23 pub proxy: GatewayProxy,
24 pub network_config: NetworkConfig,
25 pub sender_map: HashMap<Address, Sender>,
26
27 pub(crate) waiting_time_ms: u64,
28 pub pre_runners: ScenarioRunnerList,
29 pub post_runners: ScenarioRunnerList,
30
31 pub current_dir: PathBuf,
32}
33
34impl Interactor {
35 pub async fn new(gateway_url: &str) -> Self {
36 let proxy = GatewayProxy::new(gateway_url.to_string());
37 let network_config = proxy.get_network_config().await.unwrap();
38 Self {
39 proxy,
40 network_config,
41 sender_map: HashMap::new(),
42 waiting_time_ms: 0,
43 pre_runners: ScenarioRunnerList::empty(),
44 post_runners: ScenarioRunnerList::empty(),
45 current_dir: PathBuf::default(),
46 }
47 }
48
49 pub fn register_wallet(&mut self, wallet: Wallet) -> Address {
50 let address = moars_address_to_h256(wallet.address());
51 self.sender_map.insert(
52 address.clone(),
53 Sender {
54 address: address.clone(),
55 wallet,
56 current_nonce: None,
57 },
58 );
59 address
60 }
61
62 pub async fn sleep(&mut self, duration: Duration) {
63 self.waiting_time_ms += duration.as_millis() as u64;
64 tokio::time::sleep(duration).await;
65 }
66
67 pub async fn with_tracer<P: AsRef<Path>>(mut self, path: P) -> Self {
68 self.post_runners.push(ScenarioTraceFile::new(path));
69 self
70 }
71
72 pub async fn retrieve_account(&mut self, wallet_address: &Bech32Address) {
73 let set_state = retrieve_account_as_scenario_set_state(&self.proxy, wallet_address).await;
74 self.pre_runners.run_set_state_step(&set_state);
75 self.post_runners.run_set_state_step(&set_state);
76 }
77}
78
79pub(crate) fn denali_to_moars_address(denali_address: &AddressValue) -> MoarsAddress {
80 let bytes = denali_address.value.as_array();
81 MoarsAddress::from_bytes(*bytes)
82}
83
84pub(crate) fn address_h256_to_moars(address: &Address) -> MoarsAddress {
85 let bytes = address.as_array();
86 MoarsAddress::from_bytes(*bytes)
87}
88
89pub(crate) fn moars_address_to_h256(moars_address: MoarsAddress) -> Address {
90 moars_address.to_bytes().into()
91}