Skip to content

Commit

Permalink
Be aware of the current contract
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Jun 21, 2023
1 parent 9a60fd8 commit 9172484
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
13 changes: 11 additions & 2 deletions drink-cli/src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, path::PathBuf};
use std::{collections::VecDeque, env, path::PathBuf};

use drink::Sandbox;
use ratatui::text::Line;
Expand All @@ -8,7 +8,6 @@ use sp_runtime::AccountId32;
pub struct ChainInfo {
pub block_height: u64,
pub deployed_contracts: u16,
pub current_contract_address: Option<AccountId32>,
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
Expand All @@ -21,6 +20,7 @@ pub enum Mode {
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct UiState {
pub pwd: PathBuf,
pub contract_project_name: String,
pub mode: Mode,

pub user_input: String,
Expand All @@ -35,6 +35,7 @@ impl Default for UiState {
fn default() -> Self {
UiState {
pwd: env::current_dir().expect("Failed to get current directory"),
contract_project_name: "".to_string(),
mode: Default::default(),
user_input: Default::default(),
show_help: false,
Expand All @@ -45,9 +46,17 @@ impl Default for UiState {
}
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct Contract {
pub name: String,
pub address: AccountId32,
pub base_path: PathBuf,
}

#[derive(Default)]
pub struct AppState {
pub sandbox: Sandbox,
pub chain_info: ChainInfo,
pub ui_state: UiState,
pub contracts: VecDeque<Contract>,
}
28 changes: 22 additions & 6 deletions drink-cli/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use clap::Parser;
use drink::chain_api::ChainApi;
use sp_runtime::{app_crypto::sp_core::blake2_256, AccountId32};

use crate::{app_state::AppState, cli::CliCommand};
use crate::{
app_state::{AppState, Contract},
cli::CliCommand,
};

pub fn execute(app_state: &mut AppState) -> Result<()> {
let command = app_state.ui_state.user_input.clone();
Expand Down Expand Up @@ -78,7 +81,16 @@ fn deploy_contract(app_state: &mut AppState, constructor: String, salt: Vec<u8>)
app_state.print_error("Failed to find contract file");
return;
}
Some(file) => file.path(),
Some(file) => {
app_state.ui_state.contract_project_name = file
.file_name()
.to_str()
.unwrap()
.strip_suffix(".wasm")
.unwrap()
.to_string();
file.path()
}
}
}
Err(err) => {
Expand All @@ -87,7 +99,7 @@ fn deploy_contract(app_state: &mut AppState, constructor: String, salt: Vec<u8>)
}
};

let contract_bytes = match std::fs::read(contract_bytes_path) {
let contract_bytes = match fs::read(contract_bytes_path) {
Ok(bytes) => bytes,
Err(err) => {
app_state.print_error(&format!("Failed to read contract bytes\n{err}"));
Expand All @@ -103,12 +115,16 @@ fn deploy_contract(app_state: &mut AppState, constructor: String, salt: Vec<u8>)
app_state.print("Contract deployed successfully");

app_state.chain_info.deployed_contracts += 1;
app_state.chain_info.current_contract_address = Some(account_id);
app_state.contracts.push_front(Contract {
name: app_state.ui_state.contract_project_name.clone(),
address: account_id,
base_path: app_state.ui_state.pwd.clone(),
});
}

fn call_contract(app_state: &mut AppState, message: String) {
let account_id = match app_state.chain_info.current_contract_address {
Some(ref account_id) => account_id.clone(),
let account_id = match app_state.contracts.get(0).map(|c| &c.address) {
Some(account_id) => account_id.clone(),
None => {
app_state.print_error("No deployed contract");
return;
Expand Down
18 changes: 12 additions & 6 deletions drink-cli/src/ui/current_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ pub(super) fn build(app_state: &mut AppState) -> impl Widget {
.border_type(BorderType::Rounded)
.padding(Padding::horizontal(1));

let current_contract_info = match app_state.contracts.get(0) {
Some(contract) => format!(
"name: {} | address: {} | sources: {}",
contract.name,
contract.address,
contract.base_path.to_str().unwrap()
),
None => "No deployed contract".to_string(),
};

Paragraph::new(format!(
r#"Current working directory: {}
Block height: {}
Deployed contracts: {}
Current contract address: {}"#,
Current contract: {{ {} }}"#,
app_state.ui_state.pwd.to_str().unwrap(),
app_state.chain_info.block_height,
app_state.chain_info.deployed_contracts,
app_state
.chain_info
.current_contract_address
.as_ref()
.map_or("<none>".to_string(), |addr| format!("{addr}")),
current_contract_info
))
.alignment(Alignment::Left)
.wrap(Wrap { trim: false })
Expand Down

0 comments on commit 9172484

Please sign in to comment.