drt_sc_snippets/interactor_scenario/
interactor_sc_deploy.rs

1use crate::{denali_to_moars_address, network_response, Interactor};
2use log::info;
3use drt_sc_scenario::{
4    imports::Bech32Address,
5    denali_system::ScenarioRunner,
6    scenario_model::{ScDeployStep, SetStateStep},
7};
8use drt_sdk::{
9    data::{address::Address as MoarsAddress, transaction::Transaction},
10    utils::base64_encode,
11};
12
13const DEPLOY_RECEIVER: [u8; 32] = [0u8; 32];
14
15impl Interactor {
16    pub(crate) fn sc_deploy_to_blockchain_tx(&self, sc_deploy_step: &ScDeployStep) -> Transaction {
17        Transaction {
18            nonce: 0,
19            value: sc_deploy_step.tx.rewa_value.value.to_string(),
20            sender: denali_to_moars_address(&sc_deploy_step.tx.from),
21            receiver: MoarsAddress::from_bytes(DEPLOY_RECEIVER),
22            gas_price: self.network_config.min_gas_price,
23            gas_limit: sc_deploy_step.tx.gas_limit.value,
24            data: Some(base64_encode(sc_deploy_step.tx.to_tx_data())),
25            signature: None,
26            chain_id: self.network_config.chain_id.clone(),
27            version: self.network_config.min_transaction_version,
28            options: 0,
29        }
30    }
31
32    pub async fn launch_sc_deploy(&mut self, sc_deploy_step: &mut ScDeployStep) -> String {
33        self.pre_runners.run_sc_deploy_step(sc_deploy_step);
34
35        let sender_address = &sc_deploy_step.tx.from.value;
36        let mut transaction = self.sc_deploy_to_blockchain_tx(sc_deploy_step);
37        self.set_nonce_and_sign_tx(sender_address, &mut transaction)
38            .await;
39        let tx_hash = self
40            .proxy
41            .send_transaction(&transaction)
42            .await
43            .expect("error sending tx (possible API failure)");
44        println!("sc deploy tx hash: {tx_hash}");
45        info!("sc deploy tx hash: {}", tx_hash);
46
47        tx_hash
48    }
49
50    pub async fn sc_deploy<S>(&mut self, mut sc_deploy_step: S)
51    where
52        S: AsMut<ScDeployStep>,
53    {
54        let sc_deploy_step = sc_deploy_step.as_mut();
55        let tx_hash = self.launch_sc_deploy(sc_deploy_step).await;
56        let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await;
57
58        let addr = sc_deploy_step.tx.from.clone();
59        let nonce = tx.nonce;
60        sc_deploy_step.save_response(network_response::parse_tx_response(tx));
61
62        let deploy_address = sc_deploy_step
63            .response()
64            .new_deployed_address
65            .clone()
66            .unwrap();
67        let deploy_address_bech32 = Bech32Address::from(deploy_address);
68
69        let set_state_step = SetStateStep::new().new_address(addr, nonce, &deploy_address_bech32);
70
71        println!("deploy address: {deploy_address_bech32}");
72        self.pre_runners.run_set_state_step(&set_state_step);
73        self.post_runners.run_set_state_step(&set_state_step);
74
75        self.post_runners.run_sc_deploy_step(sc_deploy_step);
76    }
77}