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

Be generic over account and block number #47

Merged
merged 8 commits into from
Sep 6, 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
25 changes: 13 additions & 12 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.1.3"
version = "0.1.4"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
clap = { version = "4.3.4" }
contract-build = { version = "3.0.1" }
contract-transcode = { version = "3.0.1" }
crossterm = { version = "0.26.0" }
parity-scale-codec = { version = "3.6.0" }
parity-scale-codec = { version = "=3.6.5" }
parity-scale-codec-derive = { version = "=3.6.5" }
ratatui = { version = "0.21.0" }
scale-info = { version = "2.5.0" }
thiserror = { version = "1.0.40" }
Expand All @@ -47,4 +48,4 @@ sp-runtime-interface = { version = "18.0.0" }

# Local dependencies

drink = { version = "0.1.3", path = "drink" }
drink = { version = "0.1.4", path = "drink" }
24 changes: 14 additions & 10 deletions drink-cli/src/app_state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{env, path::PathBuf};

pub use contracts::{Contract, ContractIndex, ContractRegistry};
use drink::{runtime::MinimalRuntime, session::Session, Weight, DEFAULT_ACTOR, DEFAULT_GAS_LIMIT};
use drink::{
runtime::{MinimalRuntime, Runtime},
session::Session,
Weight, DEFAULT_GAS_LIMIT,
};
use sp_core::crypto::AccountId32;
pub use user_input::UserInput;

Expand All @@ -14,7 +18,7 @@ mod user_input;

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct ChainInfo {
pub block_height: u64,
pub block_height: u32,
pub actor: AccountId32,
pub gas_limit: Weight,
}
Expand All @@ -23,7 +27,7 @@ impl Default for ChainInfo {
fn default() -> Self {
Self {
block_height: 0,
actor: DEFAULT_ACTOR,
actor: MinimalRuntime::default_actor(),
gas_limit: DEFAULT_GAS_LIMIT,
}
}
Expand All @@ -38,7 +42,7 @@ pub enum Mode {

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct UiState {
pub pwd: PathBuf,
pub cwd: PathBuf,
pub mode: Mode,

pub user_input: UserInput,
Expand All @@ -48,12 +52,12 @@ pub struct UiState {
}

impl UiState {
pub fn new(pwd_override: Option<PathBuf>) -> Self {
let pwd = pwd_override.unwrap_or_else(
|| env::current_dir().expect("Failed to get current directory"));
pub fn new(cwd_override: Option<PathBuf>) -> Self {
let cwd = cwd_override
.unwrap_or_else(|| env::current_dir().expect("Failed to get current directory"));

UiState {
pwd,
cwd,
mode: Default::default(),
user_input: Default::default(),
output: Default::default(),
Expand All @@ -76,11 +80,11 @@ pub struct AppState {
}

impl AppState {
pub fn new(pwd_override: Option<PathBuf>) -> Self {
pub fn new(cwd_override: Option<PathBuf>) -> Self {
AppState {
session: Session::new(None).expect("Failed to create drinking session"),
chain_info: Default::default(),
ui_state: UiState::new(pwd_override),
ui_state: UiState::new(cwd_override),
contracts: Default::default(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion drink-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum CliCommand {
#[clap(alias = "nb")]
NextBlock {
#[clap(default_value = "1")]
count: u64,
count: u32,
},
AddTokens {
#[clap(value_parser = AccountId32::from_ss58check)]
Expand Down
12 changes: 6 additions & 6 deletions drink-cli/src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};

fn build_result(app_state: &mut AppState) -> Result<String, BuildError> {
let path_to_cargo_toml = app_state.ui_state.pwd.join(Path::new("Cargo.toml"));
let path_to_cargo_toml = app_state.ui_state.cwd.join(Path::new("Cargo.toml"));
let manifest_path = ManifestPath::new(path_to_cargo_toml.clone()).map_err(|err| {
BuildError::InvalidManifest {
manifest_path: path_to_cargo_toml,
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn build(app_state: &mut AppState) {

pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec<String>, salt: Vec<u8>) {
// Get raw contract bytes
let Some((contract_name, contract_file)) = find_wasm_blob(&app_state.ui_state.pwd) else {
let Some((contract_name, contract_file)) = find_wasm_blob(&app_state.ui_state.cwd) else {
app_state.print_error("Failed to find contract file");
return;
};
Expand All @@ -64,7 +64,7 @@ pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec<String>,
// Read contract metadata and prepare transcoder
let metadata_path = app_state
.ui_state
.pwd
.cwd
.join(format!("target/ink/{contract_name}.json"));

let Ok(transcoder) = ContractMessageTranscoder::load(metadata_path) else {
Expand All @@ -82,7 +82,7 @@ pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec<String>,
app_state.contracts.add(Contract {
name: contract_name,
address,
base_path: app_state.ui_state.pwd.clone(),
base_path: app_state.ui_state.cwd.clone(),
transcoder,
});
app_state.print("Contract deployed successfully");
Expand Down Expand Up @@ -119,8 +119,8 @@ pub fn call(app_state: &mut AppState, message: String, args: Vec<String>) {
}
}

fn find_wasm_blob(pwd: &Path) -> Option<(String, PathBuf)> {
let Ok(entries) = fs::read_dir(pwd.join("target/ink")) else {
fn find_wasm_blob(cwd: &Path) -> Option<(String, PathBuf)> {
let Ok(entries) = fs::read_dir(cwd.join("target/ink")) else {
return None;
};
let Some(file) = entries
Expand Down
6 changes: 4 additions & 2 deletions drink-cli/src/executor/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use thiserror::Error;


#[derive(Error, Debug)]
pub(crate) enum BuildError {
#[error("Invalid manifest path {manifest_path}: {err}")]
InvalidManifest { manifest_path: std::path::PathBuf, err: anyhow::Error },
InvalidManifest {
manifest_path: std::path::PathBuf,
err: anyhow::Error,
},
#[error("Contract build failed: {err}")]
BuildFailed { err: anyhow::Error },
#[error("Wasm code artifact not generated")]
Expand Down
6 changes: 3 additions & 3 deletions drink-cli/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub fn execute(app_state: &mut AppState) -> Result<()> {
match cli_command {
CliCommand::Clear => app_state.ui_state.output.clear(),
CliCommand::ChangeDir { path } => {
let target_dir = app_state.ui_state.pwd.join(path);
let target_dir = app_state.ui_state.cwd.join(path);
match env::set_current_dir(target_dir) {
Ok(_) => {
app_state.ui_state.pwd =
app_state.ui_state.cwd =
env::current_dir().expect("Failed to get current directory");
app_state.print("Directory changed");
}
Expand Down Expand Up @@ -66,7 +66,7 @@ pub fn execute(app_state: &mut AppState) -> Result<()> {
Ok(())
}

fn build_blocks(app_state: &mut AppState, count: u64) {
fn build_blocks(app_state: &mut AppState, count: u32) {
app_state.chain_info.block_height = app_state
.session
.chain_api()
Expand Down
5 changes: 3 additions & 2 deletions drink-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

use crate::ui::run_ui;

Expand All @@ -13,7 +14,7 @@ mod ui;
#[command(author, version, about, long_about = None)]
struct Args {
/// Starts the CLI in the provided directory
#[arg(short, long, value_name = "DIRECTORY", )]
#[arg(short, long, value_name = "DIRECTORY")]
path: Option<PathBuf>,
}

Expand Down
2 changes: 1 addition & 1 deletion drink-cli/src/ui/current_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Block height: {}
Deployed contracts: {}
Current actor: {}
Current contract: {{ {} }}"#,
app_state.ui_state.pwd.to_str().unwrap(),
app_state.ui_state.cwd.to_str().unwrap(),
app_state.chain_info.block_height,
app_state.contracts.count(),
app_state.chain_info.actor,
Expand Down
11 changes: 5 additions & 6 deletions drink-cli/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ mod layout;
mod output;
mod user_input;

use std::{io, io::Stdout};
use std::path::PathBuf;
use std::{io, io::Stdout, path::PathBuf};

use anyhow::{anyhow, Result};
use crossterm::{
Expand All @@ -29,9 +28,9 @@ use crate::{

type Terminal = ratatui::Terminal<CrosstermBackend<Stdout>>;

pub fn run_ui(pwd: Option<PathBuf>) -> Result<()> {
pub fn run_ui(cwd: Option<PathBuf>) -> Result<()> {
let mut terminal = setup_dedicated_terminal()?;
let app_result = run_ui_app(&mut terminal, pwd);
let app_result = run_ui_app(&mut terminal, cwd);
restore_original_terminal(terminal)?;
app_result
}
Expand All @@ -54,8 +53,8 @@ fn restore_original_terminal(mut terminal: Terminal) -> Result<()> {
terminal.show_cursor().map_err(|e| anyhow!(e))
}

fn run_ui_app(terminal: &mut Terminal, pwd_override: Option<PathBuf>) -> Result<()> {
let mut app_state = AppState::new(pwd_override);
fn run_ui_app(terminal: &mut Terminal, cwd_override: Option<PathBuf>) -> Result<()> {
let mut app_state = AppState::new(cwd_override);

loop {
terminal.draw(|f| layout(f, &mut app_state))?;
Expand Down
1 change: 1 addition & 0 deletions drink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pallet-contracts = { workspace = true }
pallet-contracts-primitives = { workspace = true }
pallet-timestamp = { workspace = true }
parity-scale-codec = { workspace = true }
parity-scale-codec-derive = { workspace = true }
sp-externalities = { workspace = true }
sp-runtime-interface = { workspace = true }

Expand Down
Loading
Loading