Skip to content

Commit

Permalink
Deploy many instances
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Jun 20, 2023
1 parent 6255bd2 commit 07caa07
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
7 changes: 6 additions & 1 deletion drink-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ pub enum CliCommand {
#[clap(alias = "b")]
Build,
#[clap(alias = "d")]
Deploy,
Deploy {
#[clap(default_value = "new")]
constructor: String,
#[clap(default_values_t = Vec::<u8>::new())]
salt: Vec<u8>,
},
CallGet,
CallFlip,
}
Expand Down
54 changes: 29 additions & 25 deletions drink-cli/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{env, process::Command};

use anyhow::Result;
use clap::Parser;
use sp_runtime::AccountId32;
use sp_runtime::app_crypto::sp_core::blake2_256;

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

Expand Down Expand Up @@ -39,14 +39,9 @@ pub fn execute(app_state: &mut AppState) -> Result<()> {
}
}

CliCommand::Build => {
build_contract(app_state);
}
CliCommand::Deploy => {
// account_id = Some(deploy_contract(&mut sandbox));
app_state.chain_info.deployed_contracts += 1;
app_state.chain_info.current_contract_address = Some(AccountId32::new([0; 32]));
}
CliCommand::Build => build_contract(app_state),
CliCommand::Deploy { constructor, salt } => deploy_contract(app_state, constructor, salt),

CliCommand::CallGet => {
// let account_id = match account_id {
// Some(ref account_id) => account_id.clone(),
Expand Down Expand Up @@ -83,19 +78,28 @@ fn build_contract(app_state: &mut AppState) {
}
}

// fn deploy_contract(sandbox: &mut Sandbox) -> AccountId32 {
// println!("Deploying contract...");
//
// let contract_bytes_path = env::current_dir()
// .expect("Failed to get current directory")
// .join(CONTRACT_DIR)
// .join("target/ink/example.wasm");
// let contract_bytes = std::fs::read(contract_bytes_path).expect("Failed to read contract bytes");
//
// let account_id = sandbox.deploy_contract(contract_bytes);
//
// println!("Contract deployed successfully");
//
// account_id
// }
//
fn deploy_contract(app_state: &mut AppState, constructor: String, salt: Vec<u8>) {
let contract_bytes_path = app_state.ui_state.pwd.join("target/ink/example.wasm");
let contract_bytes = match std::fs::read(contract_bytes_path) {
Ok(bytes) => bytes,
Err(err) => {
app_state.print_error(&format!("Failed to read contract bytes\n{err}"));
return;
}
};

let account_id =
app_state
.sandbox
.deploy_contract(contract_bytes, compute_selector(constructor), salt);

app_state.print("Contract deployed successfully");

app_state.chain_info.deployed_contracts += 1;
app_state.chain_info.current_contract_address = Some(account_id);
}

fn compute_selector(name: String) -> Vec<u8> {
let name = name.as_bytes();
blake2_256(&name)[..4].to_vec()

Check failure on line 104 in drink-cli/src/executor.rs

View workflow job for this annotation

GitHub Actions / Run check, test and lints

this expression creates a reference which is immediately dereferenced by the compiler
}
11 changes: 8 additions & 3 deletions drink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,21 @@ impl Sandbox {
}
}

pub fn deploy_contract(&mut self, contract_bytes: Vec<u8>) -> AccountId32 {
pub fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
selector: Vec<u8>,
salt: Vec<u8>,
) -> AccountId32 {
self.externalities.execute_with(|| {
let result = Contracts::bare_instantiate(
ALICE,
0,
GAS_LIMIT,
None,
Code::Upload(contract_bytes),
vec![155, 174, 157, 94],
Default::default(),
selector,
salt,
true,
);
let result = result.result.unwrap();
Expand Down

0 comments on commit 07caa07

Please sign in to comment.